[Haskell-cafe] Specialize a function on types of arguments?
Anthony Clayden
anthony_clayden at clear.net.nz
Sun Nov 18 04:40:51 UTC 2018
Hi 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?
> 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
> "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.
> 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.
> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20181118/d274cd13/attachment.html>
More information about the Haskell-Cafe
mailing list