/
protocols.clj
243 lines (167 loc) · 5.26 KB
/
protocols.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
(ns clojure.core.logic.protocols)
;; Marker Interfaces
(definterface IBindable)
(definterface ITreeTerm)
(definterface IVar)
;; =============================================================================
;; Utility Protocols
(defprotocol IUninitialized
(-uninitialized [coll]))
;; =============================================================================
;; miniKanren Protocols
;; -----------------------------------------------------------------------------
;; Unification protocols for core Clojure types
(defprotocol IUnifyTerms
(unify-terms [u v s]))
(defprotocol IUnifyWithRecord
(unify-with-record [u v s]))
(definterface INonStorable)
(defn non-storable? [x]
(instance? INonStorable x))
;; -----------------------------------------------------------------------------
;; Utility protocols
(defprotocol LConsSeq
(lfirst [this])
(lnext [this]))
(defprotocol LConsPrint
(toShortString [this]))
;; -----------------------------------------------------------------------------
;; Substitution
(defprotocol ISubstitutions
(ext-no-check [this x v])
(walk [this x]))
;; -----------------------------------------------------------------------------
;; Protocols for terms
(defprotocol IReifyTerm
(reify-term [v s]))
(defprotocol IWalkTerm
(walk-term [v f]))
(defprotocol IOccursCheckTerm
(occurs-check-term [v x s]))
(defprotocol IBuildTerm
(build-term [u s]))
;; -----------------------------------------------------------------------------
;; Goal protocols
(defprotocol IBind
(bind [this g]))
(defprotocol IMPlus
(mplus [a f]))
(defprotocol ITake
(take* [a]))
;; -----------------------------------------------------------------------------
;; soft cut & committed choice protocols
(defprotocol IIfA
(ifa [b gs c]))
(defprotocol IIfU
(ifu [b gs c]))
;; =============================================================================
;; Rel protocols
(defprotocol IRel
(setfn [this arity f])
(indexes-for [this arity])
(add-indexes [this arity index]))
;; =============================================================================
;; Tabling protocols
(defprotocol ITabled
(-reify-tabled [this v])
(reify-tabled [this v])
(reuse [this argv cache start end])
(subunify [this arg ans]))
(defprotocol ISuspendedStream
(ready? [this]))
(defprotocol IAnswerCache
(-add [this x])
(-cached? [this x]))
;; =============================================================================
;; cKanren protocols
(defprotocol ISubstitutionsCLP
(root-val [this x])
(root-var [this x])
(ext-run-cs [this x v])
(queue [this c])
(update-var [this x v]))
;; -----------------------------------------------------------------------------
;; Constraint Store
(defprotocol IConstraintStore
(addc [this a c])
(updatec [this a c])
(remc [this a c])
(runc [this c state])
(constraints-for [this a x ws])
(migrate [this x root a]))
;; -----------------------------------------------------------------------------
;; Generic constraint protocols
;; Step, update the constraint with latest domain information
(defprotocol IConstraintStep
(-step [c s]))
;; the following assume implementation of -step
(defprotocol IRunnable
(-runnable? [c]))
(defprotocol IEntailed
(-entailed? [c]))
(defprotocol IEntailedVar
(-entailed-var? [c x]))
;; Contraint reflection protocols
(defprotocol IWithConstraintId
(-with-id [c id]))
(defprotocol IConstraintId
(-id [c]))
(defn id [c]
(if (instance? clojure.core.logic.protocols.IConstraintId c)
(-id c)
(-> c meta ::id)))
(defn with-id [c id]
(if (instance? clojure.core.logic.protocols.IWithConstraintId c)
(-with-id c id)
(vary-meta c assoc ::id id)))
(defprotocol IConstraintWatchedStores
(-watched-stores [c]))
(defprotocol IConstraintOp
(-rator [c])
(-rands [c]))
(defprotocol IReifiableConstraint
(-reifyc [c v r a]))
(defprotocol IVerifyConstraint
(-verify [c a cs]))
(defn reifiable? [x]
(instance? clojure.core.logic.protocols.IReifiableConstraint x))
(definterface IEnforceableConstraint)
(defn enforceable? [x]
(instance? clojure.core.logic.protocols.IEnforceableConstraint x))
;; cgoal
(defprotocol IUnwrapConstraint
(-unwrap [c]))
;; generic domain related protocols
(defprotocol IMergeDomains
(-merge-doms [a b]))
(defprotocol IMemberCount
(-member-count [dom]))
(defprotocol IForceAnswerTerm
(-force-ans [v x]))
;; -----------------------------------------------------------------------------
;; Tree Constraints
(defprotocol IDisunifyTerms
(-disunify-terms [u v s cs]))
(definterface ITreeConstraint)
(defn tree-constraint? [x]
(instance? clojure.core.logic.protocols.ITreeConstraint x))
(defprotocol IPrefix
(-prefix [c]))
(defprotocol IWithPrefix
(-with-prefix [c p]))
;; -----------------------------------------------------------------------------
;; Partial Maps
(defprotocol IUnifyWithPMap
(unify-with-pmap [pmap u s]))
;; -----------------------------------------------------------------------------
;; Deep constraints
(defprotocol IConstrainTree
(-constrain-tree [t fc s]))
;; -----------------------------------------------------------------------------
;; Features
(defprotocol IFeature
(-feature [x]))
;; -----------------------------------------------------------------------------
;; Jonc
(defprotocol IJonc
(-joncf [this]))