What does my program mean?

Similar documents
Overloading, Type Classes, and Algebraic Datatypes

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

CSE 3302 Programming Languages Lecture 8: Functional Programming

Lambda Calculus and Type Inference

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

Getting Started with Haskell (10 points)

(Refer Slide Time: 4:00)

CITS3211 FUNCTIONAL PROGRAMMING

Introduction to the Lambda Calculus

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

Programming Language Concepts: Lecture 19

Functional Programming. Overview. Topics. Recall λ-terms. Examples

1 Scope, Bound and Free Occurrences, Closed Terms

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

CITS3211 FUNCTIONAL PROGRAMMING. 14. Graph reduction

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

1 Introduction. 3 Syntax

Getting Started with Haskell (10 points)

Functional Programming

CS 457/557: Functional Languages

From the λ-calculus to Functional Programming Drew McDermott Posted

Lambda Calculus and Type Inference

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Urmas Tamm Tartu 2013

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Last class. CS Principles of Programming Languages. Introduction. Outline

1.3. Conditional expressions To express case distinctions like

CSCI-GA Scripting Languages

Lambda Calculus. Lecture 4 CS /26/10

The Untyped Lambda Calculus

Introduction to Functional Programming in Haskell 1 / 56

Programming Languages Lecture 14: Sum, Product, Recursive Types

Denotational Semantics. Domain Theory

Fundamental Concepts. Chapter 1

J. Barkley Rosser, 81, a professor emeritus of mathematics and computer science at the University of Wisconsin who had served in government, died

QIR Specification. Romain Vernoux. January 6, Notations 2

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

Homework 3 COSE212, Fall 2018

Exercise 1 (2+2+2 points)

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

The Substitution Model

The Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages

Propositional Logic. Part I

Exercise 1 ( = 24 points)

CS558 Programming Languages

Chapter 5: The Untyped Lambda Calculus

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Functional Programming. Big Picture. Design of Programming Languages

The type checker will complain that the two branches have different types, one is string and the other is int

Lambda Calculus and Extensions as Foundation of Functional Programming

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

Introduction to OCaml

Data Types. Prof. Clarkson Fall Today s music: Pokémon Theme by Jason Paige

Fundamentals and lambda calculus

Lambda Calculus. Variables and Functions. cs3723 1

Course Notes Equational Programming: Lambda Calculus. Femke van Raamsdonk

CS 320 Midterm Exam. Fall 2018

A Small Interpreted Language

Constraint-based Analysis. Harry Xu CS 253/INF 212 Spring 2013

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 let x = 1 in...

Recursive Types and Subtyping

Meta-programming with Names and Necessity p.1

Programming Languages Third Edition

Lecture 9: More Lambda Calculus / Types

- M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete. - true = λ u. λ v. u. - false = λ u. λ v.

Pure Lambda Calculus. Lecture 17

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

Module 6. Knowledge Representation and Logic (First Order Logic) Version 2 CSE IIT, Kharagpur

The Untyped Lambda Calculus

Exercise 1 ( = 18 points)

CSE 505: Concepts of Programming Languages

Data Types. Guest Lecture: Andrew Myers Spring 2018

Introduction to lambda calculus Part 3

Graphical Untyped Lambda Calculus Interactive Interpreter

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Functional Programming

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Proving the Genericity Lemma by Leftmost Reduction is Simple

Denotational semantics

Data Types The ML Type System

Lambda Calculus. Concepts in Programming Languages Recitation 6:

Lecture Notes on Aggregate Data Structures

Computer Science 203 Programming Languages Fall Lecture 10. Bindings, Procedures, Functions, Functional Programming, and the Lambda Calculus

Type Systems Winter Semester 2006

CSCE 314 Programming Languages

Lambda Calculus LC-1

3.4 Deduction and Evaluation: Tools Conditional-Equational Logic

Lists. Michael P. Fourman. February 2, 2010

Project 2: Scheme Interpreter

Typed Lambda Calculus

On Meaning Preservation of a Calculus of Records

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

Chapter 2 The Language PCF

To figure this out we need a more precise understanding of how ML works

Transcription:

September 16, 2015 L02-1 What does my program mean? Armando Solar Lezama Computer Science and Artificial Intelligence Laboratory M.I.T. Adapted from Arvind 2010. Used with permission. September 16, 2015

Meaning of a term The semantics must distinguish between terms that should not be equal, i.e., if September 16, 2015 0 x. y. y; 1 x. y. x y; 2 x. y. x (x y) Then x. y. y x. y. x y x. y. x (x y) The semantics must equate terms that should be equal, i.e., the terms corresponding to (plus 0 1) and 1 must have the same meaning A semantics is said to be fully abstract if two terms have different meaning according to the semantics then there exists a term that can tell them apart In the -calculus it is possible to define the meaning of a term almost syntactically L02-2

Information content of a term Instantaneous information: A term obtained by replacing each redex in a term by where stands for no information Term ( q. p.p (q a)) ( z.z) p.p (( z.z) a)) p.p a Instantaneous information p.p p.p a b-reductions monotonically increase information The meaning of a term is the maximum information that can be obtained by b-reductions September 16, 2015 L02-3

