Haskell / GHC:{ - #SPECIALIZE# - }导致'RULE左手太复杂而无法解除'警告 [英] Haskell / GHC: {-# SPECIALIZE #-} Causes 'RULE left-hand side too complicated to desugar' Warning
问题描述
我有一个使用monad来抽象实际实现是在ST还是IO内部运行的代码体。由于内联和缺少类型类函数调用开销,移除额外的抽象层并仅替换具体类型会带来巨大的加速(〜4.5x)。我正在考虑通过使用专门的编译指示来获得一些性能,但我从编译器中获得了一个毫无意义的警告。我不能做一个简单的复制案例,因为这个简单的例子似乎有效,而且我不知道是什么导致了我的实际程序中的不同。
基本上,我的程序执行以下操作:
{ - #LANGUAGE FlexibleInstances,RankNTypes# - }
模块STImpl(runAbstractST ,MonadAbstractIOST(..),ReaderST)其中
import Control.Monad.Reader
import Control.Monad.ST
class Monad m => MonadAbstractIOST m其中
addstuff :: Int - > m Int
$ b $类型ReaderST s = ReaderT(Int)(ST s)
实例MonadAbstractIOST(ReaderST s)其中
addstuff a = return。 (a +)=<<询问
runAbstractST ::(全部。ReaderST s a) - > a
runAbstractST f = runST $ runReaderT f 99
和
模块Main(main)其中
导入STImpl
导入Control.Monad
{ - #SPECIALIZE INLINE useAbstractMonad :: ReaderST s Int# - }
useAbstractMonad :: MonadAbstractIOST m => (a +)=<<(addstuff b))0 [1..50000000]
$ b $($ + $ ab $ m
main :: IO()
main = do
let st = runAbstractST useAbstractMonad
putStrLn。显示$ st
现在,这里一切似乎都正常。但是在我的程序中,我得到了
RULE左边太复杂了,不能解析
let {
$ dFunctor :: Functor(RSTSim s)
[LclId]
$ dFunctor =
Control.Monad.Trans.Reader。$ fFunctorReaderT
@(MonadSim.SimState s)$ b $
模拟
@(Control.Monad.Trans.Reader.ReaderT
($) (MonadSim.SimState s)(GHC.ST.ST s))
(MonadSim。$ fMonadSimReaderT
@ s
$ dFunctor
(Control.Monad.Trans.Reader。$ fMonadReaderT
@(MonadSim.SimState s)
@(GHC.ST.ST s)
(GHC.ST. $ fMonadST @ s))
(Control.Monad.Trans .Reader。$ fApplicativeReaderT
@(MonadSim.SimState s)
@(GHC.ST.ST s)
$ dFunctor
(Control.Applicative。$ fApplicativeST0
@ s(GHC.ST. $ fFunctorST @ s))))
我不明白'左边','太复杂'和'desugar'是什么意思; - )
看来我有和这里描述的相同的问题: http://marc.info/?l=haskell-cafe&m=133242702914511 p>
我如何诊断?
谢谢!
在7.10 RC1上,这个错误不再发生,所以它看起来像 https://ghc.haskell.org/trac/ghc/ticket/8848 可能有帮助。
I have a body of code that uses a monad to abstract whether the actual implementation runs inside ST or IO. Removing the extra layer of abstraction and just substituting concrete types gives a huge speedup (~4.5x) due to the inlining and missing typeclass function call overhead. I was thinking of getting some of that performance by using a specialize pragma, but I'm getting a rather meaningless warning from the compiler. I can't make a simple reproduction case as the simple example seems to work, and I don't know what's causing the difference in my actual program.
Basically, my program does this:
{-# LANGUAGE FlexibleInstances, RankNTypes #-}
module STImpl (runAbstractST, MonadAbstractIOST(..), ReaderST) where
import Control.Monad.Reader
import Control.Monad.ST
class Monad m => MonadAbstractIOST m where
addstuff :: Int -> m Int
type ReaderST s = ReaderT (Int) (ST s)
instance MonadAbstractIOST (ReaderST s) where
addstuff a = return . (a +) =<< ask
runAbstractST :: (forall s. ReaderST s a) -> a
runAbstractST f = runST $ runReaderT f 99
and
module Main (main) where
import STImpl
import Control.Monad
{-# SPECIALIZE INLINE useAbstractMonad :: ReaderST s Int #-}
useAbstractMonad :: MonadAbstractIOST m => m Int
useAbstractMonad = foldM (\a b -> a `seq` return . (a +) =<< (addstuff b)) 0 [1..50000000]
main :: IO ()
main = do
let st = runAbstractST useAbstractMonad
putStrLn . show $ st
Now, here everything seems to work fine. But in my program I get
RULE left-hand side too complicated to desugar
let {
$dFunctor :: Functor (RSTSim s)
[LclId]
$dFunctor =
Control.Monad.Trans.Reader.$fFunctorReaderT
@ (MonadSim.SimState s)
@ (GHC.ST.ST s)
(GHC.ST.$fFunctorST @ s) } in
simulate
@ (Control.Monad.Trans.Reader.ReaderT
(MonadSim.SimState s) (GHC.ST.ST s))
(MonadSim.$fMonadSimReaderT
@ s
$dFunctor
(Control.Monad.Trans.Reader.$fMonadReaderT
@ (MonadSim.SimState s)
@ (GHC.ST.ST s)
(GHC.ST.$fMonadST @ s))
(Control.Monad.Trans.Reader.$fApplicativeReaderT
@ (MonadSim.SimState s)
@ (GHC.ST.ST s)
$dFunctor
(Control.Applicative.$fApplicativeST0
@ s (GHC.ST.$fFunctorST @ s))))
I don't understand what 'left-hand side', 'too complicated' and 'desugar' mean ;-)
It seems I have the same problem as described here: http://marc.info/?l=haskell-cafe&m=133242702914511
How do I diagnose this? How do I figure out what's causing the optimization to be disabled in my program?
Thanks!
For what it's worth, on the 7.10 RC1 this error no longer occurs, so it looks like the fix to https://ghc.haskell.org/trac/ghc/ticket/8848 may have helped.
这篇关于Haskell / GHC:{ - #SPECIALIZE# - }导致'RULE左手太复杂而无法解除'警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!