A Trace (of) Sandwich A Trace (of) Sandwich Christoph Rauch , Sergey Goncharov, Lutz Schr¨ oder, Maciej Pir´ og TCS Seminar SS 2019 FAU Erlangen-N¨ urnberg July 2, 2019
A Trace (of) Sandwich A prototype While language Syntax s ∶∶= skip ∣ x ∶= e ∣ s 0 ; s 1 ∣ if e then s t else s f ∣ while e do s t e are integer expressions 0 treated as false
A Trace (of) Sandwich Trace-Based Big-Step Operational Semantics For the simple While language [Nakata, Uustalu ’09] ∗ ∗ ⇒ τ ′ σ ⊧ e ( s t ,σ ∶∶ ⟨ σ ⟩) ⇒ τ ( while e do s t ,τ ) ( while e do s t ,σ ) ⇒ τ ′ σ / ⊧ e ( while e do s t ,σ ) ⇒ σ ∶∶ ⟨ σ ⟩ ∗ ⇒ τ ′ ( s ,σ ) ⇒ τ ( s ,τ ) ∗ ∗ ⇒ σ ∶∶ τ ′ ( s , ⟨ σ ⟩) ⇒ τ ( s ,σ ∶∶ τ )
A Trace (of) Sandwich Generalizing the While Language Syntax s ∶∶= skip ∣ x ∶= e ∣ s 0 ; s 1 ∣ if e then s t else s f ∣ while e do s t e are integer expressions 0 treated as false
A Trace (of) Sandwich Generalizing the While Language Syntax s ∶∶= a ∣ s 0 ; s 1 ∣ if b then s t else s f ∣ while b do s t a are actions b boolean expressions
A Trace (of) Sandwich Monads for Computational Effects Definition A monad on a category C consists of an endofunctor T ∶ C → C a natural transformation η ∶ Id → T a natural transformation µ ∶ TT → T such that the following diagrams commute: T µ T η η TTT TT T TT T µ µ µ µ TT T T
A Trace (of) Sandwich Monads for Computational Effects Definition A monad on a category C consists of Strength an endofunctor T ∶ C → C A monad T is strong if there is a natural transformation a natural transformation η ∶ Id → T τ ∶ TX × Y → T ( X × Y ) subject to standard coherence conditions a natural transformation µ ∶ TT → T such that the following diagrams commute: T µ T η η TTT TT T TT T µ µ µ µ TT T T
A Trace (of) Sandwich Monads for Computational Effects Definition A monad on a category C consists of Strength an endofunctor T ∶ C → C A monad T is strong if there is a natural transformation a natural transformation η ∶ Id → T τ ∶ TX × Y → T ( X × Y ) subject to standard coherence conditions a natural transformation µ ∶ TT → T such that the following diagrams commute: Kleisli composition T µ T η η TTT TT T TT T Tg f µ We write g ◇ f for X → TZ . → TY �→ TTZ � � µ µ µ µ TT T T
A Trace (of) Sandwich Composing Monads Examples of Monads Power set (nondeterminism), P X = { A ∣ A ⊆ X } Reader monad (for an environment E ), TX = X E
A Trace (of) Sandwich Composing Monads Examples of Monads Power set (nondeterminism), P X = { A ∣ A ⊆ X } Reader monad (for an environment E ), TX = X E Composition composing the functor parts of monads does not necessarily yield a monad e.g., PP is not a monad [Klin, Salamanca] possible under certain conditions (distributive laws, . . . )
A Trace (of) Sandwich Adjunctions Definition F An adjunction consists of functors C and a natural iso D ⊣ U Φ ∶ D ( FX , Y ) ≅ C ( X , UY ) ∶ ψ.
A Trace (of) Sandwich Adjunctions Definition F An adjunction consists of functors C and a natural iso D ⊣ U Φ ∶ D ( FX , Y ) ≅ C ( X , UY ) ∶ ψ. Unit, counit, triangle identities η ∶ 1 → UF ǫ ∶ FU → 1 F η X ǫ FX → FX = id FX (⋆) FX → FUFX � � � � U ǫ Y η UY → UY = id UY (◻) UY → UFUY � � � �
A Trace (of) Sandwich Sandwiching Composition of Adjunctions If F ⊣ U and G ⊣ V , then also GF ⊣ UV .
A Trace (of) Sandwich Sandwiching Composition of Adjunctions If F ⊣ U and G ⊣ V , then also GF ⊣ UV . Lemma: Sandwiching Let F ⊣ U be an adjunction from C to D with unit η and counit ǫ . Suppose that ( T ,η T ,µ T ) is a monad on D . Then P = UTF ∶ C → C equipped with the natural transformations U η T η P = X η → UFX → UTFX � � � U µ T µ P = UTFUTFX UT ǫ → UTTFX → UTFX � � � � is a monad on C .
A Trace (of) Sandwich Sandwiching Composition of Adjunctions If F ⊣ U and G ⊣ V , then also GF ⊣ UV . Lemma: Sandwiching Proposition Let F ⊣ U be an adjunction from C to D with unit η and counit ǫ . Every monad arises from an adjunction. Given ( T ,η T ,µ T ) on C , there Suppose that ( T ,η T ,µ T ) is a monad on D . Then P = UTF ∶ C → C exists D and F ⊣ U ,η ∶ 1 → UF ,ǫ ∶ FU → 1 s.t. equipped with the natural transformations T = UF ,η T = η,µ T = U ǫ . U η T η P = X η → UFX → UTFX � � � U µ T µ P = UTFUTFX UT ǫ → UTTFX → UTFX � � � � is a monad on C .
A Trace (of) Sandwich Sandwiching: Monad Transformers Examples Writer: M a monoid, then F Set M − Set ⊣ U where F maps X to the free M -Set X × M with the action ( x , m ) n ↦ ( x , m ⋅ n ) ; U is the obvious forgetful functor
A Trace (of) Sandwich Sandwiching: Monad Transformers Examples Writer: M a monoid, then F Set M − Set ⊣ U where F maps X to the free M -Set X × M with the action ( x , m ) n ↦ ( x , m ⋅ n ) ; U is the obvious forgetful functor State: S an object, then - - - × S Set Set ⊣ - ) S ( - - is the state monad transformer.
A Trace (of) Sandwich Denotational Semantics for the While Language Interpretation without loops ⟦ a ⟧ = a ⟦ s 0 ; s 1 ⟧ = ⟦ s 0 ⟧ ◇ ⟦ s 1 ⟧ ⟦ if b then s t else s f ⟧ = [⟦ s t ⟧ , ⟦ s f ⟧] ◇ b ⟦ while b do s t ⟧ = ? actions a are morphisms 1 → T 1 booleans b are morphisms 1 → T ( 1 + 1 )
A Trace (of) Sandwich Iteration for Monads morphisms X → T ( Y + X ) seen as systems of recursive equations obtain different classes of monads by demanding properties of solutions to these equations guardedness as a means of guaranteeing existence of solutions
A Trace (of) Sandwich Iteration for Monads Abstract guardedness for a monad T guarded: Kleisli category of T is a guarded category
A Trace (of) Sandwich Iteration for Monads Abstract guardedness for a monad T guarded: Kleisli category of T is a guarded category f ∶ X → f ∶ X → σ g ∶ Y → σ Y Z Z in 1 ○ f ∶ X → in 2 ( Y + Z ) [ f , g ] ∶ X + Y → σ Z f ∶ X → in 2 ( Y + Z ) g ∶ Y → σ V h ∶ Z → V [ g , h ] ○ f ∶ X → σ V
A Trace (of) Sandwich Iteration for Monads Abstract guardedness for a monad T guarded: Kleisli category of T is a guarded category f ∶ X → TY f ∶ X → σ TZ g ∶ Y → σ TZ T in 1 ○ f ∶ X → in 2 T ( Y + Z ) [ f , g ] ∶ X + Y → σ TZ f ∶ X → in 2 T ( Y + Z ) g ∶ Y → σ TV h ∶ Z → TV µ ○ T [ g , h ] ○ f ∶ X → σ TV
A Trace (of) Sandwich Iteration for Monads Abstract guardedness for a monad T guarded: Kleisli category of T is a guarded category
A Trace (of) Sandwich Iteration for Monads Abstract guardedness for a monad T guarded: Kleisli category of T is a guarded category guarded pre-iterative: every morphism f ∶ X → 2 T ( Y + X ) has a solution w.r.t. f † = [ η, f † ] ◇ f guarded iterative: unique solutions guarded Elgot: solutions satisfy certain additional axioms [Bloom and ´ Esik ’93]
A Trace (of) Sandwich A Trace Sandwich Sandwich Theorems for Monads with Iteration Theorem Let F ∶ C → D and U ∶ D → C be a pair of adjoint functors with associated natural isomorphism Φ ∶ D ( FX , Y ) → C ( X , UY ) , and let T be a guarded iterative monad on D . Then the monad induced on the composite functor UTF is guarded iterative, with the guardedness relation defined by taking f ∶ X → σ UTFY if and only if Φ - 1 f ∶ FX → σ TFY , and unique solutions given by f ↦ Φ (( Φ - 1 f ) † ) .
A Trace (of) Sandwich A Trace Sandwich Sandwich Theorems for Monads with Iteration Theorem Let F ∶ C → D and U ∶ D → C be a pair of adjoint functors with associated natural isomorphism Φ ∶ D ( FX , Y ) → C ( X , UY ) , and let T be a guarded iterative monad on D . Then the monad induced on the composite functor UTF is guarded iterative, with the guardedness relation defined by taking f ∶ X → σ UTFY if and only if Φ - 1 f ∶ FX → σ TFY , and unique solutions given by f ↦ Φ (( Φ - 1 f ) † ) . Theorem With an adjunction as in the previous theorem, let S be an Elgot monad on D . Then, the monad induced on the composite USF is an Elgot monad.
A Trace (of) Sandwich State Tracing with Resumptions Coinductive Resumption Transformer For a base monad R , R Σ X = νγ. R ( X + Σ γ ) . R Σ is guarded iterative (since it is completely iterative [Pir´ og, Gibbons ’14])
A Trace (of) Sandwich State Tracing with Resumptions Coinductive Resumption Transformer For a base monad R , R Σ X = νγ. R ( X + Σ γ ) . R Σ is guarded iterative (since it is completely iterative [Pir´ og, Gibbons ’14]) Transformed using the State transformer let F ⊣ U be the state monad transformer for S and Σ = --- × S ; sandwich allows to define operation save ∶ 1 → UR Σ F that saves the current state into the trace
Recommend
More recommend