Download data/lean/OAI/Computability/DeterministicSum/Machine.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 5.1 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/DeterministicSum/Machine.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Computability/DeterministicSum/Machine.lean
-
curl -L -o Machine.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/DeterministicSum/Machine.lean
5.1 kB
| import Mathlib | |
| namespace OAI | |
| /-! Finite deterministic word-RAM states, exact costs and uniform decision specifications. -/ | |
| namespace DeterministicThreeSum | |
| def ThreeSUM {n : β} (x : Fin n β β€) : Prop := | |
| β i j k : Fin n, i < j β§ j < k β§ x i + x j + x k = 0 | |
| def InUniverse {n : β} (C : β) (x : Fin n β β€) : Prop := | |
| β i, |x i| β€ (n : β€) ^ C | |
| noncomputable def wordWidth (factor n : β) : β := | |
| β(factor : β) * Real.logb 2 (n + 2)ββ | |
| def wordModulus (w : β) : β := 2 ^ w | |
| inductive Operand where | |
| | literal : β β Operand | |
| | register : β β Operand | |
| deriving DecidableEq | |
| inductive BinOp where | |
| | add | sub | mul | quot | rem | band | bor | bxor | shl | shr | |
| deriving DecidableEq | |
| inductive Test where | |
| | eq | lt | le | |
| deriving DecidableEq | |
| inductive Instruction where | |
| | assign (dst : β) (src : Operand) | |
| | binary (dst : β) (op : BinOp) (left right : Operand) | |
| | load (dst : β) (address : Operand) | |
| | store (address value : Operand) | |
| | branch (test : Test) (left right : Operand) (yes no : β) | |
| | jump (target : β) | |
| | halt (answer : Bool) | |
| deriving DecidableEq | |
| abbrev Program := List Instruction | |
| structure State where | |
| pc : β | |
| registers : β β β | |
| memory : β β Option β | |
| answer : Option Bool | |
| def readOperand (w : β) (s : State) : Operand β β | |
| | .literal v => v % wordModulus w | |
| | .register r => s.registers r % wordModulus w | |
| def evalBinOp (w : β) (op : BinOp) (a b : β) : Option β := | |
| let q := wordModulus w | |
| match op with | |
| | .add => some ((a + b) % q) | |
| | .sub => some ((a + q - b) % q) | |
| | .mul => some ((a * b) % q) | |
| | .quot => if b = 0 then none else some (a / b) | |
| | .rem => if b = 0 then none else some (a % b) | |
| | .band => some ((a &&& b) % q) | |
| | .bor => some ((a ||| b) % q) | |
| | .bxor => some ((a ^^^ b) % q) | |
| | .shl => if b < w then some ((a <<< b) % q) else none | |
| | .shr => if b < w then some (a >>> b) else none | |
| def evalTest : Test β β β β β Bool | |
| | .eq, a, b => decide (a = b) | |
| | .lt, a, b => decide (a < b) | |
| | .le, a, b => decide (a β€ b) | |
| def putRegister (s : State) (dst value : β) : State := | |
| { s with registers := Function.update s.registers dst value, pc := s.pc + 1 } | |
| def step (w : β) (p : Program) (s : State) : Option State := do | |
| if s.answer.isSome then none else do | |
| let i β p[s.pc]? | |
| match i with | |
| | .assign dst src => pure (putRegister s dst (readOperand w s src)) | |
| | .binary dst op left right => | |
| let value β evalBinOp w op (readOperand w s left) (readOperand w s right) | |
| pure (putRegister s dst value) | |
| | .load dst address => | |
| let value β s.memory (readOperand w s address) | |
| pure (putRegister s dst value) | |
| | .store address value => | |
| pure { s with | |
| pc := s.pc + 1 | |
| memory := Function.update s.memory (readOperand w s address) | |
| (some (readOperand w s value)) } | |
| | .branch test left right yes no => | |
| pure { s with pc := if evalTest test (readOperand w s left) (readOperand w s right) then yes else no } | |
| | .jump target => pure { s with pc := target } | |
| | .halt answer => pure { s with answer := some answer } | |
| def encodeWord (w : β) (a : β€) : β := | |
| (a % (wordModulus w : β€)).toNat | |
| def initialState {n : β} (w : β) (x : Fin n β β€) : State where | |
| pc := 0 | |
| registers := fun _ => 0 | |
| memory := fun a => | |
| if a = 0 then some (n % wordModulus w) | |
| else if h : a - 1 < n then some (encodeWord w (x β¨a - 1, hβ©)) | |
| else none | |
| answer := none | |
| def run (w : β) (p : Program) : β β State β Option State | |
| | 0, s => some s | |
| | t + 1, s => (step w p s).bind (run w p t) | |
| def registerBound : Instruction β β | |
| | .assign d (.register r) => max (d+1) (r+1) | |
| | .assign d _ => d+1 | |
| | .binary d _ a b => max (d+1) (max (operandBound a) (operandBound b)) | |
| | .load d a => max (d+1) (operandBound a) | |
| | .store a b => max (operandBound a) (operandBound b) | |
| | .branch _ a b _ _ => max (operandBound a) (operandBound b) | |
| | _ => 0 | |
| where operandBound : Operand β β | |
| | .literal _ => 0 | |
| | .register r => r+1 | |
| def setupCost (p : Program) (n : β) : β := | |
| n + 1 + ((p.map registerBound).foldl max 0) | |
| def ProgramDecidesWithin (p : Program) (widthFactor universeExponent : β) | |
| (saving timeConstant : β) : Prop := | |
| β (n : β), 3 β€ n β β x : Fin n β β€, InUniverse universeExponent x β | |
| β (t : β) (s : State) (answer : Bool), | |
| run (wordWidth widthFactor n) p t | |
| (initialState (wordWidth widthFactor n) x) = some s β§ | |
| s.answer = some answer β§ | |
| (answer = true β ThreeSUM x) β§ | |
| (t + setupCost p n : β) β€ timeConstant * (n : β) ^ (2 - saving) | |
| noncomputable def deterministicSaving : β := (2 : β) ^ (-(1546 : β€)) | |
| def DeterministicCubicStatement : Prop := | |
| β p : Program, β C : β, 0 < C β§ | |
| ProgramDecidesWithin p 4 3 deterministicSaving C | |
| def PolynomialUniverseStatement : Prop := | |
| β C : β, 0 < C β β p : Program, β A : β, 0 < A β§ | |
| ProgramDecidesWithin p (C + 4) C deterministicSaving A | |
| end DeterministicThreeSum | |
| end OAI | |