kind inference for datatypes - github pages · kind inference for datatypes ningning xie 1richard...

99
Kind Inference for Datatypes Ningning Xie 1 Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira 1 POPL 2020 1 The University of Hong Kong 2 Bryn Mawr College 3 Tweag I/O 1

Upload: others

Post on 27-Jul-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Kind Inference for Datatypes

Ningning Xie 1 Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira 1

POPL 2020

1The University of Hong Kong

2Bryn Mawr College

3Tweag I/O

1

Page 2: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Datatypes 101

• A datatype declaration defines a type, and the shape of each of the elements.

data List a = Nil | Cons a (List a)

-- List :: ?→ ? type constructor

-- Nil :: ∀a . List a data constructor

-- Cons :: ∀a . a → List a → List a data constructor

• Kind inference.

List Int :: ? -- accepted

List Maybe :: ? -- rejected

-- Maybe :: ?→ ?

2

Page 3: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Datatypes 101

• A datatype declaration defines a type, and the shape of each of the elements.

data List a = Nil | Cons a (List a)

-- List :: ?→ ? type constructor

-- Nil :: ∀a . List a data constructor

-- Cons :: ∀a . a → List a → List a data constructor

• Kind inference.

List Int :: ? -- accepted

List Maybe :: ? -- rejected

-- Maybe :: ?→ ?

2

Page 4: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Many Extensions

, But...

Haskell, Idris, Coq, Agda, ...

• Polymorphic Kinds

• Dependent Types

• ...

data X :: ∀a (b :: ?→ ?). a b → ?

data Y :: ∀(c :: Maybe Bool). X c → ?

error: Couldn’t match kind ...

3

Page 5: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Many Extensions, But...

Haskell, Idris, Coq, Agda, ...

• Polymorphic Kinds

• Dependent Types

• ...

data X :: ∀a (b :: ?→ ?). a b → ?

data Y :: ∀(c :: Maybe Bool). X c → ?

error: Couldn’t match kind ...

3

Page 6: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Many Extensions, But...

Haskell, Idris, Coq, Agda, ...

• Polymorphic Kinds

• Dependent Types

• ...

data X :: ∀a (b :: ?→ ?). a b → ?

data Y :: ∀(c :: Maybe Bool). X c → ?

error: Couldn’t match kind ...

3

Page 7: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Problem

• Which datatype declarations should be accepted?

• What kinds do accepted datatypes have?

4

Page 8: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Problem

• Which datatype declarations should be accepted?

• What kinds do accepted datatypes have?

4

Page 9: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Why It Is Interesting

• A declarative specification of datatypes guides programmers.

• An algorithm helps inform the design of principled inference algorithms for

datatypes.

5

Page 10: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Why It Is Interesting

• A declarative specification of datatypes guides programmers.

• An algorithm helps inform the design of principled inference algorithms for

datatypes.

5

Page 11: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Contributions

• Kind inference for Haskell98

The first formalization of Haskell98’s datatype declarations.

• Kind inference for modern Haskell

A type and kind language that is unified and dependently typed.

• Technical advances

Technical innovations important in the implementation of type-inference for dependent types.

6

Page 12: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Kind Inference for Haskell98

Page 13: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Language Features

kind κ ::= ? | κ1 → κ2

• Haskell98 supports mutual recursion.

data P a = MkP (Q a)

data Q a = MkQ (P a)

7

Page 14: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Language Features

kind κ ::= ? | κ1 → κ2

• Haskell98 supports mutual recursion.

data P a = MkP (Q a)

data Q a = MkQ (P a)

7

Page 15: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Language Features

data Tree a = Leaf | Fork (Tree a) (Tree a)

-- Tree :: ?→ ??

-- Tree :: (?→ ?)→ ??

-- ... (infinitely many incomparable kinds)

Prelude> :kind TreeTree :: ?

8

Page 16: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Language Features

data Tree a = Leaf | Fork (Tree a) (Tree a)

-- Tree :: ?→ ??

-- Tree :: (?→ ?)→ ??

-- ... (infinitely many incomparable kinds)

Prelude> :kind TreeTree :: ?

8

Page 17: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

It is possible that some parts of an inferred kind may not

be fully determined by the corresponding definitions; in

such cases, a default of ? is assumed.

Haskell98 Report Sec.4.6

9

Page 18: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Defaulting

data Tree a = Leaf | Fork (Tree a) (Tree a)

-- Tree :: ?→ ? winner!

10

Page 19: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Defaulting

data Tree a = Leaf | Fork (Tree a) (Tree a)

-- Tree :: ?→ ? winner!

10

Page 20: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Defaulting

data Tree a = Leaf | Fork (Tree a) (Tree a)

-- Tree :: ?→ ? winner!

data P1 a = MkP1 P2

data P2 = MkP2 (P1 Maybe)

10

Page 21: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Defaulting

data Tree a = Leaf | Fork (Tree a) (Tree a)

-- Tree :: ?→ ? winner!

data P1 a = MkP1 P2

data P2 = MkP2 ( P1 Maybe)

10

Page 22: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Defaulting

