examples/zebra.lisp @ ba205f6b2875

Excise the stupid fucking `set-*` opcodes

The book uses the horribly-confusingly-named `set-*` operations for handling
subterms in query mode.  The author does this because he claims this is both
easier to understand and more performant.  In reality it is neither of these
things.

If you just name the subterm-handling opcodes something not completely stupid,
like `handle-subterm-*` instead of `unify-*` it becomes obvious what they do.

Also, despite the fact that `put-*` instructions now need to set the WAM's
`mode`, we still get about a 10% speedup here, likely from some combination of
reducing the VM loop code size and simplifying the compilation process.  So it's
not even more performant.

TL;DR: Just say "No" to `set-*`.
author Steve Losh <steve@stevelosh.com>
date Sun, 10 Jul 2016 14:21:18 +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)))