260 likes | 382 Views
Artificial Intelligence and Lisp Lecture 12 Finish "Programming in Lisp, II" + Lab 5 a-b + Review and Synthesis of the Course LiU Course TDDC65 Autumn Semester, 2010 http://www.ida.liu.se/ext/TDDC65/. Finish "Programming in Lisp, II". Tentative Top Level. (defun lispos (scr vbl val)
E N D
Artificial Intelligence and LispLecture 12Finish "Programming in Lisp, II"+Lab 5 a-b+Review and Synthesis of the CourseLiU Course TDDC65Autumn Semester, 2010http://www.ida.liu.se/ext/TDDC65/
Tentative Top Level • (defun lispos (scr vbl val) • (case (car scr) • (print ...) • (script (dolist (x (cdr scr)) • (lispos x vbl val) )) • (repeat • (dolist (j (evarg (caddr scr) vbl val)) • (lispos (cadddr scr) • (cadr scr) j ))) • (t [... error handling ...] ))) • (defun lispex (scr) (lispos scr nil nil))
Tentative Top Level • (defun lispos (scr vbl val) • (case (car scr) • (print (send-to-printer • (evarg (cadr scr) vbl val) )) • ... )) • (defun evarg (arg vbl val) • (if (equal arg vbl) val • (if (stringp arg) arg • (if (symbolp arg) (get arg 'path) • (case (car arg) ... )))))
Using 'cond' instead of 'if' • (defun evarg (arg vbl val) • (if (equal arg vbl) val • (if (stringp arg) arg • (if (symbolp arg) (get arg 'path) • (case (car arg) ... ))))) • Rewrite better as follows: • (defun evarg (arg vbl val) • (cond ((equal arg vbl) val) • ((stringp arg) arg) • ((symbolp arg)(get arg 'path)) • ... ))
Using Attached Functions • (defun lispos (scr vbl val) • (case (car scr) • (print ...) • (script (dolist (x (cdr scr)) • (lispos x vbl val) )) ...)) • (defun lispos (scr vbl val) • (funcall (get (car scr) 'lisposdef) • (cdr scr) vbl val )) • (setf (get 'print 'lisposdef) • #'(lambda (args vbl val) • (send-to-printer • (evarg (cadr scr) vbl val) )))
Joint Argument Evaluation • (defun lispos (scr vbl val) • (funcall (get (car scr) 'lisposdef) • (cdr scr) vbl val )) • (setf (get 'print 'lisposdef) • #'(lambda (args vbl val) (send-to-printer • (evarg (cadr scr) vbl val) ))) • (defun lispos (scr vbl val) • (funcall (get (car scr) 'lisposdef) • (evarglist (cdr scr) vbl val) )) • (setf (get 'print 'lisposdef) • #'(lambda (args vbl val) • (send-to-printer args vbl val) ))
Joint Argument Evaluation • (defun lispos (scr vbl val) • (funcall (get (car scr) 'lisposdef) • (evarglist (cdr scr) vbl val) )) • (setf (get 'print 'lisposdef) • #'(lambda (args vbl val) • (send-to-printer args vbl val) )) • (defun evarglist (lst vbl val) • (if (null lst) nil • (cons (evarg (car lst) vbl val) • (evarglist (cdr lst) vbl val) )))
Compiling Scripts • (defun lispos (scr vbl val) • (case (car scr) • (print (send-to-pr (cadr scr) vbl val)) • (script (dolist (x (cdr scr)) • (lispos x vbl val) )) ...)) • Now consider conversion: • (lispos '(script (print "file1")(print "file2")) • nil nil ) • (progn (send-to-pr "file1" nil nil) • (send-to-pr "file2" nil nil) ) • Latter version can be compiled. • How can this conversion be automated?
Partial Application - Example • (defun foo (x y)(cons (car x)(cdr y))) • (foo '(a b c) '(g h i)) evaluates to (a h i) • (funcall 'foo '(a b c) '(g h i)) same • (funcall #'(lambda (x y)(cons (car x)(cdr y))) • '(a b c) '(g h i) ) same • (funcall #'(lambda (x y)(cons (car x)(cdr y))) • '(a b c) z ) requires value of z • (partapply #'(lambda (x y)(cons (car x)(cdr y))) • '(a b c) ) evaluates to • #'(lambda (y)(cons 'a (cdr y)))
Partial Application - Example • (partapply #'(lambda (x y)(cons (car x)(cdr y))) • '(a b c) ) evaluates to • #'(lambda (y)(cons 'a (cdr y))) • Recall definition of top level of interpreter. When a script is • to be compiled then scr is known but vbl, val unknown: • (defun lispos (scr vbl val) • (funcall (get (car scr) 'lisposdef) • (evarglist (cdr scr) vbl val) ))
Partial Evaluation and Application • (partapply fn a) takes a function fn of two arguments and returns a function of one argument • (funcall fn '(a b c) '(c d e)) = • (funcall (partapply fn '(a b c)) '(c d e)) • Notice that the function lispos took three arguments: • (lispos script vbl val) • but actually better to write as • (lispos script (list (list vbl val))) • to allow for several variables • Therefore quite feasible to compile lispos scripts to Lisp (or define partial application for functions of 3 arguments)
Script Compilation Sequence • Convert from use of attached functions to use of case-expressions • Partial evaluation with respect to the resulting script • Compilation of Lisp code to lower level (machine language or pseudocode)
Defining Lisp in Lisp • (defun apply (fn args)(cond • ((symbolp fn)(apply (fndef fn) args)) • ((equal (car fn) 'lambda) • (eval (caddr fn)(bind (cadr fn) args))) • ... etc )) • (defun eval (form bind)(cond • ((symbolp form)(lookup form bind)) • ((stringp form) form) • ((equal (car form) 'quote)(cadr form)) ... • (t (apply (car form)(evlis (cdr form) bind))) • ))
Defining (Partial) Application for Lisp • (defun apply (fn args)(cond • ((symbolp fn)(apply (fndef fn) args)) • ((equal (car fn) 'lambda) • (eval (caddr fn)(bind (cadr fn) args))) • ... etc )) • The definition of partapply follows the same pattern - straightforward to generalize
Autogenerating Partial Application for Lisp • (defun apply (fn args)(cond • ((symbolp fn)(apply (fndef fn) args)) • ((equal (car fn) 'lambda) • (eval (caddr fn)(bind (cadr fn) args))) • ... etc )) • The definition of partapply follows the same pattern - straightforward to generalize • But - apply is itself a function of two arguments. Can one apply partapply on it? • (funcall fn '(a b c) '(c d e)) = • (funcall (partapply fn '(a b c)) '(c d e))
Lab 5 is based on the following • BDI Architecture (see Compendium, Part III) • This architecture defined desires, intentions, and goals • Also, methods for precondition repair in Lab 2b-2c • Each action has preconditions; methods describe how to achieve thos preconditions in specific cases • Actions were initiated in repeatedly used scripts
Now make following changes • BDI Architecture (see Compendium, Part III) • This architecture defined desires, intentions, and goals • Also, methods for precondition repair in Lab 2b-2c • Each action has preconditions; methods describe how to achieve thos preconditions in specific cases • Actions were initiated in repeatedly used scripts • Introduce a set of desires which are universally quantified formulas, e.g. • [all .x [Hc (the health of .x) good]] • At each (suitable) timepoint, identify unsatisfied instances of desires, e.g. [Hc (the health of Groucho) good]
From Desires to Plans and Intentions • Introduce a set of desires which are universally quantified formulas, e.g. • [all .x [Hc (the health of .x) good]] • At each (suitable) timepoint, identify unsatisfied instances of desires, e.g. [Hc (the health of Groucho) good] • Select a consistent subset of the identified instances of desires; this is the set of goals • Make plans for each of the goals and combine them. The combined plan, the goals they achieve, and the additional effects (side-effects) are the intentions in the situation at hand • This is one possible realization of the BDI architectural model, using a logicist framework.
Lab 5b • In Lab 5b the lab materials will provide a fully equipped Zoo Microworld, including types, animals and personnel, actions for the personnel (esp. the Warden) and the animals, and so forth • Furthermore there will be a set of desires, in the sense used above, and, for each type of instantiated desire, a method for achieving it as a goal • The basic operation of the Warden will be to perform routine tasks (such as a tour of some of the animals) combined with repeated checking that desires are satisfied and reaction when this is not the case • One part of this setup is that the agent (the Warden) should always check that a proposed method is appropriate before starting to execute it. This requires predicting the likely effects of the method as a plan.
Lab 5a • One part of this setup is that the agent (the Warden) should always check that a proposed method is appropriate before starting to execute it. This requires predicting the likely effects of the method as a plan. • This is what Lab 5a is about. • Lab 5a is similar to Lab 2b and 2c in the sense that you create an episode in the Zoo, containing actions and their effects. • However, besides executing actions, one may also predict the state of microworld that will result if an action is performed, or a sequence of actions. • This is done by creating entities for situations (figure!).
Episode and Situations Timepoints in episode Situations
Lab 5a • Making actions in the episode is like for Lab 2b etc • No use of scripts for extending the episode, just single actions • In addition, nxs [verb arg arg ...] extends the tree of forthcoming situations with a new node from the current one, and makes the new one current • Before the use of nxs, the current timepoint is also the current situation • Performing an action in the episode will reset the current situation to the new current timepoint • The nxs operation also uses action effect laws for each verb in order to derive the effects for the new situation. The derivation is done using the resolution operator.
Lab 5a -- Practical Aspects • Practical use of the deduction of effects differ in several ways from how the lab is done: • Larger or large-scale production of situations, for example for progressive planning, and for checking methods in the form of plans that are produced for goals in the BDI architecture • Each verb may be associated with several effect rules which specify multiple effects and alternative effects depending on the current state of the world • Checking of the conditions for the effect rule(s) is done by direct lookup in the lab, but may use more complex deduction in practical cases • Dealing with uncertainty, concurrent actions, causality, etc.
Review and Synthesis of the Coursedone byLooking back at E-transparencies fromearlier lectures