单子内的值,嵌套在数据结构中?
-
18-09-2019 - |
题
假设在 Haskell 程序中我有一些数据,其类型如下:
IO [ IO (Int, String, Int) ]
, , 或者IO [ (Int, String, IO Int) ]
, , 或者[ (Int, String, IO Int) ]
但我有应该运行的纯函数 [ (Int, String, Int) ]
. 。看来我必须笨拙地从 IO monad 中删除内部值,直到我得到类似 IO [ (Int, string, Int) ] 的东西,然后(从 IO monad 内部)应用纯函数。我想没有简单的预定义方法可以做到这一点?它将整个数据结构提升为一个单子,将所有 里面 类型转换为纯类型?(那会非常方便!)
解决方案
你可以使用 liftM*
函数从 控制单子 模块,或 liftA*
函数为 应用词.
liftM
允许您提升纯函数以在 Monad 内工作,例如:
ghci> let s = return "Hello" :: IO String
ghci> liftM reverse s
"olleH"
这样你就不必手动编写诸如“s >>= \x -> return (reverse x)
“无处不在。
虽然,这不会帮助你 [(String, Int, IO Int)]
例如,如果您的纯函数处理 [(String, Int, Int)]
. 。由于元组中的第三个元素实际上不是 Int
.
在这种情况下我建议先写一个函数 [(String, Int, IO Int)] -> IO [(String, Int, Int)]
并应用提升的纯函数。
这是我能想到的最通用的函数:
conv :: Monad m => (f (m a) -> m (f a)) -> [f (m a)] -> m [f a]
conv f = sequence . map f
你可以这样称呼它:
liftTrd :: Monad m => (a, b, m c) -> m (a, b, c)
liftTrd (x, y, mz) = mz >>= \z -> return (x, y, z)
conv liftTrd [("hi", 4, return 2)] :: IO [(String, Int, Int)]
仅当您有一个位于类型深处的单个 monad 时,此函数才会起作用。如果你有多个,我认为你真的应该考虑一下你使用的类型,看看是否不能让它变得更简单。
其他提示
首先是下面的解决方案的一些使用示例 reduce
(除非您建议更好的名称):
> reduce [(["ab", "c"], "12")] :: [(String, String)]
[("ab","12"),("c","12")]
> reduce [(["ab", "c"], "12")] :: [(Char, Char)]
[('a','1'),('a','2'),('b','1'),('b','2'),('c','1'),('c','2')]
> reduce [("ab", "12"), ("cd", "3")] :: [(Char, Char)]
[('a','1'),('a','2'),('b','1'),('b','2'),('c','3'),('d','3')]
你的例子也用它解决了:
complexReduce :: Monad m => m (m (a, b, m [m (c, m d)])) -> m (a, b, [(c, d)])
complexReduce = reduce
并实施 reduce
:
{-# LANGUAGE FlexibleContexts, FlexibleInstances, IncoherentInstances, MultiParamTypeClasses, UndecidableInstances #-}
import Control.Monad
-- reduce reduces types to simpler types,
-- when the reduction is in one of the following forms:
-- * make a Monad disappear, like join
-- * move a Monad out, like sequence
-- the whole magic of Reduce is all in its instances
class Reduce s d where
reduce :: s -> d
-- Box is used only for DRY in Reduce instance definitions.
-- Without it we, a Reduce instance would need
-- to be tripled for each variable:
-- Once for a pure value, once for a monadic value,
-- and once for a reducable value
newtype Box a = Box { runBox :: a }
instance Monad m => Reduce (Box a) (m a) where
reduce = return . runBox
instance Reduce a b => Reduce (Box a) b where
reduce = reduce . runBox
redBox :: Reduce (Box a) b => a -> b
redBox = reduce . Box
-- we can join
instance (Monad m
, Reduce (Box a) (m b)
) => Reduce (m a) (m b) where
reduce = join . liftM redBox
-- we can sequence
-- * instance isnt "Reduce [a] (m [b])" so type is always reduced,
-- and thus we avoid overlapping instances.
-- * we cant make it general for any Traversable because then
-- the type system wont find the right patterns.
instance (Monad m
, Reduce (Box a) (m b)
) => Reduce (m [a]) (m [b]) where
reduce = join . liftM (sequence . fmap redBox)
instance (Monad m
, Reduce (Box a) (m c)
, Reduce (Box b) (m d)
) => Reduce (a, b) (m (c, d)) where
reduce (a, b) = liftM2 (,) (redBox a) (redBox b)
instance (Monad m
, Reduce (Box a) (m d)
, Reduce (Box b) (m e)
, Reduce (Box c) (m f)
) => Reduce (a, b, c) (m (d, e, f)) where
reduce (a, b, c) =
liftM3 (,,) (redBox a) (redBox b) (redBox c)
不隶属于 StackOverflow