[Haskell-cafe] Partially applied type synonyms in instance heads
Nicolas Frisby
nicolas.frisby at gmail.com
Sun Oct 29 01:25:04 EST 2006
Haskell's type system does not directly allow eta expansion, i.e.
> instance Map (\x y -> [(x, y)]) a b where
> ...
is not legal.
The straight-forward way to tackle this is with a newtype declaration, i.e.
> newtype Assoc a b = Assoc [(a, b)]
>
> instance Map Assoc a b where
> ...
works as expected. There's an avenue for you to continue with if you like.
In regards to what GHC was trying to tell you with its error message:
type synonyms (like your Assoc) can never be partially applied. In
your instance, Assoc is not applied to a and b (don't let the adjaceny
fool you), it's just sitting by itself... the most lonely version of
partial application. Hence it croaks.
Hope that helps,
Nick
ps - I've never pondered why it is that type synonyms can't be
partially applied. I'm sure someone will pipe up with the answer.
On 10/28/06, Mathieu Boespflug <mboes at tweag.net> wrote:
> Hi everyone,
>
> I'm running into trouble with type synonyms in instance heads and I
> can't figure out what the resulting error message means. The case I'm
> considering is as follows:
>
> -- hoist a and b to class variables so that instances declarations may
> -- constrain them.
> class Map m a b where
> toAssoc :: m a b -> [(a, b)]
> fromAssoc :: [(a, b)] -> m a b
>
> type Assoc a b = [(a, b)]
>
> instance Map Assoc a b where
> toAssoc = id
> fromAssoc = id
>
> The class Map is used to allow translation from one map type to
> another (FiniteMap, arrays, etc...) by means of expressing the map as
> an association list. Useful for defining isomorphisms and so on. Now
> I'd like to define an association list as itself a trivial instance of
> Map class, but I cannot do so without wrapping the type of an
> association list, [(a, b)], behind a type synonym, as I'm not aware of
> any way of writing a type constructor of kind (* -> * -> *)
> constructing the type [(a, b)].
> But when compiling the above code with GHC I get the following error:
>
> Map.hs:9:0:
> Type synonym `Assoc' should have 2 arguments, but has been given 0
> In the instance declaration for `Map Assoc a b'
>
> Any idea what this means? Also, is there any other way of declaring an
> instance for [(a, b)] without using type synonyms?
>
> Many thanks,
>
> Mathieu
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
More information about the Haskell-Cafe
mailing list