Skip to content

Commit 3f55037

Browse files
authored
Simplification attribute: parse and use optional priority argument (#1933)
1 parent 8424ed0 commit 3f55037

File tree

9 files changed

+160
-49
lines changed

9 files changed

+160
-49
lines changed

kore/src/Kore/Attribute/Axiom.hs

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module Kore.Attribute.Axiom
2828
, Constructor (..)
2929
, RuleIndex (..)
3030
, UniqueId (..)
31+
, PriorityAttributes (..)
3132
, axiomSymbolToSymbolOrAlias
3233
, mapAxiomVariables
3334
, parseAxiomAttributes
@@ -207,8 +208,13 @@ instance
207208
, from . owise
208209
]
209210

210-
instance From (Axiom symbol variable) (Priority, Owise) where
211-
from Axiom { priority, owise } = (priority, owise)
211+
instance From (Axiom symbol variable) PriorityAttributes where
212+
from Axiom { priority, owise, simplification } =
213+
PriorityAttributes
214+
{ priorityAttr = priority
215+
, owiseAttr = owise
216+
, simplificationAttr = simplification
217+
}
212218

213219
instance From (Axiom symbol variable) HeatCool where
214220
from Axiom { heatCool } = heatCool
@@ -280,10 +286,49 @@ mapAxiomVariables adj axiom@Axiom { concrete, symbolic } =
280286
, symbolic = mapSymbolicVariables adj symbolic
281287
}
282288

289+
data PriorityAttributes =
290+
PriorityAttributes
291+
{ priorityAttr :: !Priority
292+
, owiseAttr :: !Owise
293+
, simplificationAttr :: !Simplification
294+
}
295+
283296
getPriorityOfAxiom
284-
:: forall attrs. From attrs (Priority, Owise) => attrs -> Integer
285-
getPriorityOfAxiom attrs
286-
| isOwise = owisePriority
287-
| otherwise = fromMaybe defaultPriority getPriority
288-
where
289-
(Priority { getPriority }, Owise { isOwise }) = from @attrs attrs
297+
:: forall attrs
298+
. HasCallStack
299+
=> From attrs PriorityAttributes
300+
=> attrs
301+
-> Integer
302+
getPriorityOfAxiom
303+
(from @attrs ->
304+
PriorityAttributes
305+
{ priorityAttr
306+
, owiseAttr
307+
, simplificationAttr
308+
}
309+
)
310+
=
311+
case (priorityAttr, owiseAttr, simplificationAttr) of
312+
(Priority Nothing, Owise True, NotSimplification) ->
313+
owisePriority
314+
(Priority Nothing, Owise False, NotSimplification) ->
315+
defaultPriority
316+
(Priority (Just value), Owise False, NotSimplification) ->
317+
value
318+
(Priority Nothing, Owise False, IsSimplification Nothing) ->
319+
defaultSimplificationPriority
320+
(Priority Nothing, Owise False, IsSimplification (Just value)) ->
321+
value
322+
-- TODO: remove this case once the frontend
323+
-- modifies the simplification attribute
324+
-- to take an optional priority value
325+
(Priority (Just value), Owise False, IsSimplification Nothing) ->
326+
value
327+
errorCase@(_, _, _) ->
328+
error
329+
("An axiom cannot have the following \
330+
\ combination of attributes: "
331+
<> show errorCase
332+
<> " Please report this error."
333+
)
334+

kore/src/Kore/Attribute/Parser.hs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module Kore.Attribute.Parser
4040
, getTwoParams
4141
, getZeroArguments
4242
, getOneArgument
43+
, getZeroOrOneArguments
4344
, getTwoArguments
4445
, getSymbolOrAlias
4546
, Kore.Attribute.Parser.getStringLiteral
@@ -278,6 +279,19 @@ getOneArgument =
278279
where
279280
arity = length args
280281

