18
PADL 04 1 Implementing Cut Elimination: A case study of simulating dependent types in Haskell Chiyan Chen Dengping Zhu Hongwei Xi Boston University

PADL 041 Implementing Cut Elimination: A case study of simulating dependent types in Haskell Chiyan Chen Dengping Zhu Hongwei Xi Boston University

Embed Size (px)

Citation preview

Page 1: PADL 041 Implementing Cut Elimination: A case study of simulating dependent types in Haskell Chiyan Chen Dengping Zhu Hongwei Xi Boston University

PADL 04 1

Implementing Cut Elimination: A case study of simulating dependent types in Haskell

Chiyan Chen Dengping Zhu Hongwei Xi

Boston University

Page 2: PADL 041 Implementing Cut Elimination: A case study of simulating dependent types in Haskell Chiyan Chen Dengping Zhu Hongwei Xi Boston University

PADL 04 2

Types Very helpful in programming practice:

Document programmers’ intentions. Statically assure program invariants and

catch program errors. Two directions of evolution:

Assign more refined types to programs: from simple types to dependent types.

Type more programs: from simple types to polymorphic types.

Page 3: PADL 041 Implementing Cut Elimination: A case study of simulating dependent types in Haskell Chiyan Chen Dengping Zhu Hongwei Xi Boston University

PADL 04 3

Polymorphic Types vs. Dependent Types Polymorphic types are more widely

used than dependent types in practice: Facilitated by Hindley-Milner style type

inference. Languages with parametric

polymorphism: ML, Haskell Languages with dependent types:

Dependent ML (DML), Cayenne.

Page 4: PADL 041 Implementing Cut Elimination: A case study of simulating dependent types in Haskell Chiyan Chen Dengping Zhu Hongwei Xi Boston University

PADL 04 4

Haskell A versatile language with a variety of

advanced type features: Polymorphic recursion. Higher ranked polymorphic types. Existential types. Type classes. … …

Pure type inference is no longer supported.

Page 5: PADL 041 Implementing Cut Elimination: A case study of simulating dependent types in Haskell Chiyan Chen Dengping Zhu Hongwei Xi Boston University

PADL 04 5

Simulating Dependent Types in Haskell Types for type index expressions. Explicit equality on types for implicit

equality on index expressions. Existential datatypes for dependent

datatypes. Polymorphic recursion for

polymorphism over index expressions.

Page 6: PADL 041 Implementing Cut Elimination: A case study of simulating dependent types in Haskell Chiyan Chen Dengping Zhu Hongwei Xi Boston University

PADL 04 6

What Is This Paper About? A large variety of “cute” examples

exist in the literature that make use of similar techniques to simulate dependent types

However, we have strong doubts about the viability of this practice, and the paper uses an interesting example to provide a critique.

Page 7: PADL 041 Implementing Cut Elimination: A case study of simulating dependent types in Haskell Chiyan Chen Dengping Zhu Hongwei Xi Boston University

PADL 04 7

Define Type Equality in Haskell

data EQ a b = EQcon (a -> b) (b -> a)

fstEQ :: EQ (a1, a2) (b1, b2) -> EQ a1 b1fstEQ (EQcon to from) = let bot = bot in EQcon (\x -> fst (to (x, bot))) (\x -> fst (from (x, bot))) sndEQ :: EQ (a1, a2) (b1, b2) -> EQ a2 b2

idEQ :: EQ a a symEQ :: EQ a b -> EQ b atransEQ :: EQ a b -> EQ b c -> EQ a cpairEQ :: EQ a1 b1 -> EQ a2 b2 -> EQ (a1, a2) (b1, b2)

reflexivity

symmetrytransitivi

ty

constructor

introduction

constructor

elimination

Page 8: PADL 041 Implementing Cut Elimination: A case study of simulating dependent types in Haskell Chiyan Chen Dengping Zhu Hongwei Xi Boston University

