SlideShare une entreprise Scribd logo
1  sur  71
Télécharger pour lire hors ligne
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 specification - 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 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
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 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
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)
Don’t 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)
Don’t 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 specification
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 specification
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<’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
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
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
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
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
definition 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
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
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
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 reverse all your arrows
Nicolas Rolland Functional prototyping August 3, 2014 22 / 34
Functional programming and duality is old : we know it
works
Nicolas Rolland Functional prototyping August 3, 2014 23 / 34
encoding in Fsharp
// ∀ t . Stack(t)
type StackOps<’Rep> = {
empty :’Rep
isEmpty :’Rep -> bool
push : (int *’Rep) -> ’Rep
pop :’Rep -> ’Rep
top :’Rep -> int
}
// ∃ t. Stack(t)
[<AbstractClass>]
type ExistentialStack()=
// ∀ x. (∀ t . Stack(t) − > x) − > x ∼= ∃ t. Stack(t)
abstract Apply : StackClient<’x> -> ’x
and Stack<’Rep>(e:’Rep,ops:StackOps<’Rep>)=
inherit ExistentialStack()
member this.witness = e
member this.getOps = ops
override this.Apply(mc) = mc.Apply<’Rep>(this)
and StackClient<’y> = interface
// ∀ t. Stack(t) − > y
abstract member Apply<’t> : Stack<’t> -> ’y
Nicolas Rolland Functional prototyping August 3, 2014 24 / 34
stack demo
Nicolas Rolland Functional prototyping August 3, 2014 25 / 34
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
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
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
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
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
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
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
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
lambda man !
Nicolas Rolland Functional prototyping August 3, 2014 29 / 34
start with the types
Nicolas Rolland Functional prototyping August 3, 2014 30 / 34
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
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
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
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
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
demo time
Nicolas Rolland Functional prototyping August 3, 2014 33 / 34
There are many more tricks to practice
Nicolas Rolland Functional prototyping August 3, 2014 34 / 34
The End
Nicolas Rolland Functional prototyping August 3, 2014 35 / 34

Contenu connexe

Similaire à Tokyo F# meetup 14-08-03

Python for Scientific Computing
Python for Scientific ComputingPython for Scientific Computing
Python for Scientific ComputingAlbert DeFusco
 
Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methodsPranavSB
 
Life & Work of Robin Milner | Turing100@Persistent
Life & Work of Robin Milner | Turing100@PersistentLife & Work of Robin Milner | Turing100@Persistent
Life & Work of Robin Milner | Turing100@PersistentPersistent Systems Ltd.
 
Machine learning session3(intro to python)
Machine learning   session3(intro to python)Machine learning   session3(intro to python)
Machine learning session3(intro to python)Abhimanyu Dwivedi
 
Cleaner Code: How Clean Code is Functional Code
Cleaner Code: How Clean Code is Functional CodeCleaner Code: How Clean Code is Functional Code
Cleaner Code: How Clean Code is Functional CodeDave Fancher
 
Dynamic Type Inference for Gradual Hindley–Milner Typing
Dynamic Type Inference for Gradual Hindley–Milner TypingDynamic Type Inference for Gradual Hindley–Milner Typing
Dynamic Type Inference for Gradual Hindley–Milner TypingYusuke Miyazaki
 
Fate and functional programming
Fate and functional programmingFate and functional programming
Fate and functional programmingRino Jose
 
An overview on python commands for solving the problems
An overview on python commands for solving the problemsAn overview on python commands for solving the problems
An overview on python commands for solving the problemsRavikiran708913
 
Tuples, Dicts and Exception Handling
Tuples, Dicts and Exception HandlingTuples, Dicts and Exception Handling
Tuples, Dicts and Exception HandlingPranavSB
 
A Gentle Introduction to Coding ... with Python
A Gentle Introduction to Coding ... with PythonA Gentle Introduction to Coding ... with Python
A Gentle Introduction to Coding ... with PythonTariq Rashid
 