282+
getZeroOrOneArguments
283+
:: [AttributePattern]
284+
-> Parser (Maybe AttributePattern)
285+
getZeroOrOneArguments =
286+
\case
287+
[] -> return Nothing
288+
[arg] -> return (Just arg)
289+
args ->
290+
Kore.Error.koreFail
291+
("expected at most one argument, found " <> show arity)
292+
where
293+
arity = length args
294+
281295
{- | Accept exactly two arguments.
282296
-}
283297
getTwoArguments
@@ -317,7 +331,7 @@ getStringLiteral kore =
317331
_ :< StringLiteralF (Const lit) -> return lit
318332
_ -> Kore.Error.koreFail "expected string literal pattern"
319333

320-
{- | Accept a string literal.
334+
{- | Accept a variable.
321335
-}
322336
getVariable :: AttributePattern -> Parser (SomeVariable VariableName)
323337
getVariable kore =

kore/src/Kore/Attribute/Simplification.hs

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,30 @@ Informal example of an axiom that would use the simplification attribute:
1919
module Kore.Attribute.Simplification
2020
( Simplification (..)
2121
, simplificationId, simplificationSymbol, simplificationAttribute
22+
, defaultSimplificationPriority
2223
) where
2324

2425
import Prelude.Kore
2526

27+
import Data.Maybe
28+
( maybeToList
29+
)
2630
import qualified Generics.SOP as SOP
2731
import qualified GHC.Generics as GHC
2832

2933
import Kore.Attribute.Parser as Parser
3034
import Kore.Debug
3135

36+
type SimplificationPriority = Maybe Integer
37+
3238
{- | @Simplification@ represents the @simplification@ attribute for axioms.
39+
It takes an optional integer argument which represents the rule's priority.
40+
This allows the possibility of ordering the application of simplification rules.
3341
-}
34-
newtype Simplification = Simplification { isSimplification :: Bool }
35-
deriving (Eq, GHC.Generic, Ord, Show)
42+
data Simplification
43+
= IsSimplification !SimplificationPriority
44+
| NotSimplification
45+
deriving (Eq, Ord, Show, GHC.Generic)
3646

3747
instance SOP.Generic Simplification
3848

@@ -45,7 +55,7 @@ instance Diff Simplification
4555
instance NFData Simplification
4656

4757
instance Default Simplification where
48-
def = Simplification False
58+
def = NotSimplification
4959

5060
-- | Kore identifier representing the @simplification@ attribute symbol.
5161
simplificationId :: Id
@@ -60,11 +70,36 @@ simplificationSymbol =
6070
}
6171

6272
-- | Kore pattern representing the @simplification@ attribute.
63-
simplificationAttribute :: AttributePattern
64-
simplificationAttribute = attributePattern_ simplificationSymbol
73+
simplificationAttribute :: Maybe Integer -> AttributePattern
74+
simplificationAttribute priority =
75+
attributePattern
76+
simplificationSymbol
77+
(fmap attributeInteger (maybeToList priority))
78+
79+
defaultSimplificationPriority :: Integer
80+
defaultSimplificationPriority = 50
6581

6682
instance ParseAttributes Simplification where
67-
parseAttribute = parseBoolAttribute simplificationId
83+
parseAttribute =
84+
withApplication' parseSimplification
85+
where
86+
parseSimplification params args NotSimplification = do
87+
Parser.getZeroParams params
88+
arg <- Parser.getZeroOrOneArguments args
89+
case arg of
90+
Just arg' -> do
91+
stringLiteral <- Parser.getStringLiteral arg'
92+
integer <- Parser.parseInteger stringLiteral
93+
return (IsSimplification (Just integer))
94+
Nothing ->
95+
return (IsSimplification Nothing)
96+
parseSimplification _ _ _ =
97+
failDuplicate'
98+
99+
withApplication' = Parser.withApplication simplificationId
100+
failDuplicate' = Parser.failDuplicate simplificationId
68101

69102
instance From Simplification Attributes where
70-
from = toBoolAttributes simplificationAttribute
103+
from NotSimplification = def
104+
from (IsSimplification maybePriority) =
105+
from @AttributePattern (simplificationAttribute maybePriority)

