SLIDE 1 Examples: Well-formed types
These are types:
- int
- bool
- int * bool
- int * int -> int
SLIDE 2 Examples: Not yet types
“Types in waiting” don’t classify terms (yet)
(but int list is a type)
(but char array is a type)
(but (int -> int) ref is a type)
SLIDE 3 Examples: Not types at all!
These are utter nonsense
SLIDE 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
SLIDE 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
SLIDE 6 What’s a good type? (Type formation)
Type formation rules for Typed Impcore
fUNIT ; INT ; BOOL g is a type
(BASETYPES)
is a type
ARRAY
( ) is a type
(ARRAYFORMATION) Design idea: What values does it allow?
SLIDE 7 Type judgments for monomorphic system
Two judgments:
- The familiar typing judgment
- ` e
:
- Now we add the judgment “ is a type”
SLIDE 8 Type rules for variables
Look up the type of a variable: x
2 dom
=
:
Types match in assignment (two
’s must be equal):
x
2 dom
=
:
(x ;e ) :
SLIDE 9 Understanding the SET rule
Types match in assignment (two
’s must be equal):
x
2 dom
=
:
(x ;e ) :
SLIDE 10 Understanding the SET rule
Types match in assignment (two
’s must be equal):
x
2 dom
=
:
(x ;e ) :
x
2 dom
= x
: e x
(x ;e ) : e
(SET)
SLIDE 11 Type rules for control
Boolean condition; matching branches
: BOOL
:
:
(e1 ;e2 ;e3 ) :
SLIDE 12 Product types
New abstract syntax: PAIR, FST, SND
1 and 2 are types 1
: 1
: 2
(e1 ;e2 ) : 1
: 1
(e ) : 1
: 1
(e ) : 2
Pair rules generalize to product types with many elements (“tuples,” “structs,” and “records”)
SLIDE 13 Product elimination in ML
Pattern matching:
: 1
fx 7! 1 ;y 7! 2 g ` e′ :
- ` let val (x, y) = e in e′ end
:
SLIDE 14 Arrow types: Function from x to y
Syntax: lambda, application Typed
Scheme style: 1 ; : : : ; n and are types
(1
! ) is a type
(ARROWFORMATION) ML style: each function takes a tuple:
1 ; : : : ; n and are types 1
! is a type
(MLARROWFORMATION)
SLIDE 15 Arrow types: Function from x to y
Eliminate with application:
: (1
! )
: i ;
1
i n
(e ;e1 ; : : : ;en ) :
fx1 7! 1 ; : : : ;xn 7! n g ` e :
(x1 : 1 ; : : : ;xn : n ;e ) : (1
! )
SLIDE 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) ...
SLIDE 17 Array types
Formation:
is a type
ARRAY
( ) is a type
Introduction:
: INT
:
(e1 ;e2 ) : ARRAY ( )
SLIDE 18 Array types continued
Elimination:
: ARRAY ( )
: INT
(e1 ;e2 ) :
: ARRAY ( )
: INT
:
(e1 ;e2 ;e3 ) :
: ARRAY ( )
(e ) : INT
SLIDE 19 From rule to code
Arrow-introduction
fx1 7! 1 ; : : : ;xn 7! n g ` e :
;1 i n
(x1 : 1 ; : : : ;xn : n ;e ) : (1
! )
SLIDE 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
SLIDE 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
SLIDE 22
Talking type theory
Formation: make new types Introduction: make new values Elimination: observe (“take apart”) existing values
SLIDE 23
Types and their C constructs
Type Produce Consume Introduce Eliminate struct (definition form only) dot notation e.next, e->next pointer & * function (definition form only) application
SLIDE 24
Types and their
Scheme constructs
Type Produce Consume Introduce Eliminate record constructor function accessor functions type predicate function lambda application
SLIDE 25
Types and their ML constructs
Type Produce Consume Introduce Eliminate arrow Lambda (fn) Application constructed (algebraic) Apply constructor Pattern match constructed (tuple) (e1, ..., en) Pattern match!
SLIDE 26 Type soundness
If
:
; ;
+ hv ; ′ i
, and are consistent,
then
predicts v
Consistency:
dom
dom , and 8x 2 dom
(x ) predicts
Sample predictions: int predicts 7, bool predicts #t