TEACH PROGLECT2 Aaron Sloman Oct 1998 Revised 17 Oct 2000 See also TEACH PROGLECT1 CONTENTS -- Some reminders from previous lecture(s) -- Learning what is possible: some simple examples -- Using an AI programming environment -- -- editor -- -- an AI language -- Pop-11 -- Learning about Pop-11 -- o The grammar (syntax) of the language: -- o The "internal semantics" of the language: -- o The "external semantics" of the program -- More on the internal semantics of Pop-11 -- -- Pop-11 data types -- -- Pop11 control structures -- -- . Sequences of instructions -- -- . Conditional instructions -- -- . Multi-branch conditionals -- -- . Loops of various kinds -- -- . Packaging re-usable instructions into named procedures -- -- -- Procedures without parameters; -- -- -- Procedures with parameters (i.e. inputs, arguments) -- -- -- Procedures with inputs and outputs -- -- -- The general form: multiple inputs and outputs -- -- . Procedure calls with automatic "return" to caller -- -- . Various kinds of jumps and abnormal exits -- Using Pop-11 via VED -- -- Compiling from a VED buffer -- -- Menu-driven compilation -- -- Using VED to access information files -- -- Using VED to access library files -- -- Using ESC h and ESC n -- Libraries -- Learn to use the editor VED -- Other Pop-11 facilities -- . Reading more about these facilities: -- Some reminders from previous lecture(s) ---------------------------- AI has two sorts of closely related high level goals AI as engineering AI as science For both goals it is important to be able to build models that can be tested (a) to see if they work (i.e. if they have the claimed potential, to explain, or to do something useful) (b.1) in the case of engineering: to see if they actually perform the useful tasks they are intended to perform (e.g. diagnosing illness, controlling chemical plant, teaching learners, etc.) (b.2) in the case of science: to see if the model is correct, or at least fits the facts, or explains how animals and people actually work. There are many methodological problems about how to test such models but we ignore them for now. This course is about how to produce models especially models that involve complex symbol-manipulating capabilities. For that you have to learn o what sorts of models are possible o how to specify them so that you can plan a design o how to actually write the code to implement the design o how to test and debug the system (it won't work first time!) o how to explain how your system works (i.e. write a report on it) This involves developing a host of special skills and knowledge. The main ones are: o Fluency with the editor. This takes much practice and a study of TEACH VEDNOTES to learn about the powers of the editor. - To read online documentation - To create files with program code, with your own documentation - To test and debug programs interactively - Possibly to send and receive email. o Learning the programming language we have chosen, Pop-11 (This is a very powerful, flexible, extendable language, supporting many types of software development.) o Learning how to learn about the language and the editor. This means learning to find your way around the online documentation, the TEACH, HELP, REF files, and the online libraries, using SHOWLIB. NB: The lectures CANNOT convey all the material. So I shall present only high level overviews most of the time. Students will have to supplement this by (a) reading The Pop-11 primer, TEACH POPCORE, many online teach files. Books on AI, e.g. Sharples, Franklin, Nilsson, Winston, others. (b) developing insight and fluency by practising programming Total time: at least 13 hours a week. -- Learning what is possible: some simple examples -------------------- Example 1 pop11 +eliza Example 2 pop11 +gblocks Example 3 The riverworld lib river false -> popsyscall; ;;; for me only putin(fox); getin(); TEACH RIVER shows you how to specify a solution, then refine it, explaining some basic programming ideas (procedures, arguments(inputs), sequences) Example 4 Programs that plan and search, e.g. uses poprulebase uses prbriver prb_run( depth_first, [[goal [ [isat man left] [isat grain right] [isat fox right]] ] ]); Example 5 uses newkit teach sim_sheepdog.p Note: this course does not teach neural net programming, or evolutionary computation. However the basic skills provided can be used later on to write programs of those sorts. -- Using an AI programming environment -------------------------------- -- -- editor o For writing code, documentation, browsing, running programs, debugging, sending and reading email (e.g. from tutor). -- -- an AI language o For specifying information of various kinds specific factual knowledge The chicken is at the left bank general factual knowledge A bank cannot be put in a boat Can be done in a declarative or a procedural format procedural knowledge (how to, algorithms) How to get an animal safely across the river architecture (how it is all put together) A collection of files compiled together in a certain order. An initialisation procedure which creates all the parts of the program and gets them ready to run. tracing and cosmetic output Trace printing may be very useful while testing or debugging or producing evidence of what the program does. The main AI languages are Lisp, Prolog, Pop-11 and various extensions, e.g. rule-based systems such as OPS5 and its descendants (e.g. SOAR) and various kinds of toolkits, e.g. the Birmingham SIM_AGENT toolkit. In recent years sophisticated AI languages have also used "object oriented" extensions, e.g. Objectclass in Pop-11 and CLOS in Common lisp. Some people use C and C++ for AI, because of speed and portability, or they use Java for portability and easier development or greater robustness than C or C++ (e.g. fewer memory leaks). However, using a non-AI language can sometimes make development much harder than using special purpose AI languages, e.g. because there's no automatic store management, or because there's no facility for defining new syntactic extensions, or because the compiler is not available at run time, or because there is no built in pattern matcher and it is hard to implement one, or .... There is no best language. We use Pop-11 here not because it is claimed to be best, but because we know it is suitable both for teaching beginners and for doing advanced research, because it supports a wide range of programming paradigms only subsets of which are usually found in one language (apart from Lisp), because it has a lot of tried and tested teaching materials for AI and cognitive science, and because we know it well. Also, since 1999 it is free, despite previously being sold as an expensive commercial product. -- Pop-11 ------------------------------------------------------------- This is one of several AI languages with different properties. Pop-11 is in many ways like Common Lisp, except that it has a form of syntax which is more verbose and redundant, which for many people (not all) makes it more readable, especially beginners. Use VED for the programmable editor, for accessing all the Poplog online documentation, and for developing and testing Pop-11 programs. XVed is the multi-window version of Ved. VED itself is just a collection of Pop-11 subroutines in Poplog adding up to a "hypertext" document browser, able to follow cross-links rapidly (like Netscape without the pictures). (Also works for mail, net news, other things) A partly similar editor is Emacs, widely used by computer scientists, but not so well integrated with Pop-11. -- Learning about Pop-11 ---------------------------------------------- In order to learn a programming language you have to learn several different sorts of things, not necessarily in this order: The grammar, the internal semantics, the external semantics (varies), how to create programs, how to test and debug programs, how to use the libraries and documentation, etc. In more detail. This is what you have to learn. -- o The grammar (syntax) of the language: What the lexical items are (e.g. symbols for words, strings, numerals....) How they can be put together to form larger programming units declarations, assignments, referring expressions, procedure definitions, etc. etc. This includes learning scoping rules (which bits of program can refer to others: global vs local variables) What the syntactic categories of the language are E.g. types of variables, types of complex expressions. (Experts can look at: REF popsyntax) How the syntactic categories can be extended (not possible in all languages) -- o The "internal semantics" of the language: What sorts of entities are in the "virtual machine" when programs run. - Entities referred to explicitly: the data-types of the language both primitive (e.g. pairs) and derived (e.g. lists). e.g. numbers, words, strings, arrays, lists, databases, records, files, devices, editor buffers, windows... - Entities referred to implicitly: the components of the virtual machine architecture. In Pop-11 these include the dictionary, the heap, the user stack, the procedure stack, .... See the picture in the printed PRIMER, or the online primer http://www.cs.bham.ac.uk/research/poplog/primer/ http://www.cs.bham.ac.uk/research/poplog/primer/popmodel.eps See TEACH STACK, after you have done TEACH RESPOND - The types of processes that can occur Compile time processes: analysing program, translating to machine code checking for syntactic errors compilng libraries Run time processes (when your programs are already compiled, and start running): invoking a procedure, returning from the procedure creating new items (lists, strings, etc.) assigning values to variables getting values of variables putting items on the stack (passing arguments, returning results) testing conditions looping discovering and reporting run-time errors garbage collection (and many more) NOTE: Some processes (at compile time and at run time) occur because your program explicitly specifies them. Others (e.g. garbage collection, error checking) occur because the virtual machine does them under certain conditions. -- o The "external semantics" of the program This varies from program to program. E.g. the river program refers to the left and right side, the boat, the fox, the chicken, eating, etc. An office automation package may refer to people, salaries, ages, jobs, phone numbers, etc. A program controlling a chemical plant may refer to temperatures, flow velocities, pumps, switches, sensors, etc. A graphical program may refer to lines, circles, colours, menus, buttons, etc. Getting to understand all these different aspects of syntax and semantics and how they relate to one another takes a lot of time and can often be confusing, because it is complicated. See The Pop-11 Primer. (Also TEACH PRIMER) Read the preface, chapter 1 and chapter 2. (Skimming details on a first read.) See TEACH POPCORE -- More on the internal semantics of Pop-11 ---------------------------- -- -- Pop-11 data types Pop-11 has many kinds of "data-structures" for storing information: E.g. information that is: provided by the user where the fox, chicken and grain start off created by the program new location of an animal or the boat, after the program has run for a while read in by the program while it is running, e.g. from other programs, sensors, etc. Example information about the user's question in Eliza. Types of data include numbers (various kinds), words, strings, lists, vectors, properties, and many more Some data types are "primitive" whereas others are "derived", namely built out of the primitive structures. E.g. in Pop-11 integers and decimal numbers (reals) are primitive, whereas lists are derived. They are built out of pairs. For this course a subset of data types will suffice. (See TEACH * POPCORE, and the section on data types in the PRIMER) (Experts see REF * DATA) -- -- Pop11 control structures To understand a programming language you need to know BOTH what kinds of information items it can create and manipulate, and ALSO how to build up processes which do the work you want. This means assembling complex instructions from simple ones, using the "control structures" provided by the language. There are many sorts of "control structures", including the following. -- -- . Sequences of instructions action1; action2; ...... -- -- . Conditional instructions if condition then action endif -- -- . Multi-branch conditionals if condition1 then action1 elseif condition2 then action2 elseif .... else ... endif -- -- . Loops of various kinds repeat endrepeat while do endwhile until do enduntil Loops with "loop variables" for x from 1 to 75 do endfor for x in list do endfor Loops involving the pop-11 database foreach in do endforeach forevery in do endforeach ... and several more. See HELP * LOOPS, HELP * CONTROL -- -- . Packaging re-usable instructions into named procedures -- -- -- Procedures without parameters; Example from TEACH FACES ;;; A global declaration vars win1; define fixed_eyes(); rc_new_window_object(600, 10, 400, 400, true, 'WIN1') -> win1; rc_draw_blob(0, 0, 70, 'red'); rc_draw_blob(-35, 25, 15, 'blue'); rc_draw_blob(35, 25, 15, 'blue'); enddefine; /* ;;; test it fixed_eyes(); ;;; runs the procedure */ -- -- -- Procedures with parameters (i.e. inputs, arguments) ;;; Another example from TEACH FACES define located_eyes(x, y); ;;; draw the main circle for the face, centred at (x, y) rc_draw_blob(x, y, 70, 'red'); ;;; draw the left eye, radius 15 rc_draw_blob(x - 35, y + 25, 15, 'blue'); ;;; draw the right eye, radius 15 rc_draw_blob( x + 35, y + 25, 15, 'blue'); enddefine; Note how the parameters are used in the procedure body. /* ;;; test it located_eyes(-100, 100); ;;; runs the procedure located_eyes(100, 100); located_eyes(100, -100); */ -- -- -- Procedures with inputs and outputs output locals, input locals define greeting(name) -> result; [Hi there ^name. How are you?] -> result enddefine; Note how the value of name is used. /* ;;; test it, giving a WORD as input, printing the output greeting("fred") => ;;; save the output in a variable vars list; greeting("suzy") -> list; ;;; now print it, twice list => list => */ -- -- -- The general form: multiple inputs and outputs define name(arg1, arg2 ...) -> (output1, output2...); actions enddefine; Note: this is not an instruction to DO the instructions inside the procedure definition, but an instruction to make available a re-usable collection of instructions with a name. See TEACH DEFINE, HELP DEFINE, PRIMER chapter 4 -- -- . Procedure calls with automatic "return" to caller ; do_stuff(x,y,z); ;;; when do_stuff finishes it ;;; automatically returns here ; A procedure call is an instruction to do something using previously defined procedures. Pop-11 uses the procedure call stack to remember which procedures are currently active. When the do_stuff procedure has finished, it remembers to come back to the procedure that was previously running, and continue with -- -- . Various kinds of jumps and abnormal exits goto