A Trace (of) Sandwich Christoph Rauch , Sergey Goncharov, Lutz Schr - - PowerPoint PPT Presentation

a trace of sandwich
SMART_READER_LITE
LIVE PREVIEW

A Trace (of) Sandwich Christoph Rauch , Sergey Goncharov, Lutz Schr - - PowerPoint PPT Presentation

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 =


slide-1
SLIDE 1

A Trace (of) Sandwich

A Trace (of) Sandwich

Christoph Rauch, Sergey Goncharov, Lutz Schr¨

  • der, Maciej Pir´
  • g

TCS Seminar SS 2019 FAU Erlangen-N¨ urnberg

July 2, 2019

slide-2
SLIDE 2

A Trace (of) Sandwich

A prototype While language

Syntax

s ∶∶= skip ∣ x ∶= e ∣ s0;s1 ∣ if e thenst else sf ∣ while e do st e are integer expressions 0 treated as false

slide-3
SLIDE 3

A Trace (of) Sandwich

Trace-Based Big-Step Operational Semantics

For the simple While language [Nakata, Uustalu ’09]

σ ⊧ e (st,σ ∶∶ ⟨σ⟩)

⇒ τ (while e do st,τ)

⇒ τ ′ (while e do st,σ) ⇒ τ ′ σ / ⊧ e (while e do st,σ) ⇒ σ ∶∶ ⟨σ⟩ (s,σ) ⇒ τ (s,⟨σ⟩)

⇒ τ (s,τ)

⇒ τ ′ (s,σ ∶∶ τ)

⇒ σ ∶∶ τ ′

slide-4
SLIDE 4

A Trace (of) Sandwich

Generalizing the While Language

Syntax

s ∶∶= skip ∣ x ∶= e ∣ s0;s1 ∣ if e thenst else sf ∣ while e do st e are integer expressions 0 treated as false

slide-5
SLIDE 5

A Trace (of) Sandwich

Generalizing the While Language

Syntax

s ∶∶= a ∣ s0;s1 ∣ if b thenst else sf ∣ while b do st a are actions b boolean expressions

slide-6
SLIDE 6

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: TTT TT TT T

Tµ µ µ µ

T TT T T

η µ Tη

slide-7
SLIDE 7

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: TTT TT TT T

Tµ µ µ µ

T TT T T

η µ Tη

Strength

A monad T is strong if there is a natural transformation τ ∶ TX × Y → T(X × Y ) subject to standard coherence conditions

slide-8
SLIDE 8

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: TTT TT TT T

Tµ µ µ µ

T TT T T

η µ Tη

Strength

A monad T is strong if there is a natural transformation τ ∶ TX × Y → T(X × Y ) subject to standard coherence conditions

Kleisli composition

We write g ◇ f for X

f

  • → TY

Tg

→ TTZ

µ

  • → TZ.
slide-9
SLIDE 9

A Trace (of) Sandwich

Composing Monads

Examples of Monads

Power set (nondeterminism), PX = {A ∣ A ⊆ X} Reader monad (for an environment E), TX = X E

slide-10
SLIDE 10

A Trace (of) Sandwich

Composing Monads

Examples of Monads

Power set (nondeterminism), PX = {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, . . . )

slide-11
SLIDE 11

A Trace (of) Sandwich

Adjunctions

Definition

An adjunction consists of functors C D

F U

⊣ and a natural iso Φ ∶ D(FX,Y ) ≅ C(X,UY ) ∶ ψ.

slide-12
SLIDE 12

A Trace (of) Sandwich

Adjunctions

Definition

An adjunction consists of functors C D

F U

⊣ and a natural iso Φ ∶ D(FX,Y ) ≅ C(X,UY ) ∶ ψ.

Unit, counit, triangle identities

η ∶ 1 → UF ǫ ∶ FU → 1 FX

FηX

  • → FUFX

ǫFX

  • → FX = idFX

(⋆) UY

ηUY

  • → UFUY

UǫY

  • → UY = idUY

(◻)

slide-13
SLIDE 13

A Trace (of) Sandwich

Sandwiching

Composition of Adjunctions

If F ⊣ U and G ⊣ V , then also GF ⊣ UV .

slide-14
SLIDE 14

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 ηP = X

η

  • → UFX

UηT

  • → UTFX

µP = UTFUTFX

UTǫ

  • → UTTFX

UµT

  • → UTFX

is a monad on C.

slide-15
SLIDE 15

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 ηP = X

η

  • → UFX

UηT

  • → UTFX

µP = UTFUTFX

