Download lean-formalization/skclisp/Skclisp/Machine.lean from Snapkitty/snapkitty-clojure-lisp-bridge: direct link, hf CLI and curl.
- Browser
- Download file 4.28 kB
-
https://huggingface.co/Snapkitty/snapkitty-clojure-lisp-bridge/resolve/main/lean-formalization/skclisp/Skclisp/Machine.lean
- Command line
-
hf download hf://Snapkitty/snapkitty-clojure-lisp-bridge/lean-formalization/skclisp/Skclisp/Machine.lean
-
curl -L -o Machine.lean https://huggingface.co/Snapkitty/snapkitty-clojure-lisp-bridge/resolve/main/lean-formalization/skclisp/Skclisp/Machine.lean
4.28 kB
| -- SKC-LISP-WORLD: Machine Module (M02) | |
| -- Foundation: State + Execution relations | |
| -- Mirrors Coq formalization from coq/Machine/ | |
| import Std | |
| import Skclisp.Basic | |
| namespace Skclisp.Machine | |
| -- ============================================================================ | |
| -- Machine State (mirrors Coq State.v) | |
| -- ============================================================================ | |
| structure MachineState where | |
| pc : Nat -- program counter | |
| stack : Array Nat -- evaluation stack | |
| heap : Array Nat -- memory heap | |
| generation : Nat -- mutation generation | |
| halted : Bool -- execution halted | |
| deriving Repr, DecidableEq | |
| def initialState : MachineState where | |
| pc := 0 | |
| stack := #[] | |
| heap := #[] | |
| generation := 0 | |
| halted := false | |
| -- State validity invariant | |
| def isValidState (s : MachineState) : Prop := | |
| s.stack.size ≤ 256 && | |
| s.heap.size ≤ 4096 && | |
| s.generation < 2^32 | |
| -- ============================================================================ | |
| -- Instruction Set (mirrors Coq Instruction.v + Instructions.v) | |
| -- ============================================================================ | |
| inductive Instruction where | |
| | push (val : Nat) -- push literal to stack | |
| | add | sub | mul | div -- arithmetic ops | |
| | and | or | xor -- bitwise ops | |
| | capGate (slot rights : Nat) -- capability check | |
| | call (func : Nat) -- indirect call | |
| | alloc (size type_tag : Nat) -- memory allocate | |
| | load (offset : Nat) -- load from heap | |
| | store (offset : Nat) -- store to heap | |
| | jump (target : Nat) -- unconditional jump | |
| | jumpIf (target : Nat) -- conditional jump | |
| | ret -- return (halt) | |
| | stream | policyCheck | seal | readOnly -- semantic passes | |
| deriving Repr, DecidableEq | |
| def instructionSize : Instruction → Nat | |
| | push _ => 2 | |
| | add | sub | mul | div => 1 | |
| | and | or | xor => 1 | |
| | capGate _ _ => 3 | |
| | call _ => 2 | |
| | alloc _ _ => 3 | |
| | load _ => 2 | |
| | store _ => 2 | |
| | jump _ => 2 | |
| | jumpIf _ => 2 | |
| | ret => 1 | |
| | stream | policyCheck | seal | readOnly => 1 | |
| -- ============================================================================ | |
| -- Step Function (mirrors Coq StepFunction.v) | |
| -- ============================================================================ | |
| inductive StepResult where | |
| | ok (s : MachineState) | |
| | error (msg : String) | |
| def stepInstruction (s : MachineState) (instr : Instruction) : StepResult := | |
| if ¬isValidState s then | |
| .error "invalid state" | |
| else | |
| match instr with | |
| | Instruction.push v => | |
| let newStack := s.stack.push v | |
| if newStack.size > 256 then | |
| .error "stack overflow" | |
| else | |
| .ok { s with stack := newStack, pc := s.pc + 1 } | |
| | Instruction.add => | |
| if s.stack.size < 2 then | |
| .error "stack underflow" | |
| else | |
| let b := s.stack[s.stack.size - 1]! | |
| let a := s.stack[s.stack.size - 2]! | |
| let newStack := s.stack.pop.pop.push (a + b) | |
| .ok { s with stack := newStack, pc := s.pc + 1 } | |
| | Instruction.ret => | |
| .ok { s with halted := true } | |
| | _ => .ok { s with pc := s.pc + 1 } | |
| -- ============================================================================ | |
| -- Execution Relation (mirrors Coq Execution.v) | |
| -- ============================================================================ | |
| inductive ExecRelation : Instruction → MachineState → MachineState → Prop where | |
| | exec_valid : ∀ (instr : Instruction) (s s' : MachineState), | |
| StepResult.ok s' = stepInstruction s instr → | |
| ExecRelation instr s s' | |
| | exec_error : ∀ (instr : Instruction) (s : MachineState), | |
| ¬isValidState s → | |
| ExecRelation instr s s | |
| -- ============================================================================ | |
| -- Preservation Theorem (Coq target) | |
| -- ============================================================================ | |
| -- Theorem preservation : ∀ (instr : Instruction) (s s' : MachineState), | |
| -- isValidState s → | |
| -- ExecRelation instr s s' → | |
| -- isValidState s' | |
| -- [Proof deferred to Coq equivalence proof] | |
| end Skclisp.Machine | |