5

假设在 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 内部)应用纯函数。我想没有简单的预定义方法可以做到这一点?将整个数据结构提升为 monad,将所有内部类型转换为纯类型的东西?(那会很方便!)

4

2 回答 2

6

您可以使用Control.Monad模块中的函数,或liftM*applicatives函数。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 时才会起作用。如果你有多个,我认为你真的应该考虑你使用的类型,看看你是否不能让它更简单。

于 2009-07-21T18:07:04.473 回答
4

首先是以下解决方案的一些用法示例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)
于 2009-07-21T20:50:00.590 回答