繁体   English   中英

Haskell中的奇怪类型问题给了我问题(Par Monad)

[英]weird type issue in haskell giving me issues (Par Monad)

供参考我的代码。

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)]的具体类型,这确实让我感到困惑。 如果有人可以带领我走上正确的道路,将不胜感激。

我遇到了很多错误,但是却使我非常复杂。

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

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

将我上面提到的错误与您的错误进行比较:“无法将类型“ Par”与“ []”匹配”。 啊哈! 需要一个列表 ,但是您使用的是错误的类型( Par )!

现在,从以前的问题中推断出,我想您是Haskell和monad概念的新手。 有很多教程关于他们,包括章节在RWH在LYAH ,所以我不会提供一个在这个答案(它们实际上是相当容易的,不要被辅导的数字吓倒)。 无论哪种方式,您当前的使用情况都将完全关闭。

话虽如此,您应该将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为止。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM