haskell - 是-ddump-simpl是获得具体类型的最佳方法吗?

时间:2011-11-03 00:39:40

标签: debugging haskell types ghc concrete

我以前编写过一个似乎有效的函数,但不幸的是我没有很好地编写代码,现在必须再次弄清楚[我正在修改我正在使用的monad变换器堆栈]

run_astvn ::
    LowerMonadT (StateT LowerSketchData Identity) β
    -> Seq SketchAST
run_astvn x = get_ast2 $ runIdentity $
    runStateT (runStateT (runStateT x empty) empty)
        (LowerSketchData Set.empty)
    where get_ast2 = snd . fst

我想得到get_ast2的具体类型。我似乎能够通过我的终端输出添加标志-ddump-simpl和grep,直到找到,(清理一下)

(((β, Seq SketchAST), Seq SketchAST), LowerSketchData) -> Seq SketchAST

(对不起,这对其他人来说可能是胡说八道,但关键是它对我有用。)有更快/更方便的方法吗?如果不是很明显,在这种情况下我所说的“具体”是指上述类型是有用的;知道snd . fst的类型不是:)。

2 个答案:

答案 0 :(得分:8)

我目前有两种方法可以做到这一点,而且它们都是黑客攻击。第一种是使用隐式参数:

{-# LANGUAGE ImplicitParams #-}
import Control.Monad.State
import Control.Monad.Identity
import Data.Sequence
import qualified Data.Set as Set

data LowerSketchData = LowerSketchData (Set.Set Int)
type LowerMonadT m = StateT (Seq SketchAST) (StateT (Seq SketchAST) m)
data SketchAST = SketchAST

--run_astvn ::
--    LowerMonadT (StateT LowerSketchData Identity) β
--    -> Seq SketchAST
run_astvn x = ?get_ast2 $ runIdentity $
    runStateT (runStateT (runStateT x empty) empty)
        (LowerSketchData Set.empty)
--    where get_ast2 = snd . fst

然后,在ghci:

*Main> :t run_astvn
run_astvn
  :: (?get_ast2::(((a, Seq a1), Seq a2), LowerSketchData) -> t) =>
     StateT
       (Seq a1) (StateT (Seq a2) (StateT LowerSketchData Identity)) a
     -> t

另一种方法是给出一个故意错误的类型签名并检查编译器如何抱怨。

import Control.Monad.State
import Control.Monad.Identity
import Data.Sequence
import qualified Data.Set as Set

data LowerSketchData = LowerSketchData (Set.Set Int)
type LowerMonadT m = StateT (Seq SketchAST) (StateT (Seq SketchAST) m)
data SketchAST = SketchAST

run_astvn ::
    LowerMonadT (StateT LowerSketchData Identity) β
    -> Seq SketchAST
run_astvn x = get_ast2 $ runIdentity $
    runStateT (runStateT (runStateT x empty) empty)
        (LowerSketchData Set.empty)
--    where get_ast2 = snd . fst
    where get_ast2 :: (); get_ast2 = undefined

这给出了错误:

test.hs:13:19:
    The first argument of ($) takes one argument,
    but its type `()' has none
    In the expression:
      <snip>

将错误的类型更改为() -> ()

test.hs:13:30:
    Couldn't match expected type `()'
                with actual type `(((β, Seq SketchAST), Seq SketchAST),
                                   LowerSketchData)'
    In the second argument of `($)', namely
      <snip>

现在我们知道类型应该看起来像(((β, Seq SketchAST), Seq SketchAST), LowerSketchData) -> ()。最后一次迭代消除了最终的(),因为编译器抱怨:

test.hs:13:19:
    Couldn't match expected type `Seq SketchAST' with actual type `()'
    In the expression:
      <snip>

...所以其他()应为Seq SketchAST

答案 1 :(得分:3)

骗算编译器。添加错误的类型签名,然后它应回复“无法匹配错误的类型与真实类型”或当前的确切消息。