[Haskell-beginners] How to avoid repeating a type restriction from a data constructor
Daniel Fischer
daniel.is.fischer at googlemail.com
Wed Apr 24 17:10:45 CEST 2013
On Wednesday 24 April 2013, 14:19:35, gs wrote:
> So as far as removing redundant code is
> concerned, this can't really be done?
That depends. Your example is not well-suited for that. In other situations,
you can get rid of contexts well.
> The instance declarations still need the redundant context,
Well, the context is not redundant, that's the crux.
You can specify the type `Source v a` even when there is no `Variable v`
instance [whether you use DatatypeContexts - which, again, are pretty useless
because they don't do what one would expect - or GADTs]. And such a type is
inhabited by bottom, you just can't create non-bottom values of such a type.
Hence the `Variable v` context gives additional information that is needed for
the instance.
If you had a `v a` as argument in all methods of `class Variable v`, you could
write the
instance Variable (Source v) where ...
without the `Variable v` context by pattern-matching on `Source` to make the
`v` instance available (when using GADTs), then you wouldn't need the
`Variable v` context on the `Bindable` instance either (similarly for
BindingList).
But to have a usable `newVar`, you need the context, and that propagates.
> and function definitions need a redundant pattern-matching
> instead of a redundant context.
The pattern-matching isn't redundant either.
But all in all, basically you have the choice between adding a context or
pattern-matching for stuff like that.
More information about the Beginners
mailing list