2

a是否可以使用模式匹配/防护来编写函数?

{-# LANGUAGE PatternGuards #-}
import Control.Monad.State.Strict(State, gets, runStateT)
data MyState = MyState
    { counter :: Int
    } deriving (Show)


a :: State MyState String
a = do
    i <- gets counter
    case i of
        0 -> return "hello"
        1 -> return "bye"

run = runStateT a ( MyState{counter=0} )

我试着a写成

a' :: State MyState String
a' | i <- gets counter, i == 0 = return "hello"

但得到了错误:

No instance for (Control.Monad.State.Class.MonadState MyState m0)
  arising from a use of ‘gets’
The type variable ‘m0’ is ambiguous
Note: there are several potential instances:
  instance Control.Monad.State.Class.MonadState s m =>
           Control.Monad.State.Class.MonadState
             s (Control.Monad.Trans.Cont.ContT r m)
    -- Defined in ‘Control.Monad.State.Class’
  instance (Control.Monad.Trans.Error.Error e,
            Control.Monad.State.Class.MonadState s m) =>
           Control.Monad.State.Class.MonadState
             s (Control.Monad.Trans.Error.ErrorT e m)
    -- Defined in ‘Control.Monad.State.Class’
  instance Control.Monad.State.Class.MonadState s m =>
           Control.Monad.State.Class.MonadState
             s (Control.Monad.Trans.Except.ExceptT e m)
    -- Defined in ‘Control.Monad.State.Class’
  ...plus 12 others
In a stmt of a pattern guard for
               an equation for ‘a'’:
  i <- gets counter
In an equation for ‘a'’:
    a' | i <- gets counter, i == 0 = return "hello"

No instance for (Eq (m0 Int)) arising from a use of ‘==’
The type variable ‘m0’ is ambiguous
Relevant bindings include
  i :: m0 Int (bound at src/TestGen/Arbitrary/Helpers/Z.hs:18:6)
Note: there are several potential instances:
  instance Eq a => Eq (GHC.Real.Ratio a) -- Defined in ‘GHC.Real’
  instance (Eq e, Data.Functor.Classes.Eq1 m, Eq a) =>
           Eq (Control.Monad.Trans.Error.ErrorT e m a)
    -- Defined in ‘Control.Monad.Trans.Error’
  ...plus 118 others
In the expression: i == 0
In a stmt of a pattern guard for
               an equation for ‘a'’:
  i == 0
In an equation for ‘a'’:
    a' | i <- gets counter, i == 0 = return "hello"
4

4 回答 4

8

这是不可能的。模式保护语法中的左箭头与 do-notation 中的左箭头大多无关。

如果您愿意,可以使用新的 lambda-case 扩展:

{-# LANGUAGE LambdaCase #-}
a :: State MyState String
a = gets counter >>= \case
        0 -> return "hello"
        1 -> return "bye"

或者多路如果,也许?

{-# LANGUAGE MultiWayIf #-}
a :: State MyState String
a = do
    i <- gets counter
    if
      | i == 0 -> return "hello"
      | i == 1 -> return "bye"
于 2014-10-28T16:03:52.267 回答
3

不,这里有一些非常基本的概念不匹配。

模式匹配仅在表达式的最顶部是构造do函数时才有效,但-style 块的头部将是普通函数(在这种情况下>>=是 typeclasss 中定义的函数Monad)。

守卫期望一个类型的值,Bool但你要交给他们的值必须是类型的State MyState Bool(因为关于 monad 的一个独特之处是你无法摆脱它们)。所以守卫也永远不会工作。

但是,您可以使用仿函数实例。函子在 Prelude 中定义;有一个中缀形式的fmapcall <$>in Control.Applicative。你可以这样说:

a' = process <$> gets counter
    where 
        process 0 = "hello"
        process _ = "bye"

或使用该功能做任何您想做的事情process。为了得到更像>>=你的东西,你也可以定义你自己的操作符flip fmap,然后你可以写,比如说,gets counter >= \x -> case x of ...

于 2014-10-28T16:28:03.950 回答
2

为什么不写一个助手?

pureA :: MyState -> String
pureA (MyState 0) = "hello"
pureA (MyState 1) = "bye"
pureA _           = ""

a :: State MyState String
a = fmap doA get

这也遵循将纯逻辑的关注点与不纯逻辑的关注点分开的哲学。

于 2014-10-28T16:04:02.690 回答
1

是的,这可能的,但我建议你不要这样做——很难跟踪哪一块放在哪里。

import Control.Monad.State.Strict(StateT(..))
import Data.Functor.Identity(Identity(..))

data MyState = MyState
    { counter :: Int
    } deriving (Show)

a :: StateT MyState Identity String
a = StateT $ \ s@(MyState i) -> Identity $
  case i of
    0 -> ("hello", s)
    1 -> ("bye", s)
于 2015-02-23T20:51:25.410 回答