1 / 21

David Evans cs.virginia/~evans

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. David Evans

jmueller
Download Presentation

David Evans cs.virginia/~evans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 David Evans http://www.cs.virginia.edu/~evans CS200: Computer Science University of Virginia Computer Science

  2. Menu • Types and Type Checking • Typed Scheme CS 200 Spring 2002

  3. Types Strings Numbers • Type is a (possibly infinite) set of values • You can do some things with some types, but not others programs that halt Colors Beatle’s Songs that don’t end on the Tonic lists of lists of lists of anything CS 200 Spring 2002

  4. 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) CS 200 Spring 2002

  5. 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. CS 200 Spring 2002

  6. 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) CS 200 Spring 2002

  7. Scheme  Java • Scheme has Latent, Dynamic types • Java has Manifest, Static types CS 200 Spring 2002

  8. Java Example class Test { int tester (String s) { int x; x = s; return "okay"; } } 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 result is an integer x is an integer CS 200 Spring 2002

  9. Java Example class Test { int tester (String s) { int x; tester (“hello”); x = s; return "okay"; } } 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 CS 200 Spring 2002

  10. What do we need to do change our Mini-Scheme evaluator to provide Java-like type checking? CS 200 Spring 2002

  11. Types in Mini-Scheme Type ::= PrimitiveType Type ::= ProcedureType Type ::= ProductType ProcedureType ::= TypeType ProductType ::= TypexType PrimitiveType ::= Number | String CS 200 Spring 2002

  12. Type ::= PrimitiveType | ProcedureType | ProductType ProcedureType ::= TypeType ProductType ::= TypexType PrimitiveType ::= Number | String Examples 3 Number + Number x Number  Number (+ 3 3) Number (lambda ((x number) (y number)) (+ x y)) Number x Number  Number Changed lambda form: Expression ::= (lambda(((Name Type))*)Expr*) CS 200 Spring 2002

  13. 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 CS 200 Spring 2002

  14. Type ::= PrimitiveType | ProcedureType | ProductType ProcedureType ::= TypeType ProductType ::= TypexType PrimitiveType ::= Number | String Representing Types (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))) CS 200 Spring 2002

  15. Type ::= PrimitiveType | ProcedureType | ProductType ProcedureType ::= TypeType ProductType ::= TypexType PrimitiveType ::= Number | String Representing Types (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!"))) CS 200 Spring 2002

  16. Type of + (make-procedure-type (make-product-type (make-number-type) (make-number-type)) (make-number-type)) CS 200 Spring 2002

  17. Changing Frames global environment global environment +: (-> (x Number Number) Number) #<primitive:+> + : #<primitive:+> double: x: 3 double: (-> Number Number) x: Number 3 parameters: xbody: (lambda (x) (+ x x)) parameters: x Numberbody: (lambda (x) (+ x x)) CS 200 Spring 2002

  18. 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)) CS 200 Spring 2002

  19. 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))))) CS 200 Spring 2002

  20. 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))))) CS 200 Spring 2002

  21. 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 CS 200 Spring 2002

More Related