I am using GHC version 8.0.2 on Windows 7, & module Debug.Trace
.
In the trace of the parse
function below, my insertion of ++ show first
results in the following error:
- No instance for (Show a) arising from a use of `show' Possible fix: add (Show a) to the context of the type signature for: parse :: Parser a -> String -> [(a, String)]
- In the first argument of
(++)', namely
show first' In the second argument of(++)', namely
show first ++ "," ++ show second ++ ")]"' In the second argument of(++)', namely
" -> [(" ++ show first ++ "," ++ show second ++ ")]"'
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?
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"
.
a
is indeed an unshowable type? Either you add aShow
constraint, or you need to avoid to printfirst
. Possibly, theinput
sans thesecond
suffix is already a good representation offirst
, anyway. – chiDebug.Trace
, and I'm not aware of any modules that can do this (it would be very tricky to implement, since we would have to fabricateShow
dictionaries with very little information -- types are not stored at runtime). But you might want to check outghci
's debugging features. – luqui