Skip to content

Commit 0144916

Browse files
committed
De-duplicate showHashHex
1 parent b571a00 commit 0144916

File tree

6 files changed

+37
-17
lines changed

6 files changed

+37
-17
lines changed

booster/library/Booster/Log.hs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Booster.Pattern.Base (
1414
TermAttributes (hash),
1515
pattern AndTerm,
1616
)
17+
import Kore.Util (showHashHex)
1718
import Booster.Prettyprinter (renderOneLineText)
1819
import Booster.Syntax.Json (KorePattern, prettyPattern)
1920
import Control.Monad.IO.Class
@@ -39,12 +40,9 @@ import Data.List.Extra (splitOn, takeEnd)
3940
import Data.Set qualified as Set
4041
import Data.String (IsString)
4142
import Data.Text (Text, pack)
42-
import Data.Text qualified as Text
4343
import Data.Text.Lazy qualified as LazyText
44-
import Data.Word (Word64)
4544
import GHC.Exts (IsString (..))
4645
import GHC.TypeLits (KnownSymbol, symbolVal)
47-
import Numeric (showHex)
4846
import Prettyprinter (Pretty, pretty)
4947

5048
newtype Logger a = Logger (a -> IO ())
@@ -97,9 +95,6 @@ withContext c = withLogger (\(Logger l) -> Logger $ l . (\(LogMessage ctxt m) ->
9795

9896
newtype TermCtxt = TermCtxt Int
9997

100-
showHashHex :: Int -> Text
101-
showHashHex h = let w64 :: Word64 = fromIntegral h in Text.take 7 $ pack $ showHex w64 ""
102-
10398
instance ToLogFormat TermCtxt where
10499
toTextualLog (TermCtxt hsh) = "term " <> (showHashHex hsh)
105100

booster/library/Booster/Pattern/ApplyEquations.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ import Booster.Prettyprinter (renderDefault, renderOneLineText)
8484
import Booster.SMT.Interface qualified as SMT
8585
import Booster.Util (Bound (..), Flag (..))
8686
import Kore.JsonRpc.Types.Log qualified as KoreRpcLog
87+
import Kore.Util (showHashHex)
8788

8889
newtype EquationT io a
8990
= EquationT (ReaderT EquationConfig (ExceptT EquationFailure (StateT EquationState io)) a)

kore-rpc-types/kore-rpc-types.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ common haskell
3737
OverloadedStrings
3838
StandaloneDeriving
3939
ScopedTypeVariables
40+
TypeApplications
4041
TypeFamilies
4142
default-language: Haskell2010
4243
build-depends: base >=4.7
@@ -82,6 +83,7 @@ library
8283
import: haskell
8384
import: library
8485
exposed-modules:
86+
Kore.Util
8587
Kore.JsonRpc.Error
8688
Kore.JsonRpc.Types
8789
Kore.JsonRpc.Types.Log

kore-rpc-types/src/Kore/Util.hs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{- |
2+
Copyright : (c) Runtime Verification, 2019-2024
3+
License : BSD-3-Clause
4+
-}
5+
module Kore.Util (
6+
showHashHex,
7+
extractLogMessageContext,
8+
) where
9+
10+
import Data.Char
11+
import Data.Text (Text)
12+
import Data.Text qualified as Text
13+
import Data.Word (Word64)
14+
import Numeric (showHex)
15+
16+
-- | Represent aa 'Int' as a short hexadecimal string
17+
showHashHex :: Int -> Text
18+
showHashHex h = let cutoff = 7 in Text.take cutoff . Text.pack $ showHex (fromIntegral @Int @Word64 h) ""
19+
20+
-- | From a Kore/Booster contextual one-line log message, extract the context prefix
21+
extractLogMessageContext :: Text -> Text
22+
extractLogMessageContext = Text.takeWhile isContextCharacter
23+
where
24+
isContextCharacter :: Char -> Bool
25+
isContextCharacter c = isHexDigit c || isLower c || isSpace c || isBracket c
26+
27+
isBracket = \case
28+
'[' -> True
29+
']' -> True
30+
_ -> False

kore/src/Kore/Equation/DebugEquation.hs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import Data.Text (
3333
Text,
3434
)
3535
import Data.Text qualified as Text
36-
import Data.Word (Word64)
3736
import Debug
3837
import GHC.Generics qualified as GHC
3938
import Generics.SOP qualified as SOP
@@ -61,8 +60,8 @@ import Kore.Rewrite.RewritingVariable (
6160
RewritingVariableName,
6261
)
6362
import Kore.Unparser (Unparse (..))
63+
import Kore.Util (showHashHex)
6464
import Log
65-
import Numeric (showHex)
6665
import Prelude.Kore
6766
import Pretty (Pretty (..))
6867
import Pretty qualified
@@ -268,10 +267,7 @@ failureDescription err = shorten . Pretty.renderText . Pretty.layoutOneLine . Pr
268267
shorten :: Text -> Text
269268
shorten msg =
270269
let cutoff = 500
271-
in if Text.length msg > cutoff then Text.take 500 msg <> ("...truncated" :: Text) else msg
272-
273-
showHashHex :: Int -> Text
274-
showHashHex h = let w64 :: Word64 = fromIntegral h in Text.take 7 $ Text.pack $ showHex w64 ""
270+
in if Text.length msg > cutoff then Text.take cutoff msg <> ("...truncated" :: Text) else msg
275271

276272
ruleIdText :: Equation a -> Text
277273
ruleIdText equation = fromMaybe "UNKNOWN" (Attribute.getUniqueId . Attribute.uniqueId . attributes $ equation)

kore/src/Kore/Log/DebugAttemptedRewriteRules.hs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ module Kore.Log.DebugAttemptedRewriteRules (
1414

1515
import Data.Text (Text)
1616
import Data.Text qualified as Text
17-
import Data.Word (Word64)
1817
import Kore.Attribute.Axiom (
1918
SourceLocation,
2019
UniqueId (..),
@@ -31,7 +30,7 @@ import Kore.Internal.Variable (
3130
import Kore.Rewrite.RewritingVariable
3231
import Kore.Unparser
3332
import Log
34-
import Numeric (showHex)
33+
import Kore.Util (showHashHex)
3534
import Prelude.Kore
3635
import Pretty (
3736
Pretty (..),
@@ -59,9 +58,6 @@ instance Pretty DebugAttemptedRewriteRules where
5958
, Pretty.indent 2 . unparse $ configuration
6059
]
6160

62-
showHashHex :: Int -> Text
63-
showHashHex h = let w64 :: Word64 = fromIntegral h in Text.take 7 $ Text.pack $ showHex w64 ""
64-
6561
shortenRuleId :: Text -> Text
6662
shortenRuleId msg = Text.take 8 msg
6763

0 commit comments

Comments
 (0)