-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreif.ss
62 lines (54 loc) · 1.29 KB
/
reif.ss
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
54
55
56
57
58
59
60
61
62
(define truet (lambda () (lambda (?) (== #t ?))))
(define falset (lambda () (lambda (?) (== #f ?))))
(define ==t
(lambda (x y)
(lambda (?)
(conde
[(== #t ?) (== x y)]
[(== #f ?) (=/= x y)]))))
(define =/=t
(lambda (x y) (negt (==t x y))))
(define negt
(lambda (g)
(lambda (?)
(conde
[(== #t ?) (g #f)]
[(== #f ?) (g #t)]))))
(define-syntax conjt
;; A conjunction test
(syntax-rules ()
[(_) (truet)]
[(_ g) g]
[(_ g1 g2 gs ...)
(lambda (?)
(conde
[(g1 #t) ((conjt g2 gs ...) ?)]
[(== #f ?) (g1 #f)]))]))
(define-syntax disjt
;; A disjunction test
(syntax-rules ()
[(_) (falset)]
[(_ g) g]
[(_ g1 g2 gs ...)
(lambda (?)
(conde
[(== #t ?) (g1 #t)]
[(g1 #f) ((disjt g2 gs ...) ?)]))]))
(define-syntax fresht
(syntax-rules ()
[(_ (idens ...) gs ...)
(lambda (?) (fresh (idens ...) ((conjt gs ...) ?)))]))
;; Next: condt?
(define-syntax condo
;; Literally the relational version of 'cond'
;; Fails if no clauses match
(syntax-rules (else)
[(_ [else]) succeed]
[(_ [else g]) g]
[(_ [else g1 g2 g* ...])
(fresh () g1 g2 g* ...)]
[(_ [test g* ...] c* ...)
(conde
[(test #t) g* ...]
[(test #f) (condo c* ...)])]
[(_) fail]))