forked from teorth/equational_theories
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Closure.lean
387 lines (324 loc) · 14.6 KB
/
Closure.lean
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import equational_theories.EquationalResult
namespace Closure
open Lean Parser Elab Command
inductive Edge
| implication : Implication → Edge
| nonimplication : Implication → Edge
deriving DecidableEq, Hashable
def Edge.isTrue : Edge → Bool
| .implication _ => true
| .nonimplication _ => false
def Edge.get : Edge → Implication
| .nonimplication x | .implication x => x
def Edge.lhs : Edge → String := Implication.lhs ∘ get
def Edge.rhs : Edge → String := Implication.rhs ∘ get
inductive Outcome
/-- the implication has an explicit proof -/
| explicit_proof_true
/-- the implication can be derived from proven theorems -/
| implicit_proof_true
/-- the implication is explicitly conjectured -/
| explicit_conjecture_true
/-- the implication can be derived from theorems and conjectures -/
| implicit_conjecture_true
/-- the status of the implication is unknown -/
| unknown
/-- the implication can be disproved from theorems and conjectures -/
| implicit_conjecture_false
/-- the implication can is explicitly conjectured to be false -/
| explicit_conjecture_false
/-- the falsity of the implication can be derived from proven theorems -/
| implicit_proof_false
/-- the implication has an explicit disproof -/
| explicit_proof_false
deriving Repr, DecidableEq, Hashable, Lean.ToJson, Lean.FromJson
instance : Inhabited Outcome where
default := .unknown
instance : ToString Outcome where
toString
| .unknown => "unknown"
| .explicit_proof_true => "explicit_proof_true"
| .implicit_proof_true => "implicit_proof_true"
| .explicit_proof_false => "explicit_proof_false"
| .implicit_proof_false => "implicit_proof_false"
| .explicit_conjecture_true => "explicit_conjecture_true"
| .implicit_conjecture_true => "implicit_conjecture_true"
| .explicit_conjecture_false => "explicit_conjecture_false"
| .implicit_conjecture_false => "implicit_conjecture_false"
def Outcome.implicit_theorem : Bool → Outcome
| true => implicit_proof_true
| false => implicit_proof_false
def Outcome.explicit_theorem : Bool → Outcome
| true => explicit_proof_true
| false => explicit_proof_false
def Outcome.implicit_conjecture : Bool → Outcome
| true => implicit_conjecture_true
| false => implicit_conjecture_false
def Outcome.explicit_conjecture : Bool → Outcome
| true => explicit_conjecture_true
| false => explicit_conjecture_false
def Outcome.isExplicit : Outcome → Bool
| explicit_proof_true => true
| implicit_proof_true => false
| explicit_conjecture_true => true
| implicit_conjecture_true => false
| unknown => false
| implicit_conjecture_false => false
| explicit_conjecture_false => true
| implicit_proof_false => false
| explicit_proof_false => true
def Outcome.isProven : Outcome → Bool
| explicit_proof_true => true
| implicit_proof_true => true
| explicit_conjecture_true => false
| implicit_conjecture_true => false
| unknown => false
| implicit_conjecture_false => false
| explicit_conjecture_false => false
| implicit_proof_false => true
| explicit_proof_false => true
def Outcome.isTrue : Outcome → Bool
| explicit_proof_true => true
| implicit_proof_true => true
| explicit_conjecture_true => true
| implicit_conjecture_true => true
| unknown => false
| implicit_conjecture_false => false
| explicit_conjecture_false => false
| implicit_proof_false => false
| explicit_proof_false => false
partial def dfs1 (graph : Array (Array Nat)) (vertex : Nat) (vis : Array Bool) (order : Array Nat) :
Array Bool × Array Nat := Id.run do
let mut vis1 := vis.set! vertex true
let mut ord := order
for v in graph[vertex]! do
unless vis1[v]! do
(vis1, ord) := dfs1 graph v vis1 ord
pure (vis1, ord.push vertex)
partial def dfs2 (graph : Array (Array Nat)) (vertex : Nat) (component : Array Nat) (component_id : Nat) :
Array Nat := Id.run do
let mut comp := component.set! vertex component_id
for v in graph[vertex]! do
if component[v]! == 0 then
comp := dfs2 graph v comp component_id
pure comp
/-- This is a bitset (https://en.cppreference.com/w/cpp/utility/bitset).
It represents an array of bits by directly packing them to UInt64, which makes some operations
more efficient. This is also more space-efficient. -/
def Bitset := Array UInt64
def Bitset.toArray : Bitset → Array UInt64 := id
instance : Inhabited Bitset := ⟨#[]⟩
def Bitset.mk (n : Nat) : Bitset := Array.mkArray ((n + 63) >>> 6) 0
def Bitset.set (b : Bitset) (n : Nat) : Bitset :=
b.modify (n >>> 6) (fun x ↦ x ||| (1 <<< ((UInt64.ofNat n) &&& 63)))
def Bitset.get (b : Bitset) (n : Nat) : Bool :=
(b.toArray[n >>> 6]! >>> ((UInt64.ofNat n) &&& 63)) &&& 1 != 0
structure DenseNumbering (α : Type) [BEq α] [Hashable α] where
in_order : Array α
index : Std.HashMap α Nat
def DenseNumbering.size {α : Type} [BEq α] [Hashable α] (num : DenseNumbering α) : Nat :=
num.in_order.size
instance {α : Type} [BEq α] [Hashable α] : GetElem? (DenseNumbering α) α Nat (fun num a => a ∈ num.index) where
getElem num a h := num.index[a]
getElem? num a := num.index[a]?
def DenseNumbering.fromArray {α : Type} [BEq α] [Hashable α] (elts : Array α) : DenseNumbering α :=
let index := Std.HashMap.ofList (elts.mapIdx (fun i x => (x, i.val))).toList
⟨elts, index⟩
def DenseNumbering.map {α β : Type} [BEq α] [BEq β] [Hashable α] [Hashable β] (num : DenseNumbering α) (f : α → β) : DenseNumbering β :=
DenseNumbering.fromArray (num.in_order.map f)
def ltEquationNames (a b : String) : Bool :=
assert! a.startsWith "Equation"
assert! b.startsWith "Equation"
let aNum := (a.toSubstring.drop 8).toNat?.get!
let bNum := (b.toSubstring.drop 8).toNat?.get!
aNum < bNum
def equationSet (inp : Array EntryVariant) : Std.HashSet String := Id.run do
let mut eqs : Std.HashSet String := {}
for imp in inp do
match imp with
| .implication ⟨lhs, rhs⟩ =>
eqs := eqs.insert lhs
eqs := eqs.insert rhs
| .facts ⟨satisfied, refuted⟩ =>
for eq in satisfied ++ refuted do
eqs := eqs.insert eq
| .unconditional eq =>
eqs := eqs.insert eq
pure eqs
def number_equations (inp : Array EntryVariant) : DenseNumbering String :=
-- number the equations for easier processing
DenseNumbering.fromArray ((equationSet inp).toArray.qsort ltEquationNames)
/--
This transforms the `Facts` in the input array to `Edge`s
-/
def toEdges (inp : Array EntryVariant) : Array Edge := Id.run do
let eqs := number_equations inp
let mut edges : Array Edge := Array.mkEmpty inp.size
let mut nonimplies : Array Bitset := Array.mkArray eqs.size (Bitset.mk eqs.size)
for imp in inp do
match imp with
| .implication ⟨lhs, rhs⟩ =>
edges := edges.push (.implication ⟨lhs, rhs⟩)
| .facts ⟨satisfied, refuted⟩ =>
for f1 in satisfied do
for f2 in refuted do
nonimplies := nonimplies.modify eqs[f1]! (fun x ↦ x.set eqs[f2]!)
| _ => continue
for hi : i in [:eqs.size] do
for hj : j in [:eqs.size] do
if nonimplies[i]!.get j then
edges := edges.push (.nonimplication ⟨eqs.in_order[i], eqs.in_order[j]⟩)
return edges
structure Reachability where
size : Nat
reachable : Array Bitset
components : Array (Array Nat)
def closure_aux (inp : Array EntryVariant) (duals: Std.HashMap Nat Nat) (eqs : DenseNumbering String) : IO Reachability := do
-- construct the implication/non-implication graph
let n := eqs.size
let mut graph_size := 2 * n
let mut graph : Array (Array Nat) := Array.mkArray graph_size #[]
let mut revgraph : Array (Array Nat) := Array.mkArray graph_size #[]
for imp in inp do
match imp with
| .implication imp =>
graph := graph.modify (eqs[imp.rhs]!) (fun x => x.push eqs[imp.lhs]!)
graph := graph.modify (eqs[imp.rhs]! + n) (fun x => x.push (eqs[imp.lhs]! + n))
revgraph := revgraph.modify (eqs[imp.lhs]!) (fun x => x.push eqs[imp.rhs]!)
revgraph := revgraph.modify (eqs[imp.lhs]! + n) (fun x => x.push (eqs[imp.rhs]! + n))
| .facts ⟨satisfied, refuted⟩ =>
if satisfied.size * refuted.size < satisfied.size + refuted.size + 1 then
for lhs in satisfied do
for rhs in refuted do
graph := graph.modify (eqs[lhs]!) (fun x => x.push (eqs[rhs]! + n))
revgraph := revgraph.modify (eqs[rhs]! + n) (fun x => x.push eqs[lhs]!)
else
let dummy := graph_size
graph_size := graph_size + 2
graph := graph.push (refuted.map (eqs[·]! + n))
revgraph := revgraph.push (satisfied.map (eqs[·]!))
-- this is for the dual implications
graph := graph.push #[]
revgraph := revgraph.push #[]
for f1 in satisfied do
graph := graph.modify eqs[f1]! (fun x ↦ x.push dummy)
for f1 in refuted do
revgraph := revgraph.modify (eqs[f1]! + n) (fun x ↦ x.push dummy)
| _ => pure ()
let duals := Array.ofFn λ (i: Fin graph_size) ↦
if i < n then
duals.getD i i
else if i < 2 * n then
n + duals.getD (i - n) (i - n)
else
i ^^^ 1
for i in [0:graph_size], neighbors in graph do
let i' := duals[i]!
for j in neighbors do
let j' := duals[j]!
if i != i' ∨ j != j' then
graph := graph.modify i' (fun x => x.push j')
revgraph := revgraph.modify j' (fun x => x.push i')
let mut vis : Array Bool := Array.mkArray graph_size false
let mut order : Array Nat := Array.mkEmpty graph_size
-- compute strongly connected components and the condensation graph using Kosaraju's algorithm
for i in [:graph_size] do
unless vis[i]! do
(vis, order) := dfs1 graph i vis order
order := order.reverse
let mut component : Array Nat := Array.mkArray graph_size 0
let mut last_component : Nat := 0
for i in order do
if component[i]! == 0 then do
last_component := last_component + 1
component := dfs2 revgraph i component last_component
let mut components : Array (Array Nat) := Array.mkArray last_component #[]
let mut comp_graph : Array (Std.HashSet Nat) := Array.mkArray last_component {}
for i in [:graph_size] do
components := components.modify (component[i]!-1) (fun x ↦ x.push i)
for j in graph[i]! do
unless component[i]! == component[j]! do
comp_graph := comp_graph.modify (component[i]!-1) (fun x ↦ x.insert (component[j]!-1))
-- Run bitset transitive closure on the condensation graph
let mut reachable : Array Bitset := Array.mkArray last_component (Bitset.mk last_component)
for i_ in [:last_component] do
let i := last_component - 1 - i_
reachable := reachable.modify i (fun x ↦ x.set i)
for j in comp_graph[i]! do
reachable := reachable.modify i (fun x ↦ x.mapIdx (fun idx val ↦
reachable[j]!.toArray[idx]! ||| val))
if components[i]![0]! < n && reachable[i]!.get (component[components[i]![0]! + n]!-1) then
throw (IO.userError "Inconsistent conjectures!")
pure ⟨n, reachable, components⟩
instance {m : Type → Type} : ForIn m Reachability (Nat × Nat × Bool) where
forIn {β : Type} [Monad m] (reachability : Reachability) (state : β)
(f : (Nat × Nat × Bool) → β → m (ForInStep β)) := do
let mut v := state
for i in reachability.components, i2 in reachability.reachable do
if i.back >= reachability.size then continue
for j in reachability.components, j2 in [:reachability.components.size] do
if i2.get j2 then
for x in i do
for y in j do
if y >= reachability.size * 2 then continue
let val := if y < reachability.size then (y, x, true) else (x, y - reachability.size, false)
match ← f val v with
| .done a => return a
| .yield a => v := a
return v
def number_duals (duals: Std.HashMap String String) (eqs: DenseNumbering String) :=
Std.HashMap.ofList <| duals.toList.map (λ (i, j) ↦ (eqs[i]!, eqs[j]!))
/--
This computes the closure of the implications/non-implications represented by `inp`.
-/
def closure (inp : Array EntryVariant) (duals: Std.HashMap String String) : IO (Array Edge) := do
let eqs := number_equations inp
let n := eqs.size
let duals := number_duals duals eqs
-- extract the implications
let mut ans : Array Edge := Array.mkEmpty (n*n)
for ⟨x, y, is_true⟩ in ← closure_aux inp duals eqs do
unless x == y do
if is_true then
ans := ans.push (.implication ⟨eqs.in_order[x]!, eqs.in_order[y]!⟩)
else
ans := ans.push (.nonimplication ⟨eqs.in_order[x]!, eqs.in_order[y]!⟩)
pure ans
def list_outcomes (res : Array Entry) (duals: Std.HashMap String String): IO (Array String × Array (Array Outcome)) := do
let rs := res.map (·.variant)
let prs := res.filter (·.proven) |>.map (·.variant)
let eqs := number_equations rs
let duals := number_duals duals eqs
let n := eqs.size
let mut outcomes : Array (Array Outcome) := Array.mkArray n (Array.mkArray n .unknown)
for edge in toEdges prs do
outcomes := outcomes.modify eqs[edge.lhs]! (fun a ↦ a.set! eqs[edge.rhs]!
(.explicit_theorem edge.isTrue))
for ⟨x, y, is_true⟩ in ← closure_aux prs duals eqs do
outcomes := outcomes.modify x (fun a ↦ a.modify y
fun y ↦ if y = .unknown then .implicit_theorem is_true else y)
for edge in toEdges rs do
outcomes := outcomes.modify eqs[edge.lhs]! (fun a ↦ a.modify eqs[edge.rhs]!
fun y ↦ if y = .unknown then .explicit_conjecture edge.isTrue else y)
for ⟨x, y, is_true⟩ in ← closure_aux rs duals eqs do
outcomes := outcomes.modify x (fun a ↦ a.modify y
fun y ↦ if y = .unknown then .implicit_conjecture is_true else y)
return (eqs.in_order, outcomes)
def outcomes_mod_equiv (inp : Array EntryVariant) (duals: Std.HashMap String String) : IO (DenseNumbering (Array String) × Array (Array (Option Bool))) := do
let eqs := number_equations inp
let n := eqs.size
let duals := number_duals duals eqs
let reachable ← closure_aux inp duals eqs
let comps := reachable.components.filter (·[0]! < n) |> DenseNumbering.fromArray
let mut implies : Array (Array (Option Bool)) :=
Array.mkArray comps.size (Array.mkArray comps.size none)
for i in reachable.components, i2 in reachable.reachable do
if i[0]! >= reachable.size then continue
for j in reachable.components, j2 in [:reachable.components.size] do
if i2.get j2 then
if j[0]! < n then
implies := implies.modify comps[j]! (fun x ↦ x.set! comps[i]! true)
else if j.back < 2*n then
implies := implies.modify comps[i]! (fun x ↦ x.set! comps[j.map (·-n)]! false)
return (comps.map (fun ids => ids.map (eqs.in_order[·]!)), implies)
end Closure