0

I try to compose two function with type specifying.

foo :: Num a => a -> a
foo a = a + 2

bar :: Num a => a -> a
bar a = a * 2

fooBarCompose :: (Num a, Num b, Num c) => (a -> b) -> (c -> a) -> c -> b
fooBarCompose f g = f . g

My module compiles, but in runtime when I invoke

fooBarCompose bar foo

I get an error:

No instance for (Show (b0 -> b0))
  (maybe you haven't applied enough arguments to a function?)
  arising from a use of ‘print’
In the first argument of ‘print’, namely ‘it’
In a stmt of an interactive GHCi command: print it

Any ideas why I get this?

1 Answer 1

11

Any ideas why I get this?

You don't. Everything you've written works just fine. You can use fooBarCompose bar foo in any program you want.

Only, if you try to evaluate it in GHCi, it has a problem: fooBarCompose bar foo is a function. How the heck is it supposed to show a function? Display an exhaustive list of all possible inputs and corresponding results? Clearly not feasible. GHCi uses print under the hood, which simply invokes show. And, well, because it's not possible to show a function, it gives you an error message saying exactly this.

OTOH, the result of applying a function to any single value can easily be shown, e.g.

> fooBarCompose bar foo 2  -- aka `bar . foo $ 2`
8
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.