2015-12-21 131 views
0

在基於此article的基於併發性的一般練習中。繼續抽象

我們:

-- a is the result type on which after we continue 
type Continuation a = a-> Action 

type ContinuationPseudoMonad a = Continuation a -> Action 
-- pseudoMonad because it will need Concurrent wrapper Monad: 
-- so as to define bind operation and return operation on it 

data Concurrent a = Concurrent (ContinuationPseudoMonad a) 

所以Concurrent a是一個單子,我們有與它的兩個強制性的法律,回報和綁定來實現。

不幸的是,我發現沒有足夠的詞來更精確地定義ContinuationPseudoMonad的事情......如果我缺少詞彙,我缺乏抽象概念。

你會怎麼稱呼它?

有沒有一個詞的含義Continuation a -> Action而不是我的尷尬無意義ContinuationPseudoMonad

行動之中:

data Action = Atom (IO Action) 
      | Fork Action Action 
      | Stop 
+0

你的問題是如何定義'Concurrent'的'return'和'>> =',就像你上面定義的那樣? – rampion

+0

此外,輕微的錯字 - 我認爲你的意思是'數據Concurrent a = Concurrent(ContinuationPseudoMonad a)',因爲'數據Concurrent a = Concurrent ContinuationPseudoMonad a'是一個錯誤。 – rampion

+0

我已根據您的說法進行了相應更正。事情是我沒有成功掌握什麼是「繼續 - >行動」。這個抽象的名字會很棒。 –

回答

2

顯然Concurrent a相同Cont Action a其中Cont是延續單子。下面是延續一個簡單的解釋:

  1. 考慮函數f :: a -> b一些任意類型的ab。我們想把這個函數轉換成連續傳遞樣式。我們如何做?
  2. 假設我們有一個延續k :: b -> r,它將返回值f作爲輸入,並且它自己返回任意類型的值r。在此之後,我們可以將f轉換爲CPS。
  3. g :: a -> (b -> r) -> r成爲f的CPS版本函數。請注意,它需要一個附加參數(即繼續k)並返回應用於其輸出bk的結果。

讓我們舉一個實際的例子,其中f是謂語功能odd :: Int -> Bool

odd :: Int -> Bool 
odd n = n `mod` 2 == 1 

這裏是寫在延續傳遞風格相同的功能:

odd' :: Int -> (Bool -> r) -> r 
odd' n k = k (n `mod` 2 == 1) 

(Bool -> r) -> r部分可以抽象出作爲延續單子:

data Cont r a = Cont { runCont :: (a -> r) -> r } 

odd' :: Int -> Cont r Bool 
odd' n = return (n `mod` 2 == 1) 

instance Monad (Cont r) where 
    return a = Cont (\k -> k a) 
    m >>= f = Cont (\k -> runCont m (\a -> runCont (f a) k)) 

請注意,對於某些任意類型r,延續k的類型爲Bool -> r。因此,延續k可以是以Bool作爲參數的任何函數。例如:

cont :: Bool -> IO() 
cont = print 

main :: IO() 
main = odd' 21 cont 

然而,在Concurrent的情況下,這是r不是任意的。它專門用於Action。事實上,我們可以定義Concurrent作爲一種代名詞Cont Action如下:

type Concurrent = Cont Action 

現在,我們並不需要實現Monad實例Concurrent,因爲它是與上述定義相同Monad實例Cont r

runConcurrent :: Concurrent a -> ContinuationPseudoMonad a 
runConcurrent (Concurrent g) = g 

instance Monad Concurrent where 
    return a = Concurrent (\k -> k a) 
    m >>= f = Concurrent (\k -> runConcurrent m (\a -> runConcurrent (f a) k)) 

注意無處在instance Monad Concurrent定義有我們利用了Action。這是因爲Concurrent = Cont ActionCont r的monad實例透明地使用r

1

你似乎達到了一定的詞彙,這是短語很難回答的問題。讓我們分解一下你的步驟,看看是否有幫助。

data Action = Atom (IO Action) 
      | Fork Action Action 
      | Stop 

Action是具有三個構造一個代數數據類型。這是一個corecursive數據類型,因爲它是根據自身定義的。

type Continuation a = a -> Action 

Continuation a是用於功能類型a -> Action一個類型別名。這是一個contravariant functor的例子,因爲我們可以定義一個函數

contramap :: (a -> b) -> Continuation b -> Continuation a 
contramap aToB bToAction = aToAction 
    where aToAction = \a -> bToAction (aToB a) 

注逆轉 - contramap需要一個功能a -> b,並創建一個功能Continuation b -> Continuation a

type ContinuationPseudoMonad a = Continuation a -> Action 

ContinuationPseudoMonad a爲函數類型另一種類型的別名,但由於Continuation a也是一個功能類型,ContinuationPseudoMonad a是一種類型高階函數的,因爲它需要一個函數作爲參數。

ContinuationPseudoMonad a也是函子,但它是一個共變函子,因爲我們可以定義一個函數

fmap :: (a -> b) -> ContinuationPseudoMonad a -> ContinuationPseudoMonad b 
fmap aToB aToActionToAction = bToActionToAction 
    where bToActionToAction = \bToAction -> aToActionToAction (\a -> bToAction (aToB a))