Some more thoughts on let Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. September 21, 2006 September 21, 2006 http://www.csg.csail.mit.edu/6.827 L04Ext-1
Download ReportTranscript Some more thoughts on let Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. September 21, 2006 September 21, 2006 http://www.csg.csail.mit.edu/6.827 L04Ext-1
Some more thoughts on
let Arvind Computer Science and Artificial Intelligence Laboratory M.I.T.
September 21, 2006 September 21, 2006 http://www.csg.csail.mit.edu/6.827
L04Ext-1
Why
let
Calculus ?
Programming without (recursive) let blocks is tedious Recursive let blocks can be translated into the -calculus with constants and Y combinator but the translation is • is complicated (not simple syntactical substitutions) ; • is not intuitive or illustrative • does not match any implementation extend the -calculus with recursive let
blocks.
September 21, 2006 http://www.csg.csail.mit.edu/6.827
L04Ext-2
-calculus with Constants & Letrec
E ::= x | | PF k x.E | E E | Cond (E, E, E) (E 1 ,...,E k ) | CN 0 | CN | k (E 1 ,...,E let S in E k ) | CN k (SE 1 ,...,SE k ) PF 1 PF 2 CN 0 CN 2 ::= negate | not | ... | Prj 1 | Prj 2 ::= + | ... ::= Number | Boolean ::= cons | ...
| ...
not in initial terms Statements
S ::= | x = E | S; S
Variables on the LHS in a let expression must be pairwise distinct
September 21, 2006 http://www.csg.csail.mit.edu/6.827
L04Ext-3
Issues in giving semantics for lets- 1
1. Creating redexes ((let S in x.e
1 ) e 2 )
How do we juxtapose
( x.e
1 ) e 2 ?
Solution: Lifting rules September 21, 2006 http://www.csg.csail.mit.edu/6.827
L04Ext-4
Issues in giving semantics for lets- 2
2. How to refer to a variable binding
let
f = x.e
1
in
y = e 2 e (f y) + y 3 Solution: Instantiation rules
How and when f and y refer to their definitions
( ( x.e
1 ) y) + y ? September 21, 2006 http://www.csg.csail.mit.edu/6.827
L04Ext-5
How to define the operational semantics of let blocks: Environments Eval [[e]] environment An environment-based interpreter.
– An environment where all the (variable name, value) bindings are kept and is passed around for expression evaluation – When a let expression is encountered the environment is extended with all the let-bindings. Very complicated if the environment contains unevaluated expressions – Not abstract enough – too many concrete data structures and associated functions for proper execution September 21, 2006 http://www.csg.csail.mit.edu/6.827
L04Ext-6
How to define the operational semantics of let blocks: graphs A let simply represents a wiring diagram, i.e., a graph
let
f = x.e
1 y = e 2 e 3
in
(f y) + y ap + root x f e 1 e 2 ap y e 3 September 21, 2006 – Quite complicated to explain -substitution in a graph based interpreter http://www.csg.csail.mit.edu/6.827
L04Ext-7
How to define the operational semantics of let blocks: via a calculus • Rewrite rules – Lifting rules – Instantiation rules (need some new way of writing rules): contexts, ... • Reduction Strategy • Normal forms? Equivalences?
let x = 5 in x September 21, 2006 let x = 5 in 5 the let calculus http://www.csg.csail.mit.edu/6.827
5 let x = 5 y = 6 in x L04Ext-8
Lifting Rules
(let S’ in e’) is the
-renamed
(let S in e) avoid name conflicts in the following rules: to x = let S in e x = e’; S’ let S 1 in (let S in e) (let S in e) e 1 let S 1 ; S’ in e’ let S’ in e’ e 1 Cond((let S in e), e 1 , e 2 ) let S’ in Cond(e’, e 1 , e 2 ) PF k (e 1 ,...(let S in e),...e
k ) let S’ in PF k (e 1 ,...e’,...e
k ) September 21, 2006 http://www.csg.csail.mit.edu/6.827
L04Ext-9
let
Instantiation Rules
A free variable in an expression can be instantiated by a simple expression Instantiation rule 1 (let x = a ; S in C[x]) (let x = a ; S in C’[a]) simple expression free occurrence of x in some context C renamed C[ ] to avoid free variable capture Instantiation rule 2 (x = a ; SC[x]) Instantiation rule 3 x = a where a = C[x] (x = a ; SC’[a]) x = C’[C[x]] September 21, 2006 http://www.csg.csail.mit.edu/6.827
L04Ext-10
Once we have lets we use them elsewhere too ...
The normal -rule ( x.e) e a e [e a /x] is replaced the following -rule ( x.e) e a let t = e a where t in e[t/x] is a new variable and the Instantiation rules which are used to refer to the value of a variable September 21, 2006 http://www.csg.csail.mit.edu/6.827
L04Ext-11