Luke Collins
Luke Collins

Reputation: 1463

Is it possible to show a function's name in Haskell (ghci)?

I know it is not possible to show the function's body since Haskell carries out optimisations, but is it possible to somehow show it's name?

I would like something like

Prelude> f
[Function f]

similar to what other REPL's do (Python, javascript, etc.)

What I get with Haskell is

Prelude> f
<interactive>:2:1: error:
    • No instance for (Show (Integer -> Integer)) arising from a use of ‘print’ (maybe you haven't applied a function to enough arguments?)
    • In a stmt of an interactive GHCi command: print it

which is not helpful in demonstrations.

Upvotes: 1

Views: 153

Answers (1)

chi
chi

Reputation: 116174

For demonstration purposes, you can try using the simple-reflect package. It allows some forms of symbolic computation:

> foldr f x [1,2,3]
f 1 (f 2 (f 3 x))
> foldl f x [1,2,3]
f (f (f x 1) 2) 3

Beware, it has its own limitations. For instance (f . g) x won't work unless with suitable type annotations. [1] ++ x won't work at all.

Still, if you prepare the examples for a demonstration, you can check them in advance and ensure they work.

Upvotes: 3

Related Questions