[Haskell-cafe] Using fundeps to resolve polymorphic types to
concrete types
Bryan Donlan
bd at fushizen.net
Mon Jul 28 21:32:58 EDT 2008
Hi,
Is there any theoretical reason that functional dependencies can't be used
to resolve a polymorphic type to a concrete type? For example:
> -- compile with -fglasgow-exts
>
> class DeriveType a b | a -> b
>
> data A = A
> data B = B
>
> instance DeriveType A B
>
> simpleNarrow :: DeriveType A b => b -> B
> simpleNarrow = id
Since 'b' is uniquely determined by the fundep in DeriveType, it seems that
this ought to work; ie, since the only type equation satisfying DeriveType A b
is B -> B, it should reduce to that before trying to fit its type against its
body.
The motivation is this case:
> data ComplexType a where
> SomeConstructor :: DeriveType a b => a -> b -> ComplexType a
>
> specialCaseFunc :: ComplexType A -> B
> specialCaseFunc (SomeConstructor _ b) = b
Essentially, if I have a data structure with two types used as fields, and
one uniquely determines the other, I'd like to use these instances to avoid
re-stating the implied one in the type equations, if possible.
Is there some theoretical reason for this not to work, or is it just a
limitation of GHC's current implementation? (Note, I'm testing with GHC 6.8.2,
so it's possible this might be fixed in trunk already...)
Thanks,
Bryan Donlan
More information about the Haskell-Cafe
mailing list