Discussion:
[Haskell-cafe] Specialize a function on types of arguments?
ducis
2018-11-18 04:01:25 UTC
Permalink
Hi, everyone,

Is it possible to make combine the following "f" and "g" into one function?
f:: a -> b -> b
f x y = y
g:: a -> a -> a
g x y = x

Or similarly, "eq1" and "eq2" into one function?
eq1 :: (Eq a)=>a->a->Bool
eq1 = (==)
eq2 :: (Eq a,Eq b)=>a->b->Bool
eq2 _ _ = False

Looks like it would require some typeclasses, but at least in the first case, "a" and "b" should be any types.

Best!
Anthony Clayden
2018-11-18 04:40:51 UTC
Permalink
Hi Ducis,
Post by ducis
Is it possible to make combine the following "f" and "g" into one
function?

"combine" is vague. You perhaps mean: look at the types of the arguments,
and choose one function or the other?
Post by ducis
Looks like it would require some typeclasses,
I'll answer the question as put (yes it needs typeclasses), but I can't
help feel there's a backstory, and you might well be doing something that
could be done better, if I knew what you're trying to achieve. Let's take
the second one first
Post by ducis
"eq1" and "eq2" into one function?
eq1 :: (Eq a)=>a->a->Bool
eq1 = (==)
eq2 :: (Eq a,Eq b)=>a->b->Bool
eq2 _ _ = False
class Eqbytype a b where
eqt :: a -> b -> Bool

instance {-# OVERLAPPING #-} (Eq a) => Eqbytype a a where
eqt = (==)

instance {-# OVERLAPPABLE #-} Eqbytype a b where
eqt _ _ = False

Look at the Users Guide for what the OVERLAPPING/OVERLAPPABLE pragmas are doing.

Note for the first instance I repeated type var `a` in the head,
meaning: pick this instance if the two arguments to the method are of
the same type.

Note for the second instance, I didn't bother with the `Eq`
constraint, since we can't compare values of distinct types.
Post by ducis
f:: a -> b -> b
f x y = y
g:: a -> a -> a
g x y = x
So you want same argument types to drive which argument to pick? Or
you want the return type to drive which argument? That's possible:
look at the definition of class `Read` in the Prelude. Again we can
pick instances depending on a repeated type. But your requirements are
not clear.
Post by ducis
but at least in the first case [which I've put second], "a" and "b" should be any types.
No they can't: as you state it, you require either all three the same,
or the second to be the same as the return type.

Come back and ask a more focussed question once you've worked through the
above. (And explain why you're asking.) The above code is untested, BTW.

AntC

Continue reading on narkive:
Loading...