Skip to content

Commit

Permalink
Small cleanup to TcDeref: no need to store the value type
Browse files Browse the repository at this point in the history
Reviewed By: donsbot

Differential Revision: D67113333

fbshipit-source-id: 0863704f90424aefb42e43cc94c62d524385938f
  • Loading branch information
Simon Marlow authored and facebook-github-bot committed Dec 12, 2024
1 parent fc9b418 commit 1303f7b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 19 deletions.
4 changes: 2 additions & 2 deletions glean/db/Glean/Query/Expand.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ instantiateWithFreshVariables query numVars = do
(Typed ty $ instantiatePat base cond)
(instantiatePat base then_)
(instantiatePat base else_)
instantiateTcTerm base (TcDeref ty valTy pat) =
TcDeref ty valTy (instantiatePat base pat)
instantiateTcTerm base (TcDeref ty pat) =
TcDeref ty (instantiatePat base pat)
instantiateTcTerm base (TcFieldSelect (Typed ty pat) field) =
TcFieldSelect (Typed ty (instantiatePat base pat)) field
instantiateTcTerm base (TcAltSelect (Typed ty pat) field) =
Expand Down
8 changes: 5 additions & 3 deletions glean/db/Glean/Query/Flatten.hs
Original file line number Diff line number Diff line change
Expand Up @@ -343,14 +343,16 @@ flattenPattern pat = case pat of
return (as ++ bs)

-- *(pat : P) ==> X where pat = P X
Ref (MatchExt (Typed keyTy (TcDeref ty valTy pat))) -> do
Ref (MatchExt (Typed keyTy (TcDeref ty pat))) -> do
r <- flattenPattern pat
ref <- case ty of
ref@(PidRef _ pred) <- case ty of
Angle.PredicateTy ref -> return ref
_other -> throwError "TcDeref: not a predicate"
PredicateDetails{..} <- getPredicateDetails pred
forM r $ \(stmts, p) -> do
v <- Ref . MatchVar <$> fresh keyTy
let gen = FactGenerator ref v (Ref (MatchWild valTy)) SeekOnAllFacts
let valPat = Ref (MatchWild predicateValueType)
gen = FactGenerator ref v valPat SeekOnAllFacts
return (stmts `thenStmt` FlatStatement ty p gen, v)

-- pat.field ==> X where { field = X } = pat
Expand Down
9 changes: 9 additions & 0 deletions glean/db/Glean/Query/Flatten/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
module Glean.Query.Flatten.Types
( F
, initialFlattenState
, getPredicateDetails
, FlattenState(..)
, FlattenedQuery
, FlatQuery_(..)
Expand Down Expand Up @@ -40,6 +41,7 @@ import Compat.Prettyprinter hiding ((<>))
import Glean.Angle.Types ( PredicateId )
import Glean.Query.Codegen.Types
import Glean.Database.Schema
import Glean.Database.Schema.Types
import Glean.Database.Types (EnableRecursion(..))
import Glean.Display
import Glean.RTS.Types as RTS
Expand Down Expand Up @@ -267,6 +269,13 @@ data FlattenState = FlattenState
, flRecursion :: EnableRecursion
}

getPredicateDetails :: PredicateId -> F PredicateDetails
getPredicateDetails pred = do
dbSchema <- gets flDbSchema
case lookupPredicateId pred dbSchema of
Nothing -> error $ "predicateKeyTYpe: " <> show (displayDefault pred)
Just d -> return d

