Download presentation
Presentation is loading. Please wait.
1
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)
2
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
3
Catriel Beeri Pls/Winter 2004/5 functional-language 3 Syntax Domains and variable declarations: –Ident (identifiers), Syntax: (additions to EL)
4
Catriel Beeri Pls/Winter 2004/5 functional-language 4 In x is the formal parameter, e is the body In
5
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)
6
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
7
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
8
Catriel Beeri Pls/Winter 2004/5 functional-language 8 Comment: FL is orthogonal higher-order functions Multiple arguments can be accepted one-by-one
9
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:
10
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)
11
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?
12
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
13
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
14
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?
15
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.
16
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!)
17
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
18
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)?
19
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?
20
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?
21
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
22
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?
23
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
24
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
25
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
26
Catriel Beeri Pls/Winter 2004/5 functional-language 26 Should programs be restricted to closed ?
27
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
28
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
29
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
30
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
31
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
32
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?
33
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)
34
Catriel Beeri Pls/Winter 2004/5 functional-language 34 Variations on concrete syntax: The regions in the above include rest of program
35
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)
36
Catriel Beeri Pls/Winter 2004/5 functional-language 36 Transition Semantics: (simultaneous substitution) Natural semantics:– for you
37
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
38
Catriel Beeri Pls/Winter 2004/5 functional-language 38 Alternative rule: (this one rule is sufficient) The general case:
39
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
40
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
41
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
42
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?
43
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: !
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.