2 {-# LANGUAGE ConstraintKinds #-}
3 {-# LANGUAGE DefaultSignatures #-}
4 {-# LANGUAGE FlexibleContexts #-}
5 {-# LANGUAGE OverloadedStrings #-}
6 {-# LANGUAGE Rank2Types #-}
7 {-# LANGUAGE TypeFamilies #-}
8 module Hdoc.TCT.Debug where
10 import Control.Monad (Monad(..), mapM)
12 import Data.Eq (Eq(..))
13 import Data.Foldable (toList, null)
14 import Data.Function (($), (.), id)
16 import Data.Ratio (Ratio)
17 import Data.List.NonEmpty (NonEmpty(..))
18 import Data.Maybe (Maybe(..))
20 import Data.Semigroup (Semigroup(..))
21 import Data.Sequence (Seq)
22 import Data.String (String)
23 import Data.Text (Text)
24 import Prelude ((+), Integer)
25 import Text.Show (Show(..))
26 import qualified Control.Monad.Trans.Reader as R
27 import qualified Data.HashMap.Strict as HM
28 import qualified Data.List as List
29 import qualified Data.Map.Strict as Map
30 import qualified Data.TreeSeq.Strict as TS
31 import qualified Data.Tree as Tree
32 import qualified Data.Text.Lazy as TL
33 import qualified Debug.Trace as Trace
34 import qualified Text.Megaparsec as P
36 trace :: String -> a -> a
42 debug :: String -> a -> a
45 debug0 :: Pretty a => String -> a -> a
46 debug0 m a = Trace.trace (m <> ": " <> runPretty 2 a) a
48 debug1 :: (Pretty r, Pretty a) => String -> String -> (a -> r) -> (a -> r)
50 (\r -> Trace.trace ("] " <> nf <> ": " <> runPretty 2 r) r) $
51 Trace.trace ("[ " <> nf <> ":\n " <> na <> " = " <> runPretty 2 a) f
54 debug1_ :: (Pretty r, Pretty a) => String -> (String,a) -> r -> r
56 Trace.trace ("[ " <> nf <> ":\n " <> na <> " = " <> runPretty 2 a) $
57 Trace.trace ("] " <> nf <> ": " <> runPretty 2 r) $
60 debug2 :: (Pretty r, Pretty a, Pretty b) => String -> String -> String -> (a -> b -> r) -> (a -> b -> r)
61 debug2 nf na nb f a b =
62 (\r -> Trace.trace ("] " <> nf <> ": " <> runPretty 2 r) r) $
65 <> "\n " <> na <> " = " <> runPretty 2 a
66 <> "\n " <> nb <> " = " <> runPretty 2 b
69 debug2_ :: (Pretty r, Pretty a, Pretty b) => String -> (String,a) -> (String,b) -> r -> r
70 debug2_ nf (na,a) (nb,b) r =
73 <> "\n " <> na <> " = " <> runPretty 2 a
74 <> "\n " <> nb <> " = " <> runPretty 2 b
76 Trace.trace ("] " <> nf <> ": " <> runPretty 2 r) $
79 debug3 :: (Pretty r, Pretty a, Pretty b, Pretty c) => String -> String -> String -> String -> (a -> b -> c -> r) -> (a -> b -> c -> r)
80 debug3 nf na nb nc f a b c =
81 (\r -> Trace.trace ("] " <> nf <> ": " <> runPretty 2 r) r) $
84 <> "\n " <> na <> " = " <> runPretty 2 a
85 <> "\n " <> nb <> " = " <> runPretty 2 b
86 <> "\n " <> nc <> " = " <> runPretty 2 c
89 debug3_ :: (Pretty r, Pretty a, Pretty b, Pretty c) => String -> (String,a) -> (String,b) -> (String,c) -> r -> r
90 debug3_ nf (na,a) (nb,b) (nc,c) r =
93 <> "\n " <> na <> " = " <> runPretty 2 a
94 <> "\n " <> nb <> " = " <> runPretty 2 b
95 <> "\n " <> nc <> " = " <> runPretty 2 c
97 Trace.trace ("] " <> nf <> ": " <> runPretty 2 r) $
102 debug :: String -> a -> a
106 debug0 :: Pretty a => String -> a -> a
108 {-# INLINE debug0 #-}
110 debug1 :: (Pretty r, Pretty a) => String -> String -> (a -> r) -> (a -> r)
112 {-# INLINE debug1 #-}
114 debug1_ :: (Pretty r, Pretty a) => String -> (String,a) -> r -> r
116 {-# INLINE debug1_ #-}
118 debug2 :: (Pretty r, Pretty a, Pretty b) => String -> String -> String -> (a -> b -> r) -> (a -> b -> r)
119 debug2 _nf _na _nb = id
120 {-# INLINE debug2 #-}
122 debug2_ :: (Pretty r, Pretty a, Pretty b) => String -> (String,a) -> (String,b) -> r -> r
123 debug2_ _nf _a _b = id
124 {-# INLINE debug2_ #-}
126 debug3 :: (Pretty r, Pretty a, Pretty b, Pretty c) => String -> String -> String -> String -> (a -> b -> c -> r) -> (a -> b -> c -> r)
127 debug3 _nf _na _nb _nc = id
128 {-# INLINE debug3 #-}
130 debug3_ :: (Pretty r, Pretty a, Pretty b, Pretty c) => String -> (String,a) -> (String,b) -> (String,c) -> r -> r
131 debug3_ _nf _a _b _c = id
132 {-# INLINE debug3_ #-}
136 #if DEBUG && DEBUG_PARSER
139 , P.ShowToken (P.Token s)
140 , P.ShowErrorComponent e
144 String -> P.ParsecT e s m a -> P.ParsecT e s m a
149 , P.ShowToken (P.Token s)
150 , P.ShowErrorComponent e
154 String -> P.ParsecT e s m a -> P.ParsecT e s m a
156 {-# INLINE debugParser #-}
161 pretty :: a -> R.Reader Int String
162 default pretty :: Show a => a -> R.Reader Int String
163 pretty = return . show
165 runPretty :: Pretty a => Int -> a -> String
166 runPretty i a = pretty a `R.runReader` i
170 instance Pretty Integer
171 instance (Pretty a, Show a) => Pretty (Ratio a)
173 instance Pretty TL.Text
174 instance Pretty P.Pos
175 instance (Pretty a, Pretty b) => Pretty (a,b) where
178 a' <- R.local (+2) $ pretty a
179 b' <- R.local (+2) $ pretty b
181 (if i == 0 then "" else "\n") <>
182 List.replicate i ' ' <> "( " <> a' <>
183 "\n" <> List.replicate i ' ' <> ", " <> b' <>
184 "\n" <> List.replicate i ' ' <> ") "
185 instance Pretty a => Pretty [a] where
186 pretty [] = return "[]"
189 s <- R.local (+2) $ mapM pretty as
191 (if i == 0 then "" else "\n") <>
192 List.replicate i ' ' <> "[ " <>
193 List.intercalate ("\n" <> List.replicate i ' ' <> ", ") s <>
194 "\n" <> List.replicate i ' ' <> "] "
195 instance (Pretty k, Pretty a) => Pretty (Map.Map k a) where
196 pretty = pretty . Map.toList
197 instance (Pretty k, Pretty a) => Pretty (HM.HashMap k a) where
198 pretty = pretty . HM.toList
199 instance Pretty a => Pretty (NonEmpty a) where
200 pretty = pretty . toList
201 instance Pretty a => Pretty (Seq a) where
203 | null ss = return "[]"
207 s <- R.local (+2) $ mapM pretty as
209 (if i == 0 then "" else "\n") <>
210 List.replicate i ' ' <> "[ " <>
211 List.intercalate ("\n" <> List.replicate i ' ' <> ", ") s <>
212 "\n" <> List.replicate i ' ' <> "] "
213 instance Pretty a => Pretty (Maybe a) where
214 pretty Nothing = return "Nothing"
218 instance Show a => Pretty (Tree.Tree a) where
219 pretty (Tree.Node n ts) = do
220 s <- R.local (+2) (pretty ts)
221 return $ "Tree "<>showsPrec 11 n ""<>" "<>s
222 instance Show a => Pretty (TS.Tree a) where
223 pretty (TS.Tree n ts) = do
224 s <- R.local (+2) (pretty ts)
225 return $ "Tree "<>showsPrec 11 n ""<>" "<>s