3

我有这个行为符合预期的haskell代码:

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

然而,当转换为 Fay 时,它不会进行类型检查:

{-# 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

编译错误是:

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"

我不完全关注这里的问题。实际上,我什至尝试在 GHC 代码中删除 Control.Monad 的导入,并将 liftM2 粘贴到 Fay 代码中,但它仍然可以正确地进行类型检查......在 Fay 中使用诸如 liftMx 等函数的任何选项,或者我错过了完全在这里?

这是 Fay 0.16.0.3... 也许我应该尝试升级到 0.17?

4

1 回答 1

3

我怀疑doFay 中的符号仅适用于Faymonad,因为 AFAIK Fay 不支持类型类。看着Fay Prelude,我看到了,(>>=)并且return是单态的,专门用于Fay单子。

于 2013-09-07T07:44:57.037 回答