Haskell:模式解析错误:acc
我发现了一些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)
什么是错的代码?
您需要启用两种语言扩展功能,BangPatterns
和TemplateHaskell
。这些可以以两种方式被启动:
- 从命令行编译
- 当在源文件中的扩展正在使用(优选的)
要在命令线使能它们,通为每个扩展你需要的国旗-XExtensionName
,所以你的情况下,你会有ghc -XTemplateHaskell -XBangPatterns source_file_name.hs
。
使其能在源,使用{-# LANGUAGE ExtensionName #-}
编译在文件的顶部:
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE BangPatterns #-}
module MasterSlave where
...
语言扩展是GHC的Haskell的一个相当大的一部分。有一些是很常见的,他们出现在几乎每个现实世界的应用程序,像OverloadedStrings
它允许您使用Text
和Bytestring
与String
文字语法和MultiParamTypeClasses
是许多像lens
和mtl
更先进的图书馆是必不可少的。其他常见的包括Derive*
扩展名,如DeriveFunctor
,DeriveFoldable
等,它们可以让编译器获得更多的扩展,而不仅仅是标准的Eq
,Show
,Read
和co。
就你而言,BangPatterns
增加了在函数参数和数据类型字段中指定额外严格性的语法。这有助于减少隐性懒惰所带来的问题,但如果您不小心,也可以使用太重的手。 TemplateHaskell
为GHC内置的模板/宏语言启用了许多额外的语法。库作者可以编写带有数据类型,表达式,函数名称或其他构造的模板Haskell函数,并构建不需要留给用户的样板代码,但不容易或简洁地进行抽象。 lens
库与Yesod web框架一起使用了很多。
你有'BangPatterns'功能吗? – bheklilr 2015-01-15 16:12:52
另外'remotable ['slave]'看起来应该是'TemplateHaskell',你也打开了吗? – bheklilr 2015-01-15 16:13:29
我觉得没有。我怎样才能设置这个标志? – nowicode 2015-01-15 16:32:56