examples/zebra.lisp @ 72bbdd515725

Rewrite the compiler

A few days ago I found a bug in the compiler that I decided merited basically an
entire rewrite of it.

This was long overdue.  The compiler kind of grew organically and unhealthily
over time as I wrapped my head around how the whole WAM is structured, and now
that I understand a lot more I can do things right.

This new implementation is a lot "flatter" than the old one.  It makes use of
CLOS classes and generic methods to un-nest a lot of the crap that was
previously happening in bigass `labels` blocks.  This is a lot easier to read
and understand because you can take things a piece at a time.

Unfortunately, it's currently a lot slower than the old one.  But at least it's
*correct*, and now I can start taking a look at optimizing the performance with
a cleaner base to start from.

Notes/ideas for the near future:

* Switch to structs instead of CLOS classes for all the bits and bobs in the
  compilation process.
* Inline hot functions in the compilation process.
* Type hint the fucking compiler already.  I've put this off for far too long.
* Move the compiler to its own package for easier profiling and to maintain my
  shreds of sanity.
* Look into that generic-function-inlining library thing I saw on Reddit...
* Remove the last vestiges of `match` and kill the dependency on optima.
author Steve Losh <steve@stevelosh.com>
date Tue, 07 Jun 2016 14:49:20 +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)))