-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathex-ndfa.scm
87 lines (77 loc) · 1.63 KB
/
ex-ndfa.scm
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
;; from The Art of Prolog
;; Program 17.1
;; An interpreter for a nondeterministic finite automaton (NDFA)
(define-rel
(accept xs)
((accept-clause accept accept2 initial delta final))
()
(fresh (q)
(initial q)
(accept2 xs q)))
(define-rel
(accept2 xs q)
((accept2-clause accept2 initial delta final))
()
(conde
((fresh (x xr q1)
(== (cons x xr) xs)
(delta q x q1)
(accept2 xr q1)))
((== xs '())
(final q))))
;; Program 17.2
;; An NDFA that accepts that language (ab)*
(define-rel
(initial q)
((initial-clause initial))
()
(== q 'q0))
(define-rel
(final q)
((final-clause final))
()
(== q 'q0))
(define-rel
(delta qa c qb)
((delta-clause delta))
()
(conde
((== qa 'q0)
(== c 'a)
(== qb 'q1))
((== qa 'q1)
(== c 'b)
(== qb 'q0))))
(define (ndfa-clause a b)
(fresh (x y z)
(conde
((== `(accept ,x) a)
(accept-clause a b))
((== `(accept2 ,x ,y) a)
(accept2-clause a b))
((== `(initial ,x) a)
(initial-clause a b))
((== `(final ,x) a)
(final-clause a b))
((== `(delta ,x ,y ,z) a)
(delta-clause a b)))))
(define (should-fold g r)
(fresh (x y)
(conde
((== g `(accept2 ,x ,y))
(== r `(ab2 ,x ,y)))
((== g `(accept ,x))
(== r `(ab ,x))))))
(define (should-unfold g)
(fresh (x y z)
(conde
((== g `(initial ,x)))
((== g `(final ,x)))
((== g `(delta ,x ,y ,z))))))
(define (ndfa-pclause a b)
(fresh (x y z)
(conde
((== `(accept ,x) a)
(accept-clause a b))
((== `(accept2 ,x ,y) a)
(accept2-clause a b)))))