Documentation

GaudisCrypt.Language.Semantics

General stuff #

@[reducible]
def GaudisCrypt.recursion {a : Type u_2} {b : a → Type u_1} [(x : a) → OmegaCompletePartialOrder (b x)] [(x : a) → OrderBot (b x)] (F : ((x : a) → b x) →𝒄 (x : a) → b x) (x : a) :
b x
Equations
Instances For

    Stateful programs #

    def GaudisCrypt.ProgramDenotation (state : Type u) (a : Type v) :
    Type (max u v)

    A probabilistic program with state state and result a: from an initial state, a sub-distribution over results and final states.

    This is StateT state SubProbability a unfolded, and deliberately not defined as that: StateT puts the state and the result in one universe, but a program's state lives in a higher universe than its results (variables are indexed by VariableName, which contains a Type, so VariableAssignment and ProgramState are in Type 1, while results are Unit, Bool, …). The Monad instance below and the instances after the monad laws are StateT's, written out. The state and the results have independent universes.

    Equations
    Instances For

      Run a sub-probability as a program that leaves the state alone (StateT.lift). The bind is SubProbability.hbind since a and a × s may live in different universes.

      Equations
      Instances For
        noncomputable def GaudisCrypt.SubProbability.uniformOfFinset {α : Type u} (fs : Finset α) (hs : fs.Nonempty) :

        Uniform subprobability over a nonempty finset.

        Equations
        Instances For
          noncomputable def GaudisCrypt.ProgramDenotation.uniformOfFinset {s : Type u} {α : Type v} (fs : Finset α) (hs : fs.Nonempty) :

          Uniform sampling over a nonempty finset (used e.g. for "sample without replacement" — uniform over the complement of the values seen so far).

          Equations
          Instances For
            def GaudisCrypt.ProgramDenotation.finalProb {s : Type u_1} {a : Type u_2} (prog : ProgramDenotation s a) (st : s) (X : Set a) :
            Equations
            Instances For
              def GaudisCrypt.ProgramDenotation.finalProb1 {s : Type u_1} {a : Type u_2} (prog : ProgramDenotation s a) (st : s) (x : a) :
              Equations
              Instances For
                @[implicit_reducible]
                Equations
                • One or more equations did not get rendered due to their size.
                @[implicit_reducible]
                Equations
                @[implicit_reducible]
                Equations
                • One or more equations did not get rendered due to their size.
                @[implicit_reducible]
                Equations
                • One or more equations did not get rendered due to their size.
                theorem GaudisCrypt.ProgramDenotation.pure_apply {s : Type u} {a : Type v} (x : a) (st : s) :
                pure x st = pure (x, st)

                pure, applied to a state. (What StateT.pure unfolded to.)

                theorem GaudisCrypt.ProgramDenotation.bind_apply {s : Type u} {a b : Type v} (p : ProgramDenotation s a) (f : a → ProgramDenotation s b) (st : s) :
                (p >>= f) st = do let r ← p st f r.1 r.2

                bind, applied to a state. (What StateT.bind unfolded to.)

                @[implicit_reducible]

                Running a sub-probability as a program that leaves the state alone (StateT.lift).

                Equations
                theorem GaudisCrypt.ProgramDenotation.toProgramDenotation_apply {s : Type u} {a : Type v} (μ : SubProbability a) (st : s) :
                μ.toProgramDenotation st = μ.hbind fun (x : a) => pure (x, st)

                toProgramDenotation, applied to a state. (In one universe, SubProbability.hbind_eq_bind turns the hbind into >>=.)

                theorem GaudisCrypt.ProgramDenotation.bind_mono {i : Type u_1} {s : Type u_2} {a b : Type u_3} [Preorder i] (f : i → ProgramDenotation s a) (g : i → a → ProgramDenotation s b) (hf : Monotone f) (hg : Monotone g) :
                Monotone fun (x : i) => f x >>= g x
                Equations
                • One or more equations did not get rendered due to their size.
                Instances For
                  theorem GaudisCrypt.while_unroll {s : Type u_1} (cond : ProgramDenotation s Bool) (body : ProgramDenotation s Unit) :
                  while_loop cond body = do let __do_lift ← cond if __do_lift = true then do body while_loop cond body else pure ()
                  noncomputable def GaudisCrypt.ProgramDenotation.set_state {s : Type u_1} (st' : s) :

                  Overwrite the whole state.

                  Equations
                  Instances For
                    class GaudisCrypt.AsGetter (T : Type w) (a : outParam (Type u)) (s : outParam (Type v)) :
                    Type (max (max u v) w)

                    ProgramDenotation.get/ProgramDenotation.set accept anything that forgets to a Getter/Setter — a Getter/Setter itself, or a full Lens/Variable. The value/state types are outParams recovered from the argument, which sidesteps the Lean 4.30 coercion that no longer fires when the value type is a metavariable.

                    Instances
                      class GaudisCrypt.AsSetter (T : Type w) (a : outParam (Type u)) (s : outParam (Type v)) :
                      Type (max (max u v) w)
                      Instances
                        @[implicit_reducible]
                        instance GaudisCrypt.instAsGetterGetter {a : Type u} {s : Type v} :
                        AsGetter (Getter a s) a s
                        Equations
                        @[implicit_reducible]
                        instance GaudisCrypt.instAsGetterLens {a : Type u} {s : Type v} :
                        AsGetter (Lens a s) a s
                        Equations
                        @[implicit_reducible]
                        instance GaudisCrypt.instAsSetterSetter {a : Type u} {s : Type v} :
                        AsSetter (Setter a s) a s
                        Equations
                        @[implicit_reducible]
                        instance GaudisCrypt.instAsSetterLens {a : Type u} {s : Type v} :
                        AsSetter (Lens a s) a s
                        Equations
                        noncomputable def GaudisCrypt.ProgramDenotation.set {T : Type w} {a : Type u} {s : Type v} [AsSetter T a s] (v : T) (x : a) :
                        Equations
                        Instances For
                          noncomputable def GaudisCrypt.ProgramDenotation.get {T : Type w} {a : Type u} {s : Type v} [AsGetter T a s] (v : T) :
                          Equations
                          Instances For
                            noncomputable def GaudisCrypt.ProgramDenotation.zoom {s : Type u_1} {t : Type u_2} {a : Type u_3} (lens : Lens s t) (p : ProgramDenotation s a) :

                            Run p on the part of the state that lens selects. (s and t may live in different universes, hence SubProbability.hbind.)

                            Equations
                            Instances For

                              Monad laws for ProgramDenotation s #

                              theorem GaudisCrypt.ProgramDenotation.bind_assoc {s : Type u} {a b c : Type v} (p : ProgramDenotation s a) (f : a → ProgramDenotation s b) (g : b → ProgramDenotation s c) :
                              p >>= f >>= g = p >>= fun (x : a) => f x >>= g
                              theorem GaudisCrypt.ProgramDenotation.pure_bind {s : Type u} {a b : Type v} (x : a) (f : a → ProgramDenotation s b) :
                              pure x >>= f = f x
                              theorem GaudisCrypt.ProgramDenotation.bind_bot {s : Type u} {a b : Type v} (m : ProgramDenotation s a) :
                              (do let _ ← m ⊥) = ⊥

                              The instances StateT used to provide #

                              The monad laws. StateT would only have given this from a LawfulMonad SubProbability, which does not exist; here it comes from the three laws proved above.

                              @[implicit_reducible]

                              Reading and writing the whole state (get/set/modify in do notation). Lean's MonadStateOf puts the state and the results in one universe, so this instance only exists for results in the state's universe.

                              Equations
                              • One or more equations did not get rendered due to their size.

                              zoom is a monad morphism #

                              theorem GaudisCrypt.ProgramDenotation.zoom_pure {s : Type u} {t : Type v} {a : Type w} (lens : Lens s t) (x : a) :
                              zoom lens (pure x) = pure x
                              theorem GaudisCrypt.ProgramDenotation.zoom_bind {s : Type u} {t : Type v} {a b : Type w} (lens : Lens s t) (p : ProgramDenotation s a) (k : a → ProgramDenotation s b) :
                              zoom lens (p >>= k) = do let a ← zoom lens p zoom lens (k a)