Examples: Well-formed types These are types: • int • bool • int * bool • int * int -> int
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)
Examples: Not types at all! These are utter nonsense • int int • bool * array
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
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
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?
Type judgments for monomorphic system Two judgments: • The familiar typing judgment ` e � : � • Now we add the judgment “ � is a type”
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 � ) : �
Understanding the S ET rule Types match in assignment (two � ’s must be equal): x �( x ) ` e 2 dom � = � � : � (S ET ) ` SET ( x ; e � ) : �
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 )
Type rules for control Boolean condition; matching branches : BOOL ` e 1 ` e 2 ` e 3 � � : � � : � (I F ) ` IF ( e 1 ; e 2 ; e 3 � ) : �
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”)
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 )
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 � � � � � !
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 � : ; : : : : ) � � � !
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) ...
Array types � is a type Formation: ) is a type ARRAY ( � ` e 1 ` e 2 : INT � � : � Introduction: ( e 1 ; e 2 ` AMAKE : ARRAY � ) ( � )
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 � )
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 � : ; : : : : ) � � � !
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
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
Talking type theory Formation: make new types Introduction: make new values Elimination: observe (“take apart”) existing values
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
Types and their � Scheme constructs Type Produce Consume Introduce Eliminate constructor accessor functions record function type predicate function application lambda
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)
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
Recommend
More recommend