151 lines
2.7 KiB
Markdown
151 lines
2.7 KiB
Markdown
# Standard Type Classes
|
|
|
|
```
|
|
given A
|
|
class Semigroup
|
|
defn combine: A -> A -> A
|
|
end class
|
|
|
|
given A :: Semigroup
|
|
class Monoid
|
|
defn empty: A
|
|
end class
|
|
|
|
--- Type class for type constructors which can be mapped over.
|
|
---
|
|
--- ## Laws
|
|
---
|
|
--- **Composition**: `fa -> f -> g = fa -> (f ∘ g)`
|
|
--- **Identity**: `fa -> ((x) => x) = fa`
|
|
given F *
|
|
class Functor
|
|
--- Transform some wrapped data from one type to another, preserving the
|
|
--- wrapper.
|
|
---
|
|
--- @tparam A The type of input data.
|
|
--- @tparam B The type of output data.
|
|
--- @param fa The functor input.
|
|
--- @param f The function to transform data from `A` to `B`.
|
|
given A, B
|
|
defn map: F A -> (A -> B) -> F B
|
|
end class
|
|
|
|
given F * :: Functor
|
|
class Apply
|
|
given A, B
|
|
defn ap: F (A -> B) -> F A -> F B
|
|
end class
|
|
|
|
given F * :: Apply
|
|
class Applicative
|
|
given A
|
|
defn pure: A -> F A
|
|
|
|
fn unit: F
|
|
λ => pure ()
|
|
end fn
|
|
end class
|
|
|
|
--- Any Applicative satisfies Functor.
|
|
given F * :: Applicative
|
|
instance Functor F
|
|
given A, B
|
|
fn map: F A -> (A -> B) -> F B
|
|
λ fa f => ap (pure f) fa
|
|
end fn
|
|
end instance
|
|
|
|
given F * :: Apply
|
|
class FlatMap
|
|
given A, B
|
|
defn fmap: F A -> (A -> F B) -> F B
|
|
|
|
given A
|
|
fn flatten: F (F A) -> F A
|
|
λ ffa => fmap ffa (fa => fa)
|
|
end fn
|
|
end class
|
|
|
|
given F * :: FlatMap, Applicative
|
|
class Monad
|
|
end class
|
|
|
|
given F * :: Monad
|
|
instance Functor F
|
|
fn map: F A -> (A -> B) -> F B
|
|
λ fa f => fmap fa (λ a => pure (f a))
|
|
end fn
|
|
end instance
|
|
|
|
given F * *
|
|
class Bifunctor
|
|
given A, B, C, D
|
|
defn bimap F A B -> (A -> C) -> (B -> D) -> F C D
|
|
end class
|
|
|
|
given F * :: Functor
|
|
class CoFlatMap
|
|
given A, B
|
|
defn cofmap: F A -> (F A -> B) -> F B
|
|
|
|
given A
|
|
fn coflatten: F A -> F (F A)
|
|
λ fa => cofmap fa (λ f => f)
|
|
end fn
|
|
end class
|
|
|
|
given F * :: CoFlatMap
|
|
class CoMonad
|
|
given A
|
|
defn extract: F A -> A
|
|
end class
|
|
|
|
given A
|
|
class Show
|
|
defn show: A -> String
|
|
end class
|
|
|
|
given A, B
|
|
class Eq
|
|
defn eq: A -> B -> Boolean
|
|
|
|
fn neq: A -> B -> Boolean
|
|
λ x y => not (eq x y)
|
|
end fn
|
|
|
|
infix =: A -> B -> Boolean
|
|
λ x y => eq x y
|
|
end infix
|
|
|
|
infix !=: A -> B -> Boolean
|
|
λ x y => neq x y
|
|
end infix
|
|
end class
|
|
|
|
enum Comparison
|
|
object LessThan
|
|
object EqualTo
|
|
object GreaterThan
|
|
end enum
|
|
|
|
given A
|
|
class Compare
|
|
defn compare: A -> A -> Comparison
|
|
end class
|
|
|
|
given A :: Compare
|
|
instance Eq A A
|
|
fn eq: A -> A -> Boolean
|
|
λ x y =>
|
|
match compare x y
|
|
case EqualTo => true
|
|
case _ => false
|
|
end match
|
|
end fn
|
|
end instance
|
|
|
|
given A
|
|
class HashCode
|
|
defn hash_code: A -> Int32
|
|
end class
|
|
```
|