Imports
/-
Copyright (c) 2026 Terence Rokop. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Terence Rokop
-/
module
public import Geb.Mathlib.Data.Tree.Ranked.Code
public import Mathlib.Algebra.BigOperators.Ring.List
public import Mathlib.Computability.EncodingThe preorder encoding of ranked terms
A symbol is spelled by its block followed by its children's spellings, in index order. The encoding is a bijection onto the words a single right-to-left scan accepts, the scan carrying an incomplete block, the count of pending subterms and a liveness flag.
The idea is that of prefix notation, in which a symbol is followed by exactly
as many operands as its arity. RankedAlphabet.Binary is the case of one
symbol of arity zero and one of arity two.
Validity is stated as three conditions, in the manner of mathlib's
DyckWord, whose fields count_U_eq_count_D and count_D_le_count_U play
the roles the pending count and the liveness flag play here, and in the
direction a single right-to-left pass carrying a counter can scan.
valid_iff_scanFinal adds a third, that the incomplete block is empty: at a
general width that condition is not implied by the other two, so stating
validity as only the first two, as DyckWord does, would be false here.
A boundedRec of a Cobham-style function algebra processes a list from its
far end, so in that reading a symbol's block is read before the blocks of its
children and the symbol is applied after them: prefix notation in list order
is postfix notation in processing order.
Main definitions
RankedAlphabet.spell — the encoding.
RankedAlphabet.decodeBlock, RankedAlphabet.parseChildren,
RankedAlphabet.parseStep, RankedAlphabet.parseAux,
RankedAlphabet.parse — the fuel-bounded recursive-descent decoding.
RankedAlphabet.encoding — the two as a Computability.Encoding.
RankedAlphabet.Scan, RankedAlphabet.scanStep,
RankedAlphabet.scanFrom, RankedAlphabet.scanFinal — the scan.
RankedAlphabet.validBool, RankedAlphabet.Valid — what it accepts.
DecidablePred RankedAlphabet.Valid — the instance deciding it.
Main statements
RankedAlphabet.parse_spell — the retraction law.
RankedAlphabet.parse_eq_some_iff — the decoding inverts the encoding.
RankedAlphabet.spell_injective — distinct terms have distinct spellings.
RankedAlphabet.valid_spell — every spelling is valid.
RankedAlphabet.valid_iff_exists_spell — the valid words are exactly the
spellings.
RankedAlphabet.valid_iff_isSome_parse — the descent decides validity.
RankedAlphabet.length_buf_scanFinal_of_live — a live scan's incomplete
block holds the word's length modulo the width.
RankedAlphabet.length_buf_scanFinal_lt — the incomplete block never fills.
RankedAlphabet.depth_scanFinal_le_length — the pending count is at most
the word's length.
RankedAlphabet.valid_iff_scanFinal — validity as three conditions on the
final state.
Implementation notes
parseAux recurses on an explicit ℕ bound rather than on its input: each
child is parsed from a remainder the previous call computes, which is not a
structural subterm. parse supplies the input's length, and length_spell
with width_pos shows that bound admits every word spell emits.
Fuel exhaustion is not a rejection mechanism of its own. Each parseStep
layer consumes a whole block, which width_pos makes at least one bit, so
the invariant that the fuel is at least the remaining length holds from
parse's initial w.length down to wherever the descent stops. The
descent's own rejections are decodeBlock's two — the input falls short of
a block, or the block spells no symbol — together with a child's failure and
the trailing input parse rejects.
parseChildren is a Nat.rec and decodeBits a List.rec, so neither has
generated equation lemmas; parseChildren_succ and decodeBits_cons are
stated for the rewrites that need them.
scanStep matches on Bool values and Valid is a Bool equation rather
than an equation of Scan. Both are required by kernel reduction: an equation
of Scan would be decided through a derived DecidableEq, which at a
symbolic fold leaves decide stuck on the instance rather than reducing it,
so Scan derives none. A decidable test is not itself the obstruction, as
decodeBlock shows by branching on one and reducing.
exists_spell_append_of_live_of_buf_nil_of_one_le_depth is likewise bounded
by an explicit ℕ and driven by Nat.rec. Its step applies the hypothesis
once per child of the head symbol, through
exists_children_append_of_le_depth, which takes that hypothesis as an
argument; every use sits at the same bound, so no well-founded recursion is
needed. The step splits off the word's leading block rather than its trailing
one: spell is prefix notation while the scan reads right to left, so the
head symbol's block is read last, from the state the scan of everything after
it leaves.
The base case ascribes w.length ≤ 0 to hzero before appealing to omega:
Nat.rec's base case presents the bound as w.length ≤ Nat.zero, and omega
treats the unreduced Nat.zero as an opaque atom, so the ascription at the
literal 0 is what makes the hypothesis usable.
spell_injective is derived from Computability.Encoding.encode_injective
through encoding rather than proved directly, the encoding and its descent
being exactly that structure's three fields.
Tags
ranked alphabet, preorder, prefix notation, encoding, retraction, scan
namespace RankedAlphabetpublic sectionThe preorder encoding: a symbol's block followed by its children's spellings, in index order.
@[expose] def spell (R : RankedAlphabet) : R.Term → List Bool :=
WType.elim (List Bool) fun x ↦ R.code x.1 ++ (List.ofFn x.2).flatten@[simp] theorem spell_mk (R : RankedAlphabet) (i : Fin R.card)
(ch : Fin (R.arity i) → R.Term) :
R.spell (Term.mk R i ch) = R.code i ++ (List.ofFn fun d ↦ R.spell (ch d)).flatten :=
rfl
A spelling's length is the alphabet's width times the term's node count,
so the input length is fuel enough for the descent to read anything spell
emits.
theorem length_spell (R : RankedAlphabet) (t : R.Term) :
(R.spell t).length = R.width * t.size :=
Term.induction (motive := fun t ↦ (R.spell t).length = R.width * t.size)
(fun i ch ih ↦ R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)), (R.spell (ch d)).length = R.width * (ch d).size⊢ (R.spell (Term.mk R i ch)).length = R.width * (Term.mk R i ch).size
R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)), (R.spell (ch d)).length = R.width * (ch d).size⊢ R.width + (List.ofFn fun d ↦ R.spell (ch d)).flatten.length = R.width + R.width * (List.ofFn fun d ↦ (ch d).size).sum
congr 1 e_a R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)), (R.spell (ch d)).length = R.width * (ch d).size⊢ (List.ofFn fun d ↦ R.spell (ch d)).flatten.length = R.width * (List.ofFn fun d ↦ (ch d).size).sum
rw [List.length_flatten, e_a R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)), (R.spell (ch d)).length = R.width * (ch d).size⊢ (List.map List.length (List.ofFn fun d ↦ R.spell (ch d))).sum = R.width * (List.ofFn fun d ↦ (ch d).size).sum List.map_ofFn e_a R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)), (R.spell (ch d)).length = R.width * (ch d).size⊢ (List.ofFn (List.length ∘ fun d ↦ R.spell (ch d))).sum = R.width * (List.ofFn fun d ↦ (ch d).size).sum] e_a R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)), (R.spell (ch d)).length = R.width * (ch d).size⊢ (List.ofFn (List.length ∘ fun d ↦ R.spell (ch d))).sum = R.width * (List.ofFn fun d ↦ (ch d).size).sum
simp only [Function.comp_def, ih] e_a R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)), (R.spell (ch d)).length = R.width * (ch d).size⊢ (List.ofFn fun x ↦ R.width * (ch x).size).sum = R.width * (List.ofFn fun d ↦ (ch d).size).sum
rw [show (List.ofFn fun x ↦ R.width * (ch x).size)
= List.map (fun b ↦ R.width * b) (List.ofFn fun d ↦ (ch d).size) by R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)), (R.spell (ch d)).length = R.width * (ch d).size⊢ (R.spell (Term.mk R i ch)).length = R.width * (Term.mk R i ch).size
rw [List.map_ofFn R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)), (R.spell (ch d)).length = R.width * (ch d).size⊢ (List.ofFn fun x ↦ R.width * (ch x).size) = List.ofFn ((fun b ↦ R.width * b) ∘ fun d ↦ (ch d).size)] R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)), (R.spell (ch d)).length = R.width * (ch d).size⊢ (List.ofFn fun x ↦ R.width * (ch x).size) = List.ofFn ((fun b ↦ R.width * b) ∘ fun d ↦ (ch d).size); rfl All goals completed! 🐙, List.sum_map_mul_left, e_a R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)), (R.spell (ch d)).length = R.width * (ch d).size⊢ R.width * (List.map (fun b ↦ b) (List.ofFn fun d ↦ (ch d).size)).sum = R.width * (List.ofFn fun d ↦ (ch d).size).sum List.map_id' e_a R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)), (R.spell (ch d)).length = R.width * (ch d).size⊢ R.width * (List.ofFn fun d ↦ (ch d).size).sum = R.width * (List.ofFn fun d ↦ (ch d).size).sum] All goals completed! 🐙) tRead one block: the symbol it spells and the unconsumed remainder, absent when the word is short of a block or the block spells no symbol.
@[expose] def decodeBlock (R : RankedAlphabet) (w : List Bool) :
Option (Fin R.card × List Bool) :=
if h : decodeBits (w.take R.width) < R.card ∧ R.width ≤ w.length then
some (⟨decodeBits (w.take R.width), h.1⟩, w.drop R.width)
else none
Read n children in index order, delegating each to child.
@[expose] def parseChildren {R : RankedAlphabet}
(child : List Bool → Option (R.Term × List Bool)) :
(n : ℕ) → List Bool → Option ((Fin n → R.Term) × List Bool) :=
Nat.rec (fun w ↦ some (Fin.elim0, w))
fun _ ih w ↦
match child w with
| none => none
| some (t, w₁) =>
match ih w₁ with
| none => none
| some (f, w₂) => some (Fin.cons t f, w₂)
parseChildren unfolded on a positive count. parseChildren is a bare
Nat.rec, for which Lean generates no equation lemma.
theorem parseChildren_succ {R : RankedAlphabet}
(child : List Bool → Option (R.Term × List Bool)) (n : ℕ) (w : List Bool) :
parseChildren child (n + 1) w =
(match child w with
| none => none
| some (t, w₁) =>
match parseChildren child n w₁ with
| none => none
| some (f, w₂) => some (Fin.cons t f, w₂)) := rflOne layer of the recursive descent: read one block, then as many children as its symbol's arity.
@[expose] def parseStep (R : RankedAlphabet)
(child : List Bool → Option (R.Term × List Bool)) (w : List Bool) :
Option (R.Term × List Bool) :=
match R.decodeBlock w with
| none => none
| some (i, rest) =>
match parseChildren child (R.arity i) rest with
| none => none
| some (f, rest') => some (Term.mk R i f, rest')
Recursive descent bounded by an explicit ℕ.
@[expose] def parseAux (R : RankedAlphabet) :
ℕ → List Bool → Option (R.Term × List Bool) :=
Nat.rec (fun _ ↦ none) fun _ ih ↦ R.parseStep ihtheorem parseAux_succ (R : RankedAlphabet) (f : ℕ) :
R.parseAux (f + 1) = R.parseStep (R.parseAux f) := rflThe decoding, rejecting trailing input.
@[expose] def parse (R : RankedAlphabet) (w : List Bool) : Option R.Term :=
match R.parseAux w.length w with
| some (t, []) => some t
| _ => noneA block followed by anything is read as its own symbol, leaving what follows.
theorem decodeBlock_code_append (R : RankedAlphabet) (i : Fin R.card)
(rest : List Bool) : R.decodeBlock (R.code i ++ rest) = some (i, rest) := by R:RankedAlphabeti:Fin R.cardrest:List Bool⊢ R.decodeBlock (R.code i ++ rest) = some (i, rest)
have htake : (R.code i ++ rest).take R.width = R.code i := by
rw [← length_code R i, R:RankedAlphabeti:Fin R.cardrest:List Bool⊢ List.take (R.code i).length (R.code i ++ rest) = R.code i List.take_left R:RankedAlphabeti:Fin R.cardrest:List Bool⊢ R.code i = R.code i] R:RankedAlphabeti:Fin R.cardrest:List Boolhtake:List.take R.width (R.code i ++ rest) = R.code i⊢ R.decodeBlock (R.code i ++ rest) = some (i, rest)
have hdrop : (R.code i ++ rest).drop R.width = rest := by
rw [← length_code R i, R:RankedAlphabeti:Fin R.cardrest:List Boolhtake:List.take R.width (R.code i ++ rest) = R.code i⊢ List.drop (R.code i).length (R.code i ++ rest) = rest List.drop_left R:RankedAlphabeti:Fin R.cardrest:List Boolhtake:List.take R.width (R.code i ++ rest) = R.code i⊢ rest = rest] R:RankedAlphabeti:Fin R.cardrest:List Boolhtake:List.take R.width (R.code i ++ rest) = R.code ihdrop:List.drop R.width (R.code i ++ rest) = rest⊢ R.decodeBlock (R.code i ++ rest) = some (i, rest)
have hlen : R.width ≤ (R.code i ++ rest).length := by
rw [List.length_append, R:RankedAlphabeti:Fin R.cardrest:List Boolhtake:List.take R.width (R.code i ++ rest) = R.code ihdrop:List.drop R.width (R.code i ++ rest) = rest⊢ R.width ≤ (R.code i).length + rest.length length_code R:RankedAlphabeti:Fin R.cardrest:List Boolhtake:List.take R.width (R.code i ++ rest) = R.code ihdrop:List.drop R.width (R.code i ++ rest) = rest⊢ R.width ≤ R.width + rest.length] R:RankedAlphabeti:Fin R.cardrest:List Boolhtake:List.take R.width (R.code i ++ rest) = R.code ihdrop:List.drop R.width (R.code i ++ rest) = rest⊢ R.width ≤ R.width + rest.length
omega R:RankedAlphabeti:Fin R.cardrest:List Boolhtake:List.take R.width (R.code i ++ rest) = R.code ihdrop:List.drop R.width (R.code i ++ rest) = resthlen:R.width ≤ (R.code i ++ rest).length⊢ R.decodeBlock (R.code i ++ rest) = some (i, rest)
simp only [decodeBlock, htake, hdrop,
dite_eq_left (show decodeBits (R.code i) < R.card ∧ R.width ≤ (R.code i ++ rest).length
from ⟨by rw [decodeBits_code]; exact i.isLt, hlen⟩)] R:RankedAlphabeti:Fin R.cardrest:List Boolhtake:List.take R.width (R.code i ++ rest) = R.code ihdrop:List.drop R.width (R.code i ++ rest) = resthlen:R.width ≤ (R.code i ++ rest).length⊢ some (⟨decodeBits (R.code i), ⋯⟩, rest) = some (i, rest)
congr 1 R:RankedAlphabeti:Fin R.cardrest:List Boolhtake:List.take R.width (R.code i ++ rest) = R.code ihdrop:List.drop R.width (R.code i ++ rest) = resthlen:R.width ≤ (R.code i ++ rest).length⊢ (⟨decodeBits (R.code i), ⋯⟩, rest) = (i, rest)
exact Prod.ext (Fin.val_injective (decodeBits_code R i)) rfl All goals completed! 🐙
Reading n children off the concatenation of their spellings returns them
and the remainder.
theorem parseChildren_flatten (R : RankedAlphabet) {n : ℕ} (ch : Fin n → R.Term)
(ih : ∀ d f rest, (ch d).size ≤ f →
R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest))
(f : ℕ) (rest : List Bool) (hf : ∀ d, (ch d).size ≤ f) :
parseChildren (R.parseAux f) n
((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) = some (ch, rest) := by R:RankedAlphabetn:ℕch:Fin n → R.Termih:∀ (d : Fin n) (f : ℕ) (rest : List Bool), (ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)f:ℕrest:List Boolhf:∀ (d : Fin n), (ch d).size ≤ f⊢ parseChildren (R.parseAux f) n ((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) = some (ch, rest)
refine Nat.rec (motive := fun n ↦ ∀ (ch : Fin n → R.Term),
(∀ d f' rest', (ch d).size ≤ f' →
R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')) →
(∀ d, (ch d).size ≤ f) →
parseChildren (R.parseAux f) n
((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) = some (ch, rest))
?base ?step n ch ih hf base R:RankedAlphabetn:ℕch:Fin n → R.Termih:∀ (d : Fin n) (f : ℕ) (rest : List Bool), (ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)f:ℕrest:List Boolhf:∀ (d : Fin n), (ch d).size ≤ f⊢ ∀ (ch : Fin Nat.zero → R.Term),
(∀ (d : Fin Nat.zero) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')) →
(∀ (d : Fin Nat.zero), (ch d).size ≤ f) →
parseChildren (R.parseAux f) Nat.zero ((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) = some (ch, rest)step R:RankedAlphabetn:ℕch:Fin n → R.Termih:∀ (d : Fin n) (f : ℕ) (rest : List Bool), (ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)f:ℕrest:List Boolhf:∀ (d : Fin n), (ch d).size ≤ f⊢ ∀ (n : ℕ),
(∀ (ch : Fin n → R.Term),
(∀ (d : Fin n) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')) →
(∀ (d : Fin n), (ch d).size ≤ f) →
parseChildren (R.parseAux f) n ((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) = some (ch, rest)) →
∀ (ch : Fin n.succ → R.Term),
(∀ (d : Fin n.succ) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')) →
(∀ (d : Fin n.succ), (ch d).size ≤ f) →
parseChildren (R.parseAux f) n.succ ((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) = some (ch, rest)
· base R:RankedAlphabetn:ℕch:Fin n → R.Termih:∀ (d : Fin n) (f : ℕ) (rest : List Bool), (ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)f:ℕrest:List Boolhf:∀ (d : Fin n), (ch d).size ≤ f⊢ ∀ (ch : Fin Nat.zero → R.Term),
(∀ (d : Fin Nat.zero) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')) →
(∀ (d : Fin Nat.zero), (ch d).size ≤ f) →
parseChildren (R.parseAux f) Nat.zero ((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) = some (ch, rest) intro ch _ _ base R:RankedAlphabetn:ℕch✝:Fin n → R.Termih:∀ (d : Fin n) (f : ℕ) (rest : List Bool), (ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)f:ℕrest:List Boolhf:∀ (d : Fin n), (ch d).size ≤ fch:Fin Nat.zero → R.Terma✝¹:∀ (d : Fin Nat.zero) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')a✝:∀ (d : Fin Nat.zero), (ch d).size ≤ f⊢ parseChildren (R.parseAux f) Nat.zero ((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) = some (ch, rest)
simp only [List.ofFn_zero, List.flatten_nil, List.nil_append, parseChildren] base R:RankedAlphabetn:ℕch✝:Fin n → R.Termih:∀ (d : Fin n) (f : ℕ) (rest : List Bool), (ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)f:ℕrest:List Boolhf:∀ (d : Fin n), (ch d).size ≤ fch:Fin Nat.zero → R.Terma✝¹:∀ (d : Fin Nat.zero) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')a✝:∀ (d : Fin Nat.zero), (ch d).size ≤ f⊢ some (Fin.elim0, rest) = some (ch, rest)
exact congrArg (fun g ↦ some (g, rest)) (funext fun d ↦ d.elim0) All goals completed! 🐙
· step R:RankedAlphabetn:ℕch:Fin n → R.Termih:∀ (d : Fin n) (f : ℕ) (rest : List Bool), (ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)f:ℕrest:List Boolhf:∀ (d : Fin n), (ch d).size ≤ f⊢ ∀ (n : ℕ),
(∀ (ch : Fin n → R.Term),
(∀ (d : Fin n) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')) →
(∀ (d : Fin n), (ch d).size ≤ f) →
parseChildren (R.parseAux f) n ((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) = some (ch, rest)) →
∀ (ch : Fin n.succ → R.Term),
(∀ (d : Fin n.succ) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')) →
(∀ (d : Fin n.succ), (ch d).size ≤ f) →
parseChildren (R.parseAux f) n.succ ((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) = some (ch, rest) intro n ihn ch ihch hch step R:RankedAlphabetn✝:ℕch✝:Fin n → R.Termih:∀ (d : Fin n) (f : ℕ) (rest : List Bool), (ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)f:ℕrest:List Boolhf:∀ (d : Fin n), (ch d).size ≤ fn:ℕihn:∀ (ch : Fin n → R.Term),
(∀ (d : Fin n) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')) →
(∀ (d : Fin n), (ch d).size ≤ f) →
parseChildren (R.parseAux f) n ((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) = some (ch, rest)ch:Fin n.succ → R.Termihch:∀ (d : Fin n.succ) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')hch:∀ (d : Fin n.succ), (ch d).size ≤ f⊢ parseChildren (R.parseAux f) n.succ ((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) = some (ch, rest)
rw [List.ofFn_succ, step R:RankedAlphabetn✝:ℕch✝:Fin n → R.Termih:∀ (d : Fin n) (f : ℕ) (rest : List Bool), (ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)f:ℕrest:List Boolhf:∀ (d : Fin n), (ch d).size ≤ fn:ℕihn:∀ (ch : Fin n → R.Term),
(∀ (d : Fin n) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')) →
(∀ (d : Fin n), (ch d).size ≤ f) →
parseChildren (R.parseAux f) n ((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) = some (ch, rest)ch:Fin n.succ → R.Termihch:∀ (d : Fin n.succ) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')hch:∀ (d : Fin n.succ), (ch d).size ≤ f⊢ parseChildren (R.parseAux f) n.succ ((R.spell (ch 0) :: List.ofFn fun i ↦ R.spell (ch i.succ)).flatten ++ rest) =
some (ch, rest) List.flatten_cons, step R:RankedAlphabetn✝:ℕch✝:Fin n → R.Termih:∀ (d : Fin n) (f : ℕ) (rest : List Bool), (ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)f:ℕrest:List Boolhf:∀ (d : Fin n), (ch d).size ≤ fn:ℕihn:∀ (ch : Fin n → R.Term),
(∀ (d : Fin n) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')) →
(∀ (d : Fin n), (ch d).size ≤ f) →
parseChildren (R.parseAux f) n ((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) = some (ch, rest)ch:Fin n.succ → R.Termihch:∀ (d : Fin n.succ) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')hch:∀ (d : Fin n.succ), (ch d).size ≤ f⊢ parseChildren (R.parseAux f) n.succ (R.spell (ch 0) ++ (List.ofFn fun i ↦ R.spell (ch i.succ)).flatten ++ rest) =
some (ch, rest) List.append_assoc, step R:RankedAlphabetn✝:ℕch✝:Fin n → R.Termih:∀ (d : Fin n) (f : ℕ) (rest : List Bool), (ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)f:ℕrest:List Boolhf:∀ (d : Fin n), (ch d).size ≤ fn:ℕihn:∀ (ch : Fin n → R.Term),
(∀ (d : Fin n) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')) →
(∀ (d : Fin n), (ch d).size ≤ f) →
parseChildren (R.parseAux f) n ((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) = some (ch, rest)ch:Fin n.succ → R.Termihch:∀ (d : Fin n.succ) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')hch:∀ (d : Fin n.succ), (ch d).size ≤ f⊢ parseChildren (R.parseAux f) n.succ (R.spell (ch 0) ++ ((List.ofFn fun i ↦ R.spell (ch i.succ)).flatten ++ rest)) =
some (ch, rest) parseChildren_succ, step R:RankedAlphabetn✝:ℕch✝:Fin n → R.Termih:∀ (d : Fin n) (f : ℕ) (rest : List Bool), (ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)f:ℕrest:List Boolhf:∀ (d : Fin n), (ch d).size ≤ fn:ℕihn:∀ (ch : Fin n → R.Term),
(∀ (d : Fin n) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')) →
(∀ (d : Fin n), (ch d).size ≤ f) →
parseChildren (R.parseAux f) n ((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) = some (ch, rest)ch:Fin n.succ → R.Termihch:∀ (d : Fin n.succ) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')hch:∀ (d : Fin n.succ), (ch d).size ≤ f⊢ (match R.parseAux f (R.spell (ch 0) ++ ((List.ofFn fun i ↦ R.spell (ch i.succ)).flatten ++ rest)) with
| none => none
| some (t, w₁) =>
match parseChildren (R.parseAux f) n w₁ with
| none => none
| some (f, w₂) => some (Fin.cons t f, w₂)) =
some (ch, rest)
ihch 0 f _ (hch 0) step R:RankedAlphabetn✝:ℕch✝:Fin n → R.Termih:∀ (d : Fin n) (f : ℕ) (rest : List Bool), (ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)f:ℕrest:List Boolhf:∀ (d : Fin n), (ch d).size ≤ fn:ℕihn:∀ (ch : Fin n → R.Term),
(∀ (d : Fin n) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')) →
(∀ (d : Fin n), (ch d).size ≤ f) →
parseChildren (R.parseAux f) n ((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) = some (ch, rest)ch:Fin n.succ → R.Termihch:∀ (d : Fin n.succ) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')hch:∀ (d : Fin n.succ), (ch d).size ≤ f⊢ (match some (ch 0, (List.ofFn fun i ↦ R.spell (ch i.succ)).flatten ++ rest) with
| none => none
| some (t, w₁) =>
match parseChildren (R.parseAux f) n w₁ with
| none => none
| some (f, w₂) => some (Fin.cons t f, w₂)) =
some (ch, rest)] step R:RankedAlphabetn✝:ℕch✝:Fin n → R.Termih:∀ (d : Fin n) (f : ℕ) (rest : List Bool), (ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)f:ℕrest:List Boolhf:∀ (d : Fin n), (ch d).size ≤ fn:ℕihn:∀ (ch : Fin n → R.Term),
(∀ (d : Fin n) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')) →
(∀ (d : Fin n), (ch d).size ≤ f) →
parseChildren (R.parseAux f) n ((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) = some (ch, rest)ch:Fin n.succ → R.Termihch:∀ (d : Fin n.succ) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')hch:∀ (d : Fin n.succ), (ch d).size ≤ f⊢ (match some (ch 0, (List.ofFn fun i ↦ R.spell (ch i.succ)).flatten ++ rest) with
| none => none
| some (t, w₁) =>
match parseChildren (R.parseAux f) n w₁ with
| none => none
| some (f, w₂) => some (Fin.cons t f, w₂)) =
some (ch, rest)
simp only [] step R:RankedAlphabetn✝:ℕch✝:Fin n → R.Termih:∀ (d : Fin n) (f : ℕ) (rest : List Bool), (ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)f:ℕrest:List Boolhf:∀ (d : Fin n), (ch d).size ≤ fn:ℕihn:∀ (ch : Fin n → R.Term),
(∀ (d : Fin n) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')) →
(∀ (d : Fin n), (ch d).size ≤ f) →
parseChildren (R.parseAux f) n ((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) = some (ch, rest)ch:Fin n.succ → R.Termihch:∀ (d : Fin n.succ) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')hch:∀ (d : Fin n.succ), (ch d).size ≤ f⊢ (match parseChildren (R.parseAux f) n ((List.ofFn fun i ↦ R.spell (ch i.succ)).flatten ++ rest) with
| none => none
| some (f, w₂) => some (Fin.cons (ch 0) f, w₂)) =
some (ch, rest)
rw [ihn (fun i ↦ ch i.succ) (fun d ↦ ihch d.succ) (fun d ↦ hch d.succ) step R:RankedAlphabetn✝:ℕch✝:Fin n → R.Termih:∀ (d : Fin n) (f : ℕ) (rest : List Bool), (ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)f:ℕrest:List Boolhf:∀ (d : Fin n), (ch d).size ≤ fn:ℕihn:∀ (ch : Fin n → R.Term),
(∀ (d : Fin n) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')) →
(∀ (d : Fin n), (ch d).size ≤ f) →
parseChildren (R.parseAux f) n ((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) = some (ch, rest)ch:Fin n.succ → R.Termihch:∀ (d : Fin n.succ) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')hch:∀ (d : Fin n.succ), (ch d).size ≤ f⊢ (match some (fun i ↦ ch i.succ, rest) with
| none => none
| some (f, w₂) => some (Fin.cons (ch 0) f, w₂)) =
some (ch, rest)] step R:RankedAlphabetn✝:ℕch✝:Fin n → R.Termih:∀ (d : Fin n) (f : ℕ) (rest : List Bool), (ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)f:ℕrest:List Boolhf:∀ (d : Fin n), (ch d).size ≤ fn:ℕihn:∀ (ch : Fin n → R.Term),
(∀ (d : Fin n) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')) →
(∀ (d : Fin n), (ch d).size ≤ f) →
parseChildren (R.parseAux f) n ((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) = some (ch, rest)ch:Fin n.succ → R.Termihch:∀ (d : Fin n.succ) (f' : ℕ) (rest' : List Bool),
(ch d).size ≤ f' → R.parseAux f' (R.spell (ch d) ++ rest') = some (ch d, rest')hch:∀ (d : Fin n.succ), (ch d).size ≤ f⊢ (match some (fun i ↦ ch i.succ, rest) with
| none => none
| some (f, w₂) => some (Fin.cons (ch 0) f, w₂)) =
some (ch, rest)
exact congrArg (fun g ↦ some (g, rest)) (Fin.cons_self_tail ch) All goals completed! 🐙The descent inverts the spelling on spelled input, given fuel at least the term's node count, and returns the unconsumed remainder.
theorem parseAux_spell (R : RankedAlphabet) (t : R.Term) :
∀ (f : ℕ) (rest : List Bool), t.size ≤ f →
R.parseAux f (R.spell t ++ rest) = some (t, rest) :=
Term.induction
(motive := fun t ↦ ∀ (f : ℕ) (rest : List Bool), t.size ≤ f →
R.parseAux f (R.spell t ++ rest) = some (t, rest))
(fun i ch ih f rest hf ↦ by R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (f : ℕ) (rest : List Bool),
(ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)f:ℕrest:List Boolhf:(Term.mk R i ch).size ≤ f⊢ R.parseAux f (R.spell (Term.mk R i ch) ++ rest) = some (Term.mk R i ch, rest)
cases f with
| zero => zero R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (f : ℕ) (rest : List Bool),
(ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)rest:List Boolhf:(Term.mk R i ch).size ≤ 0⊢ R.parseAux 0 (R.spell (Term.mk R i ch) ++ rest) = some (Term.mk R i ch, rest) exact absurd hf (by R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (f : ℕ) (rest : List Bool),
(ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)rest:List Boolhf:(Term.mk R i ch).size ≤ 0⊢ ¬(Term.mk R i ch).size ≤ 0 rw [size_mk R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (f : ℕ) (rest : List Bool),
(ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)rest:List Boolhf:(Term.mk R i ch).size ≤ 0⊢ ¬(List.ofFn fun d ↦ (ch d).size).sum + 1 ≤ 0] R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (f : ℕ) (rest : List Bool),
(ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)rest:List Boolhf:(Term.mk R i ch).size ≤ 0⊢ ¬(List.ofFn fun d ↦ (ch d).size).sum + 1 ≤ 0; exact Nat.not_succ_le_zero _ All goals completed! 🐙)
| succ f => succ R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (f : ℕ) (rest : List Bool),
(ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)rest:List Boolf:ℕhf:(Term.mk R i ch).size ≤ f + 1⊢ R.parseAux (f + 1) (R.spell (Term.mk R i ch) ++ rest) = some (Term.mk R i ch, rest)
rw [parseAux_succ, succ R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (f : ℕ) (rest : List Bool),
(ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)rest:List Boolf:ℕhf:(Term.mk R i ch).size ≤ f + 1⊢ R.parseStep (R.parseAux f) (R.spell (Term.mk R i ch) ++ rest) = some (Term.mk R i ch, rest) spell_mk, succ R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (f : ℕ) (rest : List Bool),
(ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)rest:List Boolf:ℕhf:(Term.mk R i ch).size ≤ f + 1⊢ R.parseStep (R.parseAux f) (R.code i ++ (List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) =
some (Term.mk R i ch, rest) List.append_assoc, succ R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (f : ℕ) (rest : List Bool),
(ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)rest:List Boolf:ℕhf:(Term.mk R i ch).size ≤ f + 1⊢ R.parseStep (R.parseAux f) (R.code i ++ ((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest)) =
some (Term.mk R i ch, rest) parseStep, succ R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (f : ℕ) (rest : List Bool),
(ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)rest:List Boolf:ℕhf:(Term.mk R i ch).size ≤ f + 1⊢ (match R.decodeBlock (R.code i ++ ((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest)) with
| none => none
| some (i, rest) =>
match parseChildren (R.parseAux f) (R.arity i) rest with
| none => none
| some (f, rest') => some (Term.mk R i f, rest')) =
some (Term.mk R i ch, rest)
decodeBlock_code_append succ R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (f : ℕ) (rest : List Bool),
(ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)rest:List Boolf:ℕhf:(Term.mk R i ch).size ≤ f + 1⊢ (match some (i, (List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) with
| none => none
| some (i, rest) =>
match parseChildren (R.parseAux f) (R.arity i) rest with
| none => none
| some (f, rest') => some (Term.mk R i f, rest')) =
some (Term.mk R i ch, rest)] succ R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (f : ℕ) (rest : List Bool),
(ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)rest:List Boolf:ℕhf:(Term.mk R i ch).size ≤ f + 1⊢ (match some (i, (List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) with
| none => none
| some (i, rest) =>
match parseChildren (R.parseAux f) (R.arity i) rest with
| none => none
| some (f, rest') => some (Term.mk R i f, rest')) =
some (Term.mk R i ch, rest)
have hchild : ∀ d, (ch d).size ≤ f := by R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (f : ℕ) (rest : List Bool),
(ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)f:ℕrest:List Boolhf:(Term.mk R i ch).size ≤ f⊢ R.parseAux f (R.spell (Term.mk R i ch) ++ rest) = some (Term.mk R i ch, rest)
intro d R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (f : ℕ) (rest : List Bool),
(ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)rest:List Boolf:ℕhf:(Term.mk R i ch).size ≤ f + 1d:Fin (R.arity i)⊢ (ch d).size ≤ f
rw [size_mk R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (f : ℕ) (rest : List Bool),
(ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)rest:List Boolf:ℕhf:(List.ofFn fun d ↦ (ch d).size).sum + 1 ≤ f + 1d:Fin (R.arity i)⊢ (ch d).size ≤ f] at hf R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (f : ℕ) (rest : List Bool),
(ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)rest:List Boolf:ℕhf:(List.ofFn fun d ↦ (ch d).size).sum + 1 ≤ f + 1d:Fin (R.arity i)⊢ (ch d).size ≤ f
have := size_le_sum_ofFn ch d R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (f : ℕ) (rest : List Bool),
(ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)rest:List Boolf:ℕhf:(List.ofFn fun d ↦ (ch d).size).sum + 1 ≤ f + 1d:Fin (R.arity i)this:(ch d).size ≤ (List.ofFn fun e ↦ (ch e).size).sum⊢ (ch d).size ≤ f
omega succ R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (f : ℕ) (rest : List Bool),
(ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)rest:List Boolf:ℕhf:(Term.mk R i ch).size ≤ f + 1hchild:∀ (d : Fin (R.arity i)), (ch d).size ≤ f⊢ (match some (i, (List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) with
| none => none
| some (i, rest) =>
match parseChildren (R.parseAux f) (R.arity i) rest with
| none => none
| some (f, rest') => some (Term.mk R i f, rest')) =
some (Term.mk R i ch, rest)
simp only [] succ R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (f : ℕ) (rest : List Bool),
(ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)rest:List Boolf:ℕhf:(Term.mk R i ch).size ≤ f + 1hchild:∀ (d : Fin (R.arity i)), (ch d).size ≤ f⊢ (match parseChildren (R.parseAux f) (R.arity i) ((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest) with
| none => none
| some (f, rest') => some (Term.mk R i f, rest')) =
some (Term.mk R i ch, rest)
rw [parseChildren_flatten R ch ih f rest hchild succ R:RankedAlphabett:R.Termi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (f : ℕ) (rest : List Bool),
(ch d).size ≤ f → R.parseAux f (R.spell (ch d) ++ rest) = some (ch d, rest)rest:List Boolf:ℕhf:(Term.mk R i ch).size ≤ f + 1hchild:∀ (d : Fin (R.arity i)), (ch d).size ≤ f⊢ (match some (ch, rest) with
| none => none
| some (f, rest') => some (Term.mk R i f, rest')) =
some (Term.mk R i ch, rest)] All goals completed! 🐙) tThe retraction law: the descent recovers every term the encoding spells.
theorem parse_spell (R : RankedAlphabet) (t : R.Term) :
R.parse (R.spell t) = some t := by R:RankedAlphabett:R.Term⊢ R.parse (R.spell t) = some t
have hf : t.size ≤ (R.spell t).length := by
rw [length_spell R:RankedAlphabett:R.Term⊢ t.size ≤ R.width * t.size] R:RankedAlphabett:R.Term⊢ t.size ≤ R.width * t.size
exact Nat.le_mul_of_pos_left _ R.width_pos R:RankedAlphabett:R.Termhf:t.size ≤ (R.spell t).length⊢ R.parse (R.spell t) = some t
have h := parseAux_spell R t (R.spell t).length [] hf R:RankedAlphabett:R.Termhf:t.size ≤ (R.spell t).lengthh:R.parseAux (R.spell t).length (R.spell t ++ []) = some (t, [])⊢ R.parse (R.spell t) = some t
rw [List.append_nil R:RankedAlphabett:R.Termhf:t.size ≤ (R.spell t).lengthh:R.parseAux (R.spell t).length (R.spell t) = some (t, [])⊢ R.parse (R.spell t) = some t] at h R:RankedAlphabett:R.Termhf:t.size ≤ (R.spell t).lengthh:R.parseAux (R.spell t).length (R.spell t) = some (t, [])⊢ R.parse (R.spell t) = some t
rw [parse, R:RankedAlphabett:R.Termhf:t.size ≤ (R.spell t).lengthh:R.parseAux (R.spell t).length (R.spell t) = some (t, [])⊢ (match R.parseAux (R.spell t).length (R.spell t) with
| some (t, []) => some t
| x => none) =
some t h R:RankedAlphabett:R.Termhf:t.size ≤ (R.spell t).lengthh:R.parseAux (R.spell t).length (R.spell t) = some (t, [])⊢ (match some (t, []) with
| some (t, []) => some t
| x => none) =
some t] All goals completed! 🐙
The encoding and its descent as a Computability.Encoding, whose three
fields they are.
@[expose] def encoding (R : RankedAlphabet) : Computability.Encoding R.Term Bool where
encode := R.spell
decode := R.parse
decode_encode := parse_spell RDistinct terms have distinct spellings.
theorem spell_injective (R : RankedAlphabet) : Function.Injective R.spell :=
(R.encoding).encode_injectiveWhatever a block read returns, it reads that symbol's block.
theorem decodeBlock_eq_some (R : RankedAlphabet) {w rest : List Bool}
{i : Fin R.card} (h : R.decodeBlock w = some (i, rest)) :
R.code i ++ rest = w := by R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardh:R.decodeBlock w = some (i, rest)⊢ R.code i ++ rest = w
rw [decodeBlock R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardh:(if h : decodeBits (List.take R.width w) < R.card ∧ R.width ≤ w.length then
some (⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w)
else none) =
some (i, rest)⊢ R.code i ++ rest = w] at h R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardh:(if h : decodeBits (List.take R.width w) < R.card ∧ R.width ≤ w.length then
some (⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w)
else none) =
some (i, rest)⊢ R.code i ++ rest = w
split at h isTrue R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardh✝:decodeBits (List.take R.width w) < R.card ∧ R.width ≤ w.lengthh:some (⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = some (i, rest)⊢ R.code i ++ rest = wisFalse R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardh✝:¬(decodeBits (List.take R.width w) < R.card ∧ R.width ≤ w.length)h:none = some (i, rest)⊢ R.code i ++ rest = w
· isTrue R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardh✝:decodeBits (List.take R.width w) < R.card ∧ R.width ≤ w.lengthh:some (⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = some (i, rest)⊢ R.code i ++ rest = w rename_i hcond isTrue R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardhcond:decodeBits (List.take R.width w) < R.card ∧ R.width ≤ w.lengthh:some (⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = some (i, rest)⊢ R.code i ++ rest = w
have heq := Option.some.inj h isTrue R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardhcond:decodeBits (List.take R.width w) < R.card ∧ R.width ≤ w.lengthh:some (⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = some (i, rest)heq:(⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = (i, rest)⊢ R.code i ++ rest = w
have hi : decodeBits (w.take R.width) = i.val :=
congrArg (fun p ↦ (Prod.fst p).val) heq isTrue R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardhcond:decodeBits (List.take R.width w) < R.card ∧ R.width ≤ w.lengthh:some (⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = some (i, rest)heq:(⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = (i, rest)hi:decodeBits (List.take R.width w) = ↑i⊢ R.code i ++ rest = w
have hrest : w.drop R.width = rest := congrArg Prod.snd heq isTrue R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardhcond:decodeBits (List.take R.width w) < R.card ∧ R.width ≤ w.lengthh:some (⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = some (i, rest)heq:(⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = (i, rest)hi:decodeBits (List.take R.width w) = ↑ihrest:List.drop R.width w = rest⊢ R.code i ++ rest = w
have hlen : (R.code i).length = (w.take R.width).length := by
rw [length_code, R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardhcond:decodeBits (List.take R.width w) < R.card ∧ R.width ≤ w.lengthh:some (⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = some (i, rest)heq:(⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = (i, rest)hi:decodeBits (List.take R.width w) = ↑ihrest:List.drop R.width w = rest⊢ R.width = (List.take R.width w).length List.length_take R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardhcond:decodeBits (List.take R.width w) < R.card ∧ R.width ≤ w.lengthh:some (⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = some (i, rest)heq:(⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = (i, rest)hi:decodeBits (List.take R.width w) = ↑ihrest:List.drop R.width w = rest⊢ R.width = min R.width w.length] R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardhcond:decodeBits (List.take R.width w) < R.card ∧ R.width ≤ w.lengthh:some (⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = some (i, rest)heq:(⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = (i, rest)hi:decodeBits (List.take R.width w) = ↑ihrest:List.drop R.width w = rest⊢ R.width = min R.width w.length
omega isTrue R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardhcond:decodeBits (List.take R.width w) < R.card ∧ R.width ≤ w.lengthh:some (⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = some (i, rest)heq:(⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = (i, rest)hi:decodeBits (List.take R.width w) = ↑ihrest:List.drop R.width w = resthlen:(R.code i).length = (List.take R.width w).length⊢ R.code i ++ rest = w
have hcode : R.code i = w.take R.width := by
refine List.ext_getElem hlen ?_ R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardhcond:decodeBits (List.take R.width w) < R.card ∧ R.width ≤ w.lengthh:some (⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = some (i, rest)heq:(⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = (i, rest)hi:decodeBits (List.take R.width w) = ↑ihrest:List.drop R.width w = resthlen:(R.code i).length = (List.take R.width w).length⊢ ∀ (i_1 : ℕ) (h₁ : i_1 < (R.code i).length) (h₂ : i_1 < (List.take R.width w).length),
(R.code i)[i_1] = (List.take R.width w)[i_1]
intro n h₁ h₂ R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardhcond:decodeBits (List.take R.width w) < R.card ∧ R.width ≤ w.lengthh:some (⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = some (i, rest)heq:(⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = (i, rest)hi:decodeBits (List.take R.width w) = ↑ihrest:List.drop R.width w = resthlen:(R.code i).length = (List.take R.width w).lengthn:ℕh₁:n < (R.code i).lengthh₂:n < (List.take R.width w).length⊢ (R.code i)[n] = (List.take R.width w)[n]
rw [getElem_code_eq R i n h₁, R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardhcond:decodeBits (List.take R.width w) < R.card ∧ R.width ≤ w.lengthh:some (⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = some (i, rest)heq:(⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = (i, rest)hi:decodeBits (List.take R.width w) = ↑ihrest:List.drop R.width w = resthlen:(R.code i).length = (List.take R.width w).lengthn:ℕh₁:n < (R.code i).lengthh₂:n < (List.take R.width w).length⊢ (↑i).testBit n = (List.take R.width w)[n] ← hi, R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardhcond:decodeBits (List.take R.width w) < R.card ∧ R.width ≤ w.lengthh:some (⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = some (i, rest)heq:(⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = (i, rest)hi:decodeBits (List.take R.width w) = ↑ihrest:List.drop R.width w = resthlen:(R.code i).length = (List.take R.width w).lengthn:ℕh₁:n < (R.code i).lengthh₂:n < (List.take R.width w).length⊢ (decodeBits (List.take R.width w)).testBit n = (List.take R.width w)[n]
testBit_decodeBits (w.take R.width) n h₂ R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardhcond:decodeBits (List.take R.width w) < R.card ∧ R.width ≤ w.lengthh:some (⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = some (i, rest)heq:(⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = (i, rest)hi:decodeBits (List.take R.width w) = ↑ihrest:List.drop R.width w = resthlen:(R.code i).length = (List.take R.width w).lengthn:ℕh₁:n < (R.code i).lengthh₂:n < (List.take R.width w).length⊢ (List.take R.width w)[n] = (List.take R.width w)[n]] isTrue R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardhcond:decodeBits (List.take R.width w) < R.card ∧ R.width ≤ w.lengthh:some (⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = some (i, rest)heq:(⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = (i, rest)hi:decodeBits (List.take R.width w) = ↑ihrest:List.drop R.width w = resthlen:(R.code i).length = (List.take R.width w).lengthhcode:R.code i = List.take R.width w⊢ R.code i ++ rest = w
rw [hcode, isTrue R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardhcond:decodeBits (List.take R.width w) < R.card ∧ R.width ≤ w.lengthh:some (⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = some (i, rest)heq:(⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = (i, rest)hi:decodeBits (List.take R.width w) = ↑ihrest:List.drop R.width w = resthlen:(R.code i).length = (List.take R.width w).lengthhcode:R.code i = List.take R.width w⊢ List.take R.width w ++ rest = w ← hrest, isTrue R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardhcond:decodeBits (List.take R.width w) < R.card ∧ R.width ≤ w.lengthh:some (⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = some (i, rest)heq:(⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = (i, rest)hi:decodeBits (List.take R.width w) = ↑ihrest:List.drop R.width w = resthlen:(R.code i).length = (List.take R.width w).lengthhcode:R.code i = List.take R.width w⊢ List.take R.width w ++ List.drop R.width w = w List.take_append_drop isTrue R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardhcond:decodeBits (List.take R.width w) < R.card ∧ R.width ≤ w.lengthh:some (⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = some (i, rest)heq:(⟨decodeBits (List.take R.width w), ⋯⟩, List.drop R.width w) = (i, rest)hi:decodeBits (List.take R.width w) = ↑ihrest:List.drop R.width w = resthlen:(R.code i).length = (List.take R.width w).lengthhcode:R.code i = List.take R.width w⊢ w = w] All goals completed! 🐙
· isFalse R:RankedAlphabetw:List Boolrest:List Booli:Fin R.cardh✝:¬(decodeBits (List.take R.width w) < R.card ∧ R.width ≤ w.length)h:none = some (i, rest)⊢ R.code i ++ rest = w contradiction All goals completed! 🐙
Whatever parseChildren returns, it reads the concatenation of the
children's spellings.
theorem parseChildren_eq_some (R : RankedAlphabet) (f : ℕ)
(ih : ∀ w t rest, R.parseAux f w = some (t, rest) → R.spell t ++ rest = w) :
∀ (n : ℕ) (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) →
(List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = w :=
Nat.rec
(fun w g rest h ↦ by R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolg:Fin Nat.zero → R.Termrest:List Boolh:parseChildren (R.parseAux f) Nat.zero w = some (g, rest)⊢ (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = w
simp only [parseChildren] at h R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolg:Fin Nat.zero → R.Termrest:List Boolh:some (Fin.elim0, w) = some (g, rest)⊢ (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = w
have heq := Option.some.inj h R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolg:Fin Nat.zero → R.Termrest:List Boolh:some (Fin.elim0, w) = some (g, rest)heq:(Fin.elim0, w) = (g, rest)⊢ (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = w
have hw : w = rest := congrArg Prod.snd heq R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolg:Fin Nat.zero → R.Termrest:List Boolh:some (Fin.elim0, w) = some (g, rest)heq:(Fin.elim0, w) = (g, rest)hw:w = rest⊢ (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = w
rw [List.ofFn_zero, R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolg:Fin Nat.zero → R.Termrest:List Boolh:some (Fin.elim0, w) = some (g, rest)heq:(Fin.elim0, w) = (g, rest)hw:w = rest⊢ [].flatten ++ rest = w List.flatten_nil, R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolg:Fin Nat.zero → R.Termrest:List Boolh:some (Fin.elim0, w) = some (g, rest)heq:(Fin.elim0, w) = (g, rest)hw:w = rest⊢ [] ++ rest = w List.nil_append, R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolg:Fin Nat.zero → R.Termrest:List Boolh:some (Fin.elim0, w) = some (g, rest)heq:(Fin.elim0, w) = (g, rest)hw:w = rest⊢ rest = w hw R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolg:Fin Nat.zero → R.Termrest:List Boolh:some (Fin.elim0, w) = some (g, rest)heq:(Fin.elim0, w) = (g, rest)hw:w = rest⊢ rest = rest] All goals completed! 🐙)
(fun n ihn w g rest h ↦ by R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolg:Fin n.succ → R.Termrest:List Boolh:parseChildren (R.parseAux f) n.succ w = some (g, rest)⊢ (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = w
rw [parseChildren_succ R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolg:Fin n.succ → R.Termrest:List Boolh:(match R.parseAux f w with
| none => none
| some (t, w₁) =>
match parseChildren (R.parseAux f) n w₁ with
| none => none
| some (f, w₂) => some (Fin.cons t f, w₂)) =
some (g, rest)⊢ (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = w] at h R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolg:Fin n.succ → R.Termrest:List Boolh:(match R.parseAux f w with
| none => none
| some (t, w₁) =>
match parseChildren (R.parseAux f) n w₁ with
| none => none
| some (f, w₂) => some (Fin.cons t f, w₂)) =
some (g, rest)⊢ (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = w
split at h h_1 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolg:Fin n.succ → R.Termrest:List Boolx✝:Option (R.Term × List Bool)heq✝:R.parseAux f w = noneh:none = some (g, rest)⊢ (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = wh_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolg:Fin n.succ → R.Termrest:List Boolx✝:Option (R.Term × List Bool)t✝:R.Termw₁✝:List Boolheq✝:R.parseAux f w = some (t✝, w₁✝)h:(match parseChildren (R.parseAux f) n w₁✝ with
| none => none
| some (f, w₂) => some (Fin.cons t✝ f, w₂)) =
some (g, rest)⊢ (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = w
· h_1 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolg:Fin n.succ → R.Termrest:List Boolx✝:Option (R.Term × List Bool)heq✝:R.parseAux f w = noneh:none = some (g, rest)⊢ (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = w contradiction All goals completed! 🐙
· h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolg:Fin n.succ → R.Termrest:List Boolx✝:Option (R.Term × List Bool)t✝:R.Termw₁✝:List Boolheq✝:R.parseAux f w = some (t✝, w₁✝)h:(match parseChildren (R.parseAux f) n w₁✝ with
| none => none
| some (f, w₂) => some (Fin.cons t✝ f, w₂)) =
some (g, rest)⊢ (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = w rename_i t w₁ h₁ h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolg:Fin n.succ → R.Termrest:List Boolx✝:Option (R.Term × List Bool)t:R.Termw₁:List Boolh₁:R.parseAux f w = some (t✝, w₁✝)h:(match parseChildren (R.parseAux f) n w₁✝ with
| none => none
| some (f, w₂) => some (Fin.cons t✝ f, w₂)) =
some (g, rest)⊢ (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = w
split at h h_1 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolg:Fin n.succ → R.Termrest:List Boolx✝¹:Option (R.Term × List Bool)t:R.Termw₁:List Boolh₁:R.parseAux f w = some (t✝, w₁✝)x✝:Option ((Fin n → R.Term) × List Bool)heq✝:parseChildren (R.parseAux f) n w₁ = noneh:none = some (g, rest)⊢ (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = wh_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolg:Fin n.succ → R.Termrest:List Boolx✝¹:Option (R.Term × List Bool)t:R.Termw₁:List Boolh₁:R.parseAux f w = some (t✝, w₁✝)x✝:Option ((Fin n → R.Term) × List Bool)f✝:Fin n → R.Termw₂✝:List Boolheq✝:parseChildren (R.parseAux f) n w₁ = some (f✝, w₂✝)h:some (Fin.cons t f✝, w₂✝) = some (g, rest)⊢ (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = w
· h_1 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolg:Fin n.succ → R.Termrest:List Boolx✝¹:Option (R.Term × List Bool)t:R.Termw₁:List Boolh₁:R.parseAux f w = some (t✝, w₁✝)x✝:Option ((Fin n → R.Term) × List Bool)heq✝:parseChildren (R.parseAux f) n w₁ = noneh:none = some (g, rest)⊢ (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = w contradiction All goals completed! 🐙
· h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolg:Fin n.succ → R.Termrest:List Boolx✝¹:Option (R.Term × List Bool)t:R.Termw₁:List Boolh₁:R.parseAux f w = some (t✝, w₁✝)x✝:Option ((Fin n → R.Term) × List Bool)f✝:Fin n → R.Termw₂✝:List Boolheq✝:parseChildren (R.parseAux f) n w₁ = some (f✝, w₂✝)h:some (Fin.cons t f✝, w₂✝) = some (g, rest)⊢ (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = w rename_i g' w₂ h₂ h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolg:Fin n.succ → R.Termrest:List Boolx✝¹:Option (R.Term × List Bool)t:R.Termw₁:List Boolh₁:R.parseAux f w = some (t✝, w₁✝)x✝:Option ((Fin n → R.Term) × List Bool)g':Fin n → R.Termw₂:List Boolh₂:parseChildren (R.parseAux f) n w₁ = some (f✝, w₂✝)h:some (Fin.cons t f✝, w₂✝) = some (g, rest)⊢ (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = w
have heq := Option.some.inj h h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolg:Fin n.succ → R.Termrest:List Boolx✝¹:Option (R.Term × List Bool)t:R.Termw₁:List Boolh₁:R.parseAux f w = some (t✝, w₁✝)x✝:Option ((Fin n → R.Term) × List Bool)g':Fin n → R.Termw₂:List Boolh₂:parseChildren (R.parseAux f) n w₁ = some (f✝, w₂✝)h:some (Fin.cons t f✝, w₂✝) = some (g, rest)heq:(Fin.cons t g', w₂) = (g, rest)⊢ (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = w
have hg : Fin.cons t g' = g := congrArg Prod.fst heq h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolg:Fin n.succ → R.Termrest:List Boolx✝¹:Option (R.Term × List Bool)t:R.Termw₁:List Boolh₁:R.parseAux f w = some (t✝, w₁✝)x✝:Option ((Fin n → R.Term) × List Bool)g':Fin n → R.Termw₂:List Boolh₂:parseChildren (R.parseAux f) n w₁ = some (f✝, w₂✝)h:some (Fin.cons t f✝, w₂✝) = some (g, rest)heq:(Fin.cons t g', w₂) = (g, rest)hg:Fin.cons t g' = g⊢ (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = w
have hrest : w₂ = rest := congrArg Prod.snd heq h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolg:Fin n.succ → R.Termrest:List Boolx✝¹:Option (R.Term × List Bool)t:R.Termw₁:List Boolh₁:R.parseAux f w = some (t✝, w₁✝)x✝:Option ((Fin n → R.Term) × List Bool)g':Fin n → R.Termw₂:List Boolh₂:parseChildren (R.parseAux f) n w₁ = some (f✝, w₂✝)h:some (Fin.cons t f✝, w₂✝) = some (g, rest)heq:(Fin.cons t g', w₂) = (g, rest)hg:Fin.cons t g' = ghrest:w₂ = rest⊢ (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = w
subst hg h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolrest:List Boolx✝¹:Option (R.Term × List Bool)t:R.Termw₁:List Boolh₁:R.parseAux f w = some (t✝, w₁✝)x✝:Option ((Fin n → R.Term) × List Bool)g':Fin n → R.Termw₂:List Boolh₂:parseChildren (R.parseAux f) n w₁ = some (f✝, w₂✝)hrest:w₂ = resth:some (Fin.cons t g', w₂) = some (Fin.cons t g', rest)heq:(Fin.cons t g', w₂) = (Fin.cons t g', rest)⊢ (List.ofFn fun d ↦ R.spell (Fin.cons t g' d)).flatten ++ rest = w
rw [List.ofFn_succ, h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolrest:List Boolx✝¹:Option (R.Term × List Bool)t:R.Termw₁:List Boolh₁:R.parseAux f w = some (t✝, w₁✝)x✝:Option ((Fin n → R.Term) × List Bool)g':Fin n → R.Termw₂:List Boolh₂:parseChildren (R.parseAux f) n w₁ = some (f✝, w₂✝)hrest:w₂ = resth:some (Fin.cons t g', w₂) = some (Fin.cons t g', rest)heq:(Fin.cons t g', w₂) = (Fin.cons t g', rest)⊢ (R.spell (Fin.cons t g' 0) :: List.ofFn fun i ↦ R.spell (Fin.cons t g' i.succ)).flatten ++ rest = w List.flatten_cons, h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolrest:List Boolx✝¹:Option (R.Term × List Bool)t:R.Termw₁:List Boolh₁:R.parseAux f w = some (t✝, w₁✝)x✝:Option ((Fin n → R.Term) × List Bool)g':Fin n → R.Termw₂:List Boolh₂:parseChildren (R.parseAux f) n w₁ = some (f✝, w₂✝)hrest:w₂ = resth:some (Fin.cons t g', w₂) = some (Fin.cons t g', rest)heq:(Fin.cons t g', w₂) = (Fin.cons t g', rest)⊢ R.spell (Fin.cons t g' 0) ++ (List.ofFn fun i ↦ R.spell (Fin.cons t g' i.succ)).flatten ++ rest = w List.append_assoc, h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolrest:List Boolx✝¹:Option (R.Term × List Bool)t:R.Termw₁:List Boolh₁:R.parseAux f w = some (t✝, w₁✝)x✝:Option ((Fin n → R.Term) × List Bool)g':Fin n → R.Termw₂:List Boolh₂:parseChildren (R.parseAux f) n w₁ = some (f✝, w₂✝)hrest:w₂ = resth:some (Fin.cons t g', w₂) = some (Fin.cons t g', rest)heq:(Fin.cons t g', w₂) = (Fin.cons t g', rest)⊢ R.spell (Fin.cons t g' 0) ++ ((List.ofFn fun i ↦ R.spell (Fin.cons t g' i.succ)).flatten ++ rest) = w ← hrest h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolrest:List Boolx✝¹:Option (R.Term × List Bool)t:R.Termw₁:List Boolh₁:R.parseAux f w = some (t✝, w₁✝)x✝:Option ((Fin n → R.Term) × List Bool)g':Fin n → R.Termw₂:List Boolh₂:parseChildren (R.parseAux f) n w₁ = some (f✝, w₂✝)hrest:w₂ = resth:some (Fin.cons t g', w₂) = some (Fin.cons t g', rest)heq:(Fin.cons t g', w₂) = (Fin.cons t g', rest)⊢ R.spell (Fin.cons t g' 0) ++ ((List.ofFn fun i ↦ R.spell (Fin.cons t g' i.succ)).flatten ++ w₂) = w] h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolrest:List Boolx✝¹:Option (R.Term × List Bool)t:R.Termw₁:List Boolh₁:R.parseAux f w = some (t✝, w₁✝)x✝:Option ((Fin n → R.Term) × List Bool)g':Fin n → R.Termw₂:List Boolh₂:parseChildren (R.parseAux f) n w₁ = some (f✝, w₂✝)hrest:w₂ = resth:some (Fin.cons t g', w₂) = some (Fin.cons t g', rest)heq:(Fin.cons t g', w₂) = (Fin.cons t g', rest)⊢ R.spell (Fin.cons t g' 0) ++ ((List.ofFn fun i ↦ R.spell (Fin.cons t g' i.succ)).flatten ++ w₂) = w
simp only [Fin.cons_zero, Fin.cons_succ] h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolrest:List Boolx✝¹:Option (R.Term × List Bool)t:R.Termw₁:List Boolh₁:R.parseAux f w = some (t✝, w₁✝)x✝:Option ((Fin n → R.Term) × List Bool)g':Fin n → R.Termw₂:List Boolh₂:parseChildren (R.parseAux f) n w₁ = some (f✝, w₂✝)hrest:w₂ = resth:some (Fin.cons t g', w₂) = some (Fin.cons t g', rest)heq:(Fin.cons t g', w₂) = (Fin.cons t g', rest)⊢ R.spell t ++ ((List.ofFn fun i ↦ R.spell (g' i)).flatten ++ w₂) = w
rw [ihn w₁ g' w₂ h₂, h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolrest:List Boolx✝¹:Option (R.Term × List Bool)t:R.Termw₁:List Boolh₁:R.parseAux f w = some (t✝, w₁✝)x✝:Option ((Fin n → R.Term) × List Bool)g':Fin n → R.Termw₂:List Boolh₂:parseChildren (R.parseAux f) n w₁ = some (f✝, w₂✝)hrest:w₂ = resth:some (Fin.cons t g', w₂) = some (Fin.cons t g', rest)heq:(Fin.cons t g', w₂) = (Fin.cons t g', rest)⊢ R.spell t ++ w₁ = w ih w t w₁ h₁ h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = wn:ℕihn:∀ (w : List Bool) (g : Fin n → R.Term) (rest : List Bool),
parseChildren (R.parseAux f) n w = some (g, rest) → (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = ww:List Boolrest:List Boolx✝¹:Option (R.Term × List Bool)t:R.Termw₁:List Boolh₁:R.parseAux f w = some (t✝, w₁✝)x✝:Option ((Fin n → R.Term) × List Bool)g':Fin n → R.Termw₂:List Boolh₂:parseChildren (R.parseAux f) n w₁ = some (f✝, w₂✝)hrest:w₂ = resth:some (Fin.cons t g', w₂) = some (Fin.cons t g', rest)heq:(Fin.cons t g', w₂) = (Fin.cons t g', rest)⊢ w = w] All goals completed! 🐙)Whatever the descent reads, it reads a spelling.
theorem parseAux_eq_some (R : RankedAlphabet) :
∀ (f : ℕ) (w : List Bool) (t : R.Term) (rest : List Bool),
R.parseAux f w = some (t, rest) → R.spell t ++ rest = w :=
Nat.rec
(fun _ _ _ h ↦ nomatch h)
(fun f ih w t rest h ↦ by R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolt:R.Termrest:List Boolh:R.parseAux f.succ w = some (t, rest)⊢ R.spell t ++ rest = w
rw [parseAux_succ, R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolt:R.Termrest:List Boolh:R.parseStep (R.parseAux f) w = some (t, rest)⊢ R.spell t ++ rest = w parseStep R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolt:R.Termrest:List Boolh:(match R.decodeBlock w with
| none => none
| some (i, rest) =>
match parseChildren (R.parseAux f) (R.arity i) rest with
| none => none
| some (f, rest') => some (Term.mk R i f, rest')) =
some (t, rest)⊢ R.spell t ++ rest = w] at h R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolt:R.Termrest:List Boolh:(match R.decodeBlock w with
| none => none
| some (i, rest) =>
match parseChildren (R.parseAux f) (R.arity i) rest with
| none => none
| some (f, rest') => some (Term.mk R i f, rest')) =
some (t, rest)⊢ R.spell t ++ rest = w
split at h h_1 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolt:R.Termrest:List Boolx✝:Option (Fin R.card × List Bool)heq✝:R.decodeBlock w = noneh:none = some (t, rest)⊢ R.spell t ++ rest = wh_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolt:R.Termrest:List Boolx✝:Option (Fin R.card × List Bool)i✝:Fin R.cardrest✝:List Boolheq✝:R.decodeBlock w = some (i✝, rest✝)h:(match parseChildren (R.parseAux f) (R.arity i✝) rest✝ with
| none => none
| some (f, rest') => some (Term.mk R i✝ f, rest')) =
some (t, rest)⊢ R.spell t ++ rest = w
· h_1 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolt:R.Termrest:List Boolx✝:Option (Fin R.card × List Bool)heq✝:R.decodeBlock w = noneh:none = some (t, rest)⊢ R.spell t ++ rest = w contradiction All goals completed! 🐙
· h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolt:R.Termrest:List Boolx✝:Option (Fin R.card × List Bool)i✝:Fin R.cardrest✝:List Boolheq✝:R.decodeBlock w = some (i✝, rest✝)h:(match parseChildren (R.parseAux f) (R.arity i✝) rest✝ with
| none => none
| some (f, rest') => some (Term.mk R i✝ f, rest')) =
some (t, rest)⊢ R.spell t ++ rest = w rename_i i rest₁ hb h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolt:R.Termrest:List Boolx✝:Option (Fin R.card × List Bool)i:Fin R.cardrest₁:List Boolhb:R.decodeBlock w = some (i✝, rest✝)h:(match parseChildren (R.parseAux f) (R.arity i✝) rest✝ with
| none => none
| some (f, rest') => some (Term.mk R i✝ f, rest')) =
some (t, rest)⊢ R.spell t ++ rest = w
split at h h_1 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolt:R.Termrest:List Boolx✝¹:Option (Fin R.card × List Bool)i:Fin R.cardrest₁:List Boolhb:R.decodeBlock w = some (i✝, rest✝)x✝:Option ((Fin (R.arity i) → R.Term) × List Bool)heq✝:parseChildren (R.parseAux f) (R.arity i) rest₁ = noneh:none = some (t, rest)⊢ R.spell t ++ rest = wh_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolt:R.Termrest:List Boolx✝¹:Option (Fin R.card × List Bool)i:Fin R.cardrest₁:List Boolhb:R.decodeBlock w = some (i✝, rest✝)x✝:Option ((Fin (R.arity i) → R.Term) × List Bool)f✝:Fin (R.arity i) → R.Termrest'✝:List Boolheq✝:parseChildren (R.parseAux f) (R.arity i) rest₁ = some (f✝, rest'✝)h:some (Term.mk R i f✝, rest'✝) = some (t, rest)⊢ R.spell t ++ rest = w
· h_1 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolt:R.Termrest:List Boolx✝¹:Option (Fin R.card × List Bool)i:Fin R.cardrest₁:List Boolhb:R.decodeBlock w = some (i✝, rest✝)x✝:Option ((Fin (R.arity i) → R.Term) × List Bool)heq✝:parseChildren (R.parseAux f) (R.arity i) rest₁ = noneh:none = some (t, rest)⊢ R.spell t ++ rest = w contradiction All goals completed! 🐙
· h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolt:R.Termrest:List Boolx✝¹:Option (Fin R.card × List Bool)i:Fin R.cardrest₁:List Boolhb:R.decodeBlock w = some (i✝, rest✝)x✝:Option ((Fin (R.arity i) → R.Term) × List Bool)f✝:Fin (R.arity i) → R.Termrest'✝:List Boolheq✝:parseChildren (R.parseAux f) (R.arity i) rest₁ = some (f✝, rest'✝)h:some (Term.mk R i f✝, rest'✝) = some (t, rest)⊢ R.spell t ++ rest = w rename_i g rest₂ hc h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolt:R.Termrest:List Boolx✝¹:Option (Fin R.card × List Bool)i:Fin R.cardrest₁:List Boolhb:R.decodeBlock w = some (i✝, rest✝)x✝:Option ((Fin (R.arity i) → R.Term) × List Bool)g:Fin (R.arity i) → R.Termrest₂:List Boolhc:parseChildren (R.parseAux f) (R.arity i) rest₁ = some (f✝, rest'✝)h:some (Term.mk R i f✝, rest'✝) = some (t, rest)⊢ R.spell t ++ rest = w
have heq := Option.some.inj h h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolt:R.Termrest:List Boolx✝¹:Option (Fin R.card × List Bool)i:Fin R.cardrest₁:List Boolhb:R.decodeBlock w = some (i✝, rest✝)x✝:Option ((Fin (R.arity i) → R.Term) × List Bool)g:Fin (R.arity i) → R.Termrest₂:List Boolhc:parseChildren (R.parseAux f) (R.arity i) rest₁ = some (f✝, rest'✝)h:some (Term.mk R i f✝, rest'✝) = some (t, rest)heq:(Term.mk R i g, rest₂) = (t, rest)⊢ R.spell t ++ rest = w
have ht : Term.mk R i g = t := congrArg Prod.fst heq h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolt:R.Termrest:List Boolx✝¹:Option (Fin R.card × List Bool)i:Fin R.cardrest₁:List Boolhb:R.decodeBlock w = some (i✝, rest✝)x✝:Option ((Fin (R.arity i) → R.Term) × List Bool)g:Fin (R.arity i) → R.Termrest₂:List Boolhc:parseChildren (R.parseAux f) (R.arity i) rest₁ = some (f✝, rest'✝)h:some (Term.mk R i f✝, rest'✝) = some (t, rest)heq:(Term.mk R i g, rest₂) = (t, rest)ht:Term.mk R i g = t⊢ R.spell t ++ rest = w
have hrest : rest₂ = rest := congrArg Prod.snd heq h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolt:R.Termrest:List Boolx✝¹:Option (Fin R.card × List Bool)i:Fin R.cardrest₁:List Boolhb:R.decodeBlock w = some (i✝, rest✝)x✝:Option ((Fin (R.arity i) → R.Term) × List Bool)g:Fin (R.arity i) → R.Termrest₂:List Boolhc:parseChildren (R.parseAux f) (R.arity i) rest₁ = some (f✝, rest'✝)h:some (Term.mk R i f✝, rest'✝) = some (t, rest)heq:(Term.mk R i g, rest₂) = (t, rest)ht:Term.mk R i g = threst:rest₂ = rest⊢ R.spell t ++ rest = w
subst ht h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolrest:List Boolx✝¹:Option (Fin R.card × List Bool)i:Fin R.cardrest₁:List Boolhb:R.decodeBlock w = some (i✝, rest✝)x✝:Option ((Fin (R.arity i) → R.Term) × List Bool)g:Fin (R.arity i) → R.Termrest₂:List Boolhc:parseChildren (R.parseAux f) (R.arity i) rest₁ = some (f✝, rest'✝)hrest:rest₂ = resth:some (Term.mk R i g, rest₂) = some (Term.mk R i g, rest)heq:(Term.mk R i g, rest₂) = (Term.mk R i g, rest)⊢ R.spell (Term.mk R i g) ++ rest = w
rw [spell_mk, h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolrest:List Boolx✝¹:Option (Fin R.card × List Bool)i:Fin R.cardrest₁:List Boolhb:R.decodeBlock w = some (i✝, rest✝)x✝:Option ((Fin (R.arity i) → R.Term) × List Bool)g:Fin (R.arity i) → R.Termrest₂:List Boolhc:parseChildren (R.parseAux f) (R.arity i) rest₁ = some (f✝, rest'✝)hrest:rest₂ = resth:some (Term.mk R i g, rest₂) = some (Term.mk R i g, rest)heq:(Term.mk R i g, rest₂) = (Term.mk R i g, rest)⊢ R.code i ++ (List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest = w List.append_assoc, h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolrest:List Boolx✝¹:Option (Fin R.card × List Bool)i:Fin R.cardrest₁:List Boolhb:R.decodeBlock w = some (i✝, rest✝)x✝:Option ((Fin (R.arity i) → R.Term) × List Bool)g:Fin (R.arity i) → R.Termrest₂:List Boolhc:parseChildren (R.parseAux f) (R.arity i) rest₁ = some (f✝, rest'✝)hrest:rest₂ = resth:some (Term.mk R i g, rest₂) = some (Term.mk R i g, rest)heq:(Term.mk R i g, rest₂) = (Term.mk R i g, rest)⊢ R.code i ++ ((List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest) = w ← hrest, h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolrest:List Boolx✝¹:Option (Fin R.card × List Bool)i:Fin R.cardrest₁:List Boolhb:R.decodeBlock w = some (i✝, rest✝)x✝:Option ((Fin (R.arity i) → R.Term) × List Bool)g:Fin (R.arity i) → R.Termrest₂:List Boolhc:parseChildren (R.parseAux f) (R.arity i) rest₁ = some (f✝, rest'✝)hrest:rest₂ = resth:some (Term.mk R i g, rest₂) = some (Term.mk R i g, rest)heq:(Term.mk R i g, rest₂) = (Term.mk R i g, rest)⊢ R.code i ++ ((List.ofFn fun d ↦ R.spell (g d)).flatten ++ rest₂) = w
parseChildren_eq_some R f ih (R.arity i) rest₁ g rest₂ hc, h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolrest:List Boolx✝¹:Option (Fin R.card × List Bool)i:Fin R.cardrest₁:List Boolhb:R.decodeBlock w = some (i✝, rest✝)x✝:Option ((Fin (R.arity i) → R.Term) × List Bool)g:Fin (R.arity i) → R.Termrest₂:List Boolhc:parseChildren (R.parseAux f) (R.arity i) rest₁ = some (f✝, rest'✝)hrest:rest₂ = resth:some (Term.mk R i g, rest₂) = some (Term.mk R i g, rest)heq:(Term.mk R i g, rest₂) = (Term.mk R i g, rest)⊢ R.code i ++ rest₁ = w
decodeBlock_eq_some R hb h_2 R:RankedAlphabetf:ℕih:∀ (w : List Bool) (t : R.Term) (rest : List Bool), R.parseAux f w = some (t, rest) → R.spell t ++ rest = ww:List Boolrest:List Boolx✝¹:Option (Fin R.card × List Bool)i:Fin R.cardrest₁:List Boolhb:R.decodeBlock w = some (i✝, rest✝)x✝:Option ((Fin (R.arity i) → R.Term) × List Bool)g:Fin (R.arity i) → R.Termrest₂:List Boolhc:parseChildren (R.parseAux f) (R.arity i) rest₁ = some (f✝, rest'✝)hrest:rest₂ = resth:some (Term.mk R i g, rest₂) = some (Term.mk R i g, rest)heq:(Term.mk R i g, rest₂) = (Term.mk R i g, rest)⊢ w = w] All goals completed! 🐙)The descent succeeds exactly on the spellings, returning the term spelled.
theorem parse_eq_some_iff (R : RankedAlphabet) {w : List Bool} {t : R.Term} :
R.parse w = some t ↔ R.spell t = w := by R:RankedAlphabetw:List Boolt:R.Term⊢ R.parse w = some t ↔ R.spell t = w
refine ⟨fun h ↦ ?_, fun h ↦ h ▸ parse_spell R t⟩ R:RankedAlphabetw:List Boolt:R.Termh:R.parse w = some t⊢ R.spell t = w
rw [parse R:RankedAlphabetw:List Boolt:R.Termh:(match R.parseAux w.length w with
| some (t, []) => some t
| x => none) =
some t⊢ R.spell t = w] at h R:RankedAlphabetw:List Boolt:R.Termh:(match R.parseAux w.length w with
| some (t, []) => some t
| x => none) =
some t⊢ R.spell t = w
split at h h_1 R:RankedAlphabetw:List Boolt:R.Termx✝:Option (R.Term × List Bool)t✝:R.Termheq✝:R.parseAux w.length w = some (t✝, [])h:some t✝ = some t⊢ R.spell t = wh_2 R:RankedAlphabetw:List Boolt:R.Termx✝¹:Option (R.Term × List Bool)x✝:∀ (t : R.Term), R.parseAux w.length w = some (t, []) → Falseh:none = some t⊢ R.spell t = w
· h_1 R:RankedAlphabetw:List Boolt:R.Termx✝:Option (R.Term × List Bool)t✝:R.Termheq✝:R.parseAux w.length w = some (t✝, [])h:some t✝ = some t⊢ R.spell t = w rename_i t' hp h_1 R:RankedAlphabetw:List Boolt:R.Termx✝:Option (R.Term × List Bool)t':R.Termhp:R.parseAux w.length w = some (t✝, [])h:some t✝ = some t⊢ R.spell t = w
rw [← Option.some.inj h h_1 R:RankedAlphabetw:List Boolt:R.Termx✝:Option (R.Term × List Bool)t':R.Termhp:R.parseAux w.length w = some (t✝, [])h:some t✝ = some t⊢ R.spell t' = w] h_1 R:RankedAlphabetw:List Boolt:R.Termx✝:Option (R.Term × List Bool)t':R.Termhp:R.parseAux w.length w = some (t✝, [])h:some t✝ = some t⊢ R.spell t' = w
simpa using parseAux_eq_some R w.length w t' [] hp All goals completed! 🐙
· h_2 R:RankedAlphabetw:List Boolt:R.Termx✝¹:Option (R.Term × List Bool)x✝:∀ (t : R.Term), R.parseAux w.length w = some (t, []) → Falseh:none = some t⊢ R.spell t = w contradiction All goals completed! 🐙The state of the validity scan: the bits of an incomplete block, the count of pending subterms, and whether the scan has failed.
The bits of an incomplete block, most recently read first.
The count of pending subterms.
Whether the scan has not yet failed.
@[ext] structure Scan where buf : List Bool depth : ℕ live : BoolOne step of the scan, reading one bit. A failed state absorbs. An incomplete block takes the bit; a complete one is decoded, and its symbol pops its arity and pushes one, failing when the block spells no symbol or the pending count is short of the arity.
@[expose] def scanStep (R : RankedAlphabet) (b : Bool) (s : Scan) : Scan :=
match s.live with
| false => s
| true =>
match decide ((b :: s.buf).length = R.width) with
| false => ⟨b :: s.buf, s.depth, true⟩
| true =>
match R.arOf (decodeBits (b :: s.buf)) with
| none => ⟨[], s.depth, false⟩
| some r =>
match decide (r ≤ s.depth) with
| false => ⟨[], s.depth, false⟩
| true => ⟨[], s.depth - r + 1, true⟩
The scan of a word from a given state. foldr reads the word's last bit
first, which is the processing order of a right-to-left scan.
@[expose] def scanFrom (R : RankedAlphabet) (w : List Bool) (s : Scan) : Scan :=
w.foldr R.scanStep sThe scan of a word from the initial state.
@[expose] def scanFinal (R : RankedAlphabet) (w : List Bool) : Scan :=
R.scanFrom w ⟨[], 0, true⟩Whether a word spells a term: the scan ends live, with no incomplete block and exactly one pending subterm.
@[expose] def validBool (R : RankedAlphabet) (w : List Bool) : Bool :=
(R.scanFinal w).live && (R.scanFinal w).buf.isEmpty && (R.scanFinal w).depth == 1A word spells a term.
@[expose] def Valid (R : RankedAlphabet) (w : List Bool) : Prop :=
R.validBool w = true
Valid is a Bool equation, so membership is decidable. Instance search
does not unfold the def, so the instance is supplied.
instance (R : RankedAlphabet) : DecidablePred R.Valid :=
fun _ ↦ inferInstanceAs (Decidable (_ = true))@[simp] theorem scanFrom_nil (R : RankedAlphabet) (s : Scan) :
R.scanFrom [] s = s := rfl@[simp] theorem scanFrom_cons (R : RankedAlphabet) (b : Bool) (w : List Bool)
(s : Scan) : R.scanFrom (b :: w) s = R.scanStep b (R.scanFrom w s) := rfl@[simp] theorem scanFinal_nil (R : RankedAlphabet) :
R.scanFinal [] = ⟨[], 0, true⟩ := rfl@[simp] theorem scanFinal_cons (R : RankedAlphabet) (b : Bool) (w : List Bool) :
R.scanFinal (b :: w) = R.scanStep b (R.scanFinal w) := rflThe scan of a concatenation reads the later part first.
theorem scanFrom_append (R : RankedAlphabet) (u v : List Bool) (s : Scan) :
R.scanFrom (u ++ v) s = R.scanFrom u (R.scanFrom v s) :=
List.rec rfl (fun b w ih ↦ by R:RankedAlphabetu:List Boolv:List Bools:Scanb:Boolw:List Boolih:R.scanFrom (w ++ v) s = R.scanFrom w (R.scanFrom v s)⊢ R.scanFrom (b :: w ++ v) s = R.scanFrom (b :: w) (R.scanFrom v s) rw [List.cons_append, R:RankedAlphabetu:List Boolv:List Bools:Scanb:Boolw:List Boolih:R.scanFrom (w ++ v) s = R.scanFrom w (R.scanFrom v s)⊢ R.scanFrom (b :: (w ++ v)) s = R.scanFrom (b :: w) (R.scanFrom v s) scanFrom_cons, R:RankedAlphabetu:List Boolv:List Bools:Scanb:Boolw:List Boolih:R.scanFrom (w ++ v) s = R.scanFrom w (R.scanFrom v s)⊢ R.scanStep b (R.scanFrom (w ++ v) s) = R.scanFrom (b :: w) (R.scanFrom v s) ih, R:RankedAlphabetu:List Boolv:List Bools:Scanb:Boolw:List Boolih:R.scanFrom (w ++ v) s = R.scanFrom w (R.scanFrom v s)⊢ R.scanStep b (R.scanFrom w (R.scanFrom v s)) = R.scanFrom (b :: w) (R.scanFrom v s) scanFrom_cons R:RankedAlphabetu:List Boolv:List Bools:Scanb:Boolw:List Boolih:R.scanFrom (w ++ v) s = R.scanFrom w (R.scanFrom v s)⊢ R.scanStep b (R.scanFrom w (R.scanFrom v s)) = R.scanStep b (R.scanFrom w (R.scanFrom v s))] All goals completed! 🐙) uFailure absorbs: no further input restores a failed scan.
theorem scanFrom_not_live (R : RankedAlphabet) (u : List Bool) (s : Scan)
(h : s.live = false) : (R.scanFrom u s).live = false :=
List.rec h (fun b v ih ↦ by R:RankedAlphabetu:List Bools:Scanh:s.live = falseb:Boolv:List Boolih:(R.scanFrom v s).live = false⊢ (R.scanFrom (b :: v) s).live = false rw [scanFrom_cons, R:RankedAlphabetu:List Bools:Scanh:s.live = falseb:Boolv:List Boolih:(R.scanFrom v s).live = false⊢ (R.scanStep b (R.scanFrom v s)).live = false scanStep, R:RankedAlphabetu:List Bools:Scanh:s.live = falseb:Boolv:List Boolih:(R.scanFrom v s).live = false⊢ (match (R.scanFrom v s).live with
| false => R.scanFrom v s
| true =>
match decide ((b :: (R.scanFrom v s).buf).length = R.width) with
| false => { buf := b :: (R.scanFrom v s).buf, depth := (R.scanFrom v s).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFrom v s).buf)) with
| none => { buf := [], depth := (R.scanFrom v s).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFrom v s).depth) with
| false => { buf := [], depth := (R.scanFrom v s).depth, live := false }
| true => { buf := [], depth := (R.scanFrom v s).depth - r + 1, live := true }).live =
false ih R:RankedAlphabetu:List Bools:Scanh:s.live = falseb:Boolv:List Boolih:(R.scanFrom v s).live = false⊢ (match false with
| false => R.scanFrom v s
| true =>
match decide ((b :: (R.scanFrom v s).buf).length = R.width) with
| false => { buf := b :: (R.scanFrom v s).buf, depth := (R.scanFrom v s).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFrom v s).buf)) with
| none => { buf := [], depth := (R.scanFrom v s).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFrom v s).depth) with
| false => { buf := [], depth := (R.scanFrom v s).depth, live := false }
| true => { buf := [], depth := (R.scanFrom v s).depth - r + 1, live := true }).live =
false] R:RankedAlphabetu:List Bools:Scanh:s.live = falseb:Boolv:List Boolih:(R.scanFrom v s).live = false⊢ (match false with
| false => R.scanFrom v s
| true =>
match decide ((b :: (R.scanFrom v s).buf).length = R.width) with
| false => { buf := b :: (R.scanFrom v s).buf, depth := (R.scanFrom v s).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFrom v s).buf)) with
| none => { buf := [], depth := (R.scanFrom v s).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFrom v s).depth) with
| false => { buf := [], depth := (R.scanFrom v s).depth, live := false }
| true => { buf := [], depth := (R.scanFrom v s).depth - r + 1, live := true }).live =
false; exact ih All goals completed! 🐙) uA word shorter than a block only accumulates.
theorem scanFrom_short (R : RankedAlphabet) (d : ℕ) (bs : List Bool) :
∀ u : List Bool, u.length + bs.length < R.width →
R.scanFrom u ⟨bs, d, true⟩ = ⟨u ++ bs, d, true⟩ :=
List.rec (fun _ ↦ rfl)
(fun b v ih hv ↦ by R:RankedAlphabetd:ℕbs:List Boolb:Boolv:List Boolih:v.length + bs.length < R.width →
R.scanFrom v { buf := bs, depth := d, live := true } = { buf := v ++ bs, depth := d, live := true }hv:(b :: v).length + bs.length < R.width⊢ R.scanFrom (b :: v) { buf := bs, depth := d, live := true } = { buf := b :: v ++ bs, depth := d, live := true }
rw [scanFrom_cons, R:RankedAlphabetd:ℕbs:List Boolb:Boolv:List Boolih:v.length + bs.length < R.width →
R.scanFrom v { buf := bs, depth := d, live := true } = { buf := v ++ bs, depth := d, live := true }hv:(b :: v).length + bs.length < R.width⊢ R.scanStep b (R.scanFrom v { buf := bs, depth := d, live := true }) = { buf := b :: v ++ bs, depth := d, live := true } ih (by R:RankedAlphabetd:ℕbs:List Boolb:Boolv:List Boolih:v.length + bs.length < R.width →
R.scanFrom v { buf := bs, depth := d, live := true } = { buf := v ++ bs, depth := d, live := true }hv:(b :: v).length + bs.length < R.width⊢ v.length + bs.length < R.width simp only [List.length_cons] at hv R:RankedAlphabetd:ℕbs:List Boolb:Boolv:List Boolih:v.length + bs.length < R.width →
R.scanFrom v { buf := bs, depth := d, live := true } = { buf := v ++ bs, depth := d, live := true }hv:v.length + 1 + bs.length < R.width⊢ v.length + bs.length < R.width; omega All goals completed! 🐙), scanStep R:RankedAlphabetd:ℕbs:List Boolb:Boolv:List Boolih:v.length + bs.length < R.width →
R.scanFrom v { buf := bs, depth := d, live := true } = { buf := v ++ bs, depth := d, live := true }hv:(b :: v).length + bs.length < R.width⊢ (match { buf := v ++ bs, depth := d, live := true }.live with
| false => { buf := v ++ bs, depth := d, live := true }
| true =>
match decide ((b :: { buf := v ++ bs, depth := d, live := true }.buf).length = R.width) with
| false =>
{ buf := b :: { buf := v ++ bs, depth := d, live := true }.buf,
depth := { buf := v ++ bs, depth := d, live := true }.depth, live := true }
| true =>
match R.arOf (decodeBits (b :: { buf := v ++ bs, depth := d, live := true }.buf)) with
| none => { buf := [], depth := { buf := v ++ bs, depth := d, live := true }.depth, live := false }
| some r =>
match decide (r ≤ { buf := v ++ bs, depth := d, live := true }.depth) with
| false => { buf := [], depth := { buf := v ++ bs, depth := d, live := true }.depth, live := false }
| true => { buf := [], depth := { buf := v ++ bs, depth := d, live := true }.depth - r + 1, live := true }) =
{ buf := b :: v ++ bs, depth := d, live := true }] R:RankedAlphabetd:ℕbs:List Boolb:Boolv:List Boolih:v.length + bs.length < R.width →
R.scanFrom v { buf := bs, depth := d, live := true } = { buf := v ++ bs, depth := d, live := true }hv:(b :: v).length + bs.length < R.width⊢ (match { buf := v ++ bs, depth := d, live := true }.live with
| false => { buf := v ++ bs, depth := d, live := true }
| true =>
match decide ((b :: { buf := v ++ bs, depth := d, live := true }.buf).length = R.width) with
| false =>
{ buf := b :: { buf := v ++ bs, depth := d, live := true }.buf,
depth := { buf := v ++ bs, depth := d, live := true }.depth, live := true }
| true =>
match R.arOf (decodeBits (b :: { buf := v ++ bs, depth := d, live := true }.buf)) with
| none => { buf := [], depth := { buf := v ++ bs, depth := d, live := true }.depth, live := false }
| some r =>
match decide (r ≤ { buf := v ++ bs, depth := d, live := true }.depth) with
| false => { buf := [], depth := { buf := v ++ bs, depth := d, live := true }.depth, live := false }
| true => { buf := [], depth := { buf := v ++ bs, depth := d, live := true }.depth - r + 1, live := true }) =
{ buf := b :: v ++ bs, depth := d, live := true }
simp only [] R:RankedAlphabetd:ℕbs:List Boolb:Boolv:List Boolih:v.length + bs.length < R.width →
R.scanFrom v { buf := bs, depth := d, live := true } = { buf := v ++ bs, depth := d, live := true }hv:(b :: v).length + bs.length < R.width⊢ (match decide ((b :: (v ++ bs)).length = R.width) with
| false => { buf := b :: (v ++ bs), depth := d, live := true }
| true =>
match R.arOf (decodeBits (b :: (v ++ bs))) with
| none => { buf := [], depth := d, live := false }
| some r =>
match decide (r ≤ d) with
| false => { buf := [], depth := d, live := false }
| true => { buf := [], depth := d - r + 1, live := true }) =
{ buf := b :: v ++ bs, depth := d, live := true }
rw [decide_eq_false (by R:RankedAlphabetd:ℕbs:List Boolb:Boolv:List Boolih:v.length + bs.length < R.width →
R.scanFrom v { buf := bs, depth := d, live := true } = { buf := v ++ bs, depth := d, live := true }hv:(b :: v).length + bs.length < R.width⊢ ¬(b :: (v ++ bs)).length = R.width
simp only [List.length_cons, List.length_append] at hv ⊢ R:RankedAlphabetd:ℕbs:List Boolb:Boolv:List Boolih:v.length + bs.length < R.width →
R.scanFrom v { buf := bs, depth := d, live := true } = { buf := v ++ bs, depth := d, live := true }hv:v.length + 1 + bs.length < R.width⊢ ¬v.length + bs.length + 1 = R.width
omega All goals completed! 🐙)] R:RankedAlphabetd:ℕbs:List Boolb:Boolv:List Boolih:v.length + bs.length < R.width →
R.scanFrom v { buf := bs, depth := d, live := true } = { buf := v ++ bs, depth := d, live := true }hv:(b :: v).length + bs.length < R.width⊢ (match false with
| false => { buf := b :: (v ++ bs), depth := d, live := true }
| true =>
match R.arOf (decodeBits (b :: (v ++ bs))) with
| none => { buf := [], depth := d, live := false }
| some r =>
match decide (r ≤ d) with
| false => { buf := [], depth := d, live := false }
| true => { buf := [], depth := d - r + 1, live := true }) =
{ buf := b :: v ++ bs, depth := d, live := true }
rfl All goals completed! 🐙)Reading a symbol's block from a state with no incomplete block and at least the symbol's arity pending pops that arity and pushes one.
theorem scanFrom_code (R : RankedAlphabet) (i : Fin R.card) (d : ℕ)
(h : R.arity i ≤ d) :
R.scanFrom (R.code i) ⟨[], d, true⟩ = ⟨[], d - R.arity i + 1, true⟩ := by R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ d⊢ R.scanFrom (R.code i) { buf := [], depth := d, live := true } = { buf := [], depth := d - R.arity i + 1, live := true }
obtain ⟨b, v, hcode⟩ : ∃ b v, R.code i = b :: v := by R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ d⊢ ∃ b v, R.code i = b :: v
match hc : R.code i with
| [] => R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ dhc:R.code i = []⊢ ∃ b v, [] = b :: v
have hw := length_code R i R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ dhc:R.code i = []hw:(R.code i).length = R.width⊢ ∃ b v, [] = b :: v
rw [hc R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ dhc:R.code i = []hw:[].length = R.width⊢ ∃ b v, [] = b :: v] at hw R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ dhc:R.code i = []hw:[].length = R.width⊢ ∃ b v, [] = b :: v
exact absurd hw.symm (Nat.ne_of_gt R.width_pos) All goals completed! 🐙
| b :: v => R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ db:Boolv:List Boolhc:R.code i = b :: v⊢ ∃ b_1 v_1, b :: v = b_1 :: v_1 exact ⟨b, v, rfl⟩ R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ db:Boolv:List Boolhcode:R.code i = b :: v⊢ R.scanFrom (R.code i) { buf := [], depth := d, live := true } = { buf := [], depth := d - R.arity i + 1, live := true }
have hw := length_code R i R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ db:Boolv:List Boolhcode:R.code i = b :: vhw:(R.code i).length = R.width⊢ R.scanFrom (R.code i) { buf := [], depth := d, live := true } = { buf := [], depth := d - R.arity i + 1, live := true }
rw [hcode, R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ db:Boolv:List Boolhcode:R.code i = b :: vhw:(b :: v).length = R.width⊢ R.scanFrom (R.code i) { buf := [], depth := d, live := true } = { buf := [], depth := d - R.arity i + 1, live := true } List.length_cons R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ db:Boolv:List Boolhcode:R.code i = b :: vhw:v.length + 1 = R.width⊢ R.scanFrom (R.code i) { buf := [], depth := d, live := true } = { buf := [], depth := d - R.arity i + 1, live := true }] at hw R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ db:Boolv:List Boolhcode:R.code i = b :: vhw:v.length + 1 = R.width⊢ R.scanFrom (R.code i) { buf := [], depth := d, live := true } = { buf := [], depth := d - R.arity i + 1, live := true }
rw [hcode, R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ db:Boolv:List Boolhcode:R.code i = b :: vhw:v.length + 1 = R.width⊢ R.scanFrom (b :: v) { buf := [], depth := d, live := true } = { buf := [], depth := d - R.arity i + 1, live := true } scanFrom_cons, R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ db:Boolv:List Boolhcode:R.code i = b :: vhw:v.length + 1 = R.width⊢ R.scanStep b (R.scanFrom v { buf := [], depth := d, live := true }) =
{ buf := [], depth := d - R.arity i + 1, live := true } scanFrom_short R d [] v (by R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ db:Boolv:List Boolhcode:R.code i = b :: vhw:v.length + 1 = R.width⊢ v.length + [].length < R.width simp R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ db:Boolv:List Boolhcode:R.code i = b :: vhw:v.length + 1 = R.width⊢ v.length < R.width; omega All goals completed! 🐙), scanStep R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ db:Boolv:List Boolhcode:R.code i = b :: vhw:v.length + 1 = R.width⊢ (match { buf := v ++ [], depth := d, live := true }.live with
| false => { buf := v ++ [], depth := d, live := true }
| true =>
match decide ((b :: { buf := v ++ [], depth := d, live := true }.buf).length = R.width) with
| false =>
{ buf := b :: { buf := v ++ [], depth := d, live := true }.buf,
depth := { buf := v ++ [], depth := d, live := true }.depth, live := true }
| true =>
match R.arOf (decodeBits (b :: { buf := v ++ [], depth := d, live := true }.buf)) with
| none => { buf := [], depth := { buf := v ++ [], depth := d, live := true }.depth, live := false }
| some r =>
match decide (r ≤ { buf := v ++ [], depth := d, live := true }.depth) with
| false => { buf := [], depth := { buf := v ++ [], depth := d, live := true }.depth, live := false }
| true => { buf := [], depth := { buf := v ++ [], depth := d, live := true }.depth - r + 1, live := true }) =
{ buf := [], depth := d - R.arity i + 1, live := true }] R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ db:Boolv:List Boolhcode:R.code i = b :: vhw:v.length + 1 = R.width⊢ (match { buf := v ++ [], depth := d, live := true }.live with
| false => { buf := v ++ [], depth := d, live := true }
| true =>
match decide ((b :: { buf := v ++ [], depth := d, live := true }.buf).length = R.width) with
| false =>
{ buf := b :: { buf := v ++ [], depth := d, live := true }.buf,
depth := { buf := v ++ [], depth := d, live := true }.depth, live := true }
| true =>
match R.arOf (decodeBits (b :: { buf := v ++ [], depth := d, live := true }.buf)) with
| none => { buf := [], depth := { buf := v ++ [], depth := d, live := true }.depth, live := false }
| some r =>
match decide (r ≤ { buf := v ++ [], depth := d, live := true }.depth) with
| false => { buf := [], depth := { buf := v ++ [], depth := d, live := true }.depth, live := false }
| true => { buf := [], depth := { buf := v ++ [], depth := d, live := true }.depth - r + 1, live := true }) =
{ buf := [], depth := d - R.arity i + 1, live := true }
simp only [List.append_nil] R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ db:Boolv:List Boolhcode:R.code i = b :: vhw:v.length + 1 = R.width⊢ (match decide ((b :: v).length = R.width) with
| false => { buf := b :: v, depth := d, live := true }
| true =>
match R.arOf (decodeBits (b :: v)) with
| none => { buf := [], depth := d, live := false }
| some r =>
match decide (r ≤ d) with
| false => { buf := [], depth := d, live := false }
| true => { buf := [], depth := d - r + 1, live := true }) =
{ buf := [], depth := d - R.arity i + 1, live := true }
rw [decide_eq_true (by R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ db:Boolv:List Boolhcode:R.code i = b :: vhw:v.length + 1 = R.width⊢ (b :: v).length = R.width rw [List.length_cons R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ db:Boolv:List Boolhcode:R.code i = b :: vhw:v.length + 1 = R.width⊢ v.length + 1 = R.width] R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ db:Boolv:List Boolhcode:R.code i = b :: vhw:v.length + 1 = R.width⊢ v.length + 1 = R.width; omega All goals completed! 🐙)] R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ db:Boolv:List Boolhcode:R.code i = b :: vhw:v.length + 1 = R.width⊢ (match true with
| false => { buf := b :: v, depth := d, live := true }
| true =>
match R.arOf (decodeBits (b :: v)) with
| none => { buf := [], depth := d, live := false }
| some r =>
match decide (r ≤ d) with
| false => { buf := [], depth := d, live := false }
| true => { buf := [], depth := d - r + 1, live := true }) =
{ buf := [], depth := d - R.arity i + 1, live := true }
simp only [] R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ db:Boolv:List Boolhcode:R.code i = b :: vhw:v.length + 1 = R.width⊢ (match R.arOf (decodeBits (b :: v)) with
| none => { buf := [], depth := d, live := false }
| some r =>
match decide (r ≤ d) with
| false => { buf := [], depth := d, live := false }
| true => { buf := [], depth := d - r + 1, live := true }) =
{ buf := [], depth := d - R.arity i + 1, live := true }
rw [← hcode, R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ db:Boolv:List Boolhcode:R.code i = b :: vhw:v.length + 1 = R.width⊢ (match R.arOf (decodeBits (R.code i)) with
| none => { buf := [], depth := d, live := false }
| some r =>
match decide (r ≤ d) with
| false => { buf := [], depth := d, live := false }
| true => { buf := [], depth := d - r + 1, live := true }) =
{ buf := [], depth := d - R.arity i + 1, live := true } arOf_decodeBits_code R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ db:Boolv:List Boolhcode:R.code i = b :: vhw:v.length + 1 = R.width⊢ (match some (R.arity i) with
| none => { buf := [], depth := d, live := false }
| some r =>
match decide (r ≤ d) with
| false => { buf := [], depth := d, live := false }
| true => { buf := [], depth := d - r + 1, live := true }) =
{ buf := [], depth := d - R.arity i + 1, live := true }] R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ db:Boolv:List Boolhcode:R.code i = b :: vhw:v.length + 1 = R.width⊢ (match some (R.arity i) with
| none => { buf := [], depth := d, live := false }
| some r =>
match decide (r ≤ d) with
| false => { buf := [], depth := d, live := false }
| true => { buf := [], depth := d - r + 1, live := true }) =
{ buf := [], depth := d - R.arity i + 1, live := true }
simp only [] R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ db:Boolv:List Boolhcode:R.code i = b :: vhw:v.length + 1 = R.width⊢ (match decide (R.arity i ≤ d) with
| false => { buf := [], depth := d, live := false }
| true => { buf := [], depth := d - R.arity i + 1, live := true }) =
{ buf := [], depth := d - R.arity i + 1, live := true }
rw [decide_eq_true h R:RankedAlphabeti:Fin R.cardd:ℕh:R.arity i ≤ db:Boolv:List Boolhcode:R.code i = b :: vhw:v.length + 1 = R.width⊢ (match true with
| false => { buf := [], depth := d, live := false }
| true => { buf := [], depth := d - R.arity i + 1, live := true }) =
{ buf := [], depth := d - R.arity i + 1, live := true }] All goals completed! 🐙The scan of a list of spellings, each raising the pending count by one, raises it by their number.
theorem scanFrom_flatten (R : RankedAlphabet) (ws : List (List Bool))
(hw : ∀ u ∈ ws, ∀ d, R.scanFrom u ⟨[], d, true⟩ = ⟨[], d + 1, true⟩) (d : ℕ) :
R.scanFrom ws.flatten ⟨[], d, true⟩ = ⟨[], d + ws.length, true⟩ :=
List.rec (motive := fun l ↦ (∀ u ∈ l, ∀ d, R.scanFrom u ⟨[], d, true⟩ =
⟨[], d + 1, true⟩) → R.scanFrom l.flatten ⟨[], d, true⟩ = ⟨[], d + l.length, true⟩)
(fun _ ↦ by R:RankedAlphabetws:List (List Bool)hw:∀ u ∈ ws, ∀ (d : ℕ), R.scanFrom u { buf := [], depth := d, live := true } = { buf := [], depth := d + 1, live := true }d:ℕx✝:∀ u ∈ [], ∀ (d : ℕ), R.scanFrom u { buf := [], depth := d, live := true } = { buf := [], depth := d + 1, live := true }⊢ R.scanFrom [].flatten { buf := [], depth := d, live := true } = { buf := [], depth := d + [].length, live := true } simp only [List.flatten_nil, scanFrom_nil, List.length_nil, Nat.add_zero] All goals completed! 🐙)
(fun u l ih hl ↦ by R:RankedAlphabetws:List (List Bool)hw:∀ u ∈ ws, ∀ (d : ℕ), R.scanFrom u { buf := [], depth := d, live := true } = { buf := [], depth := d + 1, live := true }d:ℕu:List Booll:List (List Bool)ih:(∀ u ∈ l,
∀ (d : ℕ), R.scanFrom u { buf := [], depth := d, live := true } = { buf := [], depth := d + 1, live := true }) →
R.scanFrom l.flatten { buf := [], depth := d, live := true } = { buf := [], depth := d + l.length, live := true }hl:∀ u_1 ∈ u :: l,
∀ (d : ℕ), R.scanFrom u_1 { buf := [], depth := d, live := true } = { buf := [], depth := d + 1, live := true }⊢ R.scanFrom (u :: l).flatten { buf := [], depth := d, live := true } =
{ buf := [], depth := d + (u :: l).length, live := true }
rw [List.flatten_cons, R:RankedAlphabetws:List (List Bool)hw:∀ u ∈ ws, ∀ (d : ℕ), R.scanFrom u { buf := [], depth := d, live := true } = { buf := [], depth := d + 1, live := true }d:ℕu:List Booll:List (List Bool)ih:(∀ u ∈ l,
∀ (d : ℕ), R.scanFrom u { buf := [], depth := d, live := true } = { buf := [], depth := d + 1, live := true }) →
R.scanFrom l.flatten { buf := [], depth := d, live := true } = { buf := [], depth := d + l.length, live := true }hl:∀ u_1 ∈ u :: l,
∀ (d : ℕ), R.scanFrom u_1 { buf := [], depth := d, live := true } = { buf := [], depth := d + 1, live := true }⊢ R.scanFrom (u ++ l.flatten) { buf := [], depth := d, live := true } =
{ buf := [], depth := d + (u :: l).length, live := true } scanFrom_append, R:RankedAlphabetws:List (List Bool)hw:∀ u ∈ ws, ∀ (d : ℕ), R.scanFrom u { buf := [], depth := d, live := true } = { buf := [], depth := d + 1, live := true }d:ℕu:List Booll:List (List Bool)ih:(∀ u ∈ l,
∀ (d : ℕ), R.scanFrom u { buf := [], depth := d, live := true } = { buf := [], depth := d + 1, live := true }) →
R.scanFrom l.flatten { buf := [], depth := d, live := true } = { buf := [], depth := d + l.length, live := true }hl:∀ u_1 ∈ u :: l,
∀ (d : ℕ), R.scanFrom u_1 { buf := [], depth := d, live := true } = { buf := [], depth := d + 1, live := true }⊢ R.scanFrom u (R.scanFrom l.flatten { buf := [], depth := d, live := true }) =
{ buf := [], depth := d + (u :: l).length, live := true }
ih (fun x hx ↦ hl x (List.mem_cons_of_mem u hx)), R:RankedAlphabetws:List (List Bool)hw:∀ u ∈ ws, ∀ (d : ℕ), R.scanFrom u { buf := [], depth := d, live := true } = { buf := [], depth := d + 1, live := true }d:ℕu:List Booll:List (List Bool)ih:(∀ u ∈ l,
∀ (d : ℕ), R.scanFrom u { buf := [], depth := d, live := true } = { buf := [], depth := d + 1, live := true }) →
R.scanFrom l.flatten { buf := [], depth := d, live := true } = { buf := [], depth := d + l.length, live := true }hl:∀ u_1 ∈ u :: l,
∀ (d : ℕ), R.scanFrom u_1 { buf := [], depth := d, live := true } = { buf := [], depth := d + 1, live := true }⊢ R.scanFrom u { buf := [], depth := d + l.length, live := true } =
{ buf := [], depth := d + (u :: l).length, live := true }
hl u List.mem_cons_self (d + l.length), R:RankedAlphabetws:List (List Bool)hw:∀ u ∈ ws, ∀ (d : ℕ), R.scanFrom u { buf := [], depth := d, live := true } = { buf := [], depth := d + 1, live := true }d:ℕu:List Booll:List (List Bool)ih:(∀ u ∈ l,
∀ (d : ℕ), R.scanFrom u { buf := [], depth := d, live := true } = { buf := [], depth := d + 1, live := true }) →
R.scanFrom l.flatten { buf := [], depth := d, live := true } = { buf := [], depth := d + l.length, live := true }hl:∀ u_1 ∈ u :: l,
∀ (d : ℕ), R.scanFrom u_1 { buf := [], depth := d, live := true } = { buf := [], depth := d + 1, live := true }⊢ { buf := [], depth := d + l.length + 1, live := true } = { buf := [], depth := d + (u :: l).length, live := true } List.length_cons R:RankedAlphabetws:List (List Bool)hw:∀ u ∈ ws, ∀ (d : ℕ), R.scanFrom u { buf := [], depth := d, live := true } = { buf := [], depth := d + 1, live := true }d:ℕu:List Booll:List (List Bool)ih:(∀ u ∈ l,
∀ (d : ℕ), R.scanFrom u { buf := [], depth := d, live := true } = { buf := [], depth := d + 1, live := true }) →
R.scanFrom l.flatten { buf := [], depth := d, live := true } = { buf := [], depth := d + l.length, live := true }hl:∀ u_1 ∈ u :: l,
∀ (d : ℕ), R.scanFrom u_1 { buf := [], depth := d, live := true } = { buf := [], depth := d + 1, live := true }⊢ { buf := [], depth := d + l.length + 1, live := true } = { buf := [], depth := d + (l.length + 1), live := true }] R:RankedAlphabetws:List (List Bool)hw:∀ u ∈ ws, ∀ (d : ℕ), R.scanFrom u { buf := [], depth := d, live := true } = { buf := [], depth := d + 1, live := true }d:ℕu:List Booll:List (List Bool)ih:(∀ u ∈ l,
∀ (d : ℕ), R.scanFrom u { buf := [], depth := d, live := true } = { buf := [], depth := d + 1, live := true }) →
R.scanFrom l.flatten { buf := [], depth := d, live := true } = { buf := [], depth := d + l.length, live := true }hl:∀ u_1 ∈ u :: l,
∀ (d : ℕ), R.scanFrom u_1 { buf := [], depth := d, live := true } = { buf := [], depth := d + 1, live := true }⊢ { buf := [], depth := d + l.length + 1, live := true } = { buf := [], depth := d + (l.length + 1), live := true }
congr 1 All goals completed! 🐙)
ws hwA spelling raises the pending count by one, whatever the count before it.
theorem scanFrom_spell (R : RankedAlphabet) (t : R.Term) (d : ℕ) :
R.scanFrom (R.spell t) ⟨[], d, true⟩ = ⟨[], d + 1, true⟩ :=
Term.induction
(motive := fun t ↦ ∀ d, R.scanFrom (R.spell t) ⟨[], d, true⟩ = ⟨[], d + 1, true⟩)
(fun i ch ih d ↦ by R:RankedAlphabett:R.Termd✝:ℕi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (d_1 : ℕ),
R.scanFrom (R.spell (ch d)) { buf := [], depth := d_1, live := true } = { buf := [], depth := d_1 + 1, live := true }d:ℕ⊢ R.scanFrom (R.spell (Term.mk R i ch)) { buf := [], depth := d, live := true } =
{ buf := [], depth := d + 1, live := true }
rw [spell_mk, R:RankedAlphabett:R.Termd✝:ℕi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (d_1 : ℕ),
R.scanFrom (R.spell (ch d)) { buf := [], depth := d_1, live := true } = { buf := [], depth := d_1 + 1, live := true }d:ℕ⊢ R.scanFrom (R.code i ++ (List.ofFn fun d ↦ R.spell (ch d)).flatten) { buf := [], depth := d, live := true } =
{ buf := [], depth := d + 1, live := true } scanFrom_append, R:RankedAlphabett:R.Termd✝:ℕi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (d_1 : ℕ),
R.scanFrom (R.spell (ch d)) { buf := [], depth := d_1, live := true } = { buf := [], depth := d_1 + 1, live := true }d:ℕ⊢ R.scanFrom (R.code i) (R.scanFrom (List.ofFn fun d ↦ R.spell (ch d)).flatten { buf := [], depth := d, live := true }) =
{ buf := [], depth := d + 1, live := true }
scanFrom_flatten R _ (fun u hu e ↦ by R:RankedAlphabett:R.Termd✝:ℕi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (d_1 : ℕ),
R.scanFrom (R.spell (ch d)) { buf := [], depth := d_1, live := true } = { buf := [], depth := d_1 + 1, live := true }d:ℕu:List Boolhu:u ∈ List.ofFn fun d ↦ R.spell (ch d)e:ℕ⊢ R.scanFrom u { buf := [], depth := e, live := true } = { buf := [], depth := e + 1, live := true }
obtain ⟨n, hn⟩ := List.mem_ofFn.mp hu R:RankedAlphabett:R.Termd✝:ℕi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (d_1 : ℕ),
R.scanFrom (R.spell (ch d)) { buf := [], depth := d_1, live := true } = { buf := [], depth := d_1 + 1, live := true }d:ℕu:List Boolhu:u ∈ List.ofFn fun d ↦ R.spell (ch d)e:ℕn:Fin (R.arity i)hn:R.spell (ch n) = u⊢ R.scanFrom u { buf := [], depth := e, live := true } = { buf := [], depth := e + 1, live := true }
exact hn ▸ ih n e All goals completed! 🐙) d,
List.length_ofFn, R:RankedAlphabett:R.Termd✝:ℕi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (d_1 : ℕ),
R.scanFrom (R.spell (ch d)) { buf := [], depth := d_1, live := true } = { buf := [], depth := d_1 + 1, live := true }d:ℕ⊢ R.scanFrom (R.code i) { buf := [], depth := d + R.arity i, live := true } = { buf := [], depth := d + 1, live := true } scanFrom_code R i (d + R.arity i) (Nat.le_add_left _ _) R:RankedAlphabett:R.Termd✝:ℕi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (d_1 : ℕ),
R.scanFrom (R.spell (ch d)) { buf := [], depth := d_1, live := true } = { buf := [], depth := d_1 + 1, live := true }d:ℕ⊢ { buf := [], depth := d + R.arity i - R.arity i + 1, live := true } = { buf := [], depth := d + 1, live := true }] R:RankedAlphabett:R.Termd✝:ℕi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (d_1 : ℕ),
R.scanFrom (R.spell (ch d)) { buf := [], depth := d_1, live := true } = { buf := [], depth := d_1 + 1, live := true }d:ℕ⊢ { buf := [], depth := d + R.arity i - R.arity i + 1, live := true } = { buf := [], depth := d + 1, live := true }
congr 1 e_depth R:RankedAlphabett:R.Termd✝:ℕi:Fin R.cardch:Fin (R.arity i) → R.Termih:∀ (d : Fin (R.arity i)) (d_1 : ℕ),
R.scanFrom (R.spell (ch d)) { buf := [], depth := d_1, live := true } = { buf := [], depth := d_1 + 1, live := true }d:ℕ⊢ d + R.arity i - R.arity i + 1 = d + 1
omega All goals completed! 🐙)
t dEvery spelling is valid.
theorem valid_spell (R : RankedAlphabet) (t : R.Term) : R.Valid (R.spell t) := by R:RankedAlphabett:R.Term⊢ R.Valid (R.spell t)
rw [Valid, R:RankedAlphabett:R.Term⊢ R.validBool (R.spell t) = true validBool, R:RankedAlphabett:R.Term⊢ ((R.scanFinal (R.spell t)).live && (R.scanFinal (R.spell t)).buf.isEmpty && (R.scanFinal (R.spell t)).depth == 1) = true scanFinal, R:RankedAlphabett:R.Term⊢ ((R.scanFrom (R.spell t) { buf := [], depth := 0, live := true }).live &&
(R.scanFrom (R.spell t) { buf := [], depth := 0, live := true }).buf.isEmpty &&
(R.scanFrom (R.spell t) { buf := [], depth := 0, live := true }).depth == 1) =
true scanFrom_spell R t 0 R:RankedAlphabett:R.Term⊢ ({ buf := [], depth := 0 + 1, live := true }.live && { buf := [], depth := 0 + 1, live := true }.buf.isEmpty &&
{ buf := [], depth := 0 + 1, live := true }.depth == 1) =
true] R:RankedAlphabett:R.Term⊢ ({ buf := [], depth := 0 + 1, live := true }.live && { buf := [], depth := 0 + 1, live := true }.buf.isEmpty &&
{ buf := [], depth := 0 + 1, live := true }.depth == 1) =
true
rfl All goals completed! 🐙
The residue of a successor, in terms of the residue. Stated here rather
than taken from Nat's division API for the reason mod_two_mul is: the
modulus is a variable, so omega cannot discharge it, and the lemmas of that
API which do state it depend on Classical.choice.
theorem add_one_mod (a n : ℕ) (hn : 0 < n) :
(a + 1) % n = if a % n + 1 = n then 0 else a % n + 1 := by a:ℕn:ℕhn:0 < n⊢ (a + 1) % n = if a % n + 1 = n then 0 else a % n + 1
have hsplit : (a + 1) % n = (a % n + 1) % n := by
conv_lhs => rw [← Nat.div_add_mod a n] a:ℕn:ℕhn:0 < n| (n * (a / n) + a % n + 1) % n
rw [Nat.add_assoc, a:ℕn:ℕhn:0 < n⊢ (n * (a / n) + (a % n + 1)) % n = (a % n + 1) % n Nat.mul_add_mod a:ℕn:ℕhn:0 < n⊢ (a % n + 1) % n = (a % n + 1) % n] a:ℕn:ℕhn:0 < nhsplit:(a + 1) % n = (a % n + 1) % n⊢ (a + 1) % n = if a % n + 1 = n then 0 else a % n + 1
rw [hsplit a:ℕn:ℕhn:0 < nhsplit:(a + 1) % n = (a % n + 1) % n⊢ (a % n + 1) % n = if a % n + 1 = n then 0 else a % n + 1] a:ℕn:ℕhn:0 < nhsplit:(a + 1) % n = (a % n + 1) % n⊢ (a % n + 1) % n = if a % n + 1 = n then 0 else a % n + 1
split isTrue a:ℕn:ℕhn:0 < nhsplit:(a + 1) % n = (a % n + 1) % nh✝:a % n + 1 = n⊢ (a % n + 1) % n = 0isFalse a:ℕn:ℕhn:0 < nhsplit:(a + 1) % n = (a % n + 1) % nh✝:¬a % n + 1 = n⊢ (a % n + 1) % n = a % n + 1
· isTrue a:ℕn:ℕhn:0 < nhsplit:(a + 1) % n = (a % n + 1) % nh✝:a % n + 1 = n⊢ (a % n + 1) % n = 0 rename_i h isTrue a:ℕn:ℕhn:0 < nhsplit:(a + 1) % n = (a % n + 1) % nh:a % n + 1 = n⊢ (a % n + 1) % n = 0
rw [h, isTrue a:ℕn:ℕhn:0 < nhsplit:(a + 1) % n = (a % n + 1) % nh:a % n + 1 = n⊢ n % n = 0 Nat.mod_self isTrue a:ℕn:ℕhn:0 < nhsplit:(a + 1) % n = (a % n + 1) % nh:a % n + 1 = n⊢ 0 = 0] All goals completed! 🐙
· isFalse a:ℕn:ℕhn:0 < nhsplit:(a + 1) % n = (a % n + 1) % nh✝:¬a % n + 1 = n⊢ (a % n + 1) % n = a % n + 1 rename_i h isFalse a:ℕn:ℕhn:0 < nhsplit:(a + 1) % n = (a % n + 1) % nh:¬a % n + 1 = n⊢ (a % n + 1) % n = a % n + 1
have hr : a % n < n := Nat.mod_lt _ hn isFalse a:ℕn:ℕhn:0 < nhsplit:(a + 1) % n = (a % n + 1) % nh:¬a % n + 1 = nhr:a % n < n⊢ (a % n + 1) % n = a % n + 1
exact Nat.mod_eq_of_lt (by a:ℕn:ℕhn:0 < nhsplit:(a + 1) % n = (a % n + 1) % nh:¬a % n + 1 = nhr:a % n < n⊢ a % n + 1 < n omega All goals completed! 🐙)A step leaving the scan live: the state before it was live, and the step either accumulated the bit into an incomplete block or completed one, in which case its symbol popped that symbol's arity and pushed one.
theorem scanStep_eq_of_live (R : RankedAlphabet) (b : Bool) (s : Scan)
(h : (R.scanStep b s).live = true) :
s.live = true ∧
(((b :: s.buf).length ≠ R.width ∧ R.scanStep b s = ⟨b :: s.buf, s.depth, true⟩) ∨
∃ r, (b :: s.buf).length = R.width ∧
R.arOf (decodeBits (b :: s.buf)) = some r ∧ r ≤ s.depth ∧
R.scanStep b s = ⟨[], s.depth - r + 1, true⟩) := by R:RankedAlphabetb:Bools:Scanh:(R.scanStep b s).live = true⊢ s.live = true ∧
((b :: s.buf).length ≠ R.width ∧ R.scanStep b s = { buf := b :: s.buf, depth := s.depth, live := true } ∨
∃ r,
(b :: s.buf).length = R.width ∧
R.arOf (decodeBits (b :: s.buf)) = some r ∧
r ≤ s.depth ∧ R.scanStep b s = { buf := [], depth := s.depth - r + 1, live := true })
cases hlive : s.live with
| false => false R:RankedAlphabetb:Bools:Scanh:(R.scanStep b s).live = truehlive:s.live = false⊢ false = true ∧
((b :: s.buf).length ≠ R.width ∧ R.scanStep b s = { buf := b :: s.buf, depth := s.depth, live := true } ∨
∃ r,
(b :: s.buf).length = R.width ∧
R.arOf (decodeBits (b :: s.buf)) = some r ∧
r ≤ s.depth ∧ R.scanStep b s = { buf := [], depth := s.depth - r + 1, live := true })
simp only [scanStep, hlive] at h false R:RankedAlphabetb:Bools:Scanhlive:s.live = falseh:false = true⊢ false = true ∧
((b :: s.buf).length ≠ R.width ∧ R.scanStep b s = { buf := b :: s.buf, depth := s.depth, live := true } ∨
∃ r,
(b :: s.buf).length = R.width ∧
R.arOf (decodeBits (b :: s.buf)) = some r ∧
r ≤ s.depth ∧ R.scanStep b s = { buf := [], depth := s.depth - r + 1, live := true })
exact Bool.noConfusion h All goals completed! 🐙
| true => true R:RankedAlphabetb:Bools:Scanh:(R.scanStep b s).live = truehlive:s.live = true⊢ true = true ∧
((b :: s.buf).length ≠ R.width ∧ R.scanStep b s = { buf := b :: s.buf, depth := s.depth, live := true } ∨
∃ r,
(b :: s.buf).length = R.width ∧
R.arOf (decodeBits (b :: s.buf)) = some r ∧
r ≤ s.depth ∧ R.scanStep b s = { buf := [], depth := s.depth - r + 1, live := true })
refine ⟨rfl, ?_⟩ true R:RankedAlphabetb:Bools:Scanh:(R.scanStep b s).live = truehlive:s.live = true⊢ (b :: s.buf).length ≠ R.width ∧ R.scanStep b s = { buf := b :: s.buf, depth := s.depth, live := true } ∨
∃ r,
(b :: s.buf).length = R.width ∧
R.arOf (decodeBits (b :: s.buf)) = some r ∧
r ≤ s.depth ∧ R.scanStep b s = { buf := [], depth := s.depth - r + 1, live := true }
cases hc : decide ((b :: s.buf).length = R.width) with
| false => true.false R:RankedAlphabetb:Bools:Scanh:(R.scanStep b s).live = truehlive:s.live = truehc:decide ((b :: s.buf).length = R.width) = false⊢ (b :: s.buf).length ≠ R.width ∧ R.scanStep b s = { buf := b :: s.buf, depth := s.depth, live := true } ∨
∃ r,
(b :: s.buf).length = R.width ∧
R.arOf (decodeBits (b :: s.buf)) = some r ∧
r ≤ s.depth ∧ R.scanStep b s = { buf := [], depth := s.depth - r + 1, live := true }
refine Or.inl ⟨of_decide_eq_false hc, ?_⟩ true.false R:RankedAlphabetb:Bools:Scanh:(R.scanStep b s).live = truehlive:s.live = truehc:decide ((b :: s.buf).length = R.width) = false⊢ R.scanStep b s = { buf := b :: s.buf, depth := s.depth, live := true }
simp only [scanStep, hlive, hc] All goals completed! 🐙
| true => true.true R:RankedAlphabetb:Bools:Scanh:(R.scanStep b s).live = truehlive:s.live = truehc:decide ((b :: s.buf).length = R.width) = true⊢ (b :: s.buf).length ≠ R.width ∧ R.scanStep b s = { buf := b :: s.buf, depth := s.depth, live := true } ∨
∃ r,
(b :: s.buf).length = R.width ∧
R.arOf (decodeBits (b :: s.buf)) = some r ∧
r ≤ s.depth ∧ R.scanStep b s = { buf := [], depth := s.depth - r + 1, live := true }
cases har : R.arOf (decodeBits (b :: s.buf)) with
| none => true.true.none R:RankedAlphabetb:Bools:Scanh:(R.scanStep b s).live = truehlive:s.live = truehc:decide ((b :: s.buf).length = R.width) = truehar:R.arOf (decodeBits (b :: s.buf)) = none⊢ (b :: s.buf).length ≠ R.width ∧ R.scanStep b s = { buf := b :: s.buf, depth := s.depth, live := true } ∨
∃ r,
(b :: s.buf).length = R.width ∧
none = some r ∧ r ≤ s.depth ∧ R.scanStep b s = { buf := [], depth := s.depth - r + 1, live := true }
simp only [scanStep, hlive, hc, har] at h true.true.none R:RankedAlphabetb:Bools:Scanhlive:s.live = truehc:decide ((b :: s.buf).length = R.width) = truehar:R.arOf (decodeBits (b :: s.buf)) = noneh:false = true⊢ (b :: s.buf).length ≠ R.width ∧ R.scanStep b s = { buf := b :: s.buf, depth := s.depth, live := true } ∨
∃ r,
(b :: s.buf).length = R.width ∧
none = some r ∧ r ≤ s.depth ∧ R.scanStep b s = { buf := [], depth := s.depth - r + 1, live := true }
exact Bool.noConfusion h All goals completed! 🐙
| some r => true.true.some R:RankedAlphabetb:Bools:Scanh:(R.scanStep b s).live = truehlive:s.live = truehc:decide ((b :: s.buf).length = R.width) = truer:ℕhar:R.arOf (decodeBits (b :: s.buf)) = some r⊢ (b :: s.buf).length ≠ R.width ∧ R.scanStep b s = { buf := b :: s.buf, depth := s.depth, live := true } ∨
∃ r_1,
(b :: s.buf).length = R.width ∧
some r = some r_1 ∧ r_1 ≤ s.depth ∧ R.scanStep b s = { buf := [], depth := s.depth - r_1 + 1, live := true }
cases hle : decide (r ≤ s.depth) with
| false => true.true.some.false R:RankedAlphabetb:Bools:Scanh:(R.scanStep b s).live = truehlive:s.live = truehc:decide ((b :: s.buf).length = R.width) = truer:ℕhar:R.arOf (decodeBits (b :: s.buf)) = some rhle:decide (r ≤ s.depth) = false⊢ (b :: s.buf).length ≠ R.width ∧ R.scanStep b s = { buf := b :: s.buf, depth := s.depth, live := true } ∨
∃ r_1,
(b :: s.buf).length = R.width ∧
some r = some r_1 ∧ r_1 ≤ s.depth ∧ R.scanStep b s = { buf := [], depth := s.depth - r_1 + 1, live := true }
simp only [scanStep, hlive, hc, har, hle] at h true.true.some.false R:RankedAlphabetb:Bools:Scanhlive:s.live = truehc:decide ((b :: s.buf).length = R.width) = truer:ℕhar:R.arOf (decodeBits (b :: s.buf)) = some rhle:decide (r ≤ s.depth) = falseh:false = true⊢ (b :: s.buf).length ≠ R.width ∧ R.scanStep b s = { buf := b :: s.buf, depth := s.depth, live := true } ∨
∃ r_1,
(b :: s.buf).length = R.width ∧
some r = some r_1 ∧ r_1 ≤ s.depth ∧ R.scanStep b s = { buf := [], depth := s.depth - r_1 + 1, live := true }
exact Bool.noConfusion h All goals completed! 🐙
| true => true.true.some.true R:RankedAlphabetb:Bools:Scanh:(R.scanStep b s).live = truehlive:s.live = truehc:decide ((b :: s.buf).length = R.width) = truer:ℕhar:R.arOf (decodeBits (b :: s.buf)) = some rhle:decide (r ≤ s.depth) = true⊢ (b :: s.buf).length ≠ R.width ∧ R.scanStep b s = { buf := b :: s.buf, depth := s.depth, live := true } ∨
∃ r_1,
(b :: s.buf).length = R.width ∧
some r = some r_1 ∧ r_1 ≤ s.depth ∧ R.scanStep b s = { buf := [], depth := s.depth - r_1 + 1, live := true }
exact Or.inr ⟨r, of_decide_eq_true hc, rfl, of_decide_eq_true hle, by R:RankedAlphabetb:Bools:Scanh:(R.scanStep b s).live = truehlive:s.live = truehc:decide ((b :: s.buf).length = R.width) = truer:ℕhar:R.arOf (decodeBits (b :: s.buf)) = some rhle:decide (r ≤ s.depth) = true⊢ R.scanStep b s = { buf := [], depth := s.depth - r + 1, live := true }
simp only [scanStep, hlive, hc, har, hle] All goals completed! 🐙⟩A live scan's incomplete block holds the word's length modulo the width: block boundaries align with the word's right end.
theorem length_buf_scanFinal_of_live (R : RankedAlphabet) :
∀ w : List Bool, (R.scanFinal w).live = true →
(R.scanFinal w).buf.length = w.length % R.width :=
List.rec (fun _ ↦ by R:RankedAlphabetx✝:(R.scanFinal []).live = true⊢ (R.scanFinal []).buf.length = [].length % R.width simp only [scanFinal_nil, List.length_nil, Nat.zero_mod] All goals completed! 🐙)
(fun b v ih hlive ↦ by R:RankedAlphabetb:Boolv:List Boolih:(R.scanFinal v).live = true → (R.scanFinal v).buf.length = v.length % R.widthhlive:(R.scanFinal (b :: v)).live = true⊢ (R.scanFinal (b :: v)).buf.length = (b :: v).length % R.width
rw [scanFinal_cons R:RankedAlphabetb:Boolv:List Boolih:(R.scanFinal v).live = true → (R.scanFinal v).buf.length = v.length % R.widthhlive:(R.scanStep b (R.scanFinal v)).live = true⊢ (R.scanStep b (R.scanFinal v)).buf.length = (b :: v).length % R.width] at hlive ⊢ R:RankedAlphabetb:Boolv:List Boolih:(R.scanFinal v).live = true → (R.scanFinal v).buf.length = v.length % R.widthhlive:(R.scanStep b (R.scanFinal v)).live = true⊢ (R.scanStep b (R.scanFinal v)).buf.length = (b :: v).length % R.width
obtain ⟨hv, hcase⟩ := scanStep_eq_of_live R b (R.scanFinal v) hlive R:RankedAlphabetb:Boolv:List Boolih:(R.scanFinal v).live = true → (R.scanFinal v).buf.length = v.length % R.widthhlive:(R.scanStep b (R.scanFinal v)).live = truehv:(R.scanFinal v).live = truehcase:(b :: (R.scanFinal v).buf).length ≠ R.width ∧
R.scanStep b (R.scanFinal v) = { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true } ∨
∃ r,
(b :: (R.scanFinal v).buf).length = R.width ∧
R.arOf (decodeBits (b :: (R.scanFinal v).buf)) = some r ∧
r ≤ (R.scanFinal v).depth ∧
R.scanStep b (R.scanFinal v) = { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }⊢ (R.scanStep b (R.scanFinal v)).buf.length = (b :: v).length % R.width
have ihv := ih hv R:RankedAlphabetb:Boolv:List Boolih:(R.scanFinal v).live = true → (R.scanFinal v).buf.length = v.length % R.widthhlive:(R.scanStep b (R.scanFinal v)).live = truehv:(R.scanFinal v).live = truehcase:(b :: (R.scanFinal v).buf).length ≠ R.width ∧
R.scanStep b (R.scanFinal v) = { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true } ∨
∃ r,
(b :: (R.scanFinal v).buf).length = R.width ∧
R.arOf (decodeBits (b :: (R.scanFinal v).buf)) = some r ∧
r ≤ (R.scanFinal v).depth ∧
R.scanStep b (R.scanFinal v) = { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }ihv:(R.scanFinal v).buf.length = v.length % R.width⊢ (R.scanStep b (R.scanFinal v)).buf.length = (b :: v).length % R.width
rcases hcase with ⟨hne, heq⟩ | ⟨r, hlen, _, _, heq⟩ inl R:RankedAlphabetb:Boolv:List Boolih:(R.scanFinal v).live = true → (R.scanFinal v).buf.length = v.length % R.widthhlive:(R.scanStep b (R.scanFinal v)).live = truehv:(R.scanFinal v).live = trueihv:(R.scanFinal v).buf.length = v.length % R.widthhne:(b :: (R.scanFinal v).buf).length ≠ R.widthheq:R.scanStep b (R.scanFinal v) = { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }⊢ (R.scanStep b (R.scanFinal v)).buf.length = (b :: v).length % R.widthinr R:RankedAlphabetb:Boolv:List Boolih:(R.scanFinal v).live = true → (R.scanFinal v).buf.length = v.length % R.widthhlive:(R.scanStep b (R.scanFinal v)).live = truehv:(R.scanFinal v).live = trueihv:(R.scanFinal v).buf.length = v.length % R.widthr:ℕhlen:(b :: (R.scanFinal v).buf).length = R.widthleft✝¹:R.arOf (decodeBits (b :: (R.scanFinal v).buf)) = some rleft✝:r ≤ (R.scanFinal v).depthheq:R.scanStep b (R.scanFinal v) = { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }⊢ (R.scanStep b (R.scanFinal v)).buf.length = (b :: v).length % R.width
· inl R:RankedAlphabetb:Boolv:List Boolih:(R.scanFinal v).live = true → (R.scanFinal v).buf.length = v.length % R.widthhlive:(R.scanStep b (R.scanFinal v)).live = truehv:(R.scanFinal v).live = trueihv:(R.scanFinal v).buf.length = v.length % R.widthhne:(b :: (R.scanFinal v).buf).length ≠ R.widthheq:R.scanStep b (R.scanFinal v) = { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }⊢ (R.scanStep b (R.scanFinal v)).buf.length = (b :: v).length % R.width simp only [heq, List.length_cons] at hne ⊢ inl R:RankedAlphabetb:Boolv:List Boolih:(R.scanFinal v).live = true → (R.scanFinal v).buf.length = v.length % R.widthhlive:(R.scanStep b (R.scanFinal v)).live = truehv:(R.scanFinal v).live = trueihv:(R.scanFinal v).buf.length = v.length % R.widthhne:(R.scanFinal v).buf.length + 1 ≠ R.widthheq:R.scanStep b (R.scanFinal v) = { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }⊢ (R.scanFinal v).buf.length + 1 = (v.length + 1) % R.width
rw [ihv inl R:RankedAlphabetb:Boolv:List Boolih:(R.scanFinal v).live = true → (R.scanFinal v).buf.length = v.length % R.widthhlive:(R.scanStep b (R.scanFinal v)).live = truehv:(R.scanFinal v).live = trueihv:(R.scanFinal v).buf.length = v.length % R.widthhne:v.length % R.width + 1 ≠ R.widthheq:R.scanStep b (R.scanFinal v) = { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }⊢ (R.scanFinal v).buf.length + 1 = (v.length + 1) % R.width] at hne inl R:RankedAlphabetb:Boolv:List Boolih:(R.scanFinal v).live = true → (R.scanFinal v).buf.length = v.length % R.widthhlive:(R.scanStep b (R.scanFinal v)).live = truehv:(R.scanFinal v).live = trueihv:(R.scanFinal v).buf.length = v.length % R.widthhne:v.length % R.width + 1 ≠ R.widthheq:R.scanStep b (R.scanFinal v) = { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }⊢ (R.scanFinal v).buf.length + 1 = (v.length + 1) % R.width
rw [ihv, inl R:RankedAlphabetb:Boolv:List Boolih:(R.scanFinal v).live = true → (R.scanFinal v).buf.length = v.length % R.widthhlive:(R.scanStep b (R.scanFinal v)).live = truehv:(R.scanFinal v).live = trueihv:(R.scanFinal v).buf.length = v.length % R.widthhne:v.length % R.width + 1 ≠ R.widthheq:R.scanStep b (R.scanFinal v) = { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }⊢ v.length % R.width + 1 = (v.length + 1) % R.width add_one_mod _ _ R.width_pos, inl R:RankedAlphabetb:Boolv:List Boolih:(R.scanFinal v).live = true → (R.scanFinal v).buf.length = v.length % R.widthhlive:(R.scanStep b (R.scanFinal v)).live = truehv:(R.scanFinal v).live = trueihv:(R.scanFinal v).buf.length = v.length % R.widthhne:v.length % R.width + 1 ≠ R.widthheq:R.scanStep b (R.scanFinal v) = { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }⊢ v.length % R.width + 1 = if v.length % R.width + 1 = R.width then 0 else v.length % R.width + 1 ite_eq_right hne inl R:RankedAlphabetb:Boolv:List Boolih:(R.scanFinal v).live = true → (R.scanFinal v).buf.length = v.length % R.widthhlive:(R.scanStep b (R.scanFinal v)).live = truehv:(R.scanFinal v).live = trueihv:(R.scanFinal v).buf.length = v.length % R.widthhne:v.length % R.width + 1 ≠ R.widthheq:R.scanStep b (R.scanFinal v) = { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }⊢ v.length % R.width + 1 = v.length % R.width + 1] All goals completed! 🐙
· inr R:RankedAlphabetb:Boolv:List Boolih:(R.scanFinal v).live = true → (R.scanFinal v).buf.length = v.length % R.widthhlive:(R.scanStep b (R.scanFinal v)).live = truehv:(R.scanFinal v).live = trueihv:(R.scanFinal v).buf.length = v.length % R.widthr:ℕhlen:(b :: (R.scanFinal v).buf).length = R.widthleft✝¹:R.arOf (decodeBits (b :: (R.scanFinal v).buf)) = some rleft✝:r ≤ (R.scanFinal v).depthheq:R.scanStep b (R.scanFinal v) = { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }⊢ (R.scanStep b (R.scanFinal v)).buf.length = (b :: v).length % R.width simp only [List.length_cons] at hlen inr R:RankedAlphabetb:Boolv:List Boolih:(R.scanFinal v).live = true → (R.scanFinal v).buf.length = v.length % R.widthhlive:(R.scanStep b (R.scanFinal v)).live = truehv:(R.scanFinal v).live = trueihv:(R.scanFinal v).buf.length = v.length % R.widthr:ℕhlen:(R.scanFinal v).buf.length + 1 = R.widthleft✝¹:R.arOf (decodeBits (b :: (R.scanFinal v).buf)) = some rleft✝:r ≤ (R.scanFinal v).depthheq:R.scanStep b (R.scanFinal v) = { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }⊢ (R.scanStep b (R.scanFinal v)).buf.length = (b :: v).length % R.width
rw [ihv inr R:RankedAlphabetb:Boolv:List Boolih:(R.scanFinal v).live = true → (R.scanFinal v).buf.length = v.length % R.widthhlive:(R.scanStep b (R.scanFinal v)).live = truehv:(R.scanFinal v).live = trueihv:(R.scanFinal v).buf.length = v.length % R.widthr:ℕhlen:v.length % R.width + 1 = R.widthleft✝¹:R.arOf (decodeBits (b :: (R.scanFinal v).buf)) = some rleft✝:r ≤ (R.scanFinal v).depthheq:R.scanStep b (R.scanFinal v) = { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }⊢ (R.scanStep b (R.scanFinal v)).buf.length = (b :: v).length % R.width] at hlen inr R:RankedAlphabetb:Boolv:List Boolih:(R.scanFinal v).live = true → (R.scanFinal v).buf.length = v.length % R.widthhlive:(R.scanStep b (R.scanFinal v)).live = truehv:(R.scanFinal v).live = trueihv:(R.scanFinal v).buf.length = v.length % R.widthr:ℕhlen:v.length % R.width + 1 = R.widthleft✝¹:R.arOf (decodeBits (b :: (R.scanFinal v).buf)) = some rleft✝:r ≤ (R.scanFinal v).depthheq:R.scanStep b (R.scanFinal v) = { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }⊢ (R.scanStep b (R.scanFinal v)).buf.length = (b :: v).length % R.width
simp only [heq, List.length_nil, List.length_cons] inr R:RankedAlphabetb:Boolv:List Boolih:(R.scanFinal v).live = true → (R.scanFinal v).buf.length = v.length % R.widthhlive:(R.scanStep b (R.scanFinal v)).live = truehv:(R.scanFinal v).live = trueihv:(R.scanFinal v).buf.length = v.length % R.widthr:ℕhlen:v.length % R.width + 1 = R.widthleft✝¹:R.arOf (decodeBits (b :: (R.scanFinal v).buf)) = some rleft✝:r ≤ (R.scanFinal v).depthheq:R.scanStep b (R.scanFinal v) = { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }⊢ 0 = (v.length + 1) % R.width
rw [add_one_mod _ _ R.width_pos, inr R:RankedAlphabetb:Boolv:List Boolih:(R.scanFinal v).live = true → (R.scanFinal v).buf.length = v.length % R.widthhlive:(R.scanStep b (R.scanFinal v)).live = truehv:(R.scanFinal v).live = trueihv:(R.scanFinal v).buf.length = v.length % R.widthr:ℕhlen:v.length % R.width + 1 = R.widthleft✝¹:R.arOf (decodeBits (b :: (R.scanFinal v).buf)) = some rleft✝:r ≤ (R.scanFinal v).depthheq:R.scanStep b (R.scanFinal v) = { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }⊢ 0 = if v.length % R.width + 1 = R.width then 0 else v.length % R.width + 1 ite_eq_left hlen inr R:RankedAlphabetb:Boolv:List Boolih:(R.scanFinal v).live = true → (R.scanFinal v).buf.length = v.length % R.widthhlive:(R.scanStep b (R.scanFinal v)).live = truehv:(R.scanFinal v).live = trueihv:(R.scanFinal v).buf.length = v.length % R.widthr:ℕhlen:v.length % R.width + 1 = R.widthleft✝¹:R.arOf (decodeBits (b :: (R.scanFinal v).buf)) = some rleft✝:r ≤ (R.scanFinal v).depthheq:R.scanStep b (R.scanFinal v) = { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }⊢ 0 = 0] All goals completed! 🐙)
The incomplete block never fills, whether or not the scan has failed.
Stated unconditionally, which is the form a bitstring layout of the state
consumes; the List.rec below establishes the live and failed cases at once
rather than through length_buf_scanFinal_of_live.
theorem length_buf_scanFinal_lt (R : RankedAlphabet) (w : List Bool) :
(R.scanFinal w).buf.length < R.width :=
List.rec R.width_pos
(fun b v ih ↦ by R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.width⊢ (R.scanFinal (b :: v)).buf.length < R.width
rw [scanFinal_cons, R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.width⊢ (R.scanStep b (R.scanFinal v)).buf.length < R.width scanStep R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.width⊢ (match (R.scanFinal v).live with
| false => R.scanFinal v
| true =>
match decide ((b :: (R.scanFinal v).buf).length = R.width) with
| false => { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).buf.length <
R.width] R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.width⊢ (match (R.scanFinal v).live with
| false => R.scanFinal v
| true =>
match decide ((b :: (R.scanFinal v).buf).length = R.width) with
| false => { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).buf.length <
R.width
cases (R.scanFinal v).live false R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.width⊢ (match false with
| false => R.scanFinal v
| true =>
match decide ((b :: (R.scanFinal v).buf).length = R.width) with
| false => { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).buf.length <
R.widthtrue R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.width⊢ (match true with
| false => R.scanFinal v
| true =>
match decide ((b :: (R.scanFinal v).buf).length = R.width) with
| false => { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).buf.length <
R.width
· false R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.width⊢ (match false with
| false => R.scanFinal v
| true =>
match decide ((b :: (R.scanFinal v).buf).length = R.width) with
| false => { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).buf.length <
R.width exact ih All goals completed! 🐙
· true R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.width⊢ (match true with
| false => R.scanFinal v
| true =>
match decide ((b :: (R.scanFinal v).buf).length = R.width) with
| false => { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).buf.length <
R.width simp only [] true R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.width⊢ (match decide ((b :: (R.scanFinal v).buf).length = R.width) with
| false => { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).buf.length <
R.width
cases hc : decide ((b :: (R.scanFinal v).buf).length = R.width) true.false R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.widthhc:decide ((b :: (R.scanFinal v).buf).length = R.width) = false⊢ (match false with
| false => { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).buf.length <
R.widthtrue.true R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.widthhc:decide ((b :: (R.scanFinal v).buf).length = R.width) = true⊢ (match true with
| false => { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).buf.length <
R.width
· true.false R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.widthhc:decide ((b :: (R.scanFinal v).buf).length = R.width) = false⊢ (match false with
| false => { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).buf.length <
R.width simp only [List.length_cons] at hc ⊢ true.false R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.widthhc:decide ((R.scanFinal v).buf.length + 1 = R.width) = false⊢ (R.scanFinal v).buf.length + 1 < R.width
have := of_decide_eq_false hc true.false R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.widthhc:decide ((R.scanFinal v).buf.length + 1 = R.width) = falsethis:¬(R.scanFinal v).buf.length + 1 = R.width⊢ (R.scanFinal v).buf.length + 1 < R.width
omega All goals completed! 🐙
· true.true R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.widthhc:decide ((b :: (R.scanFinal v).buf).length = R.width) = true⊢ (match true with
| false => { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).buf.length <
R.width simp only [] true.true R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.widthhc:decide ((b :: (R.scanFinal v).buf).length = R.width) = true⊢ (match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).buf.length <
R.width
cases R.arOf (decodeBits (b :: (R.scanFinal v).buf)) true.true.none R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.widthhc:decide ((b :: (R.scanFinal v).buf).length = R.width) = true⊢ (match none with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).buf.length <
R.widthtrue.true.some R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.widthhc:decide ((b :: (R.scanFinal v).buf).length = R.width) = trueval✝:ℕ⊢ (match some val✝ with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).buf.length <
R.width
· true.true.none R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.widthhc:decide ((b :: (R.scanFinal v).buf).length = R.width) = true⊢ (match none with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).buf.length <
R.width exact R.width_pos All goals completed! 🐙
· true.true.some R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.widthhc:decide ((b :: (R.scanFinal v).buf).length = R.width) = trueval✝:ℕ⊢ (match some val✝ with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).buf.length <
R.width simp only [] true.true.some R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.widthhc:decide ((b :: (R.scanFinal v).buf).length = R.width) = trueval✝:ℕ⊢ (match decide (val✝ ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - val✝ + 1, live := true }).buf.length <
R.width
cases decide (_ ≤ (R.scanFinal v).depth) true.true.some.false R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.widthhc:decide ((b :: (R.scanFinal v).buf).length = R.width) = trueval✝:ℕ⊢ (match false with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - val✝ + 1, live := true }).buf.length <
R.widthtrue.true.some.true R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.widthhc:decide ((b :: (R.scanFinal v).buf).length = R.width) = trueval✝:ℕ⊢ (match true with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - val✝ + 1, live := true }).buf.length <
R.width <;> true.true.some.false R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.widthhc:decide ((b :: (R.scanFinal v).buf).length = R.width) = trueval✝:ℕ⊢ (match false with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - val✝ + 1, live := true }).buf.length <
R.widthtrue.true.some.true R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).buf.length < R.widthhc:decide ((b :: (R.scanFinal v).buf).length = R.width) = trueval✝:ℕ⊢ (match true with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - val✝ + 1, live := true }).buf.length <
R.width exact R.width_pos All goals completed! 🐙) wThe pending count is at most the word's length: every clause but the pop leaves it alone, and the pop raises it by at most one.
theorem depth_scanFinal_le_length (R : RankedAlphabet) (w : List Bool) :
(R.scanFinal w).depth ≤ w.length :=
List.rec (Nat.le_refl 0)
(fun b v ih ↦ by R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.length⊢ (R.scanFinal (b :: v)).depth ≤ (b :: v).length
have hlen : (b :: v).length = v.length + 1 := rfl R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1⊢ (R.scanFinal (b :: v)).depth ≤ (b :: v).length
rw [scanFinal_cons, R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1⊢ (R.scanStep b (R.scanFinal v)).depth ≤ (b :: v).length scanStep R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1⊢ (match (R.scanFinal v).live with
| false => R.scanFinal v
| true =>
match decide ((b :: (R.scanFinal v).buf).length = R.width) with
| false => { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).depth ≤
(b :: v).length] R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1⊢ (match (R.scanFinal v).live with
| false => R.scanFinal v
| true =>
match decide ((b :: (R.scanFinal v).buf).length = R.width) with
| false => { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).depth ≤
(b :: v).length
cases (R.scanFinal v).live false R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1⊢ (match false with
| false => R.scanFinal v
| true =>
match decide ((b :: (R.scanFinal v).buf).length = R.width) with
| false => { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).depth ≤
(b :: v).lengthtrue R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1⊢ (match true with
| false => R.scanFinal v
| true =>
match decide ((b :: (R.scanFinal v).buf).length = R.width) with
| false => { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).depth ≤
(b :: v).length
· false R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1⊢ (match false with
| false => R.scanFinal v
| true =>
match decide ((b :: (R.scanFinal v).buf).length = R.width) with
| false => { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).depth ≤
(b :: v).length simp only [] false R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1⊢ (R.scanFinal v).depth ≤ (b :: v).length
omega All goals completed! 🐙
· true R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1⊢ (match true with
| false => R.scanFinal v
| true =>
match decide ((b :: (R.scanFinal v).buf).length = R.width) with
| false => { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).depth ≤
(b :: v).length simp only [] true R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1⊢ (match decide ((b :: (R.scanFinal v).buf).length = R.width) with
| false => { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).depth ≤
(b :: v).length
cases decide ((b :: (R.scanFinal v).buf).length = R.width) true.false R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1⊢ (match false with
| false => { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).depth ≤
(b :: v).lengthtrue.true R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1⊢ (match true with
| false => { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).depth ≤
(b :: v).length
· true.false R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1⊢ (match false with
| false => { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).depth ≤
(b :: v).length simp only [] true.false R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1⊢ (R.scanFinal v).depth ≤ (b :: v).length
omega All goals completed! 🐙
· true.true R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1⊢ (match true with
| false => { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }
| true =>
match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).depth ≤
(b :: v).length simp only [] true.true R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1⊢ (match R.arOf (decodeBits (b :: (R.scanFinal v).buf)) with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).depth ≤
(b :: v).length
cases R.arOf (decodeBits (b :: (R.scanFinal v).buf)) true.true.none R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1⊢ (match none with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).depth ≤
(b :: v).lengthtrue.true.some R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1val✝:ℕ⊢ (match some val✝ with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).depth ≤
(b :: v).length
· true.true.none R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1⊢ (match none with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).depth ≤
(b :: v).length simp only [] true.true.none R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1⊢ (R.scanFinal v).depth ≤ (b :: v).length
omega All goals completed! 🐙
· true.true.some R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1val✝:ℕ⊢ (match some val✝ with
| none => { buf := [], depth := (R.scanFinal v).depth, live := false }
| some r =>
match decide (r ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }).depth ≤
(b :: v).length simp only [] true.true.some R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1val✝:ℕ⊢ (match decide (val✝ ≤ (R.scanFinal v).depth) with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - val✝ + 1, live := true }).depth ≤
(b :: v).length
cases decide (_ ≤ (R.scanFinal v).depth) true.true.some.false R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1val✝:ℕ⊢ (match false with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - val✝ + 1, live := true }).depth ≤
(b :: v).lengthtrue.true.some.true R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1val✝:ℕ⊢ (match true with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - val✝ + 1, live := true }).depth ≤
(b :: v).length
· true.true.some.false R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1val✝:ℕ⊢ (match false with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - val✝ + 1, live := true }).depth ≤
(b :: v).length simp only [] true.true.some.false R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1val✝:ℕ⊢ (R.scanFinal v).depth ≤ (b :: v).length
omega All goals completed! 🐙
· true.true.some.true R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1val✝:ℕ⊢ (match true with
| false => { buf := [], depth := (R.scanFinal v).depth, live := false }
| true => { buf := [], depth := (R.scanFinal v).depth - val✝ + 1, live := true }).depth ≤
(b :: v).length simp only [] true.true.some.true R:RankedAlphabetw:List Boolb:Boolv:List Boolih:(R.scanFinal v).depth ≤ v.lengthhlen:(b :: v).length = v.length + 1val✝:ℕ⊢ (R.scanFinal v).depth - val✝ + 1 ≤ (b :: v).length
omega All goals completed! 🐙) w
Validity as the three conditions on the final state, which is the form a
statement about the state word consumes. The two directions take different
routes: the forward one splits validBool's conjunction and reads each
component, and the backward one closes by reduction once the fields are
substituted.
theorem valid_iff_scanFinal (R : RankedAlphabet) (w : List Bool) :
R.Valid w ↔ ((R.scanFinal w).live = true ∧ (R.scanFinal w).buf = [] ∧
(R.scanFinal w).depth = 1) := by R:RankedAlphabetw:List Bool⊢ R.Valid w ↔ (R.scanFinal w).live = true ∧ (R.scanFinal w).buf = [] ∧ (R.scanFinal w).depth = 1
constructor mp R:RankedAlphabetw:List Bool⊢ R.Valid w → (R.scanFinal w).live = true ∧ (R.scanFinal w).buf = [] ∧ (R.scanFinal w).depth = 1mpr R:RankedAlphabetw:List Bool⊢ (R.scanFinal w).live = true ∧ (R.scanFinal w).buf = [] ∧ (R.scanFinal w).depth = 1 → R.Valid w
· mp R:RankedAlphabetw:List Bool⊢ R.Valid w → (R.scanFinal w).live = true ∧ (R.scanFinal w).buf = [] ∧ (R.scanFinal w).depth = 1 intro hv mp R:RankedAlphabetw:List Boolhv:R.Valid w⊢ (R.scanFinal w).live = true ∧ (R.scanFinal w).buf = [] ∧ (R.scanFinal w).depth = 1
have hb : ((R.scanFinal w).live && (R.scanFinal w).buf.isEmpty &&
((R.scanFinal w).depth == 1)) = true := hv mp R:RankedAlphabetw:List Boolhv:R.Valid whb:((R.scanFinal w).live && (R.scanFinal w).buf.isEmpty && (R.scanFinal w).depth == 1) = true⊢ (R.scanFinal w).live = true ∧ (R.scanFinal w).buf = [] ∧ (R.scanFinal w).depth = 1
rw [Bool.and_eq_true, mp R:RankedAlphabetw:List Boolhv:R.Valid whb:((R.scanFinal w).live && (R.scanFinal w).buf.isEmpty) = true ∧ ((R.scanFinal w).depth == 1) = true⊢ (R.scanFinal w).live = true ∧ (R.scanFinal w).buf = [] ∧ (R.scanFinal w).depth = 1 Bool.and_eq_true mp R:RankedAlphabetw:List Boolhv:R.Valid whb:((R.scanFinal w).live = true ∧ (R.scanFinal w).buf.isEmpty = true) ∧ ((R.scanFinal w).depth == 1) = true⊢ (R.scanFinal w).live = true ∧ (R.scanFinal w).buf = [] ∧ (R.scanFinal w).depth = 1] at hb mp R:RankedAlphabetw:List Boolhv:R.Valid whb:((R.scanFinal w).live = true ∧ (R.scanFinal w).buf.isEmpty = true) ∧ ((R.scanFinal w).depth == 1) = true⊢ (R.scanFinal w).live = true ∧ (R.scanFinal w).buf = [] ∧ (R.scanFinal w).depth = 1
exact ⟨hb.1.1, List.isEmpty_iff.mp hb.1.2, of_decide_eq_true hb.2⟩ All goals completed! 🐙
· mpr R:RankedAlphabetw:List Bool⊢ (R.scanFinal w).live = true ∧ (R.scanFinal w).buf = [] ∧ (R.scanFinal w).depth = 1 → R.Valid w intro hf mpr R:RankedAlphabetw:List Boolhf:(R.scanFinal w).live = true ∧ (R.scanFinal w).buf = [] ∧ (R.scanFinal w).depth = 1⊢ R.Valid w
change ((R.scanFinal w).live && (R.scanFinal w).buf.isEmpty &&
((R.scanFinal w).depth == 1)) = true mpr R:RankedAlphabetw:List Boolhf:(R.scanFinal w).live = true ∧ (R.scanFinal w).buf = [] ∧ (R.scanFinal w).depth = 1⊢ ((R.scanFinal w).live && (R.scanFinal w).buf.isEmpty && (R.scanFinal w).depth == 1) = true
rw [hf.1, mpr R:RankedAlphabetw:List Boolhf:(R.scanFinal w).live = true ∧ (R.scanFinal w).buf = [] ∧ (R.scanFinal w).depth = 1⊢ (true && (R.scanFinal w).buf.isEmpty && (R.scanFinal w).depth == 1) = true hf.2.1, mpr R:RankedAlphabetw:List Boolhf:(R.scanFinal w).live = true ∧ (R.scanFinal w).buf = [] ∧ (R.scanFinal w).depth = 1⊢ (true && [].isEmpty && (R.scanFinal w).depth == 1) = true hf.2.2 mpr R:RankedAlphabetw:List Boolhf:(R.scanFinal w).live = true ∧ (R.scanFinal w).buf = [] ∧ (R.scanFinal w).depth = 1⊢ (true && [].isEmpty && 1 == 1) = true] mpr R:RankedAlphabetw:List Boolhf:(R.scanFinal w).live = true ∧ (R.scanFinal w).buf = [] ∧ (R.scanFinal w).depth = 1⊢ (true && [].isEmpty && 1 == 1) = true
rfl All goals completed! 🐙
The converse of scanFrom_code: a live scan over a full block exhibits
that block as a symbol's code.
theorem exists_code_of_scanFrom_live (R : RankedAlphabet) (blk : List Bool)
(s : Scan) (hlen : blk.length = R.width) (hbuf : s.buf = [])
(hs : s.live = true) (h : (R.scanFrom blk s).live = true) :
∃ i : Fin R.card, R.code i = blk ∧ R.arity i ≤ s.depth ∧
R.scanFrom blk s = ⟨[], s.depth - R.arity i + 1, true⟩ := by R:RankedAlphabetblk:List Bools:Scanhlen:blk.length = R.widthhbuf:s.buf = []hs:s.live = trueh:(R.scanFrom blk s).live = true⊢ ∃ i,
R.code i = blk ∧
R.arity i ≤ s.depth ∧ R.scanFrom blk s = { buf := [], depth := s.depth - R.arity i + 1, live := true }
obtain ⟨sbuf, sdepth, slive⟩ := s R:RankedAlphabetblk:List Boolhlen:blk.length = R.widthsbuf:List Boolsdepth:ℕslive:Boolhbuf:{ buf := sbuf, depth := sdepth, live := slive }.buf = []hs:{ buf := sbuf, depth := sdepth, live := slive }.live = trueh:(R.scanFrom blk { buf := sbuf, depth := sdepth, live := slive }).live = true⊢ ∃ i,
R.code i = blk ∧
R.arity i ≤ { buf := sbuf, depth := sdepth, live := slive }.depth ∧
R.scanFrom blk { buf := sbuf, depth := sdepth, live := slive } =
{ buf := [], depth := { buf := sbuf, depth := sdepth, live := slive }.depth - R.arity i + 1, live := true }
simp only at hbuf hs R:RankedAlphabetblk:List Boolhlen:blk.length = R.widthsbuf:List Boolsdepth:ℕslive:Boolhbuf:sbuf = []hs:slive = trueh:(R.scanFrom blk { buf := sbuf, depth := sdepth, live := slive }).live = true⊢ ∃ i,
R.code i = blk ∧
R.arity i ≤ { buf := sbuf, depth := sdepth, live := slive }.depth ∧
R.scanFrom blk { buf := sbuf, depth := sdepth, live := slive } =
{ buf := [], depth := { buf := sbuf, depth := sdepth, live := slive }.depth - R.arity i + 1, live := true }
subst hbuf R:RankedAlphabetblk:List Boolhlen:blk.length = R.widthsdepth:ℕslive:Boolhs:slive = trueh:(R.scanFrom blk { buf := [], depth := sdepth, live := slive }).live = true⊢ ∃ i,
R.code i = blk ∧
R.arity i ≤ { buf := [], depth := sdepth, live := slive }.depth ∧
R.scanFrom blk { buf := [], depth := sdepth, live := slive } =
{ buf := [], depth := { buf := [], depth := sdepth, live := slive }.depth - R.arity i + 1, live := true }
subst hs R:RankedAlphabetblk:List Boolhlen:blk.length = R.widthsdepth:ℕh:(R.scanFrom blk { buf := [], depth := sdepth, live := true }).live = true⊢ ∃ i,
R.code i = blk ∧
R.arity i ≤ { buf := [], depth := sdepth, live := true }.depth ∧
R.scanFrom blk { buf := [], depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity i + 1, live := true }
obtain ⟨b, v, hblk⟩ : ∃ b v, blk = b :: v := by R:RankedAlphabetblk:List Boolhlen:blk.length = R.widthsdepth:ℕh:(R.scanFrom blk { buf := [], depth := sdepth, live := true }).live = true⊢ ∃ b v, blk = b :: v
match hb : blk with
| [] => R:RankedAlphabetblk:List Boolsdepth:ℕhlen:[].length = R.widthh:(R.scanFrom [] { buf := [], depth := sdepth, live := true }).live = truehb:blk = []⊢ ∃ b v, [] = b :: v
rw [List.length_nil R:RankedAlphabetblk:List Boolsdepth:ℕhlen:0 = R.widthh:(R.scanFrom [] { buf := [], depth := sdepth, live := true }).live = truehb:blk = []⊢ ∃ b v, [] = b :: v] at hlen R:RankedAlphabetblk:List Boolsdepth:ℕhlen:0 = R.widthh:(R.scanFrom [] { buf := [], depth := sdepth, live := true }).live = truehb:blk = []⊢ ∃ b v, [] = b :: v
exact absurd hlen.symm (Nat.ne_of_gt R.width_pos) All goals completed! 🐙
| b :: v => R:RankedAlphabetblk:List Boolsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanFrom (b :: v) { buf := [], depth := sdepth, live := true }).live = truehb:blk = b :: v⊢ ∃ b_1 v_1, b :: v = b_1 :: v_1 exact ⟨b, v, rfl⟩ R:RankedAlphabetblk:List Boolhlen:blk.length = R.widthsdepth:ℕh:(R.scanFrom blk { buf := [], depth := sdepth, live := true }).live = trueb:Boolv:List Boolhblk:blk = b :: v⊢ ∃ i,
R.code i = blk ∧
R.arity i ≤ { buf := [], depth := sdepth, live := true }.depth ∧
R.scanFrom blk { buf := [], depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity i + 1, live := true }
subst hblk R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanFrom (b :: v) { buf := [], depth := sdepth, live := true }).live = true⊢ ∃ i,
R.code i = b :: v ∧
R.arity i ≤ { buf := [], depth := sdepth, live := true }.depth ∧
R.scanFrom (b :: v) { buf := [], depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity i + 1, live := true }
have hshort : v.length + ([] : List Bool).length < R.width := by R:RankedAlphabetblk:List Bools:Scanhlen:blk.length = R.widthhbuf:s.buf = []hs:s.live = trueh:(R.scanFrom blk s).live = true⊢ ∃ i,
R.code i = blk ∧
R.arity i ≤ s.depth ∧ R.scanFrom blk s = { buf := [], depth := s.depth - R.arity i + 1, live := true }
rw [List.length_cons R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:v.length + 1 = R.widthh:(R.scanFrom (b :: v) { buf := [], depth := sdepth, live := true }).live = true⊢ v.length + [].length < R.width] at hlen R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:v.length + 1 = R.widthh:(R.scanFrom (b :: v) { buf := [], depth := sdepth, live := true }).live = true⊢ v.length + [].length < R.width
simp only [List.length_nil] R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:v.length + 1 = R.widthh:(R.scanFrom (b :: v) { buf := [], depth := sdepth, live := true }).live = true⊢ v.length + 0 < R.width
omega R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanFrom (b :: v) { buf := [], depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.width⊢ ∃ i,
R.code i = b :: v ∧
R.arity i ≤ { buf := [], depth := sdepth, live := true }.depth ∧
R.scanFrom (b :: v) { buf := [], depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity i + 1, live := true }
rw [scanFrom_cons, R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b (R.scanFrom v { buf := [], depth := sdepth, live := true })).live = truehshort:v.length + [].length < R.width⊢ ∃ i,
R.code i = b :: v ∧
R.arity i ≤ { buf := [], depth := sdepth, live := true }.depth ∧
R.scanStep b (R.scanFrom v { buf := [], depth := sdepth, live := true }) =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity i + 1, live := true } scanFrom_short R sdepth [] v hshort, R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v ++ [], depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.width⊢ ∃ i,
R.code i = b :: v ∧
R.arity i ≤ { buf := [], depth := sdepth, live := true }.depth ∧
R.scanStep b { buf := v ++ [], depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity i + 1, live := true } List.append_nil R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.width⊢ ∃ i,
R.code i = b :: v ∧
R.arity i ≤ { buf := [], depth := sdepth, live := true }.depth ∧
R.scanStep b { buf := v, depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity i + 1, live := true }] at h ⊢ R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.width⊢ ∃ i,
R.code i = b :: v ∧
R.arity i ≤ { buf := [], depth := sdepth, live := true }.depth ∧
R.scanStep b { buf := v, depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity i + 1, live := true }
obtain ⟨-, hcase⟩ := scanStep_eq_of_live R b ⟨v, sdepth, true⟩ h R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthhcase:(b :: { buf := v, depth := sdepth, live := true }.buf).length ≠ R.width ∧
R.scanStep b { buf := v, depth := sdepth, live := true } =
{ buf := b :: { buf := v, depth := sdepth, live := true }.buf,
depth := { buf := v, depth := sdepth, live := true }.depth, live := true } ∨
∃ r,
(b :: { buf := v, depth := sdepth, live := true }.buf).length = R.width ∧
R.arOf (decodeBits (b :: { buf := v, depth := sdepth, live := true }.buf)) = some r ∧
r ≤ { buf := v, depth := sdepth, live := true }.depth ∧
R.scanStep b { buf := v, depth := sdepth, live := true } =
{ buf := [], depth := { buf := v, depth := sdepth, live := true }.depth - r + 1, live := true }⊢ ∃ i,
R.code i = b :: v ∧
R.arity i ≤ { buf := [], depth := sdepth, live := true }.depth ∧
R.scanStep b { buf := v, depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity i + 1, live := true }
simp only at hcase R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthhcase:(b :: v).length ≠ R.width ∧
R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := b :: v, depth := sdepth, live := true } ∨
∃ r,
(b :: v).length = R.width ∧
R.arOf (decodeBits (b :: v)) = some r ∧
r ≤ sdepth ∧
R.scanStep b { buf := v, depth := sdepth, live := true } =
{ buf := [], depth := sdepth - r + 1, live := true }⊢ ∃ i,
R.code i = b :: v ∧
R.arity i ≤ { buf := [], depth := sdepth, live := true }.depth ∧
R.scanStep b { buf := v, depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity i + 1, live := true }
rcases hcase with ⟨hne, -⟩ | ⟨r, -, har, hle, heq⟩ inl R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthhne:(b :: v).length ≠ R.width⊢ ∃ i,
R.code i = b :: v ∧
R.arity i ≤ { buf := [], depth := sdepth, live := true }.depth ∧
R.scanStep b { buf := v, depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity i + 1, live := true }inr R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthr:ℕhar:R.arOf (decodeBits (b :: v)) = some rhle:r ≤ sdepthheq:R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := [], depth := sdepth - r + 1, live := true }⊢ ∃ i,
R.code i = b :: v ∧
R.arity i ≤ { buf := [], depth := sdepth, live := true }.depth ∧
R.scanStep b { buf := v, depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity i + 1, live := true }
· inl R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthhne:(b :: v).length ≠ R.width⊢ ∃ i,
R.code i = b :: v ∧
R.arity i ≤ { buf := [], depth := sdepth, live := true }.depth ∧
R.scanStep b { buf := v, depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity i + 1, live := true } exact absurd hlen hne All goals completed! 🐙
· inr R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthr:ℕhar:R.arOf (decodeBits (b :: v)) = some rhle:r ≤ sdepthheq:R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := [], depth := sdepth - r + 1, live := true }⊢ ∃ i,
R.code i = b :: v ∧
R.arity i ≤ { buf := [], depth := sdepth, live := true }.depth ∧
R.scanStep b { buf := v, depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity i + 1, live := true } rw [arOf inr R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthr:ℕhar:(if h : decodeBits (b :: v) < R.card then some (R.arity ⟨decodeBits (b :: v), h⟩) else none) = some rhle:r ≤ sdepthheq:R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := [], depth := sdepth - r + 1, live := true }⊢ ∃ i,
R.code i = b :: v ∧
R.arity i ≤ { buf := [], depth := sdepth, live := true }.depth ∧
R.scanStep b { buf := v, depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity i + 1, live := true }] at har inr R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthr:ℕhar:(if h : decodeBits (b :: v) < R.card then some (R.arity ⟨decodeBits (b :: v), h⟩) else none) = some rhle:r ≤ sdepthheq:R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := [], depth := sdepth - r + 1, live := true }⊢ ∃ i,
R.code i = b :: v ∧
R.arity i ≤ { buf := [], depth := sdepth, live := true }.depth ∧
R.scanStep b { buf := v, depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity i + 1, live := true }
split at har inr.isTrue R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthr:ℕhle:r ≤ sdepthheq:R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := [], depth := sdepth - r + 1, live := true }h✝:decodeBits (b :: v) < R.cardhar:some (R.arity ⟨decodeBits (b :: v), h✝⟩) = some r⊢ ∃ i,
R.code i = b :: v ∧
R.arity i ≤ { buf := [], depth := sdepth, live := true }.depth ∧
R.scanStep b { buf := v, depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity i + 1, live := true }inr.isFalse R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthr:ℕhle:r ≤ sdepthheq:R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := [], depth := sdepth - r + 1, live := true }h✝:¬decodeBits (b :: v) < R.cardhar:none = some r⊢ ∃ i,
R.code i = b :: v ∧
R.arity i ≤ { buf := [], depth := sdepth, live := true }.depth ∧
R.scanStep b { buf := v, depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity i + 1, live := true }
· inr.isTrue R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthr:ℕhle:r ≤ sdepthheq:R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := [], depth := sdepth - r + 1, live := true }h✝:decodeBits (b :: v) < R.cardhar:some (R.arity ⟨decodeBits (b :: v), h✝⟩) = some r⊢ ∃ i,
R.code i = b :: v ∧
R.arity i ≤ { buf := [], depth := sdepth, live := true }.depth ∧
R.scanStep b { buf := v, depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity i + 1, live := true } rename_i hlt inr.isTrue R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthr:ℕhle:r ≤ sdepthheq:R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := [], depth := sdepth - r + 1, live := true }hlt:decodeBits (b :: v) < R.cardhar:some (R.arity ⟨decodeBits (b :: v), h✝⟩) = some r⊢ ∃ i,
R.code i = b :: v ∧
R.arity i ≤ { buf := [], depth := sdepth, live := true }.depth ∧
R.scanStep b { buf := v, depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity i + 1, live := true }
have hr : R.arity ⟨decodeBits (b :: v), hlt⟩ = r := Option.some.inj har inr.isTrue R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthr:ℕhle:r ≤ sdepthheq:R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := [], depth := sdepth - r + 1, live := true }hlt:decodeBits (b :: v) < R.cardhar:some (R.arity ⟨decodeBits (b :: v), h✝⟩) = some rhr:R.arity ⟨decodeBits (b :: v), hlt⟩ = r⊢ ∃ i,
R.code i = b :: v ∧
R.arity i ≤ { buf := [], depth := sdepth, live := true }.depth ∧
R.scanStep b { buf := v, depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity i + 1, live := true }
refine ⟨⟨decodeBits (b :: v), hlt⟩, ?_, hr ▸ hle, ?_⟩ inr.isTrue.refine_1 R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthr:ℕhle:r ≤ sdepthheq:R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := [], depth := sdepth - r + 1, live := true }hlt:decodeBits (b :: v) < R.cardhar:some (R.arity ⟨decodeBits (b :: v), h✝⟩) = some rhr:R.arity ⟨decodeBits (b :: v), hlt⟩ = r⊢ R.code ⟨decodeBits (b :: v), hlt⟩ = b :: vinr.isTrue.refine_2 R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthr:ℕhle:r ≤ sdepthheq:R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := [], depth := sdepth - r + 1, live := true }hlt:decodeBits (b :: v) < R.cardhar:some (R.arity ⟨decodeBits (b :: v), h✝⟩) = some rhr:R.arity ⟨decodeBits (b :: v), hlt⟩ = r⊢ R.scanStep b { buf := v, depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity ⟨decodeBits (b :: v), hlt⟩ + 1,
live := true }
· inr.isTrue.refine_1 R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthr:ℕhle:r ≤ sdepthheq:R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := [], depth := sdepth - r + 1, live := true }hlt:decodeBits (b :: v) < R.cardhar:some (R.arity ⟨decodeBits (b :: v), h✝⟩) = some rhr:R.arity ⟨decodeBits (b :: v), hlt⟩ = r⊢ R.code ⟨decodeBits (b :: v), hlt⟩ = b :: v refine List.ext_getElem (by R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthr:ℕhle:r ≤ sdepthheq:R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := [], depth := sdepth - r + 1, live := true }hlt:decodeBits (b :: v) < R.cardhar:some (R.arity ⟨decodeBits (b :: v), h✝⟩) = some rhr:R.arity ⟨decodeBits (b :: v), hlt⟩ = r⊢ (R.code ⟨decodeBits (b :: v), hlt⟩).length = (b :: v).length rw [length_code, R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthr:ℕhle:r ≤ sdepthheq:R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := [], depth := sdepth - r + 1, live := true }hlt:decodeBits (b :: v) < R.cardhar:some (R.arity ⟨decodeBits (b :: v), h✝⟩) = some rhr:R.arity ⟨decodeBits (b :: v), hlt⟩ = r⊢ R.width = (b :: v).length hlen R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthr:ℕhle:r ≤ sdepthheq:R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := [], depth := sdepth - r + 1, live := true }hlt:decodeBits (b :: v) < R.cardhar:some (R.arity ⟨decodeBits (b :: v), h✝⟩) = some rhr:R.arity ⟨decodeBits (b :: v), hlt⟩ = r⊢ R.width = R.width] All goals completed! 🐙) ?_
intro n h₁ h₂ inr.isTrue.refine_1 R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthr:ℕhle:r ≤ sdepthheq:R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := [], depth := sdepth - r + 1, live := true }hlt:decodeBits (b :: v) < R.cardhar:some (R.arity ⟨decodeBits (b :: v), h✝⟩) = some rhr:R.arity ⟨decodeBits (b :: v), hlt⟩ = rn:ℕh₁:n < (R.code ⟨decodeBits (b :: v), hlt⟩).lengthh₂:n < (b :: v).length⊢ (R.code ⟨decodeBits (b :: v), hlt⟩)[n] = (b :: v)[n]
rw [getElem_code_eq R _ n h₁, inr.isTrue.refine_1 R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthr:ℕhle:r ≤ sdepthheq:R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := [], depth := sdepth - r + 1, live := true }hlt:decodeBits (b :: v) < R.cardhar:some (R.arity ⟨decodeBits (b :: v), h✝⟩) = some rhr:R.arity ⟨decodeBits (b :: v), hlt⟩ = rn:ℕh₁:n < (R.code ⟨decodeBits (b :: v), hlt⟩).lengthh₂:n < (b :: v).length⊢ (↑⟨decodeBits (b :: v), hlt⟩).testBit n = (b :: v)[n] testBit_decodeBits (b :: v) n h₂ inr.isTrue.refine_1 R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthr:ℕhle:r ≤ sdepthheq:R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := [], depth := sdepth - r + 1, live := true }hlt:decodeBits (b :: v) < R.cardhar:some (R.arity ⟨decodeBits (b :: v), h✝⟩) = some rhr:R.arity ⟨decodeBits (b :: v), hlt⟩ = rn:ℕh₁:n < (R.code ⟨decodeBits (b :: v), hlt⟩).lengthh₂:n < (b :: v).length⊢ (b :: v)[n] = (b :: v)[n]] All goals completed! 🐙
· inr.isTrue.refine_2 R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthr:ℕhle:r ≤ sdepthheq:R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := [], depth := sdepth - r + 1, live := true }hlt:decodeBits (b :: v) < R.cardhar:some (R.arity ⟨decodeBits (b :: v), h✝⟩) = some rhr:R.arity ⟨decodeBits (b :: v), hlt⟩ = r⊢ R.scanStep b { buf := v, depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity ⟨decodeBits (b :: v), hlt⟩ + 1,
live := true } rw [hr, inr.isTrue.refine_2 R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthr:ℕhle:r ≤ sdepthheq:R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := [], depth := sdepth - r + 1, live := true }hlt:decodeBits (b :: v) < R.cardhar:some (R.arity ⟨decodeBits (b :: v), h✝⟩) = some rhr:R.arity ⟨decodeBits (b :: v), hlt⟩ = r⊢ R.scanStep b { buf := v, depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - r + 1, live := true } heq inr.isTrue.refine_2 R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthr:ℕhle:r ≤ sdepthheq:R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := [], depth := sdepth - r + 1, live := true }hlt:decodeBits (b :: v) < R.cardhar:some (R.arity ⟨decodeBits (b :: v), h✝⟩) = some rhr:R.arity ⟨decodeBits (b :: v), hlt⟩ = r⊢ { buf := [], depth := sdepth - r + 1, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - r + 1, live := true }] All goals completed! 🐙
· inr.isFalse R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthr:ℕhle:r ≤ sdepthheq:R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := [], depth := sdepth - r + 1, live := true }h✝:¬decodeBits (b :: v) < R.cardhar:none = some r⊢ ∃ i,
R.code i = b :: v ∧
R.arity i ≤ { buf := [], depth := sdepth, live := true }.depth ∧
R.scanStep b { buf := v, depth := sdepth, live := true } =
{ buf := [], depth := { buf := [], depth := sdepth, live := true }.depth - R.arity i + 1, live := true } exact absurd har (by R:RankedAlphabetsdepth:ℕb:Boolv:List Boolhlen:(b :: v).length = R.widthh:(R.scanStep b { buf := v, depth := sdepth, live := true }).live = truehshort:v.length + [].length < R.widthr:ℕhle:r ≤ sdepthheq:R.scanStep b { buf := v, depth := sdepth, live := true } = { buf := [], depth := sdepth - r + 1, live := true }h✝:¬decodeBits (b :: v) < R.cardhar:none = some r⊢ ¬none = some r simp All goals completed! 🐙)A live scan with no incomplete block and nothing pending has read nothing.
theorem eq_nil_of_live_of_buf_nil_of_depth_eq_zero (R : RankedAlphabet)
(w : List Bool) (hlive : (R.scanFinal w).live = true)
(hbuf : (R.scanFinal w).buf = []) (hd : (R.scanFinal w).depth = 0) : w = [] := by R:RankedAlphabetw:List Boolhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:(R.scanFinal w).depth = 0⊢ w = []
match w with
| [] => R:RankedAlphabetw:List Boolhlive:(R.scanFinal []).live = truehbuf:(R.scanFinal []).buf = []hd:(R.scanFinal []).depth = 0⊢ [] = [] rfl All goals completed! 🐙
| b :: v => R:RankedAlphabetw:List Boolb:Boolv:List Boolhlive:(R.scanFinal (b :: v)).live = truehbuf:(R.scanFinal (b :: v)).buf = []hd:(R.scanFinal (b :: v)).depth = 0⊢ b :: v = []
rw [scanFinal_cons R:RankedAlphabetw:List Boolb:Boolv:List Boolhlive:(R.scanStep b (R.scanFinal v)).live = truehbuf:(R.scanStep b (R.scanFinal v)).buf = []hd:(R.scanStep b (R.scanFinal v)).depth = 0⊢ b :: v = []] at hlive hbuf hd R:RankedAlphabetw:List Boolb:Boolv:List Boolhlive:(R.scanStep b (R.scanFinal v)).live = truehbuf:(R.scanStep b (R.scanFinal v)).buf = []hd:(R.scanStep b (R.scanFinal v)).depth = 0⊢ b :: v = []
obtain ⟨-, hcase⟩ := scanStep_eq_of_live R b (R.scanFinal v) hlive R:RankedAlphabetw:List Boolb:Boolv:List Boolhlive:(R.scanStep b (R.scanFinal v)).live = truehbuf:(R.scanStep b (R.scanFinal v)).buf = []hd:(R.scanStep b (R.scanFinal v)).depth = 0hcase:(b :: (R.scanFinal v).buf).length ≠ R.width ∧
R.scanStep b (R.scanFinal v) = { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true } ∨
∃ r,
(b :: (R.scanFinal v).buf).length = R.width ∧
R.arOf (decodeBits (b :: (R.scanFinal v).buf)) = some r ∧
r ≤ (R.scanFinal v).depth ∧
R.scanStep b (R.scanFinal v) = { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }⊢ b :: v = []
rcases hcase with ⟨-, heq⟩ | ⟨r, -, -, -, heq⟩ inl R:RankedAlphabetw:List Boolb:Boolv:List Boolhlive:(R.scanStep b (R.scanFinal v)).live = truehbuf:(R.scanStep b (R.scanFinal v)).buf = []hd:(R.scanStep b (R.scanFinal v)).depth = 0heq:R.scanStep b (R.scanFinal v) = { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }⊢ b :: v = []inr R:RankedAlphabetw:List Boolb:Boolv:List Boolhlive:(R.scanStep b (R.scanFinal v)).live = truehbuf:(R.scanStep b (R.scanFinal v)).buf = []hd:(R.scanStep b (R.scanFinal v)).depth = 0r:ℕheq:R.scanStep b (R.scanFinal v) = { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }⊢ b :: v = []
· inl R:RankedAlphabetw:List Boolb:Boolv:List Boolhlive:(R.scanStep b (R.scanFinal v)).live = truehbuf:(R.scanStep b (R.scanFinal v)).buf = []hd:(R.scanStep b (R.scanFinal v)).depth = 0heq:R.scanStep b (R.scanFinal v) = { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }⊢ b :: v = [] rw [heq inl R:RankedAlphabetw:List Boolb:Boolv:List Boolhlive:(R.scanStep b (R.scanFinal v)).live = truehbuf:{ buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }.buf = []hd:(R.scanStep b (R.scanFinal v)).depth = 0heq:R.scanStep b (R.scanFinal v) = { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }⊢ b :: v = []] at hbuf inl R:RankedAlphabetw:List Boolb:Boolv:List Boolhlive:(R.scanStep b (R.scanFinal v)).live = truehbuf:{ buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }.buf = []hd:(R.scanStep b (R.scanFinal v)).depth = 0heq:R.scanStep b (R.scanFinal v) = { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }⊢ b :: v = []
exact absurd hbuf (by R:RankedAlphabetw:List Boolb:Boolv:List Boolhlive:(R.scanStep b (R.scanFinal v)).live = truehbuf:{ buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }.buf = []hd:(R.scanStep b (R.scanFinal v)).depth = 0heq:R.scanStep b (R.scanFinal v) = { buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }⊢ ¬{ buf := b :: (R.scanFinal v).buf, depth := (R.scanFinal v).depth, live := true }.buf = [] simp All goals completed! 🐙)
· inr R:RankedAlphabetw:List Boolb:Boolv:List Boolhlive:(R.scanStep b (R.scanFinal v)).live = truehbuf:(R.scanStep b (R.scanFinal v)).buf = []hd:(R.scanStep b (R.scanFinal v)).depth = 0r:ℕheq:R.scanStep b (R.scanFinal v) = { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }⊢ b :: v = [] rw [heq inr R:RankedAlphabetw:List Boolb:Boolv:List Boolhlive:(R.scanStep b (R.scanFinal v)).live = truehbuf:(R.scanStep b (R.scanFinal v)).buf = []r:ℕhd:{ buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }.depth = 0heq:R.scanStep b (R.scanFinal v) = { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }⊢ b :: v = []] at hd inr R:RankedAlphabetw:List Boolb:Boolv:List Boolhlive:(R.scanStep b (R.scanFinal v)).live = truehbuf:(R.scanStep b (R.scanFinal v)).buf = []r:ℕhd:{ buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }.depth = 0heq:R.scanStep b (R.scanFinal v) = { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }⊢ b :: v = []
exact absurd hd (by R:RankedAlphabetw:List Boolb:Boolv:List Boolhlive:(R.scanStep b (R.scanFinal v)).live = truehbuf:(R.scanStep b (R.scanFinal v)).buf = []r:ℕhd:{ buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }.depth = 0heq:R.scanStep b (R.scanFinal v) = { buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }⊢ ¬{ buf := [], depth := (R.scanFinal v).depth - r + 1, live := true }.depth = 0 simp All goals completed! 🐙)
Reading off as many complete subterms as the pending count allows: a live
scan with no incomplete block and at least k pending subterms has k
spellings as a prefix. The extraction of one subterm is the hypothesis ih,
at the same bound, as in parseChildren_flatten.
theorem exists_children_append_of_le_depth (R : RankedAlphabet) (n : ℕ)
(ih : ∀ w : List Bool, w.length ≤ n → (R.scanFinal w).live = true →
(R.scanFinal w).buf = [] → 1 ≤ (R.scanFinal w).depth →
∃ t rest, R.spell t ++ rest = w ∧ (R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧
(R.scanFinal rest).depth + 1 = (R.scanFinal w).depth) :
∀ (k : ℕ) (v : List Bool), v.length ≤ n → (R.scanFinal v).live = true →
(R.scanFinal v).buf = [] → k ≤ (R.scanFinal v).depth →
∃ (ch : Fin k → R.Term) (rest : List Bool),
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧ (R.scanFinal rest).buf = [] ∧
(R.scanFinal rest).depth + k = (R.scanFinal v).depth :=
Nat.rec
(fun v _ hlive hbuf _ ↦
⟨Fin.elim0, v, by R:RankedAlphabetn:ℕih:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthv:List Boolx✝¹:v.length ≤ nhlive:(R.scanFinal v).live = truehbuf:(R.scanFinal v).buf = []x✝:Nat.zero ≤ (R.scanFinal v).depth⊢ (List.ofFn fun d ↦ R.spell d.elim0).flatten ++ v = v simp only [List.ofFn_zero, List.flatten_nil, List.nil_append] All goals completed! 🐙,
hlive, hbuf, rfl⟩)
(fun k ihk v hv hlive hbuf hk ↦ by R:RankedAlphabetn:ℕih:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthk:ℕihk:∀ (v : List Bool),
v.length ≤ n →
(R.scanFinal v).live = true →
(R.scanFinal v).buf = [] →
k ≤ (R.scanFinal v).depth →
∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k = (R.scanFinal v).depthv:List Boolhv:v.length ≤ nhlive:(R.scanFinal v).live = truehbuf:(R.scanFinal v).buf = []hk:k.succ ≤ (R.scanFinal v).depth⊢ ∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k.succ = (R.scanFinal v).depth
obtain ⟨t, v₁, ht, hlive₁, hbuf₁, hd₁⟩ := ih v hv hlive hbuf (by R:RankedAlphabetn:ℕih:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthk:ℕihk:∀ (v : List Bool),
v.length ≤ n →
(R.scanFinal v).live = true →
(R.scanFinal v).buf = [] →
k ≤ (R.scanFinal v).depth →
∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k = (R.scanFinal v).depthv:List Boolhv:v.length ≤ nhlive:(R.scanFinal v).live = truehbuf:(R.scanFinal v).buf = []hk:k.succ ≤ (R.scanFinal v).depth⊢ 1 ≤ (R.scanFinal v).depth omega All goals completed! 🐙) R:RankedAlphabetn:ℕih:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthk:ℕihk:∀ (v : List Bool),
v.length ≤ n →
(R.scanFinal v).live = true →
(R.scanFinal v).buf = [] →
k ≤ (R.scanFinal v).depth →
∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k = (R.scanFinal v).depthv:List Boolhv:v.length ≤ nhlive:(R.scanFinal v).live = truehbuf:(R.scanFinal v).buf = []hk:k.succ ≤ (R.scanFinal v).deptht:R.Termv₁:List Boolht:R.spell t ++ v₁ = vhlive₁:(R.scanFinal v₁).live = truehbuf₁:(R.scanFinal v₁).buf = []hd₁:(R.scanFinal v₁).depth + 1 = (R.scanFinal v).depth⊢ ∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k.succ = (R.scanFinal v).depth
have hv₁ : v₁.length ≤ n := by
have hle : v₁.length ≤ v.length := by R:RankedAlphabetn:ℕih:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthk:ℕihk:∀ (v : List Bool),
v.length ≤ n →
(R.scanFinal v).live = true →
(R.scanFinal v).buf = [] →
k ≤ (R.scanFinal v).depth →
∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k = (R.scanFinal v).depthv:List Boolhv:v.length ≤ nhlive:(R.scanFinal v).live = truehbuf:(R.scanFinal v).buf = []hk:k.succ ≤ (R.scanFinal v).depth⊢ ∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k.succ = (R.scanFinal v).depth rw [← ht, R:RankedAlphabetn:ℕih:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthk:ℕihk:∀ (v : List Bool),
v.length ≤ n →
(R.scanFinal v).live = true →
(R.scanFinal v).buf = [] →
k ≤ (R.scanFinal v).depth →
∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k = (R.scanFinal v).depthv:List Boolhv:v.length ≤ nhlive:(R.scanFinal v).live = truehbuf:(R.scanFinal v).buf = []hk:k.succ ≤ (R.scanFinal v).deptht:R.Termv₁:List Boolht:R.spell t ++ v₁ = vhlive₁:(R.scanFinal v₁).live = truehbuf₁:(R.scanFinal v₁).buf = []hd₁:(R.scanFinal v₁).depth + 1 = (R.scanFinal v).depth⊢ v₁.length ≤ (R.spell t ++ v₁).length List.length_append R:RankedAlphabetn:ℕih:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthk:ℕihk:∀ (v : List Bool),
v.length ≤ n →
(R.scanFinal v).live = true →
(R.scanFinal v).buf = [] →
k ≤ (R.scanFinal v).depth →
∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k = (R.scanFinal v).depthv:List Boolhv:v.length ≤ nhlive:(R.scanFinal v).live = truehbuf:(R.scanFinal v).buf = []hk:k.succ ≤ (R.scanFinal v).deptht:R.Termv₁:List Boolht:R.spell t ++ v₁ = vhlive₁:(R.scanFinal v₁).live = truehbuf₁:(R.scanFinal v₁).buf = []hd₁:(R.scanFinal v₁).depth + 1 = (R.scanFinal v).depth⊢ v₁.length ≤ (R.spell t).length + v₁.length] R:RankedAlphabetn:ℕih:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthk:ℕihk:∀ (v : List Bool),
v.length ≤ n →
(R.scanFinal v).live = true →
(R.scanFinal v).buf = [] →
k ≤ (R.scanFinal v).depth →
∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k = (R.scanFinal v).depthv:List Boolhv:v.length ≤ nhlive:(R.scanFinal v).live = truehbuf:(R.scanFinal v).buf = []hk:k.succ ≤ (R.scanFinal v).deptht:R.Termv₁:List Boolht:R.spell t ++ v₁ = vhlive₁:(R.scanFinal v₁).live = truehbuf₁:(R.scanFinal v₁).buf = []hd₁:(R.scanFinal v₁).depth + 1 = (R.scanFinal v).depth⊢ v₁.length ≤ (R.spell t).length + v₁.length; omega R:RankedAlphabetn:ℕih:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthk:ℕihk:∀ (v : List Bool),
v.length ≤ n →
(R.scanFinal v).live = true →
(R.scanFinal v).buf = [] →
k ≤ (R.scanFinal v).depth →
∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k = (R.scanFinal v).depthv:List Boolhv:v.length ≤ nhlive:(R.scanFinal v).live = truehbuf:(R.scanFinal v).buf = []hk:k.succ ≤ (R.scanFinal v).deptht:R.Termv₁:List Boolht:R.spell t ++ v₁ = vhlive₁:(R.scanFinal v₁).live = truehbuf₁:(R.scanFinal v₁).buf = []hd₁:(R.scanFinal v₁).depth + 1 = (R.scanFinal v).depthhle:v₁.length ≤ v.length⊢ v₁.length ≤ n
omega R:RankedAlphabetn:ℕih:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthk:ℕihk:∀ (v : List Bool),
v.length ≤ n →
(R.scanFinal v).live = true →
(R.scanFinal v).buf = [] →
k ≤ (R.scanFinal v).depth →
∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k = (R.scanFinal v).depthv:List Boolhv:v.length ≤ nhlive:(R.scanFinal v).live = truehbuf:(R.scanFinal v).buf = []hk:k.succ ≤ (R.scanFinal v).deptht:R.Termv₁:List Boolht:R.spell t ++ v₁ = vhlive₁:(R.scanFinal v₁).live = truehbuf₁:(R.scanFinal v₁).buf = []hd₁:(R.scanFinal v₁).depth + 1 = (R.scanFinal v).depthhv₁:v₁.length ≤ n⊢ ∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k.succ = (R.scanFinal v).depth
obtain ⟨ch, rest, hch, hlive₂, hbuf₂, hd₂⟩ :=
ihk v₁ hv₁ hlive₁ hbuf₁ (by R:RankedAlphabetn:ℕih:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthk:ℕihk:∀ (v : List Bool),
v.length ≤ n →
(R.scanFinal v).live = true →
(R.scanFinal v).buf = [] →
k ≤ (R.scanFinal v).depth →
∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k = (R.scanFinal v).depthv:List Boolhv:v.length ≤ nhlive:(R.scanFinal v).live = truehbuf:(R.scanFinal v).buf = []hk:k.succ ≤ (R.scanFinal v).deptht:R.Termv₁:List Boolht:R.spell t ++ v₁ = vhlive₁:(R.scanFinal v₁).live = truehbuf₁:(R.scanFinal v₁).buf = []hd₁:(R.scanFinal v₁).depth + 1 = (R.scanFinal v).depthhv₁:v₁.length ≤ n⊢ k ≤ (R.scanFinal v₁).depth omega All goals completed! 🐙) R:RankedAlphabetn:ℕih:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthk:ℕihk:∀ (v : List Bool),
v.length ≤ n →
(R.scanFinal v).live = true →
(R.scanFinal v).buf = [] →
k ≤ (R.scanFinal v).depth →
∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k = (R.scanFinal v).depthv:List Boolhv:v.length ≤ nhlive:(R.scanFinal v).live = truehbuf:(R.scanFinal v).buf = []hk:k.succ ≤ (R.scanFinal v).deptht:R.Termv₁:List Boolht:R.spell t ++ v₁ = vhlive₁:(R.scanFinal v₁).live = truehbuf₁:(R.scanFinal v₁).buf = []hd₁:(R.scanFinal v₁).depth + 1 = (R.scanFinal v).depthhv₁:v₁.length ≤ nch:Fin k → R.Termrest:List Boolhch:(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v₁hlive₂:(R.scanFinal rest).live = truehbuf₂:(R.scanFinal rest).buf = []hd₂:(R.scanFinal rest).depth + k = (R.scanFinal v₁).depth⊢ ∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k.succ = (R.scanFinal v).depth
refine ⟨Fin.cons t ch, rest, ?_, hlive₂, hbuf₂, by R:RankedAlphabetn:ℕih:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthk:ℕihk:∀ (v : List Bool),
v.length ≤ n →
(R.scanFinal v).live = true →
(R.scanFinal v).buf = [] →
k ≤ (R.scanFinal v).depth →
∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k = (R.scanFinal v).depthv:List Boolhv:v.length ≤ nhlive:(R.scanFinal v).live = truehbuf:(R.scanFinal v).buf = []hk:k.succ ≤ (R.scanFinal v).deptht:R.Termv₁:List Boolht:R.spell t ++ v₁ = vhlive₁:(R.scanFinal v₁).live = truehbuf₁:(R.scanFinal v₁).buf = []hd₁:(R.scanFinal v₁).depth + 1 = (R.scanFinal v).depthhv₁:v₁.length ≤ nch:Fin k → R.Termrest:List Boolhch:(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v₁hlive₂:(R.scanFinal rest).live = truehbuf₂:(R.scanFinal rest).buf = []hd₂:(R.scanFinal rest).depth + k = (R.scanFinal v₁).depth⊢ (R.scanFinal rest).depth + k.succ = (R.scanFinal v).depth omega All goals completed! 🐙⟩
rw [List.ofFn_succ R:RankedAlphabetn:ℕih:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthk:ℕihk:∀ (v : List Bool),
v.length ≤ n →
(R.scanFinal v).live = true →
(R.scanFinal v).buf = [] →
k ≤ (R.scanFinal v).depth →
∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k = (R.scanFinal v).depthv:List Boolhv:v.length ≤ nhlive:(R.scanFinal v).live = truehbuf:(R.scanFinal v).buf = []hk:k.succ ≤ (R.scanFinal v).deptht:R.Termv₁:List Boolht:R.spell t ++ v₁ = vhlive₁:(R.scanFinal v₁).live = truehbuf₁:(R.scanFinal v₁).buf = []hd₁:(R.scanFinal v₁).depth + 1 = (R.scanFinal v).depthhv₁:v₁.length ≤ nch:Fin k → R.Termrest:List Boolhch:(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v₁hlive₂:(R.scanFinal rest).live = truehbuf₂:(R.scanFinal rest).buf = []hd₂:(R.scanFinal rest).depth + k = (R.scanFinal v₁).depth⊢ (R.spell (Fin.cons t ch 0) :: List.ofFn fun i ↦ R.spell (Fin.cons t ch i.succ)).flatten ++ rest = v] R:RankedAlphabetn:ℕih:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthk:ℕihk:∀ (v : List Bool),
v.length ≤ n →
(R.scanFinal v).live = true →
(R.scanFinal v).buf = [] →
k ≤ (R.scanFinal v).depth →
∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k = (R.scanFinal v).depthv:List Boolhv:v.length ≤ nhlive:(R.scanFinal v).live = truehbuf:(R.scanFinal v).buf = []hk:k.succ ≤ (R.scanFinal v).deptht:R.Termv₁:List Boolht:R.spell t ++ v₁ = vhlive₁:(R.scanFinal v₁).live = truehbuf₁:(R.scanFinal v₁).buf = []hd₁:(R.scanFinal v₁).depth + 1 = (R.scanFinal v).depthhv₁:v₁.length ≤ nch:Fin k → R.Termrest:List Boolhch:(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v₁hlive₂:(R.scanFinal rest).live = truehbuf₂:(R.scanFinal rest).buf = []hd₂:(R.scanFinal rest).depth + k = (R.scanFinal v₁).depth⊢ (R.spell (Fin.cons t ch 0) :: List.ofFn fun i ↦ R.spell (Fin.cons t ch i.succ)).flatten ++ rest = v
simp only [Fin.cons_zero, Fin.cons_succ] R:RankedAlphabetn:ℕih:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthk:ℕihk:∀ (v : List Bool),
v.length ≤ n →
(R.scanFinal v).live = true →
(R.scanFinal v).buf = [] →
k ≤ (R.scanFinal v).depth →
∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k = (R.scanFinal v).depthv:List Boolhv:v.length ≤ nhlive:(R.scanFinal v).live = truehbuf:(R.scanFinal v).buf = []hk:k.succ ≤ (R.scanFinal v).deptht:R.Termv₁:List Boolht:R.spell t ++ v₁ = vhlive₁:(R.scanFinal v₁).live = truehbuf₁:(R.scanFinal v₁).buf = []hd₁:(R.scanFinal v₁).depth + 1 = (R.scanFinal v).depthhv₁:v₁.length ≤ nch:Fin k → R.Termrest:List Boolhch:(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v₁hlive₂:(R.scanFinal rest).live = truehbuf₂:(R.scanFinal rest).buf = []hd₂:(R.scanFinal rest).depth + k = (R.scanFinal v₁).depth⊢ (R.spell t :: List.ofFn fun i ↦ R.spell (ch i)).flatten ++ rest = v
rw [List.flatten_cons, R:RankedAlphabetn:ℕih:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthk:ℕihk:∀ (v : List Bool),
v.length ≤ n →
(R.scanFinal v).live = true →
(R.scanFinal v).buf = [] →
k ≤ (R.scanFinal v).depth →
∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k = (R.scanFinal v).depthv:List Boolhv:v.length ≤ nhlive:(R.scanFinal v).live = truehbuf:(R.scanFinal v).buf = []hk:k.succ ≤ (R.scanFinal v).deptht:R.Termv₁:List Boolht:R.spell t ++ v₁ = vhlive₁:(R.scanFinal v₁).live = truehbuf₁:(R.scanFinal v₁).buf = []hd₁:(R.scanFinal v₁).depth + 1 = (R.scanFinal v).depthhv₁:v₁.length ≤ nch:Fin k → R.Termrest:List Boolhch:(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v₁hlive₂:(R.scanFinal rest).live = truehbuf₂:(R.scanFinal rest).buf = []hd₂:(R.scanFinal rest).depth + k = (R.scanFinal v₁).depth⊢ R.spell t ++ (List.ofFn fun i ↦ R.spell (ch i)).flatten ++ rest = v List.append_assoc, R:RankedAlphabetn:ℕih:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthk:ℕihk:∀ (v : List Bool),
v.length ≤ n →
(R.scanFinal v).live = true →
(R.scanFinal v).buf = [] →
k ≤ (R.scanFinal v).depth →
∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k = (R.scanFinal v).depthv:List Boolhv:v.length ≤ nhlive:(R.scanFinal v).live = truehbuf:(R.scanFinal v).buf = []hk:k.succ ≤ (R.scanFinal v).deptht:R.Termv₁:List Boolht:R.spell t ++ v₁ = vhlive₁:(R.scanFinal v₁).live = truehbuf₁:(R.scanFinal v₁).buf = []hd₁:(R.scanFinal v₁).depth + 1 = (R.scanFinal v).depthhv₁:v₁.length ≤ nch:Fin k → R.Termrest:List Boolhch:(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v₁hlive₂:(R.scanFinal rest).live = truehbuf₂:(R.scanFinal rest).buf = []hd₂:(R.scanFinal rest).depth + k = (R.scanFinal v₁).depth⊢ R.spell t ++ ((List.ofFn fun i ↦ R.spell (ch i)).flatten ++ rest) = v hch, R:RankedAlphabetn:ℕih:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthk:ℕihk:∀ (v : List Bool),
v.length ≤ n →
(R.scanFinal v).live = true →
(R.scanFinal v).buf = [] →
k ≤ (R.scanFinal v).depth →
∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k = (R.scanFinal v).depthv:List Boolhv:v.length ≤ nhlive:(R.scanFinal v).live = truehbuf:(R.scanFinal v).buf = []hk:k.succ ≤ (R.scanFinal v).deptht:R.Termv₁:List Boolht:R.spell t ++ v₁ = vhlive₁:(R.scanFinal v₁).live = truehbuf₁:(R.scanFinal v₁).buf = []hd₁:(R.scanFinal v₁).depth + 1 = (R.scanFinal v).depthhv₁:v₁.length ≤ nch:Fin k → R.Termrest:List Boolhch:(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v₁hlive₂:(R.scanFinal rest).live = truehbuf₂:(R.scanFinal rest).buf = []hd₂:(R.scanFinal rest).depth + k = (R.scanFinal v₁).depth⊢ R.spell t ++ v₁ = v ht R:RankedAlphabetn:ℕih:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthk:ℕihk:∀ (v : List Bool),
v.length ≤ n →
(R.scanFinal v).live = true →
(R.scanFinal v).buf = [] →
k ≤ (R.scanFinal v).depth →
∃ ch rest,
(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + k = (R.scanFinal v).depthv:List Boolhv:v.length ≤ nhlive:(R.scanFinal v).live = truehbuf:(R.scanFinal v).buf = []hk:k.succ ≤ (R.scanFinal v).deptht:R.Termv₁:List Boolht:R.spell t ++ v₁ = vhlive₁:(R.scanFinal v₁).live = truehbuf₁:(R.scanFinal v₁).buf = []hd₁:(R.scanFinal v₁).depth + 1 = (R.scanFinal v).depthhv₁:v₁.length ≤ nch:Fin k → R.Termrest:List Boolhch:(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest = v₁hlive₂:(R.scanFinal rest).live = truehbuf₂:(R.scanFinal rest).buf = []hd₂:(R.scanFinal rest).depth + k = (R.scanFinal v₁).depth⊢ v = v] All goals completed! 🐙)A word whose scan is live, carries no incomplete block and leaves at least one subterm pending has a complete spelling as a prefix.
theorem exists_spell_append_of_live_of_buf_nil_of_one_le_depth (R : RankedAlphabet) :
∀ (n : ℕ) (w : List Bool), w.length ≤ n →
(R.scanFinal w).live = true → (R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest, R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧ (R.scanFinal rest).buf = [] ∧
(R.scanFinal rest).depth + 1 = (R.scanFinal w).depth :=
Nat.rec
(fun w hw _ _ hd ↦ by R:RankedAlphabetw:List Boolhw:w.length ≤ Nat.zerox✝¹:(R.scanFinal w).live = truex✝:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depth⊢ ∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧ (R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depth
have hzero : w.length ≤ 0 := hw R:RankedAlphabetw:List Boolhw:w.length ≤ Nat.zerox✝¹:(R.scanFinal w).live = truex✝:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhzero:w.length ≤ 0⊢ ∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧ (R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depth
have hnil : w = [] := List.eq_nil_of_length_eq_zero (by R:RankedAlphabetw:List Boolhw:w.length ≤ Nat.zerox✝¹:(R.scanFinal w).live = truex✝:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhzero:w.length ≤ 0⊢ w.length = 0 omega All goals completed! 🐙) R:RankedAlphabetw:List Boolhw:w.length ≤ Nat.zerox✝¹:(R.scanFinal w).live = truex✝:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhzero:w.length ≤ 0hnil:w = []⊢ ∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧ (R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depth
subst hnil R:RankedAlphabethw:[].length ≤ Nat.zerox✝¹:(R.scanFinal []).live = truex✝:(R.scanFinal []).buf = []hd:1 ≤ (R.scanFinal []).depthhzero:[].length ≤ 0⊢ ∃ t rest,
R.spell t ++ rest = [] ∧
(R.scanFinal rest).live = true ∧ (R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal []).depth
exact absurd hd (by R:RankedAlphabethw:[].length ≤ Nat.zerox✝¹:(R.scanFinal []).live = truex✝:(R.scanFinal []).buf = []hd:1 ≤ (R.scanFinal []).depthhzero:[].length ≤ 0⊢ ¬1 ≤ (R.scanFinal []).depth simp All goals completed! 🐙))
(fun n ihn w hw hlive hbuf hd ↦ by R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depth⊢ ∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧ (R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depth
have hmod : w.length % R.width = 0 := by
rw [← length_buf_scanFinal_of_live R w hlive, R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depth⊢ (R.scanFinal w).buf.length = 0 hbuf, R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depth⊢ [].length = 0 List.length_nil R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depth⊢ 0 = 0] R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0⊢ ∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧ (R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depth
have hne : w ≠ [] := by
intro hcon R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hcon:w = []⊢ False
subst hcon R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthhw:[].length ≤ n.succhlive:(R.scanFinal []).live = truehbuf:(R.scanFinal []).buf = []hd:1 ≤ (R.scanFinal []).depthhmod:[].length % R.width = 0⊢ False
exact absurd hd (by R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthhw:[].length ≤ n.succhlive:(R.scanFinal []).live = truehbuf:(R.scanFinal []).buf = []hd:1 ≤ (R.scanFinal []).depthhmod:[].length % R.width = 0⊢ ¬1 ≤ (R.scanFinal []).depth simp All goals completed! 🐙) R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []⊢ ∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧ (R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depth
have hwidth : R.width ≤ w.length := by
by_contra hcon R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hcon:¬R.width ≤ w.length⊢ False
rw [Nat.mod_eq_of_lt (Nat.lt_of_not_le hcon) R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length = 0hne:w ≠ []hcon:¬R.width ≤ w.length⊢ False] at hmod R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length = 0hne:w ≠ []hcon:¬R.width ≤ w.length⊢ False
exact hne (List.eq_nil_of_length_eq_zero hmod) R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.length⊢ ∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧ (R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depth
obtain ⟨blk, rest, hsplit, hlenblk⟩ :
∃ blk rest, blk ++ rest = w ∧ blk.length = R.width :=
⟨w.take R.width, w.drop R.width, List.take_append_drop _ _,
by R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.length⊢ (List.take R.width w).length = R.width rw [List.length_take R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.length⊢ min R.width w.length = R.width] R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.length⊢ min R.width w.length = R.width; omega All goals completed! 🐙⟩ R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.width⊢ ∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧ (R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depth
have hlensum : R.width + rest.length = w.length := by
rw [← hsplit, R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.width⊢ R.width + rest.length = (blk ++ rest).length List.length_append, R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.width⊢ R.width + rest.length = blk.length + rest.length hlenblk R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.width⊢ R.width + rest.length = R.width + rest.length] R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.length⊢ ∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧ (R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depth
have hscan : R.scanFinal w = R.scanFrom blk (R.scanFinal rest) := by
conv_lhs => rw [← hsplit] R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.length| R.scanFinal (blk ++ rest)
rw [scanFinal, R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.length⊢ R.scanFrom (blk ++ rest) { buf := [], depth := 0, live := true } = R.scanFrom blk (R.scanFinal rest) scanFrom_append R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.length⊢ R.scanFrom blk (R.scanFrom rest { buf := [], depth := 0, live := true }) = R.scanFrom blk (R.scanFinal rest)] R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.length⊢ R.scanFrom blk (R.scanFrom rest { buf := [], depth := 0, live := true }) = R.scanFrom blk (R.scanFinal rest)
rfl R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)⊢ ∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧ (R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depth
have hlive' : (R.scanFinal rest).live = true := by
cases hc : (R.scanFinal rest).live with
| true => true R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hc:(R.scanFinal rest).live = true⊢ true = true rfl All goals completed! 🐙
| false => false R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hc:(R.scanFinal rest).live = false⊢ false = true
rw [hscan, false R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhlive:(R.scanFrom blk (R.scanFinal rest)).live = truehsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hc:(R.scanFinal rest).live = false⊢ false = true scanFrom_not_live R blk _ hc false R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhlive:false = truehsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hc:(R.scanFinal rest).live = false⊢ false = true] at hlive false R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhlive:false = truehsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hc:(R.scanFinal rest).live = false⊢ false = true
exact Bool.noConfusion hlive R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = true⊢ ∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧ (R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depth
have hbuf' : (R.scanFinal rest).buf = [] := by
refine List.eq_nil_of_length_eq_zero ?_ R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = true⊢ (R.scanFinal rest).buf.length = 0
rw [length_buf_scanFinal_of_live R rest hlive', R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = true⊢ rest.length % R.width = 0
← Nat.add_mod_left R.width rest.length, R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = true⊢ (R.width + rest.length) % R.width = 0 hlensum R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = true⊢ w.length % R.width = 0] R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = true⊢ w.length % R.width = 0
exact hmod R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []⊢ ∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧ (R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depth
obtain ⟨i, hcode, hari, hstep⟩ :=
exists_code_of_scanFrom_live R blk (R.scanFinal rest) hlenblk hbuf' hlive'
(by R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []⊢ (R.scanFrom blk (R.scanFinal rest)).live = true rw [← hscan R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []⊢ (R.scanFinal w).live = true] R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []⊢ (R.scanFinal w).live = true; exact hlive All goals completed! 🐙) R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []i:Fin R.cardhcode:R.code i = blkhari:R.arity i ≤ (R.scanFinal rest).depthhstep:R.scanFrom blk (R.scanFinal rest) = { buf := [], depth := (R.scanFinal rest).depth - R.arity i + 1, live := true }⊢ ∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧ (R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depth
obtain ⟨ch, rest', hch, hlive'', hbuf'', hd''⟩ :=
exists_children_append_of_le_depth R n ihn (R.arity i) rest
(by R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []i:Fin R.cardhcode:R.code i = blkhari:R.arity i ≤ (R.scanFinal rest).depthhstep:R.scanFrom blk (R.scanFinal rest) = { buf := [], depth := (R.scanFinal rest).depth - R.arity i + 1, live := true }⊢ rest.length ≤ n have := R.width_pos R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []i:Fin R.cardhcode:R.code i = blkhari:R.arity i ≤ (R.scanFinal rest).depthhstep:R.scanFrom blk (R.scanFinal rest) = { buf := [], depth := (R.scanFinal rest).depth - R.arity i + 1, live := true }this:0 < R.width⊢ rest.length ≤ n; omega All goals completed! 🐙) hlive' hbuf' hari R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []i:Fin R.cardhcode:R.code i = blkhari:R.arity i ≤ (R.scanFinal rest).depthhstep:R.scanFrom blk (R.scanFinal rest) = { buf := [], depth := (R.scanFinal rest).depth - R.arity i + 1, live := true }ch:Fin (R.arity i) → R.Termrest':List Boolhch:(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest' = resthlive'':(R.scanFinal rest').live = truehbuf'':(R.scanFinal rest').buf = []hd'':(R.scanFinal rest').depth + R.arity i = (R.scanFinal rest).depth⊢ ∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧ (R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depth
refine ⟨Term.mk R i ch, rest', ?_, hlive'', hbuf'', ?_⟩ refine_1 R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []i:Fin R.cardhcode:R.code i = blkhari:R.arity i ≤ (R.scanFinal rest).depthhstep:R.scanFrom blk (R.scanFinal rest) = { buf := [], depth := (R.scanFinal rest).depth - R.arity i + 1, live := true }ch:Fin (R.arity i) → R.Termrest':List Boolhch:(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest' = resthlive'':(R.scanFinal rest').live = truehbuf'':(R.scanFinal rest').buf = []hd'':(R.scanFinal rest').depth + R.arity i = (R.scanFinal rest).depth⊢ R.spell (Term.mk R i ch) ++ rest' = wrefine_2 R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []i:Fin R.cardhcode:R.code i = blkhari:R.arity i ≤ (R.scanFinal rest).depthhstep:R.scanFrom blk (R.scanFinal rest) = { buf := [], depth := (R.scanFinal rest).depth - R.arity i + 1, live := true }ch:Fin (R.arity i) → R.Termrest':List Boolhch:(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest' = resthlive'':(R.scanFinal rest').live = truehbuf'':(R.scanFinal rest').buf = []hd'':(R.scanFinal rest').depth + R.arity i = (R.scanFinal rest).depth⊢ (R.scanFinal rest').depth + 1 = (R.scanFinal w).depth
· refine_1 R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []i:Fin R.cardhcode:R.code i = blkhari:R.arity i ≤ (R.scanFinal rest).depthhstep:R.scanFrom blk (R.scanFinal rest) = { buf := [], depth := (R.scanFinal rest).depth - R.arity i + 1, live := true }ch:Fin (R.arity i) → R.Termrest':List Boolhch:(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest' = resthlive'':(R.scanFinal rest').live = truehbuf'':(R.scanFinal rest').buf = []hd'':(R.scanFinal rest').depth + R.arity i = (R.scanFinal rest).depth⊢ R.spell (Term.mk R i ch) ++ rest' = w rw [spell_mk, refine_1 R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []i:Fin R.cardhcode:R.code i = blkhari:R.arity i ≤ (R.scanFinal rest).depthhstep:R.scanFrom blk (R.scanFinal rest) = { buf := [], depth := (R.scanFinal rest).depth - R.arity i + 1, live := true }ch:Fin (R.arity i) → R.Termrest':List Boolhch:(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest' = resthlive'':(R.scanFinal rest').live = truehbuf'':(R.scanFinal rest').buf = []hd'':(R.scanFinal rest').depth + R.arity i = (R.scanFinal rest).depth⊢ R.code i ++ (List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest' = w hcode, refine_1 R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []i:Fin R.cardhcode:R.code i = blkhari:R.arity i ≤ (R.scanFinal rest).depthhstep:R.scanFrom blk (R.scanFinal rest) = { buf := [], depth := (R.scanFinal rest).depth - R.arity i + 1, live := true }ch:Fin (R.arity i) → R.Termrest':List Boolhch:(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest' = resthlive'':(R.scanFinal rest').live = truehbuf'':(R.scanFinal rest').buf = []hd'':(R.scanFinal rest').depth + R.arity i = (R.scanFinal rest).depth⊢ blk ++ (List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest' = w List.append_assoc, refine_1 R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []i:Fin R.cardhcode:R.code i = blkhari:R.arity i ≤ (R.scanFinal rest).depthhstep:R.scanFrom blk (R.scanFinal rest) = { buf := [], depth := (R.scanFinal rest).depth - R.arity i + 1, live := true }ch:Fin (R.arity i) → R.Termrest':List Boolhch:(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest' = resthlive'':(R.scanFinal rest').live = truehbuf'':(R.scanFinal rest').buf = []hd'':(R.scanFinal rest').depth + R.arity i = (R.scanFinal rest).depth⊢ blk ++ ((List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest') = w hch, refine_1 R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []i:Fin R.cardhcode:R.code i = blkhari:R.arity i ≤ (R.scanFinal rest).depthhstep:R.scanFrom blk (R.scanFinal rest) = { buf := [], depth := (R.scanFinal rest).depth - R.arity i + 1, live := true }ch:Fin (R.arity i) → R.Termrest':List Boolhch:(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest' = resthlive'':(R.scanFinal rest').live = truehbuf'':(R.scanFinal rest').buf = []hd'':(R.scanFinal rest').depth + R.arity i = (R.scanFinal rest).depth⊢ blk ++ rest = w hsplit refine_1 R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []i:Fin R.cardhcode:R.code i = blkhari:R.arity i ≤ (R.scanFinal rest).depthhstep:R.scanFrom blk (R.scanFinal rest) = { buf := [], depth := (R.scanFinal rest).depth - R.arity i + 1, live := true }ch:Fin (R.arity i) → R.Termrest':List Boolhch:(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest' = resthlive'':(R.scanFinal rest').live = truehbuf'':(R.scanFinal rest').buf = []hd'':(R.scanFinal rest').depth + R.arity i = (R.scanFinal rest).depth⊢ w = w] All goals completed! 🐙
· refine_2 R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []i:Fin R.cardhcode:R.code i = blkhari:R.arity i ≤ (R.scanFinal rest).depthhstep:R.scanFrom blk (R.scanFinal rest) = { buf := [], depth := (R.scanFinal rest).depth - R.arity i + 1, live := true }ch:Fin (R.arity i) → R.Termrest':List Boolhch:(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest' = resthlive'':(R.scanFinal rest').live = truehbuf'':(R.scanFinal rest').buf = []hd'':(R.scanFinal rest').depth + R.arity i = (R.scanFinal rest).depth⊢ (R.scanFinal rest').depth + 1 = (R.scanFinal w).depth rw [hscan, refine_2 R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []i:Fin R.cardhcode:R.code i = blkhari:R.arity i ≤ (R.scanFinal rest).depthhstep:R.scanFrom blk (R.scanFinal rest) = { buf := [], depth := (R.scanFinal rest).depth - R.arity i + 1, live := true }ch:Fin (R.arity i) → R.Termrest':List Boolhch:(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest' = resthlive'':(R.scanFinal rest').live = truehbuf'':(R.scanFinal rest').buf = []hd'':(R.scanFinal rest').depth + R.arity i = (R.scanFinal rest).depth⊢ (R.scanFinal rest').depth + 1 = (R.scanFrom blk (R.scanFinal rest)).depth hstep refine_2 R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []i:Fin R.cardhcode:R.code i = blkhari:R.arity i ≤ (R.scanFinal rest).depthhstep:R.scanFrom blk (R.scanFinal rest) = { buf := [], depth := (R.scanFinal rest).depth - R.arity i + 1, live := true }ch:Fin (R.arity i) → R.Termrest':List Boolhch:(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest' = resthlive'':(R.scanFinal rest').live = truehbuf'':(R.scanFinal rest').buf = []hd'':(R.scanFinal rest').depth + R.arity i = (R.scanFinal rest).depth⊢ (R.scanFinal rest').depth + 1 = { buf := [], depth := (R.scanFinal rest).depth - R.arity i + 1, live := true }.depth] refine_2 R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []i:Fin R.cardhcode:R.code i = blkhari:R.arity i ≤ (R.scanFinal rest).depthhstep:R.scanFrom blk (R.scanFinal rest) = { buf := [], depth := (R.scanFinal rest).depth - R.arity i + 1, live := true }ch:Fin (R.arity i) → R.Termrest':List Boolhch:(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest' = resthlive'':(R.scanFinal rest').live = truehbuf'':(R.scanFinal rest').buf = []hd'':(R.scanFinal rest').depth + R.arity i = (R.scanFinal rest).depth⊢ (R.scanFinal rest').depth + 1 = { buf := [], depth := (R.scanFinal rest).depth - R.arity i + 1, live := true }.depth
simp only [] refine_2 R:RankedAlphabetn:ℕihn:∀ (w : List Bool),
w.length ≤ n →
(R.scanFinal w).live = true →
(R.scanFinal w).buf = [] →
1 ≤ (R.scanFinal w).depth →
∃ t rest,
R.spell t ++ rest = w ∧
(R.scanFinal rest).live = true ∧
(R.scanFinal rest).buf = [] ∧ (R.scanFinal rest).depth + 1 = (R.scanFinal w).depthw:List Boolhw:w.length ≤ n.succhlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []hd:1 ≤ (R.scanFinal w).depthhmod:w.length % R.width = 0hne:w ≠ []hwidth:R.width ≤ w.lengthblk:List Boolrest:List Boolhsplit:blk ++ rest = whlenblk:blk.length = R.widthhlensum:R.width + rest.length = w.lengthhscan:R.scanFinal w = R.scanFrom blk (R.scanFinal rest)hlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []i:Fin R.cardhcode:R.code i = blkhari:R.arity i ≤ (R.scanFinal rest).depthhstep:R.scanFrom blk (R.scanFinal rest) = { buf := [], depth := (R.scanFinal rest).depth - R.arity i + 1, live := true }ch:Fin (R.arity i) → R.Termrest':List Boolhch:(List.ofFn fun d ↦ R.spell (ch d)).flatten ++ rest' = resthlive'':(R.scanFinal rest').live = truehbuf'':(R.scanFinal rest').buf = []hd'':(R.scanFinal rest').depth + R.arity i = (R.scanFinal rest).depth⊢ (R.scanFinal rest').depth + 1 = (R.scanFinal rest).depth - R.arity i + 1
omega All goals completed! 🐙)
Every valid word is a spelling: the converse of valid_spell.
theorem exists_spell_of_valid (R : RankedAlphabet) {w : List Bool} (h : R.Valid w) :
∃ t, R.spell t = w := by R:RankedAlphabetw:List Boolh:R.Valid w⊢ ∃ t, R.spell t = w
rw [Valid, R:RankedAlphabetw:List Boolh:R.validBool w = true⊢ ∃ t, R.spell t = w validBool R:RankedAlphabetw:List Boolh:((R.scanFinal w).live && (R.scanFinal w).buf.isEmpty && (R.scanFinal w).depth == 1) = true⊢ ∃ t, R.spell t = w] at h R:RankedAlphabetw:List Boolh:((R.scanFinal w).live && (R.scanFinal w).buf.isEmpty && (R.scanFinal w).depth == 1) = true⊢ ∃ t, R.spell t = w
simp only [Bool.and_eq_true, List.isEmpty_iff, beq_iff_eq] at h R:RankedAlphabetw:List Boolh:((R.scanFinal w).live = true ∧ (R.scanFinal w).buf = []) ∧ (R.scanFinal w).depth = 1⊢ ∃ t, R.spell t = w
obtain ⟨⟨hlive, hbuf⟩, hd⟩ := h R:RankedAlphabetw:List Boolhd:(R.scanFinal w).depth = 1hlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []⊢ ∃ t, R.spell t = w
obtain ⟨t, rest, he, hlive', hbuf', hd'⟩ :=
exists_spell_append_of_live_of_buf_nil_of_one_le_depth R w.length w le_rfl
hlive hbuf (by R:RankedAlphabetw:List Boolhd:(R.scanFinal w).depth = 1hlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []⊢ 1 ≤ (R.scanFinal w).depth omega All goals completed! 🐙) R:RankedAlphabetw:List Boolhd:(R.scanFinal w).depth = 1hlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []t:R.Termrest:List Boolhe:R.spell t ++ rest = whlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []hd':(R.scanFinal rest).depth + 1 = (R.scanFinal w).depth⊢ ∃ t, R.spell t = w
have hz : (R.scanFinal rest).depth = 0 := by omega R:RankedAlphabetw:List Boolhd:(R.scanFinal w).depth = 1hlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []t:R.Termrest:List Boolhe:R.spell t ++ rest = whlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []hd':(R.scanFinal rest).depth + 1 = (R.scanFinal w).depthhz:(R.scanFinal rest).depth = 0⊢ ∃ t, R.spell t = w
have hnil : rest = [] :=
eq_nil_of_live_of_buf_nil_of_depth_eq_zero R rest hlive' hbuf' hz R:RankedAlphabetw:List Boolhd:(R.scanFinal w).depth = 1hlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []t:R.Termrest:List Boolhe:R.spell t ++ rest = whlive':(R.scanFinal rest).live = truehbuf':(R.scanFinal rest).buf = []hd':(R.scanFinal rest).depth + 1 = (R.scanFinal w).depthhz:(R.scanFinal rest).depth = 0hnil:rest = []⊢ ∃ t, R.spell t = w
subst hnil R:RankedAlphabetw:List Boolhd:(R.scanFinal w).depth = 1hlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []t:R.Termhe:R.spell t ++ [] = whlive':(R.scanFinal []).live = truehbuf':(R.scanFinal []).buf = []hd':(R.scanFinal []).depth + 1 = (R.scanFinal w).depthhz:(R.scanFinal []).depth = 0⊢ ∃ t, R.spell t = w
exact ⟨t, by R:RankedAlphabetw:List Boolhd:(R.scanFinal w).depth = 1hlive:(R.scanFinal w).live = truehbuf:(R.scanFinal w).buf = []t:R.Termhe:R.spell t ++ [] = whlive':(R.scanFinal []).live = truehbuf':(R.scanFinal []).buf = []hd':(R.scanFinal []).depth + 1 = (R.scanFinal w).depthhz:(R.scanFinal []).depth = 0⊢ R.spell t = w simpa using he All goals completed! 🐙⟩The encoding's image is exactly the valid words: the characterization the recognizer's correctness is stated against.
theorem valid_iff_exists_spell (R : RankedAlphabet) (w : List Bool) :
R.Valid w ↔ ∃ t, R.spell t = w :=
⟨exists_spell_of_valid R, fun ⟨t, ht⟩ ↦ ht ▸ valid_spell R t⟩
The descent decides validity: a word is valid exactly when parse
accepts it.
theorem valid_iff_isSome_parse (R : RankedAlphabet) (w : List Bool) :
R.Valid w ↔ (R.parse w).isSome := by R:RankedAlphabetw:List Bool⊢ R.Valid w ↔ (R.parse w).isSome = true
rw [valid_iff_exists_spell R:RankedAlphabetw:List Bool⊢ (∃ t, R.spell t = w) ↔ (R.parse w).isSome = true] R:RankedAlphabetw:List Bool⊢ (∃ t, R.spell t = w) ↔ (R.parse w).isSome = true
refine ⟨fun ⟨t, ht⟩ ↦ ?_, fun h ↦ ?_⟩ refine_1 R:RankedAlphabetw:List Boolx✝:∃ t, R.spell t = wt:R.Termht:R.spell t = w⊢ (R.parse w).isSome = truerefine_2 R:RankedAlphabetw:List Boolh:(R.parse w).isSome = true⊢ ∃ t, R.spell t = w
· refine_1 R:RankedAlphabetw:List Boolx✝:∃ t, R.spell t = wt:R.Termht:R.spell t = w⊢ (R.parse w).isSome = true rw [← ht, refine_1 R:RankedAlphabetw:List Boolx✝:∃ t, R.spell t = wt:R.Termht:R.spell t = w⊢ (R.parse (R.spell t)).isSome = true parse_spell refine_1 R:RankedAlphabetw:List Boolx✝:∃ t, R.spell t = wt:R.Termht:R.spell t = w⊢ (some t).isSome = true] refine_1 R:RankedAlphabetw:List Boolx✝:∃ t, R.spell t = wt:R.Termht:R.spell t = w⊢ (some t).isSome = true
rfl All goals completed! 🐙
· refine_2 R:RankedAlphabetw:List Boolh:(R.parse w).isSome = true⊢ ∃ t, R.spell t = w obtain ⟨t, ht⟩ := Option.isSome_iff_exists.mp h refine_2 R:RankedAlphabetw:List Boolh:(R.parse w).isSome = truet:R.Termht:R.parse w = some t⊢ ∃ t, R.spell t = w
exact ⟨t, (parse_eq_some_iff R).mp ht⟩ All goals completed! 🐙endend RankedAlphabet