\documentstyle[a4,12pt]{article} %\pagestyle{empty} \setlength{\oddsidemargin}{-0.4cm} \setlength{\evensidemargin}{-0.4cm} \setlength{\textwidth}{6.8in} \setlength{\topmargin}{0.1cm} \setlength{\headheight}{0cm} \setlength{\headsep}{0cm} \setlength{\textheight}{9.5in} \def\sp{\\[0.1cm]} \def\Sp{\\[0.2cm]} \def\SP{\\[0.45cm]} \def\ps{\vspace*{-0.2 cm}} \def\Ps{\vspace*{-0.3 cm}} \def\PS{\vspace*{-0.7 cm}} \def\hs{\hspace*{1.0cm}} \begin{document} \parskip 0.15cm \parindent 0cm \begin{center} {\large \bf TEACH POPCORE Aaron Sloman September 1997} \end{center} This is a modified version of the Sussex local file HELP TPOP, by Mike Sharples, and is very close to the summary of Pop-11 in the book Computers and Thought edited by Sharples et al. However, it is more up to date, and reflects local extensions at Birmingham. This file lists a powerful subset of the words and constructs of Poplog Pop-11 which together are sufficient for a great many interesting programs. Those items not enclosed in angle brackets \verb;<...>; are Pop-11 reserved words (i.e. they have a special meaning to Pop-11). A relevant help or teach file is indicated by an asterisk, \\e.g. *\ MATCHES. To read the file place the cursor on the asterisk and type \verb;ESC h; or else do \verb;ENTER help matches; Sometimes the TEACH command will produce a different file, e.g. \verb;ENTER teach matches; {\bf CONTENTS -- Basic data types \\ -- Comments \\ -- Variable declarations \\ -- Miscellaneous \\ -- Printing facilities \\ -- Assignments \\ -- Inserting values in lists and vectors (using \verb;"^" and "^^";) \\ -- Defining procedures \\ -- Miscellaneous 2 \\ -- Arithmetic operators \\ -- Logical connectives \\ -- Variable formats for the matcher \\ -- Looping expressions \\ -- Arrays \\ -- Tracing utilities \\ -- FURTHER READING } \begin{verbatim} ITEM DESCRIPTION AND HELP FILE EXAMPLE ---- ------------------------- ------- \end{verbatim} {\large \bf Basic data types}\\ \PS \begin{verbatim} A letter followed by a series of "cat" letters or digits (including the "a_long_word" underscore). It may also be "M1" a series of signs such as $ "#$#$#$#" A word is put in double quotes, except within a list or vector expression. *WORDS \end{verbatim} \newpage \begin{verbatim} Can contain arbitrary characters. 'A funny %$%### string' Constructed using single quotes. Can contain "special" characters, E.g. \n (newline), \t (tab) *STRINGS '\ta tab\nand a newline' One or more digits, with an 55 optional decimal point. *NUMBERS 3.14159 4.25e10 A series of text items, such as [a b c d] words, numbers, other lists, or [1 Acacia Avenue] strings, within square brackets. ['a string' 33 cat] *LISTS [[CAT CHAT][DOG CHIEN]] [% ... %] Decorated list brackets can be [% for x from 1 to 5 do used with enclosed Pop-11 commands x to make a list. *PERCENT endfor %] => ** [1 2 3 4 5] Like a list, but cannot be extended {a four word vector} and takes up less space. {% ... %} Like decorated list brackets can {% 3+3, 99*9 %} => contain Pop-11 commands. ** {6 891} A kind of structure with a specific recordclass triple number of components and particular first second third; procedures for accessing and constriple(1, 2, 3) => updating them, etc. *RECORDCLASS ** (Or see *defclass) \end{verbatim} {\large \bf Comments}\\ \PS \begin{verbatim} ;;; Begins a comment (text that will ;;; This is a comment. be ignored by Pop-11). The comment ends at the end of the line. *COMMENT /* ... */ An alternative form of comment. /* comments can go over several lines */ \end{verbatim} {\large \bf Variable declarations}\\ \PS \begin{verbatim} vars Used to declare local or global vars x, y, z; non-lexical variables. *VARS vars num = 10; (Don't use vars for local variables except in a ). lvars Used to declare local or global lvars x, y, z; lexical variables *LVARS lvars num = 99; \end{verbatim} {\large \bf Miscellaneous}\\ \begin{verbatim} ; Semi colon terminates commands. vars a; 100 -> a; I.e. separates imperatives. It is a separator. A type of object that is the vars xxx; default value for a variable that xxx=> has been declared, but not had a ** value assigned to it. REF * IDENT/'Undef Records' undef This special constant refers to an undef => item which is often used as the ** undef default value for components of a new structure (e.g. an ). \end{verbatim} {\large \bf Printing facilities}\\ \PS \begin{verbatim} => Print arrow. *PRINTARROW 3 + 4 => ** 7 ==> Pretty print arrow (prints a long list or vector tidily). *PRINTARROW pr Prints an item (word, string, list, pr(list); vector, etc. without "**" or newline. pr('The cat'); ppr Like pr, but (a) prints lists minus ppr([[the][cat]]); any list brackets, and (b) prints a the cat space after each item. *PPR spr Like pr, but prints a following space spr("a");spr("b"); *SPR a b npr Like pr, but prints a newline after npr("a");npr("b"); each item. * NPR, *PRINTING a b printf For more sophisticated printing see printf( *PRINTF '%p plus %p gives %p', [2 3 5]); 2 plus 3 gives 5 \end{verbatim} {\large \bf Assignments}\\ \PS \begin{verbatim} -> Assignment arrow. Assigns a value vars a; to a variable. TEACH * STACK 100 -> a; Also invokes updaters, and is used 33 -> hd(list); in defining procedures with output locals. See below. Compare: vars a = 100; ->> Like assignment arrow, but first hd(x) ->> a -> b; duplicates object on stack, so that e.g. it can be assigned twice. \end{verbatim} {\large \bf Inserting values in lists and vectors (using \verb;"^" and "^^";)}\\ \PS \begin{verbatim} ^ Includes the value of an expression vars animal = "cat"; in a list or vector expression. [the ^animal sat]=> *ARROW ** [the cat sat] ^^ Includes the elements of a list vars beasts = [cat pig]; inside another list. *ARROW [the ^^beasts sat] => ** [the cat pig sat] ^ ^^ NOTE: these also work for vectors \end{verbatim} \newpage {\large \bf Defining procedures}\\ \PS \begin{verbatim} A 'package' of Pop-11 commands, hd, sqrt, maplist, usually with a name. May have an *, -, subscr, etc. updater *PROCEDURES *DEFINE are all built-in Some procedures are built-in some procedures. user-defined. define Start and end of a define perim(width,height); enddefine procedure definition *DEFINE return(2*width + 2*height) enddefine; return Terminates execution of the define first_and_last(list); current procedure, and returns return(hd(list), to whatever invoked it. last(list)) Analogous to "goto enddefine". enddefine; Items in brackets after return are left on the stack. *RETURN -> Indicates an 'output local' in define perim2(w,h)->result; a procedure header line. An 2*w + 2*h -> result; alternative to 'return' as a enddefine; way of specifying the result of a procedure call. *DEFINE, *STACK \end{verbatim} {\large \bf Miscellaneous 2}\\ \PS \begin{verbatim} readline() A Pop-11 procedure that prints a ? readline() -> input_words; and then waits for input from the terminal. Any words, numbers or strings typed on the line after the ? are returned in a list. *READLINE date() A procedure that returns a list date()=> giving the current time and date. ** [18 Sep 1985 11 47 16] *DATE length() length([the cat sat])=> A procedure that returns the ** 3 length of an item. *LENGTH length("iguana")=> The length of a item is the number ** 6 of components it contains. \end{verbatim} \newpage \begin{verbatim} An element can be picked from a vars sentence animal; list by giving its position in [the cat sat] -> sentence; brackets after the name sentence(2) -> animal; *LISTSUMMARY oneof() vars throw = Returns an element picked at random oneof([1 2 3 4 5 6]); from a list. *ONEOF \end{verbatim} {\large \bf Arithmetic operators}\\ \PS \begin{verbatim} + Adds one number to another. width+height->half_perim; * Multiplies two numbers. 3.14159*d -> circum; / Divides one number by another. total/items->average; Warning: dividing one integer by another can give a "ratio" which 10/5, 3/4 => may print as, e.g. 3_/4 ** 2 3_/4 abs When applied to a positive or abs(-10) => negative number returns its ** 10 absolute value (always positive) *ABS pop_pr_ratios false -> pop_pr_ratios; This Pop-11 variable controls how ratios are printed. If made false 10/5, 3/4 => it makes ratios print as decimals. ** 2 0.75 // Divides one integer by another to 10//3 get dividend and remainder, -> (remainder,dividend); ** Raises one number to the power of 2**3 => another. ** 8 > Compares two numbers. The result is if x > 3 then .... endif true if the first is greater. >= Compares two numbers. The result is true if the first is greater or equal. < Compares two numbers. The result is 4 < 3 => true if the first is smaller. ** \end{verbatim} \newpage \begin{verbatim} <= Compares two numbers. The result is true if the first is smaller or equal to the second. ( ) Round brackets have two uses. They (3+2)*4 => can alter the order of evaluation in ** 20 expressions, or following a variable perim(45,23) => or expression they can signify ** 136 procedure invocation. Any arguments to the procedure go in the brackets. true These are constants which hold the true => false two special boolean values ** and used in conditionals false => and loop termination tests. *BOOLEAN ** = Tests whether two items are equal if a = 100 then ... *EQUAL It can also be used to initalise vars x = [1 2 3]; a variable; == Tests whether items are identical if a == [cat] then ... /= Tests whether two items are unequal. a /= b (Looks inside structures) * EQUAL /== Tests whether two items are not a /== "cat" identical. (Does not look inside structures) \end{verbatim} {\large \bf Logical connectives}\\ (E.g. for use in conditionals) \PS \begin{verbatim} and Forms the 'conjunction' of two if x > 0 and x < 100 then boolean expressions. *AND or Forms the 'disjunction' of two word="cat" or word="puss" boolean expressions. *OR not Negates a boolean expression. not(list matches [== cat ==]) *NOT if Marks the start of an 'if' if english == "cat" then conditional. *IF "chat"=> endif; \end{verbatim} \newpage \begin{verbatim} then Ends the condition part of an 'if' conditional. *THEN (Also used with "unless") elseif Begins a second (or subsequent) if english == "cat" then condition in an 'if' statement. "chat" => *ELSEIF elseif english == "dog" then "chien" => else Marks the beginning of the else "default" course of action in [I dont know] => a conditional. *ELSE endif; endif Marks the end of a conditional. *ENDIF \end{verbatim} {\large \bf Variable formats for the matcher}\\ \PS \begin{verbatim} matches Compares a list with a pattern. vars sentence; It returns true if they match, [the cat sat] -> sentence; false otherwise. It will also sentence matches [= cat =] => "bind" variables in the pattern, ** if there are any. *MATCHES = Matches one item inside a list mylist matches [= cat sat] pattern. == Matches zero or more items inside mylist matches [== cat ==] a pattern. ? Matches one item inside a list mylist matches [?first ==] pattern and makes that the value of the variable. *MATCHES ?? alist matches Matches zero or more items within [?first ??rest] => a list pattern and makes the list of matched items the value of the ** variable. *MATCHES ! Use in front of a pattern to make mylist matches the variables lvars ![?first ??rest] => database A Pop-11 variable whose value is database ==> the database, a list of lists, used with add, remove, present, etc. *DATABASE \end{verbatim} \newpage \begin{verbatim} add() add([john loves mary]); Puts an item into the database. *ADD remove() remove([john loves =]); Removes the first item matching the pattern from the database. *REMOVE flush() flush([== loves ==]); Removes all items matching the pattern from the database. *FLUSH present() if present([?x loves mary]) Searches the database for an then item matching the database and x=> returns true if it is found, endif; false otherwise. Binds variables in the pattern. *PRESENT allpresent() if allpresent( Searches the database for items [[?x loves ?y] that consistently match all the [?y loves ?z]]) patterns, and returns true if then this succeeds and false otherwise [Triangle ^x ^y ^z] => Binds variables in the pattern. endif; *ALLPRESENT it A variable that is set by 'add', if present([?x loves mary]) 'remove', 'present' and 'foreach'. then Its value is the last item found it=> in the database. *IT endif; \end{verbatim} {\large \bf Looping expressions}\\ \PS \begin{verbatim} repeat Marks the start of a repeat loop. repeat *REPEAT readline()->line; quitif(line /== []); endrepeat Marks the end of a repeat loop. endrepeat; *ENDREPEAT times Indicates the number of times a repeat 4 times; repeat loop is to be repeated (If "."=> it is omitted then looping is endrepeat; forever,unless halted by quitif). *TIMES quitif() vars n = 2; If the expression is true then repeat; quit the loop. This example and quitif(n > 1000); the one using the while loop n => below are equivalent (ie they n*n -> n; give the same result). *QUITIF endrepeat; while Marks the start of a while loop. vars n = 2; *WHILE while n <= 1000 do n => do Ends the condition part of a n*n -> n; 'while', 'for', or 'foreach' loop. endwhile; *DO endwhile Marks the end of a while loop. *ENDWHILE for Marks the start of a for loop. for x in [paris london] do *FOR [^x is a city]=> endfor; endfor Marks the end of a for loop. *ENDFOR Note: there are many different forms of for ... endfor loops. See *LOOPS, *FOR. foreach Marks the start of a foreach loop, vars x y; which matches a pattern against foreach [?x loves ?y] do each item in the database. *FOREACH it=> endforeach; endforeach Marks the end of a foreach. *FOREACH forevery Like foreach, but takes a list of forevery patterns and tries all possible [[?x ison ?y] ways of matching them all [?y ison ?z]] consistently with items in the do database. *FOREVERY them => [^x is above ^z] => endforevery Syntax word used at the end of endforevery; a "foreach" loop. \end{verbatim} {\large \bf Arrays}\\ \PS \begin{verbatim} A compound data object with N dimensions whose components can be accessed or updated using N numerical subscripts. *ARRAYS newarray The simplest procedure to create a vars ten_by_seven = Pop-11 array. * NEWARRAY newarray( [1 10 -3 3]); boundslist When applied to an array returns a boundslist( list containing for each dimension ten_by_seven) => the upper and lower bounds. ** [1 10 -3 3] \end{verbatim} {\large \bf Tracing utilities}\\ \PS \begin{verbatim} trace trace add first_and_last; A command that alters procedures so they print out helpful information. (NB. You can trace built-in procedures like 'hd' and 'tl'). *TRACE untrace untrace add first_and_last; A command that switches off tracing of the named procedures. *TRACE untraceall Switches off any traces.*UNTRACEALL untraceall; See also *INSPECT and *DEBUGGER \end{verbatim} \newpage {\large \bf FURTHER READING} The {\bf Pop-11 Primer}, by A.Sloman, is available online as TEACH\ PRIMER and also available in hard copy from the School of Computer Science Library. \begin{verbatim} TEACH * FACES, * GSTART, * USEFULKEYS TEACH *LISTS, *LISTSUMMARY, TEACH *BOXES *POPSUMMARY, *DEFINE, *STACK, *VARS TEACH * DATABASE, * FOREACH HELP * WORDS, *LISTS, *MATH, *LOOPS, *CONTROL, *ARRAYS, *STRINGS HELP * MATCHES, *PRINT, *TRACE, *RECURSION TEACH * RECURSION, * SETS, * SETS2, * FUNCTIONAL.STYLE M. Sharples, et al. Computers and Thought, MIT Press, 1989 (This is an introduction to cognitive science using Pop-11 programming examples as illustrations.) James Anderson(ed) Pop-11 Comes of Age Ellis Horwood, 1989 (A collection of papers on the history of dialects of Pop, the features and benefits of the language, and some applications using Pop-11.) Chris Thornton & Benedict du Boulay (1992) Artificial Intelligence Through Search Kluwer Academic (Paperback version Intellect Books) (An introduction to AI using Pop-11 and Prolog. A good way to learn Prolog if you know Pop-11 or vice versa.) \end{verbatim} {\bf WARNING:} books published before 1995 are likely to have out of date information about Pop-11, though the core ideas are unchanged. In the Poplog system there is a large collection of {\bf REF} files giving definitive information about Pop-11. These files are mostly useful for experts, but occasionally you'll find that information you need is available nowhere else. The pop-forum email list and comp.lang.pop internet news group are also useful sources of information. There is a lot of pop-11 material available by ftp from the Birmingham Poplog directory \mbox{}~~~~~~~~~ {\bf http://www.cs.bham.ac.uk/research/projects/poplog/} \end{document}