Skip to content

Commit f15a5ef

Browse files
committed
Angle compiler cleanups
- rename `TcQueryGen` to `TcWhere` - change `TcAll` and `TcNegation` to contain `TcPat` instead of `TcQuery` / `[TcStatement]` respectively (this is more uniform and consistent, and simplifies some things) - refactoring in the type checker to unify how we handle locality. `oneBranch` is renamed `encloseLocal` and used consistently when typechecking `|`, `all`, `!`, and `if`, which all need locality. Probably fixes some bugs. - update a few out-of-date comments
1 parent 0c7da38 commit f15a5ef

9 files changed

Lines changed: 138 additions & 201 deletions

File tree

glean/db/Glean/Query/BindOrder.hs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,12 @@ import Glean.Query.Vars (VarSet)
3333
import Glean.RTS.Term as RTS
3434

3535
-- -----------------------------------------------------------------------------
36-
-- Fixing up MatchBind vs. MatchVar
37-
38-
-- Substitution can mess up MatchBind and MatchVar - we might end up
39-
-- with multiple MatchBinds for a variable, or a MatchVar before a
40-
-- MatchBind. To fix it up all we need to do is traverse the query in
41-
-- the correct order, keeping track of which variables are in scope,
42-
-- and change MatchBind<->MatchVar as appropriate.
43-
--
44-
-- This is all somewhat suboptimal, because the typechecker has
45-
-- already figured out MatchBind vs. MatchVar and here we mess it up
46-
-- by substitution and then fix it again. Which begs the question: why
47-
-- do we have MatchBind and MatchVar at all, couldn't we leave it
48-
-- until the last minute just before code generation to figure out
49-
-- which variables are binding occurrences? Yes, but it's nice to be
50-
-- able to give the user out-of-scope error messages from the
51-
-- typechecker. Maybe we'll change this in the future.
36+
-- Resolving MatchBind vs. MatchVar
37+
38+
-- Before the Reorder pass, MatchBind and MatchVar are the same (strictly
39+
-- speaking we should have just one variable form). The reorder pass resolves
40+
-- statement ordering so that variables are bound before their use;
41+
-- MatchBind indicates a variable binding and MatchVar indicates a variable use.
5242

