Transcript slides

Lecture 28:
Types of Types
“It would appear that we have reached the limits
of what it is possible to achieve with computer
technology, although one should be careful with
such statements, as they tend to sound pretty
silly in five years.”
John Von Neumann, 1949
CS200: Computer Science
University of Virginia
Computer Science
David Evans
http://www.cs.virginia.edu/~evans
Menu
• Types and Type Checking
• Typed Scheme
1 April 2002
CS 200 Spring 2002
2
Types
Strings
Numbers
programs that halt
Colors
Beatle’s Songs that don’t end on the Tonic
lists of lists of lists of anything
• Type is a (possibly infinite) set of values
• You can do some things with some types,
but not others
1 April 2002
CS 200 Spring 2002
3
Why have types?
• Detecting program errors
– Better to notice error than report incorrect result
– Better to find error in development then in
execution
• Make programs easier to read, understand
and maintain
– Better than comments if they are checked and
can be trusted
• Security
– Can use types to constrain the behavior of
programs (not in CS200)
1 April 2002
CS 200 Spring 2002
4
Types of Types
Does regular Scheme have types?
> (car 3)
car: expects argument of type <pair>; given 3
> (+ (cons 1 2))
+: expects argument of type <number>; given (1 . 2)
Yes, without types (car 3) would produce some silly result.
Because of types, it produces a type error.
1 April 2002
CS 200 Spring 2002
5
Type Taxonomy
• Latent vs. Manifest
– Are types visible in the program text?
• Checked statically vs. checked
dynamically
– Do you have to run the program to know if it
has type errors?
• Checked weakly vs. strongly
– How strict are the rules for using types?
– Meaningless (just matter of degree)
1 April 2002
CS 200 Spring 2002
6
Scheme  Java
• Scheme has Latent, Dynamic types
• Java has Manifest, Static types
1 April 2002
CS 200 Spring 2002
7
Java Example
javac types.java
class Test {
The result
types.java:5: Incompatible
int tester
is an integer
type for =. Can't convert
java.lang.String to int.
{
x = s;
int x;
^
x is an integer
types.java:6: Incompatible
x = s;
type for return. Can't convert
java.lang.String to int.
return
return "okay";
^
2 errors
}
}
1 April 2002
CS 200 Spring 2002
(String s)
"okay";
8
Java Example
javac types.java
types.java:6: Incompatible
type for =. Can't convert
java.lang.String to int.
x = s;
^
types.java:7: Incompatible
type for return. Can't convert
java.lang.String to int.
return "okay";
^
2 errors
1 April 2002
class Test {
int tester (String s)
{
int x;
tester (“hello”);
x = s;
return "okay";
}
}
CS 200 Spring 2002
9
What do we need to do change
our Mini-Scheme evaluator to
provide Java-like type checking?
1 April 2002
CS 200 Spring 2002
10
Types in Mini-Scheme
Type ::= PrimitiveType
Type ::= ProcedureType
Type ::= ProductType
ProcedureType ::= Type  Type
ProductType ::= Type x Type
PrimitiveType ::= Number | String
1 April 2002
CS 200 Spring 2002
11
Examples
Type ::= PrimitiveType | ProcedureType | ProductType
ProcedureType ::= Type  Type
ProductType ::= Type x Type
PrimitiveType ::= Number | String
3
Number
+
Number x Number  Number
(+ 3 3)
Changed lambda form:
Expression ::= (lambda (((Name Type))*) Expr*)
Number
(lambda ((x number) (y number)) (+ x y))
Number x Number  Number
1 April 2002
CS 200 Spring 2002
12
Changing Evaluator
• Divide evaluation into two steps:
– Checking types
– Evaluating (essentially as before)
• How do we implement check-type?
– Represent types
– Put types in frame
– Change meval into typeof
1 April 2002
CS 200 Spring 2002
13
Representing
Types
Type ::= PrimitiveType | ProcedureType | ProductType
ProcedureType ::= Type  Type
ProductType ::= Type x Type
PrimitiveType ::= Number | String
(define (make-primitive-type type) (list 'primitive-type type))
(define (primitive-type? type) (tagged-list? type 'primitive-type))
(define (make-number-type) (make-primitive-type 'number))
(define (number-type? type)
(and (primitive-type? type) (eq? (cadr type) 'number)))
(define (make-boolean-type) (make-primitive-type 'boolean))
(define (boolean-type? type)
(and (primitive-type? type) (eq? (cadr type) 'boolean)))
(define (make-string-type) (make-primitive-type 'string))
(define (string-type? type)
(and (primitive-type? type) (eq? (cadr type) 'string)))
1 April 2002
CS 200 Spring 2002
14
Representing
Types
Type ::= PrimitiveType | ProcedureType | ProductType
ProcedureType ::= Type  Type
ProductType ::= Type x Type
PrimitiveType ::= Number | String
(define (make-procedure-type params result)
(list 'procedure-type params result))
(define (procedure-type? type)
(tagged-list? type 'procedure-type))
(define (procedure-type-result type)
(assert (procedure-type? type))
(caddr type))
(define (assert pred)
(if (not pred)
(error
"Assertion failed!")))
(define (procedure-type-params type)
(assert (procedure-type? type))
(cadr type))
1 April 2002
CS 200 Spring 2002
15
Type of +
(make-procedure-type
(make-product-type (make-number-type)
(make-number-type))
(make-number-type))
1 April 2002
CS 200 Spring 2002
16
global
environment
Changing global
Frames
environment
+: (-> (x Number Number)
Number)
#<primitive:+>
+ : #<primitive:+>
x: 3
double:
x: Number 3
double:
(-> Number
Number)
parameters: x
body: (lambda (x) (+ x x))
parameters: x Number
body: (lambda (x) (+ x x))
1 April 2002
CS 200 Spring 2002
17
Changing Frames
(define (extend-environment names values env)
(make-new-environment
(map (lambda (name value)
(cons name value))
names values)
env))
(define (extend-environment names types values env)
(make-new-environment
(map (lambda (name type value)
(list name type value))
names types values)
env))
1 April 2002
CS 200 Spring 2002
18
Looking Up Variables
(define (environment-lookup-name name env)
(if (null? env) (error "No binding for" name)
(if (frame-contains? name (first-frame env))
(frame-lookup-name name (first-frame env))
(environment-lookup-name name (enclosing-environment env)))))
(define (environment-lookup-value
name env)
(if (null? env)
(error "No binding for" name)
(if (frame-contains? name
(first-frame env))
(frame-lookup-value name
(first-frame env))
(environment-lookup-value
name
(enclosing-environment env)))))
1 April 2002
(define (typeof-variable name env)
(if (null? env)
(error "No binding for" name)
(if (frame-contains? name
(first-frame env))
(frame-lookup-type name
(first-frame env))
(typeof-variable
name
(enclosing-environment env)))))
CS 200 Spring 2002
19
Frame Lookups
(define (frame-lookup-value name frame)
(if (null? frame) (error "Name not found in frame:" name)
(if (eq? (car (car frame)) name)
(caddr (car frame))
(frame-lookup-value name (cdr frame)))))
(define (frame-lookup-type name frame)
(if (null? frame) (error "Name not found in frame:" name)
(if (eq? (car (car frame)) name)
(cadr (car frame))
(frame-lookup-type name (cdr frame)))))
1 April 2002
CS 200 Spring 2002
20
Charge
• Wednesday: Finish TypedScheme
– Handling lambda
• Friday: PS7 Due
• Exam 2 out Friday or Monday (class
choice), due Weds next week
– Classify problems (undecidable, NP, P, etc.)
– Modify TypedScheme
1 April 2002
CS 200 Spring 2002
21