shared subtypes - seoul national universityrosaec.snu.ac.kr/meet/file/20081224.pdf ·...

76
1 Shared Subtypes Subtyping Recursive Parameterized Algebraic Data Types Ki Yung Ahn                   Tim Sheard    [email protected]               [email protected] Department of Computer Science Maseeh College of Engineering & Computer Science ACM SIGPLAN 2008 Haskell Symposium

Upload: others

Post on 11-Feb-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

1

Shared Subtypes

Subtyping Recursive Parameterized Algebraic Data Types

Ki Yung Ahn                   Tim Sheard    [email protected]               [email protected]

Department of Computer ScienceMaseeh College of Engineering & Computer Science

ACM SIGPLAN 2008 Haskell Symposium

Page 2: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

2

BackgroundSubtype Polymorphism in OOPL

(Object­Oriented Programming Language)

In Java,  String readLine(IntputStream s)

Parametric Polymorphism in FPL

In Haskell,

Page 3: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

3

 Subtype PolymorphismSubtype Polymorphism in OOPL

In Java,  String readLine(IntputStream s)

Can call this function any subtype of InputStream

Page 4: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

4

Parametric PolymorphismParametric Polymorphism in FPL

In Haskell,

We can apply this function on ANY list

Page 5: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

5

Some HistoryLack of Parametric Polymorphism in OOPLs

was quite a pain.  So, most major OOPLsfinally decided to extend their language

Templates in C++,  Generics in Java / C#

FPLs, especially Haskell, lived happily withoutSubtypes because of Type Classes (somewhatlike abstract interfaces, but more flexible)

Page 6: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

6

Shared SubtypesIn abbr.  SSubtypes = Shared Subtypes ● Subtyping by sharing representations

– Newtypes and now SSubtypes – While OO subtyping by consistent interface

● We can use SSubtypes when we want to– Restrict number of data constructors– Ensure static properties using type indexes– reuse library without efficiency loss

● Relatively small changes to type system

Page 7: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

7

Why Subtype in FPL?Well, not the good old subtyping in OOPLs

Haskell can mimic dynamic subtyping(sharing interface) using type classes

See Oleg & Ralf's OOHaskell library

But a different one namely Shared Subtypes

static subtyping (sharing the structure)can be quite useful in FPLs too!

Page 8: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

8

Example

Page 9: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

9

Example

Page 10: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

10

Example

Page 11: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

11

Example

Page 12: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

12

Example

(x3) Code Duplication !!!

Page 13: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

13

What's the Problem?Both  Even (even natural numbers)

and Odd (odd natural numbers)are subsets of Nat (all natural numbers) 

But, Haskell and many other FPLsdo not have language feature that canexpress this idea of subtyping directly

Page 14: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

14

Idea (Shared Subtypes)

Page 15: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

15

Is this really important?Even and Odd unary numbers

may not sound very interesting to you

But, this same problem can happen for more complex and realistic data structures such as lists and trees, as we shall see soon

Page 16: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

16

Is this really important?

Library

DataSystem

Page 17: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

17

Is this really important?

Library

DataSystem

Safe Module

Page 18: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

18

Is this really important?

Library

DataSystem

Safe Module

Safe Data

Page 19: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

19

Is this really important?

Library

DataSystem

Safe Module

Safe Data

Libraryduplication

Page 20: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

20

Is this really important?

Library

DataSystem

Safe Module

Safe Data

Libraryduplication

SafeData

Dataconversionoverhead

Page 21: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

21

 Yes, it is really important!

Library

DataSystem

Safe Module

SafeData

Data

If we had Shared Subtypes ...

Safe Data

Libraryreusenecessary check

at no cost

Page 22: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

22

Shared SubtypesIn abbr.  SSubtypes = Shared Subtypes ● Subtyping by sharing representations

– Newtypes and now SSubtypes – While OO subtyping by consistent interface

● We can use SSubtypes when we want to– Restrict number of data constructors– Ensure static properties using type indexes– reuse library without efficiency loss

● Relatively small changes to type system

Page 23: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

23

What do we do when two typesshare a common representation

1+2, 2-3, ...

4*5, 5/6 ...

1.0 in, 2.0 in, ...Inch

Exp

Term

common functions

, , ==, ...

common functions

eval, size, hight, ...

In Haskell: newtype Inch = Inch Double

Not in Haskell: (until now)

                     data Exp               = ...                     data Term < Exp  = ...

Subs

etIs

omor

phic

Page 24: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

24

Motivation

T ≃ U

T  U

declare T asnewtype of U

share T and U’srepresentation

share T and U’srepresentation ??

Page 25: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

25

Motivation

T ≃ U

