Question
In the book Category Theory for Programmers by Bartosz Milewski, chapter 4.3.
You must code a Kleisli category where morphisms are partial functions. Here is my attempt which does not compile:
data Optional a = Valid a | Invalid deriving (Show)
return :: a -> Optional a
return x = Valid x
(>=>) :: (a -> Optional b) -> (b -> Optional c) -> (a -> Optional c)
f (>=>) g = \x ->
let s = f x
in | s == Valid v = g v
| s == Invalid = Invalid
In the >=>
operator definition, I want to pattern-match the intermediate value s
to test if is Valid
(and then call f
) or if it is Invalid
(and then return Invalid
). How can I do this ?