Documentation

Complexity.Language.Eval.Continuation

Source equations with normal continuations #

Stmt.evalWith observes the existing statement evaluation and runs its supplied continuation only on normal completion. Actual returns and faults bypass that continuation. The result uses ExceptT Fault (StateT Heap Part), retaining the actual final heap even on a fault, and distinguishing finite fault from divergence. Normal continuations receive the actual updated locals as ordinary arguments; assignment does not introduce another state monad or hide changes at scope exit.

The equations below follow from the existing evaluation composition laws and mathlib's partial-value monad laws. They neither define another interpreter nor reprove source execution. In particular, a call uses the selected function's existing semantic result through genuine monadic bind; its body stays opaque.

noncomputable def Complexity.Language.Stmt.evalWith {signatures : List Signature} {Γ : List Ty} {result : Ty} (stmt : Stmt signatures Γ result) (program : Program signatures) (entry : Env Γ) (next : Env Γ → ExceptT Fault (StateT Heap Part) (Value result)) :

Interpret the existing statement outcome with a normal continuation. A returned value or fault exits immediately without evaluating next.

Equations
  • One or more equations did not get rendered due to their size.
Instances For
    @[simp]
    theorem Complexity.Language.Stmt.evalWith_skip {signatures : List Signature} {Γ : List Ty} {result : Ty} (program : Program signatures) (entry : Env Γ) (next : Env Γ → ExceptT Fault (StateT Heap Part) (Value result)) :
    skip.evalWith program entry next = next entry

    Normal completion of an empty statement runs the supplied continuation.

    @[simp]
    theorem Complexity.Language.Stmt.evalWith_assign {signatures : List Signature} {Γ : List Ty} {result : Ty} (program : Program signatures) {τ : Ty} (target : Var Γ τ) (value : Prim Γ τ) (entry : Env Γ) (next : Env Γ → ExceptT Fault (StateT Heap Part) (Value result)) :
    (assign target value).evalWith program entry next = next (entry.set target (value.eval entry))

    Assignment passes the updated local environment to the normal continuation. The native action still carries only the actual shared heap; lexical values are supplied directly rather than interpreted through a second state monad.

    @[simp]
    theorem Complexity.Language.Stmt.evalWith_ret {signatures : List Signature} {Γ : List Ty} {result : Ty} (program : Program signatures) (value : Atom Γ result) (entry : Env Γ) (next : Env Γ → ExceptT Fault (StateT Heap Part) (Value result)) :
    (ret value).evalWith program entry next = pure (value.eval entry)

    Return supplies its actual mathematical value and ignores the normal tail.

    theorem Complexity.Language.Stmt.evalWith_letPrim {signatures : List Signature} {Γ : List Ty} {result : Ty} (program : Program signatures) {τ : Ty} (value : Prim Γ τ) (continuation : Stmt signatures (τ :: Γ) result) (entry : Env Γ) (next : Env Γ → ExceptT Fault (StateT Heap Part) (Value result)) :
    (letPrim value continuation).evalWith program entry next = continuation.evalWith program (Env.cons (value.eval entry) entry) fun (finish : Env (τ :: Γ)) => next finish.tail

    A primitive binding gives its actual value to the scoped body. Only normal scope exit removes that binding before invoking the outer continuation.

    theorem Complexity.Language.Stmt.evalWith_alloc {signatures : List Signature} {Γ : List Ty} {result : Ty} (program : Program signatures) {kind : CellTy} (length : Atom Γ Ty.nat) (initial : Atom Γ kind.toTy) (continuation : Stmt signatures (Ty.buffer kind :: Γ) result) (entry : Env Γ) (next : Env Γ → ExceptT Fault (StateT Heap Part) (Value result)) :
    (alloc length initial continuation).evalWith program entry next = do let buffer ← Buffer.allocM (length.eval entry) (kind.ofValue (initial.eval entry)) continuation.evalWith program (Env.cons buffer entry) fun (finish : Env (Ty.buffer kind :: Γ)) => next finish.tail

    Allocation uses the existing native heap action and passes its actual fresh handle and extended heap to the scoped body. A later fault retains that body's final heap rather than rolling allocation back.

    theorem Complexity.Language.Stmt.evalWith_consNode {signatures : List Signature} {Γ : List Ty} {result : Ty} (program : Program signatures) {kind : CellTy} (head : Atom Γ kind.toTy) (tail : Atom Γ (Ty.node kind).option) (continuation : Stmt signatures (Ty.node kind :: Γ) result) (entry : Env Γ) (next : Env Γ → ExceptT Fault (StateT Heap Part) (Value result)) :
    (consNode head tail continuation).evalWith program entry next = do let ref ← NodeRef.consM (kind.ofValue (head.eval entry)) (tail.eval entry) continuation.evalWith program (Env.cons ref entry) fun (finish : Env (Ty.node kind :: Γ)) => next finish.tail

    Node construction uses the existing native allocation action. Its fresh reference is bound in the actual extended heap, and only normal scope exit invokes the outer continuation; returns and faults keep their final heap.

    theorem Complexity.Language.Stmt.evalWith_scope {signatures : List Signature} {Γ : List Ty} {result : Ty} (program : Program signatures) (body : Stmt signatures Γ result) (entry : Env Γ) (next : Env Γ → ExceptT Fault (StateT Heap Part) (Value result)) :
    body.scope.evalWith program entry next = fun (heap : Heap) => (body.eval program { locals := entry, heap := heap }).bind fun (outcome : State Γ × Control result) => have exited := scopeExit heap outcome; match exited.2 with | Control.normal => next exited.1.locals exited.1.heap | Control.returned value => Part.some (Except.ok value, exited.1.heap) | Control.fault error => Part.some (Except.error error, exited.1.heap)

    Run the scope boundary before interpreting its control outcome. Only a safe normal exit invokes next; a safe return keeps its value, and every fault bypasses next with the boundary's actual final heap.

    theorem Complexity.Language.Stmt.evalWith_read {signatures : List Signature} {Γ : List Ty} {result : Ty} (program : Program signatures) {kind : CellTy} (buffer : Atom Γ (Ty.buffer kind)) (index : Atom Γ Ty.nat) (continuation : Stmt signatures (kind.toTy :: Γ) result) (entry : Env Γ) (next : Env Γ → ExceptT Fault (StateT Heap Part) (Value result)) :
    (read buffer index continuation).evalWith program entry next = do let value ← Buffer.readM (buffer.eval entry) (index.eval entry) continuation.evalWith program (Env.cons (kind.toValue value) entry) fun (finish : Env (kind.toTy :: Γ)) => next finish.tail

    A checked current-heap read uses the native action and binds its actual scalar cell. Faults bypass both the scoped body and the normal continuation.

    theorem Complexity.Language.Stmt.evalWith_readNode {signatures : List Signature} {Γ : List Ty} {result : Ty} (program : Program signatures) {kind : CellTy} (ref : Atom Γ (Ty.node kind)) (continuation : Stmt signatures (kind.toTy.prod (Ty.node kind).option :: Γ) result) (entry : Env Γ) (next : Env Γ → ExceptT Fault (StateT Heap Part) (Value result)) :
    (readNode ref continuation).evalWith program entry next = do let contents ← NodeRef.readM (ref.eval entry) continuation.evalWith program (Env.cons (kind.toValue contents.1, contents.2) entry) fun (finish : Env (kind.toTy.prod (Ty.node kind).option :: Γ)) => next finish.tail

    Node reading uses the same native typed lookup and passes its stored head and shared tail to the scoped body. Failure skips both continuations and keeps the actual current heap, rather than undoing earlier effects.

    theorem Complexity.Language.Stmt.evalWith_write {signatures : List Signature} {Γ : List Ty} {result : Ty} (program : Program signatures) {kind : CellTy} (buffer : Atom Γ (Ty.buffer kind)) (index : Atom Γ Ty.nat) (value : Atom Γ kind.toTy) (entry : Env Γ) (next : Env Γ → ExceptT Fault (StateT Heap Part) (Value result)) :
    (write buffer index value).evalWith program entry next = do Buffer.writeM (buffer.eval entry) (index.eval entry) (kind.ofValue (value.eval entry)) next entry

    A source write is the existing native heap action followed by normal continuation at its actual updated heap. A failed write retains prior effects.

    theorem Complexity.Language.Stmt.evalWith_slice {signatures : List Signature} {Γ : List Ty} {result : Ty} (program : Program signatures) {kind : CellTy} (buffer : Atom Γ (Ty.buffer kind)) (offset length : Atom Γ Ty.nat) (continuation : Stmt signatures (Ty.buffer kind :: Γ) result) (entry : Env Γ) (next : Env Γ → ExceptT Fault (StateT Heap Part) (Value result)) :
    (slice buffer offset length continuation).evalWith program entry next = do let view ← Buffer.sliceM (buffer.eval entry) (offset.eval entry) (length.eval entry) continuation.evalWith program (Env.cons view entry) fun (finish : Env (Ty.buffer kind :: Γ)) => next finish.tail

    Borrowing checked slice metadata is native action composition, not a snapshot of the underlying object. The actual shared heap passes to the body.

    theorem Complexity.Language.Stmt.evalWith_seq {signatures : List Signature} {Γ : List Ty} {result : Ty} (program : Program signatures) (first second : Stmt signatures Γ result) (entry : Env Γ) (next : Env Γ → ExceptT Fault (StateT Heap Part) (Value result)) :
    (first.seq second).evalWith program entry next = first.evalWith program entry fun (middle : Env Γ) => second.evalWith program middle next

    Sequencing composes only normal continuations; an early return or fault in the first statement skips both the second statement and its normal tail.

    theorem Complexity.Language.Stmt.evalWith_ite {signatures : List Signature} {Γ : List Ty} {result : Ty} (program : Program signatures) (condition : Atom Γ Ty.bool) (yes no : Stmt signatures Γ result) (entry : Env Γ) (next : Env Γ → ExceptT Fault (StateT Heap Part) (Value result)) :
    (ite condition yes no).evalWith program entry next = if condition.eval entry = true then yes.evalWith program entry next else no.evalWith program entry next

    Only the selected branch is evaluated, with the same normal continuation.

    theorem Complexity.Language.Stmt.evalWith_matchOption {signatures : List Signature} {Γ : List Ty} {result : Ty} (program : Program signatures) {τ : Ty} (value : Atom Γ τ.option) (noneBranch : Stmt signatures Γ result) (someBranch : Stmt signatures (τ :: Γ) result) (entry : Env Γ) (next : Env Γ → ExceptT Fault (StateT Heap Part) (Value result)) :
    (matchOption value noneBranch someBranch).evalWith program entry next = match value.eval entry with | none => noneBranch.evalWith program entry next | some payload => someBranch.evalWith program (Env.cons payload entry) fun (finish : Env (τ :: Γ)) => next finish.tail

    Optional values use native pattern matching. The selected some payload is an ordinary argument to its branch, and normal completion passes the actual updated outer locals to next. Returns and faults still bypass that tail.

    theorem Complexity.Language.Stmt.evalWith_call {signatures : List Signature} {Γ : List Ty} {result : Ty} (program : Program signatures) (fn : Fin signatures.length) (args : Args Γ signatures[fn].params) (continuation : Stmt signatures (signatures[fn].result :: Γ) result) (entry : Env Γ) (next : Env Γ → ExceptT Fault (StateT Heap Part) (Value result)) :
    (call fn args continuation).evalWith program entry next = do let value ← program.eval fn (args.eval entry) continuation.evalWith program (Env.cons value entry) fun (finish : Env (signatures[fn].result :: Γ)) => next finish.tail

    A source call uses the actual callee action in ExceptT. Only a successful callee return binds a value and runs the caller's scoped continuation. This equation is not a simp rule, so recursive callee bodies remain opaque.

    theorem Complexity.Language.Program.eval_eq_evalWith {signatures : List Signature} (program : Program signatures) (fn : Fin signatures.length) (args : Env signatures[fn].params) :
    program.eval fn args = (program.body fn).evalWith program args fun (x : Env signatures[fn].params) => throw Fault.missingReturn

    The existing function evaluation is its body's continuation observation with a missing-return fault on normal fallthrough, including Unit functions.