Documentation

Lemmas.Combinators

Theorems for ParseIt.Combinators

theorem ParseIt.Spec.tokenMap {ε ι τ α : Type u} [Std.Iterator ι Id τ] [Std.Iterators.Productive ι Id] [Error ε ι τ] (f : τ → Option α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.tokenMap f ⦃ fun (a : α) (s : IterWithProgress ι τ) => s.it = it ∧ (∃ (t : τ), f t = some a) ∧ ParseIt.tokenMap f s.it = Result.ok s.rest s.p a ∧ Advanced s.rest s.it; fun (e : ε) => e = Error.unexpected it none ∨ ∃ (t : τ), f t = none ∧ e = Error.unexpected it (some t) ⦄

tokenMap f reads one token t and succeeds with a when f t = some a, consuming input. It fails with Error.unexpected it none at end of input, or with Error.unexpected it (some t) when f t = none.

theorem ParseIt.Spec.anyToken {ε ι τ : Type u} [Std.Iterator ι Id τ] [Std.Iterators.Productive ι Id] [Error ε ι τ] (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.anyToken ⦃ fun (t : τ) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.anyToken s.it = Result.ok s.rest s.p t ∧ Advanced s.rest s.it; fun (e : ε) => e = Error.unexpected it none ∨ ∃ (t : τ), e = Error.unexpected it (some t) ⦄

anyToken reads and returns one token, consuming input; it fails with an unexpected error at it.

theorem ParseIt.Spec.tokenFilter {ε ι τ : Type u} [Std.Iterator ι Id τ] [Std.Iterators.Productive ι Id] [Error ε ι τ] (test : τ → Bool) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.tokenFilter test ⦃ fun (t : τ) (s : IterWithProgress ι τ) => s.it = it ∧ (test t = true) = True ∧ ParseIt.tokenFilter test s.it = Result.ok s.rest s.p t ∧ Advanced s.rest s.it; fun (e : ε) => e = Error.unexpected it none ∨ ∃ (t : τ), (test t = true) = False ∧ e = Error.unexpected it (some t) ⦄

tokenFilter test reads one token t and succeeds when test t holds, consuming input. It fails at end of input or with Error.unexpected it (some t) when test t is false.

theorem ParseIt.Spec.token {ε ι τ : Type u} [Std.Iterator ι Id τ] [Std.Iterators.Productive ι Id] [Error ε ι τ] [BEq τ] [LawfulBEq τ] (tk : τ) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.token tk ⦃ fun (t : τ) (s : IterWithProgress ι τ) => s.it = it ∧ t = tk ∧ ParseIt.token tk s.it = Result.ok s.rest s.p t ∧ Advanced s.rest s.it; fun (e : ε) => e = Error.unexpected it none ∨ ∃ (t : τ), (t != tk) = true ∧ e = Error.unexpected it (some t) ⦄

token tk succeeds with tk when the next token equals tk, consuming input. It fails at end of input or with Error.unexpected it (some t) when the next token t differs from tk.

theorem ParseIt.Spec.tokenArray {ε ι τ : Type u} [Std.Iterator ι Id τ] [Std.Iterators.Productive ι Id] [Std.Iterators.Finite ι Id] [Error ε ι τ] [BEq τ] [LawfulBEq τ] (tks : Array τ) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.tokenArray tks ⦃ fun (r : Array τ) (s : IterWithProgress ι τ) => s.it = it ∧ r = tks ∧ ParseIt.tokenArray tks s.it = Result.ok s.rest s.p r ∧ (tks ≠ #[] → Advanced s.rest s.it); fun (e : ε) => ∃ (it' : Std.Iter τ), ∃ (t : Option τ), e = Error.unexpected it' t ⦄

tokenArray tks succeeds with tks when the input starts with the tokens of tks, consuming input if tks is non-empty; otherwise it fails with an unexpected error.

theorem ParseIt.Spec.tokenList {ε ι τ : Type u} [Std.Iterator ι Id τ] [Std.Iterators.Productive ι Id] [Std.Iterators.Finite ι Id] [Error ε ι τ] [BEq τ] [LawfulBEq τ] (tks : List τ) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.tokenList tks ⦃ fun (r : List τ) (s : IterWithProgress ι τ) => s.it = it ∧ r = tks ∧ ParseIt.tokenList tks s.it = Result.ok s.rest s.p r ∧ (tks ≠ [] → Advanced s.rest s.it); fun (e : ε) => ∃ (it' : Std.Iter τ), ∃ (t : Option τ), e = Error.unexpected it' t ⦄

tokenList tks succeeds with tks when the input starts with the tokens of tks, consuming input if tks is non-empty; otherwise it fails with an unexpected error.

theorem ParseIt.Spec.endOfInput {ε ι τ : Type u} [Std.Iterator ι Id τ] [Std.Iterators.Productive ι Id] [Error ε ι τ] (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.endOfInput ⦃ fun (x : Unitᵤ) (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it; fun (e : ε) => e = Error.unexpected it none ∨ ∃ (t : τ), e = Error.unexpected it (some t) ⦄

endOfInput succeeds without consuming input; any failure is an unexpected error at it.

theorem ParseIt.Spec.foldlP {ε ι τ α : Type u} [Std.Iterator ι Id τ] [Std.Iterators.Finite ι Id] (init : Array α) (p : ParserT ε ι τ Id α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.foldlP (fun (y : Array α) (x : α) => pure (y.push x)) init p ⦃ fun (r : Array α) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.foldlP (fun (y : Array α) (x : α) => pure (y.push x)) init p s.it = Result.ok s.rest s.p r ∧ (∀ (x : α), x ∈ r → x ∈ init ∨ ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), p it' = Result.ok it'' pr x) ∧ (r ≠ init → (∀ (s rest : Std.Iter τ) (h : Progress rest s) (x : α), p s = Result.ok rest h x → Advanced rest s) → Advanced s.rest s.it); fun (x : ε) => False ⦄

foldP never reports an error. If p always consumes input on success, foldP advances whenever the accumulator changed

theorem ParseIt.Spec.foldlM {ε ι τ α : Type u} [Std.Iterator ι Id τ] {β : Type u} [Std.Iterators.Finite ι Id] (f : β → α → Id β) (init : β) (p : ParserT ε ι τ Id α) (P : β → Prop) (hinit : P init) (hf : ∀ (y : β) (x : α), (∃ (s : Std.Iter τ), ∃ (rest : Std.Iter τ), ∃ (h : Progress rest s), p s = Result.ok rest h x) → P y → P (f y x)) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.foldlM f init p ⦃ fun (b : β) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.foldlM f init p s.it = Result.ok s.rest s.p b ∧ P b ∧ (b ≠ init → (∀ (s rest : Std.Iter τ) (h : Progress rest s) (x : α), p s = Result.ok rest h x → Advanced rest s) → Advanced s.rest s.it); fun (x : ε) => False ⦄

foldlM f init p never fails. Its result satisfies every invariant P that holds for init and is preserved by f on the outputs of p, and if p always consumes input on success, it has consumed input whenever the result differs from init.

Specs for the remaining combinators

theorem ParseIt.Spec.of_apply {ε ι τ : Type u} [Std.Iterator ι Id τ] {β : Type u} [Std.Iterators.Finite ι Id] (x : Parser ε ι τ β) (it : Std.Iter τ) (Q : β → IterWithProgress ι τ → Prop) (E : ε → Prop) (h : match x it with | Result.ok rest pr b => Q b { it := it, rest := rest, p := pr } | Result.error e => E e) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ x ⦃ fun (b : β) (s : IterWithProgress ι τ) => s.it = it ∧ x s.it = Result.ok s.rest s.p b ∧ Q b s; E ⦄

A spec for x started at it from a description of x it: on success the final state is exactly what x it returns and satisfies Q, and on failure the error satisfies E.

theorem ParseIt.Spec.take {ε ι τ α : Type u} [Std.Iterator ι Id τ] [Std.Iterators.Finite ι Id] (n : Nat) (p : ParserT ε ι τ Id α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.take n p ⦃ fun (a : Array α) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.take n p s.it = Result.ok s.rest s.p a ∧ a.size = n ∧ (∀ (x : α), x ∈ a → ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), p it' = Result.ok it'' pr x) ∧ (0 < n → (∀ (s rest : Std.Iter τ) (h : Progress rest s) (x : α), p s = Result.ok rest h x → Advanced rest s) → Advanced s.rest it); fun (e : ε) => ∃ (it'' : Std.Iter τ), p it'' = Result.error e ⦄

take n p returns exactly n outputs of p, consuming input when n > 0 and p always consumes input; on failure the error is an error of p.

theorem ParseIt.Spec.foldl {ε ι τ α : Type u} [Std.Iterator ι Id τ] {β : Type u} [Std.Iterators.Finite ι Id] (f : β → α → β) (init : β) (p : ParserT ε ι τ Id α) (P : β → Prop) (hinit : P init) (hf : ∀ (y : β) (x : α), (∃ (s : Std.Iter τ), ∃ (rest : Std.Iter τ), ∃ (h : Progress rest s), p s = Result.ok rest h x) → P y → P (f y x)) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.foldl f init p ⦃ fun (b : β) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.foldl f init p s.it = Result.ok s.rest s.p b ∧ P b ∧ (b ≠ init → (∀ (s rest : Std.Iter τ) (h : Progress rest s) (x : α), p s = Result.ok rest h x → Advanced rest s) → Advanced s.rest it); fun (x : ε) => False ⦄

foldl f init p never fails. Its result satisfies every invariant P that holds for init and is preserved by f on the outputs of p, and if p always consumes input on success, it has consumed input whenever the result differs from init.

theorem ParseIt.Spec.foldr {ε ι τ α : Type u} [Std.Iterator ι Id τ] {β : Type u} [Std.Iterators.Finite ι Id] (f : α → β → β) (p : ParserT ε ι τ Id α) (q : ParserT ε ι τ Id β) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.foldr f p q ⦃ fun (b : β) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.foldr f p q s.it = Result.ok s.rest s.p b ∧ ∃ (xs : List α), ∃ (y : β), b = List.foldr f y xs ∧ (∀ (x : α), x ∈ xs → ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), p it' = Result.ok it'' pr x) ∧ ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), q it' = Result.ok it'' pr y; fun (e : ε) => q it = Result.error e ⦄

foldr f p q returns xs.foldr f y for outputs xs of p and an output y of q; on failure the error is that of q at the start position it.

theorem ParseIt.Spec.foldlN {ε ι τ α : Type u} [Std.Iterator ι Id τ] {β : Type u} [Std.Iterators.Finite ι Id] (n : Nat) (f : β → α → β) (init : β) (p : ParserT ε ι τ Id α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.foldlN n f init p ⦃ fun (b : β) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.foldlN n f init p s.it = Result.ok s.rest s.p b ∧ ∃ (xs : List α), b = List.foldl f init xs ∧ xs.length = n ∧ (∀ (x : α), x ∈ xs → ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), p it' = Result.ok it'' pr x) ∧ (0 < n → (∀ (s rest : Std.Iter τ) (h : Progress rest s) (x : α), p s = Result.ok rest h x → Advanced rest s) → Advanced s.rest it); fun (e : ε) => ∃ (it'' : Std.Iter τ), p it'' = Result.error e ⦄

foldlN n f init p folds f over exactly n outputs of p, consuming input when n > 0 and p always consumes input; on failure the error is an error of p.

theorem ParseIt.Spec.foldlUpTo {ε ι τ α : Type u} [Std.Iterator ι Id τ] {β : Type u} [Std.Iterators.Finite ι Id] (n : Nat) (f : β → α → β) (init : β) (p : ParserT ε ι τ Id α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.foldlUpTo n f init p ⦃ fun (b : β) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.foldlUpTo n f init p s.it = Result.ok s.rest s.p b ∧ ∃ (xs : List α), b = List.foldl f init xs ∧ xs.length ≤ n ∧ (∀ (x : α), x ∈ xs → ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), p it' = Result.ok it'' pr x) ∧ (xs ≠ [] → (∀ (s rest : Std.Iter τ) (h : Progress rest s) (x : α), p s = Result.ok rest h x → Advanced rest s) → Advanced s.rest it); fun (x : ε) => False ⦄

foldlUpTo n f init p never fails: it folds f over at most n outputs of p, consuming input when it folded at least one and p always consumes input.

theorem ParseIt.Spec.foldlUntil {ε ι τ α : Type u} [Std.Iterator ι Id τ] {β γ : Type u} [Std.Iterators.Finite ι Id] (f : γ → α → γ) (init : γ) (stop : ParserT ε ι τ Id β) (p : ParserT ε ι τ Id α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.foldlUntil f init stop p ⦃ fun (b : γ × β) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.foldlUntil f init stop p s.it = Result.ok s.rest s.p b ∧ (∃ (xs : List α), b.fst = List.foldl f init xs ∧ ∀ (x : α), x ∈ xs → ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), p it' = Result.ok it'' pr x) ∧ ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), stop it' = Result.ok it'' pr b.snd; fun (e : ε) => (∃ (it'' : Std.Iter τ), p it'' = Result.error e) ∨ ∃ (it'' : Std.Iter τ), stop it'' = Result.error e ⦄

foldlUntil f init stop p folds f over outputs of p and pairs the result with an output of stop; on failure the error is an error of p or of stop.

theorem ParseIt.Spec.takeUpTo {ε ι τ α : Type u} [Std.Iterator ι Id τ] [Std.Iterators.Finite ι Id] (n : Nat) (p : ParserT ε ι τ Id α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.takeUpTo n p ⦃ fun (a : Array α) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.takeUpTo n p s.it = Result.ok s.rest s.p a ∧ a.size ≤ n ∧ (∀ (x : α), x ∈ a → ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), p it' = Result.ok it'' pr x) ∧ (a ≠ #[] → (∀ (s rest : Std.Iter τ) (h : Progress rest s) (x : α), p s = Result.ok rest h x → Advanced rest s) → Advanced s.rest it); fun (x : ε) => False ⦄

takeUpTo n p never fails: it returns at most n outputs of p, consuming input when the result is non-empty and p always consumes input.

theorem ParseIt.Spec.takeMany {ε ι τ α : Type u} [Std.Iterator ι Id τ] [Std.Iterators.Finite ι Id] (p : ParserT ε ι τ Id α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.takeMany p ⦃ fun (a : Array α) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.takeMany p s.it = Result.ok s.rest s.p a ∧ (∀ (x : α), x ∈ a → ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), p it' = Result.ok it'' pr x) ∧ (a ≠ #[] → (∀ (s rest : Std.Iter τ) (h : Progress rest s) (x : α), p s = Result.ok rest h x → Advanced rest s) → Advanced s.rest it); fun (x : ε) => False ⦄

takeMany p never fails: it returns outputs of p, consuming input when the result is non-empty and p always consumes input.

theorem ParseIt.Spec.takeMany1 {ε ι τ α : Type u} [Std.Iterator ι Id τ] [Std.Iterators.Finite ι Id] (p : ParserT ε ι τ Id α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.takeMany1 p ⦃ fun (a : Array α) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.takeMany1 p s.it = Result.ok s.rest s.p a ∧ 0 < a.size ∧ (∀ (x : α), x ∈ a → ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), p it' = Result.ok it'' pr x) ∧ ((∀ (s rest : Std.Iter τ) (h : Progress rest s) (x : α), p s = Result.ok rest h x → Advanced rest s) → Advanced s.rest it); fun (e : ε) => p it = Result.error e ⦄

takeMany1 p returns a non-empty array of outputs of p, consuming input when p always consumes input; on failure the error is that of the first p at it.

theorem ParseIt.Spec.takeManyN {ε ι τ α : Type u} [Std.Iterator ι Id τ] [Std.Iterators.Finite ι Id] (n : Nat) (p : ParserT ε ι τ Id α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.takeManyN n p ⦃ fun (a : Array α) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.takeManyN n p s.it = Result.ok s.rest s.p a ∧ n ≤ a.size ∧ (∀ (x : α), x ∈ a → ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), p it' = Result.ok it'' pr x) ∧ (0 < n → (∀ (s rest : Std.Iter τ) (h : Progress rest s) (x : α), p s = Result.ok rest h x → Advanced rest s) → Advanced s.rest it); fun (e : ε) => ∃ (it'' : Std.Iter τ), p it'' = Result.error e ⦄

takeManyN n p returns at least n outputs of p, consuming input when n > 0 and p always consumes input; on failure the error is an error of p.

theorem ParseIt.Spec.takeUntil {ε ι τ α : Type u} [Std.Iterator ι Id τ] {β : Type u} [Std.Iterators.Finite ι Id] (stop : ParserT ε ι τ Id β) (p : ParserT ε ι τ Id α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.takeUntil stop p ⦃ fun (b : Array α × β) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.takeUntil stop p s.it = Result.ok s.rest s.p b ∧ (∀ (x : α), x ∈ b.fst → ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), p it' = Result.ok it'' pr x) ∧ ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), stop it' = Result.ok it'' pr b.snd; fun (e : ε) => (∃ (it'' : Std.Iter τ), p it'' = Result.error e) ∨ ∃ (it'' : Std.Iter τ), stop it'' = Result.error e ⦄

takeUntil stop p returns outputs of p paired with an output of stop; on failure the error is an error of p or of stop.

theorem ParseIt.Spec.drop {ε ι τ α : Type u} [Std.Iterator ι Id τ] [Std.Iterators.Finite ι Id] (n : Nat) (p : ParserT ε ι τ Id α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.drop n p ⦃ fun (a : Unitᵤ) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.drop n p s.it = Result.ok s.rest s.p a ∧ (0 < n → (∀ (s rest : Std.Iter τ) (h : Progress rest s) (x : α), p s = Result.ok rest h x → Advanced rest s) → Advanced s.rest it); fun (e : ε) => ∃ (it'' : Std.Iter τ), p it'' = Result.error e ⦄

drop n p consumes input when n > 0 and p always consumes input; on failure the error is an error of p.

theorem ParseIt.Spec.dropUpTo {ε ι τ α : Type u} [Std.Iterator ι Id τ] [Std.Iterators.Finite ι Id] (n : Nat) (p : ParserT ε ι τ Id α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.dropUpTo n p ⦃ fun (a : Unitᵤ) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.dropUpTo n p s.it = Result.ok s.rest s.p a ∧ True; fun (x : ε) => False ⦄

dropUpTo n p never fails.

theorem ParseIt.Spec.dropMany {ε ι τ α : Type u} [Std.Iterator ι Id τ] [Std.Iterators.Finite ι Id] (p : ParserT ε ι τ Id α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.dropMany p ⦃ fun (a : Unitᵤ) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.dropMany p s.it = Result.ok s.rest s.p a ∧ True; fun (x : ε) => False ⦄

dropMany p never fails.

theorem ParseIt.Spec.dropMany1 {ε ι τ α : Type u} [Std.Iterator ι Id τ] [Std.Iterators.Finite ι Id] (p : ParserT ε ι τ Id α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.dropMany1 p ⦃ fun (a : Unitᵤ) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.dropMany1 p s.it = Result.ok s.rest s.p a ∧ ((∀ (s rest : Std.Iter τ) (h : Progress rest s) (x : α), p s = Result.ok rest h x → Advanced rest s) → Advanced s.rest it); fun (e : ε) => p it = Result.error e ⦄

dropMany1 p consumes input when p always consumes input; on failure the error is that of the first p at it.

theorem ParseIt.Spec.dropManyN {ε ι τ α : Type u} [Std.Iterator ι Id τ] [Std.Iterators.Finite ι Id] (n : Nat) (p : ParserT ε ι τ Id α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.dropManyN n p ⦃ fun (a : Unitᵤ) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.dropManyN n p s.it = Result.ok s.rest s.p a ∧ (0 < n → (∀ (s rest : Std.Iter τ) (h : Progress rest s) (x : α), p s = Result.ok rest h x → Advanced rest s) → Advanced s.rest it); fun (e : ε) => ∃ (it'' : Std.Iter τ), p it'' = Result.error e ⦄

dropManyN n p consumes input when n > 0 and p always consumes input; on failure the error is an error of p.

theorem ParseIt.Spec.dropUntil {ε ι τ α : Type u} [Std.Iterator ι Id τ] {β : Type u} [Std.Iterators.Finite ι Id] (stop : ParserT ε ι τ Id β) (p : ParserT ε ι τ Id α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.dropUntil stop p ⦃ fun (b : β) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.dropUntil stop p s.it = Result.ok s.rest s.p b ∧ ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), stop it' = Result.ok it'' pr b; fun (e : ε) => (∃ (it'' : Std.Iter τ), p it'' = Result.error e) ∨ ∃ (it'' : Std.Iter τ), stop it'' = Result.error e ⦄

dropUntil stop p returns an output of stop; on failure the error is an error of p or of stop.

theorem ParseIt.Spec.count {ε ι τ α : Type u} [Std.Iterator ι Id τ] [Std.Iterators.Finite ι Id] (p : ParserT ε ι τ Id α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.count p ⦃ fun (n : Natᵤ) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.count p s.it = Result.ok s.rest s.p n ∧ (↑n ≠ 0 → (∀ (s rest : Std.Iter τ) (h : Progress rest s) (x : α), p s = Result.ok rest h x → Advanced rest s) → Advanced s.rest it); fun (x : ε) => False ⦄

count p never fails, and consumes input when the count is non-zero and p always consumes input.

theorem ParseIt.Spec.countUpTo {ε ι τ α : Type u} [Std.Iterator ι Id τ] [Std.Iterators.Finite ι Id] (n : Nat) (p : ParserT ε ι τ Id α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.countUpTo n p ⦃ fun (c : Natᵤ) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.countUpTo n p s.it = Result.ok s.rest s.p c ∧ ↑c ≤ n ∧ (↑c ≠ 0 → (∀ (s rest : Std.Iter τ) (h : Progress rest s) (x : α), p s = Result.ok rest h x → Advanced rest s) → Advanced s.rest it); fun (x : ε) => False ⦄

countUpTo n p never fails: it returns a count of at most n, consuming input when the count is non-zero and p always consumes input.

theorem ParseIt.Spec.countUntil {ε ι τ α : Type u} [Std.Iterator ι Id τ] {β : Type u} [Std.Iterators.Finite ι Id] (stop : ParserT ε ι τ Id β) (p : ParserT ε ι τ Id α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.countUntil stop p ⦃ fun (b : Natᵤ × β) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.countUntil stop p s.it = Result.ok s.rest s.p b ∧ ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), stop it' = Result.ok it'' pr b.snd; fun (e : ε) => (∃ (it'' : Std.Iter τ), p it'' = Result.error e) ∨ ∃ (it'' : Std.Iter τ), stop it'' = Result.error e ⦄

countUntil stop p pairs a count with an output of stop; on failure the error is an error of p or of stop.

theorem ParseIt.Spec.endBy {ε ι τ α : Type u} [Std.Iterator ι Id τ] {β : Type u} [Std.Iterators.Finite ι Id] (sep : ParserT ε ι τ Id β) (p : ParserT ε ι τ Id α) (strict : Bool) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.endBy sep p strict ⦃ fun (a : Array α) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.endBy sep p strict s.it = Result.ok s.rest s.p a ∧ (∀ (x : α), x ∈ a → ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), p it' = Result.ok it'' pr x) ∧ (a ≠ #[] → (∀ (s rest : Std.Iter τ) (h : Progress rest s) (x : α), p s = Result.ok rest h x → Advanced rest s) → Advanced s.rest it); fun (e : ε) => strict = true ∧ ∃ (it'' : Std.Iter τ), sep it'' = Result.error e ⦄

endBy sep p strict returns outputs of p, consuming input when the result is non-empty and p always consumes input; it only fails when strict, with an error of sep.

theorem ParseIt.Spec.endBy1 {ε ι τ α : Type u} [Std.Iterator ι Id τ] {β : Type u} [Std.Iterators.Finite ι Id] (sep : ParserT ε ι τ Id β) (p : ParserT ε ι τ Id α) (strict : Bool) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.endBy1 sep p strict ⦃ fun (a : Array α) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.endBy1 sep p strict s.it = Result.ok s.rest s.p a ∧ 0 < a.size ∧ (∀ (x : α), x ∈ a → ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), p it' = Result.ok it'' pr x) ∧ ((∀ (s rest : Std.Iter τ) (h : Progress rest s) (x : α), p s = Result.ok rest h x → Advanced rest s) → Advanced s.rest it); fun (e : ε) => (p <* sep) it = Result.error e ∨ strict = true ∧ ∃ (it'' : Std.Iter τ), sep it'' = Result.error e ⦄

endBy1 sep p strict returns a non-empty array of outputs of p, consuming input when p always consumes input; on failure the error is that of the first p <* sep at it or, when strict, an error of sep.

theorem ParseIt.Spec.sepBy {ε ι τ α : Type u} [Std.Iterator ι Id τ] {β : Type u} [Std.Iterators.Finite ι Id] (sep : ParserT ε ι τ Id β) (p : ParserT ε ι τ Id α) (strict : Bool) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.sepBy sep p strict ⦃ fun (a : Array α) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.sepBy sep p strict s.it = Result.ok s.rest s.p a ∧ (∀ (x : α), x ∈ a → ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), p it' = Result.ok it'' pr x) ∧ (a ≠ #[] → (∀ (s rest : Std.Iter τ) (h : Progress rest s) (x : α), p s = Result.ok rest h x → Advanced rest s) → Advanced s.rest it); fun (e : ε) => strict = true ∧ ∃ (it'' : Std.Iter τ), p it'' = Result.error e ⦄

sepBy sep p strict returns outputs of p, consuming input when the result is non-empty and p always consumes input; it only fails when strict, with an error of p.

theorem ParseIt.Spec.sepBy1 {ε ι τ α : Type u} [Std.Iterator ι Id τ] {β : Type u} [Std.Iterators.Finite ι Id] (sep : ParserT ε ι τ Id β) (p : ParserT ε ι τ Id α) (strict : Bool) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.sepBy1 sep p strict ⦃ fun (a : Array α) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.sepBy1 sep p strict s.it = Result.ok s.rest s.p a ∧ 0 < a.size ∧ (∀ (x : α), x ∈ a → ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), p it' = Result.ok it'' pr x) ∧ ((∀ (s rest : Std.Iter τ) (h : Progress rest s) (x : α), p s = Result.ok rest h x → Advanced rest s) → Advanced s.rest it); fun (e : ε) => p it = Result.error e ∨ strict = true ∧ ∃ (it'' : Std.Iter τ), p it'' = Result.error e ⦄

sepBy1 sep p strict returns a non-empty array of outputs of p, consuming input when p always consumes input; on failure the error is that of the first p at it or, when strict, an error of p.

theorem ParseIt.Spec.sepNoEndBy {ε ι τ α : Type u} [Std.Iterator ι Id τ] {β : Type u} [Std.Iterators.Finite ι Id] (sep : ParserT ε ι τ Id β) (p : ParserT ε ι τ Id α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.sepNoEndBy sep p ⦃ fun (a : Array α) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.sepNoEndBy sep p s.it = Result.ok s.rest s.p a ∧ (∀ (x : α), x ∈ a → ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), p it' = Result.ok it'' pr x) ∧ (a ≠ #[] → (∀ (s rest : Std.Iter τ) (h : Progress rest s) (x : α), p s = Result.ok rest h x → Advanced rest s) → Advanced s.rest it); fun (e : ε) => ∃ (it'' : Std.Iter τ), p it'' = Result.error e ⦄

sepNoEndBy sep p returns outputs of p, consuming input when the result is non-empty and p always consumes input; on failure the error is an error of p.

theorem ParseIt.Spec.sepNoEndBy1 {ε ι τ α : Type u} [Std.Iterator ι Id τ] {β : Type u} [Std.Iterators.Finite ι Id] (sep : ParserT ε ι τ Id β) (p : ParserT ε ι τ Id α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.sepNoEndBy1 sep p ⦃ fun (a : Array α) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.sepNoEndBy1 sep p s.it = Result.ok s.rest s.p a ∧ 0 < a.size ∧ (∀ (x : α), x ∈ a → ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), p it' = Result.ok it'' pr x) ∧ ((∀ (s rest : Std.Iter τ) (h : Progress rest s) (x : α), p s = Result.ok rest h x → Advanced rest s) → Advanced s.rest it); fun (e : ε) => ∃ (it'' : Std.Iter τ), p it'' = Result.error e ⦄

sepNoEndBy1 sep p returns a non-empty array of outputs of p, consuming input when p always consumes input; on failure the error is an error of p.

theorem ParseIt.Spec.sepEndBy {ε ι τ α : Type u} [Std.Iterator ι Id τ] {β : Type u} [Std.Iterators.Finite ι Id] (sep : ParserT ε ι τ Id β) (p : ParserT ε ι τ Id α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.sepEndBy sep p ⦃ fun (a : Array α) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.sepEndBy sep p s.it = Result.ok s.rest s.p a ∧ (∀ (x : α), x ∈ a → ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), p it' = Result.ok it'' pr x) ∧ (a ≠ #[] → (∀ (s rest : Std.Iter τ) (h : Progress rest s) (x : α), p s = Result.ok rest h x → Advanced rest s) → Advanced s.rest it); fun (x : ε) => False ⦄

sepEndBy sep p never fails: it returns outputs of p, consuming input when the result is non-empty and p always consumes input.

theorem ParseIt.Spec.sepEndBy1 {ε ι τ α : Type u} [Std.Iterator ι Id τ] {β : Type u} [Std.Iterators.Finite ι Id] (sep : ParserT ε ι τ Id β) (p : ParserT ε ι τ Id α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.sepEndBy1 sep p ⦃ fun (a : Array α) (s : IterWithProgress ι τ) => s.it = it ∧ ParseIt.sepEndBy1 sep p s.it = Result.ok s.rest s.p a ∧ 0 < a.size ∧ (∀ (x : α), x ∈ a → ∃ (it' : Std.Iter τ), ∃ (it'' : Std.Iter τ), ∃ (pr : Progress it'' it'), p it' = Result.ok it'' pr x) ∧ ((∀ (s rest : Std.Iter τ) (h : Progress rest s) (x : α), p s = Result.ok rest h x → Advanced rest s) → Advanced s.rest it); fun (e : ε) => p it = Result.error e ⦄

sepEndBy1 sep p returns a non-empty array of outputs of p, consuming input when p always consumes input; on failure the error is that of the first p at it.