Top Banner
function theory F#
20

Function therory

Dec 17, 2014

Download

Technology

Tuomas Hietanen

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Function therory

function theoryF#

Page 2: Function therory

type inferenceF#

Page 3: Function therory

//Brackets aren't mandatory:

let f(x,y) = x+ylet f x y = x+y

Page 4: Function therory

//Function type:

let I x = x// x: 'a// I: 'a -> 'a

Page 5: Function therory

//Example 2:

let K x y = x// x: 'a// y: 'b// K: 'a -> 'b -> 'a

Page 6: Function therory

//Partial application

let constHi = K "Hello"let hi = constHi 123

val constHi : (int -> string)//hi : string ="Hello"

Page 7: Function therory

//Example 3:

let S x y z = x(z)(y(z))// x: f(a b)// x: a -> b -> c// z: a// y: a -> bS x y z: (a -> b -> c) -> (a -> b) -> a -

> c

Page 8: Function therory

//Exercise: What are these functions?

val it : (('a -> 'b) -> 'a list -> 'b list)

val it : (('a -> 'b -> 'a) -> 'a -> 'b list -> 'a)

val it : ('a * 'b -> 'a)

Page 9: Function therory

function composition

F#

f(g(x))

Page 10: Function therory

//function composition

let combine f g x = g(f(x)) Let combine f g x = (f>>g)x// f: 'a -> 'b// g: 'b -> 'c// x: 'acombine f g x: (a -> b) -> (b -> c) -> a -

> c

Page 11: Function therory

//You can remove the parameter from the last one!

let combine f g= (f>>g)// combine: ('a -> 'b) -> ('b -> 'c) -> ('a -> 'c)

Page 12: Function therory

result: generic abstraction whitout re- ference to

concrete types

let handle = (save >> validate >> send)

Page 13: Function therory

monadsF#

Page 14: Function therory

thought behind LINQ

“list monad”IEnumerable<T>

“maybe monad”Nullable<T>

Page 15: Function therory

three types of operations

1.

2.

3.'a -> M<’a>

M<'a> -> M<'b>

M<'a> -> 'b

Page 16: Function therory

typical example

2.

3.

M<'a> -> ('a -> M<'b>) -> M<'b>

M<'a> -> 'b -> ('b -> 'a -> 'b) -> 'b

Page 17: Function therory

combinatorsF#

Page 18: Function therory

S- K- I- combinators

Like we started:I x = x

K x y = x

S x y z = x z (y z)

Page 19: Function therory

y-combinator

Y = S (K (S I I)) (S (S (K S) K) (K (S I I)))

f(x) when x=f?f(f) = f and f=f(f)

f(g) = g(f(g))

Page 20: Function therory

references / links• http://en.wikipedia.org/wiki/SKI_combinator_calculus

• http://en.wikipedia.org/wiki/Fixed_point_combinator

• http://community.bartdesmet.net/blogs/bart/archive/2009/08/17/mis-using-c-4-0-dynamic-type-free-lambda-calculus-church-numerals-and-more.aspx

• http://blogs.msdn.com/b/madst/archive/2007/05/11/recursive-lambda-expressions.aspx

• http://www.madore.org/~david/programs/unlambda/

• http://channel9.msdn.com/Shows/Going+Deep/Brian-Beckman-Dont-fear-the-Monads

• http://channel9.msdn.com/Shows/Going+Deep/C9-Lectures-Greg-Meredith-Monadic-Design-Patterns-for-the-Web-Introduction-to-Monads

• http://blogs.msdn.com/b/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx

• http://channel9.msdn.com/Shows/Going+Deep/Bart-De-Smet-MinLINQ-The-Essence-of-LINQ

• http://ttic.uchicago.edu/~dreyer/course/papers/wadler.pdf

• http://www.meetup.com/FSharpHelsinki/messages/boards/forum/1395955