[Haskell-cafe] Simplifying a IsFunction type class using type
equality constraints
Manuel M T Chakravarty
chak at cse.unsw.edu.au
Thu Oct 9 00:20:17 EDT 2008
Corey O'Connor:
> 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
Yes, that's a fine way of simplifying the definition. In fact, you
should also be able to drop the functional dependency in the
IsFunction class.
Manuel
More information about the Haskell-Cafe
mailing list