From: Keith James
Subject: CL + embedded Prolog
Date: 
Message-ID: <dc0845$n4r$1@s1.uklinux.net>
Hi,

I've been hacking on embedding Prolog in Lisp via FFI. I'm aware of the
various fine Prologs-in-Lisp already available (Allegro, Lispworks, PAIP, Qi
et al). I'm mostly doing this for fun and to learn CL and Prolog.

I've got to the stage where I've embedded SWI-Prolog in CL using the
Lispworks FLI. Passing in a Lisp form similar to those used by LW
Prolog it can successfully do things like solve the "einstein
riddle" (below).

So my question is would this system be useful to anyone else?

If so, I'm considering defining proper packages and interface, UFFI-izing
the LW FLI away, abstracting to allow other Prolog backends e.g. XSB
Prolog. Oh, and documentation ;)

CL-USER> 
(deflogic %einstein "einstein/2")

(prolog '(%assert (%-
                   (%einstein ?h ?f)
                   (%and
                    (%= ?h ((%house norwegian ?_ ?_ ?_ ?_)
                            ?_
                            (%house ?_ ?_ ?_ milk ?) ?_ ?_))
                    (%member (%house british ?_ ?_ ?_ red) ?h)
                    (%member (%house swedish dog ?_ ?_ ?_) ?h)
                    (%member (%house danish ?_ ?_ tea ?_) ?h)
                    (%iright (%house ?_ ?_ ?_ ?_ green)
                             (%house ?_ ?_ ?_ ?_ white) ?h)
                    (%member (%house ?_ ?_ ?_ coffee green) ?h)
                    (%member (%house ?_ bird pallmall ?_ ?_) ?h)
                    (%member (%house ?_ ?_ dunhill ?_ yellow) ?h)
                    (%nextto (%house ?_ ?_ dunhill ?_ ?_)
                             (%house ?_ horse ?_ ?_ ?_) ?h)
                    (%member (%house ?_ ?_ ?_ milk ?_) ?h)
                    (%nextto (%house ?_ ?_ marlboro ?_ ?_)     
                             (%house ?_ cat ?_ ?_ ?_) ?h)
                    (%nextto (%house ?_ ?_ marlboro ?_ ?_)
                             (%house ?_ ?_ ?_ water ?_) ?h)
                    (%member (%house ?_ ?_ winfield beer ?_) ?h)
                    (%member (%house german ?_ rothmans ?_ ?_) ?h)
                    (%nextto (%house norwegian ?_ ?_ ?_ ?_)
                             (%house ?_ ?_ ?_ ?_ blue) ?h)
                    (%member (%house ?f fish ?_ ?_ ?_) ?h)))))
T

CL-USER> 
(query-prolog '(%einstein ?houses ?fish-owner))
((?FISH-OWNER . |german|) (?HOUSES (|house| |norwegian| |cat| |dunhill| 
water| |yellow|) (|house| |danish| |horse| |marlboro| |tea| |blue|) (
house| |british| |bird| |pallmall| |milk| |red|) (|house| |german| |fish| 
rothmans| |coffee| |green|) (|house| |swedish| |dog| |winfield| |beer| 
white|)))

T


Solving 100 times:

(defun test ()
  (dotimes (n 100)
    (query-prolog '(%einstein ?houses ?fish-owner))))

CL-USER> (time (test))
Timing the evaluation of (TEST)

user time    =     18.440
system time  =      0.010
Elapsed time =   0:00:19
Allocation   = 283072 bytes standard / 198000 bytes conses
0 Page faults

Disclaimer: this is my first Lisp project (and I also know very little
Prolog!) so expect it all to be a bit ugly to start with.

I guess I should fill in my Road-to-Lisp now...

Keith

-- 
Keith James, Cambridge, UK