Is the meaning of a term simply its normal form? Yes, but... What if a term doesn t have a normal form? Is the meaning of such terms always? Consider = ( x.x x) ( x.x x) It doesn t have a normal form, and its meaning is What about x.x? It doesn t have a normal form, but its meaning is x.x What if a term has more than one normal form? It would have two meanings. BAD Not possible in the -calculus because of Confluence... September 16, 2015 L02-4

Can the choice of redexes lead to different meaning? A term may have multiple redexes 1. (( x.m) A) (( x.n) B) ----- ---- ---- ---- 2. (( x.m) (( y.n)b)) ---- ---- ---------- -------- followed by does not necessary produce the same term as followed by Notice in the second example can destroy or duplicate Can our choice of redexes lead us to produce terms that are clearly different (e.g., x versus y.y)? September 16, 2015 L02-5

Church-Rosser Property A reduction system is said to have the Church-Rosser property, if E E 1 and E E 2 then there exits a E 3 such that E E and E 1 3 2 E 3. E E 1 E 2 E 3 also known as CR or Confluence If a system has the CR property then the divergence in terms due to the choice of redexes can be corrected September 16, 2015 L02-6

Church-Rosser Theorem Theorem: The -calculus is CR. (Martin-Lof & Tate) No satisfactory proof of this theorem was given until 1970 (30 years later!) The proof is elegant Requires showing how two divergent terms can be brought together in finite number of steps strategy for choosing reductions CR implies that if NF exists it is unique September 16, 2015 L02-7

Interpreters An interpreter for the -calculus is a program to reduce -expressions to answers. Requires: the definition of an answer - e.g., normal form? a reduction strategy - a method to choose redexes in an expression September 16, 2015 L02-8

September 16, 2015 L02-9 Definitions of Answers Normal form (NF): an expression without redexes Head normal form (HNF): x is HNF ( x.e) is in HNF if E is in HNF (x E 1... E n ) is in HNF Semantically most interesting- represents the information content of an expression Weak head normal form (WHNF): An expression in which the left most application is not a redex. x is in WHNF ( x.e) is in WHNF (x E 1... E n ) is in WHNF Practically most interesting Printable Answers

Two Common Reduction Strategies applicative order: right-most innermost redex aka call by value evaluation normal order: left-most (outermost) redex aka call by name evaluation ( x.y) (( x.x x) ( x.x x)) 1 2 applicative order normal order September 16, 2015 L02-10

September 16, 2015 L02-11 Computing a normal form 1. Every -expression does not have an answer i.e., a NF or HNF or WHNF ( x.x x) ( x.x x) = 3. Even if an expression has an answer, not all reduction strategies may produce it ( x. y.y) leftmost redex: ( x. y.y) y.y innermost redex: ( x. y.y) ( x. y. y)...

September 16, 2015 L02-12 Normalizing Strategy A reduction strategy is said to be normalizing if it terminates and produces an answer of an expression whenever the expression has an answer. aka the standard reduction Theorem: Normal order (left-most) reduction strategy is normalizing for the -calculus.

September 16, 2015 L02-13 A Call-by-name Interpreter Answers: Strategy: WHNF leftmost redex Apply the function before evaluating the arguments cn(e): Definition by cases on E E = x x.e E E [...] represents syntax [[...]] is my ppt approx cn([[x]]) = x cn([[ x.e]]) = x.e cn([[e 1 E 2 ]]) = let f = cn(e 1 ) in case f of x.e 3 = _ = Meta syntax cn(e 3 [E 2 /x]) f E 2

September 16, 2015 L02-14 A Call-by-value Interpreter Answers: Strategy: cv(e): WHNF rightmost-innermost redex but not inside a -abstraction Evaluate the argument before applying the function Definition by cases on E E = x x.e E E cv([[x]]) = x cv([[ x.e]]) = x.e cv([[e 1 E 2 ]]) = let f = cv(e 1 ) a = cv(e 2 ) in case f of x.e 3 = _ = cv(e 3 [a/x]) f a

Coding this in Haskell with Algebraic Data Types September 16, 2015 L02-15

September 20, 2011 L02-16 Algebraic types Algebraic types are tagged unions of products Example keyword data Shape = Line Pnt Pnt Triangle Pnt Pnt Pnt Quad Pnt Pnt Pnt Pnt "union" new type "products (fields) - new "constructors" (a.k.a. "tags", "disjuncts", "summands") - a k-ary constructor is applied to k type expressions

September 20, 2011 L02-17 Examples of Algebraic types data Bool = False True data Day = Sun Mon Tue Wed Thu Fri Sat data Maybe a = Nothing Just a data List a = Nil Cons a (List a) data Tree a = Leaf a Node (Tree a) (Tree a) data Tree a b = Leaf a Nonleaf b (Tree a b) (Tree a b) data Course = Course String Int String (List Course) name number description pre-reqs

