Combining Effects and Coeffects via Grading (slides) Marco Gaboardi - - PowerPoint PPT Presentation

combining effects and
SMART_READER_LITE
LIVE PREVIEW

Combining Effects and Coeffects via Grading (slides) Marco Gaboardi - - PowerPoint PPT Presentation

Combining Effects and Coeffects via Grading (slides) Marco Gaboardi Shin-ya Katsumata Dominic Orchard SUNY Buffalo, USA Kyoto University, Japan University of Kent, UK Flavien Breuvart Tarmo Uustalu INRA Sophia Antipolis, France Institute


slide-1
SLIDE 1

Combining Effects and Coeffects via Grading (slides)

Marco Gaboardi SUNY Buffalo, USA Shin-ya Katsumata Kyoto University, Japan

Dominic Orchard University of Kent, UK

Flavien Breuvart INRA Sophia Antipolis, France Tarmo Uustalu Institute of Cybernetics at TUT, Estonia

ICFP 2016, Nara, Japan - September 21st

slide-2
SLIDE 2

What are coeffects?

  • Per-variable coeffect dependency properties:
  • Live-variable analysis (live/dead)
  • Information flow, e.g., security levels (High/Low)
  • Bounded Linear Logic (how much variable reuse? resource analysis)
  • Deconstructors (strictness analysis)
  • Probabilities and schedules
  • Whole-context coeffect dependencies (not in this paper)
  • Additional hardware resources
  • Library versions
  • Type class instances

Computations make changes to the context (effects) and make demands from the context (coeffects).

1/16

slide-3
SLIDE 3

Coeffect analysis and meta languages

  • Type-and-coeffect system

➡ whole-context analysis [Petricek et al. ’13]

Γ | r ` e : τ

➡ per-variable analysis [Petricek et al. ’14, Ghica & Smith ’14]

x : A · r, . . . , y : B · s ` e : τ

  • Semiring structure on coeffect information

2/16

  • Semantics via graded comonads
  • Coeffect meta-language

➡ indexed type constructor, generalises linear exponential

Γ, x : !rA ` e : !sA

[Brunel et al. ’14]

slide-4
SLIDE 4

This paper: effect and coeffect interactions

➡ Consider two examples:

  • Security levels masking non-determinism
  • Exceptions change linear reuse bounds

3/16

➡ Graded semantics: graded monads, graded comonads,

graded distributive laws (new here)

  • parametric in effect, coeffect algebra

Γ, x : ⇤rA ` t : B Γ ` t : TeA

➡ Core calculus

  • linear-lambda calculus with effect and coeffect types
slide-5
SLIDE 5

Graded semantics Core calculus Interaction example

slide-6
SLIDE 6

Graded semantics Core calculus Interaction example

slide-7
SLIDE 7

comp id

A → TA

4/16

monads A → TB B → TC A → TC

slide-8
SLIDE 8

⇤rA → B

⇤sB → C

comp

(E, •, 1) with monoid graded comonad [coeffect papers]

6/16

with monoid (R, ∗, 1)

comp

⇤r∗sA → C

id

⇤1A → A

A → TeB B → TfC graded monad [Katsumata ’14] A → Te•fC

id

A → T1A

Graded slogan: match the structure of computation with structure in the indices

slide-9
SLIDE 9

Graded semantics Core calculus Interaction example

slide-10
SLIDE 10

Graded semantics Core calculus Interaction example

slide-11
SLIDE 11

Core calculus

  • i. Graded monadic meta language for effects
  • ii. Graded comonadic meta language for coeffects
  • iii. Linear lambda calculus (substructural via coeffects)
  • iv. Graded distributed law for interaction

7/16

slide-12
SLIDE 12
  • 1. Graded monadic meta language for effects

8/16

unit

Γ ` t : A Γ ` hti : T1A Pre-ordered monoid of effect information E = (E, •, 1, ≤)

letT

Γ ` t1 : TeA ∆, x : A ` t2 : TfB Γ + ∆ ` let hxi = t1 in t2 : Te•fB

e.g., exceptions, with simple analysis E = {>, ?} = may succeed = definite exception ⊥ > 1 = >

  • = ∧

? ^ ? = ? ? ^ > = ? > ^ ? = ? > ^ > = >

slide-13
SLIDE 13
  • 2. Graded comonad meta language for coeffects

Pre-ordered semiring of coeffect information C = (C, ∗, 1, +, 0, ≤)

9/16

der

Γ, x : A ` t : B Γ, x : ⇤1A ` t : B

