Created
January 11, 2018 15:09
-
-
Save khibino/75458292ad4efe8c717498bed97c810c to your computer and use it in GitHub Desktop.
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
module ExplicitDict where | |
-- explicit type class definition | |
data Mon m = | |
Mon | |
{ mon_op :: m -> m -> m | |
, mon_unit :: m | |
} | |
-- monoid instance of (*, 1) | |
intMulMon :: Mon Int | |
intMulMon = | |
Mon | |
{ mon_op = (*) | |
, mon_unit = 1 | |
} | |
-- monoid instance of (+, 0) | |
intAddMon :: Mon Int | |
intAddMon = | |
Mon | |
{ mon_op = (+) | |
, mon_unit = 0 | |
} | |
--- | |
-- explicit type class definition | |
data Collects e ce = | |
Collects | |
{ empty :: ce | |
, insert :: e -> ce -> ce | |
, member :: e -> ce -> Bool | |
} | |
data Eq_ a = | |
Eq { runEq :: a -> a -> Bool } | |
listCol :: Eq_ e -- ^ explicitly pass Eq instance | |
-> Collects e [e] -- ^ result Collects instance | |
listCol eq = | |
Collects | |
{ empty = [] | |
, insert = (:) | |
, member = \x -> any (runEq eq x) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment