Here you can think about it. Consider:
answer = 42 magic = 3 specialName :: Int -> String specialName answer = "the answer to the ultimate question" specialName magic = "the magic number" specialName x = "just plain ol' " ++ show x
Can you understand why this will not work? answer in pattern matching is a variable other than answer in the outer scope. So instead you have to write it like this:
answer = 42 magic = 3 specialName :: Int -> String specialName x | x == answer = "the answer to the ultimate question" specialName x | x == magic = "the magic number" specialName x = "just plain ol' " ++ show x
Actually, this is exactly what happens when you write constants in a template. I.e:
digitName :: Bool -> String digitName 0 = "zero" digitName 1 = "one" digitName _ = "math is hard"
converted by the compiler into something equivalent:
digitName :: Bool -> String digitName x | x == 0 = "zero" digitName x | x == 1 = "one" digitName _ = "math is hard"
Since you want to map a function bound to (+) , and not just bind something to a (+) symbol, you need to write your code as:
instance Show (t -> t-> t) where show f | f == (+) = "plus" show f | f == (-) = "minus"
But this will require functions to be comparable for equality. And this is generally an insoluble problem.
You might argue that you simply ask the runtime system to compare function pointers, but at the language level, the Haskell programmer does not have access to pointers. In other words, you cannot manipulate references to values ββin Haskell (*), but the values ββthemselves. This is Haskell clean and gets referential transparency.
(*) MVar , and other such objects in the IO monad are another matter, but their existence does not invalidate the point.
Mtnviewmark
source share