43
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 1 Substitution Semantics of FL – a simple functional language FL is EL + (non-recursive) function creation

  • View
    224

  • Download
    0

Embed Size (px)

Citation preview

Page 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

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)

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

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

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

Catriel Beeri Pls/Winter 2004/5 functional-language 3

Syntax

• Domains and variable declarations:– Ident (identifiers),

• Syntax: (additions to EL)

Identx

1 1 1:: ... . | ( ,... ). | ,...e t in l| | n n ne x e x x e x e x e ex

( in Scheme, in OCAlambda fu ML)nction

. - function with paramer , body

p e p e

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

Catriel Beeri Pls/Winter 2004/5 functional-language 4

In

• x is the formal parameter,

• e is the body

In

unary. - a functionx e

1.( ,... ). - n-ary an functionnx e e e

1 1let in

local, non-recur

,...

sive , definitions block an nx e x e e

.x e

1 1

defin

let ,..

ing expression bod

.

is a , is the y

inn n

i

x e x e e

e e

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

Catriel Beeri Pls/Winter 2004/5 functional-language 5

In abstract syntax trees:

Two new node labels:

• lambda x (or lambda (x1…xn)) (a parametrized construct)

• let

Q: there is no new function application label– why?

Values:

v ::= … | (functions are values)

1. | ( ,... ).nx e x x e

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

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

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

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

x

. .x f f f x

( . . )x f f f x y z

. 3x f x

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

Catriel Beeri Pls/Winter 2004/5 functional-language 8

Comment:

FL is orthogonal higher-order functions

Multiple arguments can be accepted one-by-one

*( . . 2* ) 3 4 11x y x y

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

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:

( . 3) 4 4 3x x

( .(( . 3) 5) 2* ) 8 ( . 3) 5 2*8

3 5 2*8

x x x x x x

( . . 2* ) 7 3 ( .7 2* ) 3 7 2*3x y x y y y

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

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)

x .x e

x .x e.x e

e

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

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 ei is free in (e1,…, en)

if: each free use of x in ei i=1,…3 is free in if e1 e2 e3

applic: each free use of x in ei i= 1,2, is free in e1 e2

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?

(y.e y x)

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

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

*( . . ) ( . ) 5 5 naivef y f y w y

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

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

1 2 1 2[ / ]( ) [ / ] [ / ]g x e e g x e g x e

[ / ] , [ / ] ( )g x x g g x y y y x

1 2 3 1 2 3[ / ](if ) if [ / ] [ / ] [ / ]g x e e e g x e g x e g x e

[ / ] ( )g x c c c Int Bool Op

1 1[ / ]( ,..., ) ([ / ] ,...,[ / ] )n ng x e e g x e g x e

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

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?

[ / ]( . ) .[ / ] , ( )

when does not contain free

g x y e y g x e y x

g y

[ / ]( . ) . (attention!)g x x e x e

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

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.

[ / ]( . ) [ / ]( .[ / ] ), ( , "new")

when frcontains ee

g x y e g x z z y e y x z

g y

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

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 v1 v2 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!)

(fun-apply) ( . ) [ ]x e v v x e

( )

,(The full sets of rules: [ ] , [ ] )dS FL S FL

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

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

(nat-fun) . .x e x e

1 2

1 2

. [ / ](nat-applic-fun)

e x e e v v x e v

e e v

(The full set of rules : [ ] )S FL

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

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)?

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

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?

( . )x e v[ ]v x e

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

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?

d

( . )( . )x x x x x x

,[ ] dS FL

[ ]S FL

[ ]S FL

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

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

, , and d

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

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?

3 .x x

1

1 2

v op

v v

ER

( . )x e v

if . then 1 else 2x x

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

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

5, 3 2, ( 1,3 5), ( . ) ( 2),x x yx x x z if 4 then 3 else 5x

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

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

,ER , ,ER ,ER[ ] , [ ] , [ ]dS EL S EL S EL

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

Catriel Beeri Pls/Winter 2004/5 functional-language 25

Closed expressionsAn expression w/o free variables is closedClaim: 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

( . )x e v

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

Catriel Beeri Pls/Winter 2004/5 functional-language 26

Should programs be restricted to closed ?

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

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

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

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)

1 2E E

x

y +

x y

x

z +

x z

w

y +

w y

Explore free variables, , a DAG structure

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

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

1 2 1 1

2 2 2 1 2

if E E and E E

then exists E s.t. E E , and E E

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

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

. 'x E

.[ / ] 'y y x E

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

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(v1,…, vn) = vi

This can be used in the body of functions to take apart tuple arguments

Not very convenient

.#1( ) 2* #2( )x x x

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

Catriel Beeri Pls/Winter 2004/5 functional-language 32

2. Allow tuple formal parameters (x1,…, xn).

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?

( , ). 2*x y x y

1 1[ / ,..., / ]n ng x g x e

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

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 blocklet* - a sequential definition block (definable from let)

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

Catriel Beeri Pls/Winter 2004/5 functional-language 34

Variations on concrete syntax:

The regions in the above include rest of program

1 1 n nval x and ... and x e SML parallele

1 1 n nval x ... val x e SML sequentiale

int x =5; C sequential

int y = x+3;

1 1 n nlet x and... and x e OCAML parallele

val/let ( , , ) ( 1, 2, 3) (S/OCA)ML parallelx y z E E E

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

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)

1 1: ... | let ,... in n ne x e x e e

1 1: ... | let ( ,..., ) ( ,..., ) in n ne x x e e e

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

Catriel Beeri Pls/Winter 2004/5 functional-language 36

Transition Semantics:

(simultaneous substitution)

Natural semantics:– for you

1 1 1 1

(let-eval) -

(let-apply) let ,... in [

for you

/ ,... / ]n n n nx v x v e v x v x e

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

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 3 in 2* 1 2*3 1

( .2* 1) 3 2*3 1

x x

x x

let

x e body bodyx e

applic

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

Catriel Beeri Pls/Winter 2004/5 functional-language 38

1 1 1 1 ,... ( ( ,..., ). ) ( ,...,l t )e inn n n nx e x e e x x e e e

Alternative rule: (this one rule is sufficient)

1 1

1 1

(let-eval') let ,... in

( ( ,..., ). ) ( ,..., )n n

n n

x e x e e

x x e e e

The general case:

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

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

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

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

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

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

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

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?

1 1

1 2 1 2

e e

e e e e

1 2 2 1( ) ( . ) [ / ]x e e e x e

2 2

1 2 1 2

e e

e e e e

. .

e e

x e x e

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

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: !