data Tree a = Leaf | Fork (Tree a) (Tree a)

-- Tree :: ?→ ? winner!

data P1 a = MkP1 P2

data P2 = MkP2 ( P1 Maybe)

mutual recursion

10

Page 23: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Defaulting

data Tree a = Leaf | Fork (Tree a) (Tree a)

-- Tree :: ?→ ? winner!

data P1 a = MkP1 P2

data P2 = MkP2 (P1 Maybe)

mutual recursion

10

Page 24: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Defaulting

data Tree a = Leaf | Fork (Tree a) (Tree a)

-- Tree :: ?→ ? winner!

data P1 a = MkP1 P2

data P2 = MkP2 (P1 Maybe)

mutual recursion

-- a :: ?→ ?

-- P1 :: (?→ ?)→ ?

-- P2 :: ?

10

Page 25: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Defaulting

data Tree a = Leaf | Fork (Tree a) (Tree a)

-- Tree :: ?→ ? winner!

data P1 a = MkP1 P2

data P2 = MkP2 (P1 Maybe)

mutual recursion

-- a :: ?→ ?

-- P1 :: (?→ ?)→ ?

-- P2 :: ?

310

Page 26: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Defaulting

data Tree a = Leaf | Fork (Tree a) (Tree a)

-- Tree :: ?→ ? winner!

data P1 a = MkP1 P2

data P2 = MkP2 (P1 Maybe)

mutual recursion

-- a :: ?→ ?

-- P1 :: (?→ ?)→ ?

-- P2 :: ?

3

data P1 a = MkP1 7P2data P2 = MkP2 (P1 Maybe)

10

Page 27: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Defaulting

data Tree a = Leaf | Fork (Tree a) (Tree a)

-- Tree :: ?→ ? winner!

data P1 a = MkP1 P2

data P2 = MkP2 (P1 Maybe)

mutual recursion

-- a :: ?→ ?

-- P1 :: (?→ ?)→ ?

-- P2 :: ?

3

data P1 a = MkP1

data P2 = MkP2 (P1 Maybe)

10

Page 28: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Defaulting

data Tree a = Leaf | Fork (Tree a) (Tree a)

-- Tree :: ?→ ? winner!

data P1 a = MkP1 P2

data P2 = MkP2 (P1 Maybe)

mutual recursion

-- a :: ?→ ?

-- P1 :: (?→ ?)→ ?

-- P2 :: ?

3

data P1 a = MkP1

data P2 = MkP2 (P1 Maybe)

no mutual recursion

10

Page 29: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Defaulting

data Tree a = Leaf | Fork (Tree a) (Tree a)

-- Tree :: ?→ ? winner!

data P1 a = MkP1 P2

data P2 = MkP2 (P1 Maybe)

mutual recursion

-- a :: ?→ ?

-- P1 :: (?→ ?)→ ?

-- P2 :: ?

3

data P1 a = MkP1

no mutual recursion

defaulting a :: ?

data P2 = MkP2 (P1 Maybe)

10

Page 30: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Defaulting

data Tree a = Leaf | Fork (Tree a) (Tree a)

-- Tree :: ?→ ? winner!

data P1 a = MkP1 P2

data P2 = MkP2 (P1 Maybe)

mutual recursion

-- a :: ?→ ?

-- P1 :: (?→ ?)→ ?

-- P2 :: ?

3

data P1 a = MkP1

no mutual recursion

defaulting a :: ?

data P2 = MkP2 (P1 Maybe)

10

Page 31: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Defaulting

data Tree a = Leaf | Fork (Tree a) (Tree a)

-- Tree :: ?→ ? winner!

data P1 a = MkP1 P2

data P2 = MkP2 (P1 Maybe)

mutual recursion

-- a :: ?→ ?

-- P1 :: (?→ ?)→ ?

-- P2 :: ?

3

data P1 a = MkP1

no mutual recursion

defaulting a :: ?

data P2 = MkP2 (P1 Maybe)

10

Page 32: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Defaulting

data Tree a = Leaf | Fork (Tree a) (Tree a)

-- Tree :: ?→ ? winner!

data P1 a = MkP1 P2

data P2 = MkP2 (P1 Maybe)

mutual recursion

-- a :: ?→ ?

-- P1 :: (?→ ?)→ ?

-- P2 :: ?

3

data P1 a = MkP1

-- P1 :: ?→ ?

no mutual recursion

defaulting a :: ?

data P2 = MkP2 (P1 Maybe)

10

Page 33: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Defaulting

data Tree a = Leaf | Fork (Tree a) (Tree a)

-- Tree :: ?→ ? winner!

data P1 a = MkP1 P2

data P2 = MkP2 (P1 Maybe)

mutual recursion

-- a :: ?→ ?

-- P1 :: (?→ ?)→ ?

-- P2 :: ?

3

data P1 a = MkP1

-- P1 :: ?→ ?

no mutual recursion

defaulting a :: ?

data P2 = MkP2 (P1 Maybe)

10

Page 34: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Defaulting

data Tree a = Leaf | Fork (Tree a) (Tree a)

-- Tree :: ?→ ? winner!

