Skip to content
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

Report rewrite as stuck when term index is none #464

Closed
wants to merge 2 commits into from
Closed
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
19 changes: 10 additions & 9 deletions library/Booster/Pattern/Rewrite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,23 @@ rewriteStep ::
Pattern ->
RewriteT io (RewriteFailed "Rewrite") (RewriteResult Pattern)
rewriteStep cutLabels terminalLabels pat = do
let termIdx = kCellTermIndex pat.term
when (termIdx == None) $ throw (TermIndexIsNone pat.term)
def <- getDefinition
let idxRules = fromMaybe Map.empty $ Map.lookup termIdx def.rewriteTheory
let termIdx = kCellTermIndex pat.term
idxRules = fromMaybe Map.empty $ Map.lookup termIdx def.rewriteTheory
anyRules = fromMaybe Map.empty $ Map.lookup Anything def.rewriteTheory
rules =
map snd . Map.toAscList $
if termIdx == Anything
then idxRules
else Map.unionWith (<>) idxRules anyRules

when (null rules) $ throw (NoRulesForTerm pat.term)

-- process one priority group at a time (descending priority),
-- until a result is obtained or the entire rewrite fails.
processGroups pat rules
case termIdx of
None -> pure (RewriteStuck pat)
_ -> do
when (null rules) $ throw (NoRulesForTerm pat.term)

-- process one priority group at a time (descending priority),
-- until a result is obtained or the entire rewrite fails.
processGroups pat rules
where
processGroups ::
MonadLoggerIO io =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"jsonrpc": "2.0",
"id": 1,
"result": {
"reason": "aborted",
"reason": "stuck",
"depth": 1,
"state": {
"term": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"jsonrpc": "2.0",
"id": 1,
"result": {
"reason": "aborted",
"reason": "stuck",
"depth": 1,
"state": {
"term": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"jsonrpc": "2.0",
"id": 1,
"result": {
"reason": "aborted",
"reason": "stuck",
"depth": 2,
"state": {
"term": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"jsonrpc": "2.0",
"id": 1,
"result": {
"reason": "aborted",
"reason": "stuck",
"depth": 2,
"state": {
"term": {
Expand Down
51 changes: 25 additions & 26 deletions unit-tests/Test/Booster/Pattern/Rewrite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,6 @@ errorCases =
[trm| kCell{}( kseq{}( inj{SomeSort{}, SortKItem{}}( con2{}( \dv{SomeSort{}}("thing") ) ), Thing:SortK{}) ) |]
`failsWith` NoRulesForTerm
[trm| kCell{}( kseq{}( inj{SomeSort{}, SortKItem{}}( con2{}( \dv{SomeSort{}}("thing") ) ), Thing:SortK{}) ) |]
, testCase "Index is None" $ do
let t =
[trm|
kCell{}(
kseq{}(
inj{SomeSort{}, SortKItem{}}(
\and{SomeSort{}}(
con1{}( \dv{SomeSort{}}("thing") ),
con2{}( \dv{SomeSort{}}("thing") )
)
),
Thing:SortK{}
)
)
|]
t `failsWith` TermIndexIsNone t
]
rewriteSuccess =
testCase "con1 app rewrites to f1 app" $
Expand All @@ -222,10 +206,28 @@ definednessUnclear =
[trm| kCell{}( kseq{}( inj{AnotherSort{}, SortKItem{}}( con4{}( \dv{SomeSort{}}("thing"), \dv{SomeSort{}}("thing") ) ), ConfigVar:SortK{}) ) |]
pcon4 `failsWith` DefinednessUnclear rule4 (Pattern_ pcon4) [UndefinedSymbol "f2"]
rewriteStuck =
testCase "con3 app is stuck (no rules apply)" $ do
let con3App =
[trm| kCell{}( kseq{}( inj{SomeSort{}, SortKItem{}}( con3{}( \dv{SomeSort{}}("thing"), \dv{SomeSort{}}("thing") ) ), ConfigVar:SortK{}) ) |]
getsStuck con3App
testGroup
"Stuck terms"
[ testCase "con3 app is stuck (no rules apply)" $ do
let con3App =
[trm| kCell{}( kseq{}( inj{SomeSort{}, SortKItem{}}( con3{}( \dv{SomeSort{}}("thing"), \dv{SomeSort{}}("thing") ) ), ConfigVar:SortK{}) ) |]
getsStuck con3App
, testCase "Index is None" $ do
getsStuck
[trm|
kCell{}(
kseq{}(
inj{SomeSort{}, SortKItem{}}(
\and{SomeSort{}}(
con1{}( \dv{SomeSort{}}("thing") ),
con2{}( \dv{SomeSort{}}("thing") )
)
),
Thing:SortK{}
)
)
|]
]
rulePriority =
testCase "con1 rewrites to a branch when higher priority does not apply" $
[trm| kCell{}( kseq{}( inj{SomeSort{}, SortKItem{}}( con1{}( \dv{SomeSort{}}("otherThing") ) ), ConfigVar:SortK{}) ) |]
Expand Down Expand Up @@ -318,6 +320,9 @@ canRewrite =
[trm| kCell{}( kseq{}( inj{SomeSort{}, SortKItem{}}( con3{}( \dv{SomeSort{}}("thing"), \dv{SomeSort{}}("thing") ) ), C:SortK{}) ) |]
[trm| kCell{}( kseq{}( inj{SomeSort{}, SortKItem{}}( con3{}( \dv{SomeSort{}}("thing"), \dv{SomeSort{}}("thing") ) ), C:SortK{}) ) |]
RewriteStuck
, testCase "Returns stuck when term index is None" $
getsStuck
[trm| kCell{}( kseq{}( inj{SomeSort{}, SortKItem{}}( \and{SomeSort{}}( con1{}( \dv{SomeSort{}}("thing") ), con2{}( \dv{SomeSort{}}("thing") ) ) ), C:SortK{} ) ) |]
]

abortsOnErrors :: TestTree
Expand All @@ -326,12 +331,6 @@ abortsOnErrors =
"Aborts rewrite when there is an error"
[ testCase "when there are no rules at all" $
let term = app con2 [d] in aborts (NoRulesForTerm term) term
, testCase "when the term index is None" $
let term =
[trm| kCell{}( kseq{}( inj{SomeSort{}, SortKItem{}}( \and{SomeSort{}}( con1{}( \dv{SomeSort{}}("thing") ), con2{}( \dv{SomeSort{}}("thing") ) ) ), C:SortK{} ) ) |]
in aborts
(TermIndexIsNone term)
term
]

callsError :: TestTree
Expand Down