Lecture Notes: Chapter 2

Slides:



Advertisements
Similar presentations
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Advertisements

Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Programming Languages and Paradigms
Relational Algebra Dashiell Fryer. What is Relational Algebra? Relational algebra is a procedural query language. Relational algebra is a procedural query.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Type Checking Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic.
Compiler Construction
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 31: Types of Types.
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
1 Functional languages (e.g. Scheme, ML) Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Pairs, lists and data abstraction. Data Abstraction? An interface: separate implementation from usage Think of the Map interface in Java: we know.
PPL Pairs, lists and data abstraction. Compound Data Until now: atomic, unrelated entities Now: organized into structures Why? – Better conceptual level.
Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
PPL Static Verification: Type Inference Lecture Notes: Chapter 2.
Principles of Programming Languages Lecture 1 Slides by Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
Chapter 8 - Control II: Procedures and Environments
Chapter 2: Data Abstraction 2.2 An Abstraction for Inductive Data Types Recall inductive BNF definition of binary trees: ::= | ( ) To write programs using.
PPL Applicative and Normal Form Verification, Type Checking and Inference.
Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
Type checking and inference Question 2: Typing the application (if #t (+ 1 2) 3) STAGE-I: Renaming ExpressionVariable (if #t (+ 1 2) 3) T0 (+ 1 2) T1 +
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Data Structures: Lists and Pairs Iteration vs. Recursion If we have time: live.
PPL Leftovers: LET Syntax & Formal Semantics Static Verification: Type Inference Lecture Notes: Chapter 2.
Functional Programming
Type Correctness.
Chapter 11 - Functional Programming, Part I: Concepts and Scheme
CS 3304 Comparative Languages
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
3 Introduction to Classes and Objects.
ML: a quasi-functional language with strong typing
CS 326 Programming Languages, Concepts and Implementation
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
6.001 SICP Object Oriented Programming
The interpreter.
Introduction to Scheme
CS 326 Programming Languages, Concepts and Implementation
Chapter 2: Data Abstraction 2
Chapter 15 – Functional Programming Languages
Type checking and inference
Final Review In Text: Chapters 1-3, 5-11,
Chapter 4: Types.
Midterm Review In Text: Chapters 1-3, 5-7, 15, 16.
Final Review In Text: Chapters 1-11,
Final Review In Text: Chapters 1-3, 5-10, 12,
Final Review In Text: Chapters 1-3, 5-10, 12,
Final Review In Text: Chapters 1-3, 5-10,
Midterm Review In Text: Chapters 1-3, 5-9, 15.
Final Review In Text: Chapters 1-3, 5-10,
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Midterm Review In Text: Chapters 1-3, 5-10, 15.
Final Review In Text: Chapters 1-3, 5-12,
Announcements HW4 due today. There is no HW5. (It was merged into HW4.) HW6 (Scheme, Problem 1) out today, due October 23rd We’ll cover 100% of what you.
The Metacircular Evaluator (Continued)
Practice session #4: Static Type Correctness Algorithms:
Final Review In Text: Chapters 1-3, 5-16.
6.001 SICP Variations on a Scheme
Announcements Quiz 5 HW6 due October 23
Final Review In Text: Chapters 1-3, 5-16.
Midterm Review In Text: Chapters 1-3, 5-9, 15.
Introduction to the Lab
Midterm Review In Text: Chapters 1-3, 5-11, 15.
Presentation transcript:

Lecture Notes: Chapter 2 Types Lecture Notes: Chapter 2

Midterm 2011 נתונות הפרוצדורות foo ו-goo: (define foo (lambda (x) (display x) (newline) (+ x 2))) (define goo (lambda (x) (display 5) (newline) (+ x 1))) א. בחישוב של הביטוי > (foo (goo 0)) התקבל הפלט הבא על המסך: 5 1 3 מהי שיטת החישוב של האינטרפרטר? סמן בעיגול תשובה יחידה. 1. applicative order 2. normal order

Types Recall values Type is a set of values that have stuff in common Since every expression has a value, one can associate an expression with a type We can identify the type of an expression

Types: Why? Prevent nonsense computations Example: > (+ ((lambda (x) x) (lambda (x) x)) 4) +: expects type <number> as 1st argument, given: #<procedure:x>; other arguments were: 4

Well Typeness & Well Typeness Rules A set of rules that make sure the types of expression do not conflict Previous expression is not well typed

Another Example > (define x 4) > (+ 3 (if (> x 0) (+ x 1) 'non-positive)) 5 > (define x 0) > (+ 3 (if (> x 0) (+ x 1) 'non-positive)) +: expects type <number> as 2nd argument, given: "non-positive value"; other arguments were: 3 The if expression is no well typed.

Static and Dynamic Typing Static: type checking before runtime Java, C#, C++, ML Dynamic: type checking at runtime Scheme, JS, Python

Type Classification Primitive Atomic Composite Built in User cannot add new Atomic Sets of atomic values Composite Composed of others

Specification of a Type Values Value constructor Identification predicates Equality predicates Operations

Atomic Types So far: Number, Boolean, Symbol and Void Specifications: c’mone…

Composite Types So far: Procedure, Pair and List Type constructor

Procedure Types Type constructor: -> Value constructor: lambda [t1 * … * tn -> t] denotes type of all n-ary procedures [Empty -> t] type of parameter-less procedures Value constructor: lambda ID predicate: procedure? Equality predicate: none Challenge is to find the type of the procedure

Pair Types Type constructor: Pair: Value constructor: cons: Pair(t1, t2) Value constructor: cons: ID predicate: pair? Equality predicate: equal?

List Types Type constructor: Value constructor: cons, list Homogeneous: List(t) Heterogeneous: List Value constructor: cons, list ID predicate: list? Equality predicate: equal?

Type Language for Scheme We need a language to describe inferred types (and for writing contracts…) For simplicity we do not include union Tuple type: a composite type: Type constructors: *, Empty

BNF Grammar of the Type Language Type -> ’Void’ | Non-void Non-void -> Atomic | Composite | Type-variable Atomic -> ’Number’ | ’Boolean’ | ’Symbol’ Composite -> Procedure | Pair | List Procedure -> ’[’ Tuple ’->’ Type ’]’ Tuple -> (Non-void ’*’ )* Non-void | ’Empty’ Pair -> ’Pair’ ’(’ Non-void ’,’ Non-void ’)’ List -> ’List’ ’(’ Non-void ’)’ | ’List’ Type-variable -> A symbol starting with an upper case letter פרוצדורה לא יכולה לקבל Void פרוצדורה לא יכולה להחזיר Empty.