[Haskell-cafe] Simplifying a IsFunction type class using type
equality constraints
Corey O'Connor
coreyoconnor at gmail.com
Fri Oct 3 21:58:26 EDT 2008
I recently had a need to use the IsFunction typeclass described by Oleg here:
http://okmij.org/ftp/Haskell/isFunction.lhs
and am wondering if the use of the TypeCast class can be correctly
replaced by a type equality constraint.
The IsFunction and TypeCast classes were defined as:
> data HTrue
> data HFalse
>
> class IsFunction a b | a -> b
> instance TypeCast f HTrue => IsFunction (x->y) f
> instance TypeCast f HFalse => IsFunction a f
>
> -- literally lifted from the HList library
> 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
I found the use of TypeCast in the IsFunction could be replaced by a
type family:
> class IsFunction a b | a -> b
> instance (f ~ TTrue) => IsFunction (x->y) f
> instance (f ~ TFalse) => IsFunction a f
Which, to me, is easier to understand and appears to function the
same. The type
equality is a stronger (?) constraint than the TypeCast class, but for
the case of
IsFunction the use of type equality is correct.
Am I correct? Is the second definition of IsFunction actually
equivalent to the original?
Cheers,
-Corey O'Connor
More information about the Haskell-Cafe
mailing list