2011-08-28 201 views
26

我刚刚重新发明了一些monad,但我不确定哪一个。它可以让你对一个计算的步骤进行建模,这样你就可以交错计算大量的步骤,找出哪一步先完成。Haskell:我刚刚改造了哪个monad?

{-# LANGUAGE ExistentialQuantification #-} 
module Computation where 

-- model the steps of a computation 
data Computation a = forall b. Step b (b -> Computation a) | Done a 

instance Monad Computation where 
    (Step b g) >>= f = Step b $ (>>=f) . g 
    (Done b) >>= f = Step b f 
    return = Done 

runComputation :: Computation a -> a 
runComputation (Step b g) = runComputation (g b) 
runComputation (Done a) = a 

isDone :: Computation a -> Bool 
isDone (Done _) = True 
isDone _ = False 

-- an order for a set of computations 
data Schedule a = a :> Computation (Schedule a) | Last 

toList :: Schedule a -> [a] 
toList Last = [] 
toList (a :> c) = a : (toList . runComputation) c 

-- given a set of computations, find a schedule to generate all their results 
type Strategy a = [Computation a] -> Computation (Schedule a) 

-- schedule all the completed computations, and step the rest, 
-- passing the remaining to the given function 
scheduleOrStep :: (Queue (Computation a) -> Computation (Schedule a)) -> Strategy a 
scheduleOrStep s cs = scheduleOrStep' id cs 
    where scheduleOrStep' q ((Done a):cs) = Done $ a :> scheduleOrStep' q cs 
     scheduleOrStep' q ((Step b g):cs) = scheduleOrStep' (q . (g b:)) cs 
     scheduleOrStep' q [] = s q 

-- schedule all completed compuations, step all the rest once, and repeat 
-- (may never complete for infinite lists) 
-- checking each row of 
-- [ [ c0s0, c1s0, c2s0, ... ] 
-- , [ c0s1, c1s1, c2s1, ... ] 
-- , [ c0s2, c1s2, c2s2, ... ] 
-- ... 
-- ] 
-- (where cNsM is computation N stepped M times) 
fair :: Strategy a 
fair [] = Done Last 
fair cs = scheduleOrStep (fair . ($[])) cs 

-- schedule more steps for earlier computations rather than later computations 
-- (works on infinite lists) 
-- checking the sw-ne diagonals of 
-- [ [ c0s0, c1s0, c2s0, ... ] 
-- , [ c0s1, c1s1, c2s1, ... ] 
-- , [ c0s2, c1s2, c2s2, ... ] 
-- ... 
-- ] 
-- (where cNsM is computation N stepped M times) 
diag :: Enqueue (Computation a)-> Strategy a 
diag _ [] = Done Last 
diag enq cs = diag' cs id 
    where diag' (c:cs) q = scheduleOrStep (diag' cs) (enq c q $ []) 
     diag' [] q = fair (q []) 

-- diagonal downwards : 
-- [ c0s0, 
-- c1s0, c0s1, 
-- c2s0, c1s1, c0s2, 
-- ... 
-- cNs0, c{N-1}s1, ..., c1s{N-1}, c0sN, 
-- ... 
-- ] 
diagd :: Strategy a 
diagd = diag prepend 

-- diagonal upwards : 
-- [ c0s0, 
-- c0s1, c1s0, 
-- c0s2, c1s1, c2s0, 
-- ... 
-- c0sN, c1s{N-1}, ..., c{s1N-1}, cNs0, 
-- ... 
-- ] 
diagu :: Strategy a 
diagu = diag append 

-- a queue type 
type Queue a = [a] -> [a] 
type Enqueue a = a -> Queue a -> Queue a 

append :: Enqueue a 
append x q = q . (x:) 

prepend :: Enqueue a 
prepend x q = (x:) . q 

我觉得这可能是某种线程monad?

+18

哈斯克尔是我所知道的唯一一种语言,你无法分辨出你刚刚重新创建了哪个轮子... –

+0

我即将关闭_too localized_,但人们是否真的花时间知道他们正在重塑Haskell中的东西但不是他们正在重塑的问题,使这个问题变得合法(假设很多人最终会重塑这个确切的东西,不管它是什么)? – Mat

+11

@Mat:是的,其实。至少在某些方面。在Haskell中,人们偶尔会做出一些不太有趣的事情,只要给出足够的泛型代码,如果它的类型检查几乎肯定会做一些有用的事情,即使你不确定是什么。这有点类似,因为如果你发明了某些东西来解决某个具体问题,并且它很容易理解,那么很可能已经完成了。当我第一次学习Haskell时,至少有一次我推广了一个特定的解决方案,只是意识到我已经改造了标准库中一个不起眼的角落。 –

回答

2

我没花太多时间来理解你的代码,但它听起来像monad-coroutine包中的协程monad,它可能有点更一般。

+0

我简要地瞥了一眼 - 它看起来像是基于通信的协同程序,而它们是非交流的。 – rampion

2

这看起来与Don Stewart先前使用的流融合的定义类似,也与迭代有些相关(尽管没有使用枚举器将数据推入迭代器的概念),但不如流合成我猜。

5

我不明白为什么不

data Computation a = Step (Computation a) | Done a 

instance Monad Computation where 
    (Step g) >>= f = Step $ g >>= f 
    (Done b) >>= f = Step (f b) 
    return = Done 

我不知道这个单子是什么,但它绝对简单,似乎是在许多方面等价的。

+0

好点。我喜欢。 – rampion

+1

这是简单的恢复monad。 Rampion的原始monad具有类似'unfoldr'函数的线程状态,但我无法真正了解这个状态对于给出的例子是否必要。在他的一篇论文中,威廉·哈里森评论说恢复单子基本上是“惰性的”,但没有增加状态(惰性不是他的原始单词,但目前我找不到这个单词)。 –

+1

@stephen tetley:然而,我已经给出了状态的存在性量化,没有什么*可以用它来完成,所以实际上,它只是延迟了计算。无论如何懒惰的评估是什么,所以它相当于恢复monad。所以这就是答案。 – rampion

相关问题