1 microKanren
| (require typed/microKanren/amb) | package: ambKanren |
This module is microKanren (Hemann and Friedman, μKanren: A Minimal Functional Core for Relational Programming, Scheme Workshop 2013) with its streams replaced by amb. It follows, definition by definition, typed/microKanren of the miniKanren-typed package, which is the Typed Racket version of the code at https://github.com/jasonhemann/microKanren. The definitions that only manipulate streams are gone.
1.1 From Streams to amb
In microKanren, a goal returns a stream of states: all its answers, produced lazily. Conjunction uses stream bind to run one goal on each answer of another. With amb, a goal has a single state as its return type, chooses among its answers with amb, and fails by calling (amb-fail).
|
| |||
|
| |||
success |
| (unit s/c) |
| s/c |
failure |
| mzero |
| (amb-fail) |
| (mplus (g1 s/c) (g2 s/c)) |
| ((amb g1 g2) s/c) | |
| (bind (g1 s/c) g2) |
| (g2 (amb-local (g1 s/c))) | |
|
| |||
| force and collect the stream (g empty-state) |
| advance amb-generate* until enough answers arrive |
Success returns the state unchanged, disjunction is a choice made by amb, and conjunction runs g2 on each answer of a local search of g1. amb-generate* provides a generator of answers and pauses. amb-local generates the events of g1 on demand, supplying each answer as an alternative of one choice that continues in g2. This implements the same scheduling as bind with the stream of g1 under amb-interleave and the default shuffler.
The definitions that remain are those of typed/microKanren, with these changes:
unit, mzero, mplus, bind, pull, and the stream types are removed.
take and take-all take a thunk that performs the nondeterministic computation, instead of a stream, and call/goal returns a state.
unify calls (amb-fail) instead of returning #f, so it returns a substitution, and == needs no test.
1.1.1 Suspension and the Order of Answers
In microKanren, Zzz creates a suspended stream. Here it calls amb-pause before the goal. The wrapper forms that use Zzz inherit this pause. Calling a goal directly adds no pause of its own. The default merger of amb, amb-append, does not pass the turn at a pause, so the examples select amb-interleave explicitly.
> (: relo (→ Term Goal)) > (define (relo x) (fresh (a d) (== x `(,a . ,d)) (relo x))) > (run 1 (x) (disj+ (relo '(5 . 6)) (== x 3))) - : (Listof Term)
'(3)
With amb-interleave and the default shuffler, conjunction uses a local search to follow the answer order of the stream-based implementation. Plain function composition, (g2 (g1 s/c)), runs g2 inside the choices of g1, so the merger interleaves the runs of g2 following the shape of those choices. bind interleaves them in the order of the answers of g1, with the pauses of g1 in between. When both versions completely enumerate a pure search without pruning, they produce the same answers in possibly different orders. A bounded run observes the difference.
Stream bind and conj can both change the order of answers when conjunctions are regrouped. Conjunction is therefore not associative under equality of ordered answers.
> (: association-query (→ Boolean (Listof Term)))
> (define (association-query left?) (run 2 (q x y z) (== q (list x y z)) (let ([a (disj (== x 'a) (== x 'b))] [b (disj (== y 'l) (== y 'r))] [c (disj (== z 'u) (Zzz (== z 'v)))]) (if left? (conj (conj a b) c) (conj a (conj b c)))))) > (association-query #t) - : (Listof Term)
'((a l u) (a r u))
> (association-query #f) - : (Listof Term)
'((a l u) (b l u))
1.2 Types
type
type
type
> ((== 1 1) empty-state) - : State
'(() . 0)
> ((call/fresh (λ (q) (== q 5))) empty-state) - : State
(cons (list (cons (var 0) 5)) 1)
> (take-all (λ () ((call/fresh (λ (q) (disj (== q 5) (== q 6)))) empty-state))) - : (Listof State)
(list (cons (list (cons (var 0) 5)) 1) (cons (list (cons (var 0) 6)) 1))
1.3 The Core
procedure
(call/fresh f) → Goal
f : (→ Var Goal)
> (take-all (λ () ((conj (call/fresh (λ (a) (== a 7))) (call/fresh (λ (b) (disj (== b 5) (== b 6))))) empty-state))) - : (Listof State)
(list
(cons (list (cons (var 1) 5) (cons (var 0) 7)) 2)
(cons (list (cons (var 1) 6) (cons (var 0) 7)) 2))
procedure
u : Term s : Substitution
procedure
(ext-s x v s) → Substitution
x : Var v : Term s : Substitution
procedure
(unify u v s) → Substitution
u : Term v : Term s : Substitution
1.4 User-Level Forms
These are the forms of miniKanren-wrappers.scm.
syntax
(Zzz goal)
syntax
(conj+ goal ...+)
syntax
(disj+ goal ...+)
syntax
(fresh (x ...) goal ...+)
syntax
(conde [goal ...+] ...+)
> (: appendo (→ Term Term Term Goal))
> (define (appendo l s out) (conde [(== '() l) (== s out)] [(fresh (a d res) (== `(,a . ,d) l) (== `(,a . ,res) out) (appendo d s res))])) > (run* (x y) (appendo x y '(1 2 3))) - : (Listof Term)
'(() (1) (1 2) (1 2 3))
procedure
s/c : State
procedure
v : Term s : Substitution
procedure
(reify-s v s) → Substitution
v : Term s : Substitution
procedure
(reify-name n) → Symbol
n : Natural