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

Scala (programming language) wikipedia , lookup

Closure (computer programming) wikipedia , lookup

Currying wikipedia , lookup

Anonymous function wikipedia , lookup

Standard ML wikipedia , lookup

Intuitionistic type theory wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Lambda lifting wikipedia , lookup

Lambda calculus wikipedia , lookup

Lambda calculus definition wikipedia , lookup

Curry–Howard correspondence wikipedia , lookup

Combinatory logic wikipedia , lookup

Transcript
overview
recursion
equational programming
2015 01 14
lecture 4
Haskell: monads
types
simply typed lambda calculus
schema
recursion
recursive functions: examples in Haskell
factorial n = if (n==0)
then 1
else (n * factorial (n-1))
Haskell: monads
types
som [] = 0
som (h:t) = h + (som t)
simply typed lambda calculus
length [] = 0
length (h:t) = (length t) + 1
length in lambda-calculus
recall: fixed point combinators
Curry’s fixed point combinator:
Y = λf . (λx. f (x x)) (λx. f (x x))
how do we define length in the λ-calculus?
Turing’s fixed point combinator:
T = (λx. λy . y (x x y )) (λx. λy . y (x x y ))
recursive functions: method
we try to define:
from Haskell to lambda
Haskell is translated to core Haskell which can be seen as λ+
G with G = . . . G . . .
hence we look for:
G with G = (λg . . . . g . . .) G
hence we take:
a fixed point of λg . . . . g . . .
length [] = 0
length (h:t) = (length t) + 1
becomes
length l = case l of
[] -> 0
(h:t) -> 1 + length t
becomes (...) becomes roughly
that is, using a fixed point combinator Y we define:
Y (λa. λl. if (l == []) then 0 else (λ(h : t). (1 + (a t))) l)
G = Y (λg . . . . g . . .)
lambda calculi with patterns
lambda notation: digression
• from set to predicate: characteristic function
computation by reduction and pattern matching:
χS (x) =
first projection:
(λhx, y i. x) h3, 5i → x[x, y := 3, 5] = 3
length of a non-empty list:
(λ(h : t). 1 + (length t)) (1 : nil) → (1 + (length t))[t := nil] = 1 + length nil
true als x ∈ S
false als x 6∈ S
• from predicate to set
P(x) = {x | P(x) = true}
• using lambda
λ-term P for characteristic function
Russell’s paradox
expressivity
in set theory:
define R := {X | X 6∈ X }
then R ∈ R ⇔ R 6∈ R
in lambda calculus:
definine R := λx. ¬ (x x)
then R R =β ¬ (R R)
all Turing-computable functions are λ-definable and vice versa
schema
pure and impure functional programming languages
pure: Haskell, Miranda
recursion
programs are pure mathematical functions
Haskell: monads
pure equational reasoning
types
no side effects
simply typed lambda calculus
impure: ML, Scheme
possible side effects, for example assigment and exceptions
more difficult to reason about
monads
overview
recursion
simulate in a pure setting side effects now called actions
Haskell: monads
encounter with monads in the practical work
no monads in the written exam
types
simply typed lambda calculus
types: background
• Church: typed λ-calculus to avoid paradoxes
• no self-application
Ω = (λx. x x) (λx. x x)
R = λx. not (x x)
Y = λf . (λx. f (x x)) (λx. f (x x))
• type theory alternative for set theory
schema
types voor programming languages
• Robin Milner (1934–2010):
well-typed programs
cannot go wrong
• additional check, additional documentation
• a bit more hassle
simply λ-calculus
recursion
Haskell: monads
types
• types
• terms
• typing system ‘selects typable terms’
simply typed lambda calculus
(simpele) types: examples
simple types: recursive definition
• basic types:
• int → int
• int → int → int
• (int → int) → int
nat, bool, int, . . .
• type variables:
a, b, c, . . .
• function types:
(A → B) with A and B types
• int → (int → bool) → bool
• → is associative to the right
simply typed lambda-calculus: idea
type declarations
we can only apply a ‘function’ F to an ‘argument’ P
type declaration for a variable:
x :A
if F : A → B and P : A
for some types A and B
hence no self-application anymore
environment (or context):
set of declarations for different variables
Γ = {x1 : A1 , . . . , xn : An }
typing system: idea
typing system: definition
• variable rule:
Γ, x : A ` x : A
formal system to derive statements of the form
Γ`M:A
• abstraction rule:
Γ, x : A ` M : B
Γ ` (λx. M) : A → B
‘M has type A in environment Γ’
if Γ ` M : A derivable
then M is typable in environment Γ with type A
• application rule:
Γ`F :A→B
Γ`M:A
Γ ` (F M) : B
example
digression: Curry-Howard isomorphism
• λx. x is typable
types correspond to propositions
• λx. λy . x is typable
terms or in fact typing derivations correspond met proofs
• λx. x x is not typable
more about this in the course Logical Verification