使用部分monadic函数在ADT上创建fold / catamorphism

时间:2015-12-22 14:33:45

标签: haskell monads

我有点不确定我正在尝试的实际名称是什么,创建折叠或catamorphism似乎是它的名称。

我通过折叠获得了以下数据结构:

type Order = [Int]
data Orders = Orders FilePath Order Order

type FoldOrders m o os = FilePath -> o -> o -> m os
type FoldOrder m o i = [i] -> m o
type FoldInt m i = Int -> m i

type Fold m os o i = (FoldOrders m o os, FoldOrder m o i, FoldInt m i)

foldOrders :: (Monad m) => Fold m os o i -> Orders -> m os
foldOrders (fos, fo, fi) (Orders orders1 orders2) = do o1 <- foldOrder orders1
                                                       o2 <- foldOrder orders2
                                                       fos o1 o2
    where foldOrder order = do o <- mapM foldInt order
                               fo o
          foldInt int     = fi int

此折叠适用于例如此实施&#39;:

simpleWrite :: Fold IO () () ()
simpleWrite = (fos, fo, fi)
where fos _ _ = return ()
      fo  _   = return ()
      fi  i   = putStrLn $ show i

使用此命令

foldOrders simpleWrite (Orders [1,2] [3,4])

它打印出1 2 3 4,就像你期望的那样。

到目前为止一切顺利,但是......

当我想推动&#39;在这样的数据结构上行走时,记下一些信息(在这种情况下是一个文件路径):

write :: Fold IO a b c
write = (fos, fo, fi)
     where fos path fo1 fo2 = do _ <- fo1 path
                                 _ <- fo2 path
                                 return ()
           fo fis path = do ios <- mapM (\x -> x path) fis
                            return ()           
           fi int path = appendFile path $ show int

我无法编译。它给出了这个错误:

Couldn't match type `FilePath -> IO ()' with `IO c'
Expected type: FoldInt IO c
  Actual type: Int -> FilePath -> IO ()
In the expression: fi

似乎你无法返回像这样的部分monadic函数,但为什么呢?我怎样才能让它发挥作用?

1 个答案:

答案 0 :(得分:1)

如果要写入某个特定文件,那么该文件应该只是write函数的参数,即write :: FilePath -> Fold IO a b c。但据我了解,您希望根据实际数据计算文件路径。在这种情况下,文件路径取决于数据的 size ,因此这是您需要计算的内容。你还需要计算FilePath -> IO ()类型的延续 - 你有后者,但你错过了前者。

write :: Fold IO () (Int, FilePath -> IO ()) (FilePath -> IO ())
write = (fos, fo, fi) where 

 fos :: (Int, FilePath -> IO ()) -> (Int, FilePath -> IO ()) -> IO () 
 fos (sz1, fo1) (sz2, fo2) = do 
   let fp = show (sz1 + sz2) ++ ".txt" 
   sequence_ [fo1 fp, fo2 fp] 

 fo :: [ FilePath -> IO () ] -> IO (Int, FilePath -> IO ())
 fo fis = return (length fis, \fp -> mapM_ ($ fp) fis)

 fi :: Int -> IO (FilePath -> IO ())
 fi int = return $ \fp -> appendFile fp (show int)

如您所见,原理很简单。如果您需要一次计算两件事,只需一次计算两件事!简单地添加大小,而类型FilePath -> IO ()的函数只需逐点提升然后排序。

测试(在ghci中):

>:! cat 4.txt
cat: 4.txt: No such file or directory
>foldOrders write  (Orders [1,2] [3,4])
>:! cat 4.txt
1234>foldOrders write  (Orders [1,2] [3,4])
>:! cat 4.txt
12341234>