Repository navigation
Expand file tree
/
Copy pathParserWfaProofs.lean
More file actions
1722 lines (1653 loc) · 78.5 KB
/
Copy pathParserWfaProofs.lean
File metadata and controls
1722 lines (1653 loc) · 78.5 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
import L4YAML.Spec.Grammar
import L4YAML.Parser.Composition
import L4YAML.Proofs.Parser.ParserGrammableBase
import L4YAML.Proofs.Parser.ParserNodeProofs
import L4YAML.Proofs.Parser.ParserWellBehaved
import L4YAML.Algebra.Value
/-
Copyright (c) 2026. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
-/
set_option autoImplicit false
/-!
# WellFormedAnchors Preservation through parseNode
Proves that `parseNode` preserves `WellFormedAnchors`:
if the input anchors satisfy WFA, then the output anchors do too.
Architecture: strong induction on fuel, same pattern as AG and AAR proofs.
Sub-parsers only modify anchors through recursive `parseNode` calls, so
WFA propagates through each sub-parser by the induction hypothesis.
The only non-trivial case is `applyNodeFinalization`: when an anchor is
registered, the cleaned value is universally grammable by
`adaptForFlowContext_grammable_forall ∘ compose_value_grammable`.
-/
namespace L4YAML.Proofs.ParserWfaProofs
open L4YAML
open L4YAML.Grammar
open L4YAML.TokenParser
open L4YAML.Proofs.ParserGrammable
open L4YAML.Proofs.ParserWellBehaved
open L4YAML.Proofs.ScannerPlainScalarValid
open L4YAML.Proofs.Composition
open L4YAML.Algebra.Value
open L4YAML.Proofs.ParserNodeProofs
-- Custom tactic: unfold all `*.loop*` constants in a hypothesis.
open Lean Lean.Meta Lean.Elab.Tactic in
elab "unfold_loop_at" h:ident : tactic => do
let mvarId ← getMainGoal
mvarId.withContext do
let fvarId ← getFVarId h
let ldecl ← fvarId.getDecl
let ty := ldecl.type
let namesRef ← IO.mkRef (∅ : NameSet)
let _ ← Lean.Meta.transform ty (pre := fun e => do
let fn := e.getAppFn
if fn.isConst then
let name := fn.constName!
let leaf := if name.isStr then name.getString! else ""
let parentLeaf := if name.getPrefix.isStr then name.getPrefix.getString! else ""
if leaf == "loop" || parentLeaf == "loop" then
namesRef.modify (·.insert name)
return .continue)
let names ← namesRef.get
if names.isEmpty then throwError "no loop constants found"
let mut currentTy := ty
for name in names do
let result ← Lean.Meta.unfold currentTy name
currentTy := result.expr
if ty == currentTy then throwError "no change"
let mvarId ← mvarId.replaceLocalDeclDefEq fvarId currentTy
replaceMainGoal [mvarId]
-- ============================================================
-- §1 Definitions and helpers
-- ============================================================
-- The WFA induction hypothesis.
def ParseNodeWFA (tokens : Array (Positioned YamlToken)) (n : Nat) : Prop :=
∀ (ps : ParseState) (m : Nat) (val : YamlValue) (ps' : ParseState),
m ≤ n →
ps.tokens = tokens →
parseNode ps m = .ok (val, ps') →
WellFormedAnchors ps.anchors →
WellFormedAnchors ps'.anchors
-- Empty anchors are trivially well-formed.
lemma WFA_empty : WellFormedAnchors (#[] : Array (String × YamlValue)) := by
intro name val h_find _
simp [Array.findSome?] at h_find
-- WellFormedAnchors is preserved when pushing a universally-grammable entry.
lemma WFA_of_push (anchors : Array (String × YamlValue))
(entry : String × YamlValue)
(h_wfa : WellFormedAnchors anchors)
(h_new : ∀ inFlow, Grammable entry.2.stripAnchors inFlow) :
WellFormedAnchors (anchors.push entry) := by
intro name val h_find inFlow
rw [Array.findSome?_push] at h_find
-- h_find : (anchors.findSome? f).or (f entry) = some val
generalize hg : anchors.findSome? (fun x =>
match x with | (n, v) => if n == name then some v else none) = found at h_find
cases found with
| some v' =>
-- found in original array
dsimp only [Option.or] at h_find
injection h_find with h_eq; subst h_eq
exact h_wfa name v' hg inFlow
| none =>
-- must come from entry
dsimp only [Option.or] at h_find
split at h_find
· injection h_find with h_eq; subst h_eq; exact h_new inFlow
· simp at h_find
-- ============================================================
-- §2 applyNodeFinalization preserves WFA
-- ============================================================
-- addAnchor preserves WFA when the value has a Grammable composition.
lemma addAnchor_wfa (ps : ParseState) (name : String) (val : YamlValue)
(h_wfa : WellFormedAnchors ps.anchors)
(h_gram : ∀ inFlow, Grammable ((val.resolveAliases ps.anchors).stripAnchors.adaptForFlowContext) inFlow) :
WellFormedAnchors (ps.addAnchor name val).anchors := by
unfold ParseState.addAnchor
dsimp only []
apply WFA_of_push
· exact h_wfa
· intro inFlow
have h_eq : ((val.resolveAliases ps.anchors).stripAnchors.adaptForFlowContext).stripAnchors =
(val.resolveAliases ps.anchors).stripAnchors.adaptForFlowContext :=
stripAnchors_of_cleaned (val.resolveAliases ps.anchors)
rw [h_eq]
exact h_gram inFlow
-- Helper: compose + adaptForFlowContext gives universally-grammable values.
lemma cleaned_grammable_forall
(val : YamlValue) (ps : ParseState)
(h_scannable : Scannable val false)
(h_aar : AllAliasesResolve val ps.anchors)
(h_wfa : WellFormedAnchors ps.anchors) :
∀ inFlow, Grammable ((val.resolveAliases ps.anchors).stripAnchors.adaptForFlowContext) inFlow := by
have h_g := compose_value_grammable val ps.anchors false h_scannable h_aar h_wfa
exact adaptForFlowContext_grammable_forall _ _ h_g
-- applyNodeFinalization preserves WFA when Scannable + AAR + WFA hold.
lemma applyNodeFinalization_wfa
(val : YamlValue) (ps : ParseState) (props : NodeProperties) (nodeStartPos : YamlPos)
(h_wfa : WellFormedAnchors ps.anchors)
(h_scannable : Scannable val false)
(h_aar : AllAliasesResolve val ps.anchors) :
WellFormedAnchors (applyNodeFinalization val ps props nodeStartPos).2.anchors := by
rcases props with ⟨anchor, tag, dup⟩
cases anchor with
| none =>
simp only [applyNodeFinalization]
split <;> exact h_wfa
| some name =>
cases val with
| scalar s =>
simp only [applyNodeFinalization]
split <;> exact addAnchor_wfa ps name _ h_wfa
(cleaned_grammable_forall _ ps h_scannable h_aar h_wfa)
| alias a =>
simp only [applyNodeFinalization]
split <;> exact addAnchor_wfa ps name _ h_wfa
(cleaned_grammable_forall _ ps h_scannable h_aar h_wfa)
| sequence style items otag oanchor =>
cases otag <;> cases oanchor <;> simp only [applyNodeFinalization] <;> split
all_goals (first
| exact addAnchor_wfa ps name _ h_wfa
(cleaned_grammable_forall _ ps h_scannable h_aar h_wfa)
| (exact addAnchor_wfa ps name _ h_wfa
(cleaned_grammable_forall _ ps
(.sequence _ _ _ _ _
(by cases h_scannable with | sequence _ _ _ _ _ h => exact h))
(aar_retag_sequence ps.anchors tag (some name) h_aar) h_wfa)))
| mapping style pairs otag oanchor =>
cases otag <;> cases oanchor <;> simp only [applyNodeFinalization] <;> split
all_goals (first
| exact addAnchor_wfa ps name _ h_wfa
(cleaned_grammable_forall _ ps h_scannable h_aar h_wfa)
| (exact addAnchor_wfa ps name _ h_wfa
(cleaned_grammable_forall _ ps
(.mapping _ _ _ _ _
(by cases h_scannable with | mapping _ _ _ _ _ hk _ => exact hk)
(by cases h_scannable with | mapping _ _ _ _ _ _ hv => exact hv))
(aar_retag_mapping ps.anchors tag (some name) h_aar) h_wfa)))
-- ============================================================
-- §3 parseNodeContent and sub-parser WFA
-- ============================================================
-- All sub-parsers only modify anchors through recursive parseNode calls.
-- Therefore WFA propagation through each sub-parser follows mechanically
-- from the induction hypothesis (for parseNode) and token/anchor preservation
-- lemmas (from ParseNodeWB and advance/tryConsume).
-- Helper: advance preserves anchors and tokens.
lemma advance_anchors (ps : ParseState) :
ps.advance.anchors = ps.anchors := by simp [ParseState.advance]
lemma advance_tokens (ps : ParseState) :
ps.advance.tokens = ps.tokens := by simp [ParseState.advance]
-- Helper: tryConsume preserves anchors and tokens.
lemma tc_anchors (ps : ParseState) (tok : YamlToken) :
(ps.tryConsume tok).2.anchors = ps.anchors := by
unfold ParseState.tryConsume; split <;> (try split) <;> simp [ParseState.advance]
lemma tc_tokens (ps : ParseState) (tok : YamlToken) :
(ps.tryConsume tok).2.tokens = ps.tokens := by
unfold ParseState.tryConsume; split <;> (try split) <;> simp [ParseState.advance]
-- Get tokens preservation from ParseNodeWB.
lemma parseNode_tokens_of_wb
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_wb : ParseNodeWB tokens n)
(ps : ParseState) (fuel : Nat) (h_fuel : fuel ≤ n)
(h_tok : ps.tokens = tokens)
(val : YamlValue) (ps' : ParseState)
(h_ok : parseNode ps fuel = .ok (val, ps')) :
ps'.tokens = tokens :=
(h_wb ps fuel val ps' h_fuel h_tok h_ok).2.2.2
-- parseExplicitKey: emptyNode or parseNode
lemma parseExplicitKey_wfa
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_ih : ParseNodeWFA tokens n)
(ps : ParseState) (fuel : Nat) (h_fuel : fuel ≤ n)
(h_tok : ps.tokens = tokens)
(val : YamlValue) (ps' : ParseState)
(h_ok : parseExplicitKey ps fuel = .ok (val, ps'))
(h_wfa : WellFormedAnchors ps.anchors) :
WellFormedAnchors ps'.anchors := by
unfold parseExplicitKey at h_ok
split at h_ok
all_goals (first
| (simp only [Except.ok.injEq] at h_ok; obtain ⟨_, rfl⟩ := Prod.mk.inj h_ok; exact h_wfa)
| exact h_ih ps fuel _ _ h_fuel h_tok h_ok h_wfa)
-- parseBlockSequenceLoop
lemma parseBlockSequenceLoop_wfa
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_ih : ParseNodeWFA tokens n) (h_wb : ParseNodeWB tokens n)
(ps : ParseState) (fuel : Nat) (items : Array YamlValue)
(h_fuel : fuel ≤ n + 1) (h_tok : ps.tokens = tokens)
(result : Array YamlValue) (ps' : ParseState)
(h_ok : parseBlockSequenceLoop ps fuel items = .ok (result, ps'))
(h_wfa : WellFormedAnchors ps.anchors) :
WellFormedAnchors ps'.anchors := by
induction fuel generalizing ps items with
| zero =>
unfold parseBlockSequenceLoop at h_ok
simp only [Except.ok.injEq] at h_ok
obtain ⟨_, rfl⟩ := Prod.mk.inj h_ok; exact h_wfa
| succ k ih_fuel =>
unfold parseBlockSequenceLoop at h_ok
simp only [bind, Except.bind] at h_ok
split at h_ok
· -- peek? = some .blockEntry
have h_wfa_adv : WellFormedAnchors ps.advance.anchors :=
advance_anchors ps ▸ h_wfa
have h_tok_adv : ps.advance.tokens = tokens :=
(advance_tokens ps).trans h_tok
split at h_ok
-- empty entry cases: recurse
all_goals (first
| exact ih_fuel ps.advance (items.push emptyNode) (by omega)
h_tok_adv h_ok h_wfa_adv
| skip)
-- non-empty: parseNode then recurse
· generalize h_ps_a : ps.advance = ps_a at h_ok
have h_tok_a : ps_a.tokens = tokens := by rw [← h_ps_a]; exact h_tok_adv
have h_wfa_a : WellFormedAnchors ps_a.anchors := by rw [← h_ps_a]; exact h_wfa_adv
generalize h_ps_wf : { ps_a with currentPath := _ } = ps_wf at h_ok
split at h_ok
· simp at h_ok
· rename_i pn_res heq_pn
obtain ⟨val_n, ps_n⟩ := pn_res
dsimp only [] at h_ok
have h_tok_wf : ps_wf.tokens = tokens := by
rw [← h_ps_wf]; exact h_tok_a
have h_wfa_wf : WellFormedAnchors ps_wf.anchors := by
rw [← h_ps_wf]; exact h_wfa_a
have h_wfa_n := h_ih ps_wf k _ _ (by omega) h_tok_wf heq_pn h_wfa_wf
have h_tok_n := parseNode_tokens_of_wb h_wb ps_wf k (by omega) h_tok_wf _ _ heq_pn
generalize h_ps_r : { ps_n with currentPath := _ } = ps_r at h_ok
have h_wfa_r : WellFormedAnchors ps_r.anchors := by
rw [← h_ps_r]; exact h_wfa_n
have h_tok_r : ps_r.tokens = tokens := by
rw [← h_ps_r]; exact h_tok_n
exact ih_fuel ps_r _ (by omega) h_tok_r h_ok h_wfa_r
· -- not blockEntry
simp only [Except.ok.injEq] at h_ok
obtain ⟨_, rfl⟩ := Prod.mk.inj h_ok; exact h_wfa
-- parseBlockSequence
lemma parseBlockSequence_wfa
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_ih : ParseNodeWFA tokens n) (h_wb : ParseNodeWB tokens n)
(ps : ParseState) (fuel : Nat) (h_fuel : fuel ≤ n + 1)
(h_tok : ps.tokens = tokens)
(val : YamlValue) (ps' : ParseState)
(h_ok : parseBlockSequence ps fuel = .ok (val, ps'))
(h_wfa : WellFormedAnchors ps.anchors) :
WellFormedAnchors ps'.anchors := by
unfold parseBlockSequence at h_ok
simp only [bind, Except.bind] at h_ok
split at h_ok
· simp at h_ok
· rename_i k
split at h_ok
· simp at h_ok
· rename_i loop_res heq_loop
obtain ⟨items, ps_loop⟩ := loop_res
dsimp only [] at h_ok
have h_wfa_loop := parseBlockSequenceLoop_wfa h_ih h_wb
ps.advance k #[] (by omega) ((advance_tokens ps).trans h_tok)
items ps_loop heq_loop (advance_anchors ps ▸ h_wfa)
split at h_ok <;> (
simp only [Except.ok.injEq] at h_ok
obtain ⟨_, rfl⟩ := Prod.mk.inj h_ok)
· rw [advance_anchors]; exact h_wfa_loop
· exact h_wfa_loop
-- parseImplicitBlockSequenceLoop
lemma parseImplicitBlockSequenceLoop_wfa
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_ih : ParseNodeWFA tokens n) (h_wb : ParseNodeWB tokens n)
(ps : ParseState) (fuel : Nat) (items : Array YamlValue)
(h_fuel : fuel ≤ n + 1) (h_tok : ps.tokens = tokens)
(result : Array YamlValue) (ps' : ParseState)
(h_ok : parseImplicitBlockSequenceLoop ps fuel items = .ok (result, ps'))
(h_wfa : WellFormedAnchors ps.anchors) :
WellFormedAnchors ps'.anchors := by
induction fuel generalizing ps items with
| zero =>
unfold parseImplicitBlockSequenceLoop at h_ok
simp only [Except.ok.injEq] at h_ok
obtain ⟨_, rfl⟩ := Prod.mk.inj h_ok; exact h_wfa
| succ k ih_fuel =>
unfold parseImplicitBlockSequenceLoop at h_ok
simp only [bind, Except.bind] at h_ok
split at h_ok
· have h_wfa_adv : WellFormedAnchors ps.advance.anchors :=
advance_anchors ps ▸ h_wfa
have h_tok_adv : ps.advance.tokens = tokens :=
(advance_tokens ps).trans h_tok
split at h_ok
all_goals (first
| exact ih_fuel ps.advance (items.push emptyNode) (by omega)
h_tok_adv h_ok h_wfa_adv
| skip)
· generalize h_ps_a : ps.advance = ps_a at h_ok
have h_tok_a : ps_a.tokens = tokens := by rw [← h_ps_a]; exact h_tok_adv
have h_wfa_a : WellFormedAnchors ps_a.anchors := by rw [← h_ps_a]; exact h_wfa_adv
generalize h_ps_wf : { ps_a with currentPath := _ } = ps_wf at h_ok
split at h_ok
· simp at h_ok
· rename_i pn_res heq_pn
obtain ⟨val_n, ps_n⟩ := pn_res
dsimp only [] at h_ok
have h_tok_wf : ps_wf.tokens = tokens := by
rw [← h_ps_wf]; exact h_tok_a
have h_wfa_wf : WellFormedAnchors ps_wf.anchors := by
rw [← h_ps_wf]; exact h_wfa_a
have h_wfa_n := h_ih ps_wf k _ _ (by omega) h_tok_wf heq_pn h_wfa_wf
have h_tok_n := parseNode_tokens_of_wb h_wb ps_wf k (by omega) h_tok_wf _ _ heq_pn
generalize h_ps_r : { ps_n with currentPath := _ } = ps_r at h_ok
have h_wfa_r : WellFormedAnchors ps_r.anchors := by
rw [← h_ps_r]; exact h_wfa_n
have h_tok_r : ps_r.tokens = tokens := by
rw [← h_ps_r]; exact h_tok_n
exact ih_fuel ps_r _ (by omega) h_tok_r h_ok h_wfa_r
· simp only [Except.ok.injEq] at h_ok
obtain ⟨_, rfl⟩ := Prod.mk.inj h_ok; exact h_wfa
-- parseImplicitBlockSequence
lemma parseImplicitBlockSequence_wfa
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_ih : ParseNodeWFA tokens n) (h_wb : ParseNodeWB tokens n)
(ps : ParseState) (fuel : Nat) (h_fuel : fuel ≤ n + 1)
(h_tok : ps.tokens = tokens)
(val : YamlValue) (ps' : ParseState)
(h_ok : parseImplicitBlockSequence ps fuel = .ok (val, ps'))
(h_wfa : WellFormedAnchors ps.anchors) :
WellFormedAnchors ps'.anchors := by
unfold parseImplicitBlockSequence at h_ok
simp only [bind, Except.bind] at h_ok
split at h_ok
· simp at h_ok
· rename_i k
split at h_ok
· simp at h_ok
· rename_i loop_res heq_loop
obtain ⟨items, ps_loop⟩ := loop_res
dsimp only [] at h_ok
simp only [Except.ok.injEq] at h_ok
obtain ⟨_, rfl⟩ := Prod.mk.inj h_ok
exact parseImplicitBlockSequenceLoop_wfa h_ih h_wb
ps k #[] (by omega) h_tok items ps_loop heq_loop h_wfa
-- parseBlockMappingEntryValue
set_option maxHeartbeats 400000 in
lemma parseBlockMappingEntryValue_wfa
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_ih : ParseNodeWFA tokens n)
(ps : ParseState) (fuel : Nat) (h_fuel : fuel ≤ n)
(h_tok : ps.tokens = tokens)
(keyHasContent : Bool) (keyLine keyCol : Nat)
(val : YamlValue) (ps' : ParseState)
(h_ok : parseBlockMappingEntryValue ps fuel keyHasContent keyLine keyCol = .ok (val, ps'))
(h_wfa : WellFormedAnchors ps.anchors) :
WellFormedAnchors ps'.anchors := by
unfold parseBlockMappingEntryValue at h_ok
simp only [bind, Except.bind, pure, Except.pure] at h_ok
have h_tc_wfa : WellFormedAnchors (ps.tryConsume .value).2.anchors := by
rw [tc_anchors]; exact h_wfa
have h_tc_tok : (ps.tryConsume .value).2.tokens = tokens := by
rw [tc_tokens]; exact h_tok
split at h_ok
· -- consumed = true
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
-- emptyNode goals
all_goals (try (obtain ⟨rfl, rfl⟩ := h_ok; exact h_tc_wfa))
-- parseNode goals
all_goals exact h_ih _ fuel _ _ h_fuel h_tc_tok h_ok h_tc_wfa
· -- consumed = false: retroactive-key skip (V9D5) or empty value.
-- Anchors/tokens are preserved through the skipped `key` and consumed `:`.
have h_C_wfa : WellFormedAnchors
(((ps.tryConsume .value).2.tryConsume .key).2.tryConsume .value).2.anchors := by
rw [tc_anchors, tc_anchors]; exact h_tc_wfa
have h_C_tok : (((ps.tryConsume .value).2.tryConsume .key).2.tryConsume .value).2.tokens
= tokens := by rw [tc_tokens, tc_tokens]; exact h_tc_tok
split at h_ok -- outer: peek?, peekNext?
all_goals (try (split at h_ok)) -- inner: peek? dispatch in the retroactive arm
all_goals (first
| (obtain ⟨rfl, rfl⟩ := h_ok; exact h_tc_wfa)
| (obtain ⟨rfl, rfl⟩ := h_ok; exact h_C_wfa)
| exact h_ih _ fuel _ _ h_fuel h_C_tok h_ok h_C_wfa)
-- handleBlockMappingValueEntry
lemma handleBlockMappingValueEntry_wfa
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_ih : ParseNodeWFA tokens n)
(ps : ParseState) (fuel : Nat) (h_fuel : fuel ≤ n)
(h_tok : ps.tokens = tokens)
(pairIdx : Nat) (val : YamlValue) (ps' : ParseState)
(h_ok : handleBlockMappingValueEntry ps fuel pairIdx = .ok (val, ps'))
(h_wfa : WellFormedAnchors ps.anchors) :
WellFormedAnchors ps'.anchors := by
unfold handleBlockMappingValueEntry at h_ok
simp only [bind, Except.bind] at h_ok
have h_wfa_adv : WellFormedAnchors ps.advance.anchors :=
advance_anchors ps ▸ h_wfa
have h_tok_adv : ps.advance.tokens = tokens :=
(advance_tokens ps).trans h_tok
-- Split on the inner match (peek? dispatch)
split at h_ok
-- emptyNode cases (some .key, some .blockEnd, none)
all_goals (try (
simp only [Except.ok.injEq] at h_ok
obtain ⟨_, rfl⟩ := Prod.mk.inj h_ok
dsimp only []
exact h_wfa_adv))
-- Remaining: parseNode
· split at h_ok
· simp at h_ok
· rename_i pn_res heq_pn
obtain ⟨v, ps_n⟩ := pn_res
dsimp only [] at h_ok
simp only [Except.ok.injEq] at h_ok
obtain ⟨_, rfl⟩ := Prod.mk.inj h_ok
dsimp only []
exact h_ih _ fuel _ _ h_fuel
(by dsimp only []; exact h_tok_adv) heq_pn
(by dsimp only []; exact h_wfa_adv)
-- ── §3a Tokens preservation for compound sub-parsers ──────────────
lemma parseExplicitKey_tok
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_wb : ParseNodeWB tokens n)
(ps : ParseState) (fuel : Nat) (h_fuel : fuel ≤ n)
(h_tok : ps.tokens = tokens)
(val : YamlValue) (ps' : ParseState)
(h_ok : parseExplicitKey ps fuel = .ok (val, ps')) :
ps'.tokens = tokens := by
unfold parseExplicitKey at h_ok; split at h_ok
all_goals (first
| (simp only [Except.ok.injEq] at h_ok; obtain ⟨_, rfl⟩ := Prod.mk.inj h_ok; exact h_tok)
| exact parseNode_tokens_of_wb h_wb ps fuel h_fuel h_tok _ _ h_ok)
set_option maxHeartbeats 400000 in
lemma parseBlockMappingEntryValue_tok
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_wb : ParseNodeWB tokens n)
(ps : ParseState) (fuel : Nat) (h_fuel : fuel ≤ n)
(h_tok : ps.tokens = tokens)
(keyHasContent : Bool) (keyLine keyCol : Nat)
(val : YamlValue) (ps' : ParseState)
(h_ok : parseBlockMappingEntryValue ps fuel keyHasContent keyLine keyCol = .ok (val, ps')) :
ps'.tokens = tokens := by
unfold parseBlockMappingEntryValue at h_ok
simp only [bind, Except.bind, pure, Except.pure] at h_ok
have h_tc_tok : (ps.tryConsume .value).2.tokens = tokens := (tc_tokens _ _).trans h_tok
split at h_ok
· all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (try (obtain ⟨rfl, rfl⟩ := h_ok; exact h_tc_tok))
all_goals exact parseNode_tokens_of_wb h_wb _ fuel h_fuel h_tc_tok _ _ h_ok
· -- consumed = false: retroactive-key skip (V9D5) or empty value.
have h_C_tok : (((ps.tryConsume .value).2.tryConsume .key).2.tryConsume .value).2.tokens
= tokens := (tc_tokens _ _).trans ((tc_tokens _ _).trans h_tc_tok)
split at h_ok -- outer: peek?, peekNext?
all_goals (try (split at h_ok)) -- inner: peek? dispatch in the retroactive arm
all_goals (first
| (obtain ⟨rfl, rfl⟩ := h_ok; exact h_tc_tok)
| (obtain ⟨rfl, rfl⟩ := h_ok; exact h_C_tok)
| exact parseNode_tokens_of_wb h_wb _ fuel h_fuel h_C_tok _ _ h_ok)
lemma handleBlockMappingValueEntry_tok
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_wb : ParseNodeWB tokens n)
(ps : ParseState) (fuel : Nat) (h_fuel : fuel ≤ n)
(h_tok : ps.tokens = tokens)
(pairIdx : Nat) (val : YamlValue) (ps' : ParseState)
(h_ok : handleBlockMappingValueEntry ps fuel pairIdx = .ok (val, ps')) :
ps'.tokens = tokens := by
unfold handleBlockMappingValueEntry at h_ok
simp only [bind, Except.bind] at h_ok
have h_tok_adv : ps.advance.tokens = tokens := (advance_tokens ps).trans h_tok
-- Split on the inner match (peek? dispatch)
split at h_ok
all_goals (try (
simp only [Except.ok.injEq] at h_ok; obtain ⟨_, rfl⟩ := Prod.mk.inj h_ok
dsimp only []; exact h_tok_adv))
· split at h_ok
· simp at h_ok
· rename_i pn_res heq_pn; obtain ⟨v, ps_n⟩ := pn_res
dsimp only [] at h_ok; simp only [Except.ok.injEq] at h_ok
obtain ⟨_, rfl⟩ := Prod.mk.inj h_ok
dsimp only []
exact parseNode_tokens_of_wb h_wb _ fuel h_fuel
(by dsimp only []; exact h_tok_adv) _ _ heq_pn
-- Helpers accepting undestruct'd pair results (for goals after split where v✝ is opaque)
lemma pn_tok_pair
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_wb : ParseNodeWB tokens n)
(ps : ParseState) (fuel : Nat) (h_fuel : fuel ≤ n)
(h_tok : ps.tokens = tokens)
(result : YamlValue × ParseState)
(h_ok : parseNode ps fuel = .ok result) :
result.2.tokens = tokens := by
obtain ⟨v, ps'⟩ := result
exact parseNode_tokens_of_wb h_wb ps fuel h_fuel h_tok v ps' h_ok
lemma bev_tok_pair
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_wb : ParseNodeWB tokens n)
(ps : ParseState) (fuel : Nat) (h_fuel : fuel ≤ n)
(h_tok : ps.tokens = tokens)
(keyHasContent : Bool) (keyLine keyCol : Nat)
(result : YamlValue × ParseState)
(h_ok : parseBlockMappingEntryValue ps fuel keyHasContent keyLine keyCol = .ok result) :
result.2.tokens = tokens := by
obtain ⟨v, ps'⟩ := result
exact parseBlockMappingEntryValue_tok h_wb ps fuel h_fuel h_tok keyHasContent keyLine keyCol v ps' h_ok
lemma pn_wfa_pair
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_ih : ParseNodeWFA tokens n)
(ps : ParseState) (fuel : Nat) (h_fuel : fuel ≤ n)
(h_tok : ps.tokens = tokens)
(result : YamlValue × ParseState)
(h_ok : parseNode ps fuel = .ok result)
(h_wfa : WellFormedAnchors ps.anchors) :
WellFormedAnchors result.2.anchors := by
obtain ⟨v, ps'⟩ := result
exact h_ih ps fuel v ps' h_fuel h_tok h_ok h_wfa
lemma bev_wfa_pair
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_ih : ParseNodeWFA tokens n)
(ps : ParseState) (fuel : Nat) (h_fuel : fuel ≤ n)
(h_tok : ps.tokens = tokens)
(keyHasContent : Bool) (keyLine keyCol : Nat)
(result : YamlValue × ParseState)
(h_ok : parseBlockMappingEntryValue ps fuel keyHasContent keyLine keyCol = .ok result)
(h_wfa : WellFormedAnchors ps.anchors) :
WellFormedAnchors result.2.anchors := by
obtain ⟨v, ps'⟩ := result
exact parseBlockMappingEntryValue_wfa h_ih ps fuel h_fuel h_tok keyHasContent keyLine keyCol v ps' h_ok h_wfa
set_option maxHeartbeats 800000 in
lemma handleBlockMappingKeyEntry_tok
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_wb : ParseNodeWB tokens n)
(ps : ParseState) (fuel : Nat) (h_fuel : fuel ≤ n)
(h_tok : ps.tokens = tokens)
(pairIdx : Nat) (key val : YamlValue) (ps' : ParseState)
(h_ok : handleBlockMappingKeyEntry ps fuel pairIdx = .ok (key, val, ps')) :
ps'.tokens = tokens := by
unfold handleBlockMappingKeyEntry at h_ok
simp only [bind, Except.bind] at h_ok
have h_tok_adv : ps.advance.tokens = tokens := (advance_tokens ps).trans h_tok
split at h_ok <;> first | contradiction | skip
all_goals (try (simp only [emptyNode] at h_ok))
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (simp only [Except.ok.injEq] at h_ok)
all_goals (obtain ⟨_, _, rfl⟩ := h_ok)
all_goals (dsimp only [] at *)
-- emptyNode branches: BEV input has same tokens as ps.advance
all_goals (try (
refine bev_tok_pair h_wb _ fuel h_fuel ?htok _ _ _ _ ?hok
case hok => assumption
case htok => dsimp only []; exact h_tok_adv))
-- parseNode branches: derive tokens from parseNode first
all_goals (
refine bev_tok_pair h_wb ?_ fuel h_fuel ?htok ?_ ?_ ?_ ?_ ?hok
case hok => assumption
case htok =>
dsimp only []
first
| exact h_tok_adv
| (refine pn_tok_pair h_wb ?_ fuel h_fuel ?pntok ?_ ?pnok
case pnok => assumption
case pntok => exact h_tok_adv))
lemma parseFlowMappingValue_tok
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_wb : ParseNodeWB tokens n)
(ps : ParseState) (fuel : Nat) (h_fuel : fuel ≤ n)
(h_tok : ps.tokens = tokens)
(savedPath : YamlPath) (keyContent : String)
(val : YamlValue) (ps' : ParseState)
(h_ok : parseFlowMappingValue ps fuel savedPath keyContent = .ok (val, ps')) :
ps'.tokens = tokens := by
unfold parseFlowMappingValue at h_ok
simp only [bind, Except.bind] at h_ok
-- ps → withField → tryConsume .key → tryConsume .value → optional parseNode → withField
have h_tc1 : ({ ps with currentPath := savedPath.push (.key keyContent) }.tryConsume .key).2.tokens = tokens := by
rw [tc_tokens]; exact h_tok
have h_tc2 : (({ ps with currentPath := savedPath.push (.key keyContent) }.tryConsume .key).2.tryConsume .value).2.tokens
= tokens := by rw [tc_tokens]; exact h_tc1
split at h_ok <;> first | contradiction | skip
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (simp only [Except.ok.injEq] at h_ok)
all_goals (obtain ⟨_, rfl⟩ := Prod.mk.inj h_ok)
-- emptyNode: ps' = { tc_state with currentPath := savedPath }
all_goals try (dsimp only []; exact h_tc2)
-- parseNode: ps' = { ps_pn with currentPath := savedPath }
all_goals (
dsimp only []
exact parseNode_tokens_of_wb h_wb _ fuel h_fuel h_tc2 _ _ (by assumption))
set_option maxHeartbeats 800000 in
lemma parseSinglePairMapping_tok
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_wb : ParseNodeWB tokens n)
(ps : ParseState) (fuel : Nat) (h_fuel : fuel ≤ n + 1)
(h_tok : ps.tokens = tokens)
(val : YamlValue) (ps' : ParseState)
(h_ok : parseSinglePairMapping ps fuel = .ok (val, ps')) :
ps'.tokens = tokens := by
unfold parseSinglePairMapping at h_ok
simp only [bind, Except.bind] at h_ok
split at h_ok
· simp at h_ok
· rename_i k
have h_tok_adv : ps.advance.tokens = tokens := (advance_tokens ps).trans h_tok
split at h_ok <;> first | contradiction | skip
all_goals (try (simp only [emptyNode] at h_ok))
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (simp only [Except.ok.injEq] at h_ok)
all_goals (obtain ⟨_, rfl⟩ := Prod.mk.inj h_ok)
all_goals (dsimp only [] at *)
all_goals (first
| exact h_tok_adv
| exact (tc_tokens _ _).trans h_tok_adv
| (refine pn_tok_pair h_wb ?_ k (by omega) ?pntok ?_ ?pnok
case pnok => assumption
case pntok => exact (tc_tokens _ _).trans h_tok_adv)
| (refine pn_tok_pair h_wb ?_ k (by omega) ?pntok ?_ ?pnok
case pnok => assumption
case pntok =>
refine (tc_tokens _ _).trans ?_
refine pn_tok_pair h_wb ?_ k (by omega) ?pntok2 ?_ ?pnok2
case pnok2 => assumption
case pntok2 => exact h_tok_adv)
| (refine (tc_tokens _ _).trans ?_
refine pn_tok_pair h_wb ?_ k (by omega) ?pntok ?_ ?pnok
case pnok => assumption
case pntok => exact h_tok_adv))
-- ── §3b Remaining WFA lemmas ──────────────────────────────────────
-- handleBlockMappingKeyEntry
set_option maxHeartbeats 1600000 in
lemma handleBlockMappingKeyEntry_wfa
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_ih : ParseNodeWFA tokens n) (h_wb : ParseNodeWB tokens n)
(ps : ParseState) (fuel : Nat) (h_fuel : fuel ≤ n)
(h_tok : ps.tokens = tokens)
(pairIdx : Nat) (key val : YamlValue) (ps' : ParseState)
(h_ok : handleBlockMappingKeyEntry ps fuel pairIdx = .ok (key, val, ps'))
(h_wfa : WellFormedAnchors ps.anchors) :
WellFormedAnchors ps'.anchors := by
unfold handleBlockMappingKeyEntry at h_ok
simp only [bind, Except.bind] at h_ok
have h_wfa_adv : WellFormedAnchors ps.advance.anchors :=
advance_anchors ps ▸ h_wfa
have h_tok_adv : ps.advance.tokens = tokens :=
(advance_tokens ps).trans h_tok
split at h_ok <;> first | contradiction | skip
all_goals (try (simp only [emptyNode] at h_ok))
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (simp only [Except.ok.injEq] at h_ok)
all_goals (obtain ⟨_, _, rfl⟩ := h_ok)
all_goals (dsimp only [] at *)
-- emptyNode branches
all_goals (try (
refine bev_wfa_pair h_ih ?_ fuel h_fuel ?htok ?_ ?_ ?_ ?_ ?hok ?hwfa
case hok => assumption
case htok => dsimp only []; exact h_tok_adv
case hwfa => dsimp only []; exact h_wfa_adv))
-- parseNode branches
all_goals (
refine bev_wfa_pair h_ih ?_ fuel h_fuel ?htok ?_ ?_ ?_ ?_ ?hok ?hwfa
case hok => assumption
case htok =>
dsimp only []
first
| exact h_tok_adv
| (refine pn_tok_pair h_wb ?_ fuel h_fuel ?pntok ?_ ?pnok
case pnok => assumption
case pntok => exact h_tok_adv)
case hwfa =>
dsimp only []
first
| exact h_wfa_adv
| (refine pn_wfa_pair h_ih ?_ fuel h_fuel ?pntok2 ?_ ?pnok2 ?pnwfa2
case pnok2 => assumption
case pntok2 => exact h_tok_adv
case pnwfa2 => exact h_wfa_adv))
lemma parseBlockMappingLoop_wfa
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_ih : ParseNodeWFA tokens n) (h_wb : ParseNodeWB tokens n)
(ps : ParseState) (fuel : Nat)
(pairs : Array (YamlValue × YamlValue))
(h_fuel : fuel ≤ n + 1) (h_tok : ps.tokens = tokens)
(result : Array (YamlValue × YamlValue)) (ps' : ParseState)
(h_ok : parseBlockMappingLoop ps fuel pairs = .ok (result, ps'))
(h_wfa : WellFormedAnchors ps.anchors) :
WellFormedAnchors ps'.anchors := by
induction fuel generalizing ps pairs with
| zero =>
unfold parseBlockMappingLoop at h_ok
simp only [Except.ok.injEq] at h_ok
obtain ⟨_, rfl⟩ := Prod.mk.inj h_ok; exact h_wfa
| succ k ih_fuel =>
unfold parseBlockMappingLoop at h_ok
simp only [bind, Except.bind] at h_ok
split at h_ok
· -- key
split at h_ok
· simp at h_ok
· rename_i entry_res heq_entry
obtain ⟨key_v, val_v, ps_entry⟩ := entry_res
dsimp only [] at h_ok
have h_wfa_entry := handleBlockMappingKeyEntry_wfa h_ih h_wb ps k (by omega)
h_tok _ key_v val_v ps_entry heq_entry h_wfa
have h_tok_entry := handleBlockMappingKeyEntry_tok h_wb ps k (by omega)
h_tok _ key_v val_v ps_entry heq_entry
exact ih_fuel ps_entry _ (by omega) h_tok_entry h_ok h_wfa_entry
· -- value (implicit key)
split at h_ok
· simp at h_ok
· rename_i val_res heq_val
obtain ⟨val_v, ps_val⟩ := val_res
dsimp only [] at h_ok
have h_wfa_val := handleBlockMappingValueEntry_wfa h_ih ps k (by omega)
h_tok _ val_v ps_val heq_val h_wfa
have h_tok_val := handleBlockMappingValueEntry_tok h_wb ps k (by omega)
h_tok _ val_v ps_val heq_val
exact ih_fuel ps_val _ (by omega) h_tok_val h_ok h_wfa_val
· -- not key/value
simp only [Except.ok.injEq] at h_ok
obtain ⟨_, rfl⟩ := Prod.mk.inj h_ok; exact h_wfa
-- parseBlockMapping
lemma parseBlockMapping_wfa
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_ih : ParseNodeWFA tokens n) (h_wb : ParseNodeWB tokens n)
(ps : ParseState) (fuel : Nat) (h_fuel : fuel ≤ n + 1)
(h_tok : ps.tokens = tokens)
(val : YamlValue) (ps' : ParseState)
(h_ok : parseBlockMapping ps fuel = .ok (val, ps'))
(h_wfa : WellFormedAnchors ps.anchors) :
WellFormedAnchors ps'.anchors := by
unfold parseBlockMapping at h_ok
simp only [bind, Except.bind] at h_ok
split at h_ok
· simp at h_ok
· rename_i k
split at h_ok
· simp at h_ok
· rename_i loop_res heq_loop
obtain ⟨pairs, ps_loop⟩ := loop_res
dsimp only [] at h_ok
have h_wfa_loop := parseBlockMappingLoop_wfa h_ih h_wb ps.advance k #[] (by omega)
((advance_tokens ps).trans h_tok) pairs ps_loop heq_loop (advance_anchors ps ▸ h_wfa)
split at h_ok <;> (
simp only [Except.ok.injEq] at h_ok
obtain ⟨_, rfl⟩ := Prod.mk.inj h_ok)
· rw [advance_anchors]; exact h_wfa_loop
· exact h_wfa_loop
-- parseFlowMappingValue
lemma parseFlowMappingValue_wfa
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_ih : ParseNodeWFA tokens n)
(ps : ParseState) (fuel : Nat) (h_fuel : fuel ≤ n)
(h_tok : ps.tokens = tokens)
(savedPath : YamlPath) (keyContent : String)
(val : YamlValue) (ps' : ParseState)
(h_ok : parseFlowMappingValue ps fuel savedPath keyContent = .ok (val, ps'))
(h_wfa : WellFormedAnchors ps.anchors) :
WellFormedAnchors ps'.anchors := by
unfold parseFlowMappingValue at h_ok
simp only [bind, Except.bind] at h_ok
have h_wfa_wf : WellFormedAnchors ({ ps with currentPath := savedPath.push (.key keyContent) }.tryConsume .key).2.anchors := by
rw [tc_anchors]; exact h_wfa
have h_tc2_wfa : WellFormedAnchors
(({ ps with currentPath := savedPath.push (.key keyContent) }.tryConsume .key).2.tryConsume .value).2.anchors := by
rw [tc_anchors]; exact h_wfa_wf
have h_tc2_tok : (({ ps with currentPath := savedPath.push (.key keyContent) }.tryConsume .key).2.tryConsume .value).2.tokens
= tokens := by rw [tc_tokens]; rw [tc_tokens]; exact h_tok
split at h_ok <;> first | contradiction | skip
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (simp only [Except.ok.injEq] at h_ok)
all_goals (obtain ⟨_, rfl⟩ := Prod.mk.inj h_ok)
-- emptyNode: result state = { tc2_state with currentPath := savedPath }
all_goals try (dsimp only []; exact h_tc2_wfa)
-- parseNode: result state = { ps_pn with currentPath := savedPath }
all_goals (
dsimp only []
exact h_ih _ fuel _ _ h_fuel h_tc2_tok (by assumption) h_tc2_wfa)
-- parseSinglePairMapping
set_option maxHeartbeats 1600000 in
lemma parseSinglePairMapping_wfa
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_ih : ParseNodeWFA tokens n) (h_wb : ParseNodeWB tokens n)
(ps : ParseState) (fuel : Nat) (h_fuel : fuel ≤ n + 1)
(h_tok : ps.tokens = tokens)
(val : YamlValue) (ps' : ParseState)
(h_ok : parseSinglePairMapping ps fuel = .ok (val, ps'))
(h_wfa : WellFormedAnchors ps.anchors) :
WellFormedAnchors ps'.anchors := by
unfold parseSinglePairMapping at h_ok
simp only [bind, Except.bind] at h_ok
split at h_ok
· simp at h_ok
· rename_i k
have h_wfa_adv : WellFormedAnchors ps.advance.anchors := advance_anchors ps ▸ h_wfa
have h_tok_adv : ps.advance.tokens = tokens := (advance_tokens ps).trans h_tok
split at h_ok <;> first | contradiction | skip
all_goals (try (simp only [emptyNode] at h_ok))
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (first | (split at h_ok <;> first | contradiction | skip) | skip)
all_goals (simp only [Except.ok.injEq] at h_ok)
all_goals (obtain ⟨_, rfl⟩ := Prod.mk.inj h_ok)
all_goals (dsimp only [] at *)
all_goals (first
| exact h_wfa_adv
| (rw [tc_anchors]; exact h_wfa_adv)
| (refine pn_wfa_pair h_ih ?_ k (by omega) ?pntok ?_ ?pnok ?pnwfa
case pnok => assumption
case pntok => exact (tc_tokens _ _).trans h_tok_adv
case pnwfa => rw [tc_anchors]; exact h_wfa_adv)
| (refine pn_wfa_pair h_ih ?_ k (by omega) ?pntok ?_ ?pnok ?pnwfa
case pnok => assumption
case pntok =>
refine (tc_tokens _ _).trans ?_
refine pn_tok_pair h_wb ?_ k (by omega) ?pt2 ?_ ?po2
case po2 => assumption
case pt2 => exact h_tok_adv
case pnwfa =>
rw [tc_anchors]
refine pn_wfa_pair h_ih ?_ k (by omega) ?pt3 ?_ ?po3 ?pw3
case po3 => assumption
case pt3 => exact h_tok_adv
case pw3 => exact h_wfa_adv)
| (rw [tc_anchors]
refine pn_wfa_pair h_ih ?_ k (by omega) ?pntok ?_ ?pnok ?pnwfa
case pnok => assumption
case pntok => exact h_tok_adv
case pnwfa => exact h_wfa_adv))
-- parseFlowSequenceLoop
set_option maxHeartbeats 1600000 in
lemma parseFlowSequenceLoop_wfa
{tokens : Array (Positioned YamlToken)} {n : Nat}
(h_ih : ParseNodeWFA tokens n) (h_wb : ParseNodeWB tokens n)
(ps : ParseState) (fuel : Nat) (items : Array YamlValue)
(h_fuel : fuel ≤ n + 1) (h_tok : ps.tokens = tokens)
(result : Array YamlValue) (ps' : ParseState)
(h_ok : parseFlowSequenceLoop ps fuel items = .ok (result, ps'))
(h_wfa : WellFormedAnchors ps.anchors) :
WellFormedAnchors ps'.anchors := by
induction fuel generalizing ps items with
| zero =>
unfold parseFlowSequenceLoop at h_ok
simp only [Except.ok.injEq] at h_ok
obtain ⟨_, rfl⟩ := Prod.mk.inj h_ok; exact h_wfa
| succ k ih_fuel =>
unfold parseFlowSequenceLoop at h_ok
simp only [bind, Except.bind, pure, Except.pure] at h_ok
split at h_ok
next => -- flowSequenceEnd
simp only [Except.ok.injEq] at h_ok
obtain ⟨_, rfl⟩ := Prod.mk.inj h_ok; exact h_wfa
next => -- other
split at h_ok
· -- items.size > 0
split at h_ok
next => -- flowEntry → advance then dispatch
have h_wfa_adv : WellFormedAnchors ps.advance.anchors := advance_anchors ps ▸ h_wfa
have h_tok_adv : ps.advance.tokens = tokens := (advance_tokens ps).trans h_tok
split at h_ok
next => -- key → parseSinglePairMapping
split at h_ok
next => simp at h_ok
next spm_res heq_spm =>
obtain ⟨mv, ps3⟩ := spm_res; dsimp only [] at h_ok
have h_wfa3 := parseSinglePairMapping_wfa h_ih h_wb
({ ps.advance with currentPath := _ }) k (by omega)
(by dsimp only []; exact h_tok_adv)
mv ps3 (by dsimp only [] at *; exact heq_spm)
(by dsimp only []; exact h_wfa_adv)