22. val gpp3: string -> string -> AttributeQuery<string>
val prodDataXml: string -> AttributeQuery<string>
val select: int -> string -> AttributeQuery<string>
23. // type AttributeQuery<'T> = QueryContext -> 'T
let gpp3 path key =
fun ctx -> ctx.Gpp3Data.Get path key
24. // type AttributeQuery<'T> = QueryContext -> 'T
let select idx s =
fun ctx ->
let vs = s.Split ','
vs.[idx] // TODO: error handling
28. // type AttributeQuery<'T> = QueryContext -> 'T
let Bind firstq fsecondq =
fun ctx ->
let first = firstq ctx
let secondq = fsecondq first
let second = secondq ctx
second
34. let q : AttributeQuery<string> =
bandLimits >>= select0
35. val Bind:
AttributeQuery<'T> // bandlimits
-> ('T -> AttributeQuery<'U>) // select0
-> AttributeQuery<'U>
val Return: 'T -> AttributeQuery<'T>
36. // type AttributeQuery<'T> = QueryContext -> 'T
let Bind firstq fsecondq =
fun ctx ->
let first = firstq ctx // bandlimits ctx
let secondq = fsecondq first // select0 first
let second = secondq ctx
second
50. “A monad is a monoid in the category of
endofunctors,
what's the problem?”
Philip Wadler
(Fake but funny)
51. // Monad type class
// https://wiki.haskell.org/Monad
type M<'T>
val Bind : M<'T> -> ('T -> M<'U>) -> M<'U>
val Return: 'T -> M<'T>
52. // Monad laws
// https://wiki.haskell.org/Monad_laws
let (>=>) t u = fun v -> Bind (t v) u
Left identity : Return >=> g ≡ g
Right identity: f >=> Return ≡ f
Associativity :
(f >=> g) >=> h
≡
f >=> (g >=> h)
53. Left identity : x + 0 ≡ x
Right identity: 0 + x ≡ x
Associativity : (x + y) + z ≡ x + (y + z)
54. The Essence of Monad
”The essence of monad is thus separation of
composition timeline from the composed computation's
execution timeline, as well as the ability of
computation to implicitly carry extra data, as
pertaining to the computation itself, in addition to
its one (hence the name) output, that it will
produce when run (or queried, or called upon).“
https://wiki.haskell.org/Monad
55. Monads are useful
We like to define a query to an attribute
Query implies separation between composition and
execution
We don’t want to our query to rely on singletons
Makes testing and composition (if effectful) difficult
No singletons implies an extra data
This is the essence of Monads