Skip to content

Commit a65726e

Browse files
Remove MultiOr's Functor instance (#2134)
* WIP: remove Functor instance from MultiOr * WIP: remove Functor instance from MultiOr * WIP: remove Functor instance from MultiOr * Simplification.Inj: refactor awkward map of MultiOr * WIP: remove Functor instance from MultiOr * WIP: remove Functor instance from MultiOr * Simplification.Builtin: use Logic monad instead of MultiOr as a monad * WIP: remove Functor instance from MultiOr * WIP: remove Functor instance of Result * Remove Functor instance from Result * MultiOr: traverseLogic * MultiOr: reimplement fullCrossProduct in terms of MultiAnd and MultiOr * Use old fullCrossProduct in Application simplification * MultiOr: split fullCrossProduct into distributeAnd and distributeApplication * Use MultiOr.traverse * test_distributeAnd + test_distributeApplication * WIP Address review comments * Update kore/src/Kore/Step/Simplification/Implies.hs Co-authored-by: Thomas Tuegel <[email protected]> * Update kore/src/Kore/Step/Simplification/In.hs Co-authored-by: Thomas Tuegel <[email protected]> * Update kore/src/Kore/Syntax/DomainValue.hs Co-authored-by: Thomas Tuegel <[email protected]> * Update kore/src/Kore/Internal/Inj.hs Co-authored-by: Thomas Tuegel <[email protected]> * Address review comments * Address review comment Co-authored-by: Thomas Tuegel <[email protected]>
1 parent c282007 commit a65726e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+516
-266
lines changed

kore/src/Kore/Builtin/EqTerm.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import Control.Error
1818
import qualified Control.Monad as Monad
1919

