Escolar Documentos
Profissional Documentos
Cultura Documentos
Part I
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
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
Contents
Objectives
Functional programming
7 / 106
8 / 106
Functional flow
f1
f3
Immutability
Referential transparency
in2
f2
f5
out
Lazy evaluation
in3
Recursion
f4
Higher-order functions
9 / 106
Stateful computation
Stateless computation
10 / 106
t1
y 0 6= y
t2
11 / 106
12 / 106
Functional flow
Pure
f1
in2
f2
state
Immutability
f5
Referential transparency
out
Lazy evaluation
in3
Recursion
f4
Higher-order functions
14 / 106
13 / 106
Part II
16 / 106
Contents
Contents
Introduction
Introduction
Lambda expressions
Lambda expressions
Reduction
Reduction
Normal forms
Normal forms
Evaluation order
Evaluation order
17 / 106
18 / 106
Applications
LISP
ML
Clojure
Scheme
F#
Scala
Haskell
Clean
Erlang
20 / 106
-expressions
Contents
Definition
Introduction
Lambda expressions
Reduction
Normal forms
Evaluation order
21 / 106
-expressions
22 / 106
Examples
( x . x
y )y
23 / 106
24 / 106
Variable occurrences
Variable occurrences
Definitions
Examples
x1 , x2 bound in E
x3 free in E
x2 free in F !
x free in E and F
26 / 106
25 / 106
Variable occurrences
Variables
Examples
Definitions
}|
27 / 106
28 / 106
Variable occurrences
Variable occurrences
Examples
Examples
}|
{
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
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 )
Remarks:
Bound variables
BV (x ) = 0/
BV ( x .E ) = BV (E ) {x }
BV ((E1 E2 )) = BV (E1 ) \ FV (E2 ) BV (E2 ) \ FV (E1 )
32 / 106
-reduction
Contents
Definitions
Introduction
Lambda expressions
Reduction
Normal forms
Evaluation order
34 / 106
33 / 106
-reduction
-reduction
Examples
Collisions
( x .x y ) x [y /x ] y
( x . x .x y ) x .x[y /x ] x .x
( x . y .x y ) y .x [y /x ] y .y
35 / 106
36 / 106
-conversion
-conversion
Definition
Examples
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 ]
37 / 106
38 / 106
Reduction
Reduction
Definitions
Examples
(( x . y .(y x ) y ) x .x )
( z .(z y ) x .x )
( x .x y )
y
(( x . y .(y x ) y ) x .x ) y
39 / 106
40 / 106
Reduction
Contents
Properties
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
NO
2
YES
4
Left-to-right reduction
43 / 106
44 / 106
Questions
1
Example 6.4.
( x .(x x ) x .(x x )) ( x .(x x ) x .(x x )) . . .
does not have a terminating reduction sequence.
is irreducible.
46 / 106
45 / 106
Reduction sequences
Questions
E = ( x .y )
y
2
2n 1
y , n 0
E
y
1
E
E
y
. . .
3
...
E has a nonterminating reduction sequence, but still
has a normal form, y . E is reducible, is not.
47 / 106
48 / 106
Results
Examples
E1
( x . y .(x y ) ( x .x y ))
z .(( x .x y ) z ) z .(y z ) a.(y a)
E3
E2
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.
((E1 a) b ) (a b )
((E2 a) b ) (a b )
52 / 106
Reduction order
Questions
NO
2
YES
4
Left-to-right reduction
53 / 106
Reduction order
54 / 106
Questions
55 / 106
56 / 106
Contents
Evaluation order
Definition 7.1 (Applicative-order evaluation).
Introduction
Lambda expressions
Reduction
Normal forms
Evaluation order
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
59 / 106
60 / 106
Summary
Lambda calculus: model of computation,
underpinned by functions and textual substitution
Part III
Lambda Calculus
as a Programming Language
Contents
62 / 106
Contents
The 0 language
The 0 language
10
Implementation
10
Implementation
11
Recursion
11
Recursion
12
Language specification
12
Language specification
63 / 106
64 / 106
Purpose
0 features
Instructions:
-expressions
Hypothetical -machine
Instead of
bits
bit operations,
Normal-order evaluation
we have
structured strings of symbols
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
68 / 106
No types
No types
x0
69 / 106
No types
70 / 106
So...
Consequences
Program instability
Difficulty of verification and maintenance
71 / 106
72 / 106
Contents
Definition
Definition 9.1 (Abstract data type, ADT).
The 0 language
10
Implementation
11
Recursion
12
Language specification
73 / 106
Axioms
zero?
Base constructors:
(zero ? zero ) = T
zero : Natural
pred
(pred (succ n)) = n
Operators:
zero? : Natural Bool
add
(add zero n) = n
75 / 106
76 / 106
Providing axioms
Exemple
Axiome:
add (zero, n) = n
Scheme:
More useless
1
2
3
(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
Contents
Discussion
The 0 language
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
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
(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
Operator implementation
(not F ) ( x .(x F T ) F ) (F F T ) T
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
(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
Specification
Implementation
Base constructors:
pair : A B Pair
fst : Pair A
snd : Pair B
Axioms:
(fst (pair a b )) = a
(snd (pair a b )) = b
86 / 106
Axioms
car
(car (cons e L)) = e
Base constructors:
null : List
cdr
null ?
Operators:
car : List \ {null } A
(null ? null ) = T
append
(append null B ) = B
(append (cons e A) B ) = (cons e (append A B ))
87 / 106
88 / 106
Implementation
Axioms
null def x .T
(zero ? zero ) = T
add
(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
Contents
Implementation
The 0 language
10
Implementation
11
Recursion
12
Language specification
91 / 106
92 / 106
Functions
Perspectives on recursion
id (n) = n + 1 1
id (n) = n + 2 2
...
94 / 106
93 / 106
Implementing length
Contents
Problem
Length of a list:
The 0 language
10
Implementation
11
Recursion
12
Language specification
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
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
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 )
101 / 106
102 / 106
Example
Evaluation
Reduce (v Val):
( 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
Proof.
105 / 106
106 / 106