Using `apply` from` MyApplicative ((,) e) `

Given the following, taken from Typeclassopedia :

class MyApplicative f where pure :: a -> fa ap :: f (a -> b) -> fa -> fb instance Monoid e => MyApplicative ((,) e) where pure x = (mempty, x) (u, f) `ap` (v, x) = (u `mappend` v, fx) 

I am trying to do something like:

 ghci> (+) <$> Control.Applicative.pure 100 <*> Control.Applicative.pure 50 150 

but with the new Applicative :

 ghci> Main.pure (+ 100) `ap` (Main.pure 50) <interactive>:132:1: No instance for (MyApplicative f0) arising from a use of `it' The type variable `f0' is ambiguous Note: there is a potential instance available: instance Monoid e => MyApplicative ((,) e) -- Defined at MonoidWork.hs:8:10 In the first argument of `print', namely `it' In a stmt of an interactive GHCi command: print it 

Looking for types:

 ghci> :t Main.pure (+ 100) Main.pure (+ 100) :: (MyApplicative f, Num a) => f (a -> a) 

and

 ghci> :t (Main.pure 50) (Main.pure 50) :: (MyApplicative f, Num a) => fa 

I do not understand how to fix a compile-time error.

+5
source share
1 answer

You just need to give it a type signature. GHC / GHCi has special rules for defaulting certain types to help, especially when you are in GHCi. Otherwise, you will have to do things like 1 + 2 :: Int , and not just 1 + 2 . If you give it a type signature, it will be very good:

 > Main.pure (+ 100) `ap` Main.pure 50 :: ((), Int) ((), 150) 

GHC simply does not have default rules for type classes that you define yourself (and to allow this behavior would be difficult to get right, otherwise you would have very unexpected results in your programs).

+5
source

All Articles