Skip to content

Allow matching empty sets #4009

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

Merged
merged 4 commits into from
Aug 1, 2024
Merged
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
24 changes: 22 additions & 2 deletions booster/library/Booster/Pattern/Match.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ import Data.List (partition)
import Data.List.NonEmpty as NE (NonEmpty, fromList)
import Data.Map (Map)
import Data.Map qualified as Map
import Data.Maybe (isNothing)
import Data.Sequence (Seq, (><), pattern (:<|), pattern (:|>))
import Data.Sequence qualified as Seq

import Data.Set (Set)
import Data.Set qualified as Set
import Prettyprinter

import Booster.Definition.Attributes.Base (KListDefinition, KMapDefinition)
import Booster.Definition.Attributes.Base (KListDefinition, KMapDefinition, KSetDefinition)
import Booster.Definition.Base
import Booster.Pattern.Base
import Booster.Pattern.Pretty
Expand Down Expand Up @@ -262,7 +263,7 @@ match1 Eval t1@KSet{} t2@Injection{}
match1 _ t1@KSet{} t2@Injection{} = failWith $ DifferentSymbols t1 t2
match1 _ t1@KSet{} t2@KMap{} = failWith $ DifferentSymbols t1 t2
match1 _ t1@KSet{} t2@KList{} = failWith $ DifferentSymbols t1 t2
match1 _ t1@KSet{} t2@KSet{} = addIndeterminate t1 t2
match1 _ t1@(KSet def1 patElements patRest) t2@(KSet def2 subjElements subjRest) = if def1 == def2 then matchSets def1 patElements patRest subjElements subjRest else failWith $ DifferentSorts t1 t2
match1 _ t1@KSet{} t2@ConsApplication{} = failWith $ DifferentSymbols t1 t2
match1 _ t1@KSet{} t2@FunctionApplication{} = addIndeterminate t1 t2
match1 Rewrite t1@KSet{} (Var t2) = failWith $ SubjectVariableMatch t1 t2
Expand Down Expand Up @@ -626,6 +627,25 @@ containsOtherKeys = \case
Rest OtherKey{} -> True
Rest _ -> False

------ Internalised Sets
matchSets ::
KSetDefinition ->
[Term] ->
Maybe Term ->
[Term] ->
Maybe Term ->
StateT MatchState (Except MatchResult) ()
matchSets
def
patElements
patRest
subjElements
subjRest = do
-- match only empty sets, indeterminate otherwise
if null patElements && null subjElements && isNothing patRest && isNothing subjRest
then pure ()
else addIndeterminate (KSet def patElements patRest) (KSet def subjElements subjRest)

------ Internalised Maps
matchMaps ::
KMapDefinition ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Test.Booster.Pattern.InternalCollections (
headList,
tailList,
mixedList,
emptySet,
) where

import Data.ByteString.Char8 qualified as BS
Expand Down
15 changes: 15 additions & 0 deletions booster/unit-tests/Test/Booster/Pattern/MatchEval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Booster.Pattern.Base
import Booster.Pattern.Match
import Booster.Syntax.Json.Internalise (trm)
import Test.Booster.Fixture
import Test.Booster.Pattern.InternalCollections

test_match_eval :: TestTree
test_match_eval =
Expand All @@ -28,6 +29,7 @@ test_match_eval =
, andTerms
, composite
, kmapTerms
, internalSets
]

symbols :: TestTree
Expand Down Expand Up @@ -293,6 +295,19 @@ cornerCases =
let v = var "X" someSort
in errors "identical variables" v v

internalSets :: TestTree
internalSets =
testGroup
"Internal sets"
[ test
"Can match an empty set with itself"
emptySet
emptySet
(success [])
]

----------------------------------------

test :: String -> Term -> Term -> MatchResult -> TestTree
test name pat subj expected =
testCase name $ matchTerms Eval testDefinition pat subj @?= expected
Expand Down
12 changes: 12 additions & 0 deletions booster/unit-tests/Test/Booster/Pattern/MatchImplies.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ test_match_implies =
, sorts
, injections
, internalLists
, internalSets
, internalMaps
]

Expand Down Expand Up @@ -377,6 +378,17 @@ internalLists =

klist = KList testKListDef

internalSets :: TestTree
internalSets =
testGroup
"Internal sets"
[ test
"Can match an empty set with itself"
emptySet
emptySet
(success [])
]

internalMaps :: TestTree
internalMaps =
testGroup
Expand Down
12 changes: 12 additions & 0 deletions booster/unit-tests/Test/Booster/Pattern/MatchRewrite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ test_match_rewrite =
, sorts
, injections
, internalLists
, internalSets
, internalMaps
]

Expand Down Expand Up @@ -376,6 +377,17 @@ internalLists =

klist = KList testKListDef

internalSets :: TestTree
internalSets =
testGroup
"Internal sets"
[ test
"Can match an empty set with itself"
emptySet
emptySet
Comment on lines +386 to +387
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to export emptySet from Test.Booster.Pattern.InternalCollections
Rest LGTM, we just have to fix this test.

(success [])
]

internalMaps :: TestTree
internalMaps =
testGroup
Expand Down
Loading