-
Notifications
You must be signed in to change notification settings - Fork 9
/
meta.clj
170 lines (151 loc) · 5.32 KB
/
meta.clj
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
(ns logically.art.interpreters.meta
(:refer-clojure :exclude [==])
(:use [clojure.core.logic :exclude [is] :as l]
[clojure.core.logic.nominal :exclude [fresh hash] :as nom]))
;; Section 17.2 Meta-Interpreters
;; Program 17.5 A meta-interpreter for pure Prolog
(defn solve-for* [clause]
(letfn [(solve [goals]
(conde
[(== goals ())]
[(fresh [g gs b]
(conso g gs goals)
(clause g b)
(solve b)
(solve gs))]))]
solve))
(defn solve-for [clause]
(let [solver* (solve-for* clause)]
(fn [goal] (solver* [goal]))))
(defn solver-member-clause [a b]
(conde
[(fresh [x xs]
(== a ['member x (lcons x xs)])
(== b ()))]
[(fresh [x y ys]
(== a ['member x (lcons y ys)])
(== b [['member x ys]]))]))
(def ex-solver-member (solve-for solver-member-clause))
;; Program 17.6 A meta-interpreter for pure Prolog in continutation style
(defn alt-solve-for [clause]
(letfn [(solve0 [goal]
(solve [goal] ()))
(solve [gs1 gs2]
(conde
[(== gs1 ())
(== gs2 ())]
[(== gs1 ())
(fresh [g gs]
(conso g gs gs2)
(solve [g] gs))]
[(fresh [g gs gsr b]
(conso g gs gs1)
(clause g b)
(appendo gs gs2 gsr)
(solve b gsr))]))]
solve0))
(def ex-alt-solver-member (alt-solve-for solver-member-clause))
;; Program 17.7 A tracer for Prolog
(defn solve-trace-for [clause]
(letfn [(solve0 [goal]
(solve [goal] 0))
(solve [goals depth]
(conde
[(== goals ())]
[(fresh [g gs b]
(conso g gs goals)
(clause g b)
(display g depth)
(solve b (inc depth))
(solve gs depth))]))
(display [goal depth]
(fn [a]
(println (apply str (repeat depth " ")) (walk* a goal))
a))]
solve0))
(def ex-trace-solver-member (solve-trace-for solver-member-clause))
;; Program 17.8 A meta-interpreter for building a proof tree
(defn solve-proof-for [clause]
(letfn [(solve0 [goal tree]
(solve [goal] tree))
(solve [goals tree]
(conde
[(== goals ())
(== tree ())]
[(fresh [g gs ts b tb]
(conso g gs goals)
(clause g b)
(conso [g '<-- tb] ts tree)
(solve b tb)
(solve gs ts))]))]
solve0))
(def ex-proof-solver-member (solve-proof-for solver-member-clause))
;; Program 17.9 A meta-interpreter for reasoning about uncertainty
(defn solve-cf-for [clause-cf chain-cf conj-cf]
(letfn [(solve0 [goal certainty]
(solve [goal] certainty))
(solve [goals certainty]
(conde
[(== goals ())
(== certainty 1)]
[(fresh [g gs b cg cb cs]
(conso g gs goals)
(clause-cf g b cg)
(solve b cb)
(solve gs cs)
(project [cg cb cs]
(== certainty (conj-cf (chain-cf cg cb) cs))))]))]
solve0))
(defn make-clause-cf [clause]
(fn [a b c]
(all
(== c 1)
(clause a b))))
(def ex-cf-solver-member (solve-cf-for (make-clause-cf solver-member-clause) * min))
(defn solver-family-clause-cf [a b c]
(conde
[(== [a b c] '((parent adam chris) () 0.5))]
[(== [a b c] '((parent allan chris) () 0.25))]
[(== [a b c] '((parent aziz chris) () 0.25))]
[(== [a b c] '((parent chris diana) () 1))]
[(== [a b c] '((parent chris emily) () 0.1))]
[(== [a b c] '((ancestor sam adam) () 0.7))]
[(== [a b c] '((ancestor sam allan) () 0.8))]
[(fresh [parent child grandparent]
(== a ['grandparent grandparent child])
(== b [['parent parent child] ['parent grandparent parent]])
(== c 1))]
[(fresh [parent child]
(== a ['ancestor parent child])
(== b [['parent parent child]])
(== c 1))]
[(fresh [ancestor parent child]
(== a ['ancestor ancestor child])
(== b [['parent parent child] ['ancestor ancestor parent]])
(== c 1))]))
(defn make-clause-from-clause-cf [clause-cf]
(fn [a b]
(fresh [c]
(clause-cf a b c))))
(def ex-solver-family (solve-for (make-clause-from-clause-cf solver-family-clause-cf) ))
(def ex-cf-solver-family (solve-cf-for solver-family-clause-cf * *))
;; Program 17.10 Reasoning with uncertainty with threshold cutoff
(defn solve-cft-for [clause-cf chain-cf conj-cf chain-t]
(letfn [(solve0 [goal certainty t]
(solve [goal] certainty t))
(solve [goals certainty t]
(conde
[(== goals ())
(== certainty 1)]
[(fresh [g gs b cg cb cs tb]
(conso g gs goals)
(clause-cf g b cg)
(project [t cg]
(== (> cg t) true)
(== tb (chain-t t cg)))
(solve b cb tb)
(solve gs cs t)
(project [cg cb cs]
(== certainty (conj-cf (chain-cf cg cb) cs))))]))]
solve0))
(def ex-cft-solver-family (solve-cft-for solver-family-clause-cf * * /))