Skip to content

Commit 9d0c8d8

Browse files
committed
Flatten nested disjunction
1 parent 67da0c5 commit 9d0c8d8

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

glean/db/Glean/Query/Flatten.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ flattenStmts (Statements s) = map Floating (reverse s)
506506

507507
disjunction :: [FlatStatementGroup] -> FlatStatement
508508
disjunction [FlatStatementGroup [x]] = unOrdered x
509-
disjunction groups = FlatDisjunction groups
509+
disjunction groups = flatDisjunction groups
510510

511511
mkGroup :: [Statements] -> [Statements] -> FlatStatementGroup
512512
mkGroup ords floats =

glean/db/Glean/Query/Flatten/Types.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module Glean.Query.Flatten.Types
2121
, Ordered(..)
2222
, unOrdered
2323
, grouping
24+
, flatDisjunction
2425
, mkStatementGroup
2526
, singletonGroup
2627
, boundVars
@@ -129,6 +130,18 @@ grouping :: FlatStatementGroup -> FlatStatement
129130
grouping (FlatStatementGroup [one]) = unOrdered one
130131
grouping group = FlatDisjunction [group]
131132

133+
-- | Flatten nested FlatDisjunction
134+
flatDisjunction :: [FlatStatementGroup] -> FlatStatement
135+
flatDisjunction groups =
136+
FlatDisjunction
137+
[ group
138+
| g <- groups
139+
, group <- case g of
140+
FlatStatementGroup [Ordered (FlatDisjunction gs)] -> gs
141+
FlatStatementGroup [Floating (FlatDisjunction gs)] -> gs
142+
_ -> [g]
143+
]
144+
132145
-- | Smart constructor for a FlatStatementGroup, as with "grouping"
133146
-- this flattens unnecessary nesting.
134147
mkStatementGroup :: [Ordered FlatStatement] -> FlatStatementGroup

glean/db/Glean/Query/Opt.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ instance Apply FlatStatement where
248248
[] -> case stmtss' of
249249
[] -> return (FlatDisjunction [])
250250
(ss : _) -> return (grouping ss)
251-
some -> return (FlatDisjunction some)
251+
some -> return (flatDisjunction some)
252252
apply (FlatConditional cond then_ else_) = do
253253
-- like disjunctions, assumptions arising from the conditional statements
254254
-- are not true outside of it. However, those arising from the condition
@@ -731,7 +731,7 @@ filterStmt stmt = case stmt of
731731
FlatNegation stmts -> FlatNegation <$> filterGroupEnclosed stmts
732732
FlatDisjunction [stmts] -> grouping <$> filterGroup stmts
733733
FlatDisjunction stmtss ->
734-
FlatDisjunction <$> mapM filterGroupEnclosed stmtss
734+
flatDisjunction <$> mapM filterGroupEnclosed stmtss
735735
FlatConditional cond then_ else_ -> do
736736
(cond', then') <- encloseSeen $ do
737737
cond' <- filterGroup cond

0 commit comments

Comments
 (0)