Let Arguments Go First Ningning Xie, Bruno C. d. S. Oliveira The - - PowerPoint PPT Presentation

let arguments go first
SMART_READER_LITE
LIVE PREVIEW

Let Arguments Go First Ningning Xie, Bruno C. d. S. Oliveira The - - PowerPoint PPT Presentation

Let Arguments Go First Ningning Xie, Bruno C. d. S. Oliveira The University of Hong Kong ESOP 2018, Thessaloniki, Greece 2018-04-17 1 Background 2 Bi-Directional Type Checking Well known in the folklore of type system for a long time


slide-1
SLIDE 1

Let Arguments Go First

Ningning Xie, Bruno C. d. S. Oliveira The University of Hong Kong ESOP 2018, Thessaloniki, Greece 2018-04-17

1

slide-2
SLIDE 2

Background

2

slide-3
SLIDE 3

Bi-Directional Type Checking

  • Well known in the folklore of type system for a long

time

  • Popularized by Pierce and Turner’s work*
  • Can support many type system features:

refinements, indexed types, intersections and unions, contextual modal types, object-oriented subtyping, …

* Benjamin C Pierce and David N Turner. Local type inference. TOPLAS, 22(1):1–44, 2000. 3

slide-4
SLIDE 4

Bi-Directional Type Checking

Two Modes in type-checking

  • Inference (synthesis): e synthesizes A
  • Checked: check e against A
  • Which constructs should be in which mode?

Γ ` e ) A

4

Γ ` e ( A

slide-5
SLIDE 5

Bi-Directional Type Checking

  • A recipe from Dunfield and Pfenning*
  • Introduction rules
  • Elimination rules

Γ, x : A ` e ( B Γ ` λx. e ( A ! B

Γ ` e1 ) A ! B Γ ` e2 ( A Γ ` e1 e2 ) B

APP

⇒ ⇐

Notice here how type information flows from functions to arguments

* Joshua Dunfield and Frank Pfenning. Tridirectional typechecking. POPL’04, 2004. 5

slide-6
SLIDE 6

Bi-Directional Type Checking

  • Consider designing rules for pairs

Γ ` e1 ( A Γ ` e2 ( B Γ ` (e1, e2) ( (A, B)

Pa

Γ ` e1 ) A Γ ` e2 ) B Γ ` (e1, e2) ) (A, B)

  • (1, 2) cannot type-check

…unless you write it as (1, 2) : (int, int)

  • …unless you have also an inference rule for pairs
  • Rules scales up with the typing rules

6

slide-7
SLIDE 7

Contributions

  • A variant of bi-directional type checking
  • A new design for type inference of higher-ranked

types

  • A System-F like calculus

Except the algorithm system, most parts are formalized in Coq with an application mode type information propagates from arguments to functions generalizes the Hindley-Milner type system supports syntactic sugar for polymorphic let compatible with type application encoding type declaration

7

slide-8
SLIDE 8

Contributions

  • A variant of bi-directional type checking
  • A new design for type inference of higher-ranked

types

  • A System-F like calculus

with an application mode type information propagates from arguments to functions generalizes the Hindley-Milner type system supports syntactic sugar for polymorphic let compatible with type application encoding type declaration

8

slide-9
SLIDE 9

Application Mode

9

slide-10
SLIDE 10

(λx. x) 1

  • cannot type-check

…unless you write it as

  • …unless you have also an inference rule for lambdas

(with some type inference)

  • …WAIT! What if the type of the argument is accounted for

in inferring the function?

((λx. x) : int → int) 1

Application Mode

10

slide-11
SLIDE 11

The argument 1 has type int Can the function accept arguments of int Let’s assume x : int We have return type int The type of function is int → int ………

Application Mode

(λx. x) 1

11

slide-12
SLIDE 12

Instead of… An alternative idea is to push the type of the arguments into the typing of the function

Application Mode

Γ ` e1 ) A ! B Γ ` e2 ( A Γ ` e1 e2 ) B

APP

Γ ` e2 ) A Γ p Ψ, A ` e1 ) A ! B Γp Ψ ` e1 e2 ) B

12

slide-13
SLIDE 13
  • Application context is a stack that tracks the type
  • f the arguments
  • Lambda expressions can now make use of the application

context

  • Application Mode

Ψ

Γ, x : A p Ψ ` e ) B Γp Ψ, A ` λx. e ) A ! B

Lam

(λx. x) 1

13

slide-14
SLIDE 14

Two Modes in type-checking

  • Inference (synthesis): e synthesizes A
  • Application: under application ctx

, e synthesizes A

Application Mode

Γ ` e ) A Γ p Ψ ` e ) A Ψ

14

slide-15
SLIDE 15

Application Mode

Γ ` e ) A Γ p Ψ ` e ) A

Whether the expression can be applied or not

  • Expressions can be applied:

variables, lambdas, applications, eliminations of pairs,…

  • Expressions that cannot be applied:

literals, pairs,…

Interpretation n Recipe?

15

slide-16
SLIDE 16

Application Mode

Γ ` e ) A Γ p Ψ ` e ) A

Interpretation n What if the application context is empty?

…it is not applied to any arguments …we know nothing about the expression …we should infer it!

  • We can model inference mode as a particular case of

the application mode

16

slide-17
SLIDE 17

Application Mode

Γ ` e ) A Γ p Ψ ` e ) A

n What if we have a possibly non-empty application context?

  • Inference: e
  • Checked : e, A → B → C
  • Application Mode : e

Interpretation

e : A → B → C

Ψ = A

Ψ = ∅

Ψ = A, B

finer grain notion leads to partial type checking

17

slide-18
SLIDE 18

Application Mode

Γ ` e ) A Γ p Ψ ` e ) A

n Is Inference mode + Application mode better/worse than Bi-Directional type checking?

  • No one is conservative over the other.
  • But it does open paths to design choices

Interpretation

(λx : int → int.λy : bool. y) (λy. y) True (λx. x) 1

18

slide-19
SLIDE 19

Application Mode

Γ ` e ) A Γ p Ψ ` e ) A

Type system with implicit polymorphism and/or static

  • verloading needs type information about the arguments when

type-checking function variables. id 3, (==) True False, … Type system employs an application subtyping

Benefits

Ψ ` A  B

