Skip to content

Commit de6ddad

Browse files
committed
Make Pretty prettier
1 parent 4872094 commit de6ddad

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

dev-tools/package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ executables:
157157
- base
158158
- aeson
159159
- bytestring
160+
- containers
160161
- hs-backend-booster
161162
- prettyprinter
162163
- text

dev-tools/pretty/Pretty.hs

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE FlexibleContexts #-}
12
{-# LANGUAGE PatternSynonyms #-}
23

34
{- | Pretty printer for JSON KORE terms
@@ -9,8 +10,20 @@ module Main (
910
main,
1011
) where
1112

13+
import Control.Monad (unless)
14+
import Control.Monad.Trans.Except
15+
import Data.Aeson (eitherDecode)
16+
import Data.ByteString.Lazy qualified as BS
17+
import Data.Map (Map)
18+
import Data.Map qualified as Map
19+
import Data.Text (Text)
20+
import Data.Text.IO qualified as Text
21+
import Prettyprinter
22+
import System.Environment (getArgs)
23+
24+
import Booster.Pattern.Base (Term, Variable)
1225
import Booster.Pattern.Pretty
13-
import Booster.Prettyprinter (renderDefault)
26+
import Booster.Prettyprinter (renderDefault, renderText)
1427
import Booster.Syntax.Json (KoreJson (..))
1528
import Booster.Syntax.Json.Internalise (
1629
InternalisedPredicates (..),
@@ -21,12 +34,6 @@ import Booster.Syntax.Json.Internalise (
2134
pattern DisallowAlias,
2235
)
2336
import Booster.Syntax.ParsedKore (internalise, parseKoreDefinition)
24-
import Control.Monad.Trans.Except
25-
import Data.Aeson (eitherDecode)
26-
import Data.ByteString.Lazy qualified as BS
27-
import Data.Text.IO qualified as Text
28-
import Prettyprinter
29-
import System.Environment (getArgs)
3037

3138
main :: IO ()
3239
main = do
@@ -40,24 +47,37 @@ main = do
4047
Left err -> putStrLn $ "Error: " ++ err
4148
Right KoreJson{term} -> do
4249
case runExcept $ internalisePattern DisallowAlias CheckSubsorts Nothing internalDef term of
43-
Right (trm, preds, ceils, _subst, _unsupported) -> do
44-
putStrLn "Pretty-printing pattern: "
45-
putStrLn $ renderDefault $ pretty' @'[Decoded] trm
46-
putStrLn "Bool predicates: "
47-
mapM_ (putStrLn . renderDefault . pretty' @'[Decoded]) preds
48-
putStrLn "Ceil predicates: "
49-
mapM_ (putStrLn . renderDefault . pretty' @'[Decoded]) ceils
50+
Right (trm, preds, ceils, subst, unsupported) -> do
51+
mapM_ Text.putStrLn $
52+
["Pretty-printing pattern:", renderText $ pretty' @'[Decoded] trm]
53+
<> renderThings "Bool predicates:" preds
54+
<> renderThings "Ceil predicates:" ceils
55+
<> ["Substitution:", showSubst subst]
56+
unless (null unsupported) $ do
57+
putStrLn $ "...as well as " <> show (length unsupported) <> " unsupported parts:"
58+
mapM_ print unsupported
5059
Left (NoTermFound _) ->
5160
case runExcept $ internalisePredicates DisallowAlias CheckSubsorts Nothing internalDef [term] of
5261
Left es -> error (show es)
5362
Right ts -> do
54-
putStrLn "Pretty-printing predicates: "
55-
putStrLn "Bool predicates: "
56-
mapM_ (putStrLn . renderDefault . pretty' @'[Decoded]) ts.boolPredicates
57-
putStrLn "Ceil predicates: "
58-
mapM_ (putStrLn . renderDefault . pretty' @'[Decoded]) ts.ceilPredicates
59-
putStrLn "Substitution: "
60-
mapM_ (putStrLn . renderDefault . pretty' @'[Decoded]) ts.substitution
61-
putStrLn "Unsupported predicates: "
62-
mapM_ print ts.unsupported
63+
mapM_ Text.putStrLn $
64+
"Pretty-printing predicates:"
65+
: renderThings "Bool predicates:" ts.boolPredicates
66+
<> renderThings "Ceil predicates:" ts.ceilPredicates
67+
<> ["Substitution:", showSubst ts.substitution]
68+
unless (null ts.unsupported) $ do
69+
putStrLn $ "...as well as " <> show (length ts.unsupported) <> " unsupported parts:"
70+
mapM_ print ts.unsupported
6371
Left err -> error (show err)
72+
where
73+
showSubst :: Map Variable Term -> Text
74+
showSubst m =
75+
renderText $
76+
vsep
77+
[ pretty' @'[Decoded] v <+> "->" <+> pretty' @'[Decoded] expr
78+
| (v, expr) <- Map.assocs m
79+
]
80+
81+
renderThings :: Pretty (PrettyWithModifiers '[Decoded] a) => Text -> [a] -> [Text]
82+
renderThings heading [] = [heading <> " -"]
83+
renderThings heading things = heading : map (renderText . pretty' @'[Decoded]) things

0 commit comments

Comments
 (0)