Skip to content

Add options to the kore term pretty printer #3963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion booster/library/Booster/Builtin/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Data.Text.Encoding qualified as Text
import Prettyprinter (pretty)

import Booster.Pattern.Base
import Booster.Pattern.Pretty
import Booster.Pattern.Util
import Booster.Prettyprinter

Expand Down Expand Up @@ -68,5 +69,5 @@ t `shouldHaveSort` s
throwE $
Text.unlines
[ "Argument term has unexpected sort (expected " <> Text.decodeLatin1 s <> "):"
, renderText (pretty t)
, renderText (pretty (PrettyWithModifiers @['Decoded, 'Truncated] t))
]
5 changes: 3 additions & 2 deletions booster/library/Booster/Builtin/KEQUAL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Prettyprinter
import Booster.Builtin.Base
import Booster.Pattern.Base
import Booster.Pattern.Bool
import Booster.Pattern.Pretty
import Booster.Pattern.Util (isConstructorSymbol, sortOfTerm)
import Booster.Prettyprinter (renderText)

Expand All @@ -40,8 +41,8 @@ iteHook args
unless (sortOfTerm thenVal == sortOfTerm elseVal) $
throwE . Text.unlines $
[ "Different sorts in alternatives:"
, renderText $ pretty thenVal
, renderText $ pretty elseVal
, renderText $ pretty (PrettyWithModifiers @['Decoded, 'Truncated] thenVal)
, renderText $ pretty (PrettyWithModifiers @['Decoded, 'Truncated] elseVal)
]
case cond of
TrueBool -> pure $ Just thenVal
Expand Down
17 changes: 17 additions & 0 deletions booster/library/Booster/CLOptions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import Text.Read (readMaybe)

import Booster.GlobalState (EquationOptions (..))
import Booster.Log.Context (ContextFilter, ctxt, readContextFilter)
import Booster.Pattern.Pretty
import Booster.SMT.Interface (SMTOptions (..), defaultSMTOptions)
import Booster.SMT.LowLevelCodec qualified as SMT (parseSExpr)
import Booster.Trace (CustomUserEventType)
Expand All @@ -48,6 +49,7 @@ data CLOptions = CLOptions
, smtOptions :: Maybe SMTOptions
, equationOptions :: EquationOptions
, indexCells :: [Text]
, prettyPrintOptions :: [ModifierT]
, -- developer options below
eventlogEnabledUserEvents :: [CustomUserEventType]
}
Expand Down Expand Up @@ -162,6 +164,14 @@ clOptionsParser =
<> help "Names of configuration cells to index rewrite rules with (default: 'k')"
<> value []
)
<*> option
(eitherReader $ mapM (readModifierT . trim) . splitOn ",")
( metavar "PRETTY_PRINT"
<> value [Decoded, Truncated]
<> long "pretty-print"
<> help "Prety print options for kore terms: decode, infix, truncated"
<> showDefault
)
-- developer options below
<*> many
( option
Expand Down Expand Up @@ -201,6 +211,13 @@ clOptionsParser =
"json" -> Right Json
other -> Left $ other <> ": Unsupported log format"

readModifierT :: String -> Either String ModifierT
readModifierT = \case
"truncated" -> Right Truncated
"infix" -> Right Infix
"decoded" -> Right Decoded
other -> Left $ other <> ": Unsupported prettry printer option"

readTimeStampFormat :: String -> Either String TimestampFormat
readTimeStampFormat = \case
"pretty" -> Right Pretty
Expand Down
19 changes: 11 additions & 8 deletions booster/library/Booster/Definition/Ceil.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Booster.Pattern.Base
import Booster.LLVM as LLVM (API, simplifyBool)
import Booster.Log
import Booster.Pattern.Bool
import Booster.Pattern.Pretty
import Booster.Pattern.Util (isConcrete, sortOfTerm)
import Booster.Util (Flag (..))
import Control.DeepSeq (NFData)
Expand Down Expand Up @@ -43,20 +44,20 @@ data ComputeCeilSummary = ComputeCeilSummary
deriving stock (Eq, Ord, Show, GHC.Generic)
deriving anyclass (NFData)

instance Pretty ComputeCeilSummary where
pretty ComputeCeilSummary{rule, ceils} =
instance FromModifiersT mods => Pretty (PrettyWithModifiers mods ComputeCeilSummary) where
pretty (PrettyWithModifiers ComputeCeilSummary{rule, ceils}) =
Pretty.vsep $
[ "\n\n----------------------------\n"
, pretty $ sourceRef rule
, pretty rule.lhs
, pretty' @mods rule.lhs
, "=>"
, pretty rule.rhs
, pretty' @mods rule.rhs
]
<> ( if null rule.requires
then []
else
[ "requires"
, Pretty.indent 2 . Pretty.vsep $ map pretty $ Set.toList rule.requires
, Pretty.indent 2 . Pretty.vsep $ map (pretty' @mods) $ Set.toList rule.requires
]
)
<> [ Pretty.line
Expand All @@ -69,7 +70,9 @@ instance Pretty ComputeCeilSummary where
[ Pretty.line
, "computed ceils:"
, Pretty.indent 2 . Pretty.vsep $
map (either pretty (\t -> "#Ceil(" Pretty.<+> pretty t Pretty.<+> ")")) (Set.toList ceils)
map
(either (pretty' @mods) (\t -> "#Ceil(" Pretty.<+> pretty' @mods t Pretty.<+> ")"))
(Set.toList ceils)
]

computeCeilsDefinition ::
Expand Down Expand Up @@ -209,7 +212,7 @@ mkInKeys inKeysSymbols k m =
Nothing ->
error $
"in_keys for key sort '"
<> show (pretty $ sortOfTerm k)
<> show (pretty $ PrettyWithModifiers @'[Decoded, Truncated] $ sortOfTerm k)
<> "' and map sort '"
<> show (pretty $ sortOfTerm m)
<> show (pretty $ PrettyWithModifiers @'[Decoded, Truncated] $ sortOfTerm m)
<> "' does not exist."
7 changes: 5 additions & 2 deletions booster/library/Booster/Definition/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import Booster.Definition.Base
import Booster.Definition.Ceil (ComputeCeilSummary (..))
import Booster.Pattern.Base
import Booster.Pattern.Index (CellIndex (..), TermIndex (..))
import Booster.Pattern.Pretty
import Booster.Prettyprinter
import Booster.Util

Expand Down Expand Up @@ -92,7 +93,8 @@ prettySummary :: Bool -> Summary -> String
prettySummary veryVerbose summary@Summary{computeCeilsSummary} =
Pretty.renderString . layoutPrettyUnbounded $
Pretty.vcat $
pretty summary : if veryVerbose then map pretty computeCeilsSummary else []
pretty summary
: if veryVerbose then map (pretty' @['Decoded, 'Truncated] $) computeCeilsSummary else []

instance Pretty Summary where
pretty summary =
Expand Down Expand Up @@ -146,7 +148,8 @@ instance Pretty Summary where
prettyCellIndex None = "None"

prettyCeilRule :: RewriteRule r -> Doc a
prettyCeilRule RewriteRule{lhs, rhs} = "#Ceil(" <+> pretty lhs <+> ") =>" <+> pretty rhs
prettyCeilRule RewriteRule{lhs, rhs} =
"#Ceil(" <+> pretty' @['Decoded, 'Truncated] lhs <+> ") =>" <+> pretty' @['Decoded, 'Truncated] rhs

sourceRefText :: HasSourceRef a => a -> Text
sourceRefText = renderOneLineText . pretty . sourceRef
Loading
Loading