Documentation

PolyFun.Interaction.TwoParty.Lens.Strategy

Strategies and scheduled execution #

Strategies use uniform slots at nodes and leaves. Execution uses InteractionOver.run to collect the two terminal actions, then sequences those actions in the same boundary order.

def Interaction.TwoParty.Lens.ActionsCommute {m : Type u → Type u} [Monad m] {A B : Type u} (p : m A) (c : m B) :

Two actions commute if either order gives the same result under every continuation.

Equations
Instances For
    theorem Interaction.TwoParty.Lens.ActionsCommute.pure_right {m : Type u → Type u} [Monad m] [LawfulMonad m] {A B : Type u} (p : m A) (b : B) :

    A pure right action commutes with every action in a lawful monad.

    theorem Interaction.TwoParty.Lens.ActionsCommute.pure_left {m : Type u → Type u} [Monad m] [LawfulMonad m] {A B : Type u} (a : A) (c : m B) :

    A pure left action commutes with every action in a lawful monad.

    @[implicit_reducible]

    The unique trivial context decoration. Ownership is carried only by the lens.

    Equations
    Instances For
      @[reducible, inline]

      Node slots with a pure terminal payload, used to specify a pure prefix end.

      Equations
      Instances For
        @[reducible, inline]

        A strategy with an effect slot at every boundary, including the leaf.

        Equations
        Instances For
          def Interaction.TwoParty.Lens.PureEnd.mapOutput {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Functor m] {agent : Participant} {s : P.FreeM PUnit.{u + 1}} {A B : PFunctor.FreeM.PathAlong l s → Type u} :
          ((tr : PFunctor.FreeM.PathAlong l s) → A tr → B tr) → PureEnd l m agent s A → PureEnd l m agent s B

          Map pure terminal payloads, preserving every node slot.

          Equations
          Instances For
            def Interaction.TwoParty.Lens.PureEnd.toStrategy {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Applicative m] {agent : Participant} {s : P.FreeM PUnit.{u + 1}} {Out : PFunctor.FreeM.PathAlong l s → Type u} (p : PureEnd l m agent s Out) :
            Strategy l m agent s Out

            Include a strategy whose terminal action is pure in the uniform-slot API.

            Equations
            Instances For
              def Interaction.TwoParty.Lens.Strategy.mapOutput {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Functor m] {agent : Participant} {s : P.FreeM PUnit.{u + 1}} {A B : PFunctor.FreeM.PathAlong l s → Type u} (f : (tr : PFunctor.FreeM.PathAlong l s) → A tr → B tr) (p : Strategy l m agent s A) :
              Strategy l m agent s B

              Map the outputs of a strategy after its leaf action.

              Equations
              Instances For
                def Interaction.TwoParty.Lens.Strategy.joinSlot {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Monad m] {agent : Participant} {s : P.FreeM PUnit.{u + 1}} {Out : PFunctor.FreeM.PathAlong l s → Type u} :
                m (Strategy l m agent s Out) → Strategy l m agent s Out

                Merge an action constructing a strategy into that strategy's first slot.

                Equations
                Instances For
                  def Interaction.TwoParty.Lens.Strategy.splitSlot {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Monad m] {agent : Participant} {s : P.FreeM PUnit.{u + 1}} {Out : PFunctor.FreeM.PathAlong l s → Type u} :
                  Strategy l m agent s Out → m (Strategy l m agent s Out)

                  Execute the first slot and retain its payload in a pure slot of the returned strategy.

                  Equations
                  Instances For
                    @[simp]

                    Joining a split slot preserves the original action.

                    def Interaction.TwoParty.Lens.participantProfile {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} {s : P.FreeM PUnit.{u + 1}} {OutP OutC : PFunctor.FreeM.PathAlong l s → Type u} (p : Focal l m s OutP) (c : Counterpart l m s OutC) (agent : Participant) :
                    PureEnd l m agent s (participantOutputFamily (fun (tr : PFunctor.FreeM.PathAlong l s) => m (OutP tr)) (fun (tr : PFunctor.FreeM.PathAlong l s) => m (OutC tr)) agent)

                    The profile supplied to the generic runner.

                    Equations
                    Instances For
                      def Interaction.TwoParty.Lens.runWith {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Monad m] (order : Schedule) (s : P.FreeM PUnit.{u + 1}) {OutP OutC : PFunctor.FreeM.PathAlong l s → Type u} (p : Focal l m s OutP) (c : Counterpart l m s OutC) :
                      m ((tr : PFunctor.FreeM.PathAlong l s) × OutP tr × OutC tr)

                      Run the generic interaction, then execute its collected leaf actions in boundary order.

                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For
                        def Interaction.TwoParty.Lens.run {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Monad m] (s : P.FreeM PUnit.{u + 1}) {OutP OutC : PFunctor.FreeM.PathAlong l s → Type u} (p : Focal l m s OutP) (c : Counterpart l m s OutC) :
                        m ((tr : PFunctor.FreeM.PathAlong l s) × OutP tr × OutC tr)

                        Execute focal slot, counterpart slot, and move exchange at every boundary.

                        Equations
                        Instances For
                          @[simp]
                          theorem Interaction.TwoParty.Lens.runWith_done {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Monad m] [LawfulMonad m] (order : Schedule) {OutP OutC : PFunctor.FreeM.PathAlong l (pure PUnit.unit) → Type u} (p : m (OutP PUnit.unit)) (c : m (OutC PUnit.unit)) :
                          runWith order (pure PUnit.unit) p c = order.seq p c fun (a : OutP PUnit.unit) (b : OutC PUnit.unit) => pure ⟨PUnit.unit, (a, b)⟩

                          Terminal actions execute in the selected schedule.

                          theorem Interaction.TwoParty.Lens.runWith_node {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Monad m] [LawfulMonad m] (order : Schedule) (a : P.A) (rest : P.B a → P.FreeM PUnit.{u + 1}) {OutP OutC : PFunctor.FreeM.PathAlong l (PFunctor.FreeM.liftBind a rest) → Type u} (p : Focal l m (PFunctor.FreeM.liftBind a rest) OutP) (c : Counterpart l m (PFunctor.FreeM.liftBind a rest) OutC) :
                          runWith order (PFunctor.FreeM.liftBind a rest) p c = (interaction l m order).interact (fun (x : Participant) => match x with | Participant.focal => p | Participant.counterpart => c) fun (x : rolePoly.B (l.toFunA a)) (cont : (agent : Participant) → PureEnd l m agent (rest (l.toFunB a x)) fun (tr : PFunctor.FreeM.PathAlong l (rest (l.toFunB a x))) => participantOutputFamily (fun (t : PFunctor.FreeM.PathAlong l (PFunctor.FreeM.liftBind a rest)) => m (OutP t)) (fun (t : PFunctor.FreeM.PathAlong l (PFunctor.FreeM.liftBind a rest)) => m (OutC t)) agent ⟨x, tr⟩) => do let __x ← runWith order (rest (l.toFunB a x)) (cont Participant.focal) (cont Participant.counterpart) match __x with | ⟨tr, (outP, outC)⟩ => pure ⟨⟨x, tr⟩, (outP, outC)⟩

                          The node equation retains the generic interaction law and the runtime move.

                          @[simp]
                          theorem Interaction.TwoParty.Lens.run_done {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Monad m] [LawfulMonad m] {OutP OutC : PFunctor.FreeM.PathAlong l (pure PUnit.unit) → Type u} (p : m (OutP PUnit.unit)) (c : m (OutC PUnit.unit)) :
                          run (pure PUnit.unit) p c = do let a ← p let b ← c pure ⟨PUnit.unit, (a, b)⟩

                          Focal-first execution at a leaf runs both terminal actions.

                          theorem Interaction.TwoParty.Lens.run_node {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Monad m] [LawfulMonad m] (a : P.A) (rest : P.B a → P.FreeM PUnit.{u + 1}) {OutP OutC : PFunctor.FreeM.PathAlong l (PFunctor.FreeM.liftBind a rest) → Type u} (p : Focal l m (PFunctor.FreeM.liftBind a rest) OutP) (c : Counterpart l m (PFunctor.FreeM.liftBind a rest) OutC) :
                          run (PFunctor.FreeM.liftBind a rest) p c = step (owner l a) Schedule.focalFirst p c fun (x : Move l a) (pn : StrategyOver (paired l m) Participant.focal (rest (l.toFunB a x)) ((fun (b : P.B a) => contexts (rest b)) (l.toFunB a x)) fun (path : PFunctor.FreeM.PathAlong l (rest (l.toFunB a x))) => (fun (tr : PFunctor.FreeM.PathAlong l (PFunctor.FreeM.liftBind a rest)) => m (OutP tr)) ⟨x, path⟩) (cn : StrategyOver (paired l m) Participant.counterpart (rest (l.toFunB a x)) ((fun (b : P.B a) => contexts (rest b)) (l.toFunB a x)) fun (path : PFunctor.FreeM.PathAlong l (rest (l.toFunB a x))) => (fun (tr : PFunctor.FreeM.PathAlong l (PFunctor.FreeM.liftBind a rest)) => m (OutC tr)) ⟨x, path⟩) => do let __x ← run (rest (l.toFunB a x)) pn cn match __x with | ⟨tr, (op, oc)⟩ => pure ⟨⟨x, tr⟩, (op, oc)⟩

                          Execute a node's two slots and continue along the runtime move.

                          theorem Interaction.TwoParty.Lens.run_joinSlot {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Monad m] [LawfulMonad m] (s : P.FreeM PUnit.{u + 1}) {OutP OutC : PFunctor.FreeM.PathAlong l s → Type u} (p : m (Focal l m s OutP)) (c : Counterpart l m s OutC) :
                          run s (Strategy.joinSlot p) c = do let next ← p run s next c

                          Joining a focal construction slot preserves its execution before the counterpart slot.

                          theorem Interaction.TwoParty.Lens.run_splitSlot_joinSlot {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Monad m] [LawfulMonad m] (s : P.FreeM PUnit.{u + 1}) {OutP OutC : PFunctor.FreeM.PathAlong l s → Type u} (p : Focal l m s OutP) (c : m (Counterpart l m s OutC)) :
                          run s p (Strategy.joinSlot c) = do let next ← Strategy.splitSlot p let other ← c run s next other

                          Splitting the focal slot exposes exactly the effects before a joined counterpart slot.

                          theorem Interaction.TwoParty.Lens.run_splitSlot {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Monad m] [LawfulMonad m] (s : P.FreeM PUnit.{u + 1}) {OutP OutC : PFunctor.FreeM.PathAlong l s → Type u} (p : Focal l m s OutP) (c : Counterpart l m s OutC) :
                          (do let next ← Strategy.splitSlot p run s next c) = run s p c

                          A split focal slot may be executed before the rest of the run.

                          @[simp]
                          theorem Interaction.TwoParty.Lens.PureEnd.mapOutput_id {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Functor m] [LawfulFunctor m] {agent : Participant} {s : P.FreeM PUnit.{u + 1}} {Out : PFunctor.FreeM.PathAlong l s → Type u} (p : PureEnd l m agent s Out) :
                          mapOutput (fun (x : PFunctor.FreeM.PathAlong l s) (x_1 : Out x) => x_1) p = p

                          A pure terminal-output map leaves every boundary effect intact.

                          @[simp]
                          theorem Interaction.TwoParty.Lens.Strategy.mapOutput_id {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Functor m] [LawfulFunctor m] {agent : Participant} {s : P.FreeM PUnit.{u + 1}} {Out : PFunctor.FreeM.PathAlong l s → Type u} (p : Strategy l m agent s Out) :
                          mapOutput (fun (x : PFunctor.FreeM.PathAlong l s) (x_1 : Out x) => x_1) p = p

                          Mapping identity over the terminal action preserves a strategy.

                          theorem Interaction.TwoParty.Lens.runWith_mapOutput_mapOutput {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Monad m] [LawfulMonad m] (order : Schedule) (s : P.FreeM PUnit.{u + 1}) {A B C D : PFunctor.FreeM.PathAlong l s → Type u} (f : (tr : PFunctor.FreeM.PathAlong l s) → A tr → C tr) (g : (tr : PFunctor.FreeM.PathAlong l s) → B tr → D tr) (p : Focal l m s A) (c : Counterpart l m s B) :
                          runWith order s (Strategy.mapOutput f p) (Strategy.mapOutput g c) = do let __x ← runWith order s p c match __x with | ⟨tr, (a, b)⟩ => pure ⟨tr, (f tr a, g tr b)⟩

                          Mapping both terminal outputs preserves the transcript and every scheduled effect.

                          theorem Interaction.TwoParty.Lens.run_mapOutput_mapOutput {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Monad m] [LawfulMonad m] (s : P.FreeM PUnit.{u + 1}) {A B C D : PFunctor.FreeM.PathAlong l s → Type u} (f : (tr : PFunctor.FreeM.PathAlong l s) → A tr → C tr) (g : (tr : PFunctor.FreeM.PathAlong l s) → B tr → D tr) (p : Focal l m s A) (c : Counterpart l m s B) :
                          run s (Strategy.mapOutput f p) (Strategy.mapOutput g c) = do let __x ← run s p c match __x with | ⟨tr, (a, b)⟩ => pure ⟨tr, (f tr a, g tr b)⟩

                          Focal-first execution commutes with separate terminal output maps.

                          def Interaction.TwoParty.Lens.Strategy.liftId {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Applicative m] {agent : Participant} {s : P.FreeM PUnit.{u + 1}} {Out : PFunctor.FreeM.PathAlong l s → Type u} :
                          Strategy l Id agent s Out → Strategy l m agent s Out

                          Lift a deterministic strategy by inserting pure slots at every boundary.

                          Equations
                          • One or more equations did not get rendered due to their size.
                          • p.liftId = pure p
                          Instances For
                            @[simp]

                            Deterministic leaf lifting inserts a pure terminal action.

                            theorem Interaction.TwoParty.Lens.Strategy.liftId_node {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Applicative m] {agent : Participant} (a : P.A) (rest : P.B a → P.FreeM PUnit.{u + 1}) {Out : PFunctor.FreeM.PathAlong l (PFunctor.FreeM.liftBind a rest) → Type u} (p : Strategy l Id agent (PFunctor.FreeM.liftBind a rest) Out) :
                            p.liftId = pure (Choice.map agent (owner l a) (fun (x : Move l a) (next : Strategy l Id agent (rest (l.toFunB a x)) fun (tr : PFunctor.FreeM.PathAlong l (rest (l.toFunB a x))) => Out ⟨x, tr⟩) => next.liftId) p)

                            Deterministic node lifting preserves the move and lifts every continuation.

                            theorem Interaction.TwoParty.Lens.run_sender {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Monad m] [LawfulMonad m] (a : P.A) (rest : P.B a → P.FreeM PUnit.{u + 1}) (h : owner l a = Role.sender) {OutP OutC : PFunctor.FreeM.PathAlong l (PFunctor.FreeM.liftBind a rest) → Type u} (p : Focal l m (PFunctor.FreeM.liftBind a rest) OutP) (c : Counterpart l m (PFunctor.FreeM.liftBind a rest) OutC) :
                            run (PFunctor.FreeM.liftBind a rest) p c = do let v ← have this := cast ⋯ p; this let f ← have this := cast ⋯ c; this let __x ← run (rest (l.toFunB a v.fst)) v.snd (f v.fst) match __x with | ⟨tr, (op, oc)⟩ => pure ⟨⟨v.fst, tr⟩, (op, oc)⟩

                            Sender execution runs the focal slot before the counterpart observation slot.

                            theorem Interaction.TwoParty.Lens.run_receiver {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Monad m] [LawfulMonad m] (a : P.A) (rest : P.B a → P.FreeM PUnit.{u + 1}) (h : owner l a = Role.receiver) {OutP OutC : PFunctor.FreeM.PathAlong l (PFunctor.FreeM.liftBind a rest) → Type u} (p : Focal l m (PFunctor.FreeM.liftBind a rest) OutP) (c : Counterpart l m (PFunctor.FreeM.liftBind a rest) OutC) :
                            run (PFunctor.FreeM.liftBind a rest) p c = do let f ← have this := cast ⋯ p; this let v ← have this := cast ⋯ c; this let __x ← run (rest (l.toFunB a v.fst)) (f v.fst) v.snd match __x with | ⟨tr, (op, oc)⟩ => pure ⟨⟨v.fst, tr⟩, (op, oc)⟩

                            Receiver execution runs the focal observation slot before the counterpart choice slot.

                            def Interaction.TwoParty.Lens.Choice.Rel {X : Type u} {A B : X → Type u} (role : Role) (rel : (x : X) → A x → B x → Prop) :

                            A relation on the continuations selected by exchanging two pure slot payloads.

                            Equations
                            • One or more equations did not get rendered due to their size.
                            Instances For
                              theorem Interaction.TwoParty.Lens.Choice.Rel.mono {X : Type u} {A B : X → Type u} (role : Role) {rel rel' : (x : X) → A x → B x → Prop} {p : Choice Participant.focal role X A} {c : Choice Participant.counterpart role X B} (h : Rel role rel p c) (imp : ∀ (x : X) (a : A x) (b : B x), rel x a b → rel' x a b) :
                              Rel role rel' p c

                              Selected-continuation relations are preserved by implication.

                              def Interaction.TwoParty.Lens.SlotsCommute {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Monad m] (s : P.FreeM PUnit.{u + 1}) {OutP OutC : PFunctor.FreeM.PathAlong l s → Type u} :
                              Focal l m s OutP → Counterpart l m s OutC → Prop

                              Recursively commuting slots. Each node factors its slots into actions and pure payload maps. The actions commute under arbitrary continuations, and every pair of their results selects recursively commuting strategies. At a leaf the two terminal actions commute. Factorizations allow invariants on returned payloads without a notion of monadic support.

                              Equations
                              Instances For
                                theorem Interaction.TwoParty.Lens.step_exchange {m : Type u → Type u} [Monad m] [LawfulMonad m] {X I J R : Type u} {A B : X → Type u} (role : Role) (pa : m I) (ca : m J) (pn : I → Choice Participant.focal role X A) (cn : J → Choice Participant.counterpart role X B) (k₁ k₂ : (x : X) → A x → B x → m R) (h : ActionsCommute pa ca) (hk : ∀ (i : I) (j : J), Choice.Rel role (fun (x : X) (a : A x) (b : B x) => k₁ x a b = k₂ x a b) (pn i) (cn j)) :
                                step role Schedule.focalFirst (pn <$> pa) (cn <$> ca) k₁ = step role Schedule.counterpartFirst (pn <$> pa) (cn <$> ca) k₂

                                Exchange factored slots when their actions commute and selected continuations agree.

                                Recursively commuting boundary slots make the two schedules agree.

                                def Interaction.TwoParty.Lens.PureObservers {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Monad m] (s : P.FreeM PUnit.{u + 1}) {OutP OutC : PFunctor.FreeM.PathAlong l s → Type u} :
                                Focal l m s OutP → Counterpart l m s OutC → Prop

                                Pure observer slots at every node, and a pure counterpart terminal action at each leaf. The owner slot may be effectful. Its payload map carries all possible selected continuations, so the recursive condition includes every move returned by that slot.

                                Equations
                                Instances For
                                  theorem Interaction.TwoParty.Lens.PureObservers.slotsCommute {P : PFunctor.{uA, uB}} {l : P.Lens rolePoly} {m : Type u → Type u} [Monad m] [LawfulMonad m] (s : P.FreeM PUnit.{u + 1}) {OutP OutC : PFunctor.FreeM.PathAlong l s → Type u} {p : Focal l m s OutP} {c : Counterpart l m s OutC} (h : PureObservers s p c) :

                                  Pure observers and a pure counterpart end imply recursive slot commutation.

                                  Schedules agree when observers react purely and the counterpart ends purely.