Download slides 4-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

Standard ML wikipedia , lookup

Curry–Howard correspondence wikipedia , lookup

Lambda lifting wikipedia , lookup

Lambda calculus wikipedia , lookup

Lambda calculus definition wikipedia , lookup

Combinatory logic wikipedia , lookup

Transcript
overview
Haskell
equational programming
2015 01 11
lecture 3
data types in the lambda calculus
fixed point combinators
material
overview
Haskell
data types in the lambda calculus
fixed point combinators
seen in practical work 1
arithmetic
computing with booleans, pairs, lists
elementary functions
pattern matching
material
recursion
Haskell: functions are first-class citizens
statics: application of functions
application by juxtaposition
F M N and head [1,2]
just like in λ-calculus
however: we cannot check equality of functions
(otherwise Haskell would be able to solve the Collatz conjecture)
also: partial application
sum = foldr (+) 0
also: anonymous function
λx. x en \x -> x
in Haskell: only arguments of the right type
so far: untyped λ-calculus, later simply typed λ-calculus
evaluating expressions
strategies and lazy evaluation
β-reduction, sometimes with some strategy (theory part 1)
equational reasoning (theory part 2)
both are used in the evaluation of Haskell expressions
example equational reasoning in Haskell:
definition:
Haskell is a lazy functional programming language
that is: do at least as possible, do it last moment, do it at most once
in λ-calculus: leftmost-outermost strategy plus sharing
foldl f b []
= b
foldl f b (h:t) = foldl f (f b h) t
leftmost-outermost strategy is normalizing
derivation using (informal) equational reasoning:
sharing is subtle
foldl (*) 1 [4,3,2,1] =
=
=
=
=
=
foldl (*) (1*4) [3,2,1]
foldl (*) ((1*4)*3) [2,1]
foldl (*) (((1*4)*3)*2) [1]
foldl (*) ((((1*4)*3)*2)*1) []
((((1*4)*3)*2)*1)
24
normal forms
lazy evaluatie and built-in operators
Haskell does not evaluate below a lambda
example: \x -> False && x is not evaluated
for built-in operators the arguments are evaluated first
in λ-calculus: weak head normal form
example: 0 ∗ (head[1, 2]) first evaluates head
intuition: in a WHNF at least one symbol is stable under reduction
lazy evaluatie and confluence
infinite lists
every two terminating evaluations of the same Haskell expression
yield the same result
in Haskell lists may be finite or potentially infinite
in λ-calculus: uniqueness of normal forms follows from confluence
we can also compute with infinite lists
example: if side-effects are present this is not the case:
n + (n := 1) = 0 + (n := 1) = 0 + 1 = 1
n + (n := 1) = n + 1 = 1 + 1 = 2
ones = 1 : ones
head ones gives output 1
thanks to lazy evaluation!
(what would innermost reduction give?)
algebraic data types
overview
enumeration type
Haskell
example: booleans
data Bool = False | True deriving (Eq, Show)
data types in the lambda calculus
booleans
pairs
natural numbers
lists
more general
example: natural numbers
data Nat = Zero | Suc Nat deriving (Eq, Show)
pattern matching: a function has a clause for every constructor
related to theory on algebraic specifications
Church’s Thesis
fixed point combinators
material
some models of computation
• lambda calculus
• Combinatory Logic
everything that is computable
is definable in the pure untyped lambda calculus
• Turing machines
• register machines
• cellular automata
• µ-recursive functions
• Markov algorithms
booleans as lambda-terms: idea
we try to find:
two
different
closed
normal forms
permitting to calculate
negation
booleans as lambda terms: definition
definition of term for true
true = λxy . x
definition of term for false
false = λxy . y
other operations on booleans
specification:
not true =β false
if-then-else
not false =β true
conjunction
definition:
not = λx. x false true
correctness of the definition:
check two cases
disjunction
xor
pair: idea
pair: definition
definition of pairing operator:
we try to find:
a method to combine two terms in a pair
in such a way that a component can be extracted from the pair
π := λlrz. z l r
then:
π P Q =β λz. z P Q
pair: projection
natural numbers as lambda terms
specification:
π1 (π P Q) =β P
π2 (π P Q) =β Q
definition:
π1 := λu. u (λlr . l)
π2 := λu. u (λlr . r )
correctness of the definition:
check two cases
we try to find:
inifinitely many
different
closed
normal forms
permitting to calculate
Church numerals
successor
specification:
S cn =β cn+1
c0
c1
c2
c3
c4
=
=
=
=
=
..
.
λs. λz. z
λs. λz. s z
λs. λz. s (s z)
λs. λz. s (s (s z))
λs. λz. s (s (s (s z)))
definition:
S = λx. λsz. s (x s z)
correctness of the definition:
proof by induction
cn = λs. λz. s n (z)
alternative definition:
S = λx. λsz. x s (s z)
other operations on naturaul numbers
predecessor
iszero:
iszero := λn. n (λy . false) true
auxiliary definition:
addition:
prefn := λfp. π false (ite (π1 p) (π2 p) (f (π2 p)))
plus := λmn. λsz. m s (n s z)
then:
multiplication:
prefn f (π true x) =β π false x
mul := λmn. λsz. m (n s) z
prefn f (π false x) =β π false (f x)
exponentiation:
exp := λmn. n m
definition predecessor:
pred := λx. λsz. π2 (x (prefn s) (π true z))
lists: idea
lists: definition
a constructor for the empty list:
nil := λxy . y
a list is obtained by repeatedly forming a pair
for example: [1, 2, 3] is (1, (2, (3, nil)))
a constructor for cons:
cons := λht. λz. z h t
then:
nil = λxy . y
cons H T =β λz. z H T
lists: head and tail
overview
specification:
Haskell
head (cons H T ) =β H
tail (cons H T ) =β T
definition:
head := λl. l (λht. h)
tail := λl. l (λht. t)
data types in the lambda calculus
fixed point combinators
material
fixed point
fixed point combinator
definition:
x ∈ A is a fixed point of f : A → B if f (x) = x
definition:
examples:
0 and 1 are fixed points of f : R → R with x 7→
for λ-calculus:
M is a fixed point of F if F M =β M
Y is een fixed point combinator if
x2
F (Y F ) =β Y F for every λ-term F
informally:
we can use Y to construct a fixed point for a given term F
example:
every term M is a fixed point of I
fixed point combinators
Curry’s fixed point combinator:
Y = λf . (λx. f (x x)) (λx. f (x x))
Turing’s fixed point combinator:
T = (λx. λy . y (x x y )) (λx. λy . y (x x y ))
overview
Haskell
data types in the lambda calculus
booleans
pairs
natural numbers
lists
fixed point combinators
material
material
addtional material
• logicomix
• course notes chapters Data Types and Recursion
• Kleene wiki
• wiki Alan Turing and website over Alan Turing