[Haskell-cafe] Type Constraints on Data Constructors

David Menendez dave at zednenem.com
Wed Jun 8 22:25:04 CEST 2011


On Wed, Jun 8, 2011 at 3:15 PM, Malcolm Wallace <malcolm.wallace at me.com> wrote:
>
>> data Bar f a = Foo f => Bar {bar :: f a}
>
> The class context on the data constructor buys you nothing extra in terms of expressivity in the language.  All it does is force you to repeat the context on every function that uses the datatype.  For this reason, the language committee has decided that the feature will be removed in the next revision of Haskell.

You're thinking of a context on the type constructor, i.e.,

data Foo f => Bar f a = Bar { bar :: f a }


The reason the original code does not work is that the constructor
only adds Foo f to the class context during pattern matching. So, for
example, this works:

baz :: Bar f a -> a -> f a   -- n.b., no Foo context
baz (Bar _) = foo

But the code in the original post is trying to create a value of type
Bar f a, so the context is needed.

-- 
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>



More information about the Haskell-Cafe mailing list