Difference between revisions of "Show instance for functions"
(give the lambda expression the fixed type Int > Int in order to avoid effects of strange Num instance) 
Hairy Dude (talk  contribs) (→Theoretical answer: this "mathematical" means "settheoretic"; other notions of function (e.g. MartinLöf type theory) aren't defined by their graph type theory) 

(3 intermediate revisions by 3 users not shown)  
Line 6:  Line 6:  
Why is there a Show instance, but it only prints the type? 
Why is there a Show instance, but it only prints the type? 

−  +  Prelude> :m + Text.Show.Functions 

Prelude Text.Show.Functions> show Char.ord 
Prelude Text.Show.Functions> show Char.ord 

"<function>" 
"<function>" 

Line 20:  Line 20:  
The Haskell compiler doesn't maintain the expressions as they are, but translates them to machine code or some other lowlevel representation. 
The Haskell compiler doesn't maintain the expressions as they are, but translates them to machine code or some other lowlevel representation. 

−  The function <hask>\x > x+x :: Int > Int</hask> might have been optimized to <hask>\x > 
+  The function <hask>\x > x  x + x :: Int > Int</hask> might have been optimized to <hask>\x > x :: Int > Int</hask>. If it's used anywhere, it might have been inlined and optimized to nothing. 
The variable name <hask>x</hask> is not stored anywhere. 
The variable name <hask>x</hask> is not stored anywhere. 

You might have thought, that Haskell is like a scripting language, maintaining expressions at runtime. 
You might have thought, that Haskell is like a scripting language, maintaining expressions at runtime. 

Line 35:  Line 35:  
Functional programming is about functions. 
Functional programming is about functions. 

−  A mathematical function is entirely defined by its graph, that is by pairs of objects (argument, value). 
+  A mathematical (precisely, settheoretic) function is entirely defined by its graph, that is by pairs of objects (argument, value). 
E.g. 
E.g. 

*<math> \sqrt{\ } = \{(0,0), (1,1), (4,2), (9,3), \dots \} </math> 
*<math> \sqrt{\ } = \{(0,0), (1,1), (4,2), (9,3), \dots \} </math> 

Line 54:  Line 54:  
</haskell> 
</haskell> 

−  One could do this for enumerable argument types, but it is not in the standard libraries. 

+  Code to do this is available in the [http://hackage.haskell.org/package/universereverseinstances universereverseinstances] package (which is also installed when installing the toplevel [http://hackage.haskell.org/package/universe universe] package). 

== Source == 
== Source == 
Latest revision as of 00:30, 1 February 2016
Question
Why is there no Show
instance for functions for showable argument and value types?
Why can't I enter \x > x+x
into GHCi or Hugs and get the same expression as answer?
Why is there a Show instance, but it only prints the type?
Prelude> :m + Text.Show.Functions Prelude Text.Show.Functions> show Char.ord "<function>"
How can lambdabot display this:
dons > ord lambdabot> <Char > Int>
Answer
Practical answer
The Haskell compiler doesn't maintain the expressions as they are, but translates them to machine code or some other lowlevel representation.
The function \x > x  x + x :: Int > Int
might have been optimized to \x > x :: Int > Int
. If it's used anywhere, it might have been inlined and optimized to nothing.
The variable name x
is not stored anywhere.
You might have thought, that Haskell is like a scripting language, maintaining expressions at runtime.
This is not the case.
Lambda expressions are just anonymous functions.
You will not find a possibility to request the name of a variable at runtime,
or inspect the structure of a function definition.
You can also not receive an expression from the program user, which invokes variables of your program, and evaluate it accordingly.
That is, Haskell is not reflexive.
Everything can be compiled.
A slight exception is hsplugins.
Theoretical answer
Functional programming is about functions. A mathematical (precisely, settheoretic) function is entirely defined by its graph, that is by pairs of objects (argument, value). E.g.
Since the graphs of and are equal
these both expressions denote the same function.
Now imagine both terms would be echoed by Hugs or GHCi as they are.
This would mean that equal functions lead to different output.
The interactive Haskell environments use the regular show
function,
and thus it would mean .
This would break referential transparency.
It follows that the only sensible way to show functions is to show their graph.
Prelude> \x > x+x
functionFromGraph [(0,0), (1,2), (2,4), (3,6),
Interrupted.
Code to do this is available in the universereverseinstances package (which is also installed when installing the toplevel universe package).
Source
http://www.haskell.org/pipermail/haskellcafe/2006April/015161.html