examples well formed types
play

Examples: Well-formed types These are types: int bool int * bool - PowerPoint PPT Presentation

Examples: Well-formed types These are types: int bool int * bool int * int -> int Examples: Not yet types Types in waiting dont classify terms (yet) list (but int list is a type) array (but char array is a


  1. Examples: Well-formed types These are types: • int • bool • int * bool • int * int -> int

  2. Examples: Not yet types “Types in waiting” don’t classify terms (yet) • list (but int list is a type) • array (but char array is a type) • ref (but (int -> int) ref is a type)

  3. Examples: Not types at all! These are utter nonsense • int int • bool * array

  4. Type-formation rules Are about not trusting the source code We need a way to classify type expressions into: • types that classify terms • type constructors that build types • nonsense that doesn’t mean anything

  5. Type constructors Technical name for “types in waiting” Given zero or more arguments, produce a type: • Nullary: int , bool , char ; also called base types • Unary: list , array , ref • Binary: (infix) -> More complex type constructors: • records/structs • function in C, uScheme, Impcore

  6. What’s a good type? (Type formation) Type formation rules for Typed Impcore f UNIT ; INT ; BOOL � 2 g (B ASE T YPES ) � is a type � is a type (A RRAY F ORMATION ) ) is a type ARRAY ( � Design idea: What values does it allow?

  7. Type judgments for monomorphic system Two judgments: • The familiar typing judgment ` e � : � • Now we add the judgment “ � is a type”

  8. Type rules for variables Look up the type of a variable: x �( x ) 2 dom � = � (V AR ) ` x � : � Types match in assignment (two � ’s must be equal): x �( x ) ` e 2 dom � = � � : � (S ET ) ` SET ( x ; e � ) : �

  9. Understanding the S ET rule Types match in assignment (two � ’s must be equal): x �( x ) ` e 2 dom � = � � : � (S ET ) ` SET ( x ; e � ) : �

  10. Understanding the S ET rule Types match in assignment (two � ’s must be equal): x �( x ) ` e 2 dom � = � � : � (S ET ) ` SET ( x ; e � ) : � x �( x ) ` e � x � e � x � e 2 dom � = � : � ` SET ( x ; e � e � ) : (S ET )

  11. Type rules for control Boolean condition; matching branches : BOOL ` e 1 ` e 2 ` e 3 � � : � � : � (I F ) ` IF ( e 1 ; e 2 ; e 3 � ) : �

  12. Product types New abstract syntax: PAIR , FST , SND � 1 and � 2 are types ` e 1 ` e 2 � 1 � 2 � : � : � 2 is a type ` PAIR ( e 1 ; e 2 � 1 � 1 � 2 � � ) : � ` e ` e � 1 � 2 � 1 � 2 � : � � : � ` FST ` SND ( e ( e � 1 � 2 � ) : � ) : Pair rules generalize to product types with many elements (“tuples,” “structs,” and “records”)

  13. Product elimination in ML Pattern matching: ` e ′ ` e � f x ; y � 1 � 2 � 1 � 2 � : � 7! 7! g : � ` let val ( x , y ) = e in e ′ end � : � (P AIR -E LIM )

  14. Arrow types: Function from x to y Syntax: lambda , application Typed � Scheme style: � n and � are types � 1 ; : : : ; (A RROW F ORMATION ) � ) is a type ( � 1 � n � � � ! ML style: each function takes a tuple: � n and � are types � 1 ; : : : ; (MLA RROW F ORMATION ) � is a type � 1 � n � � � � � !

  15. Arrow types: Function from x to y Eliminate with application: ` e : ( � 1 � ) � n � � � � ! ` e i 1 � i � n � i � : ; ( e ; e 1 ; e n ` APPLY � ; : : : ) : � Introduce with lambda : � f x 1 ; x n ` e � n � 1 7! ; : : : 7! g : � ( x 1 ; x n ; e : ( � 1 � ) ` LAMBDA � 1 � n � n � : ; : : : : ) � � � !

  16. Typical syntactic support for types Explicit types on lambda and define : • For lambda , argument types: (lambda ([n : int] [m : int]) (+ (* n n) (* m m))) • For define , argument and result types: (define int max ([x : int] [y : int]) (if (< x y) y x)) Abstract syntax: datatype exp = ... | LAMBDA of (name * tyex) list * exp ... datatype def = ... | DEFINE of name * tyex * ((name * tyex) list * exp) ...

  17. Array types � is a type Formation: ) is a type ARRAY ( � ` e 1 ` e 2 : INT � � : � Introduction: ( e 1 ; e 2 ` AMAKE : ARRAY � ) ( � )

  18. Array types continued Elimination: ` e 1 ` e 2 : ARRAY : INT � ( � ) � ( 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 � )

  19. From rule to code Arrow-introduction � i is a type � f x 1 ; x n ` e ; 1 � i � n � 1 � n 7! ; : : : 7! g : � ( x 1 ; x n ; e : ( � 1 � ) ` LAMBDA � n � n � 1 � : ; : : : : ) � � � !

  20. Type-checking LAMBDA datatype exp = LAMBDA of (name * tyex) list * exp ... fun ty (Gamma, LAMBDA (formals, body)) = let val Gamma’ = (* body gets new env *) foldl (fn ((x, ty), g) => bind (x, ty, g)) Gamma formals val bodytype = ty (Gamma’, body) val formaltypes = map (fn (x, ty) => ty) formals in FUNTY (formaltypes, bodytype) end

  21. Understanding language design Questions about types never seen before: • What types can I make? • What syntax goes with each form? – To create values? – To do things with values? • What functions? • What about user-defined types? Examples: pointer, struct, function, record

  22. Talking type theory Formation: make new types Introduction: make new values Elimination: observe (“take apart”) existing values

  23. Types and their C constructs Type Produce Consume Introduce Eliminate dot notation struct (definition form only) e .next , e ->next pointer & * function (definition form only) application

  24. Types and their � Scheme constructs Type Produce Consume Introduce Eliminate constructor accessor functions record function type predicate function application lambda

  25. Types and their ML constructs Type Produce Consume Introduce Eliminate arrow Lambda ( fn ) Application constructed Apply constructor Pattern match (algebraic) constructed Pattern match! ( e 1 , ..., e n ) (tuple)

  26. Type soundness If • ` e � : � � ′ • h e h v ; �; � i + ; i • � , � , and � are consistent, then � predicts v Consistency: � , and dom � = dom �( x ) predicts ( � ( x )) . 8 x 2 dom � : � Sample predictions: int predicts 7 , bool predicts #t

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