UTǫ

  • → UTTFX

UµT

  • → UTFX

is a monad on C.

Proposition

Every monad arises from an adjunction. Given (T,ηT,µT) on C, there exists D and F ⊣ U,η ∶ 1 → UF,ǫ ∶ FU → 1 s.t. T = UF,ηT = η,µT = Uǫ.

slide-16
SLIDE 16

A Trace (of) Sandwich

Sandwiching: Monad Transformers

Examples

Writer: M a monoid, then Set M−Set

F 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

slide-17
SLIDE 17

A Trace (of) Sandwich

Sandwiching: Monad Transformers

Examples

Writer: M a monoid, then Set M−Set

F 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 Set Set

  • ×S

(-

  • )S

⊣ is the state monad transformer.

slide-18
SLIDE 18

A Trace (of) Sandwich

Denotational Semantics for the While Language

Interpretation without loops

⟦a⟧ = a ⟦s0;s1⟧ = ⟦s0⟧ ◇ ⟦s1⟧ ⟦if b thenst else sf ⟧ = [⟦st⟧,⟦sf ⟧] ◇ b ⟦while b do st⟧ = ? actions a are morphisms 1 → T1 booleans b are morphisms 1 → T(1 + 1)

slide-19
SLIDE 19

A Trace (of) Sandwich

Iteration for Monads

morphisms X → T(Y + X) seen as systems of recursive equations

  • btain different classes of monads by demanding properties of

solutions to these equations guardedness as a means of guaranteeing existence of solutions

slide-20
SLIDE 20

A Trace (of) Sandwich

Iteration for Monads

Abstract guardedness for a monad T

guarded: Kleisli category of T is a guarded category

slide-21
SLIDE 21

A Trace (of) Sandwich

Iteration for Monads

Abstract guardedness for a monad T

guarded: Kleisli category of T is a guarded category f ∶ X → Y in1 ○ f ∶ X →in2 (Y + Z) f ∶ X →σ Z g ∶ Y →σ Z [f ,g] ∶ X + Y →σ Z f ∶ X →in2 (Y + Z) g ∶ Y →σ V h ∶ Z → V [g,h] ○ f ∶ X →σ V

slide-22
SLIDE 22

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 T in1 ○ f ∶ X →in2 T(Y + Z) f ∶ X →σ TZ g ∶ Y →σ TZ [f ,g] ∶ X + Y →σ TZ f ∶ X →in2 T(Y + Z) g ∶ Y →σ TV h ∶ Z → TV µ ○ T[g,h] ○ f ∶ X →σ TV

slide-23
SLIDE 23

A Trace (of) Sandwich

Iteration for Monads

Abstract guardedness for a monad T

guarded: Kleisli category of T is a guarded category

slide-24
SLIDE 24

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]

slide-25
SLIDE 25

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 Φ-1f ∶FX →σ TFY , and unique solutions given by f ↦ Φ((Φ-1f )†).

slide-26
SLIDE 26

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 Φ-1f ∶FX →σ TFY , and unique solutions given by f ↦ Φ((Φ-1f )†).

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.

slide-27
SLIDE 27

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´

  • g, Gibbons ’14])
slide-28
SLIDE 28

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´

  • g, 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

slide-29
SLIDE 29

A Trace (of) Sandwich

Denotational Trace Semantics for the While Language

Interpretation

⟦a⟧ = a ⟦s0;s1⟧ = ⟦s0⟧ ◇ ⟦s1⟧ ◇ γ ⟦if b thenst else sf ⟧ = [⟦st⟧,⟦sf ⟧] ◇ b ◇ γ ⟦while b do st⟧ = ([T inr ○⟦st⟧,T inl○η] ◇ b ◇ γ)† γ ∶ 1 → URΣF1 guard operation with γ = save, trace equivalent to Nakata/Uustalu! [Pir´

  • g, Gibbons ’13]

actions a are morphisms 1 → URΣF1 booleans b are morphisms 1 → URΣF(1 + 1)

slide-30
SLIDE 30

A Trace (of) Sandwich

Moving to the Metalanguage

Get rid of artificial guards

replace actions by operations from a signature annotated with guardedness info guarded iteration construct instead of ordinary while loop move to a multi-variable setting guardedness tracked through programs via the typing rules

slide-31
SLIDE 31

A Trace (of) Sandwich

Syntax

Types

A,B,... ∶∶= C ∣ 0 ∣ 1 ∣ A + B ∣ A × B (C ∈ Base)

Signatures