data P1 a = MkP1 P2

data P2 = MkP2 (P1 Maybe)

mutual recursion

-- a :: ?→ ?

-- P1 :: (?→ ?)→ ?

-- P2 :: ?

3

data P1 a = MkP1

-- P1 :: ?→ ?

no mutual recursion

defaulting a :: ?

data P2 = MkP2 (P1 Maybe)-- rejected

10

Page 35: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Defaulting

data Tree a = Leaf | Fork (Tree a) (Tree a)

-- Tree :: ?→ ? winner!

data P1 a = MkP1 P2

data P2 = MkP2 (P1 Maybe)

mutual recursion

-- a :: ?→ ?

-- P1 :: (?→ ?)→ ?

-- P2 :: ?

3

data P1 a = MkP1

-- P1 :: ?→ ?

no mutual recursion

defaulting a :: ?

data P2 = MkP2 (P1 Maybe)-- rejected

710

Page 36: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

More about Haskell98

• A declarative specification of the datatype language.

• A sound algorithmic system using context extension approaches (Gundry et al.,

2010; Dunfield and Krishnaswami, 2013).

• Defaulting fits naturally in the approach.

• Consequences and design alternatives of defaulting.

• Restore completeness and principality by endowing the declarative system with

kind parameters (Garcia and Cimini, 2015).

11

Page 37: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Kind Inference for Modern Haskell

Page 38: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

The Kind Language

kind, type σ, K ::= ∀a. σ | ∀a : κ. σ | τmono- τ, κ ::= ? | Int | a | T | τ1 τ2 | τ1 @τ2 |→

• Kind polymorphism

data Tree a = Leaf | Fork (Tree a) (Tree a)

• The type and kind language is unified, dependently typed

data D :: ∀( k :: ?) (a :: k ). Tree a → ?

• Visible kind application, lifted from visible type application (Eisenberg et al., 2016)

12

Page 39: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

The Kind Language

kind, type σ, K ::= ∀a. σ | ∀a : κ. σ | τmono- τ, κ ::= ? | Int | a | T | τ1 τ2 | τ1 @τ2 |→

• Kind polymorphism

data Tree a = Leaf | Fork (Tree a) (Tree a)

• The type and kind language is unified, dependently typed

data D :: ∀( k :: ?) (a :: k ). Tree a → ?

• Visible kind application, lifted from visible type application (Eisenberg et al., 2016)

12

Page 40: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

The Kind Language

kind, type σ, K ::= ∀a. σ | ∀a : κ. σ | τmono- τ, κ ::= ? | Int | a | T | τ1 τ2 | τ1 @τ2 |→

• Kind polymorphism

data Tree a = Leaf | Fork (Tree a) (Tree a)

-- Tree :: ∀k . k → ?

• The type and kind language is unified, dependently typed

data D :: ∀( k :: ?) (a :: k ). Tree a → ?

• Visible kind application, lifted from visible type application (Eisenberg et al., 2016)

12

Page 41: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

The Kind Language

kind, type σ, K ::= ∀a. σ | ∀a : κ. σ | τmono- τ, κ ::= ? | Int | a | T | τ1 τ2 | τ1 @τ2 |→

• Kind polymorphism

data Tree a = Leaf | Fork (Tree a) (Tree a)

-- Tree :: ∀k . k → ?

• The type and kind language is unified, dependently typed

data D :: ∀( k :: ?) (a :: k ). Tree a → ?

• Visible kind application, lifted from visible type application (Eisenberg et al., 2016)

12

Page 42: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

The Kind Language

kind, type σ, K ::= ∀a. σ | ∀a : κ. σ | τmono- τ, κ ::= ? | Int | a | T | τ1 τ2 | τ1 @τ2 |→

• Kind polymorphism

data Tree a = Leaf | Fork (Tree a) (Tree a)

-- Tree :: ∀k . k → ?

• The type and kind language is unified, dependently typed

data D :: ∀( k :: ?) (a :: k ). Tree a → ?

• Visible kind application, lifted from visible type application (Eisenberg et al., 2016)

12

Page 43: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Language Features

kind, type σ, K ::= ∀a. σ | ∀a : κ. σ | τmono- τ, κ ::= ? | Int | a | T | τ1 τ2 | τ1 @τ2 |→

• Open kind signature: a kind signature can contain free variables.

data D :: Tree @ k a → ?

-- D :: ∀(k :: ?) (a :: k). Tree @k a → ?

• Polymorphic recursion.

• Existential quantification for data constructors.

13

Page 44: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Language Features

kind, type σ, K ::= ∀a. σ | ∀a : κ. σ | τmono- τ, κ ::= ? | Int | a | T | τ1 τ2 | τ1 @τ2 |→

• Open kind signature: a kind signature can contain free variables.

data D :: Tree @ k a → ?

-- D :: ∀(k :: ?) (a :: k). Tree @k a → ?

• Polymorphic recursion.

• Existential quantification for data constructors.

13

Page 45: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Language Features

kind, type σ, K ::= ∀a. σ | ∀a : κ. σ | τmono- τ, κ ::= ? | Int | a | T | τ1 τ2 | τ1 @τ2 |→

