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(..))
18 import Data.Semigroup (Semigroup(..))
19 import Data.Sequence (Seq)
20 import Data.String (String)
21 import Data.Text (Text)
22 import Data.TreeSeq.Strict (Tree(..))
24 import Text.Show (Show(..))
25 import qualified Control.Monad.Trans.Reader as R
26 import qualified Data.List as List
27 import qualified Data.Text.Lazy as TL
28 import qualified Text.Megaparsec as P
32 import qualified Debug.Trace as Trace
34 debug :: String -> a -> a
37 debug0 :: Pretty a => String -> a -> a
38 debug0 m a = Trace.trace (m <> ": " <> R.runReader (pretty a) 2) a
40 debug1 :: (Pretty r, Pretty a) => String -> String -> (a -> r) -> (a -> r)
42 (\r -> Trace.trace (nf <> ": " <> R.runReader (pretty r) 2) r) $
43 (Trace.trace (nf <> ":\n " <> na <> " = " <> R.runReader (pretty a) 2) f)
46 debug1_ :: (Pretty r, Pretty a) => String -> (String,a) -> r -> r
48 Trace.trace (nf <> ":\n " <> na <> " = " <> R.runReader (pretty a) 2) $
49 Trace.trace (nf <> ": " <> R.runReader (pretty r) 2) $
52 debug2 :: (Pretty r, Pretty a, Pretty b) => String -> String -> String -> (a -> b -> r) -> (a -> b -> r)
53 debug2 nf na nb f a b =
54 (\r -> Trace.trace (nf <> ": " <> R.runReader (pretty r) 2) r) $
57 <> "\n " <> na <> " = " <> R.runReader (pretty a) 2
58 <> "\n " <> nb <> " = " <> R.runReader (pretty b) 2
61 debug2_ :: (Pretty r, Pretty a, Pretty b) => String -> (String,a) -> (String,b) -> r -> r
62 debug2_ nf (na,a) (nb,b) r =
65 <> "\n " <> na <> " = " <> R.runReader (pretty a) 2
66 <> "\n " <> nb <> " = " <> R.runReader (pretty b) 2
68 Trace.trace (nf <> ": " <> R.runReader (pretty r) 2) $
71 debugParser :: Show a => String -> P.Parsec e s a -> P.Parsec e s a
74 import Data.Function (id)
76 debug :: String -> a -> a
80 debug0 :: Pretty a => String -> a -> a
84 debug1 :: (Pretty r, Pretty a) => String -> String -> (a -> r) -> (a -> r)
88 debug2 :: (Pretty r, Pretty a, Pretty b) => String -> String -> String -> (a -> b -> r) -> (a -> b -> r)
89 debug2 _nf _na _nb = id
92 debug2_ :: (Pretty r, Pretty a, Pretty b) => String -> (String,a) -> (String,b) -> r -> r
93 debug2_ _nf _a _b = id
94 {-# INLINE debug2_ #-}
96 debugParser :: Show a => String -> P.Parsec e s a -> P.Parsec e s a
98 {-# INLINE debugParser #-}
103 pretty :: a -> R.Reader Int String
104 default pretty :: Show a => a -> R.Reader Int String
105 pretty = return . show
108 instance Pretty TL.Text
109 instance (Pretty a, Pretty b) => Pretty (a,b) where
112 a' <- R.local (+2) $ pretty a
113 b' <- R.local (+2) $ pretty b
115 "\n" <> List.replicate i ' ' <> "( " <> a' <>
116 "\n" <> List.replicate i ' ' <> ", " <> b' <>
117 "\n" <> List.replicate i ' ' <> ") "
118 instance Pretty a => Pretty [a] where
119 pretty [] = return "[]"
122 s <- R.local (+2) $ mapM pretty as
124 "\n" <> List.replicate i ' ' <> "[ " <>
125 List.intercalate ("\n" <> List.replicate i ' ' <> ", ") s <>
126 "\n" <> List.replicate i ' ' <> "] "
127 instance Pretty a => Pretty (NonEmpty a) where
128 pretty = pretty . toList
129 instance Pretty a => Pretty (Seq a) where
131 | null ss = return "[]"
135 s <- R.local (+2) $ mapM pretty as
137 "\n" <> List.replicate i ' ' <> "[ " <>
138 List.intercalate ("\n" <> List.replicate i ' ' <> ", ") s <>
139 "\n" <> List.replicate i ' ' <> "] "
140 instance Pretty a => Pretty (Maybe a) where
141 pretty Nothing = return "Nothing"
145 instance Show a => Pretty (Tree a) where
146 pretty (Tree n ts) = do
147 s <- R.local (+2) (pretty ts)
148 return $ "Tree "<>showsPrec 11 n ""<>" "<>s