row polymorphism
play

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. Row polymorphism 1/ 25

  2. 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 )

  3. Presence variables : ∀𝛽 : ∗ . ∀𝛾 : ∗ . ∀𝛿 : ∗ . ∀𝜀 : ∗ . ∀𝜒 : ∗ ⇒ ∗ . 𝜀 → Record 𝛽 𝛾 𝛿 → Record ( 𝜒 𝜀 ) 𝛾 𝛿 where 𝜒 is a type constructor variable that can be instantiated with: 3/ 25 extend ���� � Present � ( 𝜇𝛽 : ∗ . Absent)

  4. Ill-formed records Polymorphic record types allow some ill-formed type expressions: 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 � Record Int (Present String) (Present String) � List (Present Int)

  5. Infinite records What if we had infinite record types: { . . . ; foo : bar ; . . . } 5/ 25

  6. Infinite records . . . . . . ; l : 𝛿 � ; . . . } : ∀ 𝛽 : ∗ . . . . empty { . . . ; m : Present 𝛽 ; . . . ; l : 𝛾 � ; . . . } → 𝛽 ; m : 𝜒 𝛽 ; { . . . . . . } → ; : { . . . ; l : Absent ; . . . } : ∀ 𝛽 : ∗ . ∀ 𝛾 : presence . . . . . . . ∀ 𝜒 : ∗ ⇒ presence . 𝛽 → { . . . ; m : 𝛾 ; . . . ; l : 𝛿 � 6/ 25 extend � . ∀ 𝛿 � : presence . access � . ∀ 𝛾 � : presence .

  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

  8. Infinite records . . . Co-finite . . . } ; Absent : l ; { { . . . Finite { } . . . } ; Absent : l ; 8/ 25

  9. Infinite records . . . Co-finite . . . } ; Absent : l ; { { . . . Finite { } . . . } ; Absent : l ; 8/ 25

  10. Infinite records Finite Co-finite . . . } ; 𝛿 � : l ; { . . . {m : 𝛾 } { . . . . . . } ; 𝛿 � : l ; . . . ; m : 𝛾 ; 9/ 25

  11. Infinite records Finite Co-finite . . . } ; 𝛿 � : l ; { . . . {m : 𝛾 } { . . . . . . } ; 𝛿 � : l ; . . . ; m : 𝛾 ; 9/ 25

  12. Infinite records Finite Co-finite . . . } ; 𝛿 � : l ; { . . . {m : 𝜒 𝛽 } { . . . . . . } ; 𝛿 � : l ; . . . ; m : 𝜒 𝛽 ; 10/ 25

  13. Infinite records Finite Co-finite . . . } ; 𝛿 � : l ; { . . . {m : 𝜒 𝛽 } { . . . . . . } ; 𝛿 � : l ; . . . ; m : 𝜒 𝛽 ; 10/ 25

  14. Infinite records Present 𝛽 } Co-finite . . . } ; : 𝛾 � l ; { . . . Finite { m : { . . . . . . } ; : 𝛾 � l ; . . . Present 𝛽 ; ; m : 11/ 25

  15. Infinite records Present 𝛽 } Co-finite . . . } ; : 𝛾 � l ; { . . . Finite { m : { . . . . . . } ; : 𝛾 � l ; . . . Present 𝛽 ; ; m : 11/ 25

  16. Row variables { . . . ; l : Absent ; . . . } ⇓ { } 12/ 25

  17. Row variables { . . . ; l : Absent ; . . . } ⇓ { } 12/ 25

  18. Row variables { . . . ; m : Present 𝛽 ; . . . ; l : 𝛾 � ; . . . } ⇓ {m : Present 𝛽 | 𝜍 } 13/ 25

  19. Row variables { . . . ; m : Present 𝛽 ; . . . ; l : 𝛾 � ; . . . } ⇓ {m : Present 𝛽 | 𝜍 } 13/ 25

  20. Row variables empty : {} : ∀𝛽 : ∗ . ∀ 𝛾 : presence . ∀ 𝜍 : row (m) . ∀ 𝜒 : ∗ ⇒ presence . 𝛽 → {m : 𝛾 | 𝜍 } → {m : 𝜒 𝛽 | 𝜍 } : ∀𝛽 : ∗ . ∀ 𝜍 : row (m) . {m : Present 𝛽 | 𝜍 } → 𝛽 14/ 25 extend � access �

  21. Variant operations 2. Extend a match with a variant constructor ( extend_match ) 3. Use a variant constructor ( create ) 15/ 25 1. Match a variant with no constructors ( match_empty )

  22. Variant operations l e t ( square () = print_constant l e t match_empty )) s ) s − > p r i n t _ s t r i n g f ) f − > p r i n t _ f l o a t i ) i − > p r i n t _ i n t 16/ 25 print_constant = l e t match_empty ) f )) *. i )) * square = extend_match ��� ( fun i − > c r e a t e ��� ( i ( extend_match ����� ( fun f − > c r e a t e ����� ( f extend_match ��� ( fun ( extend_match ����� ( fun ( extend_match ������ ( fun ( c r e a t e ��� 5))

  23. Variant operations match_empty : ∀ 𝛽 : ∗ . 𝜍 ] Present 𝛽 | 𝛽 → [M : : ∀𝛽 : ∗ . ∀ 𝜍 : row (M) . 𝜍 ] → 𝛿 [M : 𝜒 𝛽 | 𝜍 ] → 𝛿 ) → ( 𝛽 → 𝛿 ) → ( [M : 𝛾 | ∗ ⇒ presence . ∀ 𝜍 : row (M) . ∀ 𝜒 : ∀ 𝛽 : ∗ . ∀ 𝛾 : presence . ∀ 𝛿 : ∗ . ] → 𝛽 [ 17/ 25 extend_match � : c r e a t e �

  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

  25. Object types < foo : i n t ; bar : f l o a t ; . . > The object may contain other methods besides foo and bar . In other words, the .. represents an unnamed row variable. 19/ 25

  26. Object limitations v a l : ' a − > ' b − > ' c − > < l : ' a ; m : ' b ; n : ' c > 20/ 25 Instead of extend � we have: c r e a t e �����

  27. Polymorphic variant types [ ‘ Foo of i n t | ‘ Bar of 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

  28. Polymorphic variant types [< ‘ Foo of i n t | ‘ Bar of f l o a t ] The variant is polymorphic in the presence of both constructors. In other words, the < represents two unnamed presence variables. 22/ 25

  29. Polymorphic variant types [< ‘ Foo of i n t | ‘ Bar of 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

  30. Polymorphic variant types [> ‘ Foo of i n t | ‘ Bar of 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

  31. Variant limitations v a l match ����� : ( ' a − > ' d) − > ( ' b − > ' d) − > ( ' c − > ' d) − > ' a | `M of ' b | `N of ' c ] − > ' d 25/ 25 Instead of extend_match � we have: [< `L of

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend