<HTML>
<HEAD>
<TITLE>Computer Science Logo Style vol 2 ch 5: Program as Data</TITLE>
</HEAD>
<BODY>
<CITE>Computer Science Logo Style</CITE> volume 2:
<CITE>Advanced Techniques</CITE> 2/e Copyright (C) 1997 MIT
<H1>Program as Data</H1>

<TABLE width="100%"><TR><TD>
<IMG SRC="../csls2.jpg" ALT="cover photo">
<TD><TABLE>
<TR><TD align="right"><CITE><A HREF="http://www.cs.berkeley.edu/~bh/">Brian
Harvey</A><BR>University of California, Berkeley</CITE>
<TR><TD align="right"><BR>
<TR><TD align="right"><A HREF="../pdf/v2ch05.pdf">Download PDF version</A>
<TR><TD align="right"><A HREF="../v2-toc2.html">Back to Table of Contents</A>
<TR><TD align="right"><A HREF="../v2ch4/v2ch4.html"><STRONG>BACK</STRONG></A>
chapter thread <A HREF="../v2ch6/v2ch6.html"><STRONG>NEXT</STRONG></A>
<TR><TD align="right"><A HREF="https://mitpress.mit.edu/books/computer-science-logo-style-second-edition-volume-2">MIT
Press web page for <CITE>Computer Science Logo Style</CITE></A>
</TABLE></TABLE>

<HR>

<P>
In most programming languages there is a sharp distinction between
<EM>program</EM> and <EM>data.</EM>  Data are the things you can
manipulate in your program, things like numbers and letters.  These
things live in variables, which can be given new values by your
program.  But the program itself is not subject to manipulation; it's
something you write ahead of time, and then it remains fixed.

<P>
In Logo the distinction is not so sharp.  We've made extensive use of one
mechanism by which a program can manipulate itself: the instruction lists
that are used as inputs to <CODE>run</CODE>, <CODE>if</CODE>, and so on are
data that can be computed by a program.  For example, the solitaire program
in Chapter 4 constructs a list of Logo instruction lists, each of
which would move a card to some other legal position, and then says

<PRE>
run first :onto
</PRE>

<P>
to move the card to the first such position.

<H2><CODE>Text</CODE> and <CODE>Define</CODE></H2>

<P>
In this chapter we'll use a pair of more advanced tools that allow a
program to create more program.  <CODE>Run</CODE> deals with a single
<EM>instruction;</EM> now we'll be able to examine and create
<EM>procedures.</EM>

<P>
<CODE>Text</CODE> is an operation that takes one input, a word.  That word
must be the name of a user-defined procedure.  The output from
<CODE>text</CODE> is a list.  The first member of that list is a list
containing the names of the inputs to the chosen procedure.  (If the
procedure has no inputs, the list will be empty.)*  The remaining
members of the output list are instruction lists, one for each line in the
definition of the procedure.

<P>
<SMALL><BLOCKQUOTE><SMALL><SUP>*</SUP>Berkeley Logo allows user-defined
procedures with <EM>optional</EM> inputs.  For such a procedure, this first
sublist may contain lists, representing optional inputs, as well as words,
representing required inputs.</SMALL></BLOCKQUOTE></SMALL>

<P>
Here is an example.  Suppose we've defined the procedure

<PRE>
to opinion :yes :no
print sentence [I like] :yes
print sentence [I hate] :no
end
</PRE>

<P>
Here's what the text of that procedure looks like:

<PRE>
? <U>show text "opinion</U>
[[yes no] [print sentence [I like] :yes] [print sentence [I hate] :no]]
</PRE>

<P>
In this example the output from <CODE>text</CODE> is a list with three
members.  The first member is a list containing the words <CODE>yes</CODE>
and <CODE>no</CODE>, the names of <CODE>opinion</CODE>'s inputs.  (Note that
the colons that are used to indicate inputs in a title line are <EM>not</EM>
used here.) The second and third members of the ou<style>pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */</style><div class="highlight"><pre><span></span># Compatible with ranger 1.6.0 through 1.7.*
#
# Replace your rifle.conf with this file to use xdg-open as your file opener.
# This is, of course, adaptable for use with any other file opener.
else = xdg-open &quot;$1&quot;

