with monomorphism new types are costly
play

With monomorphism, new types are costly Closed world Only language - PowerPoint PPT Presentation

With monomorphism, new types are costly Closed world Only language designer can add a new type constructor A new type constructor (array) requires New expression and type syntax New internal representation (type formation)


  1. With monomorphism, new types are costly Closed world • Only language designer can add a new type constructor A new type constructor (“array”) requires • New expression and type syntax • New internal representation (type formation) • New type rules (intro, elim) • New code in type checker • New or revised proof of soundness

  2. Costly for language designers � is a type Formation: ) is a type ARRAY ( � ` e 1 ` e 2 : INT � � : � Introduction: ( e 1 ; e 2 ` AMAKE : ARRAY � ) ( � ) ` e 1 ` e 2 : ARRAY : INT � ( � ) � Elimination: ( e 1 ; e 2 ` AAT � ) : � ` e 1 ` e 2 ` e 3 : ARRAY : INT � ( � ) � � : � ( e 1 ; e 2 ; e 3 ` APUT � ) : � ` e : ARRAY � ( � ) ( e ` ASIZE : INT � )

  3. Costly for programmers Monomorphism leads to code duplication User-defined functions are monomorphic: (check-function-type swap ([array bool] int int -> unit)) (define unit swap ([a : (array bool)] [i : int] [j : int]) (begin (set tmp (array-at a i)) (array-put a i (array-at a j)) (array-put a j tmp) (begin)))

  4. Idea: Use functions not syntax to represent new types Benefits: • No new syntax • No new internal representation • No new type rules • No new code in type checker • No new proof of soundness • Programmers can add new types Requires: more expressive function types

  5. Better type for a swap function (check-type swap (forall (’a) ([array ’a] int int -> unit)))

  6. Quantified types Heart of polymorphism: � . 8 � 1 � n ; : : : ; : In Typed � Scheme: (forall (’a1 ... ’an) type ) Two ideas: • Type variable ’a stands for an unknown type • Quantified type (with forall ) enables instantiation car � list : 8 � : ! � cdr � list � list : 8 � : ! cons � list � list : 8 � : � � ! ’() � list : 8 � : length � list ! int : 8 � :

  7. Quantified types Heart of polymorphism: � . 8 � 1 � n ; : : : ; : In Typed � Scheme: (forall (’a1 ... ’an) type ) Two ideas: • Type variable ’a stands for an unknown type • Quantified type (with forall ) enables instantiation car : (forall (’a) ([list ’a] -> ’a)) cdr : (forall (’a) ([list ’a] -> [list ’a])) cons : (forall (’a) (’a [list ’a] -> [list ’a])) ’() : (forall (’a) (list ’a)) length : (forall (’a) ([list ’a] -> int))

  8. Programming with quantified types Substitute for quantified variables: “instantiate” -> length <function> : (forall (’a) ((list ’a) -> int)) -> [@ length int] <function> : ((list int) -> int) -> (length ’(1 2 3)) type error: function is polymorphic; instantiate before applying -> ([@ length int] ’(1 2 3)) 3 : int

  9. Instantiate as you like -> length <function> : (forall (’a) ((list ’a) -> int)) -> [@ length bool] <function> : ((list bool) -> int) -> ([@ length bool] ’(#t #f)) 2 : int

  10. More instantiations -> (val length-int [@ length int]) length-int : ((list int) -> int) -> (val cons-bool [@ cons bool]) cons-bool : ((bool (list bool)) -> (list bool)) -> (val cdr-sym [@ cdr sym]) cdr-sym : ((list sym) -> (list sym)) -> (val empty-int [@ ’() int]) () : (list int)

  11. Create your own! Abstract over unknown type using type-lambda -> (val id (type-lambda [’a] (lambda ([x : ’a]) x ))) id : (forall (’a) (’a -> ’a)) ’a is type parameter (an unknown type) This feature is parametric polymorphism (aka generics)

  12. Polymorphic array swap (check-type swap (forall (’a) ([array ’a] int int -> unit))) (val swap (type-lambda (’a) (lambda ([a : (array ’a)] [i : int] [j : int]) (let ([tmp ([@ Array.at ’a] a i)]) (begin ([@ Array.put ’a] a i ([@ Array.at ’a] a j)) ([@ Array.put ’a] a j tmp))))))

  13. Power comes at high notational cost Function composition -> (val o (type-lambda [’a ’b ’c] (lambda ([f : (’b -> ’c)] [g : (’a -> ’b)]) (lambda ([x : ’a]) (f (g x)))))) o : (forall (’a ’b ’c) ((’b -> ’c) (’a -> ’b) -> (’a -> ’c))) Aka o : 8 �; � ; � : ( � ! � ) � ( � ! � ) ! ( � ! � )

  14. Representing quantified types Two new alternatives for tyex : datatype tyex = TYCON of name // int | CONAPP of tyex * tyex list // (list bool) | FUNTY of tyex list * tyex // (int int -> bool) | TYVAR of name // ’a | FORALL of name list * tyex // (forall (’a) ...)

  15. Generalize with type-lambda 8 introduction: • Concrete syntax (type-lambda [ � 1 � n ] e ) � � � • Rule (forall introduction): ` e � f � 1 � n :: � ; : : : :: �g ; � : � 1 � i � n � i 62 ftv (�) ; ; e ` TYLAMBDA ( � 1 � n 8 � 1 � n � ; � ; : : : ; ) : ; : : : ; :� � is kind environment (remembers � i ’s are types)

  16. Instantiate by substitution 8 elimination: • Concrete syntax (@ e � n ) � 1 � � � • Rule (note new judgment form � ): ` e � ; � : ` e 8 � 1 � n � ; � : ; : : : ; :� ( e ` TYAPPLY � n � n � n � 1 [ � 1 � 1 � ; � ; ; : : : ; ) : � 7! ; : : : ; 7! ℄ Substitution is in the book as function tysubst (Also in the book: instantiate )

  17. What have we gained? No more type-specific introduction rules: • Instead, use polymorphic functions No more type-specific elimination rules: • Instead, use polymorphic functions But, we still need formation rules

  18. User types can’t be blindly trusted -> (lambda ([a : array]) (Array.size a)) type error: used type constructor ‘array’ as a type -> (lambda ([x : (bool int)]) x) type error: tried to apply type bool as type constructor -> (@ car list) type error: instantiated at type constructor ‘list’, which is not a type How can we know which types are OK?

  19. We can classify type constructors Is a type: int ; bool int :: � bool :: � Takes a type (to make a type): array , list list :: � ) � array :: � ) � These labels are called kinds

  20. Type formation through kinds Each type constructor has a kind, which is either: • � , or • � 1 � n � � � � � ) � Type constructors of kind � classify terms ( int � , bool � ) :: :: Type constructors of arrow kinds are “types in waiting” ( list � , array � , pair � ) :: � ) :: � ) :: � � � )

  21. The kinding judgment “Type � has kind � ” � ` � :: � Special case: “ � is a type” ( asType ) � ` � :: � Replaces one-off type-formation rules Kind environment � tracks type constructor names and kinds. Use asType in code!

  22. Kinding rules for types � 2 dom � �( � ) = � K IND I NTRO C ON ` TYCON � ( � ) :: � � 1 � n � ` � :: � � � � � ) � 1 � i � n � i � i � ` :: ; K IND A PP ` CONAPP [ � 1 � n � ( � ; ; : : : ; ℄) :: � These two rules replace all formation rules. (Check out book functions kindof and asType )

  23. Designer’s burden reduced To extend Typed Impcore: • New syntax • New type rules • New internal representation • New code • New soundness proof To extend Typed � Scheme, none of the above! Just • New functions • New primitive type constructor in � You’ll do arrays both ways

  24. Kinds of primitive type constructors �( int ) = � �( bool ) = � �( list ) = � ) � �( option ) = � ) � �( pair ) = � � � ) �

  25. What can a programmer add? Typed Impcore: • Closed world (no new types) • Simple formation rules Typed � Scheme: • Semi-closed world (new type variables) • Types are formed via explicit abstraction and instantiation Standard ML: • Open world (programmers create new types) • How are types formed (from other types)?

  26. How ML works: Three environments maps names (of tycons and tyvars) to kinds � maps names (of variables) to types � maps names (of variables) to values or locations � New val def val x = 33 New type def type ’a transformer = ’a -> ’a New datatype def datatype color = RED | GREEN | BLUE

  27. Three environments revealed maps names (of tycons and tyvars) to kinds � maps names (of variables) to types � maps names (of variables) to values or locations � New val def modifies � , � val x = 33 means � f x � f x 7! 33 g : int g ; New type def modifies � type ’a transformer = ’a list * ’a list means � f transformer :: � ) �g New datatype def modifies � ; � ; � datatype color = RED | GREEN | BLUE means � f color � f RED : color ; GREEN : color ; BLUE : color g , :: �g ; � f RED 7! 0 ; GREEN 7! 1 ; BLUE 7! 2 g

  28. Exercise: Three environments datatype ’a tree = NODE of ’a tree * ’a * ’a tree | EMPTY means �g , � f tree 7! � ) 8 ’a : ’a tree * ’a * ’a tree ! ’a tree, � f NODE 7! 8 ’a : ’a tree g , EMPTY 7! 7! 1 g � ( l ; x ; r ) � f NODE ; EMPTY 7! : � � �

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