75 lines
1 KiB
Markdown
75 lines
1 KiB
Markdown
```
|
|
given A
|
|
def identity: A -> A
|
|
λ x => x
|
|
end def
|
|
|
|
given A
|
|
class Semigroup A
|
|
def combine: A -> A -> A
|
|
end class
|
|
|
|
given A :: Semigroup
|
|
class Monoid A
|
|
def empty: A
|
|
end class
|
|
|
|
given F *
|
|
class Functor
|
|
given A
|
|
given B
|
|
def map: F A -> (A -> B) -> F B
|
|
end class
|
|
|
|
given F * :: Functor
|
|
class Apply
|
|
given A
|
|
given B
|
|
def ap: F (A -> B) -> F A -> F B
|
|
end class
|
|
|
|
given F * :: Apply
|
|
class Applicative
|
|
given A
|
|
def pure: A -> F A
|
|
|
|
def unit: F ()
|
|
pure ()
|
|
end def
|
|
end class
|
|
|
|
given F * :: Apply
|
|
class FlatMap
|
|
given A
|
|
given B
|
|
def fmap: F A -> (A -> F B) -> F B
|
|
|
|
given A
|
|
def flatten: F (F A) -> F A
|
|
λ ffa => fmap ffa identity
|
|
end def
|
|
end class
|
|
|
|
given F * :: FlatMap, Applicative
|
|
class Monad
|
|
end class
|
|
|
|
instance Semigroup Int32
|
|
def combine: Int32 -> Int32 -> Int32
|
|
int32_add
|
|
end def
|
|
end instance
|
|
|
|
given A :: Semigroup
|
|
def +: A -> A -> A
|
|
combine
|
|
end def
|
|
|
|
def ex1: Int32 -> Int32
|
|
λ x => x + x
|
|
end def
|
|
|
|
def ex2: Int32 -> Int32
|
|
λ x => combine x x
|
|
end def
|
|
```
|