1 microKanren
| (require typed/microKanren) | package: miniKanren-typed |
This module is a Typed Racket rewrite of microKanren (Hemann and Friedman, μKanren: A Minimal Functional Core for Relational Programming, Scheme Workshop 2013), from the code at https://github.com/jasonhemann/microKanren: the core of microKanren.scm and the user-level forms of miniKanren-wrappers.scm. The core is a handful of functions over states and streams; everything else is defined on top of them.
The differences from that code are:
Types. Every definition has a type.
Variables. A variable is a struct holding its counter, (var 0), rather than the vector #(0), so that variables are distinguished from the other values of a term.
Literals. Literals are compared with equal? rather than eqv?, as in the other modules of this package.
1.1 Types
type
type
type constructor
(Streamof a)
type constructor
(Suspension a)
(define-type (Streamof a) (∪ Null (Pair a (Streamof a)) (Suspension a))) (define-type (Suspension a) (→ (Streamof a)))
A stream is empty, or an answer followed by a stream (mature), or a suspension that produces a stream when called (immature).
type
> ((== 1 1) empty-state) - : (Streamof State)
'((() . 0))
> ((call/fresh (λ (q) (== q 5))) empty-state) - : (Streamof State)
(list (cons (list (cons (var 0) 5)) 1))
1.2 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
s/c : a
value
procedure
$1 : (Streamof a) $2 : (Streamof a)
procedure
$ : (Streamof a) g : (→ a (Streamof b))
procedure
u : Term s : Substitution
procedure
(ext-s x v s) → Substitution
x : Var v : Term s : Substitution
procedure
(unify u v s) → (Option Substitution)
u : Term v : Term s : Substitution
1.3 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))
value
procedure
g : Goal
procedure
$ : (Streamof a)
procedure
n : Natural $ : (Streamof a)
procedure
$ : (Streamof a)
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