1
+ {-# LANGUAGE FlexibleContexts #-}
1
2
{-# LANGUAGE PatternSynonyms #-}
2
3
3
4
{- | Pretty printer for JSON KORE terms
@@ -9,8 +10,19 @@ 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.IO qualified as Text
20
+ import Prettyprinter
21
+ import System.Environment (getArgs )
22
+
23
+ import Booster.Pattern.Base (Term , Variable )
12
24
import Booster.Pattern.Pretty
13
- import Booster.Prettyprinter (renderDefault )
25
+ import Booster.Prettyprinter (renderDefault , renderText )
14
26
import Booster.Syntax.Json (KoreJson (.. ))
15
27
import Booster.Syntax.Json.Internalise (
16
28
InternalisedPredicates (.. ),
@@ -21,12 +33,6 @@ import Booster.Syntax.Json.Internalise (
21
33
pattern DisallowAlias ,
22
34
)
23
35
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
36
31
37
main :: IO ()
32
38
main = do
@@ -40,24 +46,37 @@ main = do
40
46
Left err -> putStrLn $ " Error: " ++ err
41
47
Right KoreJson {term} -> do
42
48
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
49
+ Right (trm, preds, ceils, subst, unsupported) -> do
50
+ Text. putStrLn . renderText . vsep $
51
+ [ " Pretty-printing pattern:"
52
+ , pretty' @ '[Decoded , Infix ] trm
53
+ , renderThings " Bool predicates:" preds
54
+ , renderThings " Ceil predicates:" ceils
55
+ , hang 2 $ " Substitution:" <> line <> showSubst subst
56
+ ]
57
+ unless (null unsupported) $ do
58
+ putStrLn $ " ...as well as " <> show (length unsupported) <> " unsupported parts:"
59
+ mapM_ print unsupported
50
60
Left (NoTermFound _) ->
51
61
case runExcept $ internalisePredicates DisallowAlias CheckSubsorts Nothing internalDef [term] of
52
62
Left es -> error (show es)
53
63
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
64
+ Text. putStrLn . renderText . vsep $
65
+ [ " Pretty-printing predicates:"
66
+ , renderThings " Bool predicates: " ts. boolPredicates
67
+ , renderThings " Ceil predicates:" ts . ceilPredicates
68
+ , hang 2 $ " Substitution: " <> line <> showSubst ts. substitution
69
+ ]
70
+ unless ( null ts . unsupported) $ do
71
+ putStrLn $ " ...as well as " <> show ( length ts . unsupported) <> " unsupported parts: "
72
+ mapM_ print ts. unsupported
63
73
Left err -> error (show err)
74
+ where
75
+ showSubst :: Map Variable Term -> Doc ann
76
+ showSubst m =
77
+ vsep
78
+ [pretty' @ '[Decoded ] v <+> " ->" <+> pretty' @ '[Decoded , Infix ] expr | (v, expr) <- Map. assocs m]
79
+
80
+ renderThings :: Pretty (PrettyWithModifiers '[Decoded , Infix ] a ) => Doc ann -> [a ] -> Doc ann
81
+ renderThings heading [] = heading <> " -"
82
+ renderThings heading things = hang 2 $ vsep $ heading : map (pretty' @ '[Decoded , Infix ]) things
0 commit comments