Show instance for functions - Revision history
https://wiki.haskell.org/index.php?title=Show_instance_for_functions&action=history
Revision history for this page on the wikienMediaWiki 1.19.14+dfsg-1Thu, 03 Sep 2015 07:40:58 GMTDmwit: /* Theoretical answer */
https://wiki.haskell.org/index.php?title=Show_instance_for_functions&diff=59452&oldid=prev
https://wiki.haskell.org/index.php?title=Show_instance_for_functions&diff=59452&oldid=prev<p><span dir="auto"><span class="autocomment">Theoretical answer</span></span></p>
<table class='diff diff-contentalign-left'>
<tr valign='top'>
<td colspan='1' style="background-color: white; color:black;">← Older revision</td>
<td colspan='1' style="background-color: white; color:black;">Revision as of 18:52, 25 February 2015</td>
</tr></table>Wed, 25 Feb 2015 18:52:22 GMTDmwithttps://wiki.haskell.org/Talk:Show_instance_for_functionsBoris: Statement that 2*x is faster than x+x is doubtful because generally (*) is slower than (+), but (*) by power of 2 may be optimized by shifts... New example is more clear I think.
https://wiki.haskell.org/index.php?title=Show_instance_for_functions&diff=33469&oldid=prev
https://wiki.haskell.org/index.php?title=Show_instance_for_functions&diff=33469&oldid=prev<p>Statement that 2*x is faster than x+x is doubtful because generally (*) is slower than (+), but (*) by power of 2 may be optimized by shifts... New example is more clear I think.</p>
<table class='diff diff-contentalign-left'>
<tr valign='top'>
<td colspan='1' style="background-color: white; color:black;">← Older revision</td>
<td colspan='1' style="background-color: white; color:black;">Revision as of 11:21, 5 February 2010</td>
</tr></table>Fri, 05 Feb 2010 11:21:29 GMTBorishttps://wiki.haskell.org/Talk:Show_instance_for_functionsLemming: give the lambda expression the fixed type Int -> Int in order to avoid effects of strange Num instance
https://wiki.haskell.org/index.php?title=Show_instance_for_functions&diff=17664&oldid=prev
https://wiki.haskell.org/index.php?title=Show_instance_for_functions&diff=17664&oldid=prev<p>give the lambda expression the fixed type Int -> Int in order to avoid effects of strange Num instance</p>
<table class='diff diff-contentalign-left'>
<tr valign='top'>
<td colspan='1' style="background-color: white; color:black;">← Older revision</td>
<td colspan='1' style="background-color: white; color:black;">Revision as of 21:30, 18 December 2007</td>
</tr></table>Tue, 18 Dec 2007 21:30:01 GMTLemminghttps://wiki.haskell.org/Talk:Show_instance_for_functionsDonStewart: /* Question */
https://wiki.haskell.org/index.php?title=Show_instance_for_functions&diff=17661&oldid=prev
https://wiki.haskell.org/index.php?title=Show_instance_for_functions&diff=17661&oldid=prev<p><span dir="auto"><span class="autocomment">Question</span></span></p>
<table class='diff diff-contentalign-left'>
<tr valign='top'>
<td colspan='1' style="background-color: white; color:black;">← Older revision</td>
<td colspan='1' style="background-color: white; color:black;">Revision as of 16:59, 18 December 2007</td>
</tr></table>Tue, 18 Dec 2007 16:59:10 GMTDonStewarthttps://wiki.haskell.org/Talk:Show_instance_for_functionsLemming: explanation
https://wiki.haskell.org/index.php?title=Show_instance_for_functions&diff=17658&oldid=prev
https://wiki.haskell.org/index.php?title=Show_instance_for_functions&diff=17658&oldid=prev<p>explanation</p>
<p><b>New page</b></p><div>== Question ==<br />
<br />
Why is there no <hask>Show</hask> instance for functions for showable argument and value types?<br />
Why can't I enter <hask>\x -> x+x</hask> into GHCi or Hugs and get the same expression as answer?<br />
<br />
== Answer ==<br />
<br />
=== Practical answer ===<br />
<br />
The Haskell compiler doesn't maintain the expressions as they are, but translates them to machine code or some other low-level representation.<br />
The function <hask>\x -> x+x</hask> might have been optimized to <hask>\x -> 2*x</hask>.<br />
The variable name <hask>x</hask> is not stored anywhere.<br />
You might have thought, that Haskell is like a scripting language, maintaining expressions at runtime.<br />
This is not the case.<br />
[[Lambda abstraction|Lambda expression]]s are just [[anonymous function]]s.<br />
You will not find a possibility to request the name of a variable at runtime,<br />
or inspect the structure of a function definition.<br />
You can also not receive an expression from the program user, which invokes variables of your program, and evaluate it accordingly.<br />
That is, Haskell is not [[reflexive language|reflexive]].<br />
Everything can be compiled.<br />
A slight exception is [[hs-plugins]].<br />
<br />
=== Theoretical answer ===<br />
<br />
Functional programming is about functions.<br />
A mathematical function is entirely defined by its graph, that is by pairs of objects (argument, value).<br />
E.g.<br />
*<math> \sqrt{\ } = \{(0,0), (1,1), (4,2), (9,3), \dots \} </math><br />
*<math> (\lambda x.\ x+x) = \{(0,0), (1,2), (2,4), (3,6), \dots \} </math><br />
Since the graphs of <math> \lambda x.\ x+x </math> and <math> \lambda x.\ 2\cdot x </math> are equal<br />
these both expressions denote the same function.<br />
Now imagine both terms would be echoed by Hugs or GHCi as they are.<br />
This would mean that equal functions lead to different output.<br />
The interactive Haskell environments use the regular <hask>show</hask> function,<br />
and thus it would mean <math> \mathrm{show}(\lambda x.\ x+x) \ne \mathrm{show}(\lambda x.\ 2\cdot x) </math>.<br />
This would break [[referential transparency]].<br />
<br />
It follows that the only sensible way to show functions is to show their graph.<br />
<haskell><br />
Prelude> \x -> x+x<br />
functionFromGraph [(0,0), (1,2), (2,4), (3,6),<br />
Interrupted.<br />
</haskell><br />
<br />
One could do this for enumerable argument types, but it is not in the standard libraries.<br />
<br />
== Source ==<br />
<br />
http://www.haskell.org/pipermail/haskell-cafe/2006-April/015161.html<br />
<br />
[[Category:FAQ]]</div>Tue, 18 Dec 2007 14:45:22 GMTLemminghttps://wiki.haskell.org/Talk:Show_instance_for_functions