1
votes

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 (++)', namelyshow 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".

1
What if a is indeed an unshowable type? Either you add a Show constraint, or you need to avoid to print first. Possibly, the input sans the second suffix is already a good representation of first, anyway.chi
Not with Debug.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 fabricate Show dictionaries with very little information -- types are not stored at runtime). But you might want to check out ghci's debugging features.luqui

1 Answers

2
votes

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; then you'll be able to parse un-Showable things again.