Documentation

CatCryptCore.Bridge.PkgEval

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 #

Main theorems #

Oracle Handler #

noncomputable def CatCrypt.Bridge.oracleHandler (p : Deep.DeepPackage) (op : ) (dom codom : Type) :
domCore.SPComp codom

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
    theorem CatCrypt.Bridge.oracleHandler_eq_linkEnv_eval (p : Deep.DeepPackage) (op : ) (dom codom : Type) (x : dom) :
    oracleHandler p op dom codom x = (p.linkEnv op dom codom x).eval

    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
      noncomputable def CatCrypt.Bridge.evalImplWith (p : Deep.DeepPackage) (oracle : (dom codom : Type) → domCore.SPComp codom) :

      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 #

        Par Correctness #

        theorem CatCrypt.Bridge.evalImpl_par_eval_left (p₁ p₂ : Deep.DeepPackage) (h_sep : p₁.sep p₂) (op : ) (dom codom : Type) (h₁ : (op, dom, codom) p₁.exports.ops) (h_par : (op, dom, codom) (p₁.par p₂ h_sep).exports.ops) (x : dom) :
        ((p₁.par p₂ h_sep).impl op dom codom h_par x).code.eval = (p₁.impl op dom codom h₁ x).code.eval

        Parallel composition dispatches left operations to the left package at eval level.

        theorem CatCrypt.Bridge.evalImpl_par_eval_right (p₁ p₂ : Deep.DeepPackage) (h_sep : p₁.sep p₂) (op : ) (dom codom : Type) (h₁ : (op, dom, codom)p₁.exports.ops) (h₂ : (op, dom, codom) p₂.exports.ops) (h_par : (op, dom, codom) (p₁.par p₂ h_sep).exports.ops) (x : dom) :
        ((p₁.par p₂ h_sep).impl op dom codom h_par x).code.eval = (p₂.impl op dom codom h₂ x).code.eval

        Parallel composition dispatches right operations to the right package at eval level.

        runPkg Connection #

        runPkg computes the evaluation of the main (0, Unit, Bool) operation.

        Functoriality: Identity Laws #

        theorem CatCrypt.Bridge.evalImplWith_fail (p : Deep.DeepPackage) :
        (evalImplWith p fun (x : ) (x_1 x_2 : Type) (x_3 : x_1) => Core.SPComp.fail) = evalImpl p

        evalImplWith with the fail handler equals evalImpl. Since evalWith (fun _ _ _ _ => fail) = eval, this is pointwise true.

        theorem CatCrypt.Bridge.oracleHandler_id (I : Deep.DeepInterface) (op : ) (dom codom : Type) (x : dom) :

        The oracle handler of the identity package is always SPComp.fail. The id package forwards all ops as oracleCall, which evaluates to fail.

        Functoriality: Associativity #

        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.

        @[reducible, inline]

        Untyped oracle handler: provides SPComp implementations for any operation identified by (op : ℕ, dom : Type, codom : Type).

        Equations
        Instances For

          The trivial handler that maps all operations to failure.

          Equations
          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)
            • resolve : HandlerHandler

              Given an oracle handler (resolving imports), produce an oracle handler (providing exports).

            Instances For
              theorem CatCrypt.Bridge.SemPkg.ext {I_imp : Deep.DeepInterface} {I_exp : Deep.DeepInterface} {s₁ s₂ : SemPkg I_imp I_exp} (h : s₁.resolve = s₂.resolve) :
              s₁ = s₂
              theorem CatCrypt.Bridge.SemPkg.ext_iff {I_imp : Deep.DeepInterface} {I_exp : Deep.DeepInterface} {s₁ s₂ : SemPkg I_imp I_exp} :
              s₁ = s₂ s₁.resolve = s₂.resolve

              Identity semantic package: passes the handler through unchanged.

              Equations
              Instances For
                noncomputable def CatCrypt.Bridge.SemPkg.comp {I_imp : Deep.DeepInterface} {I_exp : Deep.DeepInterface} {I_mid : Deep.DeepInterface} (g : SemPkg I_mid I_exp) (f : SemPkg I_imp I_mid) :
                SemPkg I_imp I_exp

                Composition of semantic packages: resolve imports of g through f.

                Equations
                Instances For
                  @[simp]
                  theorem CatCrypt.Bridge.SemPkg.comp_resolve {I_imp : Deep.DeepInterface} {I_exp : Deep.DeepInterface} {I_mid : Deep.DeepInterface} (g : SemPkg I_mid I_exp) (f : SemPkg I_imp I_mid) (h : Handler) :
                  (g.comp f).resolve h = g.resolve (f.resolve h)
                  theorem CatCrypt.Bridge.SemPkg.comp_assoc {I_exp : Deep.DeepInterface} {I₁ : Deep.DeepInterface} {I₂ : Deep.DeepInterface} {I₃ : Deep.DeepInterface} (h : SemPkg I₃ I_exp) (g : SemPkg I₂ I₃) (f : SemPkg I₁ I₂) :
                  h.comp (g.comp f) = (h.comp g).comp f

                  Composition is associative.

                  theorem CatCrypt.Bridge.SemPkg.id_comp {I_imp : Deep.DeepInterface} {I_exp : Deep.DeepInterface} (f : SemPkg I_imp I_exp) :
                  (id I_exp).comp f = f

                  Left identity for composition.

                  theorem CatCrypt.Bridge.SemPkg.comp_id {I_imp : Deep.DeepInterface} {I_exp : Deep.DeepInterface} (f : SemPkg I_imp I_exp) :
                  f.comp (id I_imp) = f

                  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).

                    theorem CatCrypt.Bridge.toSemPkg_id_mem (I : Deep.DeepInterface) (oracle : Handler) (op : ) (dom codom : Type) (x : dom) (h : (op, dom, codom) I.ops) :
                    (toSemPkg (Deep.DeepPackage.id I)).resolve oracle op dom codom x = oracle op dom codom x

                    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:

                    This gives access to notation, Category.assoc, and categorical tactics.

                    @[implicit_reducible]
                    Equations
                    • One or more equations did not get rendered due to their size.