Catriel Beeri Pls/Winter 2004/5 functional-language 1 Substitution Semantics of FL – a simple functional language FL is EL + (non-recursive) function creation.

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Substitution & Evaluation Order cos 441 David Walker.
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Elements of Lambda Calculus Functional Programming Academic Year Alessandro Cimatti
1 541: Relational Calculus. 2 Relational Calculus  Comes in two flavours: Tuple relational calculus (TRC) and Domain relational calculus (DRC).  Calculus.
Chapter 5 ( ) of Programming Languages by Ravi Sethi
Winter 2004/5Pls – expresssion-lang – Catriel Beeri22 Informal Analysis Goal: prove the semantics is deterministic Stages: Values --- we know they have.
Catriel Beeri Pls/Winter 2004/5 last 55 Two comments on let polymorphism I. What is the (time, space) complexity of type reconstruction? In practice –
Foundations of Programming Languages: Introduction to Lambda Calculus
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 3 LECTURE: LAMBDA CALCULUS PRACTICAL/TUTORIAL: (i) Do exercises given out.
1 Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Winter 2004/5Pls – inductive – Catriel Beeri1 Inductive Definitions (our meta-language for specifications)  Examples  Syntax  Semantics  Proof Trees.
Catriel Beeri Pls/Winter 2004/5 environment 68  Some details of implementation As part of / extension of type-checking: Each declaration d(x) associated.
Catriel Beeri Pls/Winter 2004/5 inductive-revisited 1 Inductive definitions revisited  Generated and Freely generated sets oPattern match, unification.
Catriel Beeri Pls/Winter 2004/5 interpreters 1 Language processors for EL  An interpreter transEval from  An interpreter natEval from An important theme:
Chair of Software Engineering 1 Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan.
Catriel Beeri Pls/Winter 2004/05 types 65  A type-checking algorithm The task: (since we start with empty H, why is the goal not just E?) The rule set.
Catriel Beeri Pls/Winter 2004/5 environment1 1 The Environment Model  Introduction and overview  A look at the execution model  Dynamic scoping  Static.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Winter 2004/5Pls – analysis – Catriel Beeri1 A precise analysis  Determinism, soundness, completeness for  Determinism for  Additional properties of.
Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Winter 2003/4Pls – syntax – Catriel Beeri1 SYNTAX Syntax: form, structure The syntax of a pl: The set of its well-formed programs The rules that define.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
Catriel Beeri Pls/Winter 2004/5 environment 19 II. Frames and frame structures Frame – set of bindings generated together in a binding generation event.
Rutgers University Relational Calculus 198:541 Rutgers University.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
The Relational Model: Relational Calculus
10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Lambda Calculus History and Syntax. History The lambda calculus is a formal system designed to investigate function definition, function application and.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
1 ML fun x -> e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors let f x = e variables These.
1 Relational Algebra and Calculas Chapter 4, Part A.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.
-Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Comp 311 Principles of Programming Languages Lecture 4 The Scope of Variables Corky Cartwright September 3, 2008.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications Chapter.
CMSC 330: Organization of Programming Languages Operational Semantics.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4, Part B.
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
CSE-321 Programming Languages -Calculus (II) POSTECH March 26, 2007 박성우.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
6/21/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
1 Proving Properties of Recursive List Functions CS 270 Math Foundations of CS Jeremy Johnson.
Operational Semantics of Scheme
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Corky Cartwright January 18, 2017
Programming Languages and Compilers (CS 421)
FP Foundations, Scheme In Text: Chapter 14.
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
L Calculus.
CSE S. Tanimoto Lambda Calculus
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Presentation transcript:

Catriel Beeri Pls/Winter 2004/5 functional-language 1 Substitution Semantics of FL – a simple functional language FL is EL + (non-recursive) function creation (and application) + local, non-recursive definitions (let)

Catriel Beeri Pls/Winter 2004/5 functional-language 2  Syntax  Semantics of unary function application  Properties of semantics  Errors and error rules  Closed expressions  Replacement of formal parameters  Multi-argument functions and let  Some history – the pure lambda calculus

Catriel Beeri Pls/Winter 2004/5 functional-language 3  Syntax Domains and variable declarations: –Ident (identifiers), Syntax: (additions to EL)

Catriel Beeri Pls/Winter 2004/5 functional-language 4 In x is the formal parameter, e is the body In

Catriel Beeri Pls/Winter 2004/5 functional-language 5 In abstract syntax trees: Two new node labels: lambda x (or lambda (x 1 …x n )) ( a parametrized construct) let Q: there is no new function application label– why? Values: v ::= … | ( functions are values)

Catriel Beeri Pls/Winter 2004/5 functional-language 6 For simplicity, we consider now only unary functions n-ary functions and let are treated later

Catriel Beeri Pls/Winter 2004/5 functional-language 7 Convention for concrete syntax: captures as far to the right as possible (. has low priority) Application is left-associative Function application has highest priority

