Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorials:beginner:designators [2016/01/22 15:41] – [Designators: an overview] gkazhoyatutorials:beginner:designators [2016/03/04 14:24] (current) – old revision restored (2016/01/25 18:12) gkazhoya
Line 1: Line 1:
-====== Creating action designators for the turtlesim ======+====== Creating action designators for the TurtleSim ======
  
 **Description:** In this tutorial you will learn what designators are, and in particular, work with action designators: you will learn how to define one and how to use it. **Description:** In this tutorial you will learn what designators are, and in particular, work with action designators: you will learn how to define one and how to use it.
Line 22: Line 22:
  
 <code lisp> <code lisp>
-(defparameter spy-location (make-designator :location `((to see) (object ,prime-minister))))+(defparameter spy-location (make-designator :location `((:to :see) (:object ,prime-minister))))
 </code> </code>
  
Line 77: Line 77:
 ==== Creating an action designator ==== ==== Creating an action designator ====
  
-Let's try to create an action designator in the repl command line:+Let's try to create an action designator in the REPL command line:
  
 <code lisp> <code lisp>
Line 101: Line 101:
 This rather obscure error message is CRAM's way of telling you there are no rules in place to resolve this designator, so let's try to provide a few such rules. This rather obscure error message is CRAM's way of telling you there are no rules in place to resolve this designator, so let's try to provide a few such rules.
  
 +In case you were wondering why we use keywords in designator properties, that is because we would like to be able to specify rules to resolve designators in one package and create designators that are resolved through these rules in a different package. To avoid naming clashes between packages and huge import lists, we simply define all designator properties in the ''keyword'' package.
 ==== Defining inference rules for designators ==== ==== Defining inference rules for designators ====
  
Line 128: Line 129:
     (desig-prop ?desig (:shape :square))     (desig-prop ?desig (:shape :square))
     (desig-prop ?desig (:radius ?radius))     (desig-prop ?desig (:radius ?radius))
-    (lisp-fun make-turtle-shape :radius ?radius :edges 4  ?action))+    (lisp-fun make-turtle-shape :radius ?radius :edges 4 ?action))
  
   ;; pentagon   ;; pentagon
Line 135: Line 136:
     (desig-prop ?desig (:shape :pentagon))     (desig-prop ?desig (:shape :pentagon))
     (desig-prop ?desig (:radius ?radius))     (desig-prop ?desig (:radius ?radius))
-    (lisp-fun make-turtle-shape :radius ?radius :edges 5  ?action))+    (lisp-fun make-turtle-shape :radius ?radius :edges 5 ?action))
  
   ;; hexagon   ;; hexagon
Line 142: Line 143:
     (desig-prop ?desig (:shape :hexagon))     (desig-prop ?desig (:shape :hexagon))
     (desig-prop ?desig (:radius ?radius))     (desig-prop ?desig (:radius ?radius))
-    (lisp-fun make-turtle-shape :radius ?radius :edges 6  ?action)))+    (lisp-fun make-turtle-shape :radius ?radius :edges 6 ?action)))
 </code> </code>
  
Line 174: Line 175:
 Prolog's inference semantics is, in a nutshell, 'IF there is some assignment to variables such that all elements of the body are true, THEN use that assignment of variables to evaluate the head'. Note that in CRAM Prolog for a symbol to be considered a variable, its name must begin with a ''?'' Prolog's inference semantics is, in a nutshell, 'IF there is some assignment to variables such that all elements of the body are true, THEN use that assignment of variables to evaluate the head'. Note that in CRAM Prolog for a symbol to be considered a variable, its name must begin with a ''?''
  
-For the example rule we've selected, Prolog looks at the designator and asks "does it contain a key-value pair that is (:type :shape)?" (so no variables here except the designator itself). Assuming this is true, it then asks "does it contain a key-value pair that is (shape pentagon)?" If still true, it asks "does it contain a key-value pair that is (radius <some variable value>)?" This last question has the effect of extracting the value of the radius parameter that we created the designator with and making it available to PROLOG. Finally, it "asks" Lisp to create a turtle-shape structure with the ?radius value just discoveredand the number of edges selected via the shape parameter, and store this in ?action.+For the example rule we've selected, Prolog looks at the designator and asks "does it contain a key-value pair that is ''(:type :shape)''?" (so no variables here except the designator itself). Assuming this is true, it then asks "does it contain a key-value pair that is ''(:shape :pentagon)''?" If still true, it asks "does it contain a key-value pair that is ''(radius <some variable value>)''?" This last question has the effect of extracting the value of the radius parameter that we created the designator with and making it available to Prolog. Finally, it "asks" Lisp to create a ''turtle-shape'' structure with the ''?radius'' value just discovered and the number of edges selected via the shape parameter, and store this in ''?action''
 + 
 +If all of the above body rules are true (or can be executed sucessfully) then the head is itself evaluated, and what it does is glue the newly created ''?action'', the object containing the concrete values, to the designator ''?desig''. Or in more concrete terms, it defines what the value of the ''action-desig'' predicate is if applied to ''?desig'', and that value is a list containing the instruction ''draw-shape'' and the value of ''?action''.
  
-If all of the above body rules are true (or can be executed sucessfullythen the head is itself run, and what it does is glue the newly created ?action, the object containing the concrete values, to the designator ?desig. Or in more concrete terms, it defines what the value of the action-desig predicate is if applied to ?desig, and that value is a list containing the name 'shape' and the values of ?action.+If in any step of the inference Prolog stumbles upon ''false'' (or ''NIL'') it exists the current search branch and continues searching somewhere else.
  
-For this tutorial, the inference is very simple: in fact, it is just a conditional switch based on the shape parameter which translates a name like 'triangle' or 'pentagon' into the corresponding number of edges.+For this tutorial, the inference is very simple: in fact, it is just a conditional switch based on the shape parameter which translates a name like ''triangle'' or ''pentagon'' into the corresponding number of edges.
  
-Reload the tutorial package in roslisp_repl. This will also load the newly defined inference rules.+Reload the tutorial package in ''roslisp_repl''. This will also load the newly defined inference rules.
  
-Let's try to create, then reference a designator in the repl command line again:+Let's try to create, then reference a designator in the REPL command line again:
  
 <code lisp> <code lisp>
-TUT> (defparameter mydesig2 (make-designator 'action `((type shape) (shape triangle) (radius 5)))) +TUT> (defparameter my-desig-2 (make-designator :action '((:type :shape) (:shape :triangle) (:radius 5)))) 
-MYDESIG +MY-DESIG-2 
-TUT> (reference mydesig2+TUT> (reference my-desig-2
-(SHAPE #S(TURTLE-SHAPE :RADIUS 5 :EDGES 3))+(DRAW-SHAPE #S(TURTLE-SHAPE :RADIUS 5 :EDGES 3))
 </code> </code>