# Show instance for functions

### From HaskellWiki

DonStewart (Talk | contribs) (→Question) |
(give the lambda expression the fixed type Int -> Int in order to avoid effects of strange Num instance) |
||

Line 20: | Line 20: | ||

The Haskell compiler doesn't maintain the expressions as they are, but translates them to machine code or some other low-level representation. |
The Haskell compiler doesn't maintain the expressions as they are, but translates them to machine code or some other low-level representation. |
||

− | The function <hask>\x -> x+x</hask> might have been optimized to <hask>\x -> 2*x</hask>. |
+ | The function <hask>\x -> x+x :: Int -> Int</hask> might have been optimized to <hask>\x -> 2*x :: Int -> Int</hask>. |

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. |

## Revision as of 21:30, 18 December 2007

## Contents |

## 1 Question

Why is there noWhy is there a Show instance, but it only prints the type?

Text.Show.FunctPrelude> :m + Text.Show.Functions Prelude Text.Show.Functions> show Char.ord "<function>"

How can lambdabot display this:

dons > ord lambdabot> <Char -> Int>

## 2 Answer

### 2.1 Practical answer

The Haskell compiler doesn't maintain the expressions as they are, but translates them to machine code or some other low-level representation.

The functionYou 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 hs-plugins.

### 2.2 Theoretical answer

Functional programming is about functions. A mathematical 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 regularand 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.

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

## 3 Source

http://www.haskell.org/pipermail/haskell-cafe/2006-April/015161.html