Row polymorphism
1/ 25
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 : : . : . : . :
1/ 25
2/ 25
extend : ∀𝛽: ∗ . ∀𝛾 : ∗ . ∀𝛿 : ∗ . ∀𝜀 : ∗ . ∀𝜒: ∗ ⇒ ∗ . 𝜀 → Record 𝛽 𝛾 𝛿 → Record (𝜒 𝜀 ) 𝛾 𝛿 where 𝜒 is a type constructor variable that can be instantiated with:
Present (𝜇𝛽:∗. Absent)
3/ 25
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
What if we had infinite record types: { . . . ; foo : bar ; . . . }
5/ 25
empty : { . . . ; l : Absent ; . . . } extend : ∀ 𝛽: ∗ . ∀ 𝛾 : presence . . . . . ∀ 𝛿 : presence . . . . ∀ 𝜒: ∗ ⇒ presence . 𝛽 → { . . . ; m : 𝛾 ; . . . ; l : 𝛿 ; . . . } → { . . . ; m : 𝜒 𝛽 ; . . . ; l : 𝛿 ; . . . } access : ∀ 𝛽: ∗ . . . . . ∀ 𝛾 : presence . . . . { . . . ; m : Present 𝛽 ; . . . ; l : 𝛾 ; . . . } → 𝛽
6/ 25
Each record type appearing above can be divided into two parts:
variable or every type parameter is Absent.
7/ 25
{ . . . ; l : Absent ; . . . } { } Finite { . . . ; l : Absent ; . . . } Co-finite
8/ 25
{ . . . ; l : Absent ; . . . } { } Finite { . . . ; l : Absent ; . . . } Co-finite
8/ 25
{ . . . ; m : 𝛾 ; . . . ; l : 𝛿 ; . . . } {m : 𝛾} Finite { . . . ; l : 𝛿 ; . . . } Co-finite
9/ 25
{ . . . ; m : 𝛾 ; . . . ; l : 𝛿 ; . . . } {m : 𝛾} Finite { . . . ; l : 𝛿 ; . . . } Co-finite
9/ 25
{ . . . ; m : 𝜒 𝛽 ; . . . ; l : 𝛿 ; . . . } {m : 𝜒 𝛽} Finite { . . . ; l : 𝛿 ; . . . } Co-finite
10/ 25
{ . . . ; m : 𝜒 𝛽 ; . . . ; l : 𝛿 ; . . . } {m : 𝜒 𝛽} Finite { . . . ; l : 𝛿 ; . . . } Co-finite
10/ 25
{ . . . ; m : Present 𝛽 ; . . . ; l : 𝛾 ; . . . } { m : Present 𝛽 } Finite { . . . ; l : 𝛾 ; . . . } Co-finite
11/ 25
{ . . . ; m : Present 𝛽 ; . . . ; l : 𝛾 ; . . . } { m : Present 𝛽 } Finite { . . . ; l : 𝛾 ; . . . } Co-finite
11/ 25
{ . . . ; l : Absent ; . . . } ⇓ { }
12/ 25
{ . . . ; l : Absent ; . . . } ⇓ { }
12/ 25
{ . . . ; m : Present 𝛽 ; . . . ; l : 𝛾 ; . . . } ⇓ {m : Present 𝛽 | 𝜍}
13/ 25
{ . . . ; m : Present 𝛽 ; . . . ; l : 𝛾 ; . . . } ⇓ {m : Present 𝛽 | 𝜍}
13/ 25
empty : {} extend : ∀𝛽: ∗ . ∀ 𝛾 : presence . ∀ 𝜍 : row (m) . ∀ 𝜒: ∗ ⇒ presence . 𝛽 → {m : 𝛾 | 𝜍} → {m : 𝜒 𝛽 | 𝜍} access : ∀𝛽: ∗ . ∀ 𝜍 : row (m) . {m : Present 𝛽 | 𝜍} → 𝛽
14/ 25
15/ 25
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
match_empty : ∀ 𝛽: ∗ . [ ] → 𝛽 extend_match : ∀ 𝛽: ∗ . ∀ 𝛾 : presence . ∀ 𝛿 : ∗ . ∀ 𝜍 : row (M) . ∀ 𝜒: ∗ ⇒ presence . (𝛽 → 𝛿) → ( [M : 𝛾 | 𝜍 ] → 𝛿) → [M : 𝜒 𝛽 | 𝜍 ] → 𝛿 c r e a t e : ∀𝛽: ∗ . ∀ 𝜍 : row (M) . 𝛽 → [M : Present 𝛽| 𝜍 ]
17/ 25
< 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
< foo : i n t ; bar : f l o a t ; . . > The object may contain other methods besides foo and bar. In
19/ 25
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
[ ‘ Foo of i n t | ‘ Bar
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
[< ‘ Foo of i n t | ‘ Bar
f l o a t ] The variant is polymorphic in the presence of both constructors. In
22/ 25
[< ‘ Foo of i n t | ‘ Bar
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
[> ‘ Foo of i n t | ‘ Bar
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
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