Skip to content
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

Updated type inference to not re-type-check the bodies and types of globals #1416

Merged
merged 2 commits into from
Aug 21, 2021
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
4 changes: 3 additions & 1 deletion saw-core/src/Verifier/SAW/OpenTerm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ dataTypeOpenTerm d all_args = OpenTerm $ do
-- | Build an 'OpenTerm' for a global name.
globalOpenTerm :: Ident -> OpenTerm
globalOpenTerm ident =
OpenTerm (liftTCM scGlobalDef ident >>= typeInferComplete)
OpenTerm (do trm <- liftTCM scGlobalDef ident
tp <- liftTCM scTypeOfGlobal ident
return $ TypedTerm trm tp)

-- | Apply an 'OpenTerm' to another
applyOpenTerm :: OpenTerm -> OpenTerm -> OpenTerm
Expand Down
15 changes: 14 additions & 1 deletion saw-core/src/Verifier/SAW/SCTypeCheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ instance TypeInfer Term where
-- calling inference on all the sub-components and extending the context inside
-- of the binding forms
instance TypeInfer (TermF Term) where
typeInfer (FTermF ftf) =
-- Dispatch to the TypeInfer instance for FlatTermF Term, which does some
-- special-case handling itself
typeInfer ftf
typeInfer (Lambda x a rhs) =
do a_tptrm <- typeInferCompleteWHNF a
-- NOTE: before adding a type to the context, we want to be sure it is in
Expand All @@ -388,12 +392,21 @@ instance TypeInfer (TermF Term) where
-- WHNF, so we don't have to normalize each time we look up a var type
rhs_tptrm <- withVar x (typedVal a_tptrm) $ typeInferComplete rhs
typeInfer (Pi x a_tptrm rhs_tptrm)
typeInfer (Constant ec _) =
-- NOTE: this special case is to prevent us from re-type-checking the
-- definition of each constant, as we assume it was type-checked when it was
-- created
return $ ecType ec
typeInfer t = typeInfer =<< mapM typeInferComplete t
typeInferComplete tf =
TypedTerm <$> liftTCM scTermF tf <*> typeInfer tf

-- Type inference for FlatTermF Term dispatches to that for FlatTermF TypedTerm
-- Type inference for FlatTermF Term dispatches to that for FlatTermF TypedTerm,
-- with special cases for primitives and constants to avoid re-type-checking
-- their types as we are assuming they were type-checked when they were created
instance TypeInfer (FlatTermF Term) where
typeInfer (Primitive pn) = return $ primType pn
typeInfer (ExtCns ec) = return $ ecType ec
typeInfer t = typeInfer =<< mapM typeInferComplete t
typeInferComplete ftf =
TypedTerm <$> liftTCM scFlatTermF ftf <*> typeInfer ftf
Expand Down