Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lambdabot suggests join, but it doesn't work

Here is a higher-order function that applies an argument to a given function twice:

dapp :: (a -> a -> a) -> a -> a
dapp = \f x -> f x x

ghci> dapp (*) 5
25

Can we make that shorter? Let's ask lambdabot:

lambdabot> @pl \f x -> f x x
join

Hooray! Let's try it out:

import Control.Monad (join)

dapp :: (a -> a -> a) -> a -> a
dapp = join

But it doesn't work :(

No instance for (Monad ((->) a))
  arising from a use of `join'
Possible fix: add an instance declaration for (Monad ((->) a))
In the expression: join
In an equation for `dapp': dapp = join

Why does this happen? Am I importing the wrong join? I couldn't find another join on Hoogle.

like image 717
fredoverflow Avatar asked Dec 26 '22 05:12

fredoverflow


1 Answers

Until recently, the Functor and Monad instances for (->) r were orphan instances in Control.Monad.Instances.

However, starting from base-4.6.0.0 (GHC 7.6.1), these instances have been moved to the Prelude and the (now empty) Control.Monad.Instances has been deprecated.

So to use these instances, either import Control.Monad.Instances or upgrade your GHC.

like image 63
hammar Avatar answered Jan 08 '23 11:01

hammar