T  U

declare T asnewtype of U

share T and U’srepresentation

declare T assharedsubtype of U

share T and U’srepresentation

Page 26: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

26

Shared SubtypesIn abbr.  SSubtypes = Shared Subtypes ● Subtyping by sharing representations

– Newtypes and now SSubtypes – While OO subtyping by consistent interface

● We can use SSubtypes when we want to– Restrict number of data constructors– Ensure static properties using type indexes– reuse library without efficiency loss

● Relatively small changes to type system

Page 27: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

27

Newtypes

Page 28: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

28

Newtypes

Code Reuse without

conversion overhead

Page 29: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

29

Newtype Summary

Page 30: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

30

Subtyping by Refinement  

● We can Partition a type (e.g. lists) into subsets (e.g. empty lists and filled lists)

● Each subset is a subtype● Possible with GADTs and type indexes

 [Int] 

[] :: [Int][1]    :: [Int][1,2] :: [Int]...

Page 31: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

31

Subtyping by Refinement  

● We can Partition a type (e.g. lists) into subsets (e.g. empty lists and filled lists)

● Each subset is a subtype● Possible with GADTs and type indexes

  List Int e  

Nil :: List Int EmptyCons 1 Nil            :: List Int FilledCons 1 (Cons 2 Nil) :: List Int Filled...

Page 32: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

32

GADT = Generalized ADT 

We can express any ADT as GADT

Page 33: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

33

The GADT (List a e) 

With GADTs, we can define richer types

by using different type indexesin the result types of data constructors

Page 34: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

34

The GADT (List a e) 

With GADTs, we can define richer types

by using different type indexesin the result types of data constructors

Page 35: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

35

Values of (List a e) 

Page 36: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

36

Relating  (List a e)   and       

Page 37: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

37

Relating  (List a e)   and       

Page 38: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

38

Subtyping GADT Summary

Page 39: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

39

Subtyping GADT Summary

Subtype rule

Sharing rules

Page 40: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

40

Comparison

Newtypes

Nt   ,  Nv    are bijections

Language Feature !!!

Subtyping (G)ADTs

St   ,  Sv   are injectionsJust a CONCEPT (until now!)

Page 41: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

41

Options in Haskell 

1st option(Code Reuse)

Conversion function that satisfies St    and Sv

Problem:  O(n) conversion overhead

2nd option (Efficiency)

Define library functions for GADTs all again

Problem:  code duplication

Page 42: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

42

Dilemmalose efficiency 

because of conversion overhead

lose code reusewriting same 

functionsfor each type

CodeReuse Efficiency

Page 43: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

43

Features of Shared Subtypes

We must specify both

subtype rule (types)

sharing rules (constructors)

Check consistency 

not all SSubtype declaration make sense 

We share representation (like newtypes)

We have implicit coercion  (newtypes are explicit)

SSubtypes form hierarchy (as in OO)

Efficient code sharing and library reuse

Page 44: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

44

SSubtype Syntax for GADT

Page 45: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

45

SSubtype Syntax for GADT

Page 46: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

46

SSubtype Syntax for GADTSubtype rule

Sharing rules

Page 47: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

47

Code Reuse for SSubtype  We can reuse library function on lists

Values of [a]

Values of List a e

Page 48: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

48

Code Reuse for SSubtype Code Reuse

Subtype rule

Sharing rules

Page 49: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

49

Consistency of SSubtypes Not all SSubtype declaration make sense

Example: sharing between different arities

What are the criteria for consistency?

And, how shall we check them?

Page 50: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

50

Consistency of SSubtypes Not all SSubtype declaration make sense

Example: sharing between different arities

What are the criteria for consistency?

And, how shall we check them?

Page 51: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

51

Consistent SSubtype decl's

No cycles in SSubtype hierarchy

Cycle means equivalence (T≤U T≥U T=U)

Newtype is the feature for equivalent types

No two subtype data constructors can share the same supertype constructor

Sharing rules are consistent with the subtype rule (see next slide)

No higher order types (no function args)

additional restriction at present (future work)

Page 52: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

52

Sharing rules are consistent  with Subtype rule   

Related data constructors must have same arity

Argument types and result types must be in subtype relation (+reflexivity for args)

We don’t worry about contravariance at present because we don’t have higher order types yet

Page 53: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

53

Related data constructors must have same arity

Argument types and result types must be in subtype relation (+reflexivity for args)

Sharing rules are consistent  with Subtype rule   

Page 54: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

54

Consistent SSubtype decl' Related data constructors must have same arity

Argument types and result types must be in subtype relation (+reflexivity for args)

Page 55: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

55

SSubtype Syntax for ADT  

