Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A critical concept that I feel neither Dave nor Peter have stressed enough is this: <em>two functions are equal if and only if (a) they have the same type, and (b) for every possible argument you can give them, they both produce the same result</em>.</p> <p>Now, with this clear, the answer for <code>Eq</code> is just what they said. Peter points out that an <code>Eq</code> instance for functions would need to be able to analyze two arbitrary functions and correctly determine whether they produce the same result for any two inputs. And as Dave points out, this is actually mathematically impossible; any checker that tried to compare arbitrary functions will fail for some functions—meaning it will hang or produce a wrong answer for some cases.</p> <p>You will see Haskell programmers talking about equality of functions all the time, however, but most of the time what they mean is that <em>humans</em> have proven that some two functions are equal. For example, the function composition operator is defined like this:</p> <pre><code>(.) :: (b -&gt; c) -&gt; (a -&gt; b) -&gt; (a -&gt; c) f . g = \x -&gt; f (g x) </code></pre> <p>We can now prove that for all possible <code>h :: c -&gt; d</code>, <code>g :: b -&gt; c</code> and <code>h :: a -&gt; b</code>, <code>f . (g . h) == (f . g) . h</code>. It's pretty easy in this case, we just expand the expressions according to the definition of <code>(.)</code>, and we get the same thing for both:</p> <pre><code>f . (g . h) = f . (\y -&gt; g (h y)) -- expand `g . h` by definition = \x -&gt; f ((\y -&gt; g (h y)) x) -- expand `f . (\y -&gt; g (h y))` = \x -&gt; f (g (h x)) -- apply the inner lambda (f . g) . h = (\y -&gt; f (g y)) . h = \x -&gt; (\y -&gt; f (g y)) (h x) = \x -&gt; f (g (h x)) </code></pre> <p>But this can only be done for carefully chosen functions, and computers generally can't do it well or reliably. For any program you write to try and test the equality of arbitrary functions, there will be some functions for which it will either produce the wrong answer or loop forever.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload