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) $
74 , P.ShowToken (P.Token s)
75 , P.ShowErrorComponent e
79 String -> P.Parsec e s a -> P.Parsec e s a
82 import Data.Function (id)
84 debug :: String -> a -> a
88 debug0 :: Pretty a => String -> a -> a
92 debug1 :: (Pretty r, Pretty a) => String -> String -> (a -> r) -> (a -> r)
96 debug2 :: (Pretty r, Pretty a, Pretty b) => String -> String -> String -> (a -> b -> r) -> (a -> b -> r)
97 debug2 _nf _na _nb = id
100 debug2_ :: (Pretty r, Pretty a, Pretty b) => String -> (String,a) -> (String,b) -> r -> r
101 debug2_ _nf _a _b = id
102 {-# INLINE debug2_ #-}
106 , P.ShowToken (P.Token s)
107 , P.ShowErrorComponent e
111 String -> P.Parsec e s a -> P.Parsec e s a
113 {-# INLINE debugParser #-}
118 pretty :: a -> R.Reader Int String
119 default pretty :: Show a => a -> R.Reader Int String
120 pretty = return . show
124 instance Pretty TL.Text
125 instance (Pretty a, Pretty b) => Pretty (a,b) where
128 a' <- R.local (+2) $ pretty a
129 b' <- R.local (+2) $ pretty b
131 "\n" <> List.replicate i ' ' <> "( " <> a' <>
132 "\n" <> List.replicate i ' ' <> ", " <> b' <>
133 "\n" <> List.replicate i ' ' <> ") "
134 instance Pretty a => Pretty [a] where
135 pretty [] = return "[]"
138 s <- R.local (+2) $ mapM pretty as
140 "\n" <> List.replicate i ' ' <> "[ " <>
141 List.intercalate ("\n" <> List.replicate i ' ' <> ", ") s <>
142 "\n" <> List.replicate i ' ' <> "] "
143 instance Pretty a => Pretty (NonEmpty a) where
144 pretty = pretty . toList
145 instance Pretty a => Pretty (Seq a) where
147 | null ss = return "[]"
151 s <- R.local (+2) $ mapM pretty as
153 "\n" <> List.replicate i ' ' <> "[ " <>
154 List.intercalate ("\n" <> List.replicate i ' ' <> ", ") s <>
155 "\n" <> List.replicate i ' ' <> "] "
156 instance Pretty a => Pretty (Maybe a) where
157 pretty Nothing = return "Nothing"
161 instance Show a => Pretty (Tree a) where
162 pretty (Tree n ts) = do
163 s <- R.local (+2) (pretty ts)
164 return $ "Tree "<>showsPrec 11 n ""<>" "<>s