2 {-# LANGUAGE ConstraintKinds #-}
3 {-# LANGUAGE DefaultSignatures #-}
4 {-# LANGUAGE FlexibleContexts #-}
5 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
6 {-# LANGUAGE OverloadedStrings #-}
7 {-# LANGUAGE Rank2Types #-}
8 {-# LANGUAGE TypeFamilies #-}
9 module Language.TCT.Debug where
11 import Control.Monad (Monad(..), mapM)
13 import Data.Foldable (toList, null)
14 import Data.Function (($), (.))
16 import Data.List.NonEmpty (NonEmpty(..))
17 import Data.Maybe (Maybe(..))
19 import Data.Semigroup (Semigroup(..))
20 import Data.Sequence (Seq)
21 import Data.String (String)
22 import Data.Text (Text)
23 import Data.TreeSeq.Strict (Tree(..))
25 import Text.Show (Show(..))
26 import qualified Control.Monad.Trans.Reader as R
27 import qualified Data.List as List
28 import qualified Data.Text.Lazy as TL
29 import qualified Text.Megaparsec as P
33 import qualified Debug.Trace as Trace
35 debug :: String -> a -> a
38 debug0 :: Pretty a => String -> a -> a
39 debug0 m a = Trace.trace (m <> ": " <> R.runReader (pretty a) 2) a
41 debug1 :: (Pretty r, Pretty a) => String -> String -> (a -> r) -> (a -> r)
43 (\r -> Trace.trace ("] " <> nf <> ": " <> R.runReader (pretty r) 2) r) $
44 (Trace.trace ("[ " <> nf <> ":\n " <> na <> " = " <> R.runReader (pretty a) 2) f)
47 debug1_ :: (Pretty r, Pretty a) => String -> (String,a) -> r -> r
49 Trace.trace ("[ " <> nf <> ":\n " <> na <> " = " <> R.runReader (pretty a) 2) $
50 Trace.trace ("] " <> nf <> ": " <> R.runReader (pretty r) 2) $
53 debug2 :: (Pretty r, Pretty a, Pretty b) => String -> String -> String -> (a -> b -> r) -> (a -> b -> r)
54 debug2 nf na nb f a b =
55 (\r -> Trace.trace ("] " <> nf <> ": " <> R.runReader (pretty r) 2) r) $
58 <> "\n " <> na <> " = " <> R.runReader (pretty a) 2
59 <> "\n " <> nb <> " = " <> R.runReader (pretty b) 2
62 debug2_ :: (Pretty r, Pretty a, Pretty b) => String -> (String,a) -> (String,b) -> r -> r
63 debug2_ nf (na,a) (nb,b) r =
66 <> "\n " <> na <> " = " <> R.runReader (pretty a) 2
67 <> "\n " <> nb <> " = " <> R.runReader (pretty b) 2
69 Trace.trace ("] " <> nf <> ": " <> R.runReader (pretty r) 2) $
72 debug3 :: (Pretty r, Pretty a, Pretty b, Pretty c) => String -> String -> String -> String -> (a -> b -> c -> r) -> (a -> b -> c -> r)
73 debug3 nf na nb nc f a b c =
74 (\r -> Trace.trace ("] " <> nf <> ": " <> R.runReader (pretty r) 2) r) $
77 <> "\n " <> na <> " = " <> R.runReader (pretty a) 2
78 <> "\n " <> nb <> " = " <> R.runReader (pretty b) 2
79 <> "\n " <> nc <> " = " <> R.runReader (pretty c) 2
82 debug3_ :: (Pretty r, Pretty a, Pretty b, Pretty c) => String -> (String,a) -> (String,b) -> (String,c) -> r -> r
83 debug3_ nf (na,a) (nb,b) (nc,c) r =
86 <> "\n " <> na <> " = " <> R.runReader (pretty a) 2
87 <> "\n " <> nb <> " = " <> R.runReader (pretty b) 2
88 <> "\n " <> nc <> " = " <> R.runReader (pretty c) 2
90 Trace.trace ("] " <> nf <> ": " <> R.runReader (pretty r) 2) $
95 , P.ShowToken (P.Token s)
96 , P.ShowErrorComponent e
100 String -> P.Parsec e s a -> P.Parsec e s a
103 import Data.Function (id)
105 debug :: String -> a -> a
109 debug0 :: Pretty a => String -> a -> a
111 {-# INLINE debug0 #-}
113 debug1 :: (Pretty r, Pretty a) => String -> String -> (a -> r) -> (a -> r)
115 {-# INLINE debug1 #-}
117 debug2 :: (Pretty r, Pretty a, Pretty b) => String -> String -> String -> (a -> b -> r) -> (a -> b -> r)
118 debug2 _nf _na _nb = id
119 {-# INLINE debug2 #-}
121 debug2_ :: (Pretty r, Pretty a, Pretty b) => String -> (String,a) -> (String,b) -> r -> r
122 debug2_ _nf _a _b = id
123 {-# INLINE debug2_ #-}
125 debug3 :: (Pretty r, Pretty a, Pretty b, Pretty c) => String -> String -> String -> String -> (a -> b -> c -> r) -> (a -> b -> c -> r)
126 debug3 _nf _na _nb _nc = id
127 {-# INLINE debug3 #-}
129 debug3_ :: (Pretty r, Pretty a, Pretty b, Pretty c) => String -> (String,a) -> (String,b) -> (String,c) -> r -> r
130 debug3_ _nf _a _b _c = id
131 {-# INLINE debug3_ #-}
135 , P.ShowToken (P.Token s)
136 , P.ShowErrorComponent e
140 String -> P.Parsec e s a -> P.Parsec e s a
142 {-# INLINE debugParser #-}
147 pretty :: a -> R.Reader Int String
148 default pretty :: Show a => a -> R.Reader Int String
149 pretty = return . show
153 instance Pretty TL.Text
154 instance (Pretty a, Pretty b) => Pretty (a,b) where
157 a' <- R.local (+2) $ pretty a
158 b' <- R.local (+2) $ pretty b
160 "\n" <> List.replicate i ' ' <> "( " <> a' <>
161 "\n" <> List.replicate i ' ' <> ", " <> b' <>
162 "\n" <> List.replicate i ' ' <> ") "
163 instance Pretty a => Pretty [a] where
164 pretty [] = return "[]"
167 s <- R.local (+2) $ mapM pretty as
169 "\n" <> List.replicate i ' ' <> "[ " <>
170 List.intercalate ("\n" <> List.replicate i ' ' <> ", ") s <>
171 "\n" <> List.replicate i ' ' <> "] "
172 instance Pretty a => Pretty (NonEmpty a) where
173 pretty = pretty . toList
174 instance Pretty a => Pretty (Seq a) where
176 | null ss = return "[]"
180 s <- R.local (+2) $ mapM pretty as
182 "\n" <> List.replicate i ' ' <> "[ " <>
183 List.intercalate ("\n" <> List.replicate i ' ' <> ", ") s <>
184 "\n" <> List.replicate i ' ' <> "] "
185 instance Pretty a => Pretty (Maybe a) where
186 pretty Nothing = return "Nothing"
190 instance Show a => Pretty (Tree a) where
191 pretty (Tree n ts) = do
192 s <- R.local (+2) (pretty ts)
193 return $ "Tree "<>showsPrec 11 n ""<>" "<>s