n Local constraint solver for function variables.

19

slide-20
SLIDE 20

Application Mode

Γ ` e ) A Γ p Ψ ` e ) A

Annotations are never needed for applied lambdas It enables let sugar

Benefits

let x = e1 in e2 (λx. e2) e1

n Declaration desugaring for lambda abstractions.

(λx. x) 1

20

slide-21
SLIDE 21

Contributions

  • A variant of bi-directional type checking
  • A new design for type inference of higher-ranked

types

  • A System-F like calculus

with an application mode type information propagates from arguments to functions generalizes the Hindley-Milner type system supports syntactic sugar for polymorphic let compatible with type application encoding type declaration

21

slide-22
SLIDE 22

Application 1 for the Application Mode

22

slide-23
SLIDE 23

Application 1

n Type Inference of (implicit, predicative) Higher-Ranked Types

  • Enables expressiveness power of System F.
  • Undecidable.
  • Hindley-Milner*(henceforth HM) with let

generalization has rank 1 types.

* Luis Damas and Robin Milner. Principal type-schemes for functional programs. POPL ’82, 1982. * J. Roger Hindley. The principal type-scheme of an object in combinatory logic. Transactions of the American Mathematical Society, 146:29–60, 1969. 23

slide-24
SLIDE 24

Application 1

n Type Inference of Higher-Ranked Types

  • GHC rejects:
  • Rewriting according to bi-directional guideline
  • …Wait! If we generalize the identity function

and propagate it into the function…

(λf. (f 1, f ’c’)) (λx. x)

we can rewrite this program as

((λf. (f 1, f ’c’)) : (∀a. a → a) → (Int, Char)) (λx . x)

24 * a state-of-the-art compiler for Haskell

slide-25
SLIDE 25

Application 1

n Type Inference of Higher-Ranked Types Apply the application mode to higher-ranked type system* with generalization on applications