• Open kind signature: a kind signature can contain free variables.

data D :: Tree @ k a → ?

-- D :: ∀(k :: ?) (a :: k). Tree @k a → ?

• Polymorphic recursion.

• Existential quantification for data constructors.

13

Page 46: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Language Features

kind, type σ, K ::= ∀a. σ | ∀a : κ. σ | τmono- τ, κ ::= ? | Int | a | T | τ1 τ2 | τ1 @τ2 |→

• Open kind signature: a kind signature can contain free variables.

data D :: Tree @ k a → ?

-- D :: ∀(k :: ?) (a :: k). Tree @k a → ?

• Polymorphic recursion.

• Existential quantification for data constructors.

13

Page 47: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Language Features

kind, type σ, K ::= ∀a. σ | ∀a : κ. σ | τmono- τ, κ ::= ? | Int | a | T | τ1 τ2 | τ1 @τ2 |→

• Open kind signature: a kind signature can contain free variables.

data D :: Tree @ k a → ?

-- D :: ∀(k :: ?) (a :: k). Tree @k a → ?

• Polymorphic recursion.

• Existential quantification for data constructors.

13

Page 48: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Language Features

kind, type σ, K ::= ∀a. σ | ∀a : κ. σ | τmono- τ, κ ::= ? | Int | a | T | τ1 τ2 | τ1 @τ2 |→

• Open kind signature: a kind signature can contain free variables.

data D :: Tree @ k a → ?

-- D :: ∀(k :: ?) (a :: k). Tree @k a → ?

• Polymorphic recursion.

• Existential quantification for data constructors.

Challenges!

13

Page 49: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #1

In which order do we put free variables in the ordered type context before kinding?

data Q :: ∀(a :: ( f b )) (c :: k ). f c → ?

-- well-scoped:

f , b , k k̀ ∀(a :: (f b)) (c :: k). f c → ?

7

f , k , b k̀ ∀(a :: (f b)) (c :: k). f c → ?

7

k , b , f k̀ ∀(a :: (f b)) (c :: k). f c → ?

3

· · ·

f :: k → ?

b :: k

k :: ?

R We cannot know the dependency order before kinding.

14

Page 50: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #1

In which order do we put free variables in the ordered type context before kinding?

data Q :: ∀(a :: ( f b )) (c :: k ). f c → ?

-- well-scoped:

f , b , k k̀ ∀(a :: (f b)) (c :: k). f c → ?

7

f , k , b k̀ ∀(a :: (f b)) (c :: k). f c → ?

7

k , b , f k̀ ∀(a :: (f b)) (c :: k). f c → ?

3

· · ·

f :: k → ?

b :: k

k :: ?

R We cannot know the dependency order before kinding.

14

Page 51: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #1

In which order do we put free variables in the ordered type context before kinding?

data Q :: ∀(a :: ( f b )) (c :: k ). f c → ?

-- well-scoped:

f , b , k k̀ ∀(a :: (f b)) (c :: k). f c → ?

7

f , k , b k̀ ∀(a :: (f b)) (c :: k). f c → ?

7

k , b , f k̀ ∀(a :: (f b)) (c :: k). f c → ?

3

· · ·

f :: k → ?

b :: k

k :: ?

R We cannot know the dependency order before kinding.

14

Page 52: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #1

In which order do we put free variables in the ordered type context before kinding?

data Q :: ∀(a :: ( f b )) (c :: k ). f c → ?

-- well-scoped:

f , b , k k̀ ∀(a :: (f b)) (c :: k). f c → ?

7

f , k , b k̀ ∀(a :: (f b)) (c :: k). f c → ?

7

k , b , f k̀ ∀(a :: (f b)) (c :: k). f c → ?

3

· · ·

f :: k → ?

b :: k

k :: ?

R We cannot know the dependency order before kinding.

14

Page 53: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #1

In which order do we put free variables in the ordered type context before kinding?

data Q :: ∀(a :: ( f b )) (c :: k ). f c → ?

-- well-scoped:

f , b , k k̀ ∀(a :: (f b)) (c :: k). f c → ?

7

f , k , b k̀ ∀(a :: (f b)) (c :: k). f c → ?

7

k , b , f k̀ ∀(a :: (f b)) (c :: k). f c → ?

3

· · ·

f :: k → ?

b :: k

k :: ?

R We cannot know the dependency order before kinding.

14

Page 54: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #1

In which order do we put free variables in the ordered type context before kinding?

data Q :: ∀(a :: ( f b )) (c :: k ). f c → ?

-- well-scoped:

f , b , k k̀ ∀(a :: (f b)) (c :: k). f c → ? 7

f , k , b k̀ ∀(a :: (f b)) (c :: k). f c → ? 7

k , b , f k̀ ∀(a :: (f b)) (c :: k). f c → ? 3

· · ·

f :: k → ?

b :: k

k :: ?

R We cannot know the dependency order before kinding.

14

Page 55: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #1

In which order do we put free variables in the ordered type context before kinding?