Since Term is a SSubtype of Exp, we can reuse functions defined on Exp for Term.

Page 56: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

56

SSubtype Syntax for ADT  

If we have    eval :: Exp Int    theneval (Const 2 `Times` Const 3)       6

Page 57: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

57

SSubtype Syntax for ADT

If we have    eval :: Exp Int    theneval (Const 2 `Times` Const 3)       6                     :: Term

Page 58: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

58

Finite Subsets of Integers 

Foreign Function Interface can beboth safe and efficient using SSubtypes

Related Feature:  F# enum types are just for this purpose to interact with .NET 

Page 59: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

59

Example from FPH (ICFP'08)

Syntax of Types in the paperby Dimitrios Vytiniotis, Stephanie Weirich, and Simon Peyton Jones

Page 60: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

60

Example from FPH (ICFP'08)

Syntax of Types in the paperby Dimitrios Vytiniotis, Stephanie Weirich, and Simon Peyton Jones

Rank­N Type Rank­NType

Page 61: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

61

Example from FPH (ICFP'08)

Syntax of Types in the paperby Dimitrios Vytiniotis, Stephanie Weirich, and Simon Peyton Jones

Rank­N Type

Starting with forall Not starting with 

forall

Not containing any foralls

Page 62: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

62

Example from FPH (ICFP'08)

Rank­NType

Code s

nipp

et fro

m their

refer

ence

 imple

mentat

ion

(with

 sligh

t sim

plific

ation

)

Page 63: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

63

SSubtype Hierarchy for FPH

Rank­NType

Page 64: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

64

LimitationsWe may lose some static properties when we 

reuse the functions on the supertype

   map :: (a  b)  [a]  [a]map id (Cons 0 Nil) :: [Int] ­­ not  List a Filledmap id Nil :: [Int] ­­ not  List a Empty

In order to preserve such static properties, we have to write such functions again

   mapList :: (a  b)  List a e  List b emapList f Nil = NilmapList f (Cons x xs) = Cons x (mapList f xs) 

Page 65: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

65

Related WorkLanguage Features

Datatype Subtyping in O’Haskell (Nordlander)

Refinement Types (Freeman & Pfenning)

Theory

Subtyping Recursive Types (Amadio & Cardelli)

Implicit Calculus of Constructions (Miquel)

Page 66: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

66

Related WorkLanguage Features

Datatype Subtyping in O’Haskell (Nordlander)

Similar syntax and idea with SSubtypes

Extends existing ADTs  (c.f. SSubtype restricts)

Shares constructor names between related types

Does not cover recursive types or GADTs 

Page 67: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

67

Related WorkLanguage Features

Refinement Types (Freeman & Pfenning)

Based on intersection types

More powerful than subtyping (e.g. map, ++)

Types can get bulky

Separate compilation is questionable

Polymorphic variants are related to this too

Page 68: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

68

Related WorkTheory

Subtyping Recursive Types (Amadio & Cardelli)

Build types from ground types using  , ,

Recursive type equation using    notation

Our work conforms to the rules in their work

Page 69: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

69

Related WorkTheory

Implicit Calculus of Constructions (Miquel)

Defines subtype relation as a Macro

Data constructors are encoded as terms, and subtype relation means that these terms coincide 

SSubtype restores this lost relation in (G)ADTs

Page 70: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

70

Future Work

(G)ADTs containing higher order values

relax from Contravariance Free types           to Variance Consistent types

Complete proof that covers all uses

Harmonize SSubtypes with Type Classes

Possibility of adopting O'Haskell features

Non­variable parameters in subtype rules

Implementation

Page 71: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

71

ConclusionSSubtype is subtyping by sharing representations

c.f. Usual subtyping (as in OO) is by sharing interface of different representation

We can use SSubtypes when we want to

Restrict number of data constructors

Ensure static property using type indexes

reuse library without efficiency loss

Separate compilation (tradeoff for the limitation)

GADTs makes this idea more powerful

Page 72: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

72

Thanks toEveryone in the Acknowledgment section in the 

paper.

Also, those who gave useful comments on the practice talk (Andrew P. Black, 

           Emerson Murphy Hill, Brian Huffman, Mark P. Jones).

And, YOU for listening.

Questions?

Page 73: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

73

Name Sharing

Page 74: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

74

SSubtype Hierarchy

Page 75: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

75

SSubtype Hierarchy

Page 76: Shared Subtypes - Seoul National Universityrosaec.snu.ac.kr/meet/file/20081224.pdf · 2018-04-12 · 51 Consistent SSubtype decl's No cycles in SSubtype hierarchy Cycle means equivalence

76

Type Inhabitation (§6.2)