I have this haskell code which behaves as expected:
import Control.Monad
getVal1 :: Maybe String
getVal1 = Just "hello"
getVal2 :: Maybe String
getVal2 = Just "World"
main = process >>= putStrLn
process :: IO String
process = case liftM2 operation getVal1 getVal2 of
Nothing -> error "can't run operation, one of the params is Nothing"
Just result -> result
operation :: String -> String -> IO String
operation a b = return $ a ++ b
However when transposed to Fay, it doesn't typecheck:
{-# LANGUAGE NoImplicitPrelude, EmptyDataDecls #-}
import Prelude
import FFI
liftM2 f m1 m2 = do { x1 <- m1; x2 <- m2; return (f x1 x2) }
getVal1 :: Maybe String
getVal1 = Just "hello"
getVal2 :: Maybe String
getVal2 = Just "World"
main = process >>= putStrLn
process :: Fay String
process = case liftM2 operation getVal1 getVal2 of
Nothing -> error "can't run operation, one of the params is Nothing"
Just result -> result
operation :: String -> String -> Fay String
operation a b = return $ a ++ b
The compile error is:
fay: ghc:
TestFay.hs:17:33:
Couldn't match expected type `Fay String'
with actual type `Maybe String'
In the second argument of `liftM2', namely `getVal1'
In the expression: liftM2 operation getVal1 getVal2
In the expression:
case liftM2 operation getVal1 getVal2 of {
Nothing
-> error "can't run operation, one of the params is Nothing"
I'm not exactly following the problem here. Actually I even tried to remove the import for Control.Monad in the GHC code and paste the liftM2 as in the Fay code, but it still typechecks properly... Any option of using such functions such as liftMx in Fay, or am I missing something completely here?
This is Fay 0.16.0.3... Maybe I should try upgrading to 0.17?
I suspect that do
notation in Fay works for the Fay
monad only, because AFAIK Fay does not support type classes. Looking at the Fay Prelude, I see that (>>=)
and return
are monomorphic, specialized to the Fay
monad.