Catriel Beeri Pls/Winter 2004/5 functional-language 8 Comment: FL is orthogonal  higher-order functions Multiple arguments can be accepted one-by-one

Catriel Beeri Pls/Winter 2004/5 functional-language 9 To formulate the semantics, we need to define: region for, then substitution  Semantics of unary function application The idea: substitute value of actual parameter for uses of formal parameter in body:

Catriel Beeri Pls/Winter 2004/5 functional-language 10 in is a declaration (binding occurrence) other occurrences are uses The region of in is Its scope is the region, excluding each nested Notions of free/bound are as in previous chapter Declarations, regions and scope in FL (unary functions for now)

Catriel Beeri Pls/Winter 2004/5 functional-language 11 Formally: (an inductive definition) A use of x is free in e, case of e: ident : x is free in x (what about ‘in y’?) (what about constants?) tuple : each free use of x in e i is free in (e 1,…, e n ) if : each free use of x in e i i=1,…3 is free in if e 1 e 2 e 3 applic : each free use of x in e i i= 1,2, is free in e 1 e 2 lambda : each free use of x in e is free in Every other use is bound in e (to what?) Q: Why concentrate on free above?

Catriel Beeri Pls/Winter 2004/5 functional-language 12 Substitution Notation: [g/x]e – the (careful) substitution of expression g for all free uses of x in e Note: [g/x]e is our notation, of the meta-language, not of FL Why careful? The naïve approach: Find each free use of x in e, replace it by g Here, the declaration of y captures a free use of y in g Careful substitution avoids such problems

Catriel Beeri Pls/Winter 2004/5 functional-language 13 Def: [g/x]e is defined by case of e: For application, tuple, if, simply push the substitution into the composite expressions

Catriel Beeri Pls/Winter 2004/5 functional-language 14 Now, the lambda case (So far, this follows the inductive def. of free, and is same as naive) But, what if g contains free occurrences of y?

Catriel Beeri Pls/Winter 2004/5 functional-language 15 Here, we first replace y by a new z, then we can safely substitute, w/o danger of capture “new” – does not occur in the expressions Replacement: the actual name of a formal is irrelevant. (a formal parameter is a “place holder”) More on replacement, and this case of [g/x]e – later.

Catriel Beeri Pls/Winter 2004/5 functional-language 16 The rules of the semantics: Transition: One additional rule: often called rule This is a new kind of redex - a beta-redex (of form v 1 v 2 where the operator is a function) Rule complements axiom (apply-op) This is call-by-value (cbv) semantics: argument must be evaluated before application (can also say that function application is strict) (not what is called in Pascal call by value!)

Catriel Beeri Pls/Winter 2004/5 functional-language 17 Natural: a function expression is a value The second rule complements the rule (nat-applic-op) for application expressions Also call-by-value

Catriel Beeri Pls/Winter 2004/5 functional-language 18 Comments: 1.Def. of region, scope, substitution (static structure) are part of the semantics 2.There is no binding management: an association between a parameter and a value is immediately used for substitution, then discarded 3.Approach is easy to explain, intuitive 4.But, inefficient; real implementations use the environment model Q: If you are given an expression, will you be able to write the computation from it (or at least n steps)?

Catriel Beeri Pls/Winter 2004/5 functional-language 19  Properties of semantics All definitions made for  for EL can be extended some minor changes/comments: --- (only) new kind of redex ( a (beta)-redex ) never an error, reduction to always possible selection path never descends to a lambda expression The selected sub-expression may be a free variable Q: What are now all forms of selected sub-expressions?

Catriel Beeri Pls/Winter 2004/5 functional-language 20 ( with rules of ) is deterministic, is (of course) non-deterministic Is there any new source of non-determinism? We use (except where important) There exist infinite computations! Consider Does it have a natural semantics proof tree?

Catriel Beeri Pls/Winter 2004/5 functional-language 21 The connections between stated for EL still hold; so does confluence (left to you) Interpreters can be constructed; some additions: Need a procedure for substitution, including : –Generate fresh identifiers when needed –Replace name of formal parameter of a lambda, when needed (last sub-case) application has now two rules in natural semantics. This is solved as the case of if

Catriel Beeri Pls/Winter 2004/5 functional-language 22  Errors and error rules A (beta)-redex is never an error is an error (no need for error rule -why?) What about ? The error axiom (apply-error) needs to be changed --- how? (a new domain: Fun) What do we assume now about the module builtIn of built-in operations?

Catriel Beeri Pls/Winter 2004/5 functional-language 23  A variable as the selected sub-expression is a run- time error : free variable error Such an error is always a free use -- why? But, it is not the case that if E contains a free use, then an error will occur after some steps! Should free variables excluded from legal programs? A variable is not a value  can be selected sub-expression not a redex  there is no transition from it

Catriel Beeri Pls/Winter 2004/5 functional-language 24 Here is a proposed error axiom: (var-error) x  ER Is it correct (sound) ? Is it sufficient (completeness)? Consider each of  d,  What rule(s) for ? The new sets: Determinism (for  d), soundness, completeness/progress can be proved as for EL

