gpt4 book ai didi

haskell - 尝试实现 "the essence of the iterator pattern"

转载 作者:行者123 更新时间:2023-12-04 16:10:26 26 4
gpt4 key购买 nike

我遇到了论文“https://www.cs.ox.ac.uk/jeremy.gibbons/publications/iterator.pdf ”,其中包含相当抽象的伪 Haskell 语法的代码示例。

我正在努力实现 6.2 节中的示例。在真正的 Haskell 中。
这是我走了多远:

module Iterator where
import Data.Functor.Const -- for Const
import Data.Monoid (Sum (..), getSum) -- for Sum
import Control.Monad.State.Lazy -- for State
import Control.Applicative -- for WrappedMonad


data Prod m n a = Prod {pfst:: m a, psnd:: n a} deriving (Show)

instance (Functor m, Functor n) => Functor (Prod m n) where
fmap f (Prod m n) = Prod (fmap f m) (fmap f n)

instance (Applicative m, Applicative n) => Applicative (Prod m n) where
pure x = Prod (pure x) (pure x)
mf <*> mx = Prod (pfst mf <*> pfst mx) (psnd mf <*> psnd mx)

-- Functor Product
x :: (Functor m, Functor n) => (a -> m b) -> (a -> n b) -> (a -> Prod m n b)
(f `x` g) y = Prod (f y) (g y)


type Count = Const (Sum Integer)
count :: a -> Count b
count _ = Const 1

cciBody :: Char -> Count a
cciBody = count

cci :: String -> Count [a]
cci = traverse cciBody

lciBody :: Char -> Count a
lciBody c = Const (Sum $ test (c == '\n'))

test :: Bool -> Integer
test b = if b then 1 else 0

lci :: String -> Count [a]
lci = traverse lciBody

clci :: String -> Prod Count Count [a]
clci = traverse (cciBody `x` lciBody)
-- up to here the code is working

-- can't get this to compile:
wciBody :: Char -> (WrappedMonad (Prod (State Bool) Count)) a
wciBody c = pure $ state (updateState c) where
updateState :: Char -> Bool -> (Integer, Bool)
updateState c w = let s = c /= ' ' in (test (not(w && s)), s)

wci :: String -> (WrappedMonad (Prod (State Bool) Count)) [a]
wci = traverse wciBody

clwci :: String -> (Prod (Prod Count Count) (WrappedMonad (Prod (State Bool) Count))) [a]
clwci = traverse (cciBody `x` lciBody `x` wciBody)

str :: [Char]
str = "hello \n nice \t and \n busy world"

iteratorDemo = do
print $ clci str
print $ clwci str

有问题的地方是 wciBody,我不知道如何实现论文中的 ⇑ 函数。
有任何想法吗?

最佳答案

我认为您可能在论文中使用的中缀类型运算符与定义中的前缀类型构造函数之间进行了错误的翻译。我这样说是因为论文包含

wciBody :: Char → (𝕄 (State Bool) ⊡ Count) a

您已将其翻译为
wciBody :: Char -> (WrappedMonad (Prod (State Bool) Count)) a

我认为这没有意义: Prod x y 没有 Monad 实例,因此将其包装在 WrapMonad 中毫无意义。相反,您打算将 ⊡ 字符视为将其整个左半部分 ( 𝕄 (State Bool) ) 与其右半部分 ( Count ) 分开,类似于 Haskell 中的值级运算符的解析方式:
wciBody :: Char -> Prod (WrappedMonad (State Bool)) Count a

这更有意义,不是吗? Prod 现在接受三个参数,其中前两个都是 * -> * 类型,而 WrappedMonad 的参数显然是一个 monad。这个变化会让你回到正轨吗?

关于haskell - 尝试实现 "the essence of the iterator pattern",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53092246/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com