data Q :: ∀(a :: ( f b )) (c :: k ). f c → ?

-- well-scoped:

f , b , k k̀ ∀(a :: (f b)) (c :: k). f c → ? 7

f , k , b k̀ ∀(a :: (f b)) (c :: k). f c → ? 7

k , b , f k̀ ∀(a :: (f b)) (c :: k). f c → ? 3

· · ·

f :: k → ?

b :: k

k :: ?

R We cannot know the dependency order before kinding.

14

Page 56: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Our Solution

Local scopes, sets of variables that may be reordered.

∆,Θ ::= • | ∆, a : κ | ∆,T : K | ∆, {∆′} | ...

Moving judgment reorders local scopes.

∆1 ++mv ∆2 Θ

15

Page 57: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #2

How to generalize unification variables?

Given

16

Page 58: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #2

How to generalize unification variables? Given

SameKind :: ∀(k :: ?). k → k → ?

16

Page 59: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #2

How to generalize unification variables? Given

SameKind :: ∀(k :: ?). k → k → ?

k 7→ β̂

β̂ :: ?

⇓ generalizes

data Q :: ∀( b :: ?) (a :: b ).

SameKind @b a a

k 7→ (c → ?)

a 7→ β̂ β̂ :: c

⇓ generalizes

data T2 :: ∀( b :: c) (c :: ?) (d :: c → ?).

SameKind @(c → ?) (T1 @c @b ) a

-- (b::c) ill-typed, c out of scope

16

Page 60: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #2

How to generalize unification variables? Given

SameKind :: ∀(k :: ?). k → k → ?

data Q :: ∀a . SameKind a a

k 7→ β̂

β̂ :: ?

⇓ generalizes

data Q :: ∀( b :: ?) (a :: b ).

SameKind @b a a

k 7→ (c → ?)

a 7→ β̂ β̂ :: c

⇓ generalizes

data T2 :: ∀( b :: c) (c :: ?) (d :: c → ?).

SameKind @(c → ?) (T1 @c @b ) a

-- (b::c) ill-typed, c out of scope

16

Page 61: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #2

How to generalize unification variables? Given

SameKind :: ∀(k :: ?). k → k → ?

data Q :: ∀(a :: β̂ ). SameKind a a

k 7→ β̂

β̂ :: ?

⇓ generalizes

data Q :: ∀( b :: ?) (a :: b ).

SameKind @b a a

k 7→ (c → ?)

a 7→ β̂ β̂ :: c

⇓ generalizes

data T2 :: ∀( b :: c) (c :: ?) (d :: c → ?).

SameKind @(c → ?) (T1 @c @b ) a

-- (b::c) ill-typed, c out of scope

16

Page 62: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #2

How to generalize unification variables? Given

SameKind :: ∀( k :: ?). k → k → ?

data Q :: ∀(a :: β̂ ). SameKind a a

k 7→ β̂

β̂ :: ?

⇓ generalizes

data Q :: ∀( b :: ?) (a :: b ).

SameKind @b a a

k 7→ (c → ?)

a 7→ β̂ β̂ :: c

⇓ generalizes

data T2 :: ∀( b :: c) (c :: ?) (d :: c → ?).

SameKind @(c → ?) (T1 @c @b ) a

-- (b::c) ill-typed, c out of scope

16

Page 63: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #2

How to generalize unification variables? Given

SameKind :: ∀( k :: ?). k → k → ?

data Q :: ∀(a :: β̂ ). SameKind a a

k 7→ β̂

β̂ :: ?

⇓ generalizes

data Q :: ∀( b :: ?) (a :: b ).

SameKind @b a a

k 7→ (c → ?)

a 7→ β̂ β̂ :: c

⇓ generalizes

data T2 :: ∀( b :: c) (c :: ?) (d :: c → ?).

SameKind @(c → ?) (T1 @c @b ) a

-- (b::c) ill-typed, c out of scope

16

Page 64: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #2

How to generalize unification variables? Given

SameKind :: ∀(k :: ?). k → k → ?

data Q :: ∀(a :: β̂ ). SameKind a a

k 7→ β̂

β̂ :: ?

⇓ generalizes

data Q :: ∀( b :: ?) (a :: b ).

SameKind @b a a

k 7→ (c → ?)

a 7→ β̂ β̂ :: c

⇓ generalizes

data T2 :: ∀( b :: c) (c :: ?) (d :: c → ?).

SameKind @(c → ?) (T1 @c @b ) a

-- (b::c) ill-typed, c out of scope

16

Page 65: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #2

How to generalize unification variables? Given

SameKind :: ∀(k :: ?). k → k → ?

data Q :: ∀(a :: β̂ ). SameKind a a

k 7→ β̂

β̂ :: ?

⇓ generalizes

data Q :: ∀( b :: ?) (a :: b ).

SameKind @b a a

data T1 :: ∀(f :: ?) (a :: f). f → ?

data T2 :: ∀(c :: ?) (d :: c → ?).

SameKind T1 d → ?

k 7→ (c → ?)

a 7→ β̂ β̂ :: c

⇓ generalizes

