Tokyo F# meetup 14-08-03

• Published on
23-Aug-2014

• View
940

21

Embed Size (px)

DESCRIPTION

Functional programming with F# (FSharp)

Transcript

• Functional thinking, a universal tool for the digital world Nicolas Rolland August 3, 2014 Nicolas Rolland Functional prototyping August 3, 2014 1 / 34
• Table of Contents 1 Types why types ? Warm-up exercices IEnumerable and IObservable 2 Universals, programming against the interface and existentials universals it sounds like L in SOLID .. existentials 3 Prototyping the functional way Parser combinator Nicolas Rolland Functional prototyping August 3, 2014 2 / 34
• Table of Contents 1 Types why types ? Warm-up exercices IEnumerable and IObservable 2 Universals, programming against the interface and existentials universals it sounds like L in SOLID .. existentials 3 Prototyping the functional way Parser combinator Nicolas Rolland Functional prototyping August 3, 2014 3 / 34
• Types What is a type ? Nicolas Rolland Functional prototyping August 3, 2014 4 / 34
• Types What is a type ? Type A type is a set of values. Nicolas Rolland Functional prototyping August 3, 2014 4 / 34
• Types What is a type ? Type A type is a set of values. why do we need to think of it in typed functional programming ? a value is a value e.g. 1 Nicolas Rolland Functional prototyping August 3, 2014 4 / 34
• Types What is a type ? Type A type is a set of values. why do we need to think of it in typed functional programming ? a value is a value e.g. 1 a function is a value too ! let plus = (+) //fun x y -> x + y let plusone = plus 1 //fun x -> x + 1 let three = plusone 2 //3 Nicolas Rolland Functional prototyping August 3, 2014 4 / 34
• Types What is a type ? Type A type is a set of values. why do we need to think of it in typed functional programming ? a value is a value e.g. 1 a function is a value too ! let plus = (+) //fun x y -> x + y let plusone = plus 1 //fun x -> x + 1 let three = plusone 2 //3 hence everything is a value, everything has a type ! Nicolas Rolland Functional prototyping August 3, 2014 4 / 34
• Using types Nicolas Rolland Functional prototyping August 3, 2014 5 / 34
• Using types by restriction we get correctness - The computer tells us the errors Nicolas Rolland Functional prototyping August 3, 2014 5 / 34
• Using types by restriction we get correctness - The computer tells us the errors restriction tells readers a specication - We can communicate to other people Nicolas Rolland Functional prototyping August 3, 2014 5 / 34
• Using types by restriction we get correctness - The computer tells us the errors restriction tells readers a specication - We can communicate to other people Types should be helping you, not get in your way. No need to write the types in F#. Less work AND more security ! You can write the types for the purpose of communication with others Nicolas Rolland Functional prototyping August 3, 2014 5 / 34
• Warm-up exercices Nicolas Rolland Functional prototyping August 3, 2014 6 / 34
• what can I do with a value of this type ? Type1 () T Nicolas Rolland Functional prototyping August 3, 2014 7 / 34
• what can I do with a value of this type ? Type1 () T let a = (fun () -> 3) : unit -> int Nicolas Rolland Functional prototyping August 3, 2014 7 / 34
• what can I do with a value of this type ? Type1 () T let a = (fun () -> 3) : unit -> int lazy values ! Nicolas Rolland Functional prototyping August 3, 2014 7 / 34
• what can I do with a value of this type ? Type1 () T let a = (fun () -> 3) : unit -> int lazy values ! let delayedValue = fun () -> someComputationForLater()) T = () T Nicolas Rolland Functional prototyping August 3, 2014 7 / 34
• what can I do with a value of this type ? Type1 () T let a = (fun () -> 3) : unit -> int lazy values ! let delayedValue = fun () -> someComputationForLater()) T = () T let delay x = fun () x : T (() T) let force f = f () : (() T) T delay force = Id()T force delay = IdT Nicolas Rolland Functional prototyping August 3, 2014 7 / 34
• what can I do with a value of this type ? Type2 type Type2 < T >= | Case1 of (T Type2 < T >) | Case0 Nicolas Rolland Functional prototyping August 3, 2014 8 / 34
• what can I do with a value of this type ? Type2 type Type2 < T >= | Case1 of (T Type2 < T >) | Case0 let a = Case1(1,Case1(2,Case1(3,Case0))) Nicolas Rolland Functional prototyping August 3, 2014 8 / 34
• what can I do with a value of this type ? Type2 type Type2 < T >= | Case1 of (T Type2 < T >) | Case0 let a = Case1(1,Case1(2,Case1(3,Case0))) This is a list ! match l with | Case1(value, rest) -> ... //do something with value | Case0 -> ... //do something else Nicolas Rolland Functional prototyping August 3, 2014 8 / 34
• what can I do with a value of this type ? Type3 type RComp < T >= | NotYet of (() RComp < T >) | Finished of T Nicolas Rolland Functional prototyping August 3, 2014 9 / 34
• what can I do with a value of this type ? Type3 type RComp < T >= | NotYet of (() RComp < T >) | Finished of T let c = NotYet(fun () -> (*first part*) NotYet (fun () -> (*second part*) Finished pi)) Nicolas Rolland Functional prototyping August 3, 2014 9 / 34
• what can I do with a value of this type ? Type3 type RComp < T >= | NotYet of (() RComp < T >) | Finished of T let c = NotYet(fun () -> (*first part*) NotYet (fun () -> (*second part*) Finished pi)) Resumable computation ! Nicolas Rolland Functional prototyping August 3, 2014 9 / 34
• can you recognize IEnumerable ? What is a value of type IEnumerable ? Nicolas Rolland Functional prototyping August 3, 2014 10 / 34
• can you recognize IEnumerable ? What is a value of type IEnumerable ? a sequence of items Nicolas Rolland Functional prototyping August 3, 2014 10 / 34
• can you recognize IEnumerable ? What is a value of type IEnumerable ? a sequence of items Item type Item of T = | Finished | Value of T Nicolas Rolland Functional prototyping August 3, 2014 10 / 34
• can you recognize IEnumerable ? What is a value of type IEnumerable ? a sequence of items Item type Item of T = | Finished | Value of T IEnumerable type IEnumerable < T > = () (() Item of T) Nicolas Rolland Functional prototyping August 3, 2014 10 / 34
• Object Oriented way Nicolas Rolland Functional prototyping August 3, 2014 11 / 34
• Object Oriented way public interface IEnumerator { bool MoveNext(); Object Current { get; } void Reset(); } public interface IEnumerable { IEnumerator GetEnumerator(); } How can I read the protocol ? Should I call MoveNext rst ? What is the value of current after enumeration is nished ? How can I make sure current is not used after enumeration is nished ? Nicolas Rolland Functional prototyping August 3, 2014 11 / 34
• IObservable / Reactive type IObservable < T > = (Item of T ()) () Nicolas Rolland Functional prototyping August 3, 2014 12 / 34
• IObservable / Reactive type IObservable < T > = (Item of T ()) () IEnumerable type IEnumerable < T > = () (() Item of T) Dont they look similar .... ? Nicolas Rolland Functional prototyping August 3, 2014 12 / 34
• IObservable / Reactive type IObservable < T > = (Item of T ()) () IEnumerable type IEnumerable < T > = () (() Item of T) Dont they look similar .... ? why ? Nicolas Rolland Functional prototyping August 3, 2014 12 / 34
• What is a type - take 2 What is a type and why do we need to think of it ? Nicolas Rolland Functional prototyping August 3, 2014 13 / 34
• What is a type - take 2 What is a type and why do we need to think of it ? Type A type is a computable specication Nicolas Rolland Functional prototyping August 3, 2014 13 / 34
• What is a type - take 2 What is a type and why do we need to think of it ? Type A type is a computable specication Domain Driven Design : the architecture is the code Nicolas Rolland Functional prototyping August 3, 2014 13 / 34
• Together Nicolas Rolland Functional prototyping August 3, 2014 14 / 34
• Table of Contents 1 Types why types ? Warm-up exercices IEnumerable and IObservable 2 Universals, programming against the interface and existentials universals it sounds like L in SOLID .. existentials 3 Prototyping the functional way Parser combinator Nicolas Rolland Functional prototyping August 3, 2014 15 / 34
• Universals Type let empty () = System.Collections.Generic.List() let stringList = empty() do stringList.Add("hello") let intList = empty() do intList.Add(1) type List = | Empty | Cons of (T * List) let stringList = Empty let stringList = Cons ("hello", stringList) let intList = Empty let intList = Cons (1, intList) What is the type of Empty Nicolas Rolland Functional prototyping August 3, 2014 16 / 34
• Universals Type let empty () = System.Collections.Generic.List() let stringList = empty() do stringList.Add("hello") let intList = empty() do intList.Add(1) type List = | Empty | Cons of (T * List) let stringList = Empty let stringList = Cons ("hello", stringList) let intList = Empty let intList = Cons (1, intList) What is the type of Empty ? Empty : T. List < T > What is the type of Cons Nicolas Rolland Functional prototyping August 3, 2014 16 / 34
• Universals Type let empty () = System.Collections.Generic.List() let stringList = empty() do stringList.Add("hello") let intList = empty() do intList.Add(1) type List = | Empty | Cons of (T * List) let stringList = Empty let stringList = Cons ("hello", stringList) let intList = Empty let intList = Cons (1, intList) What is the type of Empty ? Empty : T. List < T > What is the type of Cons ? Cons : T. T List < T > List < T > Nicolas Rolland Functional prototyping August 3, 2014 16 / 34
• Universals What is the type of Empty ? Empty : T. List < T > I dont need to know anything about the type to do my job, I will only refer to it opaquely as T implementer of List < T > does not know T user of List < T > provide the type T Nicolas Rolland Functional prototyping August 3, 2014 17 / 34
• Liskov substitution principle - Program to interface type ICurrencyQuoteProvider = abstract getQuote : string -> double type BloombergCurrencyQuoteProvider(licenceToken:obj) = interface ICurrencyQuoteProvider with member this.getQuote cur = 120.0 //implementation logic type TestQuoteProvider() = interface ICurrencyQuoteProvider with member this.getQuote cur = 120.0 //program to **interface** type Basket(curProv:ICurrencyQuoteProvider) = //add to basket etc... member this.getTotalJPY () = 0.0 //implementation logic member this.getTotalIn cur = this.getTotalJPY () * curProv.getQuote cur let testBasket = Basket(TestQuoteProvider()) let prodBasket = Basket(BloombergCurrencyQuoteProvider(...)) Nicolas Rolland Functional prototyping August 3, 2014 18 / 34
• Existentials Interfaces allows to abstract in object oriented programming Like universals types, it hides something, but what is the relation ? Nicolas Rolland Functional prototyping August 3, 2014 19 / 34
• Existentials Interfaces allows to abstract in object oriented programming Like universals types, it hides something, but what is the relation ? Functional is Mathematics ! Abstraction in FP has to have a precise denition related to universals ! Nicolas Rolland Functional prototyping August 3, 2014 19 / 34
• Existentials Interfaces allows to abstract in object oriented programming Like universals types, it hides something, but what is the relation ? Functional is Mathematics ! Abstraction in FP has to have a precise denition related to universals ! Universals denition empty : T. () List < T > suggests existentials absStack : T{empty : () T push : int T T pop : T T top : T int} Ill use whatever type I want here; you wont know anything about the type, so you can only refer to it opaquely as X Nicolas Rolland Functional prototyping August 3, 2014 19 / 34
• absStack : T{empty : () T push : int T T pop : T T top : T int} implementer Know about the specic type T user does not know about the specic type T, only about the interface Nicolas Rolland Functional prototyping August 3, 2014 20 / 34
• duality Nicolas Rolland Functional prototyping August 3, 2014 21 / 34
• duality IEnumerable vs IObservable Universals vs Existentials are examples of applying duality Nicolas Rolland Functional prototyping August 3, 2014 21 / 34
• Erik Meijer - he will rev...