[Haskell-cafe] Comparing functions

Brandon Allbery allbery.b at gmail.com
Thu Jul 11 19:50:06 CEST 2013

On Thu, Jul 11, 2013 at 1:33 PM, Vlatko Basic <vlatko.basic at gmail.com>wrote:

>     data CmpFunction a = CF (a -> a -> Bool)
> that contains comparing functions, like ==, <, > ..., and I'm trying to
> declare the Show instance for it like this
>     instance Show (CmpFunction a) where
>       show (CF (==)) = "== "                   -- no good
>       show f = case f of                            -- no good also
>                        CBF (==) -> "=="
>                         _ -> "Other"
> but compiler complains for both with
> This binding for `==' shadows the existing binding
>            imported from `Prelude' at src/Main.hs:6:8-11
>            (and originally defined in `ghc-prim:GHC.Classes')

The problem here isn't quite what you think it is; (==) is not a
constructor, therefore it is a *variable*. It's exactly the same problem as

    a = 5
    -- ...
    foo a = 3 -- this does NOT compare with the previous value of "a"; it's
identical to the next line!
    foo x = x

Just as with the above, the normal way to do it would be to use a guard...
but functions don't have an Eq instance, and *can't* have one. How do you
meaningfully compare them? And for a typeclass function like (==), do you
want (==) instantiated for Int to compare equal to (==) instantiated for
Integer? Does a native-compiled function compare equal to an interpreted
function? Remember referential transparency; the concept of comparing
pointers used in some languages is not applicable to Haskell.

brandon s allbery kf8nh                               sine nomine associates
allbery.b at gmail.com                                  ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20130711/b1720d42/attachment.htm>

More information about the Haskell-Cafe mailing list