Last active
July 23, 2020 13:39
-
-
Save chiroptical/67f4042bcd74a1df601ac96dd1be7886 to your computer and use it in GitHub Desktop.
Implement the Monoid ability in Unison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ability Semigroup m where | |
assoc : m -> m -> m | |
-- Without unique, Monoid and Ask have the same hash | |
unique ability Monoid m where | |
identity : m | |
use Semigroup assoc | |
use Monoid identity | |
runMonoidSum : '{Semigroup Nat, Monoid Nat} b -> b | |
runMonoidSum p = | |
h : Request {Semigroup Nat, Monoid Nat} b -> b | |
h = cases | |
{assoc m n -> k} -> | |
handle k (m + n) with h | |
{identity -> k} -> | |
handle k 0 with h | |
{e} -> | |
e | |
handle !p with h | |
> runMonoidSum 'let assoc 3 identity | |
> runMonoidSum 'let assoc identity 3 | |
foldMap : (a ->{Semigroup m, Monoid m} m) -> List a ->{Semigroup m, Monoid m} m | |
foldMap f xs = | |
List.foldr (a acc -> assoc (f a) acc) identity xs | |
> runMonoidSum 'let foldMap id [0, 1, 2] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment