Evaluation Bridge: DeepPackage → PkgImpl #
This file bridges DeepPackage (syntactic, oracle-based) to PkgImpl (semantic,
SPComp morphisms in Fam(KlSPComp)) via the existing RawCode.eval infrastructure.
Key insight #
DeepPackage.link performs syntactic oracle substitution (replacing oracleCall
nodes with implementations from another package), while PkgImpl composition
uses Kleisli bind. The theorem eval_substOracle connects these:
(code.substOracle env).eval = code.evalWith (eval ∘ env)
Main definitions #
oracleHandler— semantic oracle handler from a package's exportsevalImpl— evaluate a closed DeepPackage to a PkgImplevalImplWith— evaluate with a custom oracle handler
Main theorems #
evalImpl_link— linking corresponds to evalWith with oracleHandlerevalImpl_par_eval_left/right— parallel composition dispatches correctlyrunPkg_eq_eval— connects runPkg to the eval bridge
Oracle Handler #
Semantic oracle handler from a package's exports.
For each operation (op, dom, codom):
- If exported, evaluate the implementation via
RawCode.eval - Otherwise, return
SPComp.fail
Equations
Instances For
The oracle handler equals evaluating the linkEnv at every operation.
Evaluate DeepPackage to PkgImpl #
Evaluate a closed DeepPackage to a PkgImpl.
Each exported operation (indexed by Fin) is evaluated via RawCode.eval.
Unresolved oracle calls become SPComp.fail.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Evaluate a DeepPackage with a custom oracle handler.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Helper Lemmas #
Link Correctness #
Link correctness: evaluating a linked package equals evaluating the first package with the second's oracle handler.
This connects DeepPackage.link (syntactic substOracle) to semantic
oracle resolution (evalWith).
Par Correctness #
Parallel composition dispatches left operations to the left package at eval level.
Parallel composition dispatches right operations to the right package at eval level.
runPkg Connection #
Functoriality: Identity Laws #
evalImplWith with the fail handler equals evalImpl.
Since evalWith (fun _ _ _ _ => fail) = eval, this is pointwise true.
The oracle handler of the identity package is always SPComp.fail.
The id package forwards all ops as oracleCall, which evaluates to fail.
Right identity: linking with the identity package preserves evalImpl.
evalImpl (link p (id p.imports)) = evalImpl p
The id package's oracle handler maps everything to fail, which is the
same behavior as unresolved oracle calls in eval.
Left identity: linking the identity package on the left preserves evalImpl.
evalImpl (link (id p.exports) p) = evalImpl p
The id package's code is oracleCall, which after substOracle with p's
linkEnv becomes p's implementation (when the operation is exported).
Functoriality: Associativity #
Link associativity at the evalImpl level.
evalImpl (link (link p₁ p₂) p₃) = evalImpl (link p₁ (link p₂ p₃))
Syntactically, link (link p₁ p₂) p₃ ≠ link p₁ (link p₂ p₃) because
double substOracle differs from single substOracle with composed env.
But semantically (after eval), both agree because:
- Left:
(code.substOracle(env₂).substOracle(env₃)).eval - Right:
code.substOracle(env_{link p₂ p₃}).eval - Both equal
code.evalWith(handler)for the same handler.
This generalizes runPkg_link_assoc from the main operation to all operations.
Typed Semantic Packages #
A SemPkg is a semantic package at the SPComp level. It maps oracle handlers
to oracle handlers: given implementations for import operations, it produces
implementations for export operations.
SemPkg forms a proper category (with function composition as linking),
and evalImpl factors through it cleanly, avoiding the need for quotienting
DeepPackage by eval-equivalence.
Untyped oracle handler: provides SPComp implementations for any operation
identified by (op : ℕ, dom : Type, codom : Type).
Equations
- CatCrypt.Bridge.Handler = (ℕ → (dom codom : Type) → dom → CatCrypt.Core.SPComp codom)
Instances For
The trivial handler that maps all operations to failure.
Equations
- CatCrypt.Bridge.Handler.fail x✝³ x✝² x✝¹ x✝ = CatCrypt.Core.SPComp.fail
Instances For
A semantic typed package: a function from import oracle handlers to export
oracle handlers. The interface parameters I_imp and I_exp track which
operations are imported and exported.
This forms a category with function composition as linking:
- Identity: the handler that passes operations through unchanged
- Composition:
(g.comp f).resolve oracle = g.resolve (f.resolve oracle)
Given an oracle handler (resolving imports), produce an oracle handler (providing exports).
Instances For
Identity semantic package: passes the handler through unchanged.
Equations
- CatCrypt.Bridge.SemPkg.id I = { resolve := id }
Instances For
Composition of semantic packages: resolve imports of g through f.
Equations
Instances For
Composition is associative.
Left identity for composition.
Right identity for composition.
DeepPackage → SemPkg #
Convert a DeepPackage to a semantic package. For each operation in the exports, evaluates the implementation with the given oracle handler. Non-exported operations return fail.
Equations
- One or more equations did not get rendered due to their size.
Instances For
toSemPkg with the fail handler gives the same as oracleHandler.
Both evaluate the code, but oracleHandler uses eval (= evalWith fail).
toSemPkg preserves linking (pointwise): evaluating a linked package
is the same as composing the semantic handlers.
For any oracle handler oracle and operation (op, dom, codom):
toSemPkg(link p₁ p₂).resolve oracle = toSemPkg(p₁).resolve(toSemPkg(p₂).resolve oracle)
This is the fundamental functoriality theorem. It follows from
evalWith_substOracle: oracle substitution composes with evalWith.
toSemPkg preserves identity for exported operations.
For operations in I.ops, toSemPkg (id I) acts as the identity handler.
(For operations NOT in I.ops, toSemPkg (id I) returns fail, since
DeepPackage.id I only forwards operations declared in I.)
Category Instance for DeepInterface #
SemPkg satisfies the axioms of a Mathlib Category on DeepInterface objects:
Hom I J := SemPkg I J(semantic packages as morphisms)- Composition is diagrammatic:
f ≫ g = g.comp f(apply f first, then g) - Identity and associativity follow from
SemPkg.id_comp,SemPkg.comp_id,SemPkg.comp_assoc
This gives access to ≫ notation, Category.assoc, and categorical tactics.
Equations
- One or more equations did not get rendered due to their size.