我找到了一些 Cloud Haskell 演示,并尝试运行它,但出现错误,但不知道为什么。错误看起来像:
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)
Run Code Online (Sandbox Code Playgroud)
这段代码有什么问题?
您需要启用两种语言扩展,BangPatterns并且TemplateHaskell. 这些可以通过两种方式启用:
要在命令行启用它们,请-XExtensionName为您需要的每个扩展传递标志,因此对于您的情况,您将拥有ghc -XTemplateHaskell -XBangPatterns source_file_name.hs.
要在源代码中启用它们,请使用{-# LANGUAGE ExtensionName #-}文件顶部的编译指示:
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE BangPatterns #-}
module MasterSlave where
...
Run Code Online (Sandbox Code Playgroud)
语言扩展是 GHC Haskell 的重要组成部分。有些非常常见,以至于它们出现在几乎每个现实世界的应用程序中,例如OverloadedStrings允许您使用TextandBytestring与String文字语法,并且MultiParamTypeClasses对于许多更高级的库(如lensand )来说是不可或缺的mtl。其他常见的包括、等Derive*扩展,它们可以让编译器派生出比标准、、和 co 更多的扩展。DeriveFunctorDeriveFoldableEqShowRead
在您的情况下,BangPatterns添加用于在函数参数和数据类型字段中指定额外严格性的语法。这有助于减少隐性懒惰带来的问题,但如果你不小心的话,它也可能会用得太重。 TemplateHaskell为 GHC 内置的模板/宏语言启用了许多额外的语法。库作者可以编写模板 Haskell 函数,这些函数接受数据类型、表达式、函数名称或其他构造,并构建不需要留给用户的样板代码,但否则不容易或简洁地进行抽象。该lens库经常使用它以及 Yesod Web 框架。
| 归档时间: |
|
| 查看次数: |
442 次 |
| 最近记录: |