examples/zebra.lisp @ ba96e98a1d54

Add precompilation of static queries at compile time

Imagine a function like this:

    (defun legal-moves ()
      (query (legal ?who ?move)))

The argument to `query` there is constant, so we can compile it into WAM
bytecode once, when the Lisp function around it is compiled.  Then running the
query doesn't need to touch the Bones compiler -- it can just load the bytecode
from an array and first up the VM loop.

This saves a lot of time (and consing) compared to compiling the same query over
and over at runtime.
author Steve Losh <steve@stevelosh.com>
date Sun, 17 Jul 2016 16:49:06 +0000
parents ae2b13a9a629
children (none)
(in-package #:bones.paip)

(clear-db)

(rule (member ?item (?item . ?)))
(rule (member ?item (? . ?rest))
      (member ?item ?rest))

(rule (next-to ?x ?y ?list)
      (in-order ?x ?y ?list))

(rule (next-to ?x ?y ?list)
      (in-order ?y ?x ?list))

(rule (in-order ?x ?y (?x ?y . ?)))
(rule (in-order ?x ?y (? . ?rest))
      (in-order ?x ?y ?rest))

(rule (= ?x ?x))

(rule
 (zebra ?houses ?water-drinker ?zebra-owner)
 ;; Houses are of the form:
 ;; (HOUSE ?country ?pet ?cigarette ?drink ?color)

 (= ?houses
    ((house norway ? ? ? ?)
     ?
     (house ? ? ? milk ?)
     ?
     ?))

 (member   (house england ?      ?            ?            red   ) ?houses)
 (member   (house spain   dog    ?            ?            ?     ) ?houses)
 (member   (house ?       ?      ?            coffee       green ) ?houses)
 (member   (house ukraine ?      ?            tea          ?     ) ?houses)
 (member   (house ?       snails winston      ?            ?     ) ?houses)
 (member   (house ?       ?      kools        ?            yellow) ?houses)
 (member   (house ?       ?      lucky-strike orange-juice ?     ) ?houses)
 (member   (house japan   ?      parliaments  ?            ?     ) ?houses)
 (in-order (house ?       ?      ?            ?            ivory )
           (house ?       ?      ?            ?            green ) ?houses)
 (next-to  (house ?       ?      chesterfield ?            ?     )
           (house ?       fox    ?            ?            ?     ) ?houses)
 (next-to  (house ?       ?      kools        ?            ?     )
           (house ?       horse  ?            ?            ?     ) ?houses)
 (next-to  (house norway  ?      ?            ?            ?     )
           (house ?       ?      ?            ?            blue  ) ?houses)

 (member (house ?water-drinker ? ? water ?) ?houses)
 (member (house ?zebra-owner zebra ? ? ?) ?houses))

(time (query-all (zebra ?houses ?water ?zebra)))
; (declaim (optimize (speed 3) (safety 0)))