Você está na página 1de 27

Type Systems

and Functional Programming

Part I

S.l. dr. ing. Mihnea Muraru

Introduction

mmihnea@gmail.com
Computer Science Department

Fall 2016

1 / 106

Contents

2 / 106

Contents

Objectives

Objectives

Functional programming

Functional programming

3 / 106

4 / 106

Grading

Course objectives
Studying the particularities of functional
programming, such as lazy evaluation
and type systems of different strengths

Lab: 60, 30

Learning advanced mechanisms of the Haskell


language, which are impossible or difficult to simulate
in other languages

Exam: 40, 20

Final grade 50
Applying this apparatus to modeling practical
problems, e.g. program synthesis, lazy search,
probability spaces, genetic algorithms . . .

6 / 106

5 / 106

One of the lab outcomes

Contents

Objectives

Functional programming

An evaluator for a functional language,


equipped with a type synthesizer

7 / 106

8 / 106

Functional programming features

Functional flow

Mathematical functions, as value transformers


in1

Functions as first-class values

f1

No side effects or state

f3

Immutability
Referential transparency

in2

f2

f5

out

Lazy evaluation
in3

Recursion

f4

Higher-order functions

9 / 106

Stateful computation

Stateless computation

Output dependent on input and time:

Output dependent on input exlcusively:

10 / 106

t1

y 0 6= y

t2

11 / 106

12 / 106

Functional flow

Functional programming features

Pure

Mathematical functions, as value transformers


in1

f1

Functions as first-class values


No side effects or state
f3

in2

f2

state

Immutability
f5

Referential transparency
out
Lazy evaluation

in3

Recursion

f4

Higher-order functions

14 / 106

13 / 106

Why functional programming?


Simple processing model; equational reasoning
Declarative

Part II

Modularity, composability, reuse (lazy evaluation


as glue)

Untyped Lambda Calculus

Exploration of huge or formally infinite search spaces


Embedded Domain Specific Languages (EDSLs)
Massive parallelization
Type systems and logic, inextricably linked
Automatic program verification and synthesis
15 / 106

16 / 106

Contents

Contents

Introduction

Introduction

Lambda expressions

Lambda expressions

Reduction

Reduction

Normal forms

Normal forms

Evaluation order

Evaluation order

17 / 106

Untyped lambda calculus

18 / 106

Applications

Model of computation Alonzo Church, 1932


Theoretical basis of numerous languages:

Equivalent to the Turing machine (see the


Church-Turing thesis)
Main building block: the function

LISP

ML

Clojure

Scheme

F#

Scala

Haskell

Clean

Erlang

Computation: evaluation of function applications,


through textual substitution
Formal program verification, due to its simple
execution model

Evaluate = obtain a value (a function)!


No side effects or state
19 / 106

20 / 106

-expressions

Contents

Definition

Introduction

Lambda expressions

Definition 4.1 ( -expression).


Variable: a variable x is a -expression

Reduction

Normal forms

Function: if x is a variable and E is a -expression,


then x .E is a -expression, which stands for an
anonymous, unary function, with the formal argument
x and the body E
Application: if E and A are -expressions, then (E A)
is a -expression, which stands for the application of
the expression E onto the actual argument A.

Evaluation order

21 / 106

-expressions

22 / 106

Intuition on application evaluation

Examples

Example 4.2 ( -expressions).


x variable x

( x . x

x .x : the identity function

y )y

x . y .x : a function with another function as body!


( x .x y ) : the application of the identity function onto

the actual argument y


( x .(x x ) x .x )

23 / 106

24 / 106

Variable occurrences

Variable occurrences

Definitions

Examples

Definition 4.3 (Bound occurrence).

Example 4.5 (Bound and free variables).

An occurrence xn of a variable x is bound in the


expression E iff:
E = x .F or
E = . . . xn .F . . . or
E = . . . x .F . . . and xn appears in F .

In the expression E = ( x .x x ), we emphasize the


occurrences of x:
E = ( x1 . x2 x3 ).
|{z}
F

x1 , x2 bound in E
x3 free in E
x2 free in F !
x free in E and F

Definition 4.4 (Free occurrence).


A variable occurrence is free in an expression iff it is not
bound in that expression.
Bound/ free occurrence w.r.t. a given expression!

26 / 106

25 / 106

Variable occurrences

Variables

Examples

Definitions

Example 4.6 (Bound and free variables).


Definition 4.7 (Bound variable).

In the expression E = ( x . z .(z x ) (z y )), we emphasize