* Martin Odersky and Konstantin L ̈aufer. Putting type annotations to work. POPL ’96, 1996. * Simon Peyton Jones, Dimitrios Vytiniotis, Stephanie Weirich, and Mark Shields. Practical type inference for arbitrary-rank types. Journal of func- tional programming, 17(01):1–82, 2007. * Joshua Dunfield and Neelakantan R. Krishnaswami. Complete and easy bidirectional typechecking for higher-rank polymorphism. ICFP ’13, 2013. 25

slide-26
SLIDE 26

Application 1

n Type Inference of Higher-Ranked Types

  • 1. Sugars for HM style polymorphic let expression
  • 2. Conservative over the HM type system
  • 3. Comparison with existing literatures

26

slide-27
SLIDE 27

Application 1

System Types Impred Let Annotations MLF flexible and rigid yes yes

  • n polymorphically used parameters

HML flexible F-types yes yes

  • n polymorphic parameters

FPH boxy F-types yes yes

  • n polymorphic parameters and some

let bindings with higher-ranked types Peyton Jones et al. (2007) F-types no yes

  • n polymorphic parameters

Dunfield et al. (2013) F-types no no

  • n polymorphic parameters

this paper F-types no sugar on polymorphic parameters that are not applied

27

slide-28
SLIDE 28

Application 1

n Type Inference of Higher-Ranked Types

  • 1. Sugars for HM style polymorphic let expression
  • 2. Conservative over the HM type system
  • 3. Comparison with existing literatures
  • 4. Interesting metatheory studies about the application

mode formalized in Coq.

  • 5. An algorithm; a translation to System F

28

slide-29
SLIDE 29

Contributions

  • A variant of bi-directional type checking
  • A new design for type inference of higher-ranked

types

  • A System-F like calculus

with an application mode type information propagates from arguments to functions generalizes the Hindley-Milner type system supports syntactic sugar for polymorphic let compatible with type application encoding type declaration

29

slide-30
SLIDE 30

Application 2 for the Application Mode

30

slide-31
SLIDE 31

Application 2

“It is possible, of course, to come up with examples where it would be beneficial to synthesize the argument types first and then use the resulting information to avoid type annotations in the function part of an application expression....Unfortunately this refinement does not help infer the type of polymorphic functions. For example, we cannot uniquely determine the type of x in the expression (fun[A](x) e) [Int] 3” *

* Benjamin C Pierce and David N Turner. Local type inference. TOPLAS, 22(1):1–44, 2000. 31

slide-32
SLIDE 32

Application 2

n A Variant of System F with More Expressive Type Applications

…not typeable in traditional System F …using application mode, we can verify Use application context to track type equalities introduced by type application

(Λa. λx : a. x + 1) Int

a = Int

32

slide-33
SLIDE 33

Application 2

n A Variant of System F with More Expressive Type Applications (fun[A](x) e) [Int] 3 x:A

  • r x:Int

33

slide-34
SLIDE 34

Application 2

n A Variant of System F with More Expressive Type Applications

  • 1. Sugar for type synonyms
  • 2. Preserve System F type abstraction

type a = A in e (Λa. e) A

(Λa. λx : a. x + 1) Int

34

let inc = Λa. λx : a. x + 1 in inc Int e

slide-35
SLIDE 35

Application 2

n A Variant of System F with More Expressive Type Applications

  • 1. Sugar for type synonyms
  • 2. Preserve System F type abstraction
  • 3. Metatheory studies formalized in Coq.

type safety, uniqueness of typing

type a = A in e (Λa. e) A

35

slide-36
SLIDE 36

Discussion

36

slide-37
SLIDE 37

Discussion

  • Combine application and checked mode
  • Additional constructs: pairs
  • Encoding declarations in dependent type

systems*

  • Related work

See our full paper if you are interested!

* Paula Severi and Erik Poll. Pure type systems with definitions. Logical Foundations of Computer Science, pages 316–328, 1994. 37

slide-38
SLIDE 38

Thanks!

38

slide-39
SLIDE 39

Let Arguments Go First

Ningning Xie, Bruno C. d. S. Oliveira The University of Hong Kong ESOP 2018, Thessaloniki, Greece 2018-04-17

39