-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathJsonRpc.hs
664 lines (637 loc) · 34.8 KB
/
JsonRpc.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RankNTypes #-}
{- |
Module : Booster.JsonRpc
Copyright : (c) Runtime Verification, 2022
License : BSD-3-Clause
-}
module Booster.JsonRpc (
ServerState (..),
respond,
handleSmtError,
RpcTypes.rpcJsonConfig,
execStateToKoreJson,
toExecState,
) where
import Control.Applicative ((<|>))
import Control.Concurrent (MVar, putMVar, readMVar, takeMVar)
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.Trans.Except (catchE, except, runExcept, runExceptT, throwE, withExceptT)
import Crypto.Hash (SHA256 (..), hashWith)
import Data.Bifunctor (second)
import Data.Foldable
import Data.List (singleton)
import Data.Map.Strict (Map)
import Data.Map.Strict qualified as Map
import Data.Maybe (catMaybes, fromMaybe, isJust, mapMaybe)
import Data.Proxy qualified
import Data.Sequence (Seq)
import Data.Set qualified as Set
import Data.Text (Text, pack)
import Data.Text qualified as Text
import Data.Text.Encoding qualified as Text
import GHC.Records
import Numeric.Natural
import Booster.CLOptions (RewriteOptions (..))
import Booster.Definition.Attributes.Base (UniqueId, getUniqueId, uniqueId)
import Booster.Definition.Base (KoreDefinition (..))
import Booster.Definition.Base qualified as Definition (RewriteRule (..))
import Booster.LLVM as LLVM (API)
import Booster.Log
import Booster.Pattern.ApplyEquations qualified as ApplyEquations
import Booster.Pattern.Base (Pattern (..), Sort (SortApp), Term, Variable)
import Booster.Pattern.Base qualified as Pattern
import Booster.Pattern.Implies (runImplies)
import Booster.Pattern.Pretty
import Booster.Pattern.Rewrite (
RewriteConfig (..),
RewriteFailed (..),
RewriteResult (..),
RewriteTrace (..),
performRewrite,
)
import Booster.Pattern.Util (
freeVariables,
sortOfPattern,
sortOfTerm,
substituteInPredicate,
substituteInTerm,
)
import Booster.Prettyprinter (renderText)
import Booster.SMT.Interface qualified as SMT
import Booster.Syntax.Json (KoreJson (..), addHeader, prettyPattern, sortOfJson)
import Booster.Syntax.Json.Externalise
import Booster.Syntax.Json.Internalise (
InternalisedPredicates (..),
TermOrPredicates (..),
internalisePattern,
internaliseTermOrPredicate,
logPatternError,
patternErrorToRpcError,
pattern CheckSubsorts,
pattern DisallowAlias,
)
import Booster.Syntax.ParsedKore (parseKoreModule)
import Booster.Syntax.ParsedKore.Base hiding (ParsedModule)
import Booster.Syntax.ParsedKore.Base qualified as ParsedModule (ParsedModule (..))
import Booster.Syntax.ParsedKore.Internalise (
addToDefinitions,
definitionErrorToRpcError,
)
import Booster.Util (Flag (..))
import Kore.JsonRpc.Error qualified as RpcError
import Kore.JsonRpc.Server (ErrorObj (..), JsonRpcHandler (..), Respond)
import Kore.JsonRpc.Types qualified as RpcTypes
import Kore.JsonRpc.Types.Log
import Kore.Syntax.Json.Types (Id (..))
import Kore.Syntax.Json.Types qualified as KoreJson
import Kore.Syntax.Json.Types qualified as Syntax
respond ::
forall m.
LoggerMIO m =>
MVar ServerState ->
Respond (RpcTypes.API 'RpcTypes.Req) m (RpcTypes.API 'RpcTypes.Res)
respond stateVar request =
getPrettyModifiers >>= \case
ModifiersRep (_ :: FromModifiersT mods => Data.Proxy.Proxy mods) -> case request of
RpcTypes.Execute req
| isJust req.stepTimeout -> pure $ Left $ RpcError.unsupportedOption ("step-timeout" :: String)
| isJust req.movingAverageStepTimeout ->
pure $ Left $ RpcError.unsupportedOption ("moving-average-step-timeout" :: String)
RpcTypes.Execute req -> withModule req._module $ \(def, mLlvmLibrary, mSMTOptions, rewriteOpts) -> Booster.Log.withContext CtxExecute $ do
-- internalise given constrained term
let internalised = runExcept $ internalisePattern DisallowAlias CheckSubsorts Nothing def req.state.term
case internalised of
Left patternError -> do
void $ Booster.Log.withContext CtxInternalise $ logPatternError patternError
pure $
Left $
RpcError.backendError $
RpcError.CouldNotVerifyPattern
[ patternErrorToRpcError patternError
]
Right (term, preds, ceils, substitution, unsupported) -> do
unless (null unsupported) $ do
withKorePatternContext (KoreJson.KJAnd (externaliseSort $ sortOfTerm term) unsupported) $
logMessage ("ignoring unsupported predicate parts" :: Text)
let cutPoints = fromMaybe [] req.cutPointRules
terminals = fromMaybe [] req.terminalRules
mbDepth = fmap RpcTypes.getNat req.maxDepth
doTracing =
Flag $
any
(fromMaybe False)
[ req.logSuccessfulRewrites
, req.logFailedRewrites
]
-- apply the given substitution before doing anything else
let substPat =
Pattern
{ term = substituteInTerm substitution term
, constraints = Set.fromList $ map (substituteInPredicate substitution) preds
, ceilConditions = ceils
}
-- remember all variables used in the substitutions
substVars =
Set.unions
[ Set.singleton v <> freeVariables e
| (v, e) <- Map.assocs substitution
]
solver <- maybe (SMT.noSolver) (SMT.initSolver def) mSMTOptions
logger <- getLogger
prettyModifiers <- getPrettyModifiers
let rewriteConfig =
RewriteConfig
{ definition = def
, llvmApi = mLlvmLibrary
, smtSolver = solver
, varsToAvoid = substVars
, doTracing
, logger
, prettyModifiers
, mbMaxDepth = mbDepth
, mbSimplify = rewriteOpts.interimSimplification
, cutLabels = cutPoints
, terminalLabels = terminals
}
result <-
performRewrite rewriteConfig substPat
SMT.finaliseSolver solver
pure $ execResponse req result substitution unsupported
RpcTypes.AddModule RpcTypes.AddModuleRequest{_module, nameAsId = nameAsId'} -> Booster.Log.withContext CtxAddModule $ runExceptT $ do
-- block other request executions while modifying the server state
state <- liftIO $ takeMVar stateVar
let nameAsId = fromMaybe False nameAsId'
moduleHash = Text.pack $ ('m' :) . show . hashWith SHA256 $ Text.encodeUtf8 _module
restoreStateAndRethrow err = do
liftIO (putMVar stateVar state)
throwE $ RpcError.backendError err
listNames :: (HasField "name" a b, HasField "getId" b Text) => [a] -> Text
listNames = Text.intercalate ", " . map (.name.getId)
flip catchE restoreStateAndRethrow $ do
newModule <-
withExceptT (RpcError.InvalidModule . RpcError.ErrorOnly . pack) $
except $
parseKoreModule "rpc-request" _module
unless (null newModule.sorts) $
throwE $
RpcError.InvalidModule . RpcError.ErrorOnly $
"Module introduces new sorts: " <> listNames newModule.sorts
unless (null newModule.symbols) $
throwE $
RpcError.InvalidModule . RpcError.ErrorOnly $
"Module introduces new symbols: " <> listNames newModule.symbols
-- check if we already received a module with this name
when nameAsId $
case Map.lookup (getId newModule.name) state.addedModules of
-- if a different module was already added, throw error
Just m | _module /= m -> throwE $ RpcError.DuplicateModuleName $ getId newModule.name
_ -> pure ()
-- Check for a corner case when we send module M1 with the name "m<hash of M2>"" and name-as-id: true
-- followed by adding M2. Should not happen in practice...
case Map.lookup moduleHash state.addedModules of
Just m | _module /= m -> throwE $ RpcError.DuplicateModuleName moduleHash
_ -> pure ()
newDefinitions <-
withExceptT (RpcError.InvalidModule . definitionErrorToRpcError) $
except $
runExcept $
addToDefinitions newModule{ParsedModule.name = Id moduleHash} state.definitions
liftIO $
putMVar
stateVar
state
{ definitions =
if nameAsId
then Map.insert (getId newModule.name) (newDefinitions Map.! moduleHash) newDefinitions
else newDefinitions
, addedModules =
(if nameAsId then Map.insert (getId newModule.name) _module else id) $
Map.insert moduleHash _module state.addedModules
}
Booster.Log.logMessage $
"Added a new module. Now in scope: " <> Text.intercalate ", " (Map.keys newDefinitions)
pure $ RpcTypes.AddModule $ RpcTypes.AddModuleResult moduleHash
RpcTypes.Simplify req -> withModule req._module $ \(def, mLlvmLibrary, mSMTOptions, _) -> Booster.Log.withContext CtxSimplify $ do
let internalised =
runExcept $ internaliseTermOrPredicate DisallowAlias CheckSubsorts Nothing def req.state.term
solver <- maybe (SMT.noSolver) (SMT.initSolver def) mSMTOptions
result <- case internalised of
Left patternErrors -> do
forM_ patternErrors $ \patternError ->
void $ Booster.Log.withContext CtxInternalise $ logPatternError patternError
pure $
Left $
RpcError.backendError $
RpcError.CouldNotVerifyPattern $
map patternErrorToRpcError patternErrors
-- term and predicate (pattern)
Right (TermAndPredicates pat substitution unsupported) -> do
unless (null unsupported) $ do
withKorePatternContext (KoreJson.KJAnd (externaliseSort $ sortOfPattern pat) unsupported) $ do
logMessage ("ignoring unsupported predicate parts" :: Text)
-- apply the given substitution before doing anything else
let substPat =
Pattern
{ term = substituteInTerm substitution pat.term
, constraints = Set.map (substituteInPredicate substitution) pat.constraints
, ceilConditions = pat.ceilConditions
}
ApplyEquations.evaluatePattern def mLlvmLibrary solver mempty substPat >>= \case
(Right newPattern, _) -> do
let (term, mbPredicate, mbSubstitution) = externalisePattern newPattern substitution
tSort = externaliseSort (sortOfPattern newPattern)
result = case catMaybes (mbPredicate : mbSubstitution : map Just unsupported) of
[] -> term
ps -> KoreJson.KJAnd tSort $ term : ps
pure $ Right (addHeader result)
(Left ApplyEquations.SideConditionFalse{}, _) -> do
let tSort = externaliseSort $ sortOfPattern pat
pure $ Right (addHeader $ KoreJson.KJBottom tSort)
(Left (ApplyEquations.EquationLoop _terms), _) ->
pure . Left . RpcError.backendError $ RpcError.Aborted "equation loop detected"
(Left other, _) ->
pure . Left . RpcError.backendError $ RpcError.Aborted (Text.pack . show $ other)
-- predicate only
Right (Predicates ps)
| null ps.boolPredicates && null ps.ceilPredicates && null ps.substitution && null ps.unsupported ->
pure $
Right
(addHeader $ Syntax.KJTop (fromMaybe (error "not a predicate") $ sortOfJson req.state.term))
| otherwise -> do
unless (null ps.unsupported) $ do
withKorePatternContext (KoreJson.KJAnd (externaliseSort $ SortApp "SortBool" []) ps.unsupported) $ do
logMessage ("ignoring unsupported predicate parts" :: Text)
-- apply the given substitution before doing anything else
let predicates = map (substituteInPredicate ps.substitution) ps.boolPredicates
withContext CtxConstraint $
ApplyEquations.simplifyConstraints
def
mLlvmLibrary
solver
mempty
predicates
>>= \case
(Right newPreds, _) -> do
let predicateSort =
fromMaybe (error "not a predicate") $
sortOfJson req.state.term
result =
map (externalisePredicate predicateSort) newPreds
<> map (externaliseCeil predicateSort) ps.ceilPredicates
<> map (uncurry $ externaliseSubstitution predicateSort) (Map.toList ps.substitution)
<> ps.unsupported
pure $ Right (addHeader $ Syntax.KJAnd predicateSort result)
(Left something, _) ->
pure . Left . RpcError.backendError $ RpcError.Aborted $ renderText $ pretty' @mods something
SMT.finaliseSolver solver
let mkSimplifyResponse state =
RpcTypes.Simplify
RpcTypes.SimplifyResult{state, logs = Nothing}
pure $ second mkSimplifyResponse result
RpcTypes.GetModel req -> withModule req._module $ \case
(_, _, Nothing, _) -> do
withContext CtxGetModel $
logMessage' ("get-model request, not supported without SMT solver" :: Text)
pure $ Left RpcError.notImplemented
(def, _, Just smtOptions, _) -> do
let internalised =
runExcept $
internaliseTermOrPredicate DisallowAlias CheckSubsorts Nothing def req.state.term
case internalised of
Left patternErrors -> do
forM_ patternErrors $ \patternError ->
void $ Booster.Log.withContext CtxInternalise $ logPatternError patternError
pure $
Left $
RpcError.backendError $
RpcError.CouldNotVerifyPattern $
map patternErrorToRpcError patternErrors
-- various predicates obtained
Right things -> do
-- term and predicates were sent. Only work on predicates
(boolPs, suppliedSubst) <-
case things of
TermAndPredicates pat substitution unsupported -> do
withContext CtxGetModel $
logMessage' ("ignoring supplied terms and only checking predicates" :: Text)
unless (null unsupported) $ do
withContext CtxGetModel $ do
logMessage' ("ignoring unsupported predicates" :: Text)
withContext CtxDetail $
logMessage (Text.unwords $ map prettyPattern unsupported)
pure (Set.toList pat.constraints, substitution)
Predicates ps -> do
unless (null ps.ceilPredicates && null ps.unsupported) $ do
withContext CtxGetModel $ do
logMessage' ("ignoring supplied ceils and unsupported predicates" :: Text)
withContext CtxDetail $
logMessage
( Text.unlines $
map
(renderText . ("#Ceil:" <>) . pretty' @mods)
ps.ceilPredicates
<> map prettyPattern ps.unsupported
)
pure (ps.boolPredicates, ps.substitution)
smtResult <-
if null boolPs && Map.null suppliedSubst
then do
-- as per spec, no predicate, no answer
withContext CtxGetModel $
withContext CtxSMT $
logMessage ("No predicates or substitutions given, returning Unknown" :: Text)
pure $ SMT.IsUnknown "No predicates or substitutions given"
else do
solver <- SMT.initSolver def smtOptions
result <- SMT.getModelFor solver boolPs suppliedSubst
SMT.finaliseSolver solver
pure result
withContext CtxGetModel $ withContext CtxSMT $ case smtResult of
SMT.IsSat subst -> do
logMessage $
"SMT result: " <> pack ((("Subst: " <>) . show . Map.size) subst)
let sort = fromMaybe (error "Unknown sort in input") $ sortOfJson req.state.term
substitution
| Map.null subst = Nothing
| [(var, term)] <- Map.assocs subst =
Just . addHeader $
KoreJson.KJEquals
(externaliseSort var.variableSort)
sort
(externaliseTerm $ Pattern.Var var)
(externaliseTerm term)
| otherwise =
Just . addHeader $
KoreJson.KJAnd
sort
[ KoreJson.KJEquals
(externaliseSort var.variableSort)
sort
(externaliseTerm $ Pattern.Var var)
(externaliseTerm term)
| (var, term) <- Map.assocs subst
]
pure . Right . RpcTypes.GetModel $
RpcTypes.GetModelResult
{ satisfiable = RpcTypes.Sat
, substitution
}
SMT.IsUnsat -> do
logMessage ("SMT result: Unsat" :: Text)
pure . Right . RpcTypes.GetModel $
RpcTypes.GetModelResult
{ satisfiable = RpcTypes.Unsat
, substitution = Nothing
}
SMT.IsUnknown reason -> do
logMessage $ "SMT result: Unknown - " <> reason
pure . Right . RpcTypes.GetModel $
RpcTypes.GetModelResult
{ satisfiable = RpcTypes.Unknown
, substitution = Nothing
}
RpcTypes.Implies req -> withModule req._module $ \(def, mLlvmLibrary, mSMTOptions, _) -> runImplies def mLlvmLibrary mSMTOptions req.antecedent req.consequent
-- this case is only reachable if the cancel appeared as part of a batch request
RpcTypes.Cancel -> pure $ Left RpcError.cancelUnsupportedInBatchMode
where
withModule ::
Maybe Text ->
( (KoreDefinition, Maybe LLVM.API, Maybe SMT.SMTOptions, RewriteOptions) ->
m (Either ErrorObj (RpcTypes.API 'RpcTypes.Res))
) ->
m (Either ErrorObj (RpcTypes.API 'RpcTypes.Res))
withModule mbMainModule action = do
state <- liftIO $ readMVar stateVar
let mainName = fromMaybe state.defaultMain mbMainModule
case Map.lookup mainName state.definitions of
Nothing -> pure $ Left $ RpcError.backendError $ RpcError.CouldNotFindModule mainName
Just d -> action (d, state.mLlvmLibrary, state.mSMTOptions, state.rewriteOptions)
handleSmtError :: JsonRpcHandler
handleSmtError = JsonRpcHandler $ \case
SMT.GeneralSMTError err -> runtimeError "problem" err
SMT.SMTTranslationError err -> runtimeError "translation" err
where
runtimeError prefix err = do
let msg = "SMT " <> prefix <> ": " <> err
pure $ RpcError.runtimeError msg
data ServerState = ServerState
{ definitions :: Map Text KoreDefinition
-- ^ definitions for each loaded module as main module
, defaultMain :: Text
-- ^ default main module (initially from command line, could be changed later)
, mLlvmLibrary :: Maybe LLVM.API
-- ^ Read-only: optional LLVM simplification library
, mSMTOptions :: Maybe SMT.SMTOptions
-- ^ Read-only: (optional) SMT solver options
, rewriteOptions :: RewriteOptions
-- ^ Read-only: configuration related to booster rewriting
, addedModules :: Map Text Text
-- ^ map of raw modules added via add-module
}
execStateToKoreJson :: RpcTypes.ExecuteState -> KoreJson.KoreJson
execStateToKoreJson RpcTypes.ExecuteState{term = t, substitution, predicate} =
let subAndPred = catMaybes [KoreJson.term <$> substitution, KoreJson.term <$> predicate]
innerSorts = mapMaybe sortOfJson $ KoreJson.term t : subAndPred
topLevelSort = case innerSorts of
[] -> KoreJson.SortApp (KoreJson.Id "SortGeneratedTopCell") []
xs ->
if all (== head xs) (tail xs) -- we know xs is non-empty, hence `head` is safe here
then KoreJson.SortApp (head xs).name []
else KoreJson.SortApp (KoreJson.Id "SortGeneratedTopCell") []
in t
{ KoreJson.term =
if null subAndPred then t.term else KoreJson.KJAnd topLevelSort (t.term : subAndPred)
}
execResponse ::
RpcTypes.ExecuteRequest ->
(Natural, Seq (RewriteTrace ()), RewriteResult Pattern) ->
Map Variable Term ->
[Syntax.KorePattern] ->
Either ErrorObj (RpcTypes.API 'RpcTypes.Res)
execResponse req (d, traces, rr) originalSubstitution unsupported = case rr of
RewriteBranch p nexts ->
Right $
RpcTypes.Execute
RpcTypes.ExecuteResult
{ reason = RpcTypes.Branching
, depth
, logs
, state = toExecState p originalSubstitution unsupported Nothing
, nextStates =
Just $
map (\(_, muid, p') -> toExecState p' originalSubstitution unsupported (Just muid)) $
toList nexts
, rule = Nothing
, unknownPredicate = Nothing
}
RewriteStuck p ->
Right $
RpcTypes.Execute
RpcTypes.ExecuteResult
{ reason = RpcTypes.Stuck
, depth
, logs
, state = toExecState p originalSubstitution unsupported Nothing
, nextStates = Nothing
, rule = Nothing
, unknownPredicate = Nothing
}
RewriteTrivial p ->
Right $
RpcTypes.Execute
RpcTypes.ExecuteResult
{ reason = RpcTypes.Vacuous
, depth
, logs
, state = toExecState p originalSubstitution unsupported Nothing
, nextStates = Nothing
, rule = Nothing
, unknownPredicate = Nothing
}
RewriteCutPoint lbl _ p next ->
Right $
RpcTypes.Execute
RpcTypes.ExecuteResult
{ reason = RpcTypes.CutPointRule
, depth
, logs
, state = toExecState p originalSubstitution unsupported Nothing
, nextStates = Just [toExecState next originalSubstitution unsupported Nothing]
, rule = Just lbl
, unknownPredicate = Nothing
}
RewriteTerminal lbl _ p ->
Right $
RpcTypes.Execute
RpcTypes.ExecuteResult
{ reason = RpcTypes.TerminalRule
, depth
, logs
, state = toExecState p originalSubstitution unsupported Nothing
, nextStates = Nothing
, rule = Just lbl
, unknownPredicate = Nothing
}
RewriteFinished _ _ p ->
Right $
RpcTypes.Execute
RpcTypes.ExecuteResult
{ reason = RpcTypes.DepthBound
, depth
, logs
, state = toExecState p originalSubstitution unsupported Nothing
, nextStates = Nothing
, rule = Nothing
, unknownPredicate = Nothing
}
RewriteAborted failure p -> do
Right $
RpcTypes.Execute
RpcTypes.ExecuteResult
{ reason = RpcTypes.Aborted
, depth
, logs =
let abortRewriteLog =
mkLogRewriteTrace
(logSuccessfulRewrites, logFailedRewrites)
(RewriteStepFailed failure)
in logs <|> abortRewriteLog
, state = toExecState p originalSubstitution unsupported Nothing
, nextStates = Nothing
, rule = Nothing
, unknownPredicate = Nothing
}
where
logSuccessfulRewrites = fromMaybe False req.logSuccessfulRewrites
logFailedRewrites = fromMaybe False req.logFailedRewrites
depth = RpcTypes.Depth d
logs =
let traceLogs =
concat . catMaybes . toList $
fmap
( mkLogRewriteTrace
(logSuccessfulRewrites, logFailedRewrites)
)
traces
in case traceLogs of
[] -> Nothing
xs@(_ : _) -> Just xs
toExecState ::
Pattern -> Map Variable Term -> [Syntax.KorePattern] -> Maybe UniqueId -> RpcTypes.ExecuteState
toExecState pat sub unsupported muid =
RpcTypes.ExecuteState
{ term = addHeader t
, predicate = addHeader <$> addUnsupported p
, substitution = addHeader <$> s
, ruleSubstitution = Nothing
, rulePredicate = Nothing
, ruleId = getUniqueId <$> muid
}
where
(t, p, s) = externalisePattern pat sub
termSort = externaliseSort $ sortOfPattern pat
allUnsupported = Syntax.KJAnd termSort unsupported
addUnsupported
| null unsupported = id
| otherwise = maybe (Just allUnsupported) (Just . Syntax.KJAnd termSort . (: unsupported))
mkLogRewriteTrace ::
(Bool, Bool) ->
RewriteTrace () ->
Maybe [LogEntry]
mkLogRewriteTrace
(logSuccessfulRewrites, logFailedRewrites) =
\case
RewriteSingleStep _ uid _ _res
| logSuccessfulRewrites ->
Just $
singleton $
Kore.JsonRpc.Types.Log.Rewrite
{ result =
Success
{ rewrittenTerm = Nothing
, substitution = Nothing
, ruleId = getUniqueId uid
}
, origin = Booster
}
RewriteBranchingStep _ _ -> Nothing -- we may or may not want to emit a trace here in the future
RewriteStepFailed reason
| logFailedRewrites ->
Just $
singleton $
Kore.JsonRpc.Types.Log.Rewrite
{ result = case reason of
NoApplicableRules{} -> Failure{reason = "No applicable rules found", _ruleId = Nothing}
TermIndexIsNone{} -> Failure{reason = "Term index is None for term", _ruleId = Nothing}
RuleApplicationUnclear r _ _ ->
Failure
{ reason = "Uncertain about unification of rule"
, _ruleId = Just $ getUniqueId (uniqueId $ Definition.attributes r)
}
RuleConditionUnclear r _ ->
Failure
{ reason = "Uncertain about a condition in rule"
, _ruleId = Just $ getUniqueId (uniqueId $ Definition.attributes r)
}
DefinednessUnclear r _ undefReasons ->
Failure
{ reason = "Uncertain about definedness of rule because of: " <> pack (show undefReasons)
, _ruleId = Just $ getUniqueId (uniqueId $ Definition.attributes r)
}
RewriteSortError r _ _ ->
Failure
{ reason = "Sort error while unifying"
, _ruleId = Just $ getUniqueId (uniqueId $ Definition.attributes r)
}
InternalMatchError{} ->
Failure
{ reason = "Internal match error"
, _ruleId = Nothing
}
, origin = Booster
}
RewriteSimplified{} -> Just []
_ -> Nothing