the occurrences of x, y , z:

A variable is bound in an expression iff all its occurrences


are bound in that expression.

}|

E = ( x1 . z1 .(z2 x2 ) (z3 y1 )).


x1 , x2 , z1 , z2 bound in E
y1 , z3 free in E
z1 , z2 bound in F
x2 free in F
x bound in E, but free in F
y free in E
z free in E, but bound in F

Definition 4.8 (Free variable).


A variable is free in an expression iff it is not bound in that
expression i.e., iff at least one of its occurrences is free in
that expression.
Bound/ free variable w.r.t. a given expression!

27 / 106

28 / 106

Variable occurrences

Variable occurrences

Examples

Examples

Example 4.6 (Bound and free variables).


Example 4.5 (Bound and free variables).

In the expression E = ( x . z .(z x ) (z y )), we emphasize


the occurrences of x, y , z:

In the expression E = ( x .x x ), we emphasize the


occurrences of x:

}|
{
z
E = ( x1 . z1 .(z2 x2 ) (z3 y1 )).

E = ( x1 . x2 x3 ).

x1 , x2 , z1 , z2 bound in E
y1 , z3 free in E
z1 , z2 bound in F
x2 free in F
x bound in E, but free in F
y free in E
z free in E, but bound in F

|{z}
F

x1 , x2 bound in E
x3 free in E
x2 free in F !
x free in E and F

29 / 106

Free and bound variables

30 / 106

Closed expressions
Definition 4.9 (Closed expression).
An expression that does not contain any free variables.

Free variables
FV (x ) = {x }
FV ( x .E ) = FV (E ) \ {x }
FV ((E1 E2 )) = FV (E1 ) FV (E2 )

Example 4.10 (Closed expressions).


( x .x x . y .x ) : closed
( x .x a) : open, since a is free

Remarks:

Bound variables

Free variables may stand for other -expressions,


as in x .((+ x ) 1).

BV (x ) = 0/
BV ( x .E ) = BV (E ) {x }
BV ((E1 E2 )) = BV (E1 ) \ FV (E2 ) BV (E2 ) \ FV (E1 )

Before evaluation, an expression must be brought


to the closed form.
The substitution process must terminate.
31 / 106

32 / 106

-reduction

Contents

Definitions

Introduction

Definition 5.1 ( -reduction).


4

Lambda expressions

Reduction

Normal forms

The evaluation of the application ( x .E A), by substituting


every free occurrence of the formal argument, x, in the
function body, E, with the actual argument, A:
( x .E A) E[A/x ] .

Definition 5.2 ( -redex).


7

The application ( x .E A).

Evaluation order

34 / 106

33 / 106

-reduction

-reduction

Examples

Collisions

Problem: within the expression ( x .E A):

Example 5.3 ( -reduction).

FV (A) BV (E ) = 0/ correct reduction always

( x .x y ) x [y /x ] y
( x . x .x y ) x .x[y /x ] x .x

FV (A) BV (E ) 6= 0/ potentially wrong reduction

Solution: rename the bound variables in E,


that are free in A

( x . y .x y ) y .x [y /x ] y .y

Wrong! The free variable y becomes bound,


changing its meaning!

Example 5.4 (Bound variable renaming).


( x . y .x y ) ( x . z .x y ) z .x [y /x ] z .y

35 / 106

36 / 106

-conversion

-conversion

Definition

Examples

Definition 5.5 ( -conversion).


Example 5.7 ( -conversion).

Systematic relabeling of bound variables in a function:


x .E y .E[y /x ] . Two conditions must be met.

x .(x y ) z .(z y ) : Correct!


x . x .(x y ) y . x .(x y ) : Wrong!
y is free in x .(x y ).

Example 5.6 ( -conversion).


x .y y .y[y /x ] y .y : Wrong!

x . y .(y x ) y . y .(y y ) : Wrong!


The free occurrence of x in y .(y x ) becomes bound,
after substitution, in y .(y y ).

x . y .x y . y .x[y /x ] y . y .y : Wrong!

Conditions:
y is not free in E
a free occurrence in E stays free in E[y /x ]

x . y .(y y ) y . y .(y y ) : Correct!

37 / 106

38 / 106

Reduction

Reduction

Definitions

Examples

Definition 5.8 (Reduction step).


Example 5.10 (Reduction).

A sequence made of a possible -conversion, followed by


a -reduction, such that the second produces no
collisions: E1 E2 E1 E3 E2 .

(( x . y .(y x ) y ) x .x )
( z .(z y ) x .x )
( x .x y )
y

Definition 5.9 (Reduction sequence).

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

A string of zero or more reduction steps: E1


E2 . It is an
element of the reflexive transitive closure of relation .

39 / 106

40 / 106

Reduction

Contents

Properties

Reduction step = reduction sequence:


E1 E2 E1 E2
Reflexivity:

Introduction

Lambda expressions

Reduction

Normal forms

Evaluation order

E E
Transitivity:
E1 E2 E2 E3 E1 E3

42 / 106

41 / 106

Questions
1

Normal forms

When does the computation terminate?


Does it always?

Definition 6.1 (Normal form).


The form of an expression that cannot be reduced i.e.,
that contains no -redexes.

NO
2

Does the answer depend on the reduction sequence?


YES

x .E, even if E contains -redexes.

If the computation terminates for distinct reduction


sequences, do we always get the same result?

Example 6.3 (Normal forms).


( x . y .(x y ) x .x ) FNF y .( x .x y ) NF y .y

YES
4

Definition 6.2 (Functional normal form, FNF).

If the result is unique, how do we safely obtain it?

FNF is used in programming, where the function body


is evaluated only when the function is effectively applied.

Left-to-right reduction

43 / 106

44 / 106

Reduction termination (reducibility)

Questions
1

Example 6.4.
( x .(x x ) x .(x x )) ( x .(x x ) x .(x x )) . . .
does not have a terminating reduction sequence.

When does the computation terminate?


Does it always?
NO

Does the answer depend on the reduction sequence?


YES

Definition 6.5 (Reducible expression).

An expression that has a terminating reduction sequence.

If the computation terminates for distinct reduction


sequences, do we always get the same result?
YES

is irreducible.

If the result is unique, how do we safely obtain it?


Left-to-right reduction

46 / 106

45 / 106

Reduction sequences

Questions

Example 6.6 (Reduction sequences).

E = ( x .y )

When does the computation terminate?


Does it always?
NO

y
2

2n 1

y , n 0

E
y
1

E
E
y

Does the answer depend on the reduction sequence?


YES

. . .
3

...
E has a nonterminating reduction sequence, but still
has a normal form, y . E is reducible, is not.

If the computation terminates for distinct reduction


sequences, do we always get the same result?
YES

The length of terminating reduction sequences


is unbounded.

If the result is unique, how do we safely obtain it?


Left-to-right reduction

47 / 106

48 / 106

Normal form uniqueness

Normal form uniqueness

Results

Examples

Theorem 6.7 (Church-Rosser / diamond).

Example 6.9 (Normal form uniqueness).

If E E1 and E E2 , then there is an E3 such that


E1 E3 and E2 E3 .

E1

( x . y .(x y ) ( x .x y ))
z .(( x .x y ) z ) z .(y z ) a.(y a)

( x . y .(x y ) y ) w .(y w ) a.(y a)

E3

E2

Normal form: class of expressions, equivalent under


systematic relabeling

Corollary 6.8 (Normal form uniqueness).


If an expression is reducible, its normal form is unique. It
corresponds to the value of that expression.

Value: distinguished member of this class

49 / 106

Structural equivalence

50 / 106

Computational equivalence
Definition 6.12 (Computational equivalence).
Two expressions are computationally equivalent iff they
the behave in the same way when applied onto the same
arguments.

Definition 6.10 (Structural equivalence).


Two expressions are structurally equivalent iff they both
reduce to the same expression.

Example 6.13 (Computational equivalence).


E1 = y . x .(y x )
E2 = x .x

Example 6.11 (Structural equivalence).


z .(( x .x y ) z ) and ( x . y .(x y ) y ) in Example 6.9.

((E1 a) b ) (a b )
((E2 a) b ) (a b )

E1 6 E2 and E2 6 E1 (not structurally equivalent)


51 / 106

52 / 106

Reduction order

Questions

Definitions and examples


1

When does the computation terminate?


Does it always?

Definition 6.14 (Left-to-right reduction step).


The reduction of the outermost leftmost -redex.

NO
2

Does the answer depend on the reduction sequence?


YES

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

If the computation terminates for distinct reduction


sequences, do we always get the same result?

Definition 6.16 (Right-to-left reduction step).


The reduction of the innermost rightmost -redex.

YES
4

Example 6.15 (Left-to-right reduction).

Example 6.17 (Right-to-left reduction).

If the result is unique, how do we safely obtain it?

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

Left-to-right reduction

53 / 106

Reduction order

54 / 106

Questions

Which one is better?


1

When does the computation terminate?


Does it always?
NO

Theorem 6.18 (Normalization).


If an expression is reducible, its left-to-right reduction
terminates.

Does the answer depend on the reduction sequence?


YES

The theorem does not guarantee the termination for any


expression, but only for reducible ones!

If the computation terminates for distinct reduction


sequences, do we always get the same result?
YES

If the result is unique, how do we safely obtain it?


Left-to-right reduction

55 / 106

56 / 106

Contents

Evaluation order
Definition 7.1 (Applicative-order evaluation).

Introduction

Lambda expressions

Reduction

Corresponds to right-to-left reduction. Function


arguments are evaluated before the function is applied.

Definition 7.2 (Strict function).


A function that uses applicative-order evaluation.

Definition 7.3 (Normal-order evaluation).


6

Normal forms

Evaluation order

Corresponds to left-to-right reduction. Function


arguments are evaluated when needed.

Definition 7.4 (Non-strict function).


A function that uses normal-order evaluation.
58 / 106

57 / 106

In practice I

In practice II
Lazy evaluation (a kind of normal-order evaluation) in
Haskell: on-demand evaluation of arguments, allowing for
interesting constructions

Applicative-order evaluation employed in most


programming languages, due to efficiency one-time
evaluation of arguments: C, Java, Scheme, PHP, etc.

Example 7.6 (Lazy evaluation in Haskell).


((\x -> x + x) (2 + 3))
(2 + 3) + (2 + 3)
5 + 5
10

Example 7.5 (Applicative-order evaluation in


Scheme).
(( (x) (+ x x)) (+ 2 3))
(( (x) (+ x x)) 5)
(+ 5 5)
10

Need for non-strict functions, even in applicative


languages: if, and, or, etc.

59 / 106

60 / 106

Summary
Lambda calculus: model of computation,
underpinned by functions and textual substitution

Part III

Bound/free variables and variable occurrences w.r.t.


an expression

Lambda Calculus
as a Programming Language

-reduction, -conversion, reduction step, reduction

sequence, reduction order, normal forms


Left-to-right reduction (normal-order evaluation):
always terminates for reducible expressions
Right-to-left reduction (applicative-order evaluation):
more efficient but no guarantee on termination even
for reducible expressions!
61 / 106

Contents

62 / 106

Contents

The 0 language

The 0 language

Abstract data types (ADTs)

Abstract data types (ADTs)

10

Implementation

10

Implementation

11

Recursion

11

Recursion

12

Language specification

12

Language specification

63 / 106

64 / 106

Purpose

0 features
Instructions:

Proving the expressive power of lambda calculus

-expressions

Hypothetical -machine

top-level variable bindings: variable def expression


e.g., true def x . y .x

Machine code: -expressions the 0 language

Values represented as functions

Instead of

Expressions brought to the closed form,


prior to evaluation

bits
bit operations,

Normal-order evaluation

we have
structured strings of symbols

Functional normal form (see Definition 6.2)

reduction textual substitution

No predefined types!
65 / 106

Shorthands

66 / 106

Purpose of types
Way of expressing the programmers intent
Documentation: which operators act
onto which objects

x1 . x2 . . . . . xn .E x1 x2 . . . xn .E

Particular representation for values of different types:


1, Hello, #t, etc.
((. . . ((E A1 ) A2 ) . . .) An ) (E A1 A2 . . . An )

Optimization of specific operations


Error prevention
Formal verification
67 / 106

68 / 106

No types

No types

How are objects represented?

How is correctness affected?

A number, list or tree potentially designated


by the same value e.g.,

Inability of the machine to


interpret the meaning of expressions

number 3 x . y .x list (() () ())

ensure their correctness

Both values and operators represented by functions


context-dependent meaning

Every operator applicable onto every value


Both aspects above delegated to the programmer

number 3 x . y .x operator car

Erroneus constructs accepted without warning,


but computation ended with

Value applicable onto another value, as an operator!


x

values with no meaning or


expressions that are neither values, nor reducible
e.g., (x x )

x0
69 / 106

No types

70 / 106

So...

Consequences

Enhanced representational flexibility

How do we employ the 0 language in everyday


programming?

Useful when the uniform representation of objects,


as lists de symbols, is convenient
Increased error-proneness

How do we represent usual values numbers,


booleans, lists, etc. and their corresponding
operators?

Program instability
Difficulty of verification and maintenance

71 / 106

72 / 106

Contents

Definition
Definition 9.1 (Abstract data type, ADT).

The 0 language

Abstract data types (ADTs)

10

Implementation

11

Recursion

12

Language specification

Mathematical model of a set of values and their


corresponding operations.

Example 9.2 (ADTs).


Natural, Bool, List, Set, Stack , Tree, ... -expression!
Components:
base constructors: how are values built
operators: what can be done with these values
axioms: how
74 / 106

73 / 106

The Natural ADT

The Natural ADT

Base constructors and operators

Axioms

zero?

Base constructors:

(zero ? zero ) = T

zero : Natural

(zero ? (succ n)) = F

succ : Natural Natural

pred
(pred (succ n)) = n

Operators:
zero? : Natural Bool

add

pred : Natural \ {zero} Natural

(add zero n) = n

add : Natural 2 Natural

(add (succ m) n) = (succ (add m n))

75 / 106

76 / 106

From ADTs to functional programming

Providing axioms

Exemple

Axiome:
add (zero, n) = n

One axiom for each (operator, base constructor) pair

add (succ (m), n) = succ (add (m, n))

Scheme:

More useless
1
2
3

Less insufficient for completely specifying


the operators

(define add
(lambda (m n)
(if (zero? m) n
(+ 1 (add (- m 1) n)))))

Haskell:
1
2

add 0 n = n
add (m + 1) n = 1 + (add m n)

78 / 106

77 / 106

From ADTs to functional programming

Contents

Discussion

Proving ADT correctness


structural induction

The 0 language

Proving properties of -expressions, seen as values


of an ADT with 3 base constructors!

Abstract data types (ADTs)

Functional programming
reflection of mathematical specifications

10

Implementation

11

Recursion

12

Language specification

Recursion
natural instrument, inherited from axioms
Applying formal methods on the recursive code,
taking advantage of the lack of side effects
79 / 106

80 / 106

The Bool ADT

The Bool ADT

Base contrsuctors and operators

Axioms

not
(not T ) = F

Base constructors:

(not F ) = T

T : Bool

and

F : Bool

(and T a) = a
(and F a) = F

Operators:
or

not : Bool Bool


and : Bool 2 Bool

(or T a) = T

or : Bool 2 Bool

(or F a) = a

if

if : Bool T T T

(if T a b ) = a
(if F a b ) = b
81 / 106

82 / 106

The Bool ADT

The Bool ADT

Base constructor implementation

Operator implementation

not def x .(x F T )


(not T ) ( x .(x F T ) T ) (T F T ) F

Intuition: selecting one of the two values, true or false

(not F ) ( x .(x F T ) F ) (F F T ) T

and def xy .(x y F )

T def xy .x

(and T a) ( xy .(x y F ) T a) (T a F ) a
(and F a) ( xy .(x y F ) F a) (F a F ) F

F def xy .y

or def xy .(x T y )
(or T a) ( xy .(x T y ) T a) (T T a) T

Selector-like behavior:

(or F a) ( xy .(x T y ) F a) (F T a) a

(T a b ) ( xy .x a b ) a

if def cte.(c t e) non-strict!

(F a b ) ( xy .y a b ) b

(if T a b ) ( cte.(c t e) T a b ) (T a b ) a
(if F a b ) ( cte.(c t e) F a b ) (F a b ) b
83 / 106

84 / 106

The Pair ADT

The Pair ADT

Specification

Implementation

Intuition: a pair = a function that expects a selector, in


order to apply it onto its components

Base constructors:
pair : A B Pair

pair def xys.(s x y )


Operators:

(pair a b ) ( xys .(s x y ) a b ) s .(s a b )

fst : Pair A

fst def p.(p T )

snd : Pair B

(fst (pair a b )) ( p .(p T ) s .(s a b ))


( s .(s a b ) T ) (T a b ) a

Axioms:
(fst (pair a b )) = a

snd def p.(p F )

(snd (pair a b )) = b

(snd (pair a b )) ( p .(p F ) s .(s a b ))


( s .(s a b ) F ) (F a b ) b
85 / 106

86 / 106

The List ADT

The List ADT

Base constructors and operators

Axioms

car
(car (cons e L)) = e

Base constructors:
null : List

cdr

cons : A List List

(cdr (cons e L)) = L

null ?

Operators:
car : List \ {null } A

(null ? null ) = T

cdr : List \ {null } List

(null ? (cons e L)) = F

null ? : List Bool

append

append : List 2 List

(append null B ) = B
(append (cons e A) B ) = (cons e (append A B ))
87 / 106

88 / 106

The List ADT

The Natural ADT

Implementation

Axioms

Intuition: a list = a (head, tail) pair


zero?

null def x .T

(zero ? zero ) = T

cons def pair

(zero ? (succ n)) = F

car def fst


pred

cdr def snd

(pred (succ n)) = n

null ? def L.(L xy .F )


(null ? null ) ( L.(L xy .F ) x .T ) ( x .T . . .) T

add

(null ? (cons e L)) ( L.(L xy .F ) s .(s e L))


( s .(s e L) xy .F ) ( xy .F e L) F

(add zero n) = n
(add (succ m) n) = (succ (add m n))

append def
. . . no closed form
AB .(if (null ? A) B (cons (car A) (append (cdr A) B )))

90 / 106

89 / 106

The Natural ADT

Contents

Implementation

Intuition: a number = a list having the number value


as its length

The 0 language

Abstract data types (ADTs)

10

Implementation

11

Recursion

12

Language specification

zero def null


succ def n.(cons null n)
zero? def null ?
pred def cdr
add def append

91 / 106

92 / 106

Functions

Perspectives on recursion

Several possible definitions of the identity function:


id (n) = n

Textual: a function that refers itself,


using its name

id (n) = n + 1 1
id (n) = n + 2 2
...

Constructivist: recursive functions as values of an


ADT, with specific ways of building them

Infinitely many textual representations for the same


function

Semantic: the mathematical object designated


by a recursive function

Then. . . what is a function? A relation between inputs


and outputs, independent of any textual
representation e.g.,
id = {(0, 0), (1, 1), (2, 2), . . .}

94 / 106

93 / 106

Implementing length

Contents

Problem

Length of a list:

The 0 language

Abstract data types (ADTs)

10

Implementation

11

Recursion

12

Language specification

length def L.(if (null ? L) zero (succ (length (cdr L))))


What do we replace the underlined area with,
to avoid textual recursion?
Rewrite the definition as a fixed-point equation
Length def f L.(if (null ? L) zero (succ (f (cdr L))))
(Length length) length
How do we compute the fixed point? (see code
archive)
95 / 106

96 / 106

Axiomatization benefits

Syntax
Variable:
Var ::= any symbol distinct from , ., (, )

Disambiguation

Expression:
Expr :: = Var
| Var .Expr
| (Expr Expr )

Proof of properties

Implementation skeleton
Value:
Val ::= Var .Expr

98 / 106

97 / 106

Semantics for normal-order evaluation

Evaluation rules

Evaluation

Reduce:
Rule name:

( x .e e0 ) e[e0 /x ]

precondition1 , . . . , preconditionn
conclusion
Eval:
e e0
(e e00 ) (e0 e00 )

99 / 106

100 / 106

Semantics for normal-order evaluation

Semantics for normal-order evaluation

Substitution

Free variables

x[e/x ] = e
y[e/x ] = y ,

y 6= x

FV (x ) = {x }

h x .ei[e0 /x ] = x .e

FV ( x .e) = FV (e) \ {x }
h y .ei[e0 /x ] = y .e[e0 /x ] ,

y 6= x y 6

FV (e0 )
FV ((e0 e00 )) = FV (e0 ) FV (e00 )

h y .ei[e0 /x ] = z .e[z /y ][e0 /x ] ,


y 6= x y FV (e0 ) z 6 FV (e) FV (e0 )
(e0 e00 )[e/x ] = (e[0e/x ] e[00e/x ] )

101 / 106

102 / 106

Semantics for normal-order evaluation

Semantics for applicative-order evaluation

Example

Evaluation

Reduce (v Val):

Example 12.1 (Evaluation rules).


(( x . y .y a) b )

( x .e v ) e[v /x ]

Eval 1 :
( x . y .y a) y .y (Reduce)
(( x . y .y a) b ) ( y .y b )

e e0
(e e00 ) (e0 e00 )

(Eval )

Eval 2 (v Val):
( y .y b ) b

e e0
(v e) (v e0 )

(Reduce)

103 / 106

104 / 106

Formal proof

Summary

Proposition 12.2 (Free and bound variables).


Practical usage of the untyped lambda calculus,
as a programming language

e Expr BV (e) FV (e) = 0/

Proof.

Formal specifications, for different evaluation


semantics

Structural induction, according to the different forms of


-expressions (see the lecture notes).

105 / 106

106 / 106

Você também pode gostar