Reputation: 307
I have this problem in Common Lisp. I need to manipulate existential variables introducing the rule of skolemization.
For example I need to buid a function which turns
(exist ?x (p ?x))
in (p sk00042)
.
sk00042
is a variable. Note that this function becomes a bit harder when universal variables are involved.
For example, the function given the expression (forall ?y (exist ?x (p ?x ?y))
turns it into (forall ?y (p (sf666 ?y) ?y)
.
The idea is that the existencial variable tells me that there is something that satisfies the formulae. If this existential quantifier is the outer , then this quantifier does not depend on anything and the variable ?x
in the first example above should be replaced with the constant skoo42
which is generated by this function :
(defun skolem-variable () (gentemp "SV-"))
.
If the harder (second) case takes place and there's a universal quantifier "out" of the existential one, then that something that exists depends on variables universally quantified, meaning that the function must take care of this dependence and the universal variables become incorporated in the constant, like in the example :
(forall ?y (exist ?x (p ?x ?y))
----> (forall ?y (p (sf666 ?y) ?y)
For this also serves the function:
(defun skolem-function* (&rest args) (cons (gentemp "SF-") args))
(defun skolem-function (args) (apply #'skolem-function* args))
Here are some examples to get more familiar with the idea :
(skolemize '(forall ?y (exist ?x (p ?x ?y))))
;=> (forall ?y (P (SF-1 ?Y) ?Y))
(skolemize '(exist ?y (forall ?x (p ?x ?y))))
;=> (for all ?x (P ?X SV-2)
(skolemize '(exist ?y (and (p ?x) (f ?y))))
;=> (AND (P ?X) (F SV-4))
(skolemize '(forall ?x (exist ?y (and (p ?x) (f ?y)))))
;=> (forall ?x (AND (P ?X) (F (SF-5 ?X)))
I need to build the function (using skolem-variable
and skolem-function
above) that given
an expression controls if the outer is exist, then replaces the variable with skolem-variable. If the outer is a forall followed by and exist, the function does what i've explained above.
Upvotes: 2
Views: 461
Reputation: 14291
I just skimmed the Wikipedia article on the skolem normal form, but if I get it right, every existential becomes a skolem function invocation with the bound universals as arguments (or a skolem constant if no universals are in scope). One simple approach would be having a stack of bound universals while walking the expression tree recursively:
(defun skolemize (form &optional (universals nil))
(cond ((null form) nil) ; subtree done
((consp (car form)) ; walk branches
(cons (skolemize (car form) universals)
(skolemize (cdr form) universals)))
((eq (car form) 'forall) ; universal binding
(list 'forall
(cadr form)
(skolemize (caddr form) ; skolemize body
(cons (cadr form) universals)))) ; new var on the stack
((eq (car form) 'exist) ; existential binding
(subst (if universals ; substitute variables
(cons (gentemp "SF-") universals) ; with skolem function
(gentemp "SV-")) ; with skolem constant
(cadr form)
(skolemize (caddr form) universals)))
(t (cons (car form) (skolemize (cdr form) universals)))))
Note that this is just to get you started – I neither delved into this topic, nor is this really tested or optimized for performance or elegance. Also, it will accept malformed input, e.g. (skolemize '(forall (foo bar)))
.
Your examples:
CL-USER> (skolemize '(exist ?x (p ?x)))
(P SV-16)
CL-USER> (skolemize '(forall ?y (exist ?x (p ?x ?y))))
(FORALL ?Y (P (SF-17 ?Y) ?Y))
CL-USER> (skolemize '(exist ?y (forall ?x (p ?x ?y))))
(FORALL ?X (P ?X SV-18))
CL-USER> (skolemize '(exist ?y (and (p ?x) (f ?y))))
(AND (P ?X) (F SV-19))
CL-USER> (skolemize '(forall ?x (exist ?y (and (p ?x) (f ?y)))))
(FORALL ?X (AND (P ?X) (F (SF-20 ?X))))
Testing a more complex expression:
CL-USER> (skolemize '(exist ?a
(forall ?b
(exist ?c
(forall ?d
(exist ?e (and (or (and (f ?a) (g ?b))
(and (f ?c) (g ?d)))
(or (and (f ?c) (g ?e))
(and (f ?d) (g ?e))))))))))
(FORALL ?B
(FORALL ?D (AND (OR (AND (F SV-15) (G ?B))
(AND (F (SF-16 ?B)) (G ?D)))
(OR (AND (F (SF-16 ?B)) (G (SF-17 ?D ?B)))
(AND (F ?D) (G (SF-17 ?D ?B)))))))
Upvotes: 2