-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexamples.rkt
53 lines (41 loc) · 1.26 KB
/
examples.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#lang racket
(require "mykanren.rkt")
(require (except-in rackunit fail))
; "fail" is already defined in rackunit, so you need to use except-in when importing rackunit
(check-equal?(run 1 (q) (== 'olive q) (== 'oil q)) '())
(check-equal? (run 1 (q) (== 'olive q))
'((olive)))
(check-equal? (run 4237 (q) (== 'olive q))
'((olive)))
(check-equal? (run 0 (q) (== 'olive q))
'())
(defrel (conso f r out)
(== (cons f r) out))
(check-equal? (run* q (conso 1 q '(1 2 3))) '((2 3)))
(defrel (caro l out)
(fresh (r)
(conso out r l)))
(defrel (cdro l out)
(fresh (f)
(conso f out l)))
(defrel (nullo l)
(conde
[(== l '())]))
(defrel (appendo l t out)
(conde
[(nullo l)(== t out)]
[(fresh (a d res)
(conso a d l)
(conso a res out)
(appendo d t res))]))
(check-equal? (run 5 (q w) (appendo q w '(1 2 3)))
'((() (1 2 3))
((1) (2 3))
((1 2) (3))
((1 2 3) ())))
(check-equal? (run 5 (q w e) (appendo q w e))
'((() _0 _0)
((_0) _1 (_0 . _1))
((_0 _1) _2 (_0 _1 . _2))
((_0 _1 _2) _3 (_0 _1 _2 . _3))
((_0 _1 _2 _3) _4 (_0 _1 _2 _3 . _4))))