?he 'LOGO''Page %'
?fo 'Aaron Sloman'- % -'\*(DY'
.ce 2
POP11 AND LOGO
==============

The Edinburgh Lecture Notes on Artificial Intelligence by Bundy,
Burstall, Weir and Young contain a lot of material that is useful
for our course.

But all their programming examples use the language LOGO, not
POP11.  These notes are intended to help you read the LOGO
examples.

LOGO was originally developed in America for teaching children
programming.  There are now several different versions of the
language (just as POP11 is a version of the language POP2).  The
Edinburgh version of LOGO has much in common with POP11, with respect
to what you can do with it.  However, the two languages look very
different.  That is they use different syntax.

.ce2
Translating LOGO into POP11
---------------------------

The easiest way to explain LOGO is to show how to translate
it into POP11.  The LOGO examples are follwed by approximate POP11
equivalents.

1.  Assignments in LOGO go from right to left and use the word
    "MAKE" instead of "->".
 	LOGO			POP11
 	----			-----

    (a) MAKE 'X  3		3 -> X;
    (b) MAKE 'X  :Y		Y -> X;
    (c) MAKE 'X  (DOUBLE :Y)	DOUBLE(Y) -> X;
    (d) MAKE 'Y (:Y+:Y)		Y+Y -> Y;
    (e) MAKE 'Y (FIRST :L)	HD(L) -> Y;

2.  Variables, values, identifiers
.br
----------------------------------

Note that in LOGO the target variable in an assignment has 
to be preceded by the quote mark.  If you want to refer to the value
of a variable in LOGO you write a colon in front, as in (b) to (e)
above.

3.  Function application: procedure execution.
.br
----------------------------------------------
.br

In LOGO if a word is used without either the colon or the quote mark
(like MAKE, DOUBLE, and FIRST in section 1 above), then that means
apply (or execute) the procedure named by the word.  In POP11 you have
to follow the word with parentheses (or precede it by a dot).

If a procedure is to be applied to arguments then in LOGO you simply
write the arguments after the procedure name, whereas in POP11 the
arguments go between parentheses.  LOGO does not use commas.
.br
Examples:
.tp10
 	LOGO			POP11
.br
 	----			-----

   	DOUBLE  :X		DOUBLE(X)
 	TYPE 'HELLO		PR("HELLO")
 	TYPE :X			PR(X)
 	ELEMENT 3 :L		ELEMENT(3,L)
 	ELEMENT SUM :X :Y :L	ELEMENT(SUM(X,Y),L)

In LOGO you can use parentheses, but they surround both the procedure
name and the arguments, as in LISP, e.g.
 	(ELEMENT (SUM :X :Y) :L)
.br
Hence the brackets are quite redundant.  So long as ELEMENT is a
procedure requiring two arguments and SUM is a procedure requiring 
two arguments, LOGO will accept either expression.  This is what 
Logicians call POLISH bracket-free prefix notation.

If the procedure requires no arguments then it is executed simply by
writing its name.
.br
E.g.	LOGO			POP11
 	----			-----
 	GREET			GREET();


4.  Defining procedures
.br
-----------------------

In LOGO the word "TO" means roughly the same as "FUNCTION"
in POP11.  However LOGO insists that you use line numbers (like EDIT
in POP11).  Here are some LOGO procedures and POP11 equivalents.
.br
(a)
 	TO DOUBLE 'X
 	10 RETURN (:X + :X)
 	END
.br
is the same as
 	FUNCTION DOUBLE (X);
 		X+X
 	END;

(b)
 	TO ELEMENT 'NUMBER 'LIST
 	10 IF  :NUMBER=1  THEN RESULT  FIRST :LIST
 	20 RESULT ELEMENT (:NUMBER-1) (BUTFIRST :LIST)
 	END
.br
is the same as:
.tp10
 	FUNCTION ELEMENT(NUMBER,LIST);
 		IF NUMBER=1  THEN HD(LIST)
 		ELSE
 			ELEMENT(NUMBER-1,TL(LIST))
 		CLOSE
 	END;
.tp7

(c)
 	TO COUNT 'LIST
 	10 IF  EMPTYQ  :LIST  THEN  RESULT 0
 	20 RESULT  SUM 1  COUNT  BUTFIRST  :LIST
 	END
.br
is the same as:
 	FUNCTION COUNT(LIST);
 		IF	LIST=[]
 		THEN	0
 		ELSE	1 + COUNT(TL(LIST))
 		CLOSE
 	END;
.br
or
 	FUNCTION COUNT(LIST);
 		IF	LIST=[]
 		THEN	0	EXIT;
 		1 + COUNT(TL(LIST))
 	END;

In LOGO the word "RESULT" (used interchangeably with "OUTPUT"
and "RETURN") indicates that the procedure is to halt and return a
result "STOP" simply causes the procedure to halt, like "RETURN" in
POP11
.br