Kotlin for Android Developers - 2
Kotlin for Android Developers - 2Kotlin for Android Developers - 2
Kotlin for Android Developers - 2Mohamed Nabil, MSc.
 
Python_Unit_1.pdf
Python_Unit_1.pdfPython_Unit_1.pdf
Python_Unit_1.pdfalaparthi
 
An introduction to functional programming with Swift
An introduction to functional programming with SwiftAn introduction to functional programming with Swift
An introduction to functional programming with SwiftFatih Nayebi, Ph.D.
 
Core Concept_Python.pptx
Core Concept_Python.pptxCore Concept_Python.pptx
Core Concept_Python.pptxAshwini Raut
 
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxpriestmanmable
 

Similaire à Tokyo F# meetup 14-08-03 (20)

Python for Scientific Computing
Python for Scientific ComputingPython for Scientific Computing
Python for Scientific Computing
 
Python basics
Python basicsPython basics
Python basics
 
Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methods
 
Life & Work of Robin Milner | Turing100@Persistent
Life & Work of Robin Milner | Turing100@PersistentLife & Work of Robin Milner | Turing100@Persistent
Life & Work of Robin Milner | Turing100@Persistent
 
Machine learning session3(intro to python)
Machine learning   session3(intro to python)Machine learning   session3(intro to python)
Machine learning session3(intro to python)
 
Cleaner Code: How Clean Code is Functional Code
Cleaner Code: How Clean Code is Functional CodeCleaner Code: How Clean Code is Functional Code
Cleaner Code: How Clean Code is Functional Code
 
Dynamic Type Inference for Gradual Hindley–Milner Typing
Dynamic Type Inference for Gradual Hindley–Milner TypingDynamic Type Inference for Gradual Hindley–Milner Typing
Dynamic Type Inference for Gradual Hindley–Milner Typing
 
Fate and functional programming
Fate and functional programmingFate and functional programming
Fate and functional programming
 
An overview on python commands for solving the problems
An overview on python commands for solving the problemsAn overview on python commands for solving the problems
An overview on python commands for solving the problems
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
 
Python Part 1
Python Part 1Python Part 1
Python Part 1
 
Tuples, Dicts and Exception Handling
Tuples, Dicts and Exception HandlingTuples, Dicts and Exception Handling
Tuples, Dicts and Exception Handling
 
A Gentle Introduction to Coding ... with Python
A Gentle Introduction to Coding ... with PythonA Gentle Introduction to Coding ... with Python
A Gentle Introduction to Coding ... with Python
 
Python by Rj
Python by RjPython by Rj
Python by Rj
 
Kotlin for Android Developers - 2
Kotlin for Android Developers - 2Kotlin for Android Developers - 2
Kotlin for Android Developers - 2
 
Python_Unit_1.pdf
Python_Unit_1.pdfPython_Unit_1.pdf
Python_Unit_1.pdf
 
Python
PythonPython
Python
 
An introduction to functional programming with Swift
An introduction to functional programming with SwiftAn introduction to functional programming with Swift
An introduction to functional programming with Swift
 
Core Concept_Python.pptx
Core Concept_Python.pptxCore Concept_Python.pptx
Core Concept_Python.pptx
 
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
 

Dernier

Site specific recombination and transposition.........pdf
Site specific recombination and transposition.........pdfSite specific recombination and transposition.........pdf
Site specific recombination and transposition.........pdfCherry
 
THE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptx
THE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptxTHE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptx
THE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptxANSARKHAN96
 
Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.Cherry
 
GBSN - Microbiology (Unit 3)Defense Mechanism of the body
GBSN - Microbiology (Unit 3)Defense Mechanism of the body GBSN - Microbiology (Unit 3)Defense Mechanism of the body
GBSN - Microbiology (Unit 3)Defense Mechanism of the body Areesha Ahmad
 
POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.Cherry
 
Terpineol and it's characterization pptx
Terpineol and it's characterization pptxTerpineol and it's characterization pptx
Terpineol and it's characterization pptxMuhammadRazzaq31
 
