Possible to automatically determine typeclass membership?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Possible to automatically determine typeclass membership?

Ryan Ingram
Is it possible in Haskell + GHC extensions to use reflection
techniques to determine typeclass membership?  I'm thinking of things
like the following:

Idea 1:

> data MaybeEq a = NoEq a | Eq a => HasEq a
> checkEq :: (some typeclass constraint) => a -> MaybeEq a

(such that checkEq x returns HasEq x if and only if x has an Eq instance.)

Idea 2:

> data HTrue
> data HFalse

> class MaybeEq a b | a -> b

> instance Eq a => MaybeEq a HTrue
> instance (otherwise) => MaybeEq a HFalse

  -- ryan
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Possible to automatically determine typeclass membership?

Jeff P-2
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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Possible to automatically determine typeclass membership?

Bugzilla from alfonso.acosta@gmail.com
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 <[hidden email]>:

> 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
>  [hidden email]
>  http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe