I am playing with Monads in Haskell trying to get good at it. This piece of coded I tried to finish by writing the right sides of the definitions in the instantiation of the Trace Monad.
The function and the newtype I didn't provide, they were there as part of the exercise.
As you can see I get an error. I am not sure I understand the error.
I interpret the error as: The function show isn't declared for the type (Trace Integer) and it therefore can't use print on the provided input 2.
If this is a correct interpretation where did I then make the mistake, in the function call, or in the definition of the monad? And how to correct it?
newtype Trace a = T (a, String) --My comment: here we apparently don't have a constructor ?
instance Monad Trace where
(T p) >>= f = f (fst p)
return x = T(x, mempty)
traceable :: String -> (t -> a) -> t -> Trace a
traceable name f = \x -> T(f x, name ++" called.")
OUTPUT:
ghci> traceable "customStr" (*2) 2
<interactive>:2:1: error:
* No instance for (Show (Trace Integer))
arising from a use of `print'
* In a stmt of an interactive GHCi command: print it