Substitution Semantics of FL a simple functional language - PowerPoint PPT Presentation

About This Presentation
Title:

Substitution Semantics of FL a simple functional language

Description:

FL is orthogonal higher-order functions. Multiple arguments can be accepted one-by-one ... Declarations, regions and scope in FL (unary functions for now) ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 44
Provided by: off9
Category:

less

Transcript and Presenter's Notes

Title: Substitution Semantics of FL a simple 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
  • 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
Syntax
  • Domains and variable declarations
  • Ident (identifiers),
  • Syntax (additions to EL)

4
  • In
  • x is the formal parameter,
  • e is the body
  • In

5
  • In abstract syntax trees
  • Two new node labels
  • lambda x (or lambda (x1xn)) (a parametrized
    construct)
  • let
  • Q there is no new function application label
    why?
  • Values
  • v
    (functions are values)

6
  • For simplicity, we consider now only
  • unary functions
  • n-ary functions and let are treated later

7
(No Transcript)
8
  • Comment
  • FL is orthogonal ? higher-order functions
  • Multiple arguments can be accepted one-by-one

9
Semantics of unary function application
  • The idea substitute value of actual parameter
    for uses of formal parameter in body

10
Declarations, regions and scope in FL(unary
functions for now)
  • 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

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 i1,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?

12
Substitution
  • Notation g/xe the (careful) substitution of
    expression g for all free uses of x in e
  • Note g/xe 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
  • Def g/xe is defined by case of e
  • For application, tuple, if, simply push the
    substitution into the composite expressions

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
  • 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/xe
    later.

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

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
  • Comments
  • Def. of region, scope, substitution (static
    structure) are part of the semantics
  • There is no binding management an association
    between a parameter and a value is immediately
    used for substitution, then discarded
  • Approach is easy to explain, intuitive
  • 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
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
  • (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
  • 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
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
A variable is not a value ? can be selected
sub-expression not a redex ? there is no
transition from it
  • 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?

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
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/xe is never needed
    for closed expressions)
  • Corollary
  • A computation from closed E never raises
  • free var error

26
  • Should programs be restricted to closed ?

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?

2. Equivalent expressions ? same semantics
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)

Explore free variables, , a DAG structure
29
  • Claim (alpha) is an equivalence a congruence
  • Claim (same semantics)
  • The above a declarative def of alpha

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
Multi-argument functions let
  • Two ways (at least) to add n-ary functions to a
    language with tuples and unary functions
  • 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

32
  • 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?

33
(non-recursive) definitions let,
let(alternatively letpar, letseq)
  • let x 3, y 5 in xy ? 8
  • let x 3, y 5
  • in let x y1, y x 1 in x-y ? 4
  • let x 3, y 5
  • in let x y1, y x 1 in x-y ? 1
  • let a parallel definition block
  • let - a sequential definition block (definable
    from let)

34
  • Variations on concrete syntax
  • The regions in the above include rest of program

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
  • Transition Semantics
  • (simultaneous substitution)
  • Natural semantics for you

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

applic
let
x
e
body
e
body
x
38
The general case
? Alternative rule (this one rule is sufficient)
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
  • 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
Some history The pure lambda-calculus
  • Invented by A. Church in the late 1930s
  • 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
Churchs semantics for the pure lambda-calculus
  • (and rule alpha)
  • How does this set of rules differ from ours?

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 !
Write a Comment
User Comments (0)
About PowerShow.com