FAIRSpectra - Enabling the FAIRification of Analytical Science
FAIRSpectra - Enabling the FAIRification of Analytical ScienceFAIRSpectra - Enabling the FAIRification of Analytical Science
FAIRSpectra - Enabling the FAIRification of Analytical ScienceAlex Henderson
 
Call Girls Ahmedabad +917728919243 call me Independent Escort Service
Call Girls Ahmedabad +917728919243 call me Independent Escort ServiceCall Girls Ahmedabad +917728919243 call me Independent Escort Service
Call Girls Ahmedabad +917728919243 call me Independent Escort Serviceshivanisharma5244
 
biology HL practice questions IB BIOLOGY
biology HL practice questions IB BIOLOGYbiology HL practice questions IB BIOLOGY
biology HL practice questions IB BIOLOGY1301aanya
 
Concept of gene and Complementation test.pdf
Concept of gene and Complementation test.pdfConcept of gene and Complementation test.pdf
Concept of gene and Complementation test.pdfCherry
 
Thyroid Physiology_Dr.E. Muralinath_ Associate Professor
Thyroid Physiology_Dr.E. Muralinath_ Associate ProfessorThyroid Physiology_Dr.E. Muralinath_ Associate Professor
Thyroid Physiology_Dr.E. Muralinath_ Associate Professormuralinath2
 
Cyanide resistant respiration pathway.pptx
Cyanide resistant respiration pathway.pptxCyanide resistant respiration pathway.pptx
Cyanide resistant respiration pathway.pptxCherry
 
Genetics and epigenetics of ADHD and comorbid conditions
Genetics and epigenetics of ADHD and comorbid conditionsGenetics and epigenetics of ADHD and comorbid conditions
Genetics and epigenetics of ADHD and comorbid conditionsbassianu17
 
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 bAsymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 bSérgio Sacani
 
Role of AI in seed science Predictive modelling and Beyond.pptx
Role of AI in seed science  Predictive modelling and  Beyond.pptxRole of AI in seed science  Predictive modelling and  Beyond.pptx
Role of AI in seed science Predictive modelling and Beyond.pptxArvind Kumar
 
Module for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learningModule for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learninglevieagacer
 
Kanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
Kanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot GirlsKanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
Kanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot GirlsDeepika Singh
 
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune WaterworldsBiogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune WaterworldsSérgio Sacani
 

Dernier (20)

Site specific recombination and transposition.........pdf
Site specific recombination and transposition.........pdfSite specific recombination and transposition.........pdf
Site specific recombination and transposition.........pdf
 
THE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptx
THE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptxTHE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptx
THE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptx
 
Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.
 
GBSN - Microbiology (Unit 3)Defense Mechanism of the body
GBSN - Microbiology (Unit 3)Defense Mechanism of the body GBSN - Microbiology (Unit 3)Defense Mechanism of the body
GBSN - Microbiology (Unit 3)Defense Mechanism of the body
 
POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.
 
Terpineol and it's characterization pptx
Terpineol and it's characterization pptxTerpineol and it's characterization pptx
Terpineol and it's characterization pptx
 
FAIRSpectra - Enabling the FAIRification of Analytical Science
FAIRSpectra - Enabling the FAIRification of Analytical ScienceFAIRSpectra - Enabling the FAIRification of Analytical Science
FAIRSpectra - Enabling the FAIRification of Analytical Science
 
Call Girls Ahmedabad +917728919243 call me Independent Escort Service
Call Girls Ahmedabad +917728919243 call me Independent Escort ServiceCall Girls Ahmedabad +917728919243 call me Independent Escort Service
Call Girls Ahmedabad +917728919243 call me Independent Escort Service
 
biology HL practice questions IB BIOLOGY
biology HL practice questions IB BIOLOGYbiology HL practice questions IB BIOLOGY
biology HL practice questions IB BIOLOGY
 
Concept of gene and Complementation test.pdf
Concept of gene and Complementation test.pdfConcept of gene and Complementation test.pdf
Concept of gene and Complementation test.pdf
 