initialFlattenState
:: EnableRecursion
-> DbSchema
Expand Down
6 changes: 3 additions & 3 deletions glean/db/Glean/Query/Prune.hs
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@ prune hasFacts (QueryWithInfo q _ gen t) = do
, pa
, pb
]
TcDeref ty' valTy p
TcDeref ty' p
| Angle.PredicateTy (PidRef _ predId) <- ty', not $ hasFacts predId ->
Nothing
| otherwise ->
Ref . MatchExt . Typed ty . TcDeref ty' valTy <$> prunePat p
Ref . MatchExt . Typed ty . TcDeref ty' <$> prunePat p
TcFieldSelect (Typed ty' p) f -> do
p' <- prunePat p
return $ Ref $ MatchExt $ Typed ty $ TcFieldSelect (Typed ty' p') f
Expand Down Expand Up @@ -243,7 +243,7 @@ renumberVars gen ty q =
TcPrimCall op xs -> TcPrimCall op <$> traverse renamePat xs
TcIf cond then_ else_ ->
TcIf <$> traverse renamePat cond <*> renamePat then_ <*> renamePat else_
TcDeref ty valTy p -> TcDeref ty valTy <$> renamePat p
TcDeref ty p -> TcDeref ty <$> renamePat p
TcFieldSelect (Typed ty p) f -> do
p' <- renamePat p
return $ TcFieldSelect (Typed ty p') f
Expand Down
6 changes: 3 additions & 3 deletions glean/db/Glean/Query/Typecheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ fieldSelect src ty pat fieldName recordOrSum = do
PredicateDetails{..} <- case HashMap.lookup ref tcEnvPredicates of
Nothing -> prettyErrorIn src $ "fieldSelect: " <> displayDefault ref
Just details -> return details
let deref = TcDeref ty predicateValueType pat
let deref = TcDeref ty pat
fieldSelect src predicateKeyType
(Ref (MatchExt (Typed predicateKeyType deref)))
fieldName recordOrSum
Expand Down Expand Up @@ -1022,7 +1022,7 @@ tcQueryDeps q = Set.fromList $ map getRef (overQuery q)
TcNegation stmts -> foldMap overStatement stmts
TcPrimCall _ xs -> foldMap overPat xs
TcIf (Typed _ x) y z -> foldMap overPat [x, y, z]
TcDeref _ _ p -> overPat p
TcDeref _ p -> overPat p
TcFieldSelect (Typed _ p) _ -> overPat p
TcAltSelect (Typed _ p) _ -> overPat p
TcPromote _ p -> overPat p
Expand Down Expand Up @@ -1080,7 +1080,7 @@ tcTermUsesNegation = \case
TcPrimCall _ xs -> firstJust tcPatUsesNegation xs
-- one can replicate negation using if statements
TcIf{} -> Just IfStatement
TcDeref _ _ p -> tcPatUsesNegation p
TcDeref _ p -> tcPatUsesNegation p
TcFieldSelect (Typed _ p) _ -> tcPatUsesNegation p
TcAltSelect (Typed _ p) _ -> tcPatUsesNegation p
TcPromote _ p -> tcPatUsesNegation p
Expand Down
4 changes: 2 additions & 2 deletions glean/db/Glean/Query/Typecheck/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ data TcTerm
| TcNegation [TcStatement]
| TcPrimCall PrimOp [TcPat]
| TcIf { cond :: Typed TcPat, then_ :: TcPat, else_ :: TcPat }
| TcDeref Type Type TcPat
-- pat* : if pat has predicate type, evaluates to the key(s). We
-- don't expose this at the source level except via
-- field-selection, e.g. X.a will dereference X before
-- selecting the field 'a' if X has predicate type.
| TcDeref Type TcPat
| TcFieldSelect (Typed TcPat) FieldName
| TcAltSelect (Typed TcPat) FieldName
| TcPromote Type TcPat
Expand All @@ -83,7 +83,7 @@ data TcTerm

instance Display TcTerm where
display opts (TcOr a b) = display opts a <+> "|" <+> display opts b
display opts (TcDeref _ _ pat) = displayAtom opts pat <> "*"
display opts (TcDeref _ pat) = displayAtom opts pat <> ".*"
display opts (TcFieldSelect (Typed _ pat) field) =
displayAtom opts pat <> "." <> pretty field
display opts (TcAltSelect pat field) =
Expand Down
10 changes: 4 additions & 6 deletions glean/db/Glean/Query/Typecheck/Unify.hs
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,8 @@ zonkTcPat p = case p of
(_, TyVar{}) -> error "zonkMatch: tyvar"
(PredicateTy (PidRef _ ref), PredicateTy (PidRef _ ref'))
| ref == ref' -> return e'
(_other, PredicateTy (PidRef _ ref)) -> do
PredicateDetails{..} <- getPredicateDetails ref
return (Ref (MatchExt (Typed ty'
(TcDeref inner' predicateValueType e'))))
(_other, PredicateTy{}) -> do
return (Ref (MatchExt (Typed ty' (TcDeref inner' e'))))
_ ->
return e'
Ref (MatchExt (Typed ty (TcStructPat fs))) -> do
Expand Down Expand Up @@ -313,8 +311,8 @@ zonkTcTerm t = case t of
<$> (Typed <$> zonkType ty <*> zonkTcPat cond)
<*> zonkTcPat th
<*> zonkTcPat el
TcDeref ty valTy p ->
TcDeref <$> zonkType ty <*> zonkType valTy <*> zonkTcPat p
TcDeref ty p ->
TcDeref <$> zonkType ty <*> zonkTcPat p
TcFieldSelect (Typed ty p) f ->
TcFieldSelect
<$> (Typed <$> zonkType ty <*> zonkTcPat p)
Expand Down

0 comments on commit 1303f7b

Please sign in to comment.