Row polymorphism 1/ 25 Record operations 2. Extend a record with a - - PowerPoint PPT Presentation

row polymorphism
SMART_READER_LITE
LIVE PREVIEW

Row polymorphism 1/ 25 Record operations 2. Extend a record with a - - PowerPoint PPT Presentation

Row polymorphism 1/ 25 Record operations 2. Extend a record with a field ( extend ) 3. Access the contents of a field ( access ) 2/ 25 1. An empty record ( empty ) Presence variables : : . : . : . :


slide-1
SLIDE 1

Row polymorphism

1/ 25

slide-2
SLIDE 2

Record operations

  • 1. An empty record (empty)
  • 2. Extend a record with a field (extend)
  • 3. Access the contents of a field (access)

2/ 25

slide-3
SLIDE 3

Presence variables

extend : ∀𝛽: ∗ . ∀𝛾 : ∗ . ∀𝛿 : ∗ . ∀𝜀 : ∗ . ∀𝜒: ∗ ⇒ ∗ . 𝜀 → Record 𝛽 𝛾 𝛿 → Record (𝜒 𝜀 ) 𝛾 𝛿 where 𝜒 is a type constructor variable that can be instantiated with:

Present (𝜇𝛽:∗. Absent)

3/ 25

slide-4
SLIDE 4

Ill-formed records

Polymorphic record types allow some ill-formed type expressions:

Record Int (Present String) (Present String) List (Present Int)

These are prevented using the kind system by creating a new kind presence such that: Absent : presence Present : ∗ ⇒ presence Record : presence ⇒ presence ⇒ presence ⇒ ∗

4/ 25

slide-5
SLIDE 5

Infinite records

What if we had infinite record types: { . . . ; foo : bar ; . . . }

5/ 25

slide-6
SLIDE 6

Infinite records

empty : { . . . ; l : Absent ; . . . } extend : ∀ 𝛽: ∗ . ∀ 𝛾 : presence . . . . . ∀ 𝛿 : presence . . . . ∀ 𝜒: ∗ ⇒ presence . 𝛽 → { . . . ; m : 𝛾 ; . . . ; l : 𝛿 ; . . . } → { . . . ; m : 𝜒 𝛽 ; . . . ; l : 𝛿 ; . . . } access : ∀ 𝛽: ∗ . . . . . ∀ 𝛾 : presence . . . . { . . . ; m : Present 𝛽 ; . . . ; l : 𝛾 ; . . . } → 𝛽

6/ 25

slide-7
SLIDE 7

Infinite records

Each record type appearing above can be divided into two parts:

  • 1. A finite part
  • 2. A co-finite part where either every type parameter is a free

variable or every type parameter is Absent.

7/ 25

slide-8
SLIDE 8

Infinite records

{ . . . ; l : Absent ; . . . } { } Finite { . . . ; l : Absent ; . . . } Co-finite

8/ 25

slide-9
SLIDE 9

Infinite records

{ . . . ; l : Absent ; . . . } { } Finite { . . . ; l : Absent ; . . . } Co-finite

8/ 25

slide-10
SLIDE 10

Infinite records

{ . . . ; m : 𝛾 ; . . . ; l : 𝛿 ; . . . } {m : 𝛾} Finite { . . . ; l : 𝛿 ; . . . } Co-finite

9/ 25

slide-11
SLIDE 11

Infinite records

{ . . . ; m : 𝛾 ; . . . ; l : 𝛿 ; . . . } {m : 𝛾} Finite { . . . ; l : 𝛿 ; . . . } Co-finite

9/ 25

slide-12
SLIDE 12

Infinite records

{ . . . ; m : 𝜒 𝛽 ; . . . ; l : 𝛿 ; . . . } {m : 𝜒 𝛽} Finite { . . . ; l : 𝛿 ; . . . } Co-finite

10/ 25

slide-13
SLIDE 13

Infinite records

{ . . . ; m : 𝜒 𝛽 ; . . . ; l : 𝛿 ; . . . } {m : 𝜒 𝛽} Finite { . . . ; l : 𝛿 ; . . . } Co-finite

10/ 25

slide-14
SLIDE 14

Infinite records

{ . . . ; m : Present 𝛽 ; . . . ; l : 𝛾 ; . . . } { m : Present 𝛽 } Finite { . . . ; l : 𝛾 ; . . . } Co-finite

11/ 25

slide-15
SLIDE 15

Infinite records

{ . . . ; m : Present 𝛽 ; . . . ; l : 𝛾 ; . . . } { m : Present 𝛽 } Finite { . . . ; l : 𝛾 ; . . . } Co-finite

11/ 25

slide-16
SLIDE 16