data T2 :: ∀( b :: c) (c :: ?) (d :: c → ?).

SameKind @(c → ?) (T1 @c @b ) a

-- (b::c) ill-typed, c out of scope

16

Page 66: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #2

How to generalize unification variables? Given

SameKind :: ∀( k :: ?). k → k → ?

data Q :: ∀(a :: β̂ ). SameKind a a

k 7→ β̂

β̂ :: ?

⇓ generalizes

data Q :: ∀( b :: ?) (a :: b ).

SameKind @b a a

data T1 :: ∀(f :: ?) (a :: f). f → ?

data T2 :: ∀(c :: ?) (d :: c → ?).

SameKind T1 d → ?

k 7→ (c → ?)

a 7→ β̂ β̂ :: c

⇓ generalizes

data T2 :: ∀( b :: c) (c :: ?) (d :: c → ?).

SameKind @(c → ?) (T1 @c @b ) a

-- (b::c) ill-typed, c out of scope

16

Page 67: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #2

How to generalize unification variables? Given

SameKind :: ∀(k :: ?). k → k → ?

data Q :: ∀(a :: β̂ ). SameKind a a

k 7→ β̂

β̂ :: ?

⇓ generalizes

data Q :: ∀( b :: ?) (a :: b ).

SameKind @b a a

data T1 :: ∀( f :: ?) (a :: f). f → ?

data T2 :: ∀(c :: ?) (d :: c → ?).

SameKind T1 d → ?

k 7→ (c → ?) f 7→ c

a 7→ β̂ β̂ :: c

⇓ generalizes

data T2 :: ∀( b :: c) (c :: ?) (d :: c → ?).

SameKind @(c → ?) (T1 @c @b ) a

-- (b::c) ill-typed, c out of scope

16

Page 68: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #2

How to generalize unification variables? Given

SameKind :: ∀(k :: ?). k → k → ?

data Q :: ∀(a :: β̂ ). SameKind a a

k 7→ β̂

β̂ :: ?

⇓ generalizes

data Q :: ∀( b :: ?) (a :: b ).

SameKind @b a a

data T1 :: ∀(f :: ?) ( a :: f). f → ?

data T2 :: ∀(c :: ?) (d :: c → ?).

SameKind T1 d → ?

k 7→ (c → ?) f 7→ c

a 7→ β̂ β̂ :: c

⇓ generalizes

data T2 :: ∀( b :: c) (c :: ?) (d :: c → ?).

SameKind @(c → ?) (T1 @c @b ) a

-- (b::c) ill-typed, c out of scope

16

Page 69: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #2

How to generalize unification variables? Given

SameKind :: ∀(k :: ?). k → k → ?

data Q :: ∀(a :: β̂ ). SameKind a a

k 7→ β̂

β̂ :: ?

⇓ generalizes

data Q :: ∀( b :: ?) (a :: b ).

SameKind @b a a

data T1 :: ∀(f :: ?) (a :: f). f → ?

data T2 :: ∀(c :: ?) (d :: c → ?).

SameKind T1 d → ?

k 7→ (c → ?) f 7→ c

a 7→ β̂ β̂ :: c

⇓ generalizes

data T2 :: ∀( b :: c) (c :: ?) (d :: c → ?).

SameKind @(c → ?) (T1 @c @b ) a

-- (b::c) ill-typed, c out of scope

16

Page 70: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #2

How to generalize unification variables? Given

SameKind :: ∀(k :: ?). k → k → ?

data Q :: ∀(a :: β̂ ). SameKind a a

k 7→ β̂

β̂ :: ?

⇓ generalizes

data Q :: ∀( b :: ?) (a :: b ).

SameKind @b a a

data T1 :: ∀(f :: ?) (a :: f). f → ?

data T2 :: ∀(c :: ?) (d :: c → ?).

SameKind T1 d → ?

k 7→ (c → ?) f 7→ c

a 7→ β̂ β̂ :: c

⇓ generalizes

data T2 :: ∀( b :: c) (c :: ?) (d :: c → ?).

SameKind @(c → ?) (T1 @c @b ) a

-- (b::c) ill-typed, c out of scope

16

Page 71: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #2

How to generalize unification variables? Given

SameKind :: ∀(k :: ?). k → k → ?

data Q :: ∀(a :: β̂ ). SameKind a a

k 7→ β̂

β̂ :: ?

⇓ generalizes

data Q :: ∀( b :: ?) (a :: b ).

SameKind @b a a

data T1 :: ∀(f :: ?) (a :: f). f → ?

data T2 :: ∀(c :: ?) (d :: c → ?).

SameKind T1 d → ?

k 7→ (c → ?) f 7→ c

a 7→ β̂ β̂ :: c

⇓ generalizes

data T2 :: ∀( b :: c) (c :: ?) (d :: c → ?).

SameKind @(c → ?) (T1 @c @b ) a

-- (b::c) ill-typed, c out of scope

R How to handle unsolved unification variables with

dependency on local variables?

16

Page 72: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Our Solution

Reject it!

∀(a :: k). σ after kinding σ, no unsolved unification variables can depend on a .