value signature Σv of f ∶ A → B (e.g. + ∶ Nat × N → N) effect signature Σc of f ∶ A → B[C] (e.g. put ∶ N → 0[1], the type in the brackets denotes the guarded portion of the type)

Value and Computation Term Judgements

Γ ⊢v v ∶ A and ∆ ∣ Γ ⊢c p ∶ A Γ = (x1 ∶ A1,...,xn ∶ An) (variable context) ∆ = (e1 ∶ E α1

1 ,...,em ∶ E αm m )

(exception context) αi ∈ {g,u} indicate (un-)guardedness

slide-32
SLIDE 32

A Trace (of) Sandwich

Some Derivation Rules

f ∶ A → B[C] ∈ Σc Γ ⊢v v ∶ A ∆ ∣ Γ,x ∶ B ⊢c p ∶ D ∆′ ∣ Γ,y ∶ C ⊢c q ∶ D ∣∆∣ = ∣∆′∣ ∆ ∣ Γ ⊢c gcasef (v) of inlx ↦ p; inr y ↦ q ∶ D ∆,e ∶ E u ∣ Γ ⊢c p ∶ A ∆ ∣ Γ,e ∶ E ⊢c q ∶ A ∆ ∣ Γ ⊢c handle e in p with q ∶ A e ∶ E u in ∆ Γ ⊢v q ∶ E ∆ ∣ Γ ⊢c raisee q ∶ D Γ ⊢v p ∶ E ∆,e ∶ E g ∣ Γ,e ∶ E ⊢c q ∶ A ∆ ∣ Γ ⊢c handleit e = p in q ∶ A

slide-33
SLIDE 33

A Trace (of) Sandwich

The Final Trace Sandwich

Ingredients

slide-34
SLIDE 34

A Trace (of) Sandwich

The Final Trace Sandwich

Ingredients

base monad R containing the guard operations

slide-35
SLIDE 35

A Trace (of) Sandwich

The Final Trace Sandwich

Ingredients

base monad R containing the guard operations monad of computations T with decomposition as adjunction F ⊣ U

slide-36
SLIDE 36

A Trace (of) Sandwich

The Final Trace Sandwich

Ingredients

base monad R containing the guard operations monad of computations T with decomposition as adjunction F ⊣ U functor Σ connecting T with the traces, e.g. Σ = ---×S for TX = (X × S)S

slide-37
SLIDE 37

A Trace (of) Sandwich

The Final Trace Sandwich

Ingredients

base monad R containing the guard operations monad of computations T with decomposition as adjunction F ⊣ U functor Σ connecting T with the traces, e.g. Σ = ---×S for TX = (X × S)S generalized coinductive resumption transform RΣ

slide-38
SLIDE 38

A Trace (of) Sandwich

The Final Trace Sandwich

Ingredients

base monad R containing the guard operations monad of computations T with decomposition as adjunction F ⊣ U functor Σ connecting T with the traces, e.g. Σ = ---×S for TX = (X × S)S generalized coinductive resumption transform RΣ trace computations in T using the sandwich URΣF

slide-39
SLIDE 39

A Trace (of) Sandwich

The Final Trace Sandwich

Possible Directions

slide-40
SLIDE 40

A Trace (of) Sandwich

The Final Trace Sandwich

Possible Directions

  • btain trace semantics in the style of [Nakata, Uustalu ’09] for

bigger languages

slide-41
SLIDE 41

A Trace (of) Sandwich

The Final Trace Sandwich

Possible Directions

  • btain trace semantics in the style of [Nakata, Uustalu ’09] for

bigger languages experiment with different monads for tracing

slide-42
SLIDE 42

A Trace (of) Sandwich

References I

  • S. L. Bloom and Z. ´

Esik (1993) Iteration Theories: The Equational Logic of Iterative Processes

  • S. Goncharov, C. Rauch, L. Schr¨
  • der (ICTAC ’18)

A Metalanguage for Guarded Iteration

  • M. P. Jones, L. Duponcheel (1993)

Composing Monads

  • B. Klin, J. Salamanca (MFPS ’18)

Iterated Covariant Powerset is not a Monad

  • O. Manzyuk (2012)

Calculating Monad Transformers with Category Theory

  • K. Nakata, T. Uustalu (TPHOLs ’09)

Trace-Based Coinductive Operational Semantics for While

slide-43
SLIDE 43

A Trace (of) Sandwich

References II

  • M. Pir´
  • g, J. Gibbons (ENTCS ’13)

Monads for Behaviour

  • M. Pir´
  • g, J. Gibbons (MFPS ’14)

The Coinductive Resumption Monad