Download presentation
Presentation is loading. Please wait.
Published byTodd Fox Modified over 8 years ago
1
David Evans http://www.cs.virginia.edu/evans CS200: Computer Science University of Virginia Computer Science Lecture 27: 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
2
31 March 2003CS 200 Spring 20032 Menu Types and Type Checking Typed Scheme
3
31 March 2003CS 200 Spring 20033 Types Numbers Strings Beatle’s Songs that don’t end on the Tonic Colors lists of lists of lists of anything programs that halt Type is a (possibly infinite) set of values You can do some things with some types, but not others
4
31 March 2003CS 200 Spring 20034 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
5
31 March 2003CS 200 Spring 20035 Types of Types Does regular Scheme have types? > (car 3) car: expects argument of type ; given 3 > (+ (cons 1 2)) +: expects argument of type ; given (1. 2) Yes, without types (car 3) would produce some silly result. Because of types, it produces a type error.
6
31 March 2003CS 200 Spring 20036 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)
7
31 March 2003CS 200 Spring 20037 Scheme Java Scheme has Latent, Dynamic types –Don’t see explicit types when you look at code –Checked when an expression is evaluated Java has Manifest, Static types –Type declarations must be included in code –Type of most expressions is checked before running the program Some types checked at run-time
8
31 March 2003CS 200 Spring 20038 Java Example class Test { int tester (String s) { int x; x = s; return "okay"; } The result is an integer The place x holds an integer > javac types.java types.java:5: Incompatible type for =. Can't convert java.lang.String to int. x = s; ^ types.java:6: Incompatible type for return. Can't convert java.lang.String to int. return "okay"; ^ 2 errors The parameter must be a String javac compiles (and type checks) the program. It does not execute it.
9
31 March 2003CS 200 Spring 20039 What do we need to do change our Mini-Scheme evaluator to provide Java-like type checking?
10
31 March 2003CS 200 Spring 200310 Types in Mini-Scheme Type ::= PrimitiveType Type ::= ProcedureType Type ::= ProductType ProcedureType ::= Type Type ProductType ::= Type x Type PrimitiveType ::= Number | String
11
31 March 2003CS 200 Spring 200311 Examples 3 Number + Number x Number Number (+ 3 3) Number (lambda ((x number) (y number)) (+ x y)) Number x Number Number Type ::= PrimitiveType | ProcedureType | ProductType ProcedureType ::= Type Type ProductType ::= Type x Type PrimitiveType ::= Number | String Changed lambda form: Expression ::= (lambda (((Name Type))*) Expr*)
12
31 March 2003CS 200 Spring 200312 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
13
31 March 2003CS 200 Spring 200313 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)))
14
31 March 2003CS 200 Spring 200314 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 (procedure-type-params type) (assert (procedure-type? type)) (cadr type)) (define (assert pred) (if (not pred) (error "Assertion failed!")))
15
31 March 2003CS 200 Spring 200315 Type of + (make-procedure-type (make-product-type (make-number-type) (make-number-type)) + is a procedure that takes 2 numbers as input produces one number as output
16
31 March 2003CS 200 Spring 200316 typeof Examples > (typeof '(+ 3 4) the-global-environment) (primitive-type number) > (typeof '(+ 3 +) the-global-environment) Type mismatch. Application (+ 3 +) parameter type is (Number x Number) -> (Number) x Number, should be Number x Number. (error-type) > (typeof '(+ 3) the-global-environment) Type mismatch. Application (+ 3) parameter type is Number, should be Number x Number. (error-type)
17
31 March 2003CS 200 Spring 200317 Start with meval (define (meval expr env) (cond ((self-evaluating? expr) expr) ((variable? expr) (environment-lookup-name expr env)) ((lambda? expr) (make-procedure (lambda-parameters expr) (lambda-body expr) env)) ((application? expr) (mapply (meval (application-operator expr) env) (map (lambda (subexpr) (meval subexpr env)) (application-operands expr)))) (else (error "Unknown expression: " exp))))
18
31 March 2003CS 200 Spring 200318 typeof (define (typeof expr env) (cond ((self-evaluating? expr) (typeof-self-evaluating expr) ((variable? expr) (typeof-variable expr env)) ((lambda? expr) (typeof-procedure expr env)) ((application? expr) (typeof-application expr env)) (else (error "Unknown expression: " exp))))
19
31 March 2003CS 200 Spring 200319 typeof-self-evaluating (define (typeof-self-evaluating expr) (cond ((number? expr) (make-number-type)) ((string? expr) (make-string-type)) ((primitive-procedure? expr) (error “Bad typeof-self-evaluating”))) (define (self-evaluating? expr) (or (number? expr) (string? expr) (primitive-procedure? expr)))
20
31 March 2003CS 200 Spring 200320 typeof (define (typeof expr env) (cond ((self-evaluating? expr) (typeof-self-evaluating expr) ((variable? expr) (typeof-variable expr env)) ((lambda? expr) (typeof-procedure expr env)) ((application? expr) (typeof-application expr env)) ((definition? expr) (typeof-definition expr env)) (else (error "Unknown expression: " exp))))
21
31 March 2003CS 200 Spring 200321 Changing Frames global environment + : # double: x: 3 parameters: x body: (lambda (x) (+ x x)) global environment +: (-> (x Number Number) Number) # double: (-> Number Number) x: Number 3 parameters: x Number body: (lambda (x) (+ x x))
22
31 March 2003CS 200 Spring 200322 Changing Frames (define (extend-environment names types values env) (make-new-environment (map (lambda (name type value) (list name type value)) names types values) env)) (define (extend-environment names values env) (make-new-environment (map (lambda (name value) (cons name value)) names values) env))
23
31 March 2003CS 200 Spring 200323 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))))) (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)))))
24
31 March 2003CS 200 Spring 200324 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)))))
25
31 March 2003CS 200 Spring 200325 typeof (define (typeof expr env) (cond ((self-evaluating? expr) (typeof-self-evaluating expr) ((variable? expr) (typeof-variable expr env)) ((lambda? expr) (typeof-procedure expr env)) ((application? expr) (typeof-application expr env)) ((definition? expr) (typeof-definition expr env)) (else (error "Unknown expression: " exp))))
26
31 March 2003CS 200 Spring 200326 typeof-application (define (typeof-application expr env) (let ((operator (typeof (application-operator expr) env))) (if (procedure-type? operator) (let ((argument-types (typelist-to-product-type (map (lambda (operand) (typeof operand env)) (application-operands expr))))) (if (type-match argument-types (procedure-type-params operator)) (procedure-type-result operator) (begin (printf "Type mismatch… " ) (make-error-type))))))) The type of an application of an operator of type params result is the result type of the operator
27
31 March 2003CS 200 Spring 200327 typeof-application (define (typeof-application expr env) (let ((operator (typeof (application-operator expr) env))) (if (procedure-type? operator) (let ((argument-types (typelist-to-product-type (map (lambda (operand) (typeof operand env)) (application-operands expr))))) (if (type-match argument-types (procedure-type-params operator)) (procedure-type-result operator) (begin (printf "Type mismatch… " ) (make-error-type))))))) But, also check the parameter types match!
28
31 March 2003CS 200 Spring 200328 typelist-to-product-type (define (typelist-to-product-type typelist) (if (null? typelist) (make-empty-type) (if (eq? (length typelist) 1) (car typelist) (make-product-type (car typelist) (typelist-to-product-type (cdr typelist))))))
29
31 March 2003CS 200 Spring 200329 type-match (define (type-match t1 t2) (cond ((number-type? t1) (number-type? t2)) ((string-type? t1) (string-type? t2)) ((procedure-type? t1) (and (procedure-type? t2) (type-match (procedure-type-params t1) (procedure-type-params t2)) (type-match (procedure-type-result t1) (procedure-type-result t2)))) ((product-type? t1) (and (product-type? t2) (type-match (product-type-first t1) (product-type-first t2)) (type-match (product-type-second t1) (product-type-second t2)))) (else (error "Bad type: " t1))))
30
31 March 2003CS 200 Spring 200330 Testing 1 2 3… > (typeof '(+ 3 4) the-global-environment) (primitive-type number) > (typeof '(+ 3 +) the-global-environment) Type mismatch. Application (+ 3 +) parameter type is (Number x Number) -> (Number) x Number, should be Number x Number. (error-type) > (typeof '(+ 3) the-global-environment) Type mismatch. Application (+ 3) parameter type is Number, should be Number x Number. (error-type)
31
31 March 2003CS 200 Spring 200331 typeof (define (typeof expr env) (cond ((self-evaluating? expr) (typeof-self-evaluating expr) ((variable? expr) (typeof-variable expr env)) ((lambda? expr) (typeof-procedure expr env)) ((application? expr) (typeof-application expr env)) (else (error "Unknown expression: " exp))))
32
31 March 2003CS 200 Spring 200332 (define (typeof-procedure expr env) (let ((params (lambda-parameters expr)) (body (lambda-body expr))) (make-procedure-type (typelist-to-product-type (map (lambda (param) (parse-type (cadr param))) params))) (typeof-sequence body (extend-environment (map (lambda (param) (car param)) params) ;; names param-types (map (lambda (param) 'unknown) params) ;; values env)))) Typing Procedures
33
31 March 2003CS 200 Spring 200333 typeof Examples > (check-type '(lambda ((x number) (y number)) (+ x y))) "(Number x Number) -> (Number)" > (check-type '(lambda ((x number) (y number)) (+ x))) Type mismatch. Application (+ x) parameter types are Number, should be Number x Number. "(Number x Number) -> (Error)" > (check-type '(lambda ((x number)) (lambda ((y number)) (+ x y)))) "(Number) -> ((Number) -> (Number))" > (check-type '((lambda ((x number)) (lambda ((y number)) (+ x y))) 1)) "(Number) -> (Number)" > (check-type '(((lambda ((x number)) (lambda ((y number)) (+ x y))) 1) 2)) "Number" > (check-type '(((lambda ((x number)) (+ x 1)) "test")) Type mismatch. Application (((lambda ((x number)) (+ x 1)) “test”) parameter type is String, should be Number. "Error" (define (check-type expr) (display-type (typeof expr the-global-environment)))
34
31 March 2003CS 200 Spring 200334 Charge Wednesday: Networking Friday: Building Dynamic Web Sites PS7 Due Friday Remember to send your PS8 team requests before 5pm today!
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.