Documentation

Lemmas.Basic

Theorems for ParseIt.Basic

theorem ParseIt.Progress.eq_refl {ι τ : Type u} [Std.Iterator ι Id τ] [Std.Iterators.Finite ι Id] {it : Std.Iter τ} (p : Progress it it) :
p = refl it

For finite iterators, the only Progress proof from an iterator to itself is Progress.refl.

theorem ParseIt.Spec.from_start {ε ι τ α : Type u} [Std.Iterator ι Id τ] {x : Parser ε ι τ α} {Q : α → Std.Iter τ → Prop} {E : ε → Prop} (it₀ it : Std.Iter τ) (h : ⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ x ⦃ fun (a : α) (s : IterWithProgress ι τ) => s.it = it ∧ Q a s.rest; E ⦄) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it₀ ∧ s.rest = it ⦄ x ⦃ fun (a : α) (s : IterWithProgress ι τ) => s.it = it₀ ∧ Q a s.rest; E ⦄

A spec for x started at the parse start it also holds when x is started at it with an arbitrary parse start it₀, as long as the postcondition only depends on the current iterator.

theorem ParseIt.Spec.progress {ε ι τ α : Type u} [Std.Iterator ι Id τ] (x : Parser ε ι τ α) (it₀ it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it₀ ∧ s.rest = it ⦄ x ⦃ fun (x : α) (s : IterWithProgress ι τ) => s.it = it₀ ∧ (s.rest = it ∨ Advanced s.rest it); fun (x : ε) => True ⦄

Any x started at it (with parse start it₀) never moves backwards from it.

theorem ParseIt.Spec.default_parser {ε ι τ α : Type u} [Std.Iterator ι Id τ] [Std.Iterators.Finite ι Id] (x : Parser ε ι τ α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ x ⦃ fun (a : α) (s : IterWithProgress ι τ) => s.it = it ∧ x s.it = Result.ok s.rest s.p a; fun (e : ε) => x it = Result.error e ⦄

Generic specification of an arbitrary parser x started at it: on success the final state is exactly what x it returns, and on failure the error is the one x it returns.

theorem ParseIt.Spec.pure_parser {ε ι τ α : Type u} [Std.Iterator ι Id τ] [inst : Inhabited α] (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ pure default ⦃ fun (a : α) (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ∧ a = default; fun (x : ε) => False ⦄

pure default succeeds without consuming input and returns default.

theorem ParseIt.Spec.monadLift {ε ι τ α : Type u} [Std.Iterator ι Id τ] (default : α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ MonadLift.monadLift (MonadLiftT.monadLift default) ⦃ fun (a : α) (s : IterWithProgress ι τ) => it = s.it ∧ s.rest = it ∧ a = default; fun (x : ε) => False ⦄

Lifting a pure Id value into ParserT succeeds without consuming input and returns that value.

theorem ParseIt.Spec.orElse_Parser {ε ι τ α : Type u} [Std.Iterator ι Id τ] (p q : Parser ε ι τ α) (post : α → IterWithProgress ι τ → Prop) (epost : ε → Prop) :
⦃ fun (s : IterWithProgress ι τ) => s.it = s.rest ∧ match p s.rest with | Result.ok s' h' a => post a (s.trans s' h') | Result.error err => Std.WP.wp q post epost s ⦄ p <|> q ⦃ post; epost ⦄

p <|> q (starting with no input consumed): if p succeeds at the current position its result is used, otherwise q is run from the same position.

theorem ParseIt.Spec.seqRigth_Parser {ε ι τ α β : Type u} [Std.Iterator ι Id τ] (p : Parser ε ι τ α) (q : Parser ε ι τ β) (post : β → IterWithProgress ι τ → Prop) (epost : ε → Prop) :
⦃ fun (s : IterWithProgress ι τ) => Std.WP.wp p (fun (x : α) (s : IterWithProgress ι τ) => Std.WP.wp q post epost s) epost s ⦄ p *> q ⦃ post; epost ⦄

p *> q satisfies post whenever running p and then q does; the result of p is discarded.

@[simp]
theorem ParseIt.seqRight_pure_apply {ε ι τ α β : Type u} [Std.Iterator ι Id τ] [inst : Inhabited β] (x : Parser ε ι τ α) (it : Std.Iter τ) :
(x *> pure default) it = match x it with | Result.ok rest h value => Result.ok rest h default | Result.error e => Result.error e

Evaluation of x *> pure default: run x and replace its result by default.

theorem ParseIt.Spec.seqLeft_Parser {ε ι τ α β : Type u} [Std.Iterator ι Id τ] (p : Parser ε ι τ α) (q : Parser ε ι τ β) (post : α → IterWithProgress ι τ → Prop) (epost : ε → Prop) :
⦃ fun (s : IterWithProgress ι τ) => Std.WP.wp p (fun (a : α) (s : IterWithProgress ι τ) => match q s.rest with | Result.ok s' h' value => post a { it := s.it, rest := s', p := h'.trans s.p } | Result.error e => epost e) epost s ⦄ p <* q ⦃ post; epost ⦄

p <* q runs p and then q from where p stopped, returning the result of p; an error of either parser is propagated.

@[simp]
theorem ParseIt.seqLeft_pure_apply {ε ι τ α β : Type u} [Std.Iterator ι Id τ] [inst : Inhabited α] (x : Parser ε ι τ β) (it : Std.Iter τ) :
(pure default <* x) it = match x it with | Result.ok rest h value => Result.ok rest h default | Result.error e => Result.error e

Evaluation of pure default <* x: run x and return default instead of its result.

theorem ParseIt.Spec.withSpan {ε ι τ α : Type u} [Std.Iterator ι Id τ] {pre : IterWithProgress ι τ → Prop} {post : α → IterWithProgress ι τ → Prop} {epost : ε → Prop} [HasPos ι τ] (x : Parser ε ι τ α) (h : ⦃ pre ⦄ x ⦃ post; epost ⦄) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ∧ pre s ⦄ ParseIt.withSpan x ⦃ fun (a : α × HasPos.Pos ι × HasPos.Pos ι) (s : IterWithProgress ι τ) => s.it = it ∧ post a.fst s ∧ a.snd.fst = HasPos.pos it ∧ a.snd.snd = HasPos.pos s.rest; epost ⦄

withSpan x behaves like x and additionally returns the positions of the start iterator it and of the iterator after x.

@[simp]
theorem ParseIt.getPos_apply {ε ι τ : Type u} [Std.Iterator ι Id τ] [HasPos ι τ] (it : Std.Iter τ) :

Evaluation of getPos at it: succeed without consuming input and return the position of it.

theorem ParseIt.Spec.getPos {ε ι τ : Type u} [Std.Iterator ι Id τ] [HasPos ι τ] (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.getPos ⦃ fun (a : HasPos.Pos ι) (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ∧ a = HasPos.pos it; fun (x : ε) => False ⦄

getPos never fails: it succeeds without consuming input and returns the position of the current iterator it.

theorem ParseIt.Spec.throwUnexpected {ε ι τ α : Type u} [Std.Iterator ι Id τ] [Error ε ι τ] (input : Option τ) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.rest = it ⦄ ParseIt.throwUnexpected input ⦃ fun (x : α) (x_1 : IterWithProgress ι τ) => False; fun (e : ε) => e = Error.unexpected it input ⦄

throwUnexpected input always fails with Error.unexpected it input.

theorem ParseIt.Spec.throwUnexpectedWithMessage {ε ι τ α : Type u} [Std.Iterator ι Id τ] [Error ε ι τ] (input : Option τ) (msg : String) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.rest = it ⦄ ParseIt.throwUnexpectedWithMessage input msg ⦃ fun (x : α) (x_1 : IterWithProgress ι τ) => False; fun (e : ε) => e = Error.addMessage (Error.unexpected it input) it msg ⦄

throwUnexpectedWithMessage input msg always fails with Error.unexpected it input annotated with msg.

@[simp]
theorem ParseIt.throwUnexpectedWithMessage_eq_error {ε ι τ α : Type u} [Std.Iterator ι Id τ] [Error ε ι τ] (input : Option τ) (msg : String) (it : Std.Iter τ) :

Evaluation of throwUnexpectedWithMessage input msg at it.

theorem ParseIt.Spec.withErrorMessage {ε ι τ α : Type u} [Std.Iterator ι Id τ] {pre : IterWithProgress ι τ → Prop} {post : α → IterWithProgress ι τ → Prop} {epost : ε → Prop} [Std.Iterators.Finite ι Id] [Error ε ι τ] (x : Parser ε ι τ α) (h : ⦃ pre ⦄ x ⦃ post; epost ⦄) (msg : String) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ∧ pre s ⦄ ParseIt.withErrorMessage msg x ⦃ fun (a : α) (s : IterWithProgress ι τ) => post a s ∧ s.it = it ∧ ParseIt.withErrorMessage msg x s.it = Result.ok s.rest s.p a; fun (e : ε) => ∃ (e' : ε), epost e' ∧ e = Error.addMessage e' it msg ⦄

withErrorMessage msg x behaves like x on success; on failure the error e' of x is annotated with msg at position it.

theorem ParseIt.Spec.lookAhead {ε ι τ α : Type u} [Std.Iterator ι Id τ] {pre : IterWithProgress ι τ → Prop} {post : α → IterWithProgress ι τ → Prop} {epost : ε → Prop} (x : Parser ε ι τ α) (h : ⦃ pre ⦄ x ⦃ post; epost ⦄) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ∧ pre s ⦄ ParseIt.lookAhead x ⦃ fun (a : α) (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ∧ ∃ (rest : Std.Iter τ), ∃ (h : Progress rest s.it), post a { it := s.it, rest := rest, p := h }; epost ⦄

lookAhead x runs x without consuming input: on success the result of x is returned and the position is reset to it.

theorem ParseIt.Spec.notFollowedBy {ε ι τ α : Type u} [Std.Iterator ι Id τ] [Error ε ι τ] (x : Parser ε ι τ α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.notFollowedBy x ⦃ fun (x_1 : Unitᵤ) (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ∧ ∃ (e : ε), x it = Result.error e; fun (e : ε) => e = Error.unexpected it none ∧ ∃ (rest : Std.Iter τ), ∃ (h : Progress rest it), ∃ (a : α), x it = Result.ok rest h a ⦄

notFollowedBy x succeeds without consuming input exactly when x fails at it; otherwise it fails with Error.unexpected it none.

theorem ParseIt.optionD_of_eq {ε ι τ α : Type u} [Std.Iterator ι Id τ] (x : Parser ε ι τ α) (default : α) (it : Std.Iter τ) {r : Id (Result ε it α)} (h : optionD x default it = r) :
(∃ (it' : Std.Iter τ), ∃ (a : α), ∃ (p : Progress it' it), r = Result.ok it' p a ∧ x it = Result.ok it' p a) ∨ ∃ (e : ε), r = Result.ok it (Progress.refl it) default ∧ x it = Result.error e

optionD x default never fails: it returns the result of x if x succeeds, and default without consuming input otherwise.

theorem ParseIt.Spec.optionD {ε ι τ α : Type u} [Std.Iterator ι Id τ] (x : Parser ε ι τ α) (default : α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.optionD x default ⦃ fun (a : α) (s : IterWithProgress ι τ) => match x it with | Result.ok rest progress v => s.it = it ∧ s.rest = rest ∧ a = v | Result.error err => s.it = it ∧ s.rest = it ∧ a = default; fun (x : ε) => False ⦄

optionD x default never fails: it returns the result of x if x succeeds, and default without consuming input otherwise.

theorem ParseIt.Spec.option! {ε ι τ α : Type u} [Std.Iterator ι Id τ] [inst : Inhabited α] (x : Parser ε ι τ α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.option! x ⦃ fun (a : α) (s : IterWithProgress ι τ) => match x it with | Result.ok rest progress v => s.it = it ∧ s.rest = rest ∧ a = v | Result.error err => s.it = it ∧ s.rest = it ∧ a = default; fun (x : ε) => False ⦄

option! x never fails: it returns the result of x if x succeeds, and the Inhabited default without consuming input otherwise.

theorem ParseIt.option?_of_eq {ε ι τ α : Type u} [Std.Iterator ι Id τ] (x : Parser ε ι τ α) (it : Std.Iter τ) {r : Id (Result ε it (Option α))} (h : option? x it = r) :
(∃ (it' : Std.Iter τ), ∃ (a : α), ∃ (p : Progress it' it), r = Result.ok it' p (some a) ∧ x it = Result.ok it' p a) ∨ ∃ (e : ε), r = Result.ok it (Progress.refl it) none ∧ x it = Result.error e

option? x never fails: it returns some v if x succeeds with v, and none without consuming input otherwise.

theorem ParseIt.Spec.option? {ε ι τ α : Type u} [Std.Iterator ι Id τ] (x : Parser ε ι τ α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.option? x ⦃ fun (a : Option α) (s : IterWithProgress ι τ) => match x it with | Result.ok rest progress v => s.it = it ∧ s.rest = rest ∧ a = some v | Result.error err => s.it = it ∧ s.rest = it ∧ a = none; fun (x : ε) => False ⦄

option? x never fails: it returns some v if x succeeds with v, and none without consuming input otherwise.

theorem ParseIt.Spec.optional {ε ι τ α : Type u} [Std.Iterator ι Id τ] (x : Parser ε ι τ α) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.optional x ⦃ fun (x_1 : Unitᵤ) (s : IterWithProgress ι τ) => match x it with | Result.ok rest progress value => s.it = it ∧ s.rest = rest | Result.error err => s.it = it ∧ s.rest = it; fun (x : ε) => False ⦄

optional x never fails: it consumes what x consumes if x succeeds, and nothing otherwise.

theorem ParseIt.Spec.first {ε ι τ α : Type u} [Std.Iterator ι Id τ] [Error ε ι τ] (ps : List (Parser ε ι τ α)) (combine : ε → ε → ε) (it : Std.Iter τ) :
⦃ fun (s : IterWithProgress ι τ) => s.it = it ∧ s.rest = it ⦄ ParseIt.first ps combine ⦃ fun (a : α) (s : IterWithProgress ι τ) => s.it = it ∧ ∃ (pre : List (Parser ε ι τ α)), ∃ (post : List (Parser ε ι τ α)), ∃ (q : Parser ε ι τ α), ∃ (h : Progress s.rest it), ps = pre ++ q :: post ∧ (∀ (q' : Parser ε ι τ α), q' ∈ pre → ∃ (e' : ε), q' it = Result.error e') ∧ q it = Result.ok s.rest h a; fun (e : ε) => ps = [] ∧ e = Error.unexpected it none ∨ ∃ (e₀ : ε), ∃ (es : List ε), List.map (fun (q : Parser ε ι τ α) => q it) ps = List.map (fun (e' : ε) => Result.error e') (e₀ :: es) ∧ e = List.foldl combine e₀ es ⦄

first ps combine started at it succeeds exactly like the first parser of ps that succeeds at it, after all parsers before it failed. When all parsers fail, the error is the fold of their errors with combine, from left to right; when ps is empty, it is Error.unexpected it none.