# You need an &quot;editor&quot; and &quot;pager&quot; in order to use certain functions in ranger:
label editor = &quot;$EDITOR&quot; -- &quot;$@&quot;
label pager  = &quot;$PAGER&quot; -- &quot;$@&quot;
</pre></div>
</code></pre></td></tr></table>
</div> <!-- class=content -->
<div class='footer'>generated by <a href='https://git.causal.agency/cgit-pink/about/'>cgit-pink 1.4.1-2-gfad0</a> (<a href='https://git-scm.com/'>git 2.36.2.497.gbbea4dcf42</a>) at 2024-12-30 21:18:14 +0000</div>
</div> <!-- id=cgit -->
</body>
</html>
orward 20]] [B [back 20]]
                 [L [left 15]] [R [right 15]] [P [pens]]]
onekey "pens [[U [penup stop]] [D [pendown stop]] [E [penerase stop]]]
</PRE>

<P>
With these definitions, typing <CODE>P</CODE> to <CODE>instant</CODE>
prepares to accept a pen command from the second list.  In effect,
<CODE>instant</CODE> recognizes two-letter commands <CODE>PU</CODE> for <CODE>penup</CODE>
and so on, except that the sequence <CODE>P?</CODE> will display the help
information for just the pen commands.  Here's another example:

<PRE>
onekey "tinyturns [[F [forward 20]] [B [back 20]]
                   [L [left 5]] [R [right 5]] [H [hugeturns]]]
onekey "hugeturns [[F [forward 20]] [B [back 20]]
                   [L [left 45]] [R [right 45]] [T [tinyturns]]]
</PRE>

<H2>Procedure Cross-Reference Listings</H2>

<P>
When you're working on a very large project, it's easy to lose track
of which procedure invokes which other one.  We can use the computer
to help solve this problem by
creating a <EM>cross-reference listing</EM> for all
the procedures in a project.  For every procedure
in the project, a cross-reference listing tells which other procedures
invoke that one.  If you write long procedures, it can also be helpful
to list which instruction line in procedure <CODE>A</CODE> invokes procedure
<CODE>B</CODE>.

<P>
The general strategy will be to look through the <CODE>text</CODE> of every
procedure, looking for the name of the procedure we're interested in.
Suppose we're finding all the references to procedure <CODE>X</CODE> and
we're looking through procedures <CODE>A</CODE>, <CODE>B</CODE>, and
<CODE>C</CODE>.  For each line of each procedure, we want to know whether
the word <CODE>X</CODE> appears in that line.  (Of course you would not
really name a procedure <CODE>A</CODE> or <CODE>X</CODE>.  You'd use
meaningful names.  This is just an example.)  We can't, however, just test

<PRE>
memberp "x :instr
</PRE>

<P>
(I'm imagining that the variable <CODE>instr</CODE> contains an
instruction line.) The reason is that a procedure invocation can be
part of a <EM>sublist</EM> of the instruction list if <CODE>X</CODE> is
invoked by way of something like <CODE>if</CODE>.  For example, the word
<CODE>X</CODE> is not a member of the list

<PRE>
[if emptyp :list [x :foo stop]]
</PRE>

<P>
But it's a member of a member.  (Earlier I made a big fuss about the fact
that if that instruction were part of procedure <CODE>A</CODE>, it's
actually <CODE>if</CODE> that invokes <CODE>X</CODE>, not <CODE>A</CODE>.
That's the true story, for the Logo interpreter.  But for purposes of a
cross-reference listing, it does us no good to know that <CODE>if</CODE>
invokes <CODE>X</CODE>; what we want to know is which procedure definition to
look at if we want to find the instruction that uses <CODE>X</CODE>.)

<P>
So the first thing we need is a procedure <CODE>submemberp</CODE> that takes
inputs like those of <CODE>memberp</CODE> but outputs <CODE>true</CODE> if the
first input is a member of the second, or a member of a member, and so
on.