PADL 04 8

Constructor Elimination Is Not Always Implementable

data T a b = T a

fstTEQ :: EQ (T a1 a2) (T b1 b2) -> EQ a1 b1sndTEQ :: EQ (T a1 a2) (T b1 b2) -> EQ a2 b2

Can be implemented

Cannot be implemented

Page 9: PADL 041 Implementing Cut Elimination: A case study of simulating dependent types in Haskell Chiyan Chen Dengping Zhu Hongwei Xi Boston University

PADL 04 9

Constructor Elimination Is Not Always Implementable

data T a b = T (a -> b)

fstTEQ :: EQ (T a1 a2) (T b1 b2) -> EQ a1 b1sndTEQ :: EQ (T a1 a2) (T b1 b2) -> EQ a2 b2

Cannot be implemented

Can be implemented

Page 10: PADL 041 Implementing Cut Elimination: A case study of simulating dependent types in Haskell Chiyan Chen Dengping Zhu Hongwei Xi Boston University

PADL 04 10

Use Type Equality

toEQ :: EQ a b a -> b

toEQ (EQcon to from) = to

fromEQ :: EQ a b -> b -> a

fromEQ (EQcon to from) = from

Page 11: PADL 041 Implementing Cut Elimination: A case study of simulating dependent types in Haskell Chiyan Chen Dengping Zhu Hongwei Xi Boston University

PADL 04 11

Intuitionistic Propositional Logic (IPL)

Page 12: PADL 041 Implementing Cut Elimination: A case study of simulating dependent types in Haskell Chiyan Chen Dengping Zhu Hongwei Xi Boston University

PADL 04 12

Sequent Calculus for IPL

Page 13: PADL 041 Implementing Cut Elimination: A case study of simulating dependent types in Haskell Chiyan Chen Dengping Zhu Hongwei Xi Boston University

PADL 04 13

Sequent Calculus for IPL

Page 14: PADL 041 Implementing Cut Elimination: A case study of simulating dependent types in Haskell Chiyan Chen Dengping Zhu Hongwei Xi Boston University

PADL 04 14

Cut Elimination

Page 15: PADL 041 Implementing Cut Elimination: A case study of simulating dependent types in Haskell Chiyan Chen Dengping Zhu Hongwei Xi Boston University

PADL 04 15

Type Equality on Constructors

Page 16: PADL 041 Implementing Cut Elimination: A case study of simulating dependent types in Haskell Chiyan Chen Dengping Zhu Hongwei Xi Boston University

PADL 04 16

Explicit Proofs for Type Equalities

to (INone pf) =

INone (transEQ (transEQ (symEQ pf2) pf) (pairEQ idEQ pf1))

EQ g1 g2

IN a1 g1

EQ a1 a2

EQ g1 (g’, a1)

EQ g2 (g’, a1)

EQ g2 g1

EQ (g’, a1) (g’, a2)

EQ g2 (g’, a2)IN a2 g2

Page 17: PADL 041 Implementing Cut Elimination: A case study of simulating dependent types in Haskell Chiyan Chen Dengping Zhu Hongwei Xi Boston University

PADL 04 17

Type Equality on Constructors

Page 18: PADL 041 Implementing Cut Elimination: A case study of simulating dependent types in Haskell Chiyan Chen Dengping Zhu Hongwei Xi Boston University

PADL 04 18

Conclusion Simulate a restricted form of dependent types

Type equality for constructor elimination is not always implementable.

Practicality Cannot simulate sorts in dependent type systems. It is expected that only identity function can inhabit a

type of the form EQ a b (although this cannot be guaranteed by the type system of Haskell). Why bother to define them?

Constructing explicit proofs for type equalities in programs is exceedingly tedious.

Small changes to a program may result in the need for global reconstruction of proofs for type equalities.

EQ Int Bool

type EQ a b = f. f a -> f b (Baars and Swierstra 02)