data T1 :: ∀(f :: ?) (a :: f). f → ?

data T2 :: ∀(c :: ?) (d :: c → ?). SameKind T1 d → ?

k 7→ (c → ?)

f 7→ c

a 7→ β̂

β̂ :: c

7

An unification variable, after kinding σ, is either

• solved; or

• since it doesn’t depend on a , generalized outside a .

17

Page 73: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Our Solution

Reject it! via Quantification Check.

∀(a :: k). σ after kinding σ, no unsolved unification variables can depend on a .

data T1 :: ∀(f :: ?) (a :: f). f → ?

data T2 :: ∀(c :: ?) (d :: c → ?). SameKind T1 d → ?

k 7→ (c → ?)

f 7→ c

a 7→ β̂

β̂ :: c

7

An unification variable, after kinding σ, is either

• solved; or

• since it doesn’t depend on a , generalized outside a .

17

Page 74: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Our Solution

Reject it! via Quantification Check.

∀(a :: k). σ after kinding σ, no unsolved unification variables can depend on a .

data T1 :: ∀(f :: ?) (a :: f). f → ?

data T2 :: ∀(c :: ?) (d :: c → ?). SameKind T1 d → ?

k 7→ (c → ?)

f 7→ c

a 7→ β̂

β̂ :: c

7

An unification variable, after kinding σ, is either

• solved; or

• since it doesn’t depend on a , generalized outside a .

17

Page 75: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Our Solution

Reject it! via Quantification Check.

∀(a :: k). σ after kinding σ, no unsolved unification variables can depend on a .

data T1 :: ∀(f :: ?) (a :: f). f → ?

data T2 :: ∀(c :: ?) (d :: c → ?). SameKind T1 d → ?

k 7→ (c → ?)

f 7→ c

a 7→ β̂

β̂ :: c

7

An unification variable, after kinding σ, is either

• solved; or

• since it doesn’t depend on a , generalized outside a .

17

Page 76: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Our Solution

Reject it! via Quantification Check.

∀(a :: k). σ after kinding σ, no unsolved unification variables can depend on a .

data T1 :: ∀(f :: ?) (a :: f). f → ?

data T2 :: ∀(c :: ?) (d :: c → ?). SameKind T1 d → ?

k 7→ (c → ?)

f 7→ c

a 7→ β̂

β̂ :: c7

An unification variable, after kinding σ, is either

• solved; or

• since it doesn’t depend on a , generalized outside a .

17

Page 77: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Our Solution

Reject it! via Quantification Check.

∀(a :: k). σ after kinding σ, no unsolved unification variables can depend on a .

data T1 :: ∀(f :: ?) (a :: f). f → ?

data T2 :: ∀(c :: ?) (d :: c → ?). SameKind T1 d → ?

k 7→ (c → ?)

f 7→ c

a 7→ β̂

β̂ :: c7

An unification variable, after kinding σ, is either

• solved; or

• since it doesn’t depend on a , generalized outside a .

17

Page 78: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #3

Unification for a dependently typed language.

• First-order unification, for the algorithm to be guessing-free and

predicative (Vytiniotis et al., 2011).

• Coen (2004) considers first-order unification, but only soundness is proved.

• A higher-order unification algorithm for Coq (Ziliani and Sozeau, 2015) favors

syntactic equality by trying first-order unification. To our knowledge, they lack a

correctness proof.

18

Page 79: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #3

Unification for a dependently typed language.

• First-order unification, for the algorithm to be guessing-free and

predicative (Vytiniotis et al., 2011).

• Coen (2004) considers first-order unification, but only soundness is proved.

• A higher-order unification algorithm for Coq (Ziliani and Sozeau, 2015) favors

syntactic equality by trying first-order unification. To our knowledge, they lack a

correctness proof.

18

Page 80: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #3

Unification for a dependently typed language.

• First-order unification, for the algorithm to be guessing-free and

predicative (Vytiniotis et al., 2011).

• Coen (2004) considers first-order unification, but only soundness is proved.

• A higher-order unification algorithm for Coq (Ziliani and Sozeau, 2015) favors

syntactic equality by trying first-order unification. To our knowledge, they lack a

correctness proof.

18

Page 81: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Challenge #3

Unification for a dependently typed language.

• First-order unification, for the algorithm to be guessing-free and

predicative (Vytiniotis et al., 2011).

• Coen (2004) considers first-order unification, but only soundness is proved.

• A higher-order unification algorithm for Coq (Ziliani and Sozeau, 2015) favors

syntactic equality by trying first-order unification. To our knowledge, they lack a

correctness proof.

18

Page 82: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Unification

α̂ : κ1, β̂ : κ2 u α̂ ≈ β̂

¬ Try solving β̂ = α̂

­ May not be well-formed: κ1 may not match κ2.

® Try unifying κ1 ≈ κ2 first.

¯ Unifying a type recurs to its kind, how does it terminate?

° ? : ? axiom.

19

Page 83: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Unification

α̂ : κ1, β̂ : κ2 u α̂ ≈ β̂

¬ Try solving β̂ = α̂

­ May not be well-formed: κ1 may not match κ2.

