David Evans CS200: Computer Science University of Virginia Computer Science Lecture 27: Types of Types “It would appear.

Slides:



Advertisements
Similar presentations
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.
Advertisements

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Scheme in Scheme?!? …the metacircular evaluator….
1 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 23: Inheritance.
מבוא מורחב למדעי המחשב תרגול 13 Lazy Evaluation. Lazy Evaluation Implementation Two types of values – Delayed Values – Actual Values Introduce two functions.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 31: Types of Types.
1 The Metacircular Evaluator Chapter 4 Section 4.1 We will not cover every little detail in the lectures, so you MUST read Section 4.1 in full.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Metacircular Evaluator 4.1, pages definitions file on web 2.
(define applicative-eval (lambda (exp) (cond ((atomic? exp) (eval-atomic exp)) ((special-form? exp) (eval-special-form exp)) ((list-form? exp) (eval-list.
1 Lecture 18 Continue Evaluator. 2 z9 true#t + twice Representing procedures (eval '(define twice (lambda (x) (+ x x))) GE) symbol primitive scheme procedure.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 26: In Praise of Idleness.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Techniques for language design: Interpretation: eval/apply Semantics vs. syntax Syntactic.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Scheme More MCE examples. Q1 new special form which defines global variables (static ) search the global environment – Variable exists: does nothing,
SICP Variations on a Scheme (2) Beyond Scheme – designing language variants: Lazy evaluation Complete conversion – normal order evaluator Upward.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 28: Implementing Interpreters.
The environment model evaluator and compiler 1 The env model evaluator Motivation In one word: Efficiency Saves repeated renaming and substitution: Using.
1 Saves repeated renaming and substitution: explicit substitution is replaced by variable bindings using new data structures (frame, environment). Can.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
1 Continue Evaluator. 2 z9 true#t + twice Representing procedures (eval '(define twice (lambda (x) (+ x x))) GE) symbol primitive scheme procedure + symbol.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is aa good way to learn more about programming languages  Interpreters are.
1 The metacircular evaluator (Cont.) Defining new procedures (define (lambda? e) (tag-check e 'lambda)) (define (eval exp env) (cond ((number? exp)
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 15: Meta-Circular Evaluator 한 태숙.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
Lecture 31: Laziness University of Virginia cs1120 Fall 2009 David Evans.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 30: Laziness.
The environment-based operational semantics Chapter
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 29: Typed Scheme MC Escher, Liberation.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 14: Types of Types “It would.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
1 Lecture 20 Lazy Evaluation Continued (4.2.1, 4.2.2) MC-eval examples from exams (Time permitting)
1 Read-Eval-Print Loop (define (driver-loop) (prompt-for-input input-prompt) (let ((input (read))) (let ((output (eval input the-global-env))) (announce-output.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
1 Env. Model Implementation & Analyzer Practice Session #10 Env. Model Implementation & Analyzer Practice Session #10.
1 Lecture 19 Dynamic Scoping Lazy Evaluation (4.2.1, 4.2.2)
1/ SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Making the environment model concrete Defining eval defines the language –Provides.
CS61A Lecture Colleen Lewis 1. Clicker poll Are you in a two person group for project 4? A)Yes – I have my partner B)No – I plan to work.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
(Thunking about Thunks)
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
6.001 SICP Variations on a Scheme
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Env. Model Implementation
Original material by Eric Grimson
Class 19: Think Globally, Mutate Locally CS150: Computer Science
The Metacircular Evaluator
Lecture 28: Types of Types
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
Lecture 24: Metalinguistics CS200: Computer Science
The Metacircular Evaluator (Continued)
Lecture 27: In Praise of Idleness CS200: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
6.001 SICP Variations on a Scheme
6.001 SICP Interpretation Parts of an interpreter
topics interpreters meta-linguistic abstraction eval and apply
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
David Evans Lecture 32: Truthiness “It would appear that we have reached the limits of what it is possible to achieve.
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

David 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

31 March 2003CS 200 Spring Menu Types and Type Checking Typed Scheme

31 March 2003CS 200 Spring 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

31 March 2003CS 200 Spring 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

31 March 2003CS 200 Spring 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.

31 March 2003CS 200 Spring 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)

31 March 2003CS 200 Spring 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

31 March 2003CS 200 Spring 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.

31 March 2003CS 200 Spring What do we need to do change our Mini-Scheme evaluator to provide Java-like type checking?

31 March 2003CS 200 Spring Types in Mini-Scheme Type ::= PrimitiveType Type ::= ProcedureType Type ::= ProductType ProcedureType ::= Type  Type ProductType ::= Type x Type PrimitiveType ::= Number | String

31 March 2003CS 200 Spring 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*)

31 March 2003CS 200 Spring 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

31 March 2003CS 200 Spring 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)))

31 March 2003CS 200 Spring 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!")))

31 March 2003CS 200 Spring 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

31 March 2003CS 200 Spring 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)

31 March 2003CS 200 Spring 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))))

31 March 2003CS 200 Spring 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))))

31 March 2003CS 200 Spring 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)))

31 March 2003CS 200 Spring 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))))

31 March 2003CS 200 Spring 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))

31 March 2003CS 200 Spring 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))

31 March 2003CS 200 Spring 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)))))

31 March 2003CS 200 Spring 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)))))

31 March 2003CS 200 Spring 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))))

31 March 2003CS 200 Spring 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

31 March 2003CS 200 Spring 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!

31 March 2003CS 200 Spring 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))))))

31 March 2003CS 200 Spring 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))))

31 March 2003CS 200 Spring 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 March 2003CS 200 Spring 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))))

31 March 2003CS 200 Spring (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

31 March 2003CS 200 Spring 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)))

31 March 2003CS 200 Spring Charge Wednesday: Networking Friday: Building Dynamic Web Sites PS7 Due Friday Remember to send your PS8 team requests before 5pm today!