Repository navigation
Expand file tree
/
Copy pathObs.lean
More file actions
3147 lines (3033 loc) · 156 KB
/
Copy pathObs.lean
File metadata and controls
3147 lines (3033 loc) · 156 KB
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- LEGACY: statement target of pre-rebuild theorems; compiles, refuses what
-- it does not implement, gains no consumers; deleted when re-founded.
import LeanModels.Core.Order
import LeanModels.Python.Logic
/-!
# Fuel monotonicity and cross-fuel determinism (`LeanModels.Python`)
The enabling theorems for the `Obs` spine (docs/spec-surface.md §10): adding
fuel never changes a decided (non-`timeout`) interpreter result. This is what
makes the fuel parameter a pure implementation detail — any two runs that
decide, at any two fuels, decide identically, so `CallsTo` is functional and
the strengthened partial judgment `~~>` becomes stateable.
Since the H1 core re-shape the mutual block is `Run`-typed (state is data:
the decided outcome CONTAINS the final `FrameState`/`World`, and
monotonicity is in fuel only — a decided state survives fuel increase
exactly like a decided value), and the public `callFunction` is a
non-recursive wrapper. Structure:
* `Res.le` (`x ⊑ y`) — the flat approximation order on public results, and
`Run.le` (`x ⊑ʳ y`) — the same order on `Run`-typed outcomes. Fuel-indexed
runs form a chain in them.
* `fuelMono` — THE theorem: one conjunction over all nine functions of the
interpreter's mutual block (`evalExpr`, `evalExprs`, `evalBoolChain`,
`evalCompareChain`, `execStmt`, `execStmts`, `execWhile`, `callIn`,
`execFor`), proved by a single induction on fuel. Each case is symbolic
execution of one interpreter step, glued by the congruence lemmas
`Run.le_bind` / `Run.le_ite` / `Run.le_withLocals` / `Run.le_toWorld`
(every fuel-free helper is `⊑ʳ`-reflexive, every recursive call is the
induction hypothesis at the decremented fuel).
* `evalExpr_mono` … `callIn_mono`, `execFor_mono` — the per-function
corollaries in implication form, and `callFunction_mono` — the public
monotonicity, derived through the wrapper decomposition (thaw and freeze
are fuel-free; the only fuel inside the wrapper is `callIn`'s).
* `callFunction_det` — cross-fuel determinism; `CallsTo.functional` /
`CallsTo.not_raises` — the spec-level consequences.
* `PyOut` / `Obs` — the observation spine itself (docs/spec-surface.md §10):
the four-way outcome partition of a call, with fuel confined inside the
judgment; `Obs.det` (at most one outcome, stuck *messages included*) and
`Obs.total` (at least one, classically), hence `Obs.existsUnique` — the
outcome is a well-defined denotation of the call.
-/
namespace LeanModels.Python
/-! ## The approximation order on results -/
/-- Flat approximation order on public interpreter results: `x ⊑ y` iff `x`
is `timeout` (the run gave up) or `x = y` (the run decided, and `y` agrees). -/
protected def Res.le {α : Type} (x y : Res α) : Prop :=
x = .timeout ∨ x = y
@[inherit_doc] scoped infix:50 " ⊑ " => Res.le
theorem Res.le_iff {α : Type} {x y : Res α} :
x ⊑ y ↔ (x = .timeout ∨ x = y) := Iff.rfl
theorem Res.le_refl {α : Type} (x : Res α) : x ⊑ x := Or.inr rfl
theorem Res.timeout_le {α : Type} (y : Res α) : (.timeout : Res α) ⊑ y :=
Or.inl rfl
/-- A decided (non-`timeout`) lower bound is already the value: `⊑` collapses
to equality. This is the extraction step of every `_mono` corollary. -/
theorem Res.le_eq {α : Type} {x y : Res α} (h : x ⊑ y) (hx : x ≠ .timeout) :
x = y := (Res.le_iff.mp h).resolve_left hx
/-- **`Res.le` IS Core's flat order** (`LeanModels/Core/Order.lean`), and this
`Iff.rfl` is the whole bridge. Stated as an iff rather than as a redefinition on
purpose: `Res.le`'s spelling, its `⊑` notation and its consumers all stay put,
and the tree gains the shared name additively. The congruences below stay
tier-local — `Sv.Res` and `Python.Res` are different types, so `le_bind` and
`le_ite` are each tier's own. -/
theorem Res.le_iff_flatLe {α : Type} {x y : Res α} :
x ⊑ y ↔ FlatLe .timeout x y := Iff.rfl
/-- Congruence of `⊑` under `bind`: run the prefix (IH), then the
continuation pointwise (IH again, or reflexivity for fuel-free tails). -/
theorem Res.le_bind {α β : Type} {x x' : Res α} {f f' : α → Res β}
(hx : x ⊑ x') (hf : ∀ a, f a ⊑ f' a) : (x >>= f) ⊑ (x' >>= f') := by
rcases hx with h | h
· subst h; exact Or.inl rfl
· subst h
cases x with
| ok a => exact hf a
| exn e => exact Or.inr rfl
| timeout => exact Or.inl rfl
| unsupported msg => exact Or.inr rfl
/-- Congruence of `⊑` under `if`: same condition on both sides, each branch
by its own proof. -/
theorem Res.le_ite {α : Type} {c : Prop} [Decidable c] {x x' y y' : Res α}
(hx : x ⊑ x') (hy : y ⊑ y') :
(if c then x else y) ⊑ (if c then x' else y') := by
by_cases h : c
· simpa only [if_pos h] using hx
· simpa only [if_neg h] using hy
/-! ## The approximation order on `Run`-typed outcomes
State is data: `x ⊑ʳ y` compares whole outcomes — final state, value, error,
message and all. `fuelMono` shows every function of the mutual block is
monotone in fuel wrt `⊑ʳ`: a run that decided keeps its exact outcome
(state included) at any higher fuel. -/
/-- Flat approximation order on `Run`-typed outcomes. -/
protected def Run.le {σ α : Type} (x y : Run σ α) : Prop :=
x = .timeout ∨ x = y
@[inherit_doc] scoped infix:50 " ⊑ʳ " => Run.le
theorem Run.le_iff {σ α : Type} {x y : Run σ α} :
x ⊑ʳ y ↔ (x = .timeout ∨ x = y) := Iff.rfl
theorem Run.le_refl {σ α : Type} (x : Run σ α) : x ⊑ʳ x := Or.inr rfl
theorem Run.timeout_le {σ α : Type} (y : Run σ α) :
(.timeout : Run σ α) ⊑ʳ y := Or.inl rfl
/-- A decided (non-`timeout`) lower bound is already the outcome. -/
theorem Run.le_eq {σ α : Type} {x y : Run σ α} (h : x ⊑ʳ y)
(hx : x ≠ .timeout) : x = y := (Run.le_iff.mp h).resolve_left hx
/-- The same bridge for the state-carrying order — the fourth instance of the
one shape. -/
theorem Run.le_iff_flatLe {σ α : Type} {x y : Run σ α} :
x ⊑ʳ y ↔ FlatLe .timeout x y := Iff.rfl
/-- Congruence of `⊑ʳ` under `Run.bind`: run the prefix (IH), then the
continuation pointwise at every intermediate state. -/
theorem Run.le_bind {σ α β : Type} {x x' : Run σ α} {f f' : σ → α → Run σ β}
(hx : x ⊑ʳ x') (hf : ∀ s a, f s a ⊑ʳ f' s a) : x.bind f ⊑ʳ x'.bind f' := by
rcases hx with h | h
· subst h; exact Or.inl rfl
· subst h
cases x with
| ok s a => exact hf s a
| exn s e => exact Or.inr rfl
| timeout => exact Or.inl rfl
| unsupported msg => exact Or.inr rfl
/-- Congruence of `⊑ʳ` under `Run.bindE` (the exceptions tier: the
stepper's close-on-exn-through-resume continuation — `fuelMono`'s glue
for the one `bindE` consumer). -/
theorem Run.le_bindE {σ α β : Type} {x x' : Run σ α} {f f' : σ → α → Run σ β}
{g g' : σ → PyErr → Run σ β} (hx : x ⊑ʳ x')
(hf : ∀ s a, f s a ⊑ʳ f' s a) (hg : ∀ s e, g s e ⊑ʳ g' s e) :
x.bindE f g ⊑ʳ x'.bindE f' g' := by
rcases hx with h | h
· subst h; exact Or.inl rfl
· subst h
cases x with
| ok s a => exact hf s a
| exn s e => exact hg s e
| timeout => exact Or.inl rfl
| unsupported msg => exact Or.inr rfl
/-- Congruence of `⊑ʳ` under `if`. -/
theorem Run.le_ite {σ α : Type} {c : Prop} [Decidable c] {x x' y y' : Run σ α}
(hx : x ⊑ʳ x') (hy : y ⊑ʳ y') :
(if c then x else y) ⊑ʳ (if c then x' else y') := by
by_cases h : c
· simpa only [if_pos h] using hx
· simpa only [if_neg h] using hy
/-- Congruence of `⊑ʳ` under `Run.withLocals` (the nested-call splice). -/
theorem Run.le_withLocals {α : Type} {l : REnv} {x x' : Run World α}
(h : x ⊑ʳ x') : Run.withLocals l x ⊑ʳ Run.withLocals l x' := by
rcases h with h | h
· subst h; exact Or.inl rfl
· subst h; exact Or.inr rfl
/-- Congruence of `⊑ʳ` under `Run.toWorld` (the call-return projection). -/
theorem Run.le_toWorld {α : Type} {x x' : Run FrameState α}
(h : x ⊑ʳ x') : Run.toWorld x ⊑ʳ Run.toWorld x' := by
rcases h with h | h
· subst h; exact Or.inl rfl
· subst h; exact Or.inr rfl
/-- Congruence of `⊑`→`⊑ʳ` under `Run.liftRes` (the fueled helper splice:
`evalCompareOpH` is fuel-dependent since H1-proper). -/
theorem Run.le_liftRes {σ α : Type} {s : σ} {x y : Res α} (h : x ⊑ y) :
Run.liftRes s x ⊑ʳ Run.liftRes s y := by
rcases h with h | h
· subst h; exact Or.inl rfl
· subst h; exact Or.inr rfl
/-! ## Dict-equality fuel monotonicity (the `heapEq` mutual block)
`heapEq` is the only fueled helper outside the interpreter's mutual block
(dict `==` recurses through stored values). Its monotonicity is what lets
`fuelMono`'s compare-chain case splice it. -/
/-- Fuel monotonicity for the `heapEq`/`heapEqList`/`heapEqEntries` block,
one conjunction, by induction on fuel. -/
theorem heapEqMono (fuel : Nat) :
(∀ (h : Heap) (active : List (Addr × Addr)) (a b : RVal) (fuel' : Nat),
fuel ≤ fuel' → heapEq h fuel active a b ⊑ heapEq h fuel' active a b) ∧
(∀ (h : Heap) (active : List (Addr × Addr)) (as bs : List RVal) (fuel' : Nat),
fuel ≤ fuel' → heapEqList h fuel active as bs ⊑ heapEqList h fuel' active as bs) ∧
(∀ (h : Heap) (active : List (Addr × Addr)) (left right : List (RVal × RVal))
(fuel' : Nat), fuel ≤ fuel' →
heapEqEntries h fuel active left right ⊑ heapEqEntries h fuel' active left right) := by
induction fuel with
| zero =>
refine ⟨?_, ?_, ?_⟩
· exact fun h active a b fuel' _ => Or.inl (by simp [heapEq])
· exact fun h active as bs fuel' _ => Or.inl (by simp [heapEqList])
· exact fun h active l r fuel' _ => Or.inl (by simp [heapEqEntries])
| succ fuel ih =>
obtain ⟨ihE, ihL, ihN⟩ := ih
refine ⟨?_, ?_, ?_⟩
· intro h active a b fuel' hf
cases fuel' with
| zero => exact absurd hf (Nat.not_succ_le_zero fuel)
| succ k =>
have hk : fuel ≤ k := Nat.le_of_succ_le_succ hf
cases a <;> cases b <;> simp only [heapEq] <;>
try exact Res.le_refl _
-- remaining: tuple/tuple, listV/listV, the tuple/namedtuple square
-- (elementwise), ref/ref (dicts)
case tuple.tuple xs ys => exact ihL h active xs.toList ys.toList k hk
case listV.listV xs ys => exact ihL h active xs.toList ys.toList k hk
case ntuple.ntuple tn1 fs1 xs tn2 fs2 ys =>
exact ihL h active xs.toList ys.toList k hk
case ntuple.tuple tn1 fs1 xs ys =>
exact ihL h active xs.toList ys.toList k hk
case tuple.ntuple xs tn2 fs2 ys =>
exact ihL h active xs.toList ys.toList k hk
case ref.ref x y =>
refine Res.le_ite (Res.le_refl _) (Res.le_ite (Res.le_refl _) ?_)
cases hx : Heap.get? h x with
| none =>
cases Heap.get? h y <;> exact Res.le_refl _
| some o1 =>
cases Heap.get? h y with
| none => cases o1 <;> exact Res.le_refl _
| some o2 =>
cases o1 with
| cell cv => cases o2 <;> exact Res.le_refl _
| dict es v1 =>
cases o2 with
| dict fs v2 =>
exact Res.le_ite (ihN h ((x, y) :: active) es.toList fs.toList k hk)
(Res.le_refl _)
| list ys => exact Res.le_refl _
| «instance» ci attrs => exact Res.le_refl _
| generator qn lo kk stt => exact Res.le_refl _
| cell cv => exact Res.le_refl _
| closure nm ps ao lo' hg ig bd cap => exact Res.le_refl _
| pyset zs => exact Res.le_refl _
| list xs =>
cases o2 with
| dict fs v2 => exact Res.le_refl _
| list ys =>
exact Res.le_ite (ihL h ((x, y) :: active) xs.toList ys.toList k hk)
(Res.le_refl _)
| «instance» ci attrs => exact Res.le_refl _
| generator qn lo kk stt => exact Res.le_refl _
| cell cv => exact Res.le_refl _
| closure nm ps ao lo' hg ig bd cap => exact Res.le_refl _
| pyset zs => exact Res.le_refl _
| «instance» ci attrs => cases o2 <;> exact Res.le_refl _
| generator qn lo kk stt => cases o2 <;> exact Res.le_refl _
| closure nm ps ao lo' hg ig bd cap =>
cases o2 <;> exact Res.le_refl _
| pyset zs => cases o2 <;> exact Res.le_refl _
· intro h active as bs fuel' hf
cases fuel' with
| zero => exact absurd hf (Nat.not_succ_le_zero fuel)
| succ k =>
have hk : fuel ≤ k := Nat.le_of_succ_le_succ hf
cases as with
| nil => cases bs <;> (simp only [heapEqList]; exact Res.le_refl _)
| cons a as' =>
cases bs with
| nil => simp only [heapEqList]; exact Res.le_refl _
| cons b bs' =>
simp only [heapEqList]
exact Res.le_bind (ihE h active a b k hk) fun e =>
Res.le_ite (ihL h active as' bs' k hk) (Res.le_refl _)
· intro h active l r fuel' hf
cases fuel' with
| zero => exact absurd hf (Nat.not_succ_le_zero fuel)
| succ k =>
have hk : fuel ≤ k := Nat.le_of_succ_le_succ hf
cases l with
| nil => simp only [heapEqEntries]; exact Res.le_refl _
| cons kv rest =>
obtain ⟨kk, vv⟩ := kv
simp only [heapEqEntries]
cases dictFind r kk with
| none => exact Res.le_refl _
| some w =>
exact Res.le_bind (ihE h active vv w k hk) fun e =>
Res.le_ite (ihN h active rest r k hk) (Res.le_refl _)
/-- Fuel monotonicity of the H2 list-membership scan (elementwise
`heapEq` splices). -/
theorem heapContainsScan_mono {h : Heap} {fuel : Nat} {x : RVal}
{l : List RVal} {fuel' : Nat} (hf : fuel ≤ fuel') :
heapContainsScan h fuel x l ⊑ heapContainsScan h fuel' x l := by
induction l with
| nil => exact Res.le_refl _
| cons v vs ih =>
simp only [heapContainsScan]
exact Res.le_bind ((heapEqMono fuel).1 h [] v x fuel' hf)
fun e => Res.le_ite (Res.le_refl _) ih
/-- Fuel monotonicity for `setDedup` (H7+ set construction): the
element-equality scans thread the fuel; the accumulator generalizes. -/
theorem setDedup_mono {h : Heap} {fuel fuel' : Nat} {xs : List RVal}
(hf : fuel ≤ fuel') :
∀ acc, setDedup h fuel acc xs ⊑ setDedup h fuel' acc xs := by
induction xs with
| nil => intro acc; exact Res.le_refl _
| cons v vs ih =>
intro acc
simp only [setDedup]
refine Res.le_ite ?_ (Res.le_refl _)
exact Res.le_bind (heapContainsScan_mono hf) fun dup =>
Res.le_ite (ih _) (ih _)
/-- Fuel monotonicity of heap-container membership. -/
theorem heapContains_mono {h : Heap} {fuel : Nat} {a : Addr} {k : RVal}
{fuel' : Nat} (hf : fuel ≤ fuel') :
heapContains h fuel a k ⊑ heapContains h fuel' a k := by
simp only [heapContains]
cases Heap.get? h a with
| none => exact Res.le_refl _
| some o =>
cases o with
| dict es v => exact Res.le_refl _
| list xs => exact heapContainsScan_mono hf
| «instance» ci attrs => exact Res.le_refl _
| generator qn lo kk stt => exact Res.le_refl _
| cell cv => exact Res.le_refl _
| closure nm ps ao lo' hg ig bd cap => exact Res.le_refl _
| pyset zs => exact Res.le_ite (heapContainsScan_mono hf) (Res.le_refl _)
/-- Fuel monotonicity of the heap deep-freeze (`freezeH`/`freezeListH`),
one conjunction by induction on fuel — the freeze leg of the public
wrapper's monotonicity decomposition (docs/memory-model.md v2). -/
theorem freezeHMono (fuel : Nat) :
(∀ (h : Heap) (path : List Addr) (v : RVal) (fuel' : Nat), fuel ≤ fuel' →
RVal.freezeH h fuel path v ⊑ RVal.freezeH h fuel' path v) ∧
(∀ (h : Heap) (path : List Addr) (l : List RVal) (fuel' : Nat), fuel ≤ fuel' →
RVal.freezeListH h fuel path l ⊑ RVal.freezeListH h fuel' path l) := by
induction fuel with
| zero =>
refine ⟨?_, ?_⟩
· exact fun h path v fuel' _ => Or.inl (by simp [RVal.freezeH])
· exact fun h path l fuel' _ => Or.inl (by simp [RVal.freezeListH])
| succ fuel ih =>
obtain ⟨ihV, ihL⟩ := ih
refine ⟨?_, ?_⟩
· intro h path v fuel' hf
cases fuel' with
| zero => exact absurd hf (Nat.not_succ_le_zero fuel)
| succ k =>
have hk : fuel ≤ k := Nat.le_of_succ_le_succ hf
cases v <;> simp only [RVal.freezeH] <;> try exact Res.le_refl _
case listV xs =>
exact Res.le_bind (ihL h path xs.toList k hk) fun vs => Res.le_refl _
case tuple xs =>
exact Res.le_bind (ihL h path xs.toList k hk) fun vs => Res.le_refl _
case ref a =>
refine Res.le_ite (Res.le_refl _) ?_
cases Heap.get? h a with
| none => exact Res.le_refl _
| some o =>
cases o with
| dict es v => exact Res.le_refl _
| «instance» ci attrs => exact Res.le_refl _
| generator qn lo kk stt => exact Res.le_refl _
| cell cv => exact Res.le_refl _
| closure nm ps ao lo' hg ig bd cap => exact Res.le_refl _
| pyset zs => exact Res.le_refl _
| list xs =>
exact Res.le_bind (ihL h (a :: path) xs.toList k hk)
fun vs => Res.le_refl _
· intro h path l fuel' hf
cases fuel' with
| zero => exact absurd hf (Nat.not_succ_le_zero fuel)
| succ k =>
have hk : fuel ≤ k := Nat.le_of_succ_le_succ hf
cases l with
| nil => simp only [RVal.freezeListH]; exact Res.le_refl _
| cons v vs =>
simp only [RVal.freezeListH]
exact Res.le_bind (ihV h path v k hk) fun v' =>
Res.le_bind (ihL h path vs k hk) fun vs' => Res.le_refl _
/-- Fuel monotonicity of container membership (H5 iteration): a str
receiver is pure, every element-scanning receiver splices the H2 scan. -/
theorem valContains_mono {h : Heap} {fuel : Nat} {a b : RVal}
{fuel' : Nat} (hf : fuel ≤ fuel') :
valContains h fuel a b ⊑ valContains h fuel' a b := by
cases b <;> simp only [valContains] <;> try exact Res.le_refl _
case ref d => exact heapContains_mono hf
case listV xs => exact heapContainsScan_mono hf
case tuple xs => exact heapContainsScan_mono hf
case ntuple tn fs xs => exact heapContainsScan_mono hf
/-- Fuel monotonicity of the heap-aware comparison step. -/
theorem evalCompareOpH_mono {h : Heap} {fuel : Nat} {op : CmpOp} {a b : RVal}
{fuel' : Nat} (hf : fuel ≤ fuel') :
evalCompareOpH h fuel op a b ⊑ evalCompareOpH h fuel' op a b := by
cases op <;> simp only [evalCompareOpH] <;> try exact Res.le_refl _
case eq =>
exact Res.le_ite (Res.le_refl _) ((heapEqMono fuel).1 h [] a b fuel' hf)
case notEq =>
exact Res.le_ite (Res.le_refl _)
(Res.le_bind ((heapEqMono fuel).1 h [] a b fuel' hf)
fun e => Res.le_refl _)
case inOp => exact valContains_mono hf
case notIn =>
exact Res.le_bind (valContains_mono hf) fun e => Res.le_refl _
/-! ## Fuel monotonicity — the enabling theorem -/
/-- **Fuel monotonicity**, one conjunction over the whole mutual block, by
induction on fuel: for every interpreter function `F` and `fuel ≤ fuel'`,
`F fuel ⊑ʳ F fuel'` — a run that decided keeps its exact outcome (final
state included) at any higher fuel. Conjunct order: `evalExpr`, `evalExprs`,
`evalBoolChain`, `evalCompareChain`, `execStmt`, `execStmts`, `execWhile`,
`callIn`, `execFor` (the mutual block's order — `callIn` sits where
`callFunction` sat before the H1 re-shape, keeping the projection paths of
the other corollaries stable). Consume it through the per-function `_mono`
corollaries below. -/
theorem fuelMono (fuel : Nat) :
(∀ (m : Module) (st : FrameState) (e : Expr) (fuel' : Nat), fuel ≤ fuel' →
evalExpr m fuel st e ⊑ʳ evalExpr m fuel' st e) ∧
(∀ (m : Module) (st : FrameState) (es : List Expr) (fuel' : Nat), fuel ≤ fuel' →
evalExprs m fuel st es ⊑ʳ evalExprs m fuel' st es) ∧
(∀ (m : Module) (st : FrameState) (op : BoolOp) (e : Expr) (rest : List Expr)
(fuel' : Nat), fuel ≤ fuel' →
evalBoolChain m fuel st op e rest ⊑ʳ evalBoolChain m fuel' st op e rest) ∧
(∀ (m : Module) (st : FrameState) (lhs : RVal) (ops : List CmpOp) (cs : List Expr)
(fuel' : Nat), fuel ≤ fuel' →
evalCompareChain m fuel st lhs ops cs ⊑ʳ evalCompareChain m fuel' st lhs ops cs) ∧
(∀ (m : Module) (st : FrameState) (s : Stmt) (fuel' : Nat), fuel ≤ fuel' →
execStmt m fuel st s ⊑ʳ execStmt m fuel' st s) ∧
(∀ (m : Module) (st : FrameState) (ss : List Stmt) (fuel' : Nat), fuel ≤ fuel' →
execStmts m fuel st ss ⊑ʳ execStmts m fuel' st ss) ∧
(∀ (m : Module) (st : FrameState) (test : Expr) (body orelse : List Stmt)
(fuel' : Nat), fuel ≤ fuel' →
execWhile m fuel st test body orelse ⊑ʳ execWhile m fuel' st test body orelse) ∧
(∀ (m : Module) (w : World) (fname : String) (args : Array RVal)
(fuel' : Nat), fuel ≤ fuel' →
callIn m fuel w fname args ⊑ʳ callIn m fuel' w fname args) ∧
(∀ (m : Module) (st : FrameState) (target : Expr) (xs : List RVal)
(body : List Stmt) (fuel' : Nat), fuel ≤ fuel' →
execFor m fuel st target xs body ⊑ʳ execFor m fuel' st target xs body) ∧
(∀ (m : Module) (st : FrameState) (keys values : List Expr) (fuel' : Nat),
fuel ≤ fuel' →
evalDictItems m fuel st keys values ⊑ʳ evalDictItems m fuel' st keys values) ∧
(∀ (m : Module) (st : FrameState) (target : Expr) (a : Addr) (i : Nat)
(body : List Stmt) (fuel' : Nat), fuel ≤ fuel' →
execForList m fuel st target a i body ⊑ʳ execForList m fuel' st target a i body) ∧
(∀ (m : Module) (st : FrameState) (a : Addr) (attr : String)
(args : List Expr) (fuel' : Nat), fuel ≤ fuel' →
execAttrCall m fuel st a attr args ⊑ʳ execAttrCall m fuel' st a attr args) ∧
-- H4 (appended LAST, the recorded discipline: existing projection
-- paths stay put): the generator stepper, the continuation walker
-- and the lazy `for` cursor.
(∀ (m : Module) (w : World) (a : Addr) (fuel' : Nat), fuel ≤ fuel' →
stepIter m fuel w a ⊑ʳ stepIter m fuel' w a) ∧
(∀ (m : Module) (st : FrameState) (k : GenCont) (fuel' : Nat), fuel ≤ fuel' →
execGen m fuel st k ⊑ʳ execGen m fuel' st k) ∧
(∀ (m : Module) (st : FrameState) (target : Expr) (a : Addr)
(body : List Stmt) (fuel' : Nat), fuel ≤ fuel' →
execForGen m fuel st target a body ⊑ʳ execForGen m fuel' st target a body) ∧
-- H6 (appended LAST, the recorded discipline): the draining
-- consumers' full drain and short-circuit drain
(∀ (m : Module) (w : World) (a : Addr) (fuel' : Nat), fuel ≤ fuel' →
drainIter m fuel w a ⊑ʳ drainIter m fuel' w a) ∧
(∀ (m : Module) (w : World) (a : Addr) (isAll : Bool) (fuel' : Nat), fuel ≤ fuel' →
anyAllIter m fuel w a isAll ⊑ʳ anyAllIter m fuel' w a isAll) ∧
-- H7 (appended LAST): the closure invocation
(∀ (m : Module) (w : World) (name : String) (params : Array Param)
(ao lo ig : Bool) (body : Array Stmt) (cap : REnv) (args : Array RVal)
(fuel' : Nat), fuel ≤ fuel' →
callClosure m fuel w name params ao lo ig body cap args ⊑ʳ
callClosure m fuel' w name params ao lo ig body cap args) := by
induction fuel with
| zero =>
-- Fuel 0 is `.timeout` everywhere, the bottom of `⊑ʳ`.
refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩
· exact fun m st e fuel' _ => Or.inl (by simp [evalExpr])
· exact fun m st es fuel' _ => Or.inl (by simp [evalExprs])
· exact fun m st op e rest fuel' _ => Or.inl (by simp [evalBoolChain])
· exact fun m st lhs ops cs fuel' _ => Or.inl (by simp [evalCompareChain])
· exact fun m st s fuel' _ => Or.inl (by simp [execStmt])
· exact fun m st ss fuel' _ => Or.inl (by simp [execStmts])
· exact fun m st test body orelse fuel' _ => Or.inl (by simp [execWhile])
· exact fun m w fname args fuel' _ => Or.inl (by simp [callIn])
· exact fun m st target xs body fuel' _ => Or.inl (by simp [execFor])
· exact fun m st keys values fuel' _ => Or.inl (by simp [evalDictItems])
· exact fun m st target a i body fuel' _ => Or.inl (by simp [execForList])
· exact fun m st a attr args fuel' _ => Or.inl (by simp [execAttrCall])
· exact fun m w a fuel' _ => Or.inl (by simp [stepIter])
· exact fun m st k fuel' _ => Or.inl (by simp [execGen])
· exact fun m st target a body fuel' _ => Or.inl (by simp [execForGen])
· exact fun m w a fuel' _ => Or.inl (by simp [drainIter])
· exact fun m w a isAll fuel' _ => Or.inl (by simp [anyAllIter])
· exact fun m w name params ao lo ig body cap args fuel' _ =>
Or.inl (by simp [callClosure])
| succ fuel ih =>
obtain ⟨ihE, ihEs, ihB, ihC, ihS, ihSs, ihW, ihCall, ihFor, ihItems, ihForL,
ihAttrC, ihStep, ihGen, ihForG, ihDrain, ihAnyAll, ihClosure⟩ := ih
refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩
-- evalExpr
· intro m st e fuel' hf
cases fuel' with
| zero => exact absurd hf (Nat.not_succ_le_zero fuel)
| succ k =>
have hk : fuel ≤ k := Nat.le_of_succ_le_succ hf
cases e with
| constant c _ => simp only [evalExpr]; exact Run.le_refl _
| namedExpr id v _ =>
simp only [evalExpr]
exact Run.le_bind (ihE m st v k hk) fun st r => Run.le_refl _
| name id _ => simp only [evalExpr]; exact Run.le_refl _
| binOp l op r _ =>
simp only [evalExpr]
exact Run.le_bind (ihE m st l k hk) fun st a =>
Run.le_bind (ihE m st r k hk) fun st b => Run.le_refl _
| unaryOp op operand _ =>
simp only [evalExpr]
exact Run.le_bind (ihE m st operand k hk) fun st v => Run.le_refl _
| boolOp op values _ =>
simp only [evalExpr]
cases values.toList with
| nil => exact Run.le_refl _
| cons e0 es => exact ihB m st op e0 es k hk
| compare l ops comparators _ =>
simp only [evalExpr]
exact Run.le_bind (ihE m st l k hk) fun st a =>
ihC m st a ops.toList comparators.toList k hk
| call cf cargs ckw cu _ =>
cases cu with
| some reason => simp only [evalExpr]; exact Run.le_refl _
| none =>
-- H6: unfold once, split on the keyword gate (the `cases`
-- substitutes the scrutinee in the goal), reduce the ite,
-- THEN fork on the callee shape
simp only [evalExpr]
cases hkw : ckw.isEmpty with
| false =>
-- ===== H6 keyword tier: positionals bind, keyword VALUES
-- bind, the pure merge lifts, the call recurses through
-- `callIn` (its own conjunct) =====
simp only [Bool.false_eq_true, if_false]
cases cf <;> try (dsimp only; exact Run.le_refl _)
case name fname _ =>
dsimp only
cases Env.lookup st.locals fname with
| some v =>
cases v <;>
exact Run.le_bind (ihEs m st cargs.toList k hk) fun st _ =>
Run.le_bind (ihEs m st (ckw.toList.map (·.2)) k hk) fun st _ =>
Run.le_refl _
| none =>
cases lookupG (moduleGlobals m).1 fname with
| some vv =>
cases vv with
| some v =>
cases v <;>
exact Run.le_bind (ihEs m st cargs.toList k hk) fun st _ =>
Run.le_bind (ihEs m st (ckw.toList.map (·.2)) k hk) fun st _ =>
Run.le_refl _
| none => exact Run.le_refl _
| none =>
cases findFunction m fname with
| some fdefn =>
try dsimp only
refine Run.le_ite (Run.le_refl _) (Run.le_ite (Run.le_refl _) ?_)
refine Run.le_bind (ihEs m st cargs.toList k hk) fun st vs => ?_
refine Run.le_bind (ihEs m st (ckw.toList.map (·.2)) k hk) fun st kvs => ?_
refine Run.le_bind (Run.le_refl _) fun st full => ?_
exact Run.le_withLocals (ihCall m st.world fname full k hk)
| none =>
try dsimp only
-- 2026-08-13: `dict(k=v, …)` sits before `sorted`
-- — a positional-argument refusal, else the kwarg
-- values bind and the allocation is fuel-free
refine Run.le_ite (Run.le_refl _) (Run.le_ite (Run.le_refl _)
(Run.le_ite
(Run.le_ite (Run.le_refl _)
(Run.le_bind (ihEs m st (ckw.toList.map (·.2)) k hk)
fun st _ => Run.le_refl _))
(Run.le_ite ?_ (Run.le_ite (Run.le_refl _)
(Run.le_ite (Run.le_refl _) (Run.le_refl _))))))
-- sorted with keywords (H6 draining tier): key= is
-- a fuel-free refusal; a stray keyword binds then
-- raises; reverse= binds, truthiness, then the
-- drain / heap sort
refine Run.le_ite (Run.le_refl _) ?_
cases ckw.toList.find? (fun kv => kv.1 != "reverse") with
| some kv =>
exact Run.le_bind (ihEs m st cargs.toList k hk) fun st _ =>
Run.le_bind (ihEs m st (ckw.toList.map (·.2)) k hk) fun st _ =>
Run.le_refl _
| none =>
refine Run.le_bind (ihEs m st cargs.toList k hk) fun st vs => ?_
refine Run.le_bind (ihEs m st (ckw.toList.map (·.2)) k hk) fun st kvs => ?_
cases vs with
| nil => exact Run.le_refl _
| cons v vtail =>
cases vtail with
| cons _ _ => exact Run.le_refl _
| nil =>
cases kvs with
| nil => exact Run.le_refl _
| cons rv rtail =>
cases rtail with
| cons _ _ => exact Run.le_refl _
| nil =>
refine Run.le_bind (Run.le_refl _) fun st desc => ?_
cases v <;> try exact Run.le_refl _
case ref a =>
dsimp only
cases Heap.get? st.world.heap a with
| none => exact Run.le_refl _
| some obj =>
cases obj <;> try exact Run.le_refl _
case generator q l c stat =>
refine Run.le_bind
(Run.le_withLocals (ihDrain m st.world a k hk))
fun st vals => ?_
exact Run.le_bind (Run.le_refl _) fun st s2 =>
Run.le_refl _
case «attribute» recv attr spa =>
dsimp only
refine Run.le_bind (ihE m st recv k hk) fun st r => ?_
cases r <;> try exact Run.le_refl _
case ref a =>
-- H7+: instance-method keywords — plan fork, then the
-- merge and the call through `callIn`'s conjunct
dsimp only
cases attrCallPlan m st.world.heap a attr <;> try exact Run.le_refl _
case instMethod qname =>
dsimp only
cases findFunction m qname with
| some fdefn =>
try dsimp only
refine Run.le_ite (Run.le_refl _) ?_
refine Run.le_bind (ihEs m st cargs.toList k hk) fun st vs => ?_
refine Run.le_bind (ihEs m st (ckw.toList.map (·.2)) k hk) fun st kvs => ?_
refine Run.le_bind (Run.le_refl _) fun st full => ?_
exact Run.le_withLocals (ihCall m st.world qname full k hk)
| none => exact Run.le_refl _
case ntuple tn fs xs =>
dsimp only
cases ntupleCallPlan m tn fs attr <;> try exact Run.le_refl _
case instMethod qname =>
dsimp only
cases findFunction m qname with
| some fdefn =>
try dsimp only
refine Run.le_ite (Run.le_refl _) ?_
refine Run.le_bind (ihEs m st cargs.toList k hk) fun st vs => ?_
refine Run.le_bind (ihEs m st (ckw.toList.map (·.2)) k hk) fun st kvs => ?_
refine Run.le_bind (Run.le_refl _) fun st full => ?_
exact Run.le_withLocals (ihCall m st.world qname full k hk)
| none => exact Run.le_refl _
| true =>
simp only [eq_self_iff_true, if_true]
cases cf <;> try (dsimp only; exact Run.le_refl _)
case «attribute» recv attr spa =>
-- receiver-first dispatch (H3): the receiver evaluates, then
-- `execAttrCall` forks on the pure plan (its own conjunct);
-- an ntuple VALUE receiver (H5) forks on `ntupleCallPlan` —
-- only the method arm recurses (args + `callIn`).
-- Pass 6: the TRACE-CLOCK fork comes first (`isClockCall`
-- is fuel-free, so both sides split identically; the pop
-- and its refusals are fuel-free too).
dsimp only
split
· exact Run.le_refl _
refine Run.le_bind (ihE m st recv k hk) fun st r => ?_
cases r <;>
first
| exact Run.le_refl _
| exact ihAttrC m st _ attr cargs.toList k hk
| skip
case ntuple tn fs xs =>
-- iota-reduce the receiver matcher, then fork on the plan
dsimp only
cases ntupleCallPlan m tn fs attr <;> try exact Run.le_refl _
case instMethod qname =>
exact Run.le_bind (ihEs m st cargs.toList k hk) fun st vs =>
Run.le_withLocals
(ihCall m st.world qname ((RVal.ntuple tn fs xs :: vs).toArray) k hk)
case str sv =>
-- str METHOD dispatch (H5 strings): fork on the pure plan;
-- every in-tier arm is args + a fuel-independent worker
dsimp only
cases strCallPlan attr <;>
first
| exact Run.le_refl _
| exact Run.le_bind (ihEs m st cargs.toList k hk)
fun st vs => Run.le_refl _
case name fname _ =>
dsimp only
cases Env.lookup st.locals fname with
| some v =>
cases v <;>
first
| exact Run.le_refl _
| exact Run.le_bind (ihEs m st cargs.toList k hk) fun _ _ =>
Run.le_refl _
| skip
case ref a =>
-- H7: the guarded closure call
refine Run.le_bind (ihEs m st cargs.toList k hk) fun st vs => ?_
refine Run.le_ite (Run.le_refl _) ?_
cases Heap.get? st.world.heap a with
| none => exact Run.le_refl _
| some o =>
cases o <;> try exact Run.le_refl _
case closure nm ps ao lo hg ig bd cap =>
exact Run.le_bind (Run.le_refl _) fun st cap' =>
Run.le_withLocals
(ihClosure m st.world
nm ps ao lo ig bd cap' vs.toArray k hk)
| none =>
-- module globals (G1) → module function → builtins →
-- NameError/unsupported (the globals and the final fork are
-- fuel-independent, hence `le_refl`)
cases lookupG (moduleGlobals m).1 fname with
| some vv =>
cases vv with
| some v =>
cases v <;>
exact Run.le_bind (ihEs m st cargs.toList k hk) fun _ _ =>
Run.le_refl _
| none =>
-- pass 3: the poisoned-arm live view — value/exn
-- arms bind then decide; a closure ref recurses
-- through `callClosure` (its own conjunct)
cases Env.lookup st.world.globals fname with
| none => exact Run.le_refl _
| some v =>
cases v <;>
first
| exact Run.le_bind (ihEs m st cargs.toList k hk)
fun st _ => Run.le_refl _
| skip
case ref a =>
refine Run.le_bind (ihEs m st cargs.toList k hk) fun st vs => ?_
refine Run.le_ite (Run.le_refl _) ?_
cases Heap.get? st.world.heap a with
| none => exact Run.le_refl _
| some obj =>
cases obj <;> try exact Run.le_refl _
case closure nm ps ao lo hg ig bd cap =>
exact Run.le_bind (Run.le_refl _) fun st cap' =>
Run.le_withLocals
(ihClosure m st.world
nm ps ao lo ig bd cap' vs.toArray k hk)
| none =>
-- findFunction (def/class collision → call) → class
-- instantiation (guards, args, `__init__` through
-- `callIn`) → len → sorted → max → min → abs → int →
-- NameError/unsupported (each builtin: bind args, result
-- fuel-independent)
have hb : ∀ {β : Type} (g : FrameState → List RVal → Run FrameState β),
(evalExprs m fuel st cargs.toList).bind g ⊑ʳ
(evalExprs m k st cargs.toList).bind g :=
fun g => Run.le_bind (ihEs m st cargs.toList k hk)
fun st vs => Run.le_refl _
refine Run.le_ite
(Run.le_ite (Run.le_refl _)
(Run.le_bind (ihEs m st cargs.toList k hk) fun st vs =>
Run.le_withLocals (ihCall m st.world fname vs.toArray k hk)))
?_
cases findClass m fname with
| some p =>
obtain ⟨ci, c⟩ := p
-- namedtuple-collision guard, then the exceptions
-- tier's isExc guard (both fuel-independent)
refine Run.le_ite (Run.le_refl _) (Run.le_ite (Run.le_refl _) ?_)
cases c.ntBase with
| some nt =>
-- value-like subclass construction (H5): guards,
-- then args, then a fuel-independent value/arity fork
refine Run.le_ite (Run.le_refl _) (Run.le_ite (Run.le_refl _)
(Run.le_ite (Run.le_refl _) ?_))
exact Run.le_bind (ihEs m st cargs.toList k hk)
fun st vs => Run.le_refl _
| none =>
refine Run.le_ite (Run.le_refl _) (Run.le_ite (Run.le_refl _) ?_)
refine Run.le_bind (ihEs m st cargs.toList k hk) fun st vs => ?_
refine Run.le_ite ?_ (Run.le_refl _)
refine Run.le_bind (Run.le_withLocals
(ihCall m _ (fname ++ ".__init__") ((RVal.ref st.world.heap.size :: vs).toArray) k hk))
fun st'' r => ?_
cases r <;> exact Run.le_refl _
| none =>
-- namedtuple construction binds the arguments, then a
-- fuel-independent value/arity fork; the builtin chain
-- is as before
cases findNamedTuple m fname with
| some nt => exact hb _
| none =>
-- len, sorted (drains a generator), max/min (the
-- guarded drain), any/all (the short-circuit
-- drain), abs, int, enumerate, count (both
-- ALLOCATE an iterator object), NEXT (binds the
-- args, then steps the generator), ord, chr
-- len → sorted → max → min → any/all → set → abs →
-- int → sum → tuple → list → dict → range →
-- enumerate → count → next → ord → chr → tail
refine Run.le_ite (hb _) -- len
(Run.le_ite ?_ -- sorted
(Run.le_ite ?_ -- max
(Run.le_ite ?_ -- min
(Run.le_ite ?_ -- any/all
(Run.le_ite ?_ -- set
(Run.le_ite (hb _) -- abs
(Run.le_ite (hb _) -- int
(Run.le_ite ?_ -- sum
(Run.le_ite ?_ -- tuple
(Run.le_ite ?_ -- list
(Run.le_ite (hb _) -- dict
(Run.le_ite (hb _) -- range
(Run.le_ite (hb _) -- enumerate
(Run.le_ite (hb _) -- count
(Run.le_ite ?_ -- next
(Run.le_ite (hb _) -- ord
(Run.le_ite (hb _) -- chr
?_)))))))))))))))))
-- sorted
· refine Run.le_bind (ihEs m st cargs.toList k hk) fun st vs => ?_
cases vs with
| nil => exact Run.le_refl _
| cons v vtail =>
cases vtail with
| cons _ _ => exact Run.le_refl _
| nil =>
dsimp only
cases v <;> try exact Run.le_refl _
case ref a =>
dsimp only
cases Heap.get? st.world.heap a with
| none => exact Run.le_refl _
| some obj =>
cases obj <;> try exact Run.le_refl _
case generator q l c stat =>
refine Run.le_bind
(Run.le_withLocals (ihDrain m st.world a k hk))
fun st vals => ?_
exact Run.le_bind (Run.le_refl _) fun st s2 =>
Run.le_refl _
-- max
· refine Run.le_bind (ihEs m st cargs.toList k hk) fun st vs => ?_
cases vs with
| nil => exact Run.le_refl _
| cons v vtail =>
cases v <;> try exact Run.le_refl _
case ref a =>
cases vtail with
| cons _ _ => exact Run.le_refl _
| nil =>
dsimp only
cases Heap.get? st.world.heap a with
| none => exact Run.le_refl _
| some obj =>
cases obj <;> try exact Run.le_refl _
case generator q l c stat =>
refine Run.le_ite (Run.le_refl _) ?_
refine Run.le_bind
(Run.le_withLocals (ihDrain m st.world a k hk))
fun st vals => ?_
exact Run.le_refl _
-- min
· refine Run.le_bind (ihEs m st cargs.toList k hk) fun st vs => ?_
cases vs with
| nil => exact Run.le_refl _
| cons v vtail =>
cases v <;> try exact Run.le_refl _
case ref a =>
cases vtail with
| cons _ _ => exact Run.le_refl _
| nil =>
dsimp only
cases Heap.get? st.world.heap a with
| none => exact Run.le_refl _
| some obj =>
cases obj <;> try exact Run.le_refl _
case generator q l c stat =>
refine Run.le_ite (Run.le_refl _) ?_
refine Run.le_bind
(Run.le_withLocals (ihDrain m st.world a k hk))
fun st vals => ?_
exact Run.le_refl _
-- any/all
· refine Run.le_bind (ihEs m st cargs.toList k hk) fun st vs => ?_
cases vs with
| nil => exact Run.le_refl _
| cons v vtail =>
cases vtail with
| cons _ _ => exact Run.le_refl _
| nil =>
cases v <;> try exact Run.le_refl _
case ref a =>
dsimp only
cases Heap.get? st.world.heap a with
| none => exact Run.le_refl _
| some obj =>
cases obj <;> try exact Run.le_refl _
case generator q l c stat =>
refine Run.le_bind
(Run.le_withLocals
(ihAnyAll m st.world a (fname == "all") k hk))
fun st b => ?_
exact Run.le_refl _
-- set (H7+): binds, then per-receiver dedup
-- (fuel-threaded scans) or the generator drain
· refine Run.le_bind (ihEs m st cargs.toList k hk) fun st vs => ?_
cases vs with
| nil => exact Run.le_refl _
| cons v vtail =>
cases vtail with
| cons _ _ => exact Run.le_refl _
| nil =>
dsimp only
cases v <;>
first
| exact Run.le_refl _
| exact Run.le_bind (Run.le_liftRes (setDedup_mono hk _))
fun st es => Run.le_refl _
| skip
case rangeV lo hi step =>
exact Run.le_bind (Run.le_refl _) fun st xs =>
Run.le_bind (Run.le_liftRes (setDedup_mono hk _))
fun st es => Run.le_refl _
case ref a =>
dsimp only
cases Heap.get? st.world.heap a with
| none => exact Run.le_refl _
| some obj =>
cases obj <;>
first
| exact Run.le_refl _
| exact Run.le_bind (Run.le_liftRes (setDedup_mono hk _))
fun st es => Run.le_refl _
| skip
case generator q l c stat =>
refine Run.le_bind
(Run.le_withLocals (ihDrain m st.world a k hk))
fun st vals => ?_
exact Run.le_bind (Run.le_liftRes (setDedup_mono hk _))
fun st es => Run.le_refl _
-- sum (pass 3): arity fork, the str-start fork,
-- then the receiver — every value arm is the pure
-- fold; the single-generator arm drains behind the
-- `moduleGenFree` guard, like max/min
· refine Run.le_bind (ihEs m st cargs.toList k hk) fun st vs => ?_
cases sumArgs vs with
| none => exact Run.le_refl _
| some pr =>
obtain ⟨v, start⟩ := pr
dsimp only
cases start <;> try exact Run.le_refl _
all_goals
cases v <;> try exact Run.le_refl _
all_goals
case ref a =>
dsimp only
cases Heap.get? st.world.heap a with
| none => exact Run.le_refl _
| some obj =>
cases obj <;> try exact Run.le_refl _
case generator q l c stat =>
refine Run.le_ite (Run.le_refl _) ?_
exact Run.le_bind
(Run.le_withLocals (ihDrain m st.world a k hk))
fun st vals => Run.le_refl _
-- tuple (pass 3): snapshot constructors are
-- fuel-free; the generator arm drains, guarded
· refine Run.le_bind (ihEs m st cargs.toList k hk) fun st vs => ?_
cases vs with
| nil => exact Run.le_refl _
| cons v vtail =>
cases vtail with
| cons _ _ => exact Run.le_refl _
| nil =>
dsimp only
cases v <;> try exact Run.le_refl _
case ref a =>
dsimp only
cases Heap.get? st.world.heap a with
| none => exact Run.le_refl _
| some obj =>
cases obj <;> try exact Run.le_refl _
case generator q l c stat =>