pr

⇤Γ ` t : B r⇤⇤Γ ` [t] : ⇤rB e.g. Bounded Linear Logic (Girard et al.) C = (N, ∗, 1, +, 0, ≤)

let⇤Γ ` t1 : ⇤rA

∆, x : ⇤rA ` t2 : B Γ + ∆ ` let [x] = t1 in t2 : B

pr

x : [int]1, y : [int]2 ` x + y + y : int

pr

1 2 `

x : [int]2, y : [int]4 ` [x + y + y] : ⇤2int

slide-14
SLIDE 14
  • 3. Linear lambda calculus

Coeffect + for contraction (let☐), (letT), (app)

ax

x : A ` x : A

abs

Γ, x : A ` t : B Γ ` λx.t : A ! B

app

Γ ` t : A ! B ∆ ` t0 : A Γ + ∆ ` t t0 : B

Coeffect 0 for weakening (sub)

10/16

sub

Γ ` t : A Γ0 <: Γ A <: B Γ0, ⇤0∆ ` t : B (x : ⇤rA, Γ) + (x : ⇤sA, ∆) = x : ⇤r+sA, (Γ + ∆) (x : A, Γ) + (x : A, ∆) is ill-formed

slide-15
SLIDE 15

Graded semantics Core calculus Interaction example

slide-16
SLIDE 16

Graded semantics Core calculus Interaction example

slide-17
SLIDE 17

pr⇤rΓ ` [t1] : ⇤rT⊥A

BLL / exception composition

11/16

∆, x : ⇤rA ` t2 : B Γ ` t1 : T⊥A

pr der*⇤1Γ ` t1 : T⊥A

unit

∆, x : ⇤rA ` t2 : B ∆, x : ⇤rA ` ht2i : T1B ∆, x0 : T?⇤rA ` . . . : T?B

Goal: an exception “cancels out” resource requirements i.e. (above) r = 1

(definite exception)

slide-18
SLIDE 18

BLL / exception composition

11/16

∆, x : ⇤rA ` t2 : B Γ ` t1 : T⊥A

pr der*⇤1Γ ` t1 : T⊥A

unit

∆, x : ⇤rA ` t2 : B ∆, x : ⇤rA ` ht2i : T1B ∆, x0 : T?⇤rA ` . . . : T?B

pr⇤1Γ ` [t1] : ⇤1T⊥A

Notion of a distributive law of comonad over monad provides:

dist : ⇤ T A → T ⇤ A

Indexed distributive law?

distr,e,A : ⇤r Te A → Te ⇤r A

No effect and coeffect interaction Goal: an exception “cancels out” resource requirements

i.e. (above) r = 1

sequence :: Monad m => [m a] → m [a]

🔕

(definite exception)

slide-19
SLIDE 19

distributive law on monoids: “matched pair” (Zappa-Szep product)

hκ, ιi : R ⇥ E ! E ⇥ R

Indexed vs. graded distributive law

12/16

Graded slogan: algebraic structure on indices matching shape of computation

ι : R × E → R κ : R × E → E

ι(r, 1) = r ι(r, e • f) = ι(ι(r, e), f) ι(1, e) = 1 ι(r ∗ s, e) = ι(r, κ(s, e)) ∗ ι(s, e) κ(1, e) = e κ(r ∗ s, e) = κ(r, κ(s, e)) κ(r, 1) = 1 κ(r, e • f) = κ(r, e) • κ(ι(r, e), f)

Graded:

distr,e,A : ⇤ι(r,e) Te A → Tκ(r,e) ⇤r A

Primitive in our core calculus Indexed:

distr,e,A : ⇤r Te A → Te ⇤r A

slide-20
SLIDE 20

BLL / exception composition

13/16

∆, x : ⇤rA ` t2 : B Γ ` t1 : T⊥A

pr der*⇤1Γ ` t1 : T⊥A

unit

∆, x : ⇤rA ` t2 : B ∆, x : ⇤rA ` ht2i : T1B ∆, x0 : T?⇤rA ` . . . : T?B

pr⇤1Γ ` [t1] : ⇤1T⊥A

Goal: an exception “cancels out” resource requirements

  • Matched pair

κ(r, e) = e distr,e,A : ⇤ι(r,e) Te A → Tκ(r,e) ⇤r A

slide-21
SLIDE 21

BLL / exception composition

13/16

∆, x : ⇤rA ` t2 : B Γ ` t1 : T⊥A

pr der*⇤1Γ ` t1 : T⊥A