Thyroid Physiology_Dr.E. Muralinath_ Associate Professor
Thyroid Physiology_Dr.E. Muralinath_ Associate ProfessorThyroid Physiology_Dr.E. Muralinath_ Associate Professor
Thyroid Physiology_Dr.E. Muralinath_ Associate Professor
 
Cyanide resistant respiration pathway.pptx
Cyanide resistant respiration pathway.pptxCyanide resistant respiration pathway.pptx
Cyanide resistant respiration pathway.pptx
 
Genetics and epigenetics of ADHD and comorbid conditions
Genetics and epigenetics of ADHD and comorbid conditionsGenetics and epigenetics of ADHD and comorbid conditions
Genetics and epigenetics of ADHD and comorbid conditions
 
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 bAsymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
 
Role of AI in seed science Predictive modelling and Beyond.pptx
Role of AI in seed science  Predictive modelling and  Beyond.pptxRole of AI in seed science  Predictive modelling and  Beyond.pptx
Role of AI in seed science Predictive modelling and Beyond.pptx
 
PATNA CALL GIRLS 8617370543 LOW PRICE ESCORT SERVICE
PATNA CALL GIRLS 8617370543 LOW PRICE ESCORT SERVICEPATNA CALL GIRLS 8617370543 LOW PRICE ESCORT SERVICE
PATNA CALL GIRLS 8617370543 LOW PRICE ESCORT SERVICE
 
Module for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learningModule for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learning
 
Kanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
Kanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot GirlsKanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
Kanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
 
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune WaterworldsBiogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
 
Early Development of Mammals (Mouse and Human).pdf
Early Development of Mammals (Mouse and Human).pdfEarly Development of Mammals (Mouse and Human).pdf
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
  • 9. Using types Nicolas Rolland Functional prototyping August 3, 2014 5 / 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
  • 13. Warm-up exercices Nicolas Rolland Functional prototyping August 3, 2014 6 / 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
  • 29. Object Oriented way Nicolas Rolland Functional prototyping August 3, 2014 11 / 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
  • 37. Together Nicolas Rolland Functional prototyping August 3, 2014 14 / 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
  • 48. duality Nicolas Rolland Functional prototyping August 3, 2014 21 / 34
  • 49. duality IEnumerable vs IObservable Universals vs Existentials are examples of applying duality Nicolas Rolland Functional prototyping August 3, 2014 21 / 34
  • 50. Erik Meijer - he will reverse all your arrows Nicolas Rolland Functional prototyping August 3, 2014 22 / 34
  • 51. Functional programming and duality is old : we know it works Nicolas Rolland Functional prototyping August 3, 2014 23 / 34
  • 52. encoding in Fsharp // ∀ t . Stack(t) type StackOps<’Rep> = { empty :’Rep isEmpty :’Rep -> bool push : (int *’Rep) -> ’Rep pop :’Rep -> ’Rep top :’Rep -> int } // ∃ t. Stack(t) [<AbstractClass>] type ExistentialStack()= // ∀ x. (∀ t . Stack(t) − > x) − > x ∼= ∃ t. Stack(t) abstract Apply : StackClient<’x> -> ’x and Stack<’Rep>(e:’Rep,ops:StackOps<’Rep>)= inherit ExistentialStack() member this.witness = e member this.getOps = ops override this.Apply(mc) = mc.Apply<’Rep>(this) and StackClient<’y> = interface // ∀ t. Stack(t) − > y abstract member Apply<’t> : Stack<’t> -> ’y Nicolas Rolland Functional prototyping August 3, 2014 24 / 34
  • 53. stack demo Nicolas Rolland Functional prototyping August 3, 2014 25 / 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
  • 62. lambda man ! Nicolas Rolland Functional prototyping August 3, 2014 29 / 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
  • 69. demo time Nicolas Rolland Functional prototyping August 3, 2014 33 / 34
  • 70. There are many more tricks to practice Nicolas Rolland Functional prototyping August 3, 2014 34 / 34
  • 71. The End Nicolas Rolland Functional prototyping August 3, 2014 35 / 34