Download slides 1-up

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Corecursion wikipedia , lookup

Transcript
equational programming
2016 01 04
lecture 1
overview
practical issues
introductory remarks
lambda terms
material
overview
practical issues
introductory remarks
lambda terms
material
who
• lectures:
Femke van Raamsdonk
f.van.raamsdonk at vu.nl
T446
• exercise classes:
Roy Overbeek
• Haskell lab:
Sebastian Österlund
classes
• lectures:
week 1–4
Monday 11.00-12.45 in main building 02A24
Thursday 11.00-12.45 in main building 08A33
• exercise classes:
week 1–4
Tuesday 11.00-12.45 in main building 04A33
Friday 11.00-12.45 in M143
• computer lab:
week 1–4
Tuesday 13.30-17.00 in P337 and P447
Thursday 13.30-17.00 in P337 and P447
exam
• 3 or 4 sets of Haskell exercises
• 4 sets of theory exercises
• written exam
Friday January 29, 2016
• minimum
5,5 for partial results
• validity
partial results are valid only this year (2015-2016)
• final grade
30% Haskell exercises, 70% written exam,
at most 0.5 bonus on exam grade for theory exercises
material
• courses notes, slides, exercises
• webpage of the course
• via web
• extra
some additional material via slides and links
(unless otherwise stated not for exam)
overview
practical issues
introductory remarks
lambda terms
material
equational programming
foundations of functional programming
functional programming
a functional program is an expression,
and is executed by evaluating the expression
(use definitions from left to right)
focus on what and not so much on how
the functions are pure (or, mathematical)
an input always gives the same output
example
in Haskell:
sum [1 .. 100]
in Java:
total = 0;
for (i = 1; i <= 10; ++i)
total = total + i;
functional programming: properties
high level of abstraction
more confidence in correctness
(read, check, prove correct)
Lisp
Lisp
John McCarthy (1927-2011), Turing Award 1971
ML
ML
Robin Milner (1934-2010), Turing Award 1991, et al
Haskell
Haskell
a group containing ao Philip Wadler and Simon Peyton Jones
functional programming languages
typed
untyped
strict
ML
Lisp
lazy
Haskell
functional programming and lambda calculus
Based on the lambda calculus, Lisp rapidly became ...
(from: wikipedia page John McCarthy)
Haskell is based on the lambda calculus, hence the lambda we use as a logo.
(from: the Haskell website)
Historically, ML stands for metalanguage: it was conceived to develop proof
tactics in the LCF theorem prover (whose language, pplambda, a
combination of the first-order predicate calculus and the simply typed
polymorphic lambda calculus, had ML as its metalanguage).
(from: wikipedia page of ML)
functional programmeren en algebraic specifications
programming languages with algebraic data types:
Haskell,
OCaml
see wikipedia page on algebraic data types
other functional programming languages
F# (Microsoft)
Erlang (Ericsson)
Scala (Java plus ML)
equational programming
• lambda calculus
• algebraic specifications
• exercises functional programming: Haskell
overvoew
practical issues
introductory remarks
lambda terms
material
lambda calculus
inventor:
Alonzo Church (1936)
foundations of mathematics
foundations of concept ‘computability’
restriction to functions
basis of functional programming
notation for functions
• mathematical notation:
f : nat → nat
f (x) = square(x)
• or also:
f : nat → nat
f : x 7→ square(x)
• lambda notation:
λx. square x
lambda terms: intuition
abstraction:
λx. M is the function mapping x to M
λx. square x is the function mapping x to square x
application:
F M is the application
(not the result of applying)
of the function F to its argument M
lambda terms: inductive definition
• variabele
x
• constant
c
• abstraction
(λx. M)
• application
(F M)
terms as trees: example
@
@
x
@
@
λx
x
y
terms as trees: general
famous terms
I = λx. x
K = λx. λy . x
S = λx. λy . λz. x z (y z)
Ω = (λx. x x) (λx. x x)
parentheses
• application is associative to the left
(M N P) instead of ((M N) P)
• outermost parentheses are omitted
M N P instead of (M N P)
• lambda extends to the right as far as possible
λx. M N instead of λx. (M N)
more notation
• (λx. λy . M) instead of (λx. (λy . M))
• (M λx. N) instead of (M (λx. N))
• λxy . M instead of λx. λy . M
• sometimes we combine lambdas
λx1 . . . xn . M instead of λx1 . . . . λxn . M
lambda terms: examples
(λxyz. y (x y z)) λvw . v w
(λxy . x x y ) (λzu. u (u z)) λw . w
inductive definition of terms
• definitions recursively on the definition of terms
example: definition of the free variables of a term
• proofs by induction on the definition of terms
example: every term has finitely many free variables
typed and untyped
typed lambda calculus to avoid paradoxes
our course: first untyped, then simply typed
Haskell (and ML): typed
terms in Haskell: typed
just as lambda terms, but typed; example:
sum : natlist → nat
[1, 2] : natlist
sum [1, 2] : nat
plus : nat → (nat → nat)
2 : nat
plus 2 : nat → nat
(plus 2) 5 : nat
5 : nat
terms in Haskell: more constants
just as lambda terms but more predefined functions; example:
2 + 3 →δ 5
let x = s in t
Currying
a function that intuitively has type (nat × nat) → nat
is written as a function intuitively of type nat → nat → nat
not λ(x, y ). plus x y
but: λx. λy . plus x y
bound variables: definition
x is bound by the first λx above it in the term tree
examples: the underlined x is bound in
λx. x
λx. x x
(λx. x) x
λx. y x
free variabeles: definition
a variable that is not bound is free
alternatively: define recursively the set FV(M) of free variables of M:
FV(x) = {x}
FV(c) = ∅
FV(λx. M) = FV(M)\{x}
FV(F P) = FV(F ) ∪ FV(P)
substitution: intuition
M[x := N] means:
the result of replacing in M all free occurrences of x by N
substitition: recursive definition
substitution in a variable or a constant:
x[x := N] = N
a[x := N] = a with a 6= x a variable or a constant
substitution in an application:
(P Q)[x := N] = (P[x := N] (Q[x := N])
substitution in an abstraction:
(λx. P)[x := N] = λx. P
(λy . P)[x := N] = λy . (P[x := N]) if x 6= y and y 6∈ FV(N)
(λy . P)[x := N] = λz. (P[y := z][x := N]) x
if x 6= y and z 6∈ FV(N) ∪ FV(P) and y ∈ FV(N))
substitition: examples
(λx. x)[x := c] = λx. x
(λx. y )[y := c] = λx. c
(λx. y )[y := x] = λz. x
alpha conversion
• alpha conversion:
bound variables may be renamed
• example:
λx. x =α λy . y
• compare with:
f : x 7→ x 2 is f : y 7→ y 2
∀x. P(x) is ∀y . P(y )
• identification of alpha-equivalent terms
we work with equivalence classes modulo α
beta reduction: examples
• (λx. x) y →β y
• (λx. x x) y →β y y
• (λx. x z) y →β y z
• (λx. z) y →β z
beta reduction rule: definition
(λx. M) N →β M[x := N]
here:
x is a variabele
M and N are terms
[x := N] is the substitution of N for x
theory of reduction
redex
sub-term of the form (λx. M) N
reduction or rewrite step
application of β-reduction rule in a term
reduction sequence
0, 1 or more reduction steps
normal form
term not admitting β-reduction
that is: term without a redex
δ-reductie
• in the examples we sometimes use predefined functions or constants
for some constants we assume δ-reduction rules
• redexes:
β-redex (λx. M) N
δ-redex plus 3 5
• reduction steps:
(λx. f x x) y →β f y y
plus 3 5 →δ 8
overview
practical issues
introductory remarks
lambda terms
material
material
• course notes chapter Terms and Reduction
• Haskell pages
additional material
• paper: Why functional programming matters
by John Hughes
• paper: History of Lambda-calculus and Combinatory Logic
by Felice Cardone en J.Roger Hindley