gdp-wired 0.51.0 → 0.52.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/core/accel-refute.d.ts +53 -0
- package/dist/core/accel-refute.js +997 -0
- package/dist/core/cli.d.ts +2 -0
- package/dist/core/cli.js +8 -1
- package/dist/core/cycles.d.ts +2 -0
- package/dist/core/cycles.js +12 -9
- package/dist/core/dsl.d.ts +7 -1
- package/dist/core/dsl.js +147 -26
- package/dist/core/live-support.d.ts +5 -0
- package/dist/core/live-support.js +148 -0
- package/dist/core/live.js +194 -37
- package/dist/core/lockstep.d.ts +5 -0
- package/dist/core/lockstep.js +23 -0
- package/dist/core/ordered-pair-seeds.d.ts +2 -0
- package/dist/core/ordered-pair-seeds.js +109 -0
- package/dist/core/proof-output.d.ts +38 -1
- package/dist/core/proof-output.js +38 -0
- package/dist/core/search-termination.d.ts +4 -0
- package/dist/core/search-termination.js +1 -1
- package/dist/core/source-check.d.ts +4 -1
- package/dist/core/source-check.js +5 -2
- package/dist/core/sparse-kind-proof.d.ts +7 -0
- package/dist/core/sparse-kind-proof.js +49 -0
- package/dist/core/step-seeds.d.ts +46 -0
- package/dist/core/step-seeds.js +316 -0
- package/dist/lean-kernel/Cert.lean +222 -0
- package/dist/lean-kernel/Count.lean +1568 -0
- package/dist/lean-kernel/Ghost.lean +373 -0
- package/dist/lean-kernel/GhostMap.lean +426 -0
- package/dist/lean-kernel/Kernel.lean +2881 -0
- package/dist/lean-kernel/Live.lean +1168 -0
- package/dist/lean-kernel/Progress.lean +543 -0
- package/dist/lean-kernel/Sets.lean +1975 -0
- package/dist/lean-kernel/StepLaw.lean +128 -0
- package/dist/lean-kernel/Steps.lean +477 -0
- package/dist/lean-kernel/Sums.lean +916 -0
- package/dist/lean-kernel/emit.d.ts +4 -0
- package/dist/lean-kernel/emit.js +23 -4
- package/dist/lean-kernel/lean-aggregate/FiniteFold.lean +44 -0
- package/dist/lean-kernel/lean-aggregate/GroupedTransport.lean +308 -0
- package/dist/lean-kernel/lean-aggregate/aggregate.d.ts +13 -0
- package/dist/lean-kernel/lean-aggregate/aggregate.js +68 -0
- package/dist/lean-kernel/lean-aggregate/assets.d.ts +5 -0
- package/dist/lean-kernel/lean-aggregate/assets.js +7 -0
- package/dist/lean-kernel/lean-aggregate/audit.d.ts +3 -0
- package/dist/lean-kernel/lean-aggregate/audit.js +13 -0
- package/dist/lean-kernel/lean-aggregate/exact.d.ts +15 -0
- package/dist/lean-kernel/lean-aggregate/exact.js +51 -0
- package/dist/lean-kernel/lean-aggregate/index.d.ts +19 -0
- package/dist/lean-kernel/lean-aggregate/index.js +52 -0
- package/dist/lean-kernel/lean-aggregate/point-fold.d.ts +27 -0
- package/dist/lean-kernel/lean-aggregate/point-fold.js +79 -0
- package/dist/lean-kernel/lean-aggregate/replay-fold.d.ts +2 -0
- package/dist/lean-kernel/lean-aggregate/replay-fold.js +163 -0
- package/dist/lean-kernel/lean-aggregate/source.d.ts +2 -0
- package/dist/lean-kernel/lean-aggregate/source.js +141 -0
- package/dist/lean-kernel/lean-aggregate/weighted-flags.d.ts +2 -0
- package/dist/lean-kernel/lean-aggregate/weighted-flags.js +26 -0
- package/dist/lean-kernel/lean-aggregate/weighted-replay.d.ts +7 -0
- package/dist/lean-kernel/lean-aggregate/weighted-replay.js +53 -0
- package/dist/lean-kernel/lean-aggregate/weighted-seeded.d.ts +30 -0
- package/dist/lean-kernel/lean-aggregate/weighted-seeded.js +64 -0
- package/dist/lean-kernel/lean-aggregate/weighted.d.ts +18 -0
- package/dist/lean-kernel/lean-aggregate/weighted.js +115 -0
- package/dist/lean-kernel/lean-aggregate/worker.d.ts +10 -0
- package/dist/lean-kernel/lean-aggregate/worker.js +46 -0
- package/docs/lean-aggregate.md +29 -0
- package/package.json +6 -1
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import Progress
|
|
2
|
+
|
|
3
|
+
/-!
|
|
4
|
+
StepLaw: kernel.ts's step laws, "every request the machine allows takes a step where `then` holds", proven sound.
|
|
5
|
+
|
|
6
|
+
`then` reads the state before the step (with the request's inputs) by plain names and the state and maps after it by
|
|
7
|
+
primed names (`prime`, `primeMaps`), as Progress does; `when` narrows the requests. Where Progress names one request
|
|
8
|
+
and must prove it allowed, a step law is about every request: its inputs are any `ι`, and the machine's assumption
|
|
9
|
+
(flags decided) is a hypothesis. Lean builds the goal itself from the machine (`stepLawGoal`, reusing Progress's
|
|
10
|
+
`progressBefore`, `primeSub` and `rdAfter` with an empty request), and `steplaw_sound` shows that an accepted tree makes
|
|
11
|
+
`StepLawHolds` true in every reachable state. The exporter inlines flags into `when` and `then` (as kernel.ts's goal
|
|
12
|
+
does), so both must read no flag variable.
|
|
13
|
+
-/
|
|
14
|
+
|
|
15
|
+
namespace Kern
|
|
16
|
+
|
|
17
|
+
structure StepLaw where
|
|
18
|
+
when : C
|
|
19
|
+
then_ : C
|
|
20
|
+
prime : List (Nat × Nat) -- a state variable, and the name `then` reads its value after the step by
|
|
21
|
+
primeMaps : List (Nat × Nat) -- a map, and the name `then` reads it after the step by
|
|
22
|
+
insts : List (Nat × E) -- (law index, key): where the goal assumes the laws about every key
|
|
23
|
+
proof : List VDef × Tree
|
|
24
|
+
|
|
25
|
+
-- The same goal shape as Progress's, with no request named: the request's inputs are the inputs as they are.
|
|
26
|
+
def asProgress (sl : StepLaw) : Progress := ⟨sl.when, sl.then_, [], sl.prime, sl.primeMaps, sl.insts, sl.proof, sl.proof⟩
|
|
27
|
+
|
|
28
|
+
def stepLawGoal (f : FMMachine) (laws : List MLaw) (sl : StepLaw) : List C :=
|
|
29
|
+
progressBefore f laws (asProgress sl)
|
|
30
|
+
++ [inlineC f.flags f.m.assume, .not (substRC (primeSub f (asProgress sl)) (rdAfter f (asProgress sl)) sl.then_)]
|
|
31
|
+
|
|
32
|
+
def checkStepLaw (f : FMMachine) (ps : List MEntry) (sl : StepLaw) : Bool :=
|
|
33
|
+
avoids (flagVars f.flags) sl.then_ && avoids (flagVars f.flags) sl.when
|
|
34
|
+
&& checkGoal ⟨sl.proof.1, stepLawGoal f (ps.map (·.law)) sl⟩ sl.proof.2
|
|
35
|
+
|
|
36
|
+
/-- A step law: in every reachable state, every request (inputs ι) the machine allows with its flags decided, and that
|
|
37
|
+
`when` admits, takes a step where `then` holds: primed names read the state and maps after it, plain names the state
|
|
38
|
+
before it with the request's inputs. -/
|
|
39
|
+
def StepLawHolds (f : FMMachine) (sl : StepLaw) : Prop :=
|
|
40
|
+
∀ ρ M, FMReach f ρ M → ∀ ι : Nat → Nat,
|
|
41
|
+
evalC (decideFlags f.flags (withInputs f.m.inputs ρ ι) M) M f.m.assume = true →
|
|
42
|
+
evalC (withInputs f.m.inputs ρ ι) M sl.when = true →
|
|
43
|
+
evalC (primed sl.prime (applyS f.m.step (decideFlags f.flags (withInputs f.m.inputs ρ ι) M) M) (withInputs f.m.inputs ρ ι))
|
|
44
|
+
(primedMaps sl.primeMaps (writeMaps f.m.writes (decideFlags f.flags (withInputs f.m.inputs ρ ι) M) M) M)
|
|
45
|
+
sl.then_ = true
|
|
46
|
+
|
|
47
|
+
theorem applyS_nil (ρ : Nat → Nat) (M : Nat → Nat → Nat) : applyS [] ρ M = ρ := by
|
|
48
|
+
funext i; simp [applyS, assoc, lookup]
|
|
49
|
+
|
|
50
|
+
theorem steplaw_sound (f : FMMachine) (ps : List MEntry) (sl : StepLaw)
|
|
51
|
+
(hl : checkFMMachine f ps = true) (hs : checkStepLaw f ps sl = true) : StepLawHolds f sl := by
|
|
52
|
+
intro ρ M hr ι hA hW
|
|
53
|
+
have hl0 := hl
|
|
54
|
+
simp only [checkFMMachine, Bool.and_eq_true, List.all_eq_true] at hl
|
|
55
|
+
obtain ⟨⟨⟨hf, hpref⟩, _⟩, hc⟩ := hl
|
|
56
|
+
simp only [checkStepLaw, Bool.and_eq_true] at hs
|
|
57
|
+
obtain ⟨_, hgoal⟩ := hs
|
|
58
|
+
obtain ⟨ρ₀, hr₀, hag⟩ := fmreach_plain f hf hr
|
|
59
|
+
-- The laws and the precondition hold in the reachable state.
|
|
60
|
+
have hlaws : ∀ e ∈ ps, e.law.Holds ρ M := fmmachine_sound f ps hl0 ρ M hr
|
|
61
|
+
have hcm := hc
|
|
62
|
+
simp only [checkMMachine, Bool.and_eq_true, List.all_eq_true] at hcm
|
|
63
|
+
obtain ⟨⟨hpa, hpr⟩, hent⟩ := hcm
|
|
64
|
+
try simp only [Bool.not_eq_true'] at hpr
|
|
65
|
+
have hpre : evalC ρ M f.m.pre = true := by
|
|
66
|
+
rw [congrC ρ ρ₀ M f.m.pre (fun j hj => hag j (avoids_occ hpref hj))]
|
|
67
|
+
exact pre_mreach (m := f.plain) hpa hpr hr₀
|
|
68
|
+
-- They hold with the request's inputs too: neither reads an input.
|
|
69
|
+
have hagX : Agree f.m.inputs (withInputs f.m.inputs ρ ι) ρ := fun i hi => withInputs_other hi ρ ι
|
|
70
|
+
have hpreX : evalC (withInputs f.m.inputs ρ ι) M f.m.pre = true := by
|
|
71
|
+
rw [congrC (withInputs f.m.inputs ρ ι) ρ M f.m.pre (fun j hj => hagX j (fun h =>
|
|
72
|
+
avoids_occ hpa hj (List.mem_append_left _ (List.mem_append_left _ h))))]
|
|
73
|
+
exact hpre
|
|
74
|
+
have hlawsX : ∀ e ∈ ps, e.law.Holds (withInputs f.m.inputs ρ ι) M := by
|
|
75
|
+
intro e he
|
|
76
|
+
have hav := hent e he
|
|
77
|
+
exact holds_agree e.law hav.1.1.1 hagX M (hlaws e he)
|
|
78
|
+
have hb := before_true f ps (asProgress sl) (withInputs f.m.inputs ρ ι) M hpreX hlawsX hW
|
|
79
|
+
apply Decidable.byContradiction; intro hn
|
|
80
|
+
apply sound _ _ hgoal (withInputs f.m.inputs ρ ι) M
|
|
81
|
+
intro c hc'
|
|
82
|
+
simp only [stepLawGoal, List.mem_append, List.mem_cons, List.not_mem_nil, or_false] at hc'
|
|
83
|
+
rcases hc' with hc' | rfl | rfl
|
|
84
|
+
· exact hb c hc'
|
|
85
|
+
· rw [inline_evalC]; exact hA
|
|
86
|
+
· let e := decideFlags f.flags (withInputs f.m.inputs ρ ι) M
|
|
87
|
+
have hnow : ∀ t, evalE (withInputs f.m.inputs ρ ι) M (nowE f (asProgress sl) t) = evalE e M t := by
|
|
88
|
+
intro t; simp only [nowE, asProgress, subst_evalE, applyS_nil, inline_evalE, e]
|
|
89
|
+
have hrd : ∀ mp k, evalE (withInputs f.m.inputs ρ ι) M (rdAfter f (asProgress sl) mp k)
|
|
90
|
+
= primedMaps sl.primeMaps (writeMaps f.m.writes e M) M mp (evalE (withInputs f.m.inputs ρ ι) M k) := by
|
|
91
|
+
intro mp k
|
|
92
|
+
simp only [rdAfter, primedMaps, asProgress]
|
|
93
|
+
cases hq : lookup (fun q => q.2 == mp) sl.primeMaps with
|
|
94
|
+
| none => simp [evalE]
|
|
95
|
+
| some q =>
|
|
96
|
+
simp only [writeMaps]
|
|
97
|
+
cases hw : lookup (fun w => w.1 == q.1) f.m.writes with
|
|
98
|
+
| none => simp [evalE]
|
|
99
|
+
| some w =>
|
|
100
|
+
have h1 := hnow w.2.1
|
|
101
|
+
have h2 := hnow w.2.2
|
|
102
|
+
simp only [asProgress] at h1 h2
|
|
103
|
+
simp only [evalE, evalC, h1, h2]
|
|
104
|
+
by_cases hk : evalE (withInputs f.m.inputs ρ ι) M k = evalE e M w.2.1 <;> simp [hk]
|
|
105
|
+
have henv : applyS (primeSub f (asProgress sl)) (withInputs f.m.inputs ρ ι) M = primed sl.prime (applyS f.m.step e M) (withInputs f.m.inputs ρ ι) := by
|
|
106
|
+
funext i
|
|
107
|
+
have hps : lookup (fun p : Nat × E => p.1 == i) (primeSub f (asProgress sl))
|
|
108
|
+
= (lookup (fun q => q.2 == i) sl.prime).map (fun q => (q.2, nowE f (asProgress sl) (stepTerm f q.1))) := by
|
|
109
|
+
simp only [primeSub, asProgress, lookup_map_pred]
|
|
110
|
+
simp only [primed, applyS, assoc, hps]
|
|
111
|
+
cases hq : lookup (fun q => q.2 == i) sl.prime with
|
|
112
|
+
| none => rfl
|
|
113
|
+
| some q =>
|
|
114
|
+
simp only [Option.map_some]
|
|
115
|
+
rw [hnow]
|
|
116
|
+
simp only [stepTerm]
|
|
117
|
+
cases hs : assoc f.m.step q.1 with
|
|
118
|
+
| none =>
|
|
119
|
+
rw [show Option.map (fun x => x.2) (lookup (fun p => p.1 == q.1) f.m.step) = none from hs]
|
|
120
|
+
simp [evalE, e]
|
|
121
|
+
| some t =>
|
|
122
|
+
rw [show Option.map (fun x => x.2) (lookup (fun p => p.1 == q.1) f.m.step) = some t from hs]
|
|
123
|
+
simp only [evalC, substR_evalC _ _ _ M _ hrd, henv]
|
|
124
|
+
simpa [e] using hn
|
|
125
|
+
|
|
126
|
+
end Kern
|
|
127
|
+
|
|
128
|
+
#print axioms Kern.steplaw_sound
|
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
import StepLaw
|
|
2
|
+
|
|
3
|
+
/-!
|
|
4
|
+
Steps: kernel.ts's k-step laws, "every k requests in a row that the machine allows, each meeting its `when`, take a
|
|
5
|
+
reachable state to one where `then` holds", proven sound.
|
|
6
|
+
|
|
7
|
+
The goal is a symbolic run, as kernel.ts builds it: a frame is a substitution (each state variable a term over the
|
|
8
|
+
first state) and a read of each map (a term over the first state's maps); a step composes the machine's step terms
|
|
9
|
+
(flags inlined) after the frame, and a map read after it is read-over-write. Before each step the laws, the assumption
|
|
10
|
+
and that step's `when` are read at the frame; the first step reads its request's inputs by their own names, the j-th
|
|
11
|
+
(j ≥ 2) by fresh variables (`ren`). `then` reads the first state by plain names and the last by primed ones.
|
|
12
|
+
|
|
13
|
+
One invariant carries the proof (`Denotes`, kept by `step_denotes`): the frame denotes the real state after j steps
|
|
14
|
+
(outside the inputs, the fresh variables and the flags) and the real maps. So every hypothesis holds at the first state
|
|
15
|
+
with the fresh variables set to the later requests (`krun_sound`), and a refuted goal leaves `then` no way to fail
|
|
16
|
+
(`kstep_sound`). Lean builds the goal itself; only the tree comes from TypeScript.
|
|
17
|
+
-/
|
|
18
|
+
|
|
19
|
+
namespace Kern
|
|
20
|
+
|
|
21
|
+
structure KStep where
|
|
22
|
+
whens : List C -- one per step, flags inlined (0 ≤ 0 for a step with none); k is the length
|
|
23
|
+
then_ : C
|
|
24
|
+
ren : List (List (Nat × Nat)) -- per step: (input, the variable the goal reads it by there); [] for the first step
|
|
25
|
+
prime : List (Nat × Nat) -- a state variable, and the name `then` reads its value after the last step by
|
|
26
|
+
primeMaps : List (Nat × Nat) -- a map, and the name `then` reads it after the last step by
|
|
27
|
+
insts : List (Nat × E) -- (law index, key): where each frame assumes the laws about every key
|
|
28
|
+
proof : List VDef × Tree
|
|
29
|
+
|
|
30
|
+
def frameSub (r : List (Nat × Nat)) (σ : List (Nat × E)) : List (Nat × E) := r.map (fun p => (p.1, .var p.2)) ++ σ
|
|
31
|
+
def stepSub (f : FMMachine) (τ : List (Nat × E)) (rd : Nat → E → E) : List (Nat × E) :=
|
|
32
|
+
f.m.step.map (fun p => (p.1, substRE τ rd (inlineE f.flags p.2))) ++ τ
|
|
33
|
+
def stepRd (f : FMMachine) (τ : List (Nat × E)) (rd : Nat → E → E) : Nat → E → E := fun mp k =>
|
|
34
|
+
match lookup (fun w => w.1 == mp) f.m.writes with
|
|
35
|
+
| some w => .ite (.eq k (substRE τ rd (inlineE f.flags w.2.1))) (substRE τ rd (inlineE f.flags w.2.2)) (rd mp k)
|
|
36
|
+
| none => rd mp k
|
|
37
|
+
def frameHyps (f : FMMachine) (laws : List MLaw) (insts : List (Nat × E)) (w : C) : List C :=
|
|
38
|
+
scalarHyps laws ++ instHyps laws insts ++ [inlineC f.flags f.m.assume, w]
|
|
39
|
+
|
|
40
|
+
-- The hypotheses along the steps, and the frame after them.
|
|
41
|
+
def kRun (f : FMMachine) (laws : List MLaw) (insts : List (Nat × E)) :
|
|
42
|
+
List C → List (List (Nat × Nat)) → List (Nat × E) → (Nat → E → E) → List C × List (Nat × E) × (Nat → E → E)
|
|
43
|
+
| w :: ws, r :: rs, σ, rd =>
|
|
44
|
+
((frameHyps f laws insts w).map (substRC (frameSub r σ) rd) ++ (kRun f laws insts ws rs (stepSub f (frameSub r σ) rd) (stepRd f (frameSub r σ) rd)).1,
|
|
45
|
+
(kRun f laws insts ws rs (stepSub f (frameSub r σ) rd) (stepRd f (frameSub r σ) rd)).2)
|
|
46
|
+
| _, _, σ, rd => ([], σ, rd)
|
|
47
|
+
|
|
48
|
+
def getRd : Nat → E → E := fun mp k => .get mp k
|
|
49
|
+
def kEndSub (sl : KStep) (σ : List (Nat × E)) (rd : Nat → E → E) : List (Nat × E) :=
|
|
50
|
+
sl.prime.map fun q => (q.2, substRE σ rd (.var q.1))
|
|
51
|
+
def kEndRd (sl : KStep) (rd : Nat → E → E) : Nat → E → E := fun mp k =>
|
|
52
|
+
match lookup (fun q => q.2 == mp) sl.primeMaps with
|
|
53
|
+
| some q => rd q.1 k
|
|
54
|
+
| none => .get mp k
|
|
55
|
+
|
|
56
|
+
def kOut (f : FMMachine) (laws : List MLaw) (sl : KStep) := kRun f laws sl.insts sl.whens sl.ren [] getRd
|
|
57
|
+
def kGoal (f : FMMachine) (laws : List MLaw) (sl : KStep) : List C :=
|
|
58
|
+
[f.m.pre] ++ (kOut f laws sl).1 ++ [.not (substRC (kEndSub sl (kOut f laws sl).2.1 (kOut f laws sl).2.2) (kEndRd sl (kOut f laws sl).2.2) sl.then_)]
|
|
59
|
+
|
|
60
|
+
def distinct : List Nat → Bool
|
|
61
|
+
| [] => true
|
|
62
|
+
| a :: as => !as.contains a && distinct as
|
|
63
|
+
|
|
64
|
+
def tgts : List (List (Nat × Nat)) → List Nat
|
|
65
|
+
| [] => []
|
|
66
|
+
| r :: rs => r.map (·.2) ++ tgts rs
|
|
67
|
+
def zone (f : FMMachine) (sl : KStep) : List Nat := tgts sl.ren ++ flagVars f.flags
|
|
68
|
+
|
|
69
|
+
def checkKStep (f : FMMachine) (ps : List MEntry) (sl : KStep) : Bool :=
|
|
70
|
+
sl.whens.length == sl.ren.length && sl.ren.head? == some [] && distinct (tgts sl.ren)
|
|
71
|
+
&& sl.ren.all (fun r => r.all fun p => f.m.inputs.contains p.1)
|
|
72
|
+
&& sl.ren.tail.all (fun r => f.m.inputs.all fun i => r.any (·.1 == i))
|
|
73
|
+
&& (tgts sl.ren).all (fun v => !f.m.inputs.contains v)
|
|
74
|
+
&& sl.prime.all (fun q => !f.m.inputs.contains q.1 && !(zone f sl).contains q.1)
|
|
75
|
+
&& avoids (zone f sl) f.m.pre
|
|
76
|
+
&& sl.whens.all (fun w => (frameHyps f (ps.map (·.law)) sl.insts w).all (avoids (zone f sl)))
|
|
77
|
+
&& f.m.step.all (fun p => avoidsE (zone f sl) (inlineE f.flags p.2))
|
|
78
|
+
&& f.m.writes.all (fun w => avoidsE (zone f sl) (inlineE f.flags w.2.1) && avoidsE (zone f sl) (inlineE f.flags w.2.2))
|
|
79
|
+
&& avoids (flagVars f.flags) sl.then_
|
|
80
|
+
&& checkGoal ⟨sl.proof.1, kGoal f (ps.map (·.law)) sl⟩ sl.proof.2
|
|
81
|
+
|
|
82
|
+
/-! What a k-step law claims -/
|
|
83
|
+
|
|
84
|
+
-- One step from ρ, M with request ι: flags decided, then the step's terms and writes.
|
|
85
|
+
def kNext (f : FMMachine) (ρ : Nat → Nat) (M : Nat → Nat → Nat) (ι : Nat → Nat) : (Nat → Nat) × (Nat → Nat → Nat) :=
|
|
86
|
+
(applyS f.m.step (decideFlags f.flags (withInputs f.m.inputs ρ ι) M) M,
|
|
87
|
+
writeMaps f.m.writes (decideFlags f.flags (withInputs f.m.inputs ρ ι) M) M)
|
|
88
|
+
|
|
89
|
+
def kEnd (f : FMMachine) : (Nat → Nat) → (Nat → Nat → Nat) → List (Nat → Nat) → (Nat → Nat) × (Nat → Nat → Nat)
|
|
90
|
+
| ρ, M, [] => (ρ, M)
|
|
91
|
+
| ρ, M, ι :: ιs => kEnd f (kNext f ρ M ι).1 (kNext f ρ M ι).2 ιs
|
|
92
|
+
|
|
93
|
+
-- Each request allowed (the assumption, flags decided) and meeting its step's `when`.
|
|
94
|
+
def KAllowed (f : FMMachine) : List C → (Nat → Nat) → (Nat → Nat → Nat) → List (Nat → Nat) → Prop
|
|
95
|
+
| w :: ws, ρ, M, ι :: ιs =>
|
|
96
|
+
evalC (decideFlags f.flags (withInputs f.m.inputs ρ ι) M) M f.m.assume = true
|
|
97
|
+
∧ evalC (withInputs f.m.inputs ρ ι) M w = true ∧ KAllowed f ws (kNext f ρ M ι).1 (kNext f ρ M ι).2 ιs
|
|
98
|
+
| _, _, _, _ => True
|
|
99
|
+
|
|
100
|
+
-- What `then`'s plain names read: the first state with the first request, each later request at its fresh variables.
|
|
101
|
+
def renEnv : List (List (Nat × Nat)) → List (Nat → Nat) → (Nat → Nat) → Nat → Nat
|
|
102
|
+
| r :: rs, ι :: ιs, b => fun v => match lookup (fun p => p.2 == v) r with
|
|
103
|
+
| some p => ι p.1
|
|
104
|
+
| none => renEnv rs ιs b v
|
|
105
|
+
| _, _, b => b
|
|
106
|
+
|
|
107
|
+
/-- A k-step law: from every reachable state, every k requests in a row the machine allows (each with its flags
|
|
108
|
+
decided, each meeting its `when`) end in a state where `then` holds: primed names read the last state and maps, plain
|
|
109
|
+
names the first state with the first request, and each later request's inputs are read by their fresh variables. -/
|
|
110
|
+
def KStepHolds (f : FMMachine) (sl : KStep) : Prop :=
|
|
111
|
+
∀ ρ M, FMReach f ρ M → ∀ ι₁ ιs, ιs.length + 1 = sl.whens.length → KAllowed f sl.whens ρ M (ι₁ :: ιs) →
|
|
112
|
+
evalC (primed sl.prime (kEnd f ρ M (ι₁ :: ιs)).1 (renEnv sl.ren (ι₁ :: ιs) (withInputs f.m.inputs ρ ι₁)))
|
|
113
|
+
(primedMaps sl.primeMaps (kEnd f ρ M (ι₁ :: ιs)).2 M) sl.then_ = true
|
|
114
|
+
|
|
115
|
+
/-! Lists -/
|
|
116
|
+
|
|
117
|
+
theorem distinct_cons {a : Nat} {as : List Nat} (h : distinct (a :: as) = true) : a ∉ as ∧ distinct as = true := by
|
|
118
|
+
simp only [distinct, Bool.and_eq_true, Bool.not_eq_true'] at h
|
|
119
|
+
refine ⟨fun hm => ?_, h.2⟩
|
|
120
|
+
have h1 := h.1
|
|
121
|
+
simp at h1
|
|
122
|
+
exact h1 hm
|
|
123
|
+
|
|
124
|
+
theorem distinct_append : ∀ {l1 l2 : List Nat}, distinct (l1 ++ l2) = true →
|
|
125
|
+
distinct l1 = true ∧ distinct l2 = true ∧ ∀ a ∈ l1, a ∉ l2
|
|
126
|
+
| [], l2, h => ⟨rfl, h, fun _ ha => by simp at ha⟩
|
|
127
|
+
| a :: as, l2, h => by
|
|
128
|
+
rw [List.cons_append] at h
|
|
129
|
+
obtain ⟨hn, hd⟩ := distinct_cons h
|
|
130
|
+
obtain ⟨h1, h2, h3⟩ := distinct_append hd
|
|
131
|
+
refine ⟨by simp only [distinct, h1, Bool.and_true, Bool.not_eq_true']; simpa using fun h' => hn (List.mem_append_left l2 h'), h2, fun x hx => ?_⟩
|
|
132
|
+
rcases List.mem_cons.mp hx with rfl | hx'
|
|
133
|
+
· exact fun h' => hn (List.mem_append_right _ h')
|
|
134
|
+
· exact h3 x hx'
|
|
135
|
+
|
|
136
|
+
theorem assoc_append_or (l1 l2 : List (Nat × E)) (i : Nat) : assoc (l1 ++ l2) i = (assoc l1 i).or (assoc l2 i) := by
|
|
137
|
+
simp only [assoc, lookup_append]
|
|
138
|
+
cases lookup (fun p : Nat × E => p.1 == i) l1 <;> rfl
|
|
139
|
+
|
|
140
|
+
theorem applyS_nil' (W : Nat → Nat) (M : Nat → Nat → Nat) : applyS [] W M = W := by
|
|
141
|
+
funext i; simp [applyS, assoc, lookup]
|
|
142
|
+
|
|
143
|
+
theorem assoc_stepSub (f : FMMachine) (τ : List (Nat × E)) (rd : Nat → E → E) (v : Nat) :
|
|
144
|
+
assoc (f.m.step.map (fun p => (p.1, substRE τ rd (inlineE f.flags p.2)))) v = (assoc f.m.step v).map (fun e => substRE τ rd (inlineE f.flags e)) :=
|
|
145
|
+
assoc_map_snd f.m.step (fun e => substRE τ rd (inlineE f.flags e)) v
|
|
146
|
+
|
|
147
|
+
-- Reading `then`'s primed names: each the frame's term for its state variable, the rest as they are.
|
|
148
|
+
theorem applyS_primes (σ : List (Nat × E)) (rd : Nat → E → E) (W : Nat → Nat) (M : Nat → Nat → Nat) (i : Nat) :
|
|
149
|
+
∀ l : List (Nat × Nat), applyS (l.map fun q => (q.2, substRE σ rd (.var q.1))) W M i
|
|
150
|
+
= match lookup (fun q => q.2 == i) l with
|
|
151
|
+
| some q => evalE W M (substRE σ rd (.var q.1))
|
|
152
|
+
| none => W i
|
|
153
|
+
| [] => by simp [applyS, assoc, lookup]
|
|
154
|
+
| a :: as => by
|
|
155
|
+
have ih := applyS_primes σ rd W M i as
|
|
156
|
+
simp only [applyS, assoc, List.map_cons, lookup] at ih ⊢
|
|
157
|
+
by_cases h : a.2 = i
|
|
158
|
+
· simp [h]
|
|
159
|
+
· have hb : (a.2 == i) = false := by simpa using h
|
|
160
|
+
simp only [hb, Bool.false_eq_true, ite_false]; exact ih
|
|
161
|
+
|
|
162
|
+
-- A fresh variable of one step reads its request.
|
|
163
|
+
theorem lookup_ren : ∀ {r : List (Nat × Nat)}, distinct (r.map (·.2)) = true → ∀ p ∈ r, lookup (fun q => q.2 == p.2) r = some p
|
|
164
|
+
| [], _, p, hp => by simp at hp
|
|
165
|
+
| a :: as, hd, p, hp => by
|
|
166
|
+
simp only [List.map_cons] at hd
|
|
167
|
+
obtain ⟨hna, hd'⟩ := distinct_cons hd
|
|
168
|
+
simp only [lookup]
|
|
169
|
+
rcases List.mem_cons.mp hp with rfl | hp'
|
|
170
|
+
· simp
|
|
171
|
+
· have hne : (a.2 == p.2) = false := by
|
|
172
|
+
simp only [beq_eq_false_iff_ne]; intro he; exact hna (by rw [he]; exact List.mem_map_of_mem hp')
|
|
173
|
+
simp only [hne, Bool.false_eq_true, ite_false]; exact lookup_ren hd' p hp'
|
|
174
|
+
|
|
175
|
+
theorem renEnv_other : ∀ (rs : List (List (Nat × Nat))) (ιs : List (Nat → Nat)) (b : Nat → Nat) (v : Nat),
|
|
176
|
+
v ∉ tgts rs → renEnv rs ιs b v = b v
|
|
177
|
+
| [], ιs, b, v, _ => by cases ιs <;> rfl
|
|
178
|
+
| r :: rs, [], b, v, _ => rfl
|
|
179
|
+
| r :: rs, ι :: ιs, b, v, hv => by
|
|
180
|
+
simp only [tgts, List.mem_append, not_or] at hv
|
|
181
|
+
have hn : lookup (fun p : Nat × Nat => p.2 == v) r = none := lookup_none (fun p hp => by
|
|
182
|
+
simp only [beq_eq_false_iff_ne]; intro he; exact hv.1 (by rw [← he]; exact List.mem_map_of_mem hp))
|
|
183
|
+
simp only [renEnv, hn]
|
|
184
|
+
exact renEnv_other rs ιs b v hv.2
|
|
185
|
+
|
|
186
|
+
-- Each step's fresh variables hold its request's inputs.
|
|
187
|
+
def RenOK (W : Nat → Nat) : List (List (Nat × Nat)) → List (Nat → Nat) → Prop
|
|
188
|
+
| r :: rs, ι :: ιs => (∀ p ∈ r, W p.2 = ι p.1) ∧ RenOK W rs ιs
|
|
189
|
+
| _, _ => True
|
|
190
|
+
|
|
191
|
+
theorem renOK_congr {W W' : Nat → Nat} : ∀ (rs : List (List (Nat × Nat))) (ιs : List (Nat → Nat)),
|
|
192
|
+
(∀ v ∈ tgts rs, W v = W' v) → RenOK W' rs ιs → RenOK W rs ιs
|
|
193
|
+
| [], _, _, _ => trivial
|
|
194
|
+
| _ :: _, [], _, _ => trivial
|
|
195
|
+
| r :: rs, ι :: ιs, hag, ⟨hh, ht⟩ => by
|
|
196
|
+
refine ⟨fun p hp => ?_, renOK_congr rs ιs (fun v hv => hag v (by simp [tgts, hv])) ht⟩
|
|
197
|
+
rw [hag p.2 (by simp only [tgts, List.mem_append]; exact Or.inl (List.mem_map_of_mem hp))]; exact hh p hp
|
|
198
|
+
|
|
199
|
+
theorem renEnv_ok : ∀ (rs : List (List (Nat × Nat))) (ιs : List (Nat → Nat)) (b : Nat → Nat),
|
|
200
|
+
distinct (tgts rs) = true → RenOK (renEnv rs ιs b) rs ιs
|
|
201
|
+
| [], _, _, _ => trivial
|
|
202
|
+
| _ :: _, [], _, _ => trivial
|
|
203
|
+
| r :: rs, ι :: ιs, b, hd => by
|
|
204
|
+
simp only [tgts] at hd
|
|
205
|
+
obtain ⟨hr, hrs, hdis⟩ := distinct_append hd
|
|
206
|
+
refine ⟨fun p hp => ?_, renOK_congr rs ιs (fun v hv => ?_) (renEnv_ok rs ιs b hrs)⟩
|
|
207
|
+
· simp only [renEnv, lookup_ren hr p hp]
|
|
208
|
+
· have hn : lookup (fun p : Nat × Nat => p.2 == v) r = none := lookup_none (fun p hp => by
|
|
209
|
+
simp only [beq_eq_false_iff_ne]; intro he; exact hdis v (by rw [← he]; exact List.mem_map_of_mem hp) hv)
|
|
210
|
+
simp only [renEnv, hn]
|
|
211
|
+
|
|
212
|
+
/-! Frames -/
|
|
213
|
+
|
|
214
|
+
-- A step's input, read at a frame whose fresh variables hold the request.
|
|
215
|
+
theorem applyS_frame_in {r : List (Nat × Nat)} {σ : List (Nat × E)} {W : Nat → Nat} {M0 : Nat → Nat → Nat} {ι : Nat → Nat} {i : Nat}
|
|
216
|
+
(hcov : ∃ p ∈ r, p.1 = i) (hval : ∀ p ∈ r, W p.2 = ι p.1) : applyS (frameSub r σ) W M0 i = ι i := by
|
|
217
|
+
simp only [applyS, frameSub, assoc_append_or]
|
|
218
|
+
cases h : assoc (r.map fun p => (p.1, E.var p.2)) i with
|
|
219
|
+
| none =>
|
|
220
|
+
obtain ⟨p, hp, rfl⟩ := hcov
|
|
221
|
+
simp only [assoc, Option.map_eq_none_iff] at h
|
|
222
|
+
have := lookup_none_all h (p.1, .var p.2) (List.mem_map_of_mem hp)
|
|
223
|
+
simp at this
|
|
224
|
+
| some e =>
|
|
225
|
+
simp only [assoc, Option.map_eq_some_iff] at h
|
|
226
|
+
obtain ⟨q, hq, rfl⟩ := h
|
|
227
|
+
obtain ⟨hqm, hqi⟩ := lookup_some hq
|
|
228
|
+
obtain ⟨p, hp, rfl⟩ := List.mem_map.mp hqm
|
|
229
|
+
simp at hqi
|
|
230
|
+
simp only [Option.some_or, evalE, hval p hp, hqi]
|
|
231
|
+
|
|
232
|
+
-- Any other variable reads through to the frame.
|
|
233
|
+
theorem applyS_frame_out {r : List (Nat × Nat)} {σ : List (Nat × E)} {W : Nat → Nat} {M0 : Nat → Nat → Nat} {i : Nat}
|
|
234
|
+
(hi : ∀ p ∈ r, p.1 ≠ i) : applyS (frameSub r σ) W M0 i = applyS σ W M0 i := by
|
|
235
|
+
have hn : assoc (r.map fun p => (p.1, E.var p.2)) i = none := by
|
|
236
|
+
simp only [assoc, Option.map_eq_none_iff]
|
|
237
|
+
exact lookup_none (fun a ha => by
|
|
238
|
+
obtain ⟨p, hp, rfl⟩ := List.mem_map.mp ha
|
|
239
|
+
simp only [beq_eq_false_iff_ne]; exact hi p hp)
|
|
240
|
+
simp only [applyS, frameSub, assoc_append_or, hn, Option.none_or]
|
|
241
|
+
|
|
242
|
+
-- The frame σ, rd denotes the state ρ, M (read from the first state W, M0): every variable outside the inputs and
|
|
243
|
+
-- the zone, and every map read.
|
|
244
|
+
def Denotes (f : FMMachine) (Z : List Nat) (W : Nat → Nat) (M0 : Nat → Nat → Nat) (σ : List (Nat × E)) (rd : Nat → E → E)
|
|
245
|
+
(ρ : Nat → Nat) (M : Nat → Nat → Nat) : Prop :=
|
|
246
|
+
(∀ v, v ∉ f.m.inputs → v ∉ Z → applyS σ W M0 v = ρ v) ∧ (∀ mp k, evalE W M0 (rd mp k) = M mp (evalE W M0 k))
|
|
247
|
+
|
|
248
|
+
-- The frame a step reads (its inputs set) agrees with the state and request outside the zone.
|
|
249
|
+
def HeadAgree (f : FMMachine) (Z : List Nat) (W : Nat → Nat) (M0 : Nat → Nat → Nat) (σ : List (Nat × E)) (ρ : Nat → Nat) :
|
|
250
|
+
List (List (Nat × Nat)) → List (Nat → Nat) → Prop
|
|
251
|
+
| r :: _, ι :: _ => Agree Z (applyS (frameSub r σ) W M0) (withInputs f.m.inputs ρ ι)
|
|
252
|
+
| _, _ => True
|
|
253
|
+
|
|
254
|
+
section
|
|
255
|
+
variable (f : FMMachine) (Z : List Nat) (W : Nat → Nat) (M0 : Nat → Nat → Nat)
|
|
256
|
+
|
|
257
|
+
-- A term of the step read at the frame is its value at the real state, flags decided.
|
|
258
|
+
theorem frame_term {σ : List (Nat × E)} {rd : Nat → E → E} {ρ : Nat → Nat} {M : Nat → Nat → Nat} {r : List (Nat × Nat)} {ι : Nat → Nat}
|
|
259
|
+
(hrd : ∀ mp k, evalE W M0 (rd mp k) = M mp (evalE W M0 k))
|
|
260
|
+
(hX : Agree Z (applyS (frameSub r σ) W M0) (withInputs f.m.inputs ρ ι)) (e : E) (he : avoidsE Z (inlineE f.flags e) = true) :
|
|
261
|
+
evalE W M0 (substRE (frameSub r σ) rd (inlineE f.flags e)) = evalE (decideFlags f.flags (withInputs f.m.inputs ρ ι) M) M e := by
|
|
262
|
+
rw [substR_evalE (frameSub r σ) rd W M0 M hrd,
|
|
263
|
+
congrE _ (withInputs f.m.inputs ρ ι) M _ (fun i hi => hX i (avoidsE_occ he hi)), inline_evalE]
|
|
264
|
+
|
|
265
|
+
theorem step_denotes {σ : List (Nat × E)} {rd : Nat → E → E} {ρ : Nat → Nat} {M : Nat → Nat → Nat} (r : List (Nat × Nat)) (ι : Nat → Nat)
|
|
266
|
+
(hD : Denotes f Z W M0 σ rd ρ M)
|
|
267
|
+
(hX : Agree Z (applyS (frameSub r σ) W M0) (withInputs f.m.inputs ρ ι))
|
|
268
|
+
(hfz : ∀ v ∈ flagVars f.flags, v ∈ Z)
|
|
269
|
+
(hst : ∀ p ∈ f.m.step, avoidsE Z (inlineE f.flags p.2) = true)
|
|
270
|
+
(hwr : ∀ w ∈ f.m.writes, avoidsE Z (inlineE f.flags w.2.1) = true ∧ avoidsE Z (inlineE f.flags w.2.2) = true) :
|
|
271
|
+
Denotes f Z W M0 (stepSub f (frameSub r σ) rd) (stepRd f (frameSub r σ) rd) (kNext f ρ M ι).1 (kNext f ρ M ι).2 := by
|
|
272
|
+
obtain ⟨_, hrd⟩ := hD
|
|
273
|
+
refine ⟨fun v hvi hvz => ?_, fun mp k => ?_⟩
|
|
274
|
+
· simp only [kNext, applyS, stepSub, assoc_append_or, assoc_stepSub]
|
|
275
|
+
cases h : assoc f.m.step v with
|
|
276
|
+
| some e =>
|
|
277
|
+
have hm : (v, e) ∈ f.m.step := by
|
|
278
|
+
simp only [assoc, Option.map_eq_some_iff] at h
|
|
279
|
+
obtain ⟨q, hq, rfl⟩ := h
|
|
280
|
+
obtain ⟨hqm, hqv⟩ := lookup_some hq
|
|
281
|
+
simp only [beq_iff_eq] at hqv
|
|
282
|
+
rw [← hqv]; exact hqm
|
|
283
|
+
simp only [Option.map_some, Option.some_or]
|
|
284
|
+
exact frame_term f Z W M0 hrd hX e (hst (v, e) hm)
|
|
285
|
+
| none =>
|
|
286
|
+
simp only [Option.map_none, Option.none_or]
|
|
287
|
+
have hvf : v ∉ flagVars f.flags := fun h' => hvz (hfz v h')
|
|
288
|
+
rw [decide_other hvf, withInputs_other hvi]
|
|
289
|
+
show applyS (frameSub r σ) W M0 v = ρ v
|
|
290
|
+
rw [hX v hvz, withInputs_other hvi]
|
|
291
|
+
· simp only [kNext, stepRd, writeMaps]
|
|
292
|
+
cases hw : lookup (fun w : Nat × E × E => w.1 == mp) f.m.writes with
|
|
293
|
+
| none => simp only [hrd]
|
|
294
|
+
| some w =>
|
|
295
|
+
have hm := (lookup_some hw).1
|
|
296
|
+
simp only [evalE, evalC, frame_term f Z W M0 hrd hX w.2.1 (hwr w hm).1, frame_term f Z W M0 hrd hX w.2.2 (hwr w hm).2, hrd]
|
|
297
|
+
by_cases hk : evalE W M0 k = evalE (decideFlags f.flags (withInputs f.m.inputs ρ ι) M) M w.2.1 <;> simp [hk]
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
-- The laws and the precondition hold at a reachable state with any request's inputs (neither reads an input).
|
|
301
|
+
theorem reach_facts (f : FMMachine) (ps : List MEntry) (hl : checkFMMachine f ps = true) {ρ : Nat → Nat} {M : Nat → Nat → Nat}
|
|
302
|
+
(hr : FMReach f ρ M) (ι : Nat → Nat) :
|
|
303
|
+
evalC (withInputs f.m.inputs ρ ι) M f.m.pre = true ∧ ∀ e ∈ ps, e.law.Holds (withInputs f.m.inputs ρ ι) M := by
|
|
304
|
+
have hl0 := hl
|
|
305
|
+
simp only [checkFMMachine, Bool.and_eq_true, List.all_eq_true] at hl
|
|
306
|
+
obtain ⟨⟨⟨hf, hpref⟩, _⟩, hc⟩ := hl
|
|
307
|
+
obtain ⟨ρ₀, hr₀, hag⟩ := fmreach_plain f hf hr
|
|
308
|
+
have hlaws : ∀ e ∈ ps, e.law.Holds ρ M := fmmachine_sound f ps hl0 ρ M hr
|
|
309
|
+
have hcm := hc
|
|
310
|
+
simp only [checkMMachine, Bool.and_eq_true, List.all_eq_true] at hcm
|
|
311
|
+
obtain ⟨⟨hpa, hpr⟩, hent⟩ := hcm
|
|
312
|
+
try simp only [Bool.not_eq_true'] at hpr
|
|
313
|
+
have hpre : evalC ρ M f.m.pre = true := by
|
|
314
|
+
rw [congrC ρ ρ₀ M f.m.pre (fun j hj => hag j (avoids_occ hpref hj))]
|
|
315
|
+
exact pre_mreach (m := f.plain) hpa hpr hr₀
|
|
316
|
+
have hagX : Agree f.m.inputs (withInputs f.m.inputs ρ ι) ρ := fun i hi => withInputs_other hi ρ ι
|
|
317
|
+
refine ⟨?_, fun e he => holds_agree e.law (hent e he).1.1.1 hagX M (hlaws e he)⟩
|
|
318
|
+
rw [congrC (withInputs f.m.inputs ρ ι) ρ M f.m.pre (fun j hj => hagX j (fun h =>
|
|
319
|
+
avoids_occ hpa hj (List.mem_append_left _ (List.mem_append_left _ h))))]
|
|
320
|
+
exact hpre
|
|
321
|
+
|
|
322
|
+
-- Every hypothesis of a frame holds where the frame is read.
|
|
323
|
+
theorem frame_hyps (f : FMMachine) (ps : List MEntry) (hl : checkFMMachine f ps = true) (Z : List Nat) (insts : List (Nat × E))
|
|
324
|
+
{W : Nat → Nat} {M0 : Nat → Nat → Nat} {σ : List (Nat × E)} {rd : Nat → E → E} {ρ : Nat → Nat} {M : Nat → Nat → Nat}
|
|
325
|
+
{r : List (Nat × Nat)} {ι : Nat → Nat} {w : C}
|
|
326
|
+
(hr : FMReach f ρ M) (hrd : ∀ mp k, evalE W M0 (rd mp k) = M mp (evalE W M0 k))
|
|
327
|
+
(hX : Agree Z (applyS (frameSub r σ) W M0) (withInputs f.m.inputs ρ ι))
|
|
328
|
+
(hav : ∀ c ∈ frameHyps f (ps.map (·.law)) insts w, avoids Z c = true)
|
|
329
|
+
(hA : evalC (decideFlags f.flags (withInputs f.m.inputs ρ ι) M) M f.m.assume = true)
|
|
330
|
+
(hW : evalC (withInputs f.m.inputs ρ ι) M w = true) :
|
|
331
|
+
∀ c ∈ frameHyps f (ps.map (·.law)) insts w, evalC W M0 (substRC (frameSub r σ) rd c) = true := by
|
|
332
|
+
intro c hc
|
|
333
|
+
obtain ⟨hpre, hlaws⟩ := reach_facts f ps hl hr ι
|
|
334
|
+
have hb := before_true f ps ⟨w, w, [], [], [], insts, ([], .leaf [] [] [] []), ([], .leaf [] [] [] [])⟩ _ M hpre hlaws hW
|
|
335
|
+
have hY : evalC (withInputs f.m.inputs ρ ι) M c = true := by
|
|
336
|
+
simp only [frameHyps, List.mem_append, List.mem_cons, List.not_mem_nil, or_false] at hc
|
|
337
|
+
rcases hc with (hc | hc) | rfl | rfl
|
|
338
|
+
· exact hb c (by simp only [progressBefore, List.mem_append]; exact Or.inl (Or.inl (Or.inr hc)))
|
|
339
|
+
· exact hb c (by simp only [progressBefore, List.mem_append]; exact Or.inl (Or.inr hc))
|
|
340
|
+
· rw [inline_evalC]; exact hA
|
|
341
|
+
· exact hW
|
|
342
|
+
rw [substR_evalC (frameSub r σ) rd W M0 M hrd, congrC _ (withInputs f.m.inputs ρ ι) M c (fun i hi => hX i (avoids_occ (hav c hc) hi))]
|
|
343
|
+
exact hY
|
|
344
|
+
|
|
345
|
+
theorem krun_sound (f : FMMachine) (ps : List MEntry) (hl : checkFMMachine f ps = true) (Z : List Nat) (insts : List (Nat × E))
|
|
346
|
+
(W : Nat → Nat) (M0 : Nat → Nat → Nat)
|
|
347
|
+
(hfz : ∀ v ∈ flagVars f.flags, v ∈ Z)
|
|
348
|
+
(hst : ∀ p ∈ f.m.step, avoidsE Z (inlineE f.flags p.2) = true)
|
|
349
|
+
(hwr : ∀ w ∈ f.m.writes, avoidsE Z (inlineE f.flags w.2.1) = true ∧ avoidsE Z (inlineE f.flags w.2.2) = true) :
|
|
350
|
+
∀ (ws : List C) (rs : List (List (Nat × Nat))) (ιs : List (Nat → Nat)) (σ : List (Nat × E)) (rd : Nat → E → E)
|
|
351
|
+
(ρ : Nat → Nat) (M : Nat → Nat → Nat),
|
|
352
|
+
ws.length = rs.length → ws.length = ιs.length →
|
|
353
|
+
(∀ w ∈ ws, ∀ c ∈ frameHyps f (ps.map (·.law)) insts w, avoids Z c = true) →
|
|
354
|
+
(∀ r ∈ rs, ∀ p ∈ r, p.1 ∈ f.m.inputs) →
|
|
355
|
+
(∀ r ∈ rs.tail, ∀ i ∈ f.m.inputs, ∃ p ∈ r, p.1 = i) →
|
|
356
|
+
RenOK W rs.tail ιs.tail →
|
|
357
|
+
FMReach f ρ M → Denotes f Z W M0 σ rd ρ M → HeadAgree f Z W M0 σ ρ rs ιs → KAllowed f ws ρ M ιs →
|
|
358
|
+
(∀ c ∈ (kRun f (ps.map (·.law)) insts ws rs σ rd).1, evalC W M0 c = true)
|
|
359
|
+
∧ Denotes f Z W M0 (kRun f (ps.map (·.law)) insts ws rs σ rd).2.1 (kRun f (ps.map (·.law)) insts ws rs σ rd).2.2
|
|
360
|
+
(kEnd f ρ M ιs).1 (kEnd f ρ M ιs).2
|
|
361
|
+
| [], rs, ιs, σ, rd, ρ, M, hl1, hl2, _, _, _, _, _, hD, _, _ => by
|
|
362
|
+
cases rs with
|
|
363
|
+
| cons _ _ => simp at hl1
|
|
364
|
+
| nil =>
|
|
365
|
+
cases ιs with
|
|
366
|
+
| cons _ _ => simp at hl2
|
|
367
|
+
| nil => exact ⟨fun c hc => by simp [kRun] at hc, hD⟩
|
|
368
|
+
| w :: ws, rs, ιs, σ, rd, ρ, M, hl1, hl2, hav, hkeys, hcov, hren, hr, hD, hH, hK => by
|
|
369
|
+
rcases rs with _ | ⟨r, rs⟩
|
|
370
|
+
· simp at hl1
|
|
371
|
+
rcases ιs with _ | ⟨ι, ιs⟩
|
|
372
|
+
· simp at hl2
|
|
373
|
+
simp only [List.length_cons, Nat.add_right_cancel_iff] at hl1 hl2
|
|
374
|
+
simp only [List.tail_cons] at hcov hren
|
|
375
|
+
obtain ⟨hA, hW, hK'⟩ := hK
|
|
376
|
+
have hX : Agree Z (applyS (frameSub r σ) W M0) (withInputs f.m.inputs ρ ι) := hH
|
|
377
|
+
have hD' := step_denotes f Z W M0 r ι hD hX hfz hst hwr
|
|
378
|
+
have hr' : FMReach f (kNext f ρ M ι).1 (kNext f ρ M ι).2 := FMReach.step ρ M ι hr hA
|
|
379
|
+
-- The next step's frame, its inputs set, agrees with the next state and request.
|
|
380
|
+
have hH' : HeadAgree f Z W M0 (stepSub f (frameSub r σ) rd) (kNext f ρ M ι).1 rs ιs := by
|
|
381
|
+
rcases rs with _ | ⟨r', rs'⟩
|
|
382
|
+
· exact trivial
|
|
383
|
+
rcases ιs with _ | ⟨ι', ιs'⟩
|
|
384
|
+
· exact trivial
|
|
385
|
+
intro v hvz
|
|
386
|
+
by_cases hvi : v ∈ f.m.inputs
|
|
387
|
+
· show applyS (frameSub r' (stepSub f (frameSub r σ) rd)) W M0 v = withInputs f.m.inputs (kNext f ρ M ι).1 ι' v
|
|
388
|
+
rw [applyS_frame_in (hcov r' List.mem_cons_self v hvi) hren.1]
|
|
389
|
+
simp only [withInputs, List.contains_iff_mem, hvi, ite_true]
|
|
390
|
+
· show applyS (frameSub r' (stepSub f (frameSub r σ) rd)) W M0 v = withInputs f.m.inputs (kNext f ρ M ι).1 ι' v
|
|
391
|
+
rw [applyS_frame_out (fun p hp he => hvi (by rw [← he]; exact hkeys r' (List.mem_cons_of_mem _ List.mem_cons_self) p hp)),
|
|
392
|
+
hD'.1 v hvi hvz, withInputs_other hvi]
|
|
393
|
+
have hren' : RenOK W rs.tail ιs.tail := by
|
|
394
|
+
rcases rs with _ | ⟨r', rs'⟩
|
|
395
|
+
· exact trivial
|
|
396
|
+
rcases ιs with _ | ⟨ι', ιs'⟩
|
|
397
|
+
· cases rs' <;> exact trivial
|
|
398
|
+
exact hren.2
|
|
399
|
+
obtain ⟨ih1, ih2⟩ := krun_sound f ps hl Z insts W M0 hfz hst hwr ws rs ιs _ _ _ _ hl1 hl2
|
|
400
|
+
(fun w' hw' => hav w' (List.mem_cons_of_mem _ hw'))
|
|
401
|
+
(fun r' hr'' => hkeys r' (List.mem_cons_of_mem _ hr''))
|
|
402
|
+
(fun r' hr'' => hcov r' (List.mem_of_mem_tail hr''))
|
|
403
|
+
hren' hr' hD' hH' hK'
|
|
404
|
+
refine ⟨fun c hc => ?_, ih2⟩
|
|
405
|
+
simp only [kRun, List.mem_append, List.mem_map] at hc
|
|
406
|
+
rcases hc with ⟨c', hc', rfl⟩ | hc
|
|
407
|
+
· exact frame_hyps f ps hl Z insts hr hD.2 hX (hav w List.mem_cons_self) hA hW c' hc'
|
|
408
|
+
· exact ih1 c hc
|
|
409
|
+
|
|
410
|
+
theorem kstep_sound (f : FMMachine) (ps : List MEntry) (sl : KStep)
|
|
411
|
+
(hl : checkFMMachine f ps = true) (hs : checkKStep f ps sl = true) : KStepHolds f sl := by
|
|
412
|
+
intro ρ M hr ι₁ ιs hlen hK
|
|
413
|
+
simp only [checkKStep, Bool.and_eq_true, beq_iff_eq, List.all_eq_true] at hs
|
|
414
|
+
obtain ⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨hlw, hhead⟩, hdist⟩, hkeys⟩, hcov⟩, hrv⟩, hpr⟩, hpre⟩, hws⟩, hst⟩, hwr⟩, hthen⟩, hgoal⟩ := hs
|
|
415
|
+
have hflags : ∀ v ∈ flagVars f.flags, v ∈ zone f sl := fun v hv => List.mem_append_right _ hv
|
|
416
|
+
obtain ⟨rs, hrs⟩ : ∃ rs, sl.ren = [] :: rs := by
|
|
417
|
+
cases h : sl.ren with
|
|
418
|
+
| nil => rw [h] at hhead; simp at hhead
|
|
419
|
+
| cons r rs => rw [h] at hhead; simp only [List.head?_cons, Option.some.injEq] at hhead; exact ⟨rs, by rw [hhead]⟩
|
|
420
|
+
let W := renEnv sl.ren (ι₁ :: ιs) (withInputs f.m.inputs ρ ι₁)
|
|
421
|
+
have hWo : ∀ v, v ∉ zone f sl → W v = withInputs f.m.inputs ρ ι₁ v := fun v hv =>
|
|
422
|
+
renEnv_other _ _ _ v (fun h => hv (List.mem_append_left _ h))
|
|
423
|
+
have hD0 : Denotes f (zone f sl) W M [] getRd ρ M :=
|
|
424
|
+
⟨fun v hvi hvz => by rw [applyS_nil', hWo v hvz, withInputs_other hvi], fun mp k => by simp [getRd, evalE]⟩
|
|
425
|
+
have hH0 : HeadAgree f (zone f sl) W M [] ρ sl.ren (ι₁ :: ιs) := by
|
|
426
|
+
rw [hrs]
|
|
427
|
+
show Agree _ (applyS (frameSub [] []) W M) (withInputs f.m.inputs ρ ι₁)
|
|
428
|
+
intro v hvz
|
|
429
|
+
simp only [frameSub, List.map_nil, List.append_nil, applyS_nil']; exact hWo v hvz
|
|
430
|
+
have hren0 : RenOK W sl.ren.tail (ι₁ :: ιs).tail := by
|
|
431
|
+
have h : RenOK W sl.ren (ι₁ :: ιs) := renEnv_ok sl.ren (ι₁ :: ιs) (withInputs f.m.inputs ρ ι₁) hdist
|
|
432
|
+
rw [hrs] at h ⊢; exact h.2
|
|
433
|
+
obtain ⟨hh, hfin⟩ := krun_sound f ps hl (zone f sl) sl.insts W M hflags
|
|
434
|
+
hst hwr
|
|
435
|
+
sl.whens sl.ren (ι₁ :: ιs) [] getRd ρ M hlw (by simp only [List.length_cons]; omega)
|
|
436
|
+
hws (fun r hr p hp => List.contains_iff_mem.mp (hkeys r hr p hp))
|
|
437
|
+
(fun r hr i hi => by have := hcov r hr i hi; simpa only [List.any_eq_true, beq_iff_eq] using this)
|
|
438
|
+
hren0 hr hD0 hH0 hK
|
|
439
|
+
apply Decidable.byContradiction; intro hn
|
|
440
|
+
apply sound _ _ hgoal W M
|
|
441
|
+
intro c hc
|
|
442
|
+
simp only [kGoal, List.mem_append, List.mem_cons, List.not_mem_nil, or_false] at hc
|
|
443
|
+
rcases hc with (rfl | hc) | rfl
|
|
444
|
+
· -- The precondition: at the first state (it reads no input and no fresh variable).
|
|
445
|
+
obtain ⟨hpX, _⟩ := reach_facts f ps hl hr ι₁
|
|
446
|
+
rw [congrC W (withInputs f.m.inputs ρ ι₁) M f.m.pre (fun j hj => hWo j (avoids_occ hpre hj))]; exact hpX
|
|
447
|
+
· exact hh c hc
|
|
448
|
+
· -- `then`, read across the run, is what KStepHolds reads.
|
|
449
|
+
have hrdE : ∀ mp k, evalE W M (kEndRd sl (kOut f (ps.map (·.law)) sl).2.2 mp k)
|
|
450
|
+
= primedMaps sl.primeMaps (kEnd f ρ M (ι₁ :: ιs)).2 M mp (evalE W M k) := by
|
|
451
|
+
intro mp k
|
|
452
|
+
simp only [kEndRd, primedMaps]
|
|
453
|
+
cases hq : lookup (fun q => q.2 == mp) sl.primeMaps with
|
|
454
|
+
| none => simp [evalE]
|
|
455
|
+
| some q => exact hfin.2 q.1 k
|
|
456
|
+
have henv : applyS (kEndSub sl (kOut f (ps.map (·.law)) sl).2.1 (kOut f (ps.map (·.law)) sl).2.2) W M
|
|
457
|
+
= primed sl.prime (kEnd f ρ M (ι₁ :: ιs)).1 W := by
|
|
458
|
+
funext i
|
|
459
|
+
rw [kEndSub, applyS_primes]
|
|
460
|
+
simp only [primed]
|
|
461
|
+
cases hq : lookup (fun q => q.2 == i) sl.prime with
|
|
462
|
+
| none => rfl
|
|
463
|
+
| some q =>
|
|
464
|
+
have hq1 := hpr q (lookup_some hq).1
|
|
465
|
+
simp only [Bool.not_eq_true'] at hq1
|
|
466
|
+
have hqi : q.1 ∉ f.m.inputs := by simpa using hq1.1
|
|
467
|
+
have hqz : q.1 ∉ zone f sl := by simpa using hq1.2
|
|
468
|
+
show evalE W M (substRE (kOut f (ps.map (·.law)) sl).2.1 (kOut f (ps.map (·.law)) sl).2.2 (.var q.1)) = (kEnd f ρ M (ι₁ :: ιs)).1 q.1
|
|
469
|
+
have hfin' : Denotes f (zone f sl) W M (kOut f (ps.map fun e : MEntry => e.law) sl).2.1 (kOut f (ps.map fun e : MEntry => e.law) sl).2.2
|
|
470
|
+
(kEnd f ρ M (ι₁ :: ιs)).1 (kEnd f ρ M (ι₁ :: ιs)).2 := hfin
|
|
471
|
+
exact (substR_evalE (kOut f (ps.map fun e : MEntry => e.law) sl).2.1 _ W M _ hfin'.2 (.var q.1)).trans (hfin'.1 q.1 hqi hqz)
|
|
472
|
+
simp only [evalC, substR_evalC _ _ W M _ hrdE, henv]
|
|
473
|
+
simpa using hn
|
|
474
|
+
|
|
475
|
+
end Kern
|
|
476
|
+
|
|
477
|
+
#print axioms Kern.kstep_sound
|