Haskell-无法使用类似Monad的定义来定义类似于State Monad的函数

时间:2018-09-06 13:40:46

标签: haskell monads state-monad

我试图通过尝试编写泛型版本的功能来理解Monad的概念,这些功能随后可能包含记录日志,更改状态的副作用。

这是我想出的:(代码有点长,但是它可以显示我是如何理解monad的-这种方法可能不正确)

data Maybe' a = Nothing' | Just' a deriving Show

sqrt' :: (Floating a, Ord a) => a -> Maybe' a
sqrt' x = if x < 0 then Nothing' else Just' (sqrt x)


inv' :: (Floating a, Ord a) => a -> Maybe' a
inv' x = if x == 0 then Nothing' else Just' (1/x)

log' :: (Floating a, Ord a) => a -> Maybe' a
log' x = if x == 0 then Nothing' else Just' (log x)


sqrtInvLog' :: (Floating a, Ord a) => a -> Maybe' a
sqrtInvLog' x = case (sqrt' x) of
                 Nothing' -> Nothing'
                 (Just' y) -> case (inv' y) of
                               Nothing' -> Nothing'
                               (Just' z) -> log' z

-- Now attempt to simplify the nested case:
fMaybe' :: (Maybe' a) -> (a -> Maybe' b) -> Maybe' b
fMaybe' Nothing' _ = Nothing'
fMaybe' (Just' x) f = f x

-- using fMaybe':
sqrtInvLog'' :: (Floating a, Ord a) => a -> Maybe' a
sqrtInvLog'' x = (sqrt' x) `fMaybe'` (inv') `fMaybe'` (log')

-- now we can generalize the concept to any type, instead of just Maybe' by defining a Monad =>
class Monad' m where
 bind' :: m a -> (a -> m b) -> m b
 return' :: a -> m a

instance Monad' Maybe' where
 bind' Nothing' _ = Nothing'
 bind' (Just' x) f = f x
 return' x = Just' x

-- using Monad sqrtInvLog'' can be written as:
sqrtInvLog''' :: (Floating a, Ord a) => a -> Maybe' a
sqrtInvLog''' x = (sqrt' x) `bind'` (inv') `bind'` (log')

-- Further lets attempt to use this for state maintenence and logging, logging:
-- first attempt the specific version:
data ST a = ST (a, Maybe' a) deriving Show

sqrtSt :: (Floating a, Ord a)=> a -> a -> ST a
sqrtSt st x = let r = sqrt' x in case r of
                                  Nothing' -> ST (st, Nothing')
                                  (Just' y) -> ST (st+y, (Just' y))


invSt :: (Floating a, Ord a)=> a -> a -> ST a
invSt st x = let r = inv' x in case r of
                                  Nothing' -> ST (st, Nothing')
                                  (Just' y) -> ST (st+y, (Just' y))


logSt :: (Floating a, Ord a)=> a -> a -> ST a
logSt st x = let r = log' x in case r of
                                  Nothing' -> ST (st, Nothing')
                                  (Just' y) -> ST (st+y, (Just' y))

-- let us first define function which is similar to bind and manipulates the state and invokes the given function:
stBind :: (Floating a, Ord a) => ST a -> (a->a->ST a) -> ST a
stBind (ST (a, Nothing')) _ = ST (a, Nothing')
stBind (ST (s, (Just' y))) f = f s y

sqrtInvLogSt :: (Floating a, Ord a) => a -> a -> ST a
sqrtInvLogSt st x = (sqrtSt st x) `stBind` (invSt) `stBind` (logSt)

-- stBind does not fit the pattern of bind

-- Another version:

sqrtSt' :: (Floating a, Ord a)=> ST a -> ST a
sqrtSt' (ST (st, Nothing')) = ST (st, Nothing')
sqrtSt' (ST (st, (Just' x))) = let r = sqrt' x in case r of
                                                  Nothing' -> ST (st, Nothing')
                                                  (Just' y) -> ST (st+y, (Just' y))



invSt' :: (Floating a, Ord a)=> ST a -> ST a
invSt' (ST (st, Nothing')) = ST (st, Nothing')
invSt' (ST (st, (Just' x))) = let r = inv' x in case r of
                                                  Nothing' -> ST (st, Nothing')
                                                  (Just' y) -> ST (st+y, (Just' y))


logSt' :: (Floating a, Ord a)=> ST a -> ST a
logSt' (ST (st, Nothing')) = ST (st, Nothing')
logSt' (ST (st, (Just' x))) = let r = log' x in case r of
                                                  Nothing' -> ST (st, Nothing')
                                                  (Just' y) -> ST (st+y, (Just' y))

-- define stBind' here
stBind' :: (Floating a, Ord a) => ST a -> (ST a->ST a) -> ST a
stBind' (ST (a, Nothing')) _ = ST (a, Nothing')
stBind' stx f = f stx

sqrtInvLogSt' :: (Floating a, Ord a) => ST a->ST a
sqrtInvLogSt' stx = (sqrtSt' stx) `stBind'` (invSt') `stBind'` (logSt')

-- Even this does not fit the pattern of bind,

最后定义的功能stBind'不适合bind'模式。 在这种情况下如何提出实现以匹配绑定运算符签名?

1 个答案:

答案 0 :(得分:2)

冒着破坏的危险,在protected $fillable = [ 'addNo', 'addStreet', 'addCity', 'intentToJoin', 'user_id', ...]; 单子的定义达到顶峰之前可能会很有趣,因为它在所有事物都变成变压器之前就已经存在:

State

也就是说,状态为newtype State s a = State { runState :: s -> (a, s) } 且产生类型为s的值的有状态动作是从旧状态a到值和新状态{{1 }}。

s的正确定义开始,应该使开发的其余部分更容易迷惑。