September 20, 2011 L02-18 Constructors are functions Constructors can be used as functions to create values of the type let in l1 :: Shape l1 = Line e1 e2 t1 :: Shape = Triangle e3 e4 e5 q1 :: Shape = Quad e6 e7 e8 e9... where each "ej" is an expression of type "Pnt"

September 20, 2011 L02-19 Pattern-matching on algebraic types Pattern-matching is used to examine values of an algebraic type anchorpnt :: Shape -> Pnt anchorpnt s = case s of Line p1 p2 -> p1 Triangle p3 p4 p5 -> p3 Quad p6 p7 p8 p9 -> p6 A pattern-match has two roles: A test: "does the given value match this pattern?" Binding ("if the given value matches the pattern, bind the variables in the pattern to the corresponding parts of the value") Clauses are examined top-to-bottom and left-to-right for pattern matching

September 16, 2015 L02-20 Big Step Semantics

Big Step Operational Semantics Model the execution in an abstract machine Basic Notation: Judgments Some alternative notations: describe how a program configuration is evaluated into a result the configuration is usually a program fragment together with any state. Basic Notation: Inference rules define how to derive judgments for an arbitrary program also called derivation rules or evaluation rules usually defined recursively L02-21

What evaluation order is this? Call by Name cn([[x]]) = x cn([[ x.e]]) = x.e cn([[e 1 E 2 ]])= let f = cn(e 1 ) in case f of x.e 3 = cn(e 3 [E 2 /x]) _ = f E 2 Do these semantics coincide? L02-22

What evaluation order is this? Call by Value cv([[x]]) = x cv([[ x.e]]) = x.e cv([[e 1 E 2 ]])= let f = cv(e 1 ) a = cv(e 2 ) in case f of x.e 3 = cv(e 3 [a/x]) _ = f a L02-23

Recursion and the Y Combinator L02-24

September 16, 2015 L02-25 Recursion and Fixed Point Equations Recursive functions can be thought of as solutions of fixed point equations: fact = n. Cond (Zero? n) 1 (Mul n (fact (Sub n 1))) Suppose then H = f. n.cond (Zero? n) 1 (Mul n (f (Sub n 1))) fact = H fact fact is a fixed point of function H!

September 16, 2015 L02-26 Fixed Point Equations f : D D A fixed point equation has the form f(x) = x Its solutions are called the fixed points of f because if x p is a solution then x p = f(x p ) = f(f(x p )) = f(f(f(x p ))) =... We want to consider fixed-point equations whose solutions are functions, i.e., sets that contain their function spaces domain theory, Scottary,...

September 16, 2015 L02-27 An example Consider f n = if n=0 then 1 else (if n=1 then f 3 else f (n-2)) H = f. n.cond(n=0, 1, Cond(n=1, f 3, f (n-2)) Is there an f p such that f p = H f p? f1 n = 1 if n is even = otherwise f2 n = 1 if n is even = 5 otherwise f1 contains no arbitrary information and is said to be the least fixed point (lfp) Under the assumption of monotonicity and continuity least fixed points are unique and computable

September 16, 2015 L02-28 Computing a Fixed Point Recursion requires repeated application of a function Self application allows us to recreate the original term Consider: = ( x. x x) ( x. x x) Notice b-reduction of leaves : Now to get F (F (F (F...))) we insert F in : F = ( x.f (x x)) ( x.f (x x)) which b-reduces to: F F( x. F(x x))( x. F(x x)) F F F(F F ) F(F(F F ))... Now -abstract F to get a Fix-Point Combinator: Y f.( x. (f (x x))) ( x.(f (x x)))

September 16, 2015 L02-29 Y : A Fixed Point Operator Y f.( x. (f (x x))) ( x.(f (x x))) Notice Y F ( x.f (x x)) ( x.f (x x)) F ( x.f (x x)) ( x.f (x x)) F (Y F) F (Y F) = Y F (Y F) is a fixed point of F Y computes the least fixed point of any function! There are many different fixed point operators.

September 16, 2015 L02-30 Mutual Recursion odd n = if n==0 then False else even (n-1) even n = if n==0 then True else odd (n-1) odd = H 1 even even = H 2 odd where H 1 = f. n.cond(n=0, False, f(n-1)) H 2 = f. n.cond(n=0, True, f(n-1)) substituting H 2 odd for even odd = H 1 (H 2 odd) = H odd where H = odd = Y H Can we express odd using Y? f. H 1 (H 2 f)

September 16, 2015 L02-31 Self-application and Paradoxes Self application, i.e., (x x) is dangerous. Suppose: u y. if (y y) = a then b else a What is (u u)? (u u) if (u u) = a then b else a Contradiction!!! Any semantics of -calculus has to make sure that functions such as u have the meaning, i.e. totally undefined or no information. Self application also violates every type discipline.

September 16, 2015 L02-32 -calculus with Combinator Y Recursive programs can be translated into the -calculus with constants and combinator Y. However, Y violates every type discipline translation is messy in case of mutually recursive functions extend the -calculus with recursive let blocks. The let Calculus

MIT OpenCourseWare http://ocw.mit.edu 6.820 Fundamentals of Program Analysis Fall 2015 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.