Row variables

{ . . . ; l : Absent ; . . . } ⇓ { }

12/ 25

slide-17
SLIDE 17

Row variables

{ . . . ; l : Absent ; . . . } ⇓ { }

12/ 25

slide-18
SLIDE 18

Row variables

{ . . . ; m : Present 𝛽 ; . . . ; l : 𝛾 ; . . . } ⇓ {m : Present 𝛽 | 𝜍}

13/ 25

slide-19
SLIDE 19

Row variables

{ . . . ; m : Present 𝛽 ; . . . ; l : 𝛾 ; . . . } ⇓ {m : Present 𝛽 | 𝜍}

13/ 25

slide-20
SLIDE 20

Row variables

empty : {} extend : ∀𝛽: ∗ . ∀ 𝛾 : presence . ∀ 𝜍 : row (m) . ∀ 𝜒: ∗ ⇒ presence . 𝛽 → {m : 𝛾 | 𝜍} → {m : 𝜒 𝛽 | 𝜍} access : ∀𝛽: ∗ . ∀ 𝜍 : row (m) . {m : Present 𝛽 | 𝜍} → 𝛽

14/ 25

slide-21
SLIDE 21

Variant operations

  • 1. Match a variant with no constructors (match_empty)
  • 2. Extend a match with a variant constructor (extend_match)
  • 3. Use a variant constructor (create)

15/ 25

slide-22
SLIDE 22

Variant operations

l e t square = extend_match ( fun i −> c r e a t e ( i * i )) ( extend_match ( fun f −> c r e a t e ( f *. f )) match_empty ) l e t print_constant = extend_match ( fun i −> p r i n t _ i n t i ) ( extend_match ( fun f −> p r i n t _ f l o a t f ) ( extend_match ( fun s −> p r i n t _ s t r i n g s ) match_empty )) l e t () = print_constant ( square ( c r e a t e 5))

16/ 25

slide-23
SLIDE 23

Variant operations

match_empty : ∀ 𝛽: ∗ . [ ] → 𝛽 extend_match : ∀ 𝛽: ∗ . ∀ 𝛾 : presence . ∀ 𝛿 : ∗ . ∀ 𝜍 : row (M) . ∀ 𝜒: ∗ ⇒ presence . (𝛽 → 𝛿) → ( [M : 𝛾 | 𝜍 ] → 𝛿) → [M : 𝜒 𝛽 | 𝜍 ] → 𝛿 c r e a t e : ∀𝛽: ∗ . ∀ 𝜍 : row (M) . 𝛽 → [M : Present 𝛽| 𝜍 ]

17/ 25

slide-24
SLIDE 24

Object types

< foo : i n t ; bar : f l o a t > An object type where the method foo has type int and the method bar has type float. Both methods are present, and all other methods are absent.

18/ 25

slide-25
SLIDE 25

Object types

< foo : i n t ; bar : f l o a t ; . . > The object may contain other methods besides foo and bar. In

  • ther words, the .. represents an unnamed row variable.

19/ 25

slide-26
SLIDE 26

Object limitations

Instead of extend we have: v a l c r e a t e : ' a −> ' b −> ' c −> < l : ' a ; m : ' b ; n : ' c >

20/ 25

slide-27
SLIDE 27

Polymorphic variant types

[ ‘ Foo of i n t | ‘ Bar

  • f

f l o a t ] Represents a variant type where the constructor `Foo has type int and the constructor `Bar has type float. Both constructors are definitely present.

21/ 25

slide-28
SLIDE 28

Polymorphic variant types

[< ‘ Foo of i n t | ‘ Bar

  • f

f l o a t ] The variant is polymorphic in the presence of both constructors. In

  • ther words, the < represents two unnamed presence variables.

22/ 25

slide-29
SLIDE 29

Polymorphic variant types

[< ‘ Foo of i n t | ‘ Bar

  • f

f l o a t > ‘ Bar ] The variant is only polymorphic in the presence of the `Foo constructor – the `Bar constructor is definitely present. In other words, the < represents a single unnamed presence variable associated with `Foo.

23/ 25

slide-30
SLIDE 30

Polymorphic variant types

[> ‘ Foo of i n t | ‘ Bar

  • f

f l o a t ] The variant may contain more constructors than just `Foo and `Bar. In other words, the > represents an unnamed row variable. Constructors `Foo and `Bar are definitely present.

24/ 25

slide-31
SLIDE 31

Variant limitations

Instead of extend_match we have: v a l match : ( ' a −> ' d) −> ( ' b −> ' d) −> ( ' c −> ' d) −> [< `L of ' a | `M of ' b | `N of ' c ] −> ' d

25/ 25