5343
data Scope = Scope
5444
{ isScope :: VarSet

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

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -81,46 +81,15 @@ data CgStatement_ var
8181
| CgAllStatement var (Expr_ var) [CgStatement_ var]
8282
| CgNegation [CgStatement_ var]
8383
| CgDisjunction [[CgStatement_ var]]
84-
-- ^ For rationale, see Note [why do we have sequential composition?]
8584
| CgConditional
8685
{ cond :: [CgStatement_ var]
8786
, then_ :: [CgStatement_ var]
8887
, else_ :: [CgStatement_ var]
8988
}
9089
deriving (Show, Functor, Foldable, Traversable)
9190

92-
9391
type CgStatement = CgStatement_ Var
9492

95-
{- Note [why do we have sequential composition?]
96-
97-
The issue is that queries for sum types can't necessarily be handled
98-
by nested generators. Consider
99-
100-
v = cxx1.FunctionName (name(cxx1.Name "xyz" ) | operator(cxx1.Name "+"))
101-
102-
If we flattened this into nested generators we would get
103-
104-
x = cxx1.Name "xyz"
105-
y = cxx1.Name "+"
106-
z = cxx1.FunctionName (name x | operator y)
107-
108-
Now suppose there is no name xyz. This query will match nothing,
109-
because the generator for cxx1.Name "xyz" would be empty. (even if
110-
the generator matched, flattening out the generators like this will
111-
test too many combinations and do too much work).
112-
113-
With sequential composition of queries we can do it like this:
114-
115-
n = (name x where x = cxx1.Name "xyz") |
116-
(operator x where x = cxx1.Name "+")
117-
v = cxx1.FunctionName n
118-
119-
(Note that this query won't work if you write it because we can't
120-
typecheck the sub-query "name x where ...", but we can generate the
121-
AST for it in the JSON query compiler.)
122-
-}
123-
12493
type Generator = Generator_ Var
12594

12695
-- | A generator produces a finite series of terms

glean/db/Glean/Query/Expand.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ instantiateWithFreshVariables query numVars = do
102102
TcElementsOfSet (instantiatePat base pat)
103103
instantiateTcTerm base (TcElementsUnresolved ty pat) =
104104
TcElementsUnresolved ty (instantiatePat base pat)
105-
instantiateTcTerm base (TcQueryGen query) =
106-
TcQueryGen (instantiateQuery base query)
107-
instantiateTcTerm base (TcAll query) =
108-
TcAll (instantiateQuery base query)
109-
instantiateTcTerm base (TcNegation stmts) =
110-
TcNegation (map (instantiateStmt base) stmts)
105+
instantiateTcTerm base (TcWhere query) =
106+
TcWhere (instantiateQuery base query)
107+
instantiateTcTerm base (TcAll pat) =
108+
TcAll (instantiatePat base pat)
109+
instantiateTcTerm base (TcNegation p) =
110+
TcNegation (instantiatePat base p)
111111
instantiateTcTerm base (TcPrimCall op args) =
112112
TcPrimCall op (map (instantiatePat base) args)
113113
instantiateTcTerm base (TcIf (Typed ty cond) then_ else_) =

glean/db/Glean/Query/Flatten.hs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,20 +210,25 @@ flattenSeqGenerators (Ref (MatchExt (Typed ty match))) = case match of
210210
TcElementsOfSet pat -> do
211211
r <- flattenPattern pat
212212
return [(mempty, stmts, SetElementGenerator ty pat') | (stmts,pat') <- r ]
213-
TcQueryGen query -> do
213+
TcWhere query -> do
214214
(group, term, _) <- flattenQuery' query
215215
return [(floatGroup group, mempty, TermGenerator term)]
216-
TcAll query -> do
217-
(group, term, _) <- flattenQuery' query
216+
TcAll p -> do
217+
let elemTy = case derefType ty of
218+
Schema.SetTy t -> t
219+
_other -> error "TcAll: not SetTy"
220+
(group, term, _) <- flattenQuery' $
221+
TcQuery elemTy p Nothing [] Angle.Unordered
218222
var <- fresh ty
219223
return
220224
[ (Statements [FlatAllStatement var term group]
221225
, mempty
222226
, TermGenerator (Ref (MatchVar var)))]
223-
TcNegation stmts -> do
224-
(ords, floats) <- mapAndUnzipM flattenStatement stmts
225-
let neg = FlatNegation (mkGroup ords floats)
226-
return [(oneStmt neg, mempty, TermGenerator $ Tuple [])]
227+
TcNegation p -> do
228+
(group, _term, _) <- flattenQuery' (TcQuery ty p Nothing [] Angle.Ordered)
229+
-- Note: term is discarded. e.g. in !(X where ...) we don't do anything
230+
-- with the X.
231+
return [(oneStmt (FlatNegation group), mempty, TermGenerator $ Tuple [])]
227232
TcPrimCall op args -> do
228233
r <- manyTerms (\args -> PrimCall op args ty) <$> mapM flattenPattern args
229234
return [ (mempty, float, gen) | (float, gen) <- r ]

glean/db/Glean/Query/Prune.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,12 @@ prune hasFacts (QueryWithInfo q _ gen t) = do
155155
Ref . MatchExt . Typed ty . TcElementsOfSet <$> prunePat x
156156
TcElementsUnresolved ty' x ->
157157
Ref . MatchExt . Typed ty . TcElementsUnresolved ty' <$> prunePat x
158-
TcQueryGen q ->
159-
Ref . MatchExt . Typed ty . TcQueryGen <$> pruneTcQuery q
158+
TcWhere q ->
159+
Ref . MatchExt . Typed ty . TcWhere <$> pruneTcQuery q
160160
-- we dont' want to handle negation here because if it tries to match
161161
-- against things that are not in the database it should succeed.
162-
TcAll query ->
163-
Ref . MatchExt . Typed ty . TcAll <$> pruneTcQuery query
162+
TcAll p ->
163+
Ref . MatchExt . Typed ty . TcAll <$> prunePat p
164164
TcNegation{} -> Just pat
165165
TcPrimCall op xs -> Ref . MatchExt . Typed ty . TcPrimCall op
166166
<$> traverse prunePat xs
@@ -240,9 +240,9 @@ renumberVars gen ty q =
240240
TcElementsOfArray x -> TcElementsOfArray <$> renamePat x
241241
TcElementsOfSet x -> TcElementsOfSet <$> renamePat x
242242
TcElementsUnresolved ty x -> TcElementsUnresolved ty <$> renamePat x
243-
TcQueryGen q -> TcQueryGen <$> renameQuery q
244-
TcAll query -> TcAll <$> renameQuery query
245-
TcNegation xs -> TcNegation <$> traverse renameStmt xs
243+
TcWhere q -> TcWhere <$> renameQuery q
244+
TcAll p -> TcAll <$> renamePat p
245+
TcNegation p -> TcNegation <$> renamePat p
246246
TcPrimCall op xs -> TcPrimCall op <$> traverse renamePat xs
247247
TcIf cond then_ else_ ->
248248
TcIf <$> traverse renamePat cond <*> renamePat then_ <*> renamePat else_

0 commit comments

Comments
 (0)