Skip to content

Instantly share code, notes, and snippets.

@khibino
Created January 11, 2018 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khibino/75458292ad4efe8c717498bed97c810c to your computer and use it in GitHub Desktop.
Save khibino/75458292ad4efe8c717498bed97c810c to your computer and use it in GitHub Desktop.
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