[Haskell-cafe] Type signature for function where function parameter is used twice?

Ivan Lazar Miljenovic ivan.miljenovic at gmail.com
Sun Jul 19 00:23:27 UTC 2015

On 19 July 2015 at 10:13, Clinton Mead <clintonmead at gmail.com> wrote:
> Lets say I've got the following data type:
> data D c a b = D (c a b) (c b a)
> And I define a function to manipulate it:
> apply :: (c a b -> c a2 b2) -> (c b a -> c b2 a2) -> D c a b -> D c a2 b2
> apply f1 f2 (D x y) = D (f1 x) (f2 y)
> This is all fine. But I want a shorter function if (f1 = f2). So I write:
> applyBoth f = apply f f
> I originally thought that if "apply f f" is valid, then logically "applyBoth
> f" should also be valid. But it seems that type inference results in
> applyBoth only working for functions "c a a -> c a2 a2".

applyBoth f = apply f f

In apply, (f1 :: c a b -> c a2 b2) and (f2 :: c b a -> c b2 a2).

So for "apply f f" to typecheck, we must have that a ~ b and a2 ~ b2
(as the above two type signatures must match since f1 = f2).

So we must have that (f :: c a a -> c a2 a2).

> Is there a way to type "applyBoth" so it works for all functions that would
> work simply by repeating them twice in "apply"?

I'm not sure what you mean; what else would make sense?  Can you
provide an example?

Ivan Lazar Miljenovic
Ivan.Miljenovic at gmail.com

More information about the Haskell-Cafe mailing list