haskell中奇怪的类型问题给了我一些问题(Par Monad)

时间:2014-08-06 05:08:12

标签: haskell types

参考我的代码。

import Control.Monad.Par

makeGridx:: (Enum a,Num a)=>a->a->a->[a]
makeGridx start end h = [start,(start+h)..end]
makeGridt:: (Enum a, Num a)=>a->a->a->[a]
makeGridt start end h = [start,(start+h)..end]

generateBaseLine:: (Eq a,Num a)=>(a->a)-> [a] -> [(a,a,a)]
generateBaseLine f (x:xs) = if (null xs)
                            then [(x,0,0)]
                            else if(x==0)
                                then (x,0,0) : (generateBaseLine f xs)
                                else (x,0,(f x)) : (generateBaseLine f xs)

--fdm :: (Enum a,Num a) =>a->a->a->a->a->a->a->(a->a)->[(a,a,a)]
--fdm alpha startt endt startx endx dx dt bbFunction = start alpha (makeGridx startx endx dx) (makeGridt startt endt dt) (generateBaseLine bbFunction (makeGridx startx endx dx)) dx dt

--start:: Num a=>a->[a]->[a]->[(a,a,a)]->a->a->[(a,a,a)]
--start alpha (x:xs) (t:ts) (phi:phis) dx dt =  (startPar alpha (x:xs) (ts) (phi:phis) dx dt [] [])

startPar:: Num a =>a->[a]->[a]->[(a,a,a)]->a->a->[(a,a,a)]
startPar alpha (x:xs) (t:ts) (phi1:(ph2:(ph3:phis))) dx dt = (phi1:(ph2:(ph3:phis))) ++ (buildPhiListIds alpha (x:xs) (t:ts) (phi1:(ph2:(ph3:phis))) dx dt [] [])
buildPhiListIds:: Num a=> a->[a]->[a]->[(a,a,a)]->a->a->[Par (IVar (a, a, a))]->[a]->[(a,a,a)]                                                              
buildPhiListIds alpha (x:xs) (t:ts) (phi1:(ph2:(ph3:phis))) dx dt phiIds newX = do 
                                                                        one<-third phi1
                                                                        two<-third ph2
                                                                        three<-third ph3
                                                                        newSolId<- spawn( return (newPhi (x:xs) t (one,two,three,dx,dt,alpha) ))
                                                                        buildPhiListIds alpha xs (t:ts) (ph2:(ph3:phis)) dx dt (phiIds ++ [newSolId]) (newX ++ [x])

buildPhiListIds alpha (0:xs) (t:ts) (phi1:(ph2:(ph3:phis))) dx dt phiIds newX = do 
                                                                        newSolId<-spawn (return (newPhi (0:xs) t (1,2,3,4,5,6)))            
                                                                        buildPhiListIds alpha xs (t:ts) (phi1:(ph2:(ph3:phis))) dx dt (phiIds ++ [newSolId]) (newX ++ [0])

buildPhiListIds alpha [] (t:ts) (phi1:(ph2:(ph3:phis))) dx dt phiIds newX = do
                                                                            (getSolutions (getTuples(getSolutions phiIds))) ++ (buildPhiListIds alpha newX  ts (getSolutions (getTuples(getSolutions phiIds)))  dx dt [] []) 

buildPhiListIds _ _ [] _ _ _ _ _ = []


getTuples::[IVar a]->[Par a]
getTuples (x:xs) = (get x) : (getSolutions xs)
getTuples [] = []  

getSolutions:: [Par a]->[a]
getSolutions (x:xs) = (runPar x):(getTuples xs)
getSolutions [] = []


third (_,_,x)=x

ex f g x = runPar $ do
      fx <- spawn (return (f x))  
      gx <- spawn (return (g x))  
      a <- get fx       
      b <- get gx       
      return (a,b)      
newPhi:: (Eq a,Fractional a)=> [a]->a->(a,a,a,a,a,a)->(a,a,a)
newPhi (0:xs) t (phiL,phiC,phiR,dx,dt,alpha)= (0,t,0)
newPhi (x:[]) t (phiL,phiC,phiR,dx,dt,alpha)= (x,t,0)
newPhi (x:xs) t (phiL,phiC,phiR,dx,dt,alpha)= (x,t,(phiC + (alpha * (dt/(dx^2)))*(phiR -(2*phiC) + phiL)))

我得到了一堆错误,但是很多错误让我很困惑。

heateqpar.hs:28:156:
    Couldn't match type `Par' with `[]'
    Expected type: [IVar (a1, a1, a1)]
      Actual type: Par (IVar (a1, a1, a1))
    In a stmt of a 'do' block:
      newSolId <- spawn
                    (return (newPhi (x : xs) t (one, two, three, dx, dt, alpha))) ::
                    Par (IVar (a, a, a))
    In the expression:
      do { one <- third phi1;
           two <- third ph2;
           three <- third ph3;
           newSolId <- spawn
                         (return (newPhi (x : xs) t (one, two, three, dx, dt, alpha))) ::
                         Par (IVar (a, a, a));
           .... }
    In an equation for `buildPhiListIds':
        buildPhiListIds
          alpha
          (x : xs)
          (t : ts)
          (phi1 : (ph2 : (ph3 : phis)))
          dx
          dt
          phiIds
          newX
          = do { one <- third phi1;
                 two <- third ph2;
                 three <- third ph3;
                 .... }

这是我想要的实际类型,但由于某种原因,它试图强制执行这种不是spawn的返回类型的类型?当我看到这一点似乎在我的类型声明中试图强制执行此操作但是我的类型如下

buildPhiListIds:: Num a=> a->[a]->[a]->[(a,a,a)]->a->a->[Par (IVar (a, a, a))]->[a]->[(a,a,a)]  

我没有看到[IVar(a1,a1,a1)]的具体类型,这让我很困惑。如果有人能带领我走上正确的道路,那将非常感激。

1 个答案:

答案 0 :(得分:2)

  

我得到了一堆错误,但很多错误让我感到困惑。

do表达式中,每个monadic动作必须属于同一个monad。 buildPhiListIds的返回类型为[something],因此do的结果类型为[something]。因此,您的所有操作都应位于列表 monad中,而不是 Par monad中。现在再次查看spawn

spawn :: NFData a => Par a -> Par (IVar a)

将我上面提到的与你的错误进行比较:&#34;无法匹配类型`Par&#39;与`[]&#39;&#34;。啊哈!它需要一个列表,但您使用了错误的类型(Par)!

现在,从你的previous questions推断我认为你是Haskell的新手和monad的概念。关于它们有很多tutorials,包括章节in RWHin LYAH,所以我不会在这个答案中提供一个(他们实际上相当容易,不要&#39} ;受到教程数量的威胁)。无论哪种方式,您当前的使用都完全关闭。

话虽如此,你应该重构buildPhiListIds以具有以下类型:

buildPhiListIds:: Num a => ... -> Par [(a,a,a)]

此外,您对getTuplesgetSolutions的定义没有多大意义。以下内容更简单,可能实现您的实际需求:

getTuples :: [IVar a] -> [Par a]
getTuples = map get

getSolutions :: [Par a] -> [a]
getSolutions = runPar . sequence 

此外,您应该尝试将runPar的呼叫保持在最低限度:

  

runPar函数本身相对昂贵[...]。因此,在使用Par monad时,您通常应该尝试将Par monad线程化到需要并行性的所有位置,以避免需要多次runPar调用。 [...]特别是,对runPar的嵌套调用(在执行另一个Par计算的过程中评估runPar)通常会产生不良结果。

我建议你写一些实际编译的简单程序,直到你得到一般的monad和Par