1

Functions aren't instances of the Show typeclass, so one cannot see it in neat form. But compiler has it somewhere on which it returns fails.

So why it is not possible to show it and if it can be what it will look like? I shall be thankful if you could take some trivial example to state this.

Pankaj Sejwal
  • 1,605
  • 1
  • 18
  • 27

2 Answers2

5

Haskell is a compiled language. Functions are, internally, just code. It doesn't really make sense to Show them. Even if it was possible, it'd probably not be desirable, because it would most likely mean that extensionally equal functions have different String representations, and/or that compiler optimisations would be visible in the String that is being generated.

The only things you can do are: define a dummy representation for all functions, or define a specific instance for specific function types with small domains (say, functions of type Bool -> Bool or Bool -> Int) that can still easily be observed completely from the outside.

kosmikus
  • 19,549
  • 3
  • 51
  • 66
  • I meant to say that it every step will generate some processed code, so the user if gets access to this, can learn about how compiler handles code. – Pankaj Sejwal Nov 27 '13 at 18:38
  • @Blackbird If you want to look at the internal core representation, you can do so via the compiler flag `-ddump-simpl`. – kosmikus Nov 27 '13 at 18:47
  • The size of the codomain doesn't really matter for a `Show` instance (the _information content_ of function values being C^D i.e. polynomial in _C_, the required space scales linearly). — GRRR, why am I not even allowed to typeset scripts in StackOverflow comments anymore? – leftaroundabout Nov 27 '13 at 19:23
  • @leftaroundabout Yes, you're right. I typed too quickly. Thanks, fixed. – kosmikus Nov 27 '13 at 20:28
2

There is no way to inspect functions to obtain their code and any captured environment (in the case of a thunk).

In order to show a function one would typically try to create an instance of Show:

instance Show (a -> b) where
    show f = 

What value goes on the right hand side of the =? As I said, you can't inspect functions so the common solution is to use:

    show f = "<function>"

Which is what is implemented in Text.Show.Functions

Thomas M. DuBuisson
  • 64,245
  • 7
  • 109
  • 166