haskell - Functional Dependencies / Type Families -
with functional dependencies can constrain type of dependent parameter in type class using multi-parameter type classes. this:
{-# language functionaldependencies, multiparamtypeclasses,typesynonyminstances #-} class (num a, integral b) => f b | -> b f :: -> b instance f int int f = id instance f float integer f = truncate and everything'll work perfectly.
> f (1 :: int) 1 > f (1.9 :: float) 1 but if try write like
instance f double string f = show i'll following compilation error:
no instance (integral string) arising superclasses of instance declaration possible fix: add instance declaration (integral string) in instance declaration `f double string' is there way approach type families instead of fundeps?
i guess want have this:
{-# language typefamilies, flexiblecontexts #-} class num => g type b g :: (integral (b a)) => -> b instance g int type b int = int g = id instance g float type b float = integer g = truncate this example not directly using type family, associated type synonym, activated same flag. idea easy. instead of explicit giving second parameter typeclass, define type synonym, filled in appropriate type.
this first time use associated type synonyms, seems quite cool feature.
Comments
Post a Comment