1
+ {-# LANGUAGE FlexibleContexts #-}
1
2
{-# LANGUAGE PatternSynonyms #-}
2
3
3
4
{- | Pretty printer for JSON KORE terms
@@ -9,8 +10,20 @@ module Main (
9
10
main ,
10
11
) where
11
12
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 )
12
25
import Booster.Pattern.Pretty
13
- import Booster.Prettyprinter (renderDefault )
26
+ import Booster.Prettyprinter (renderDefault , renderText )
14
27
import Booster.Syntax.Json (KoreJson (.. ))
15
28
import Booster.Syntax.Json.Internalise (
16
29
InternalisedPredicates (.. ),
@@ -21,12 +34,6 @@ import Booster.Syntax.Json.Internalise (
21
34
pattern DisallowAlias ,
22
35
)
23
36
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 )
30
37
31
38
main :: IO ()
32
39
main = do
@@ -40,24 +47,37 @@ main = do
40
47
Left err -> putStrLn $ " Error: " ++ err
41
48
Right KoreJson {term} -> do
42
49
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
50
59
Left (NoTermFound _) ->
51
60
case runExcept $ internalisePredicates DisallowAlias CheckSubsorts Nothing internalDef [term] of
52
61
Left es -> error (show es)
53
62
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
63
71
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