2015-01-15 71 views
0

我發現了一些Cloud Haskell演示,我嘗試運行它,但出現錯誤,我不知道爲什麼。錯誤的樣子:Haskell:模式解析錯誤:acc

MasterSlave.hs:18:9:解析錯誤的模式:ACC

的代碼MasterSlave.hs是:

module MasterSlave where 

import Control.Monad 
import Control.Distributed.Process 
import Control.Distributed.Process.Closure 
import PrimeFactors 

slave :: (ProcessId, Integer) -> Process() 
slave (pid, n) = send pid (numPrimeFactors n) 

remotable ['slave] 

-- | Wait for n integers and sum them all up 
sumIntegers :: Int -> Process Integer 
sumIntegers = go 0 
    where 
    go :: Integer -> Int -> Process Integer 
    go !acc 0 = return acc 
    go !acc n = do 
     m <- expect 
     go (acc + m) (n - 1) 

data SpawnStrategy = SpawnSyncWithReconnect 
        | SpawnSyncNoReconnect 
        | SpawnAsync 
    deriving (Show, Read) 

master :: Integer -> SpawnStrategy -> [NodeId] -> Process Integer 
master n spawnStrategy slaves = do 
    us <- getSelfPid 

    -- Distribute 1 .. n amongst the slave processes 
    spawnLocal $ case spawnStrategy of 
    SpawnSyncWithReconnect -> 
     forM_ (zip [1 .. n] (cycle slaves)) $ \(m, there) -> do 
     them <- spawn there ($(mkClosure 'slave) (us, m)) 
     reconnect them 
    SpawnSyncNoReconnect -> 
     forM_ (zip [1 .. n] (cycle slaves)) $ \(m, there) -> do 
     _them <- spawn there ($(mkClosure 'slave) (us, m)) 
     return() 
    SpawnAsync -> 
     forM_ (zip [1 .. n] (cycle slaves)) $ \(m, there) -> do 
     spawnAsync there ($(mkClosure 'slave) (us, m)) 
     _ <- expectTimeout 0 :: Process (Maybe DidSpawn) 
     return() 

    -- Wait for the result 
    sumIntegers (fromIntegral n) 

什麼是錯的代碼?

+0

你有'BangPatterns'功能嗎? – bheklilr 2015-01-15 16:12:52

+0

另外'remotable ['slave]'看起來應該是'TemplateHaskell',你也打開了嗎? – bheklilr 2015-01-15 16:13:29

+0

我覺得沒有。我怎樣才能設置這個標誌? – nowicode 2015-01-15 16:32:56

回答

4

您需要啓用兩種語言擴展功能,BangPatternsTemplateHaskell。這些可以以兩種方式被啓動:

  1. 從命令行編譯
  2. 當在源文件中的擴展正在使用(優選的)

要在命令線使能它們,通爲每個擴展你需要的國旗-XExtensionName,所以你的情況下,你會有ghc -XTemplateHaskell -XBangPatterns source_file_name.hs

使其能在源,使用{-# LANGUAGE ExtensionName #-}編譯在文件的頂部:

{-# LANGUAGE TemplateHaskell #-} 
{-# LANGUAGE BangPatterns #-} 
module MasterSlave where 

... 

語言擴展是GHC的Haskell的一個相當大的一部分。有一些是很常見的,他們出現在幾乎每個現實世界的應用程序,像OverloadedStrings它允許您使用TextBytestringString文字語法和MultiParamTypeClasses是許多像lensmtl更先進的圖書館是必不可少的。其他常見的包括Derive*擴展名,如DeriveFunctor,DeriveFoldable等,它們可以讓編譯器獲得更多的擴展,而不僅僅是標準的Eq,Show,Read和co。

就你而言,BangPatterns增加了在函數參數和數據類型字段中指定額外嚴格性的語法。這有助於減少隱性懶惰所帶來的問題,但如果您不小心,也可以使用太重的手。 TemplateHaskell爲GHC內置的模板/宏語言啓用了許多額外的語法。庫作者可以編寫帶有數據類型,表達式,函數名稱或其他構造的模板Haskell函數,並構建不需要留給用戶的樣板代碼,但不容易或簡潔地進行抽象。 lens庫與Yesod web框架一起使用了很多。