unit

∆, x : ⇤rA ` t2 : B ∆, x : ⇤rA ` ht2i : T1B ∆, x0 : T?⇤rA ` . . . : T?B

pr⇤1Γ ` [t1] : ⇤1T⊥A

  • Matched pair

κ(r, e) = e

Goal: an exception “cancels out” resource requirements

ι(r, ⊥) = 1 distr,e,A : ⇤ι(r,e) Te A → Te ⇤r A

slide-22
SLIDE 22

BLL / exception composition

13/16

Γ ` t1 : T⊥A

pr der*⇤1Γ ` t1 : T⊥A

⇤ ∆, x : ⇤rA ` t2 : B

unit

∆, x : ⇤rA ` t2 : B ∆, x : ⇤rA ` ht2i : T1B ∆, x0 : T?⇤rA ` . . . : T?B

pr⇤1Γ ` [t1] : ⇤1T⊥A

  • Matched pair

κ(r, e) = e ι(r, ⊥) = 1 distr,>,A : ⇤r T> A → T> ⇤r A ι(r, >) = r ⇤1Γ ` dist [t1] : T⊥⇤rA ∆ + ⇤1Γ ` let x0 = dist [t1] in . . . : T?A ✔

Goal: an exception “cancels out” resource requirements

distr,⊥,A : ⇤1 T⊥ A → T⊥ ⇤r A

slide-23
SLIDE 23

φ T over ⇤ (T⇤) ⇤ over T (⇤T) F φ

r,e

r,e

F φ

r,e

r,e

ll Te ⇤r → ⇤ι(r,e) Tκ(r,e) ⇤r Te → Tκ(r,e) ⇤ι(r,e) rr Tκ(r,e) ⇤ι(r,e) → ⇤r Te ⇤ι(r,e) Tκ(r,e) → Te ⇤r lr Te ⇤ι(r,e) → ⇤r Tκ(r,e) ⇤ι(r,e) Te → Tκ(r,e) ⇤r rl Tκ(r,e) ⇤r → ⇤ι(r,e) Te ⇤r Tκ(r,e) → Te ⇤ι(r,e)

  • 4. Graded distributive law design space

“forward” (L) vs “backward” (R) “T☐” vs “☐T” ⇤ T A → T ⇤ A T ⇤ A → ⇤ T A

14/16

Te ⇤... A → ⇤... Tκ(e,r) A T... ⇤r A → ⇤ι(r,e) T... A ` distφ : F φ

r,eA ! Gφ r,eA

Tκ(e,r) ⇤... A → ⇤... Te A T... ⇤ι(r,e) A → ⇤r T... A

slide-24
SLIDE 24

Categorical semantics

  • Graded strong monad (Katsumata 2014)
  • Graded monoidal comonad (Petricek et al. 2013, Brunel et al.

2014, Petricek et al. 2014, Ghica&Smith et al. 2014)

15/16

  • Family of matched-pair graded distributive law (new)
  • Soundness theorem
slide-25
SLIDE 25

Concluding remarks

  • Foundation for considering effect-coeffect interaction
  • Further possibilities?
  • Exceptions interacting with security levels (e.g., content of a high

security exception not observable to lower security observer)

  • Non-determinism with probabilistic variables
  • Graded distributive laws useful in other contexts

Thank you.

16/16

sequence :: [m a] → m [a] sequence :: [me a]n → men[a]n

slide-26
SLIDE 26

Extras

slide-27
SLIDE 27

distributive law on monoids: “matched pair” (Zappa-Szep product)

DrA

f

− → TeB DsB

g

− → TfC Dr∗ι(s,e)A

f †

− → Dι(s,e)TeB

σs,e,A

− − − − → Tκ(s,e)DsB

g∗

− → Tκ(s,e)•fC − → − → − → Dι(s,e)TeB

σs,e,A

− − − − → Tκ(s,e)DsB

backward propagation of coeffects forward propagation of effects

Graded distributive law

17/16

(monoid on E × R)

hκ, ιi : R ⇥ E ! E ⇥ R ι : R × E → R κ : R × E → E

ι(r, 1) = r ι(r, e • f) = ι(ι(r, e), f) ι(1, e) = 1 ι(r ∗ s, e) = ι(r, κ(s, e)) ∗ ι(s, e) κ(1, e) = e κ(r ∗ s, e) = κ(r, κ(s, e)) κ(r, 1) = 1 κ(r, e • f) = κ(r, e) • κ(ι(r, e), f)