Type inference for monotonicity Michael Arntzenius University of - - PowerPoint PPT Presentation

type inference for monotonicity
SMART_READER_LITE
LIVE PREVIEW

Type inference for monotonicity Michael Arntzenius University of - - PowerPoint PPT Presentation

Type inference for monotonicity Michael Arntzenius University of Birmingham S-REPLS 9, 2018 x + log x x x log x 4 ::= R | A B types A , B terms M , N ::= x | x . M | M N A Poset R = R A B =


slide-1
SLIDE 1

Type inference for monotonicity

Michael Arntzenius

University of Birmingham

S-REPLS 9, 2018

slide-2
SLIDE 2

x + log x −x x − log x

4

slide-3
SLIDE 3

types

A, B ::= R | A → B

terms M, N ::= x | λx. M | M N

A ∈

Poset

R = R A → B =

monotone maps A → B,

  • rdered pointwise
slide-4
SLIDE 4

λx. x + log x : R → R λx. −x :

???? → R

λx. x − log x :

??? → R

λx. 4 :

??? → R

slide-5
SLIDE 5

λx. x + log x : R → R λx. −x :

  • p R → R

λx. x − log x : ✷R → R λx. 4 : ♦R → R

slide-6
SLIDE 6

Let ✷A be A, ordered discretely:

x y : ✷A ⇐ ⇒ x = y : A

Then f : ✷A → B is monotone iff

x = y = ⇒ f(x) f(y)

i.e. always!

slide-7
SLIDE 7

λx. x + log x : R → R λx. −x :

  • p R → R

λx. x − log x : ✷R → R λx. 4 : ♦R → R

slide-8
SLIDE 8

♦A identifies weakly connected elements in A: x y ∨ y x : A = ⇒ x = y : ♦A

slide-9
SLIDE 9

♦A identifies weakly connected elements in A: x y ∨ y x : A = ⇒ x = y : ♦A

Theorem:

f : ♦A → B ⇐ ⇒ f : A → ✷B

slide-10
SLIDE 10

λx. x + log x : R → R λx. −x :

  • p R → R

λx. x − log x : ✷R → R λx. 4 : ♦R → R

slide-11
SLIDE 11

✷ is a necessity modality / monoidal comonad; ♦ is a possibility modality / monoidal monad.

Pfenning & Davies gave typing rules for these in A Judgmental Reconstruction of Modal Logic!

slide-12
SLIDE 12

(λx. x − log x) : ✷R → R

slide-13
SLIDE 13

(λx. x − log x) : ✷R → R (λx. let box u = x in u − box (log u)) : ✷R →

