[Haskell-cafe] Class constraints with "free" type variables and fundeps

Alexander Solla alex.solla at gmail.com
Sat Sep 29 03:33:23 CEST 2012

On Fri, Sep 28, 2012 at 5:36 PM, Francesco Mazzoli <f at mazzo.li> wrote:

> At Fri, 28 Sep 2012 17:19:36 -0700,
> Alexander Solla wrote:
> > Well, then what exactly is the problem?  Are you getting an error?
> ...well yes.  The error I get with the posted class declarations is
>    Not in scope: type variable `b'
> at the line with
>    class Foo a b => Bar a where
> Which I get because all the type vars in the LHS must be referenced on the
> (or so it seems).

Yes, indeed.

> Now, in my case the problem does not stop there, because I also want to
> reference the tyvar on the LHS in a type signature of a method, since in
> that
> case there is a fundep on b (`a -> b'), which makes `b' decidable if you
> have
> `a' and `Foo a b'.

Only with respect to type inference.

> > You don't need to bring 'b' into scope.  You will already have real
> types in
> > scope.
> >
> > instance Foo A B
> > instance Bar A where foo A B = C
> Again, I'm not sure what you mean here.

I wouldn't have replied with that line of thought if you had just told us
what the problem was in the first place.  I /was/ saying that you can use
explicit type annotations to disambiguate instances.

Most of us haven't memorized the Haskell 2010 report.  We let the compiler
tell us what's wrong and either learn why, or how to fix it.  So post your

By the way, it is rather rude to publicly post a private email...

Now, on to your real problem.

Use TypeFamilies instead:

class Foo a where
         type BarThing a :: *

class Foo a => Bar a where
         foo :: a -> BarThing a -> b

This is pretty pointless, since you can just refactor into the nearly

class Foo a

class Foo a => Bar a where
        type BarThing a :: *
        foo :: a -> BarThing a -> c

It may or may not matter to which family the type synonym belongs.

What is the problem you are actually trying to solve?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120928/1dcf668f/attachment.htm>

More information about the Haskell-Cafe mailing list