Catriel Beeri Pls/Winter 2004/5 functional-language 25  Closed expressions An expression w/o free variables is closed Claim: If E is closed, and E  E’, then E’ is closed (If E is closed, and its selected sub-expression is then both sides are closed) Corollary: If E is closed, and E  * E’, then E’ is closed (hence last sub-case of [g/x]e is never needed for closed expressions) Corollary: A computation from closed E never raises free var error

Catriel Beeri Pls/Winter 2004/5 functional-language 26 Should programs be restricted to closed ?

Catriel Beeri Pls/Winter 2004/5 functional-language 27  Replacement of formal parameters Rule : actual name of a formal parameter is irrelevant (a parameter is just a place holder) What do we mean by irrelevant? 1. The relation on expressions defined by is an equivalence and a congruence relation Identical, except for some formal parameters, and the uses bound to them 2. Equivalent expressions  same semantics

Catriel Beeri Pls/Winter 2004/5 functional-language 28 Def: if They have same labeled tree structure (internal nodes) The same constant and free use leaves (same vars!) The same bound-to relation on remaining leaves (thus, bound leaves are split into same partitions) x y+ x y x z+ x z w y+ w y Explore free variables,, a DAG structure

Catriel Beeri Pls/Winter 2004/5 functional-language 29 Claim: (alpha) is an equivalence & a congruence Claim: (same semantics) The above: a declarative def of alpha

Catriel Beeri Pls/Winter 2004/5 functional-language 30 Procedural replacement: Can change in a sub-expression of E the parameter x to y, provided y does not occur free in E’ (capture!) To do that, replace it by Is this guaranteed to produce an expression? Are there any other captures that should be avoided? Here and now is the place for you to look at the last case of substitution again Summary: Replacement of bound variables exists in all formal calculi, and its correct handling is always an (unavoidable) headache

Catriel Beeri Pls/Winter 2004/5 functional-language 31  Multi-argument functions & let Two ways (at least) to add n-ary functions to a language with tuples and unary functions 1.add/assume projection operations #i: #i(v 1,…, v n ) = v i This can be used in the body of functions to take apart tuple arguments Not very convenient

Catriel Beeri Pls/Winter 2004/5 functional-language 32 2.Allow tuple formal parameters (x 1,…, x n ). these can be used in the body Our approach, can be viewed as syntactic sugar for 1. Needs: define region, scope, then: simultaneous (careful) substitution of n expressions for n variables in an expression Left to you Q: what about projection functions in this approach?

Catriel Beeri Pls/Winter 2004/5 functional-language 33 (non-recursive) definitions: let, let* (alternatively: letpar, letseq) let x = 3, y = 5 in x+y  * 8 let x = 3, y = 5 in let x = y+1, y = x –1 in x-y  * 4 let x = 3, y = 5 in let* x = y+1, y = x –1 in x-y  * 1 let – a parallel definition block let* - a sequential definition block (definable from let)

Catriel Beeri Pls/Winter 2004/5 functional-language 34 Variations on concrete syntax: The regions in the above include rest of program

Catriel Beeri Pls/Winter 2004/5 functional-language 35 Formally: Alternatively: We use the former (despite using tuples for functions) let – a binding construct Region of each defined variable: the body Scope: now need to account for holes in region due to both lambda and let (both for parameters and for for let- defined variables)

Catriel Beeri Pls/Winter 2004/5 functional-language 36 Transition Semantics: (simultaneous substitution) Natural semantics:– for you

Catriel Beeri Pls/Winter 2004/5 functional-language 37 Is let a really new, independent,construct? In both, x is bound to value of e, binding valid in body  let is syntactic sugar, equivalent (region, semantics) to application of anonymous function to argument let xebody x e applic

Catriel Beeri Pls/Winter 2004/5 functional-language 38  Alternative rule: (this one rule is sufficient) The general case:

Catriel Beeri Pls/Winter 2004/5 functional-language 39 let* is definable from let. Still, it is a good idea to formulate its region, semantics, and type rules Left to you Hint for the semantics: Deal with the variables one by one

Catriel Beeri Pls/Winter 2004/5 functional-language 40 Now that we have let and let* in addition to lambda, what are the new errors (if any) ? Q: Can one use let or let* to define recursive functions? A: Think of the region, scope, and the tranisition semantics

Catriel Beeri Pls/Winter 2004/5 functional-language 41  Some history: The pure lambda-calculus Invented by A. Church in the late 1930’s Contains only function creation (lambda) and application --- no atomic types/built-in ops The semantics (next page) uses rewriting and substitution (just like ours) It was the starting point for functional programming languages

Catriel Beeri Pls/Winter 2004/5 functional-language 42 Church’s semantics for the pure lambda-calculus (and rule alpha) How does this set of rules differ from ours?

Catriel Beeri Pls/Winter 2004/5 functional-language 43 Q: What can one do in a language that contains only functions?? A: Church proved it is Turing-complete Q: How can a language be Turing complete w/o recursion? A: !