: (

slide-14
SLIDE 14

How to infer monotonicity:

  • 1. Infer variable usage modes bottom-up.
  • 2. Use modal subtyping for implicit coercion.
slide-15
SLIDE 15
  • 1. Bottom-up mode inference

Γ ⊢ M : B

slide-16
SLIDE 16
  • 1. Bottom-up mode inference

x : [T] A ⊢ M : B

slide-17
SLIDE 17
  • 1. Bottom-up mode inference

x : [T] A ⊢ M : B

modes T ::= id | op | ✷ | ♦

slide-18
SLIDE 18
  • 1. Bottom-up mode inference: duplication

x : [T] A ⊢ M : A x : [U] A ⊢ N : B x : [??????] A ⊢ (M, N) : A × B

slide-19
SLIDE 19
  • 1. Bottom-up mode inference: duplication

x : [T] A ⊢ M : A x : [U] A ⊢ N : B x : [T ∧ U] A ⊢ (M, N) : A × B ♦ ✷

id

  • p
slide-20
SLIDE 20
  • 1. Bottom-up mode inference: composition

x : [T] A ⊢ M : B y : [U] B ⊢ N : C x : [UT] A ⊢ let y = M in N

slide-21
SLIDE 21
  • 1. Bottom-up mode inference: composition

x : [T] A ⊢ M : B y : [U] B ⊢ N : C x : [UT] A ⊢ let y = M in N

slide-22
SLIDE 22
  • 1. Bottom-up mode inference: composition

x : [T] A ⊢ M : B y : [U] B ⊢ N : C x : [UT] A ⊢ let y = M in N

Composition UT is a monoid, with id neutral:

UT T

id

  • p

✷ ♦ U

id id

  • p

✷ ♦

  • p
  • p

id

✷ ♦ ✷ ✷ ✷ ✷ ♦ ♦ ♦ ♦ ✷ ♦

slide-23
SLIDE 23

Composition UT and meet T ∧ U form a semiring! Other systems with semiring-valued annotations:

  • 1. Linear Haskell (POPL 2018)
  • 2. I Got Plenty o’ Nuttin (McBride)
  • 3. Monotonicity Types (PMLDC 2017)
  • 4. Bounded Linear Types in a Resource Semiring (ESOP 2014)
  • 5. The Next 700 Modal Type Assignment Systems (TYPES 2015)
  • 6. A Fibrational Framework for Substructural and Modal Logics

(FSCD 2017) massively generalizes this pattern. ... more?

slide-24
SLIDE 24
  • 2. Modal subtyping

(λx. let box u = x in u − box (log u)) : ✷R →

slide-25
SLIDE 25
  • 2. Modal subtyping: ✷-introduction?

SUBSUMPTION

Γ ⊢ M : A A <: B Γ ⊢ M : B

But A <: ✷A!

slide-26
SLIDE 26
  • 2. Modal subtyping!

[U] A <: B

Finds the most general mode U such that UA <: B.

slide-27
SLIDE 27
  • 2. Modal subtyping: ✷-introduction!

SUBSUMPTION

x : [T] A ⊢ M : B [U] B <: C x : [UT] A ⊢ M : C

slide-28
SLIDE 28
  • 2. Modal subtyping: ✷-introduction!

SUBSUMPTION

x : [T] A ⊢ M : B [U] B <: C x : [UT] A ⊢ M : C

SUBTYPING INTO ✷

[T] A <: B [✷T] A <: ✷B

slide-29
SLIDE 29
  • 2. Modal subtyping: ✷-introduction!

SUBSUMPTION

x : [T] A ⊢ M : B [U] B <: C x : [UT] A ⊢ M : C

SUBTYPING INTO ✷

[T] A <: B [✷T] A <: ✷B

✷-INTRODUCTION VIA SUBSUMPTION

x : [T] A ⊢ M : B x : [✷T] A ⊢ M : ✷B

slide-30
SLIDE 30
  • 2. Modal subtyping: ✷-elimination

f : ♦A → B ⇐ ⇒ f : A → ✷B

slide-31
SLIDE 31
  • 2. Modal subtyping: ✷-elimination

f : ♦A → B ⇐ ⇒ f : A → ✷B

Which suggests this elimination rule for ✷:

x : [T] A ⊢ M : ✷B x : [♦T] A ⊢ unbox M : B

slide-32
SLIDE 32
  • 2. Modal subtyping: ✷-elimination

f : ♦A → B ⇐ ⇒ f : A → ✷B

Which suggests this elimination rule for ✷:

x : [T] A ⊢ M : ✷B x : [♦T] A ⊢ unbox M : B

Happily, we can do this with subtyping!

SUBTYPING OUT OF ✷

[T] A <: B [T♦] ✷A <: B

✷-ELIMINATION VIA SUBSUMPTION

x : [T] A ⊢ M : ✷B x : [♦T] A ⊢ M : B

slide-33
SLIDE 33

How to infer monotonicity:

  • 1. Infer variable usage modes bottom-up.
  • 2. Use modal subtyping for implicit coercion.
slide-34
SLIDE 34

State of work

  • 1. Solved: Pattern matching!
  • 2. Conjectured: Substitution.
  • 3. Unsolved: Internalizing ♦ w/o annotations.
  • 4. Open: HM-style type inference?
  • 5. Pfenning-Davies elim rule is derivable from

substitution.

http://www.rntz.net/files/tones.pdf http://www.rntz.net/datafun