<PRE>
to submemberp :thing :list
if emptyp :list [output "false]
if equalp :thing first :list [output "true]
if listp first :list ~
   [if submemberp :thing first :list [output "true]]
output submemberp :thing butfirst :list
end
</PRE>

<P>
Now we want a procedure that will take two words as input, both of
which are the names of procedures, and will print a list of all the
references to the first procedure in the text of the second.

<PRE>
to reference :target :examinee
ref1 :target :examinee butfirst text :examinee 1
end

to ref1 :target :examinee :instrs :linenum
if emptyp :instrs [stop]
if submemberp :target first :instrs ~
   [print sentence "|   | (word :examinee "\( :linenum "\) )]
ref1 :target :examinee butfirst :instrs :linenum+1
end
</PRE>

<P>
<CODE>Reference</CODE> uses <CODE>butfirst text :examinee</CODE> as the third
input to <CODE>ref1</CODE> to avoid the list of inputs to the procedure we're
examining.  That's because one of those inputs might have the same name as
the <CODE>target</CODE> procedure, and we'd get a false indication of
success.  (In the body of the definition of <CODE>:examinee</CODE>, any
reference to a variable named <CODE>X</CODE> will not use the word
<CODE>X</CODE> but rather the word <CODE>"X</CODE> or the word
<CODE>:X</CODE>.  You may find that statement confusing.  When you type an
<EM>instruction</EM> like

<PRE>
print "foo
</PRE>

<P>
the Logo evaluator interprets <CODE>"foo</CODE> as a request for the word
<CODE>foo</CODE>, quoted (as opposed to evaluated).  So <CODE>print</CODE>
won't print a quotation mark.  But if we look at the <EM>list</EM>

<PRE>
[print "foo]
</PRE>

<P>
then we are not, right now, evaluating it as a Logo
instruction.  The second member of that list is the word <CODE>"foo</CODE>,
quote mark and all.)

<P>
We can still get &quot;false hits,&quot; finding the word <CODE>X</CODE>
(or whatever procedure name we're looking for) in an instruction list,
but not being used as a procedure name:

<PRE>
print [w x y z]
</PRE>

<P>
But cases like that will be relatively rare compared to the
cases of variables and procedures with the same name.

<P>
The reason I'm printing spaces before the information is that I'm
working toward a listing that will look like this:

<PRE>
target1
   proca(3)
   procb(1)
   procc(4)
target2
   procb(3)
   procb(4)
</PRE>

<P>
This means that the procedure named <CODE>target1</CODE> is invoked
in each of the procedures <CODE>proca</CODE>, <CODE>procb</CODE>, and <CODE>procc</CODE>;
procedure <CODE>target2</CODE> is invoked by <CODE>procb</CODE> on two different
instruction lines.

<P>
Okay, now we can find references to one specific procedure within the
text of another specific procedure.  Now we want to look for
references to one procedure within <EM>all</EM> the procedures making up
a project.

<PRE>
to xref :target :list
print :target
foreach :list [reference :target ?]
end
</PRE>

<P>
We're almost done.  Now we want to apply <CODE>xref</CODE> to every
procedure in the project.  This involves another run through the list
of projects:

<PRE>
to xrefall :list
foreach :list [xref ? :list]
end
</PRE>

<P>
To use this program to make a cross-reference listing of
itself, you'd say

<PRE>
xrefall [xrefall xref reference ref1 submemberp]
</PRE>

<P>
To cross-reference all of the procedures in your workspace,
you'd say

<PRE>
xrefall procedures
</PRE>

<P>
If you try this program on a project with a large number of procedures,
you should expect it to take a <EM>long</EM> time.  If there
are five procedures, we have to examine each of them for
references to each of them, so we invoke <CODE>reference</CODE> 25 times.  If
there are 10 procedures, we invoke <CODE>reference</CODE> 100 times!  In
general, the number of invocations is the square of the number of
procedures.  The fancy way to say this is that the program &quot;takes
quadratic time&quot; or that it &quot;behaves quadratically.&quot;

<P><A HREF="../v2-toc2.html">(back to Table of Contents)</A>
<P><A HREF="../v2ch4/v2ch4.html"><STRONG>BACK</STRONG></A>
chapter thread <A HREF="../v2ch6/v2ch6.html"><STRONG>NEXT</STRONG></A>

<P>
<ADDRESS>
<A HREF="../index.html">Brian Harvey</A>, 
<CODE>bh@cs.berkeley.edu</CODE>
</ADDRESS>
</BODY>
</HTML>