2020
import qualified Kore.Builtin.Bool as Bool
21+
import qualified Kore.Internal.MultiOr as MultiOr
2122
import qualified Kore.Internal.OrPattern as OrPattern
2223
import Kore.Internal.Pattern
2324
( Pattern
@@ -72,7 +73,7 @@ unifyEqTerm unifyChildren (NotSimplifier notSimplifier) eqTerm termLike2
7273
| Just value2 <- Bool.matchBool termLike2
7374
= lift $ do
7475
solution <- unifyChildren operand1 operand2 & OrPattern.gather
75-
let solution' = fmap eraseTerm solution
76+
let solution' = MultiOr.map eraseTerm solution
7677
(if value2 then pure else notSimplifier SideCondition.top) solution'
7778
>>= Unify.scatter
7879
| otherwise = empty

kore/src/Kore/Exec.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import Control.Monad.Trans.Except
4444
import Data.Coerce
4545
( coerce
4646
)
47+
import qualified Data.Foldable as Foldable
4748
import qualified Data.List as List
4849
import qualified Data.Map.Strict as Map
4950
import Data.Text
@@ -82,7 +83,6 @@ import qualified Kore.Internal.Condition as Condition
8283
import qualified Kore.Internal.MultiAnd as MultiAnd
8384
( extractPatterns
8485
)
85-
import qualified Kore.Internal.MultiOr as MultiOr
8686
import Kore.Internal.Pattern
8787
( Pattern
8888
)
@@ -342,7 +342,7 @@ search breadthLimit verifiedModule strategy termLike searchPattern searchConfig
342342
$ Pattern.fromTermLike termLike
343343
let
344344
initialPattern =
345-
case MultiOr.extractPatterns simplifiedPatterns of
345+
case Foldable.toList simplifiedPatterns of
346346
[] -> Pattern.bottomOf (termLikeSort termLike)
347347
(config : _) -> config
348348
runStrategy' =
@@ -356,7 +356,7 @@ search breadthLimit verifiedModule strategy termLike searchPattern searchConfig
356356
(match SideCondition.topTODO (mkRewritingPattern searchPattern))
357357
executionGraph
358358
let
359-
solutions = concatMap MultiOr.extractPatterns solutionsLists
359+
solutions = concatMap Foldable.toList solutionsLists
360360
orPredicate =
361361
makeMultipleOrPredicate (Condition.toPredicate <$> solutions)
362362
return

kore/src/Kore/Internal/Inj.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ import Kore.Sort
3636
import Kore.Syntax.Application
3737
( Application (..)
3838
)
39+
import Kore.TopBottom
40+
( TopBottom (..)
41+
)
3942
import Kore.Unparser
4043
import qualified Pretty
4144

@@ -99,6 +102,10 @@ instance Synthetic (FreeVariables variable) Inj where
99102
synthetic = injChild
100103
{-# INLINE synthetic #-}
101104

105+
instance TopBottom a => TopBottom (Inj a) where
106+
isTop _ = False
107+
isBottom Inj { injChild } = isBottom injChild
108+
102109
toSymbol :: Inj a -> Symbol
103110
toSymbol inj@(Inj _ _ _ _ _) =
104111
Symbol

kore/src/Kore/Internal/MultiAnd.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Portability : portable
1313

1414
module Kore.Internal.MultiAnd
1515
( MultiAnd
16+
, top
1617
, extractPatterns
1718
, make
1819
, toPredicate
@@ -115,6 +116,9 @@ instance
115116
from = fromTermLike
116117
{-# INLINE from #-}
117118

119+
top :: MultiAnd term
120+
top = MultiAnd []
121+
118122
{-| 'AndBool' is an some sort of Bool data type used when evaluating things
119123
inside a 'MultiAnd'.
120124
-}

kore/src/Kore/Internal/MultiOr.hs

Lines changed: 76 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,62 @@ Portability : portable
1313

1414
module Kore.Internal.MultiOr
1515
( MultiOr (..)
16-
, crossProductGeneric
17-
, crossProductGenericF
18-
, extractPatterns
16+
, bottom
1917
, filterOr
2018
, flatten
21-
, flattenGeneric
22-
, fullCrossProduct
19+
, distributeAnd
20+
, distributeApplication
2321
, gather
2422
, observeAllT
23+
, observeAll
2524
, make
2625
, merge
2726
, mergeAll
2827
, singleton
2928
, map
29+
, traverse
3030
-- * Re-exports
3131
, Alternative (..)
3232
) where
3333

3434
import Prelude.Kore hiding
3535
( map
36+
, traverse
3637
)
3738

3839
import Control.DeepSeq
3940
( NFData
4041
)
42+
import qualified Control.Lens as Lens
43+
import qualified Data.Foldable as Foldable
44+
import Data.Generics.Product
45+
( field
46+
)
4147
import Data.List
4248
( foldl'
4349
)
4450
import qualified Data.Set as Set
51+
import qualified Data.Traversable as Traversable
4552
import qualified Generics.SOP as SOP
4653
import GHC.Exts
4754
( IsList
4855
)
4956
import qualified GHC.Generics as GHC
5057

5158
import Kore.Debug
59+
import Kore.Internal.MultiAnd
60+
( MultiAnd
61+
)
62+
import qualified Kore.Internal.MultiAnd as MultiAnd
63+
import Kore.Syntax.Application
64+
( Application (..)
65+
)
5266
import Kore.TopBottom
5367
( TopBottom (..)
5468
)
5569
import Logic
56-
( LogicT
70+
( Logic
71+
, LogicT
5772
, MonadLogic
5873
)
5974
import qualified Logic
@@ -73,17 +88,12 @@ patterns.
7388
-}
7489
newtype MultiOr child = MultiOr { getMultiOr :: [child] }
7590
deriving
76-
( Alternative
77-
, Applicative
78-
, Eq
91+
( Eq
7992
, Foldable
80-
, Functor
8193
, GHC.Generic
8294
, IsList
83-
, Monad
8495
, Ord
8596
, Show
86-
, Traversable
8797
)
8898

8999
instance SOP.Generic (MultiOr child)
@@ -119,6 +129,9 @@ instance (Ord child, TopBottom child) => From [child] (MultiOr child) where
119129
instance From (MultiOr child) [child] where
120130
from = getMultiOr
121131

132+
bottom :: MultiOr term
133+
bottom = MultiOr []
134+
122135
{-| 'OrBool' is an some sort of Bool data type used when evaluating things
123136
inside a 'MultiOr'.
124137
-}
@@ -172,54 +185,37 @@ singleton term
172185
| isBottom term = MultiOr []
173186
| otherwise = MultiOr [term]
174187

175-
{-| 'extractPatterns' instantiates 'getMultiOr' at 'Pattern'.
176-
177-
It returns the patterns inside an @\or@.
178-
-}
179-
extractPatterns
180-
:: MultiOr term
181-
-> [term]
182-
extractPatterns = getMultiOr
183-
184-
{-| 'fullCrossProduct' distributes all the elements in a list of or, making
185-
all possible tuples. Each of these tuples will be an element of the resulting
186-
or. This is useful when, say, distributing 'And' or 'Application' patterns
187-
over 'Or'.
188-
189-
As an example,
190-
191-
@
192-
fullCrossProduct
193-
[ make [a1, a2]
194-
, make [b1, b2]
195-
, make [c1, c2]
196-
]
197-
@
198-
199-
will produce something equivalent to
200-
201-
@
202-
makeGeneric
203-
[ [a1, b1, c1]
204-
, [a1, b1, c2]
205-
, [a1, b2, c1]
206-
, [a1, b2, c2]
207-
, [a2, b1, c1]
208-
, [a2, b1, c2]
209-
, [a2, b2, c1]
210-
, [a2, b2, c2]
211-
]
212-
@
213-
214-
-}
215-
fullCrossProduct
216-
:: [MultiOr term]
217-
-> MultiOr [term]
218-
fullCrossProduct [] = MultiOr [[]]
219-
fullCrossProduct ors =
220-
foldr (crossProductGeneric (:)) lastOrsWithLists (init ors)
188+
distributeAnd
189+
:: Ord term
190+
=> TopBottom term
191+
=> MultiAnd (MultiOr term)
192+
-> MultiOr (MultiAnd term)
193+
distributeAnd =
194+
foldr (crossProductGeneric and') (singleton MultiAnd.top)
221195
where
222-
lastOrsWithLists = fmap (: []) (last ors)
196+
and' term ma =
197+
term : MultiAnd.extractPatterns ma & MultiAnd.make
198+
199+
distributeApplication
200+
:: Ord head
201+
=> Ord term
202+
=> TopBottom term
203+
=> Application head (MultiOr term)
204+
-> MultiOr (Application head term)
205+
distributeApplication
206+
Application
207+
{ applicationSymbolOrAlias
208+
, applicationChildren
209+
}
210+
=
211+
foldr
212+
(crossProductGeneric applyTo)
213+
(singleton application)
214+
applicationChildren
215+
where
216+
applyTo term = Lens.over (field @"applicationChildren") (term :)
217+
application =
218+
Application { applicationSymbolOrAlias, applicationChildren = [] }
223219

224220
{-| 'flatten' transforms a MultiOr (MultiOr term)
225221
into a (MultiOr term) by or-ing all the inner elements.
@@ -308,18 +304,6 @@ flattenGeneric
308304
flattenGeneric (MultiOr []) = MultiOr []
309305
flattenGeneric (MultiOr ors) = foldr1 mergeGeneric ors
310306

311-
{-| The same as 'crossProductGeneric' except that it works under an
312-
applicative thing.
313-
-}
314-
crossProductGenericF
315-
:: Applicative f
316-
=> (child1 -> child2 -> f child3)
317-
-> MultiOr child1
318-
-> MultiOr child2
319-
-> f (MultiOr child3)
320-
crossProductGenericF joiner (MultiOr first) (MultiOr second) =
321-
MultiOr <$> sequenceA (joiner <$> first <*> second)
322-
323307
{-| 'crossProductGeneric' makes all pairs between the elements of two ors,
324308
then applies the given function to the result.
325309
@@ -346,12 +330,14 @@ makeGeneric
346330
347331
-}
348332
crossProductGeneric
349-
:: (child1 -> child2 -> child3)
333+
:: Ord child3
334+
=> TopBottom child3
335+
=> (child1 -> child2 -> child3)
350336
-> MultiOr child1
351337
-> MultiOr child2
352338
-> MultiOr child3
353339
crossProductGeneric joiner (MultiOr first) (MultiOr second) =
354-
MultiOr $ joiner <$> first <*> second
340+
make $ joiner <$> first <*> second
355341

356342
gather :: (Ord a, TopBottom a, MonadLogic m) => m a -> m (MultiOr a)
357343
gather act = make <$> Logic.gather act
@@ -361,10 +347,26 @@ observeAllT :: (Ord a, TopBottom a, Monad m) => LogicT m a -> m (MultiOr a)
361347
observeAllT act = make <$> Logic.observeAllT act
362348
{-# INLINE observeAllT #-}
363349

350+
observeAll :: (Ord a, TopBottom a) => Logic a -> MultiOr a
351+
observeAll = make . Logic.observeAll
352+
{-# INLINE observeAll #-}
353+
364354
map
365355
:: Ord child2
366356
=> TopBottom child2
367357
=> (child1 -> child2)
368358
-> MultiOr child1
369359
-> MultiOr child2
370-
map f = make . fmap f . extractPatterns
360+
map f = make . fmap f . Foldable.toList
361+
{-# INLINE map #-}
362+
363+
-- | Warning: 'traverse' should not be used with 'LogicT'.
364+
traverse
365+
:: Ord child2
366+
=> TopBottom child2
367+
=> Applicative f
368+
=> (child1 -> f child2)
369+
-> MultiOr child1
370+
-> f (MultiOr child2)
371+
traverse f = fmap make . Traversable.traverse f . Foldable.toList
372+
{-# INLINE traverse #-}

kore/src/Kore/Internal/OrCondition.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,4 @@ toPredicate
109109
:: InternalVariable variable
110110
=> MultiOr (Predicate variable) -> Predicate variable
111111
toPredicate multiOr =
112-
Predicate.makeMultipleOrPredicate (MultiOr.extractPatterns multiOr)
112+
Predicate.makeMultipleOrPredicate (Foldable.toList multiOr)

kore/src/Kore/Internal/OrPattern.hs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module Kore.Internal.OrPattern
2626
, MultiOr.gather
2727
, MultiOr.observeAllT
2828
, MultiOr.map
29+
, MultiOr.traverse
2930
) where
3031

3132
import Prelude.Kore
@@ -153,7 +154,7 @@ toPattern
153154
=> OrPattern variable
154155
-> Pattern variable
155156
toPattern multiOr =
156-
case MultiOr.extractPatterns multiOr of
157+
case Foldable.toList multiOr of
157158
[] -> Pattern.bottom
158159
[patt] -> patt
159160
patts -> Foldable.foldr1 mergeWithOr patts
@@ -189,7 +190,7 @@ toTermLike
189190
:: InternalVariable variable
190191
=> OrPattern variable -> TermLike variable
191192
toTermLike multiOr =
192-
case MultiOr.extractPatterns multiOr of
193+
case Foldable.toList multiOr of
193194
[] -> mkBottom_
194195
[patt] -> Pattern.toTermLike patt
195196
patts -> Foldable.foldr1 mkOr (Pattern.toTermLike <$> patts)
@@ -213,13 +214,15 @@ targetBinder Binder { binderVariable, binderChild } =
213214
targetIfEqual
214215
$ unElementVariableName . variableName $ binderVariable
215216
newChild =
216-
Pattern.mapVariables
217-
AdjSomeVariableName
218-
{ adjSomeVariableNameElement =
219-
ElementVariableName targetBoundVariables
220-
, adjSomeVariableNameSet = SetVariableName NonTarget
221-
}
222-
<$> binderChild
217+
MultiOr.map
218+
(Pattern.mapVariables
219+
AdjSomeVariableName
220+
{ adjSomeVariableNameElement =
221+
ElementVariableName targetBoundVariables
222+
, adjSomeVariableNameSet = SetVariableName NonTarget
223+
}
224+
)
225+
binderChild
223226
in Binder
224227
{ binderVariable = newVar
225228
, binderChild = newChild

0 commit comments

Comments
 (0)