-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmicroKanren.rkt
40 lines (39 loc) · 1.23 KB
/
microKanren.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#lang racket
(provide (all-defined-out))
(define (var n) n)
(define (var? n) (number? n))
(define (ext-s x v s) (cons (cons x v) s))
(define (walk u s)
(let ((pr (and (var? u) (assv u s))))
(if pr (walk (cdr pr) s) u)))
(define (unify u v s)
(let ((u (walk u s)) (v (walk v s)))
(cond
((eqv? u v) s)
((var? u) (ext-s u v s))
((var? v) (ext-s v u s))
((and (pair? u) (pair? v))
(let ((s (unify (car u) (car v) s)))
(and s (unify (cdr u) (cdr v) s))))
(else #f))))
(define (== u v)
(lambda (s/c)
(let ((s (unify u v (car s/c))))
(if s (list (cons s (cdr s/c))) `()))))
(define (call/fresh f)
(lambda (s/c)
(let ((c (cdr s/c)))
((f (var c)) (cons (car s/c) (+ 1 c))))))
(define (disj g1 g2) (lambda (s/c) ($-append (g1 s/c) (g2 s/c))))
(define (conj g1 g2) (lambda (s/c) ($-append-map g2 (g1 s/c))))
(define ($-append $1 $2)
(cond
((procedure? $1) (lambda () ($-append $2 ($1))))
((null? $1) $2)
(else (cons (car $1) ($-append (cdr $1) $2)))))
(define ($-append-map g $)
(cond
((procedure? $) (lambda () ($-append-map g ($))))
((null? $) `())
(else ($-append (g (car $)) ($-append-map g (cdr $))))))
(define (call/empty-state g) (g (cons '() 0)))