-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcascade.clj
222 lines (208 loc) · 7.44 KB
/
cascade.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
(ns dyntab.cascade
(:require [clojure.core.reducers :as r]
[clojure.set :as set]
[dyntab.util :as u]
[dyntab.bag :as bag]
[dyntab.syntax :as syntax]
[dyntab.tableau :as tab]))
(defn tableau-cascade [form]
(bag/post (bag/tuple-bag [bag/index-by-arity
bag/index-pairs-by-first
bag/index-triples-by-first-second
bag/index-triples-by-second
bag/index-triples-by-third])
[[:start-tableau]
[:start-tableau (tab/tableau form)]]))
(defn newest-tableau [casc t]
(-> (bag/newest casc [:pairs-by-first t])
(second)))
(defn saturate-tableau
([] [:by-arity 2])
([casc [t tab]]
(->> [tab]
(tab/saturate)
((fn [[changed tableaux]]
(if changed
(r/map (fn [new-tab] [t new-tab])
tableaux))))
(r/foldcat)
(vector))))
(defn meta-process [tab rules]
[(r/fold
u/mmap-merge
(second (bag/process tab :meta-post-mark rules)))])
(defn meta-post [casc tables]
(->> (apply u/mmap-merge tables)
(r/mapcat (fn [t coll]
(if t
[[t (-> (newest-tableau casc t)
(bag/mark :meta-post-mark)
(bag/post coll))]]
coll)))
(r/foldcat)
(bag/post casc)))
(defn prime
([] [:by-arity 2])
([casc [t tab]]
(meta-process
tab
[(fn
([] [:pairs-by-fsecond :!])
([cur-tab [n [op form1 form2] :as node-label]]
(let [new-t (gensym "tableau")]
(if (-> (bag/query cur-tab [:by-arity 2])
(contains? [n form1]))
[{nil #{[new-t]
[new-t (tab/tableau n form2)]
[form1 t new-t]}
t #{node-label}}]
[]))))
(fn
([] [:by-arity 2])
([cur-tab [n form :as node-label]]
[{nil (->> (bag/query
cur-tab
[:pairs-by-first-fsecond-fssecond
n :! form])
(r/mapcat
(fn [[n2 [op form2 form3]]]
(let [new-t (gensym "tableau")]
[[new-t]
[new-t (tab/tableau n form3)]
[form t new-t]])))
(u/foldset))
t #{node-label}}]))
(fn
([] [:pairs-by-fsecond-fssecond :not :!])
([cur-tab [n [op1 [op2 form1 form2]] :as node-label]]
(let [new-t (gensym "tableau")]
[{nil #{[new-t]
[new-t (tab/tableau n [:not form2])]
[form1 t new-t]}
t #{node-label}}
{t #{[n form1]}}])))])))
(defn synchronization-range [casc t]
(->> (bag/query casc [:triples-by-second t])
(r/map #(nth % 2))
(r/foldcat)
(r/cat (->> (bag/query casc [:triples-by-third t])
(r/map second)
(r/foldcat)))
(r/mapcat (fn [t2]
(->> (newest-tableau casc t2)
(#(bag/query % [:by-arity 1]))
(r/map (fn [[n]] {n #{t2}})))))
(r/fold u/mmap-merge)))
(defn synchronize-init
([] [:by-arity 3])
([casc [precond t1 t2]]
[(r/fold u/mmap-merge
[(->> (bag/query (newest-tableau casc t1)
[:pairs-by-second precond])
(r/map (fn [[n form]] {t2 #{[n]}}))
(r/fold u/mmap-merge))])]))
(defn synchronize
([] [:by-arity 2])
([casc [t tab]]
(let [sync-range (synchronization-range casc t)]
(meta-process
tab
[(fn ; copy node to left-hand tableaux
([] [:by-arity 1])
([cur-tab [n]]
(->> (bag/query casc [:triples-by-third t])
(r/map (fn [[precond t1 t2]]
{t1 #{[n]
[n precond]}})))))
(fn ; copy node to right-hand tableaux
([] [:by-arity 2])
([cur-tab [n form]]
(->> (bag/query casc
[:triples-by-first-second
form t])
(r/map (fn [[precond t1 t2]]
{t2 #{[n]}})))))
(fn ; copy atoms to this tableau
([] [:by-arity 1])
([cur-tab [n]]
(->> (get sync-range n)
(r/map #(bag/newest casc
[:pairs-by-first %]))
(r/mapcat
#(bag/query
(second %)
[:pairs-by-fsecond nil]))
(r/filter #(= (first %) n))
(r/map (fn [x] {t #{x}})))))
(fn ; copy atoms to neighboring tableaux
([] [:pairs-by-fsecond nil])
([cur-tab [n atom]]
(r/map (fn [t2] {t2 #{[n atom]}})
(get sync-range n))))
(fn ; copy incoming edges to this tableau
([] [:by-arity 1])
([cur-tab [n]]
(->> (get sync-range n)
(r/map #(bag/newest casc
[:pairs-by-first %]))
(r/mapcat #(bag/query (second %)
[:triples-by-third n]))
(r/filter
#(contains? (bag/query cur-tab
[:by-arity 1])
[(second %)]))
(r/map (fn [x] {t #{x}})))))
(fn ; copy outgoing edges to this tableau
([] [:by-arity 1])
([cur-tab [n]]
(->> (get sync-range n)
(r/map #(bag/newest casc
[:pairs-by-first %]))
(r/mapcat
#(bag/query (second %)
[:triples-by-second n]))
(r/filter
#(contains? (bag/query cur-tab
[:by-arity 1])
[(nth % 2)]))
(r/map (fn [x] {t #{x}})))))
(fn ; copy edges to neighboring tableaux
([] [:by-arity 3])
([cur-tab [idx n m]]
(r/map (fn [t2] {t2 #{[idx n m]}})
(set/intersection
(get sync-range n)
(get sync-range m)))))]))))
(defn saturate [cascades]
(let [next-casc
(->> cascades
(r/map #(bag/process
%
:casc-sat-mark
[saturate-tableau]))
(r/mapcat (partial apply tab/disjunctive-post))
(r/map #(bag/process
%
:casc-sat-mark2
[prime
synchronize
synchronize-init]))
(r/map (partial apply meta-post))
(r/fold 8 (r/monoid r/cat vector) conj))]
(if-not (->> next-casc
(r/mapcat #(bag/since % :casc-sat-mark))
(u/fold-empty?))
(recur next-casc)
next-casc)))
(defn consistent? [casc]
(->> (bag/query casc [:by-arity 1])
(r/map #(bag/newest casc [:pairs-by-first (first %)]))
(r/filter #(not (tab/consistent? (second %))))
(u/fold-empty?)))
(defn satisfiable? [form]
(->> (saturate [(tableau-cascade form)])
(r/filter consistent?)
(u/fold-empty?)
(not)))
(defn valid? [form]
(not (satisfiable? [:not form])))