[Haskell-cafe] Possible to automatically determine typeclass
membership?
Alfonso Acosta
alfonso.acosta at gmail.com
Mon Mar 31 06:23:05 EDT 2008
Hopefully, accessing the instance environment from Tempalte Haskell
will be possible in next GHC's release:
http://hackage.haskell.org/trac/ghc/ticket/1835
2008/3/31 jeff p <mutjida at gmail.com>:
> Hello,
>
>
> > Is it possible in Haskell + GHC extensions to use reflection
> > techniques to determine typeclass membership? I'm thinking of things
> > like the following:
> >
> I think the short answer is not in general; i.e. I don't think there is any
> way to access the members of an arbitrary typeclass (but I'd love to be
> proved wrong).
>
> However, you could always explicitly list the members of a typeclass you are
> interested in (this is similar to your Idea 2):
>
> {-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances
> -fallow-overlapping-instances #-}
>
> class InEq a b | a -> b where inEq :: a -> Bool
> instance TypeCast b HFalse => InEq a b where inEq _ = False
> instance InEq Int HTrue where inEq _ = True
> instance InEq a b => InEq [a] b where inEq _ = inEq (undefined :: a)
>
> data HTrue
> data HFalse
>
> class TypeCast a b | a -> b, b-> a where typeCast :: a -> b
> class TypeCast' t a b | t a -> b, t b -> a where typeCast' :: t -> a -> b
> class TypeCast'' t a b | t a -> b, t b -> a where typeCast'' :: t -> a -> b
> instance TypeCast' () a b => TypeCast a b where typeCast x = typeCast' ()
> x
> instance TypeCast'' t a b => TypeCast' t a b where typeCast' = typeCast''
> instance TypeCast'' () a a where typeCast'' _ x = x
>
> You can also do an arguably nicer, more flexible version of the previous by
> recreating some of the typeclass machinery yourself; Oleg has several
> examples of this such as: //okmij.org/ftp/Haskell/poly2.txt
>
> -Jeff
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
More information about the Haskell-Cafe
mailing list