2 First Edition
| (require typed/miniKanren/amb/trs1) | package: ambKanren |
This module implements the language of the first edition of The Reasoned Schemer on top of amb.
2.1 Implementation Basis
The core follows microKanren: a goal returns one substitution and chooses among its answers with amb. Unlike microKanren, there is no state and no variable counter (fresh creates variables directly). Substitutions are association lists, as in miniKanren-typed.
The user-level language is that of the first edition of The Reasoned Schemer (Friedman, Byrd, and Kiselyov, MIT Press, 2005): run with a single query variable, conde with else, the interleaving forms condi and alli, and the committed-choice forms conda and condu. “Chapter n” below refers to that edition.
The differences from the book are:
Goals use amb in place of streams. Unification through == uses the occurs check, as in miniKanren-typed, and fails with (amb-fail).
conde is depth-first, as in the first edition, and uses amb-append. The interleaving forms condi and alli follow mplusi and bindi of the first edition (see Interleaving). With the default shuffler, the book examples covered by the tests give the same answers in the same order.
2.2 Types
type
type
type
type
type
2.3 Running Programs
2.4 Goals
> (run* (q) (fresh (x) (== q (cons x 2)) (== x 1))) - : (Listof Term)
'((1 . 2))
> (run* (q) (== q (list q))) - : (Listof Term)
'()
syntax
(fresh (x ...) goal ...)
syntax
(λG (s) body ...+)
> (: repeato (→ Term Term Goal))
> (define (repeato x l) (conde [(== l '())] [(fresh (d) (== l (cons x d)) (repeato x d))])) > (run 6 (q) (conde [(repeato 'a q)] [(repeato 'b q)])) - : (Listof Term)
'(() (a) (a a) (a a a) (a a a a) (a a a a a))
> (run 6 (q) (condi [(repeato 'a q)] [(repeato 'b q)])) - : (Listof Term)
'(() () (a) (b) (a a) (b b))
> (run* (q) (conda [(conde [(== q 1)] [(== q 2)])] [(== q 3)])) - : (Listof Term)
'(1 2)
> (run* (q) (condu [(conde [(== q 1)] [(== q 2)])] [(== q 3)])) - : (Listof Term)
'(1)
> (run* (q) (condu [(conde [(== q 1)] [(== q 2)])])) - : (Listof Term)
'(1 2)
syntax
(ife g0 g1 g2)
syntax
(ifi g0 g1 g2)
syntax
(ifa g0 g1 g2)
syntax
(ifu g0 g1 g2)
syntax
(cond-aux ifer clause ...)
syntax
(all-aux bnd goal ...)
2.4.1 Interleaving
condi chooses clauses with amb-alternate, which passes the turn at every answer. alli binds the answers of each non-final goal through a local search (amb-local) under the same merger and continues from each answer. These forms take the place of the stream-based mplusi and bindi.
In the example below, counto has an answer for every length, and the answers with 'R are still reached:
2.5 Substitutions and Unification
value
procedure
s : Substitution
procedure
(ext-s x v s) → Substitution
x : Var v : Term s : Substitution
procedure
v : Term s : Substitution
procedure
v : Term s : Substitution
procedure
x : Var v : Term s : Substitution
procedure
(unify u v s) → Substitution
u : Term v : Term s : Substitution
procedure
(reify-name n) → Symbol
n : Index
procedure
(reify-s v s) → Substitution
v : Term s : Substitution
procedure
v : Term
procedure
(sequence->amb s) → a
s : (Sequenceof a)
procedure
(list->amb l) → a
l : (Listof a)