(d)
 	TO MUCHLOVE 'NUMBER
 	10 REPEAT  :NUMBER  SAY  [I LOVE YOU]
 	END
.br
is the same as
.br
 	FUNCTION MUCHLOVE(NUMBER);
 		REPEAT NUMBER TIMES
 			PPR([I LOVE YOU])
 		CLOSE
 	END;
.br

5.  Declaring local variables
.br
-----------------------------

In POP11 we use VARS, whereas LOGO uses NEW.
.br
The line
 	10 NEW 'STRING
.br
in a LOGO procedure corresponds to 
.br
 	VARS STRING; 
.br
in POP11

In LOGO each variable requires a separate declaration
whereas in POP11 one "VARS" command can declare several
variables.

6.  Complex instructions
.br
------------------------
 
In POP11 you can put as many instructions as you like between THEN and 
CLOSE or between THEN and ELSE in an IF or LOOPIF statement, so long as
they are separated by semi-colons.  LOGO does not have a closing
bracket for IF, so if several things are to be done after THEN, the
word AND is used to join them up into one command.
.br
E.g., in LOGO
.br
.tp12
 	IF SUNNY  :DAY  THEN HANGOUT  :WASHING
 		AND WEED  :FLOWERBEDS
 		AND  SUNBATH
.br
means the same as
 	IF	SUNNY(DAY)
 	THEN	HANGOUT(WASHING);
 		WEED(FLOWERBEDS);
 		SUNBATH()
 	CLOSE;


7.  Lists
.br
---------

List constant brackets work the same way in LOGO as in POP11.  E.g.
 	[[A LIST] WITH [THREE ELEMENTS]]
.br
is a three element list in both.
.br
Instead of decorated list brackets, LOGO uses "<<" and ">>". 
.br
So in LOGO
 	<< :X :Y  FIRST :LIST  'CAT>>
.br
becomes, in POP11:
.br
 	[% X, Y,  HD(LIST),  "CAT" %]
.br
LOGO doesn't use [] for the empty list, it uses [] instead.  (You
can use this in POP11  too - it is interpreted as []).  To ask
if a list is empty in LOGO apply the procedure EMPTYQ to it.
.br
So in LOGO
 	IF EMPTYQ  :L  THEN  .....
.br
becomes, in POP11
.br
 	IF	L=[]	THEN	.....
.br
LOGO often uses "Q" at the end of a word  to indicate that it is the
name of a predicate, i.e. a procedure which produces the result TRUE
or FALSE.
.br
 	   FIRST in LOGO   =	HD in POP11
 	BUTFIRST in LOGO   =	TL
.br
Logo also supplies procedures called LAST and BUTLAST.  These are 
not standard POP11 - but may be added to the library.
.br
Note LOGO often uses abbreviations:
.br
e.g.
 	F for FIRST	BF for BUTFIRST
 	L for LAST	BL for BUTLAST.

Concatenating lists : 
.br
LOGO uses the procedure JOIN.  So
 	JOIN	:L1	:L2
.br
means the same as:
 	L1 <> L2
.br
and:
 	JOIN	F :LIST	BF :L
.br
means:	HD(LIST) <> TL(L)

For adding a single element onto a list, LOGO uses FPUT (short for 
FIRSTPUT),
.br
so
 	FPUT 'CAT BF [DOG ON MAT]
.br
is the same as the POP11 expression:
 	[%"CAT"%] <>TL([DOG ON MAT])
.br
or
 	"CAT" :: TL([DOG ON MAT])
.br
and results in the list [CAT ON MAT]

In LOGO you cannot assign to the HD or TL of a list.  So the following
POP11 instructions have no LOGO equivalent, except for the first line.
 	[4 4 5] -> L;
 	3 -> HD(L);
 	[] -> TL(TL(L));

Conclusion
.br
----------

There are several additional differences between LOGO and POP11 which
you may notice as you read the Edinburgh lecture notes.  In particular
LOGO does not provide strips, partial application, functions which take
functions as arguments, and does not allow functions to be treated as
objects (e.g. you can't make a list of functions - only a list
of numbers, words or lists).  We seriously considered using LOGO as
a teaching language at Sussex, but decided that although at first its
syntax may seem simpler, it is too restricted by comparison with POP2.
It is probably slightly easier to start learning LOGO,
but harder to go as far with it as with POP2.  This is why we finally
settled on POP2.  (However, POP11 has many minor differences from standard
POP2.)

Warning
.br
-------

Some of our reading material will include examples of LOGO.  You should
not assume that they all refer to Edinburgh LOGO.  In particular
other versions of LOGO probably do not include lists.  Instead they
contain objects called "sentences" which are roughly analogous to lists
of words.  There may be other differences making them even less like POP11
than Edinburgh LOGO is.

Possible project:
.br
-----------------

Write a LOGO interpreter in POP11.
