[Haskell-cafe] Closed classes
Imants Cekusins
imantc at gmail.com
Thu Feb 18 09:22:59 UTC 2016
David, thank you very much for the examples. They are very clear.
Would it be difficult to post the first one (working in current setup)
on Wiki?
either https://wiki.haskell.org/GHC/Type_families
or https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/type-families.html
?
expanding your and Clinton's suggestions, here is one more version.
Not sure if this may work however this may be intuitive to C#, Java
users:
-- Names for the instances
data InstanceChoice = ChooseFirst | ChooseSecond | IgnoreBoth
-- Determine which instance should be used
type family Choose a b where
Choose Int y = 'ChooseFirst
Choose x Int = 'ChooseSecond
Choose x y = 'IgnoreBoth
class Closed a b where
fun :: a -> b -> Int
instance (Choose Int y) => Closed Int y where fun x _ = x
instance (Choose x Int) => Closed x Int where fun _ y = y
instance (Choose x y) => Closed x y where fun _ _ = 0
the differences:
no need to use new keyword "closed"
no need for new language pragma
non-type family instances may still be defined
instances may be defined in other modules
(if possible:) multiple type families may be used with the same class.
Instance constraint would hint, which (if any) type family to apply in
selecting an instance
how does this sound?
More information about the Haskell-Cafe
mailing list