® Try unifying κ1 ≈ κ2 first.

¯ Unifying a type recurs to its kind, how does it terminate?

° ? : ? axiom.

19

Page 84: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Unification

α̂ : κ1, β̂ : κ2 u α̂ ≈ β̂

¬ Try solving β̂ = α̂

­ May not be well-formed: κ1 may not match κ2.

® Try unifying κ1 ≈ κ2 first.

¯ Unifying a type recurs to its kind, how does it terminate?

° ? : ? axiom.

19

Page 85: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Unification

α̂ : κ1, β̂ : κ2 u α̂ ≈ β̂

¬ Try solving β̂ = α̂

­ May not be well-formed: κ1 may not match κ2.

® Try unifying κ1 ≈ κ2 first.

¯ Unifying a type recurs to its kind, how does it terminate?

° ? : ? axiom.

19

Page 86: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Unification

α̂ : κ1, β̂ : κ2 u α̂ ≈ β̂

¬ Try solving β̂ = α̂

­ May not be well-formed: κ1 may not match κ2.

® Try unifying κ1 ≈ κ2 first.

¯ Unifying a type recurs to its kind, how does it terminate?

° ? : ? axiom.

19

Page 87: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Unification

α̂ : κ1, β̂ : κ2 u α̂ ≈ β̂

¬ Try solving β̂ = α̂

­ May not be well-formed: κ1 may not match κ2.

® Try unifying κ1 ≈ κ2 first.

¯ Unifying a type recurs to its kind, how does it terminate?

° ? : ? axiom.

19

Page 88: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Our Solution

Dependency graph visualizes the dependency information in an ordered context.

∆ = α̂ : ?, α̂1 : ?, α̂2 : ? = α̂1, α̂3 : ? → α̂2

?→ Int

α̂1 α̂

α̂3

3 We proved unification terminates.

20

Page 89: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Our Solution

Dependency graph visualizes the dependency information in an ordered context.

∆ = α̂ : ?, α̂1 : ?, α̂2 : ? = α̂1, α̂3 : ? → α̂2

?→ Int

α̂1 α̂

α̂3

3 We proved unification terminates.

20

Page 90: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

More about modern Haskell

• A declarative specification of the datatype language.

• A sound algorithmic system.

Principality: our algorithm does not infer every kind acceptable by the declarative system, but the

kinds it does infer are always the best (principal) one (Vytiniotis et al., 2011).

• Discussion of language extensions.

Higher-rank polymorphism, GADTs, type families, visible dependent quantification, datatype

promotion, partial type signature.

21

Page 91: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Summary

Page 92: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Conclusion

We have presented the first known, detailed account of kind

inference for datatypes, both for Haskell98 and the Haskell

of today.

22

Page 93: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Haskell and GHC

¬ Goal: produce a sound and complete pair of specification and

algorithm.

23

Page 94: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Haskell and GHC

­ Detailed comparison with GHC included in our technical

supplement (Xie et al., 2019).

23

Page 95: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Haskell and GHC

® Identified ways GHC can improve.

23

Page 96: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Haskell and GHC

® Identified ways GHC can improve.

unexpected semantics =⇒ quantification check

23

Page 97: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Beyond Haskell

· · ·

Inform the design of principled inference algorithms

for languages beyond Haskell.

24

Page 98: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

Kind Inference for Datatypes

Ningning Xie 1 Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira 1

POPL 2020

1The University of Hong Kong

2Bryn Mawr College

3Tweag I/O

25

Page 99: Kind Inference for Datatypes - GitHub Pages · Kind Inference for Datatypes Ningning Xie 1Richard A. Eisenberg 2,3 Bruno C. d. S. Oliveira POPL 2020 1The University of Hong Kong 2Bryn

References

Claudio Sacerdoti Coen. 2004. Mathematical knowledge management and interactive theorem proving. Ph.D. Dissertation. University of Bologna,

2004. Technical Report UBLCS 2004-5.

Joshua Dunfield and Neelakantan R. Krishnaswami. 2013. Complete and easy bidirectional typechecking for higher-rank polymorphism. In ICFP’13.

14.

Richard A Eisenberg, Stephanie Weirich, and Hamidhasan G Ahmed. 2016. Visible type application. In ESOP’16.

Ronald Garcia and Matteo Cimini. 2015. Principal type schemes for gradual programs. In POPL’15. 13.

Adam Gundry, Conor McBride, and James McKinna. 2010. Type inference in context. In MSFP.

Dimitrios Vytiniotis, Simon Peyton Jones, Tom Schrijvers, and Martin Sulzmann. 2011. OutsideIn (X) Modular type inference with local assumptions.

JFP 21, 4-5 (2011), 333–412.

Ningning Xie, Richard A. Eisenberg, and Bruno C. d. S. Oliveira. 2019. Kind Inference for Datatypes: Technical Supplement. (Nov. 2019).

arXiv:1911.06153.

Beta Ziliani and Matthieu Sozeau. 2015. A Unification Algorithm for Coq Featuring Universe Polymorphism and Overloading. In ICFP’15.

26