Buckets:
cfahlgren1/math / lean /OAI /MathematicalPhysics /NavierStokes /BalancedTransport /RecorderRules.lean
| import OAI.MathematicalPhysics.NavierStokes.BalancedTransport.Model | |
| namespace OAI | |
| noncomputable section | |
| namespace BalancedTransport.Recorder | |
| structure Machine (Q Γ : Type*) where | |
| halt : Q | |
| blank : Γ | |
| transition : Q → Γ → Transition Q Γ | |
| inductive Record (Q Γ : Type*) where | |
| | stay (q : Q) (a : Γ) | |
| | left (q : Q) (a c : Γ) | |
| | right (q : Q) (a c : Γ) | |
| | extend (q : Q) (a : Γ) | |
| deriving DecidableEq, Fintype | |
| inductive Letter (Q Γ : Type*) where | |
| | work (a : Γ) | |
| | marked (a : Γ) | |
| | boundary | |
| | record (r : Record Q Γ) | |
| deriving DecidableEq, Fintype | |
| inductive Control (Q : Type*) where | |
| | ready (q : Q) | |
| | outward (q : Q) | |
| | returning (q : Q) | |
| | execute (q : Q) | |
| deriving DecidableEq, Fintype | |
| structure Configuration (Q Γ : Type*) where | |
| control : Control Q | |
| left : Stream' (Letter Q Γ) | |
| right : Stream' (Letter Q Γ) | |
| history : Stream' (Letter Q Γ) | |
| structure Tails (Q Γ : Type*) where | |
| left : Stream' (Letter Q Γ) | |
| right : Stream' (Letter Q Γ) | |
| history : Stream' (Letter Q Γ) | |
| structure Instruction (Q Γ : Type*) where | |
| sourceControl : Control Q | |
| targetControl : Control Q | |
| sourceLeft : List (Letter Q Γ) | |
| sourceRight : List (Letter Q Γ) | |
| sourceHistory : List (Letter Q Γ) | |
| targetLeft : List (Letter Q Γ) | |
| targetRight : List (Letter Q Γ) | |
| targetHistory : List (Letter Q Γ) | |
| end BalancedTransport.Recorder | |
| end | |
| noncomputable section | |
| namespace BalancedTransport.Recorder.Instruction | |
| open Stream' | |
| variable {Q Γ : Type*} | |
| def source (r : Instruction Q Γ) (t : Tails Q Γ) : Configuration Q Γ := | |
| ⟨r.sourceControl, r.sourceLeft ++ₛ t.left, | |
| r.sourceRight ++ₛ t.right, r.sourceHistory ++ₛ t.history⟩ | |
| def target (r : Instruction Q Γ) (t : Tails Q Γ) : Configuration Q Γ := | |
| ⟨r.targetControl, r.targetLeft ++ₛ t.left, | |
| r.targetRight ++ₛ t.right, r.targetHistory ++ₛ t.history⟩ | |
| def sourceTails (r : Instruction Q Γ) (c : Configuration Q Γ) : Tails Q Γ := | |
| ⟨Stream'.drop r.sourceLeft.length c.left, | |
| Stream'.drop r.sourceRight.length c.right, | |
| Stream'.drop r.sourceHistory.length c.history⟩ | |
| def targetTails (r : Instruction Q Γ) (c : Configuration Q Γ) : Tails Q Γ := | |
| ⟨Stream'.drop r.targetLeft.length c.left, | |
| Stream'.drop r.targetRight.length c.right, | |
| Stream'.drop r.targetHistory.length c.history⟩ | |
| @[simp] theorem sourceTails_source (r : Instruction Q Γ) (t : Tails Q Γ) : | |
| r.sourceTails (r.source t) = t := by | |
| cases t | |
| simp [sourceTails, source, Stream'.drop_append_stream] | |
| @[simp] theorem targetTails_target (r : Instruction Q Γ) (t : Tails Q Γ) : | |
| r.targetTails (r.target t) = t := by | |
| cases t | |
| simp [targetTails, target, Stream'.drop_append_stream] | |
| def Balanced (r : Instruction Q Γ) : Prop := | |
| r.sourceLeft.length + r.sourceRight.length + r.sourceHistory.length = | |
| r.targetLeft.length + r.targetRight.length + r.targetHistory.length | |
| end BalancedTransport.Recorder.Instruction | |
| end | |
| noncomputable section | |
| namespace BalancedTransport.Recorder | |
| open Stream' | |
| inductive Row (Q Γ : Type*) where | |
| | enter (q : Q) (a : Γ) | |
| | outward (q : Q) (c : Γ) | |
| | supply (q : Q) | |
| | backward (q : Q) (c : Γ) | |
| | restore (q : Q) (a : Γ) | |
| | stay (q : Q) (a : Γ) | |
| | left (q : Q) (a c : Γ) | |
| | right (q : Q) (a c : Γ) | |
| | extend (q : Q) (a : Γ) | |
| deriving DecidableEq, Fintype | |
| end BalancedTransport.Recorder | |
| end | |
| noncomputable section | |
| namespace BalancedTransport.Recorder.Row | |
| open Stream' | |
| variable {Q Γ : Type*} | |
| open Control Letter | |
| def valid (M : Machine Q Γ) : Row Q Γ → Prop | |
| | enter q _ | outward q _ | supply q | backward q _ | restore q _ => q ≠ M.halt | |
| | stay q a => q ≠ M.halt ∧ (M.transition q a).move = .stay | |
| | left q a _ => q ≠ M.halt ∧ (M.transition q a).move = .left | |
| | right q a _ | extend q a => q ≠ M.halt ∧ (M.transition q a).move = .right | |
| def instruction (M : Machine Q Γ) : Row Q Γ → Instruction Q Γ | |
| | enter q a => | |
| ⟨ready q, Control.outward q, [], [work a], [], [marked a], [], []⟩ | |
| | outward q c => | |
| ⟨Control.outward q, Control.outward q, [], [work c], [], [work c], [], []⟩ | |
| | supply q => | |
| ⟨Control.outward q, returning q, [], [boundary, work M.blank], [], | |
| [], [boundary], [work M.blank]⟩ | |
| | backward q c => | |
| ⟨returning q, returning q, [work c], [], [], [], [work c], []⟩ | |
| | restore q a => | |
| ⟨returning q, execute q, [marked a], [], [], [], [work a], []⟩ | |
| | stay q a => | |
| ⟨execute q, ready (M.transition q a).state, [], [work a], [work M.blank], | |
| [], [work (M.transition q a).write], [record (.stay q a)]⟩ | |
| | left q a c => | |
| ⟨execute q, ready (M.transition q a).state, [work c], [work a], [work M.blank], | |
| [], [work c, work (M.transition q a).write], [record (.left q a c)]⟩ | |
| | right q a c => | |
| ⟨execute q, ready (M.transition q a).state, [], [work a, work c], [work M.blank], | |
| [work (M.transition q a).write], [work c], [record (.right q a c)]⟩ | |
| | extend q a => | |
| ⟨execute q, ready (M.transition q a).state, | |
| [], [work a, boundary, work M.blank], [work M.blank], | |
| [work (M.transition q a).write], [work M.blank, boundary], [record (.extend q a)]⟩ | |
| theorem balanced (M : Machine Q Γ) (r : Row Q Γ) : | |
| (r.instruction M).Balanced := by | |
| cases r <;> simp [instruction, Instruction.Balanced] | |
| end BalancedTransport.Recorder.Row | |
| end | |
| noncomputable section | |
| namespace BalancedTransport.Recorder | |
| open Stream' | |
| variable {Q Γ : Type*} [DecidableEq Q] [DecidableEq Γ] | |
| open Letter Control | |
| def sourceRow (M : Machine Q Γ) (c : Configuration Q Γ) : Option (Row Q Γ) := | |
| match c.control with | |
| | ready q => | |
| if q = M.halt then none else | |
| match c.right.head with | |
| | work a => some (.enter q a) | |
| | _ => none | |
| | outward q => | |
| if q = M.halt then none else | |
| match c.right.head with | |
| | work a => some (.outward q a) | |
| | boundary => if c.right.tail.head = work M.blank then some (.supply q) else none | |
| | _ => none | |
| | returning q => | |
| if q = M.halt then none else | |
| match c.left.head with | |
| | work a => some (.backward q a) | |
| | marked a => some (.restore q a) | |
| | _ => none | |
| | execute q => | |
| if q = M.halt then none else | |
| if c.history.head = work M.blank then | |
| match c.right.head with | |
| | work a => | |
| match (M.transition q a).move with | |
| | .stay => some (.stay q a) | |
| | .left => | |
| match c.left.head with | |
| | work v => some (.left q a v) | |
| | _ => none | |
| | .right => | |
| match c.right.tail.head with | |
| | work v => some (.right q a v) | |
| | boundary => | |
| if c.right.tail.tail.head = work M.blank then some (.extend q a) else none | |
| | _ => none | |
| | _ => none | |
| else none | |
| def targetRow (c : Configuration Q Γ) : Option (Row Q Γ) := | |
| match c.control with | |
| | ready _ => | |
| match c.history.head with | |
| | record (.stay q a) => some (.stay q a) | |
| | record (.left q a v) => some (.left q a v) | |
| | record (.right q a v) => some (.right q a v) | |
| | record (.extend q a) => some (.extend q a) | |
| | _ => none | |
| | outward q => | |
| match c.left.head with | |
| | marked a => some (.enter q a) | |
| | work a => some (.outward q a) | |
| | _ => none | |
| | returning q => | |
| match c.right.head with | |
| | boundary => some (.supply q) | |
| | work a => some (.backward q a) | |
| | _ => none | |
| | execute q => | |
| match c.right.head with | |
| | work a => some (.restore q a) | |
| | _ => none | |
| @[simp] theorem sourceRow_source (M : Machine Q Γ) (r : Row Q Γ) | |
| (hr : r.valid M) (t : Tails Q Γ) : | |
| sourceRow M ((r.instruction M).source t) = some r := by | |
| cases r <;> | |
| simp_all [Row.valid, Row.instruction, Instruction.source, sourceRow, | |
| Stream'.appendStream', Stream'.head] | |
| omit [DecidableEq Q] [DecidableEq Γ] in | |
| @[simp] theorem targetRow_target (M : Machine Q Γ) (r : Row Q Γ) (t : Tails Q Γ) : | |
| targetRow ((r.instruction M).target t) = some r := by | |
| cases r <;> | |
| simp [Row.instruction, Instruction.target, targetRow, | |
| Stream'.appendStream', Stream'.head] | |
| theorem source_separation (M : Machine Q Γ) {r s : Row Q Γ} | |
| (hr : r.valid M) (hs : s.valid M) {t u : Tails Q Γ} | |
| (he : (r.instruction M).source t = (s.instruction M).source u) : r = s ∧ t = u := by | |
| have hrs : r = s := by | |
| have hh := congrArg (sourceRow M) he | |
| simpa [sourceRow_source M r hr, sourceRow_source M s hs] using hh | |
| subst s | |
| exact ⟨rfl, by simpa using congrArg (Instruction.sourceTails (r.instruction M)) he⟩ | |
| omit [DecidableEq Q] [DecidableEq Γ] in | |
| theorem target_separation (M : Machine Q Γ) {r s : Row Q Γ} {t u : Tails Q Γ} | |
| (he : (r.instruction M).target t = (s.instruction M).target u) : r = s ∧ t = u := by | |
| have hrs : r = s := by | |
| have hh := congrArg targetRow he | |
| simpa using hh | |
| subst s | |
| exact ⟨rfl, by simpa using congrArg (Instruction.targetTails (r.instruction M)) he⟩ | |
| end BalancedTransport.Recorder | |
| end | |
| noncomputable section | |
| namespace BalancedTransport.Recorder | |
| open Stream' | |
| def Step {Q Γ : Type*} (M : Machine Q Γ) (c d : Configuration Q Γ) : Prop := | |
| ∃ r : Row Q Γ, r.valid M ∧ ∃ t : Tails Q Γ, | |
| c = (r.instruction M).source t ∧ d = (r.instruction M).target t | |
| end BalancedTransport.Recorder | |
| end | |
| noncomputable section | |
| namespace BalancedTransport.Recorder | |
| open Stream' | |
| variable {Q Γ : Type*} [DecidableEq Q] [DecidableEq Γ] | |
| theorem step_deterministic (M : Machine Q Γ) {c d e : Configuration Q Γ} | |
| (hd : Step M c d) (he : Step M c e) : d = e := by | |
| obtain ⟨r, hr, t, hrt, hrd⟩ := hd | |
| obtain ⟨s, hs, u, hsu, hse⟩ := he | |
| obtain ⟨rfl, rfl⟩ := source_separation M hr hs (hrt.symm.trans hsu) | |
| exact hrd.trans hse.symm | |
| omit [DecidableEq Q] [DecidableEq Γ] in | |
| theorem step_injective (M : Machine Q Γ) {c d e : Configuration Q Γ} | |
| (hc : Step M c e) (hd : Step M d e) : c = d := by | |
| obtain ⟨r, hr, t, hrt, hre⟩ := hc | |
| obtain ⟨s, hs, u, hsu, hse⟩ := hd | |
| obtain ⟨rfl, rfl⟩ := target_separation M (hre.symm.trans hse) | |
| exact hrt.trans hsu.symm | |
| instance (M : Machine Q Γ) : DecidablePred (Row.valid M) := by | |
| intro r | |
| cases r <;> simp only [Row.valid] <;> infer_instance | |
| def table [Fintype Q] [Fintype Γ] (M : Machine Q Γ) : Finset (Row Q Γ) := | |
| Finset.univ.filter (Row.valid M) | |
| omit [DecidableEq Γ] in | |
| @[simp] theorem mem_table [Fintype Q] [Fintype Γ] (M : Machine Q Γ) (r : Row Q Γ) : | |
| r ∈ table M ↔ r.valid M := by | |
| simp [table] | |
| end BalancedTransport.Recorder | |
| end | |
| noncomputable section | |
| namespace BalancedTransport.Recorder | |
| open Stream' | |
| inductive Exec {Q Γ : Type*} (M : Machine Q Γ) : | |
| ℕ → Configuration Q Γ → Configuration Q Γ → Prop | |
| | refl (c) : Exec M 0 c c | |
| | cons {n c d e} : Step M c d → Exec M n d e → Exec M (n + 1) c e | |
| end BalancedTransport.Recorder | |
| end | |
| noncomputable section | |
| namespace BalancedTransport.Recorder | |
| open Stream' | |
| variable {Q Γ : Type*} (M : Machine Q Γ) | |
| open Letter Control | |
| theorem row_step (r : Row Q Γ) (hr : r.valid M) (t : Tails Q Γ) : | |
| Step M ((r.instruction M).source t) ((r.instruction M).target t) := | |
| ⟨r, hr, t, rfl, rfl⟩ | |
| theorem outward_scan (q : Q) (hq : q ≠ M.halt) (w : List Γ) | |
| (L R J : Stream' (Letter Q Γ)) : | |
| Exec M w.length | |
| ⟨outward q, L, w.map work ++ₛ R, J⟩ | |
| ⟨outward q, w.reverse.map work ++ₛ L, R, J⟩ := by | |
| induction w generalizing L with | |
| | nil => simpa using Exec.refl (M := M) (⟨outward q, L, R, J⟩ : Configuration Q Γ) | |
| | cons a as ih => | |
| have h := row_step M (.outward q a) hq ⟨L, as.map work ++ₛ R, J⟩ | |
| have hh := Exec.cons h (ih (work a :: L)) | |
| simpa [Row.instruction, Instruction.source, Instruction.target, | |
| Stream'.appendStream', List.reverse_cons, List.map_append] using hh | |
| theorem returning_scan (q : Q) (hq : q ≠ M.halt) (w : List Γ) | |
| (L R J : Stream' (Letter Q Γ)) : | |
| Exec M w.length | |
| ⟨returning q, w.map work ++ₛ L, R, J⟩ | |
| ⟨returning q, L, w.reverse.map work ++ₛ R, J⟩ := by | |
| induction w generalizing R with | |
| | nil => simpa using Exec.refl (M := M) (⟨returning q, L, R, J⟩ : Configuration Q Γ) | |
| | cons a as ih => | |
| have h := row_step M (.backward q a) hq ⟨as.map work ++ₛ L, R, J⟩ | |
| have hh := Exec.cons h (ih (work a :: R)) | |
| simpa [Row.instruction, Instruction.source, Instruction.target, | |
| Stream'.appendStream', List.reverse_cons, List.map_append] using hh | |
| end BalancedTransport.Recorder | |
| end | |
| end OAI | |
Xet Storage Details
- Size:
- 12.6 kB
- Xet hash:
- 05b623293ca7f583e2056caa638533b27559efa868989534e3a397f651be76b2
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.