[英]When tracing in Haskell, how does one `show` a value whose data type is not known at compile-time?
I am using GHC version 8.0.2 on Windows 7, & module Debug.Trace
. 我正在Windows 7和模块Debug.Trace
上使用GHC版本8.0.2。 In the trace of the parse
function below, my insertion of ++ show first
results in the following error: 在下面的parse
函数的跟踪中,我插入++ show first
了以下错误的++ show first
结果:
(++)', namely
show first' In the second argument of (++)', namely
show first ++ "," ++ show second ++ ")]"' In the second argument of (++)', namely
" -> [(" ++ show first ++ "," ++ show second ++ ")]"' 在(++)', namely
的第一个参数中(++)', namely
显示为首'在(++)', namely
的第二个参数中(++)', namely
显示为第一个++“,” ++显示第二个++“)]”' (++)', namely
“-> [(“ ++显示第一个++”,“ ++显示第二个++”)]“'' My question: is there a way to show the first element of the ordered pair (a,String)
even though its type is not known at compile-time? 我的问题:有没有办法显示有序对(a,String)
的第一个元素,即使在编译时未知它的类型?
My source code is shown below: 我的源代码如下所示:
{-# LANGUAGE MonomorphismRestriction #-}
import Data.Typeable
import Data.Char
import Debug.Trace
newtype Parser a = P ( String -> [(a,String)] )
parse :: Parser a -> String -> [(a,String)]
parse (P p) input | trace
( let result = (p input)
element = head result
first = fst element
second = snd element
in ("parse maps " ++ input ++ " -> [(" ++ show first ++ "," ++ show second ++ ")]")
) False = undefined
parse (P p) input = p input
nextChar :: Parser Char
nextChar = P ( \input -> case input of { [] -> [] ; (c:cs) -> [(c,cs)] } )
I am hoping to trace evaluation of parse nextChar "ABCD"
. 我希望跟踪parse nextChar "ABCD"
评估。
Yes, sure, just follow the instructions in the error: 是的,可以,请按照错误中的说明进行操作:
parse :: Show a => Parser a -> String -> [(a,String)]
Once you're done debugging, you can delete the call to trace
and the Show
constraint; 调试完成后,您可以删除对trace
的调用和Show
约束。 then you'll be able to parse un- Show
able things again. 那么您将可以再次解析无法Show
内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.