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

A.13 Adding Built-in Predicates

Common Prolog provides several special forms for adding new predicates written in Lisp. Each one is described below, with an example.

A.13.1 The defdetpred form

The syntax of this form is:

(defdetpred <name> <num-args> <body>)

which defines a simple predicate that just runs Lisp code and does not have to unify any variables. Arguments are referenced with: (special-arg <num>). The body succeeds by default, but if a failure case arises, use: (detpred-fail <name> <num-args>).

For example:

(defdetpred my-integer 1
  (unless (integerp (special-arg 0))
  (detpred-fail my-integer 1)))

A.13.2 The defdetunipred form

The syntax of this form is:

(defdetunipred <name> <num-args> <unifier1 unifier2> 
              <aux-vars> <body>)

defdetunipred is used when the defined predicate needs to unify values with arguments (or unify in general). The body is executed and, if successful, (that is, detpred-fail has not been called) unification is performed on the two unifiers. (If more than two items need to be unified, cons up lists of items to unify).

For example:

(defdetunipred my-arg 3 (temp1 temp2) 
         (temp1 temp2 index term value)
         (setf index (special-arg 0)
               term (special-arg 1)
               value (special-arg 2))
         (unless (and (numberp index)
                      (plusp index)
                      (or (and (term-p term)
                               (< index (length term)))
                          (and (consp term) 
                               (< index 3))))
                 (detpred-fail my-arg 3))
         (if (consp term)
             (setf temp1 (if (= index 1)
                             (car term)
                             (cdr term)))
            (setf temp1 (term-ref term index)))
         (setf temp2 value))

KnowledgeWorks and Prolog User Guide (Macintosh version) - 01 Dec 2021 19:35:39