Skip to content

Commit 8a9161b

Browse files
goodlyrottenapplegithub-actions
andauthored
Fix collections internalisation (#3841)
Fix pattern synonyms to store e.g. {...REST} as just REST --------- Co-authored-by: github-actions <[email protected]>
1 parent f2e04c2 commit 8a9161b

File tree

2 files changed

+51
-34
lines changed

2 files changed

+51
-34
lines changed

booster/library/Booster/Pattern/Base.hs

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -596,17 +596,20 @@ pattern KMap def keyVals rest <- Term _ (KMapF def keyVals rest)
596596
r -> ([], r)
597597
newKeyVals = sortAndDeduplicate $ keyVals ++ keyVals'
598598
newRest = rest'
599-
in Term
600-
argAttributes
601-
{ hash =
602-
Hashable.hash
603-
( "KMap" :: ByteString
604-
, def
605-
, map (\(k, v) -> (hash $ getAttributes k, hash $ getAttributes v)) newKeyVals
606-
, hash . getAttributes <$> newRest
607-
)
608-
}
609-
$ KMapF def newKeyVals newRest
599+
in case (newKeyVals, newRest) of
600+
([], Just r) -> r
601+
_ ->
602+
Term
603+
argAttributes
604+
{ hash =
605+
Hashable.hash
606+
( "KMap" :: ByteString
607+
, def
608+
, map (\(k, v) -> (hash $ getAttributes k, hash $ getAttributes v)) newKeyVals
609+
, hash . getAttributes <$> newRest
610+
)
611+
}
612+
$ KMapF def newKeyVals newRest
610613

611614
pattern KList :: KListDefinition -> [Term] -> Maybe (Term, [Term]) -> Term
612615
pattern KList def heads rest <- Term _ (KListF def heads rest)
@@ -630,18 +633,21 @@ pattern KList def heads rest <- Term _ (KListF def heads rest)
630633
(\(m, ts) -> (heads <> heads', Just (m, ts <> tails)))
631634
rest'
632635
other -> (heads, other)
633-
in Term
634-
argAttributes
635-
{ hash =
636-
Hashable.hash
637-
( "KList" :: ByteString
638-
, def
639-
, map (hash . getAttributes) newHeads
640-
, fmap (hash . getAttributes . fst) newRest
641-
, fmap (map (hash . getAttributes) . snd) newRest
642-
)
643-
}
644-
$ KListF def newHeads newRest
636+
in case (newHeads, newRest) of
637+
([], Just (r, [])) -> r
638+
_ ->
639+
Term
640+
argAttributes
641+
{ hash =
642+
Hashable.hash
643+
( "KList" :: ByteString
644+
, def
645+
, map (hash . getAttributes) newHeads
646+
, fmap (hash . getAttributes . fst) newRest
647+
, fmap (map (hash . getAttributes) . snd) newRest
648+
)
649+
}
650+
$ KListF def newHeads newRest
645651

646652
pattern KSet :: KSetDefinition -> [Term] -> Maybe Term -> Term
647653
pattern KSet def elements rest <- Term _ (KSetF def elements rest)
@@ -662,17 +668,20 @@ pattern KSet def elements rest <- Term _ (KSetF def elements rest)
662668
other -> ([], other)
663669
newElements = sortAndDeduplicate $ elements <> elements'
664670
newRest = rest'
665-
in Term
666-
argAttributes
667-
{ hash =
668-
Hashable.hash
669-
( "KSet" :: ByteString
670-
, def
671-
, map (hash . getAttributes) newElements
672-
, fmap (hash . getAttributes) newRest
673-
)
674-
}
675-
$ KSetF def newElements newRest
671+
in case (newElements, newRest) of
672+
([], Just r) -> r
673+
_ ->
674+
Term
675+
argAttributes
676+
{ hash =
677+
Hashable.hash
678+
( "KSet" :: ByteString
679+
, def
680+
, map (hash . getAttributes) newElements
681+
, fmap (hash . getAttributes) newRest
682+
)
683+
}
684+
$ KSetF def newElements newRest
676685
{-# COMPLETE AndTerm, SymbolApplication, DomainValue, Var, Injection, KMap, KList, KSet #-}
677686

678687
-- hard-wired injection symbol

booster/unit-tests/Test/Booster/Pattern/InternalCollections.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ listInternalisation =
8989
replicate 4 $
9090
inList [trm| \dv{SomeSort{}}("after variable") |]
9191
in testCase "mixing a list" $ internalise (listConcat before listAfter) @=? mixedList
92+
, testCase "[...REST] normalises to REST" $
93+
KList Fixture.testKListDef [] (Just ([trm| REST:SortTestList{} |], []))
94+
@=? [trm| REST:SortTestList{} |]
9295
]
9396
where
9497
internalise = internaliseKList Fixture.testKListDef
@@ -178,6 +181,8 @@ setInternalisation =
178181
(Just $ SymbolApplication Fixture.setConcatSym [] [var1, var2])
179182
in testCase "two variables and some concrete elements in set, concat pushed inwards" $
180183
result @=? internalise twoVarsSet
184+
, testCase "{...REST} normalises to REST" $
185+
KSet Fixture.testKSetDef [] (Just [trm| REST:SortTestSet{} |]) @=? [trm| REST:SortTestSet{} |]
181186
]
182187
where
183188
internalise = internaliseKSet Fixture.testKSetDef
@@ -270,6 +275,9 @@ mapSmartConstructors =
270275
let input = makeKMapWithRest [1, 2, 3] (makeKMapWithRest [1, 2, 4] [trm| REST:SortTestMap{}|])
271276
expected = makeKMapWithRest [1, 2, 3, 4] [trm| REST:SortTestMap{}|]
272277
in input @=? expected
278+
, testCase "{...REST} normalises to REST" $
279+
KMap Fixture.testKMapDefinition [] (Just [trm| REST:SortTestMap{} |])
280+
@=? [trm| REST:SortTestMap{} |]
273281
]
274282
where
275283
-- produced a map of identities for all input ints: x1 |-> x1, x2 |-> x2 ...

0 commit comments

Comments
 (0)