kore/src/Kore/Equation/Equation.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,9 @@ refreshVariables
354354

355355
isSimplificationRule :: Equation variable -> Bool
356356
isSimplificationRule Equation { attributes } =
357-
isSimplification
358-
where
359-
Attribute.Simplification { isSimplification } =
360-
Attribute.simplification attributes
357+
case Attribute.simplification attributes of
358+
Attribute.IsSimplification _ -> True
359+
_ -> False
361360

362361
equationPriority :: Equation variable -> Integer
363362
equationPriority = Attribute.getPriorityOfAxiom . attributes

kore/src/Kore/Step.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ anyRewrite rewrites =
151151
Strategy.any (rewriteStep <$> rewrites)
152152

153153
priorityAllStrategy
154-
:: From rewrite (Attribute.Priority, Attribute.Owise)
154+
:: From rewrite Attribute.PriorityAttributes
155155
=> [rewrite]
156156
-> Strategy (Prim rewrite)
157157
priorityAllStrategy rewrites =
@@ -160,7 +160,7 @@ priorityAllStrategy rewrites =
160160
priorityGroups = groupSortOn Attribute.getPriorityOfAxiom rewrites
161161

162162
priorityAnyStrategy
163-
:: From rewrite (Attribute.Priority, Attribute.Owise)
163+
:: From rewrite Attribute.PriorityAttributes
164164
=> [rewrite]
165165
-> Strategy (Prim rewrite)
166166
priorityAnyStrategy rewrites =

kore/src/Kore/Step/RulePattern.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ instance TopBottom (RulePattern variable) where
248248
isTop _ = False
249249
isBottom _ = False
250250

251-
instance From (RulePattern variable) (Attribute.Priority, Attribute.Owise) where
251+
instance From (RulePattern variable) Attribute.PriorityAttributes where
252252
from = from @(Attribute.Axiom _ _) . attributes
253253

254254
instance From (RulePattern variable) Attribute.HeatCool where
@@ -542,7 +542,7 @@ instance
542542
freeVariables (RewriteRule rule) = freeVariables rule
543543
{-# INLINE freeVariables #-}
544544

545-
instance From (RewriteRule variable) (Attribute.Priority, Attribute.Owise) where
545+
instance From (RewriteRule variable) Attribute.PriorityAttributes where
546546
from = from @(RulePattern _) . getRewriteRule
547547

548548
instance From (RewriteRule variable) Attribute.HeatCool where

kore/src/Kore/Strategies/Rule.hs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,10 @@ import qualified Generics.SOP as SOP
1818
import qualified GHC.Generics as GHC
1919

2020
import Debug
21+
import qualified Kore.Attribute.Axiom as Attribute
2122
import Kore.Attribute.Label as Attribute
2223
( Label
2324
)
24-
import Kore.Attribute.Owise as Attribute
25-
( Owise
26-
)
27-
import Kore.Attribute.Priority as Attribute
28-
( Priority
29-
)
3025
import Kore.Attribute.RuleIndex as Attribute
3126
( RuleIndex
3227
)
@@ -66,7 +61,7 @@ instance Diff (Rule OnePathRule)
6661
instance ToRulePattern (Rule OnePathRule) where
6762
toRulePattern = getRewriteRule . unRewritingRule . unRuleOnePath
6863

69-
instance From (Rule OnePathRule) (Attribute.Priority, Attribute.Owise) where
64+
instance From (Rule OnePathRule) Attribute.PriorityAttributes where
7065
from = from @(RewriteRule _) . unRuleOnePath
7166

7267
-- * All-path reachability
@@ -86,7 +81,7 @@ instance Diff (Rule AllPathRule)
8681
instance ToRulePattern (Rule AllPathRule) where
8782
toRulePattern = getRewriteRule . unRewritingRule . unRuleAllPath
8883

89-
instance From (Rule AllPathRule) (Attribute.Priority, Attribute.Owise) where
84+
instance From (Rule AllPathRule) Attribute.PriorityAttributes where
9085
from = from @(RewriteRule _) . unRuleAllPath
9186

9287
-- * Reachability
@@ -107,8 +102,7 @@ instance Diff (Rule ReachabilityRule)
107102
instance ToRulePattern (Rule ReachabilityRule) where
108103
toRulePattern = getRewriteRule . unRewritingRule . unReachabilityRewriteRule
109104

110-
instance From (Rule ReachabilityRule) (Attribute.Priority, Attribute.Owise)
111-
where
105+
instance From (Rule ReachabilityRule) Attribute.PriorityAttributes where
112106
from = from @(RewriteRule _) . unReachabilityRewriteRule
113107

114108
instance From (Rule ReachabilityRule) Attribute.SourceLocation where

kore/test/Test/Kore/Attribute/Simplification.hs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
module Test.Kore.Attribute.Simplification
22
( test_simplification
3+
, test_simplification_with_argument
34
, test_Attributes
5+
, test_Attributes_with_argument
46
, test_duplicate
5-
, test_arguments
7+
, test_arguments_wrong_type
8+
, test_multiple_arguments
69
, test_parameters
710
) where
811

@@ -22,32 +25,56 @@ parseSimplification = parseAttributes
2225
test_simplification :: TestTree
2326
test_simplification =
2427
testCase "[simplification{}()] :: Simplification"
25-
$ expectSuccess Simplification { isSimplification = True }
26-
$ parseSimplification $ Attributes [ simplificationAttribute ]
28+
$ expectSuccess (IsSimplification Nothing)
29+
$ parseSimplification $ Attributes [ simplificationAttribute Nothing ]
30+
31+
test_simplification_with_argument :: TestTree
32+
test_simplification_with_argument =
33+
testCase "[simplification{}(\"5\")] :: Simplification"
34+
$ expectSuccess (IsSimplification (Just 5))
35+
$ parseSimplification $ Attributes [ simplificationAttribute (Just 5) ]
2736

2837
test_Attributes :: TestTree
2938
test_Attributes =
3039
testCase "[simplification{}()] :: Attributes"
3140
$ expectSuccess attrs $ parseAttributes attrs
3241
where
33-
attrs = Attributes [ simplificationAttribute ]
42+
attrs = Attributes [ simplificationAttribute Nothing ]
43+
44+
test_Attributes_with_argument :: TestTree
45+
test_Attributes_with_argument =
46+
testCase "[simplification{}(\"5\")] :: Attributes"
47+
$ expectSuccess attrs $ parseAttributes attrs
48+
where
49+
attrs = Attributes [ simplificationAttribute (Just 5) ]
3450

3551
test_duplicate :: TestTree
3652
test_duplicate =
3753
testCase "[simplification{}(), simplification{}()]"
3854
$ expectFailure
3955
$ parseSimplification
40-
$ Attributes [ simplificationAttribute, simplificationAttribute ]
56+
$ Attributes [ simplificationAttribute Nothing, simplificationAttribute Nothing ]
4157

42-
test_arguments :: TestTree
43-
test_arguments =
58+
test_arguments_wrong_type :: TestTree
59+
test_arguments_wrong_type =
4460
testCase "[simplification{}(\"illegal\")]"
4561
$ expectFailure
4662
$ parseSimplification $ Attributes [ illegalAttribute ]
4763
where
4864
illegalAttribute =
4965
attributePattern simplificationSymbol [attributeString "illegal"]
5066

67+
test_multiple_arguments :: TestTree
68+
test_multiple_arguments =
69+
testCase "[simplification{}(\"3\", \"26\")]"
70+
$ expectFailure
71+
$ parseSimplification $ Attributes [ illegalAttributes ]
72+
where
73+
illegalAttributes =
74+
attributePattern
75+
simplificationSymbol
76+
[ attributeInteger 3, attributeInteger 26 ]
77+
5178
test_parameters :: TestTree
5279
test_parameters =
5380
testCase "[simplification{illegal}()]"

0 commit comments

Comments
 (0)