[Haskell-cafe] Re: distinguish functions from non-functions in a class/instances

apfelmus apfelmus at quantentunnel.de
Fri Dec 7 05:32:56 EST 2007

Philipp N. wrote:
> i'm trying to wrap functions (a -> b -> ... -> z) of any arity to functions
> of type ([String] -> y), where list of strings replaces the typed arguments.
> one attempt looks like this (here written with type families, you can
> replace it by functional dependencies or what ever):
> type family Res x
> type instance Res x = x
> type instance Res (x->y) = Res y
> class Nary x where
>     nary :: x -> [String] -> Res x
> instance Nary x where
>     nary x [] = x
> instance Nary (x->y) where
>     nary f (x:xs) = nary (f $ read x) xs
> i hope you can get the idea.
> the problem is, that you cannot distinguish type (x->y) from z, so these
> instances are overlapping.

Exactly. What you want to do is most likely impossible, which I think is 
due to polymorphism. What type should the expressions

   nary id
   nary ($)


Nary only works when the result is a monotype, which means that you 
would have to add every concrete monotype as base case

   instance Nary Int  where nary x [] = x
   instance Nary Char where nary x [] = x
   instance Nary a => Nary [a] where nary x [] = x


More information about the Haskell-Cafe mailing list