Early Development of Mammals (Mouse and Human).pdf
Tokyo F# meetup 14-08-03
1. Functional thinking, a universal tool for the digital world
Nicolas Rolland
August 3, 2014
Nicolas Rolland Functional prototyping August 3, 2014 1 / 34
2. 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
3. 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
4. Types
What is a type ?
Nicolas Rolland Functional prototyping August 3, 2014 4 / 34
5. Types
What is a type ?
Type
A type is a set of values.
Nicolas Rolland Functional prototyping August 3, 2014 4 / 34
6. 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
7. 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
8. 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
10. Using types
by restriction we get correctness - The computer tells us the errors
Nicolas Rolland Functional prototyping August 3, 2014 5 / 34
11. Using types
by restriction we get correctness - The computer tells us the errors
restriction tells readers a specification - We can communicate to other
people
Nicolas Rolland Functional prototyping August 3, 2014 5 / 34
12. Using types
by restriction we get correctness - The computer tells us the errors
restriction tells readers a specification - 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
14. what can I do with a value of this type ?
Type1
() −→ T
Nicolas Rolland Functional prototyping August 3, 2014 7 / 34
15. 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
16. 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
17. 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
18. 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
19. 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
20. 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
21. 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
22. 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
23. 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
24. 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
25. can you recognize IEnumerable ?
What is a value of type IEnumerable ?
Nicolas Rolland Functional prototyping August 3, 2014 10 / 34
26. can you recognize IEnumerable ?
What is a value of type IEnumerable ? a sequence of items
Nicolas Rolland Functional prototyping August 3, 2014 10 / 34
27. 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
28. 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
30. 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 first ?
What is the value of current
after enumeration is finished ?
How can I make sure current is
not used after enumeration is
finished ?
Nicolas Rolland Functional prototyping August 3, 2014 11 / 34
31. IObservable / Reactive
type IObservable < T > = (Item of T → ()) → ()
Nicolas Rolland Functional prototyping August 3, 2014 12 / 34
32. IObservable / Reactive
type IObservable < T > = (Item of T → ()) → ()
IEnumerable
type IEnumerable < T > = () → (() → Item of T)
Don’t they look similar .... ?
Nicolas Rolland Functional prototyping August 3, 2014 12 / 34
33. IObservable / Reactive
type IObservable < T > = (Item of T → ()) → ()
IEnumerable
type IEnumerable < T > = () → (() → Item of T)
Don’t they look similar .... ?
why ?
Nicolas Rolland Functional prototyping August 3, 2014 12 / 34
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
35. 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 specification
Nicolas Rolland Functional prototyping August 3, 2014 13 / 34
36. 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 specification
Domain Driven Design : the architecture is the code
Nicolas Rolland Functional prototyping August 3, 2014 13 / 34
38. 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
39. Universals
Type
let empty () = System.Collections.Generic.List()
let stringList = empty()
do stringList.Add("hello")
let intList = empty()
do intList.Add(1)
type List<’T> = | Empty | Cons of (’T * List<’T>)
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
40. Universals
Type
let empty () = System.Collections.Generic.List()
let stringList = empty()
do stringList.Add("hello")
let intList = empty()
do intList.Add(1)
type List<’T> = | Empty | Cons of (’T * List<’T>)
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
41. Universals
Type
let empty () = System.Collections.Generic.List()
let stringList = empty()
do stringList.Add("hello")
let intList = empty()
do intList.Add(1)
type List<’T> = | Empty | Cons of (’T * List<’T>)
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
42. Universals
What is the type of Empty ? Empty : ∀T. List < T >
”I don’t 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
43. 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
44. 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
45. 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
definition related to universals !
Nicolas Rolland Functional prototyping August 3, 2014 19 / 34
46. 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
definition related to universals !
Universals definition
empty : ∀T. () → List < T >
suggests ”existentials”
absStack : ∃T{empty : () → T
push : int ∗ T → T
pop : T → T
top : T → int}
” I’ll 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
47. absStack : ∃T{empty : () → T
push : int ∗ T → T
pop : T → T
top : T → int}
implementer
Know about the specific type T
user
does not know about the specific
type T, only about the interface
Nicolas Rolland Functional prototyping August 3, 2014 20 / 34
54. Existentials - References
Luca Cardelli, Peter Wegner (1985)
On Understanding Types, Data Abstraction, and Polymorphism
William Cook (2009)
On Understanding Data Abstraction, Revisited
Nicolas Rolland Functional prototyping August 3, 2014 26 / 34
55. 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 27 / 34
56. How do we go from here to there ?
string
”[a number is like 1234 but
can also be 9.12 ]”
value
let b = List([String "a"
String "number"
String "is"
String "like"
NumberI 1234
List ([
String "but"
String "can"
String "also"
String "be"
NumberF 9.12
]) ])
Nicolas Rolland Functional prototyping August 3, 2014 28 / 34
57. How do we go from here to there ?
string
”[a number is like 1234 but
can also be 9.12 ]”
value
let b = List([String "a"
String "number"
String "is"
String "like"
NumberI 1234
List ([
String "but"
String "can"
String "also"
String "be"
NumberF 9.12
]) ])
This is a job for .....
Nicolas Rolland Functional prototyping August 3, 2014 28 / 34
58. How do we go from here to there ?
string
”[a number is like 1234 but
can also be 9.12 ]”
value
let b = List([String "a"
String "number"
String "is"
String "like"
NumberI 1234
List ([
String "but"
String "can"
String "also"
String "be"
NumberF 9.12
]) ])
This is a job for ..... grep ?
Nicolas Rolland Functional prototyping August 3, 2014 28 / 34
59. How do we go from here to there ?
string
”[a number is like 1234 but
can also be 9.12 ]”
value
let b = List([String "a"
String "number"
String "is"
String "like"
NumberI 1234
List ([
String "but"
String "can"
String "also"
String "be"
NumberF 9.12
]) ])
This is a job for ..... grep ? imperative ?
Nicolas Rolland Functional prototyping August 3, 2014 28 / 34
60. How do we go from here to there ?
string
”[a number is like 1234 but
can also be 9.12 ]”
value
let b = List([String "a"
String "number"
String "is"
String "like"
NumberI 1234
List ([
String "but"
String "can"
String "also"
String "be"
NumberF 9.12
]) ])
This is a job for ..... grep ? imperative ? or...
Nicolas Rolland Functional prototyping August 3, 2014 28 / 34
61. How do we go from here to there ?
string
”[a number is like 1234 but
can also be 9.12 ]”
value
let b = List([String "a"
String "number"
String "is"
String "like"
NumberI 1234
List ([
String "but"
String "can"
String "also"
String "be"
NumberF 9.12
]) ])
This is a job for ..... grep ? imperative ? or... λ !
Nicolas Rolland Functional prototyping August 3, 2014 28 / 34
63. start with the types
Nicolas Rolland Functional prototyping August 3, 2014 30 / 34
64. start with the types
value type
type Val = | List of Val list
| NumberI of int
| NumberF of double
| String of string
value
let b = List([String "a"
String "number"
String "is"
String "like"
NumberI 1234
List ([
String "but"
String "can"
String "also"
String "be"
NumberF 9.12
]) ])
Nicolas Rolland Functional prototyping August 3, 2014 30 / 34
65. parser return type - first try
type ParseReturn<’ret> = | Success of ’ret | Failure of string
type Parser<’ret,’token> =
List<’token> -> ParseReturn<’ret> * List<’token>
Nicolas Rolland Functional prototyping August 3, 2014 31 / 34
66. parser return type - first try
type ParseReturn<’ret> = | Success of ’ret | Failure of string
type Parser<’ret,’token> =
List<’token> -> ParseReturn<’ret> * List<’token>
reading the parser type
a parser takes a list of token in
it returns a ParserReturn , and the tokens not yet processed
Nicolas Rolland Functional prototyping August 3, 2014 31 / 34
67. parser return type - first try
type ParseReturn<’ret> = | Success of ’ret | Failure of string
type Parser<’ret,’token> =
List<’token> -> ParseReturn<’ret> * List<’token>
reading the parser type
a parser takes a list of token in
it returns a ParserReturn , and the tokens not yet processed
a ParserReturn is the value represented by the token consumed
Ok, but........ ”9.12 ” :
9 is a number.. but if we recognize ”9” as 9 only, what do we do for
”.12” ?
=⇒ the result of a parser depends also on the rest of the string
If it matters, it should be part of the returned type of a parser !
Nicolas Rolland Functional prototyping August 3, 2014 31 / 34
68. parser return type
type ParseReturn<’ret,’token> =
| Success of ’ret *(’token list)
| Failure of string
type Parser<’ret,’token> =
’token list -> ParseReturn<’ret,’token>
e.g. ”9.12 ”
reading the parser type
Each success contains a recognized value along with the
”rest”
Because the notion of what is the ”rest” is dependent on the
Val produced
ParseReturn now contains a coherent and actionable unit of
information
Nicolas Rolland Functional prototyping August 3, 2014 32 / 34