A.12 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.

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

Defines a simple predicate that just runs lisp code and doesn't have to unify any variables. Arguments are referenced with: (special-arg <argnum> ) . Succeeds by default. If a failure case arises, use: (detpred-fail <name> <num args> ) .

(defdetpred integer 1
  (unless (integerp (special-arg 0))
    (go fail)))
(defdetunipred <name
> <num args
> <unifier1
> <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).

(defdetunipred 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 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 - 4 Apr 2005