All Manuals > KnowledgeWorks and Prolog User Guide > Appendix A: Common Prolog

A.5 Accessing Lisp From Common Prolog

It is apparent from the Common Prolog syntax that the first element of any valid goal expression must be a symbol. Common Prolog takes advantage of this fact and gives a special interpretation to a goal with a list in the first position. A list in the car of a goal is treated as a Lisp expression with normal Lisp evaluation rules. Any logic variables in the expression are instantiated with their values. (They must be bound). The rest of the goal expression should be a list of expressions to be unified with the values returned by the Lisp evaluation. Any extra values returned are ignored, and any extra expressions in the tail of a goal are unified with new unbound variables.

A.5.1 Examples

|==> ((print "foo"))
|
|"foo"
|YES.
|
|==> (and (= ?x 3) ((* ?x ?x) ?y)) 
                 ; Note that "?y" is unified with 9
|?X = 3
|?Y = 9
|
|==> ((* 3 3) 10)
|NO.
|
|==> ((floor 3 4) ?x ?y)
|
|?X = 0
|?Y = 3
|==> ((floor 3 4) ?x)
|
|?X = 0
|
|==> ((* 3 4) ?x ?y)
|
|?X = 12
|?Y = ?0   
  ; note that system generated variables look like:
  ; ?<integer>
|==> ((typep 3 'integer) ?x)
|
|?X = T
|
|==> ((typep 3 'integer) t)
|
|YES.
|
|==> (and ((floor 5 3) ?x) ((floor 4 3) ?x))
|
|?X = 1
|
|==> ((cons 3 4) (?x . ?y))
|
|?X = 3
|?Y = 4
|
|==> (and (= ?op *) ((list ?op 3 4) ?y) (call (?y ?z)))
|
|?OP = *
|?Y = (* 3 4)
|?Z = 12
|
|==> (and (defrel fact
|           ((fact 0 1))
|           ((fact ?x ?y)
|            ((- ?x 1) ?w)
|            (fact ?w ?z)
|            ((* ?z ?x) ?y)))
|         (fact 10 ?result))
|
|?X = ?0
|?Y = ?1
|?W = ?2
|?Z = ?3
|?RESULT = 3628800

KnowledgeWorks and Prolog User Guide (Unix version) - 01 Dec 2021 19:35:52