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

Francesco Mazzoli f at mazzo.li
Sat Sep 29 10:30:07 CEST 2012


At Fri, 28 Sep 2012 18:33:23 -0700,
Alexander Solla wrote:
> Only with respect to type inference.

I don't understand this comment.

> 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
> errors.

Sorry, I posted in a hurry.  Besides, feeding those lines to a compiler before
replying takes 10 seconds.

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

I thought that you had forgot to reply all, as often happens.  I also want the
discussion to stay on the list, so that people can read in the future.  I'm
sorry if that email was meant to be private, I saw nothing private about it.

> 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

As I mentioned in the original post, `Foo' is outside my control.

> This is pretty pointless, since you can just refactor into the nearly
> equivalent:
> 
> 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.

Mine was a contrived example to show the issue.  The whole point is to reduce
the number of arguments of a class by referring to another class and its
fundeps.

> What is the problem you are actually trying to solve?

What I actually (would like to) have is something like this:

    class (EditDistance algo sym,  ListLike full sym) =>
          Search container full algo | container -> full, container -> algo where
        <methods referencing `sym'>

This limitation is annoying even without referencing `sym' (which I can avoid to
do) since is prevents me from putting constraints that express what the purpose
of the class is and will have to be repeated in every instance.

Then I'd also like to have

    newtype TST sym algo = <...>

    instance (Ord sym, ListLike full sym) => Search (TST sym algo) full algo

This one is a different problem - it requires UndecidableInstances and I don't
understand why.  It seems to me that the coverage condition
(<http://www.haskell.org/ghc/docs/latest/html/users_guide/type-class-extensions.html>,
point 7.6.3.2) is too strict in these cases.  But this is not that important.

--
Francesco * Often in error, never in doubt



More information about the Haskell-Cafe mailing list