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

Lower the boom on the mess in the saw-script typechecker #2157

Merged
merged 22 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a700434
Typechecker (MGU.hs): move code around.
sauclovian-g Dec 11, 2024
70a4a97
Typechecker (MGU.hs): Comments, minor cleanup, and some naming reform.
sauclovian-g Dec 11, 2024
fa18a27
Typechecker (MGU.hs): move another block of code.
sauclovian-g Dec 11, 2024
372321b
Typechecker (MGU.hs): some more invasive internal name reform
sauclovian-g Dec 11, 2024
01c4ca8
Typechecker (MGU.hs): Try to make the rattiest parts of this legible.
sauclovian-g Dec 11, 2024
eca889f
Typechecker (MGU.hs): fix unergonomic checking of function applications
sauclovian-g Dec 11, 2024
99b436a
Typechecker (MGU.hs): adjustments and notes prior to adding kind-chec…
sauclovian-g Dec 11, 2024
7a1c89a
AST: Remove the type hint in StmtBind.
sauclovian-g Dec 12, 2024
91f9bbf
Typechecker (MGU.hs): implement the stubbed-out kind checking
sauclovian-g Dec 12, 2024
8d805d1
Typechecker (MGU.hs): Split out the check for a single statement.
sauclovian-g Dec 12, 2024
357fa00
Interpreter: typecheck top-level typedef statements before executing …
sauclovian-g Dec 12, 2024
16a67c7
Typechecker (MGU.hs): need Control.Monad
sauclovian-g Dec 12, 2024
e3bdc48
Add changelog entry mentioning #2077
sauclovian-g Dec 12, 2024
6f179ef
Typechecker (MGU.hs): restrict monad imports
sauclovian-g Dec 12, 2024
bf86a7f
Typechecker (MGU.hs): further review changes
sauclovian-g Dec 12, 2024
f048c6e
Add a :tenv repl command, that's like :env but prints the typedef env…
sauclovian-g Dec 13, 2024
9ddda46
Make the repl prefer exact matches when looking up :-commands.
sauclovian-g Dec 14, 2024
1b239e3
Typechecker (MGU.hs): more review bits, mostly comments and minor cle…
sauclovian-g Dec 14, 2024
2037343
Interpreter: rename rwTypes -> rwValueTypes, rwTypedef -> rwNamedTypes.
sauclovian-g Dec 14, 2024
f3015a1
Interpreter and typechecker: allow the named type env to hold opaque …
sauclovian-g Dec 14, 2024
39f5d5b
Interpreter: declare the types used by builtins, in preliminary form.
sauclovian-g Dec 14, 2024
be94c5f
Additional minor adjustments from review.
sauclovian-g Dec 16, 2024
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
3 changes: 3 additions & 0 deletions intTests/test_type_errors/err003.log.good
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Loading file "err003.saw"
err003.saw:3:16-3:27: Unbound type variable Nonexistent
FAILED
8 changes: 8 additions & 0 deletions intTests/test_type_errors/err003.saw
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

let bar = do {
typedef t = Nonexistent;

let foo (a: t) = a;

return 0;
};
3 changes: 3 additions & 0 deletions intTests/test_type_errors/err004.log.good
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Loading file "err004.saw"
err004.saw:2:13-2:24: Unbound type variable Nonexistent
FAILED
4 changes: 4 additions & 0 deletions intTests/test_type_errors/err004.saw
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

typedef t = Nonexistent;

let foo (a: t) = a;
3 changes: 3 additions & 0 deletions intTests/test_type_errors/err005.log.good
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Loading file "err005.saw"
err005.saw:1:30-1:31: Unbound type variable x
FAILED
1 change: 1 addition & 0 deletions intTests/test_type_errors/err005.saw
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
typedef x = { foo: Int, bar: x };
RyanGlScott marked this conversation as resolved.
Show resolved Hide resolved
24 changes: 22 additions & 2 deletions src/SAWScript/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import SAWScript.JavaExpr
import SAWScript.LLVMBuiltins
import SAWScript.Options
import SAWScript.Lexer (lexSAW)
import SAWScript.MGU (checkDecl, checkDeclGroup)
import SAWScript.MGU (checkDecl, checkDeclGroup, checkStmt)
import SAWScript.Parser (parseSchema)
import SAWScript.TopLevel
import SAWScript.Utils
Expand Down Expand Up @@ -294,6 +294,8 @@ processStmtBind printBinds pat expr = do -- mx mt
-- isn't any single variable use "it". We seem to get here only for
-- statements typed at the repl, so it apparently isn't wrong to use
-- "it".
-- XXX: that's not actually true, file loads come here via
-- interpretStmt and interpretFile.
-- XXX: it seems problematic to discard the type for a tuple binding...
let it pos = SS.Located "it" "it" pos
let (lname, mt) = case pat of
Expand Down Expand Up @@ -377,6 +379,18 @@ interpretStmt :: InteractiveMonad m =>
m ()
interpretStmt printBinds stmt =
let ?fileReader = BS.readFile in

-- XXX: not yet. The code in processStmtBind that typechecks the
-- statement incrementally does extra things behind the typechecker's
-- back (it wraps each bind in a Decl so it passes through generalize)
-- and we need to figure out the correct way to make that happen
-- before typechecking up front.
{-
rw <- getTopLevelRW
stmt' <- either failTypecheck return $
checkStmt (rwTypes rw) (rwTypedef rw) stmt
-}

case stmt of

SS.StmtBind pos pat expr ->
Expand Down Expand Up @@ -412,9 +426,15 @@ interpretStmt printBinds stmt =
putTopLevelRW $ rw { rwCryptol = cenv' }
--showCryptolEnv

SS.StmtTypedef _ name ty ->
SS.StmtTypedef _ _ _ ->
liftTopLevel $
do rw <- getTopLevelRW
ctx <- getMonadContext
pos <- getPosition
-- XXX: hack this until such time as we can get it to work up front
stmt' <- either failTypecheck return $
checkStmt (rwTypes rw) (rwTypedef rw) pos ctx stmt
let (SS.StmtTypedef _ name ty) = stmt'
putTopLevelRW $ addTypedef (getVal name) ty rw

interpretFile :: FilePath -> Bool {- ^ run main? -} -> TopLevel ()
Expand Down
21 changes: 21 additions & 0 deletions src/SAWScript/MGU.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
module SAWScript.MGU
( checkDecl
, checkDeclGroup
, checkStmt
, instantiate
) where

Expand Down Expand Up @@ -1186,7 +1187,7 @@
getErrorTyVar pos
else do
-- note that this will ignore the extra params
args' <- zipWithM checkType params args

Check failure on line 1190 in src/SAWScript/MGU.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 3.10.3.0, 9.6.6, true, false)

Variable not in scope:

Check failure on line 1190 in src/SAWScript/MGU.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 3.10.3.0, 9.8.2, true, false)

Variable not in scope:
return $ TyCon pos tycon args'

TyRecord pos fields -> do
Expand Down Expand Up @@ -1256,6 +1257,26 @@
(res, _, []) -> Right res
(_, _, errs) -> Left errs

-- Check a single statement. (This is an external interface.)
--
-- The first two arguments are the starting variable and typedef
-- environments to use.
--
-- The third is a current position, and the fourth is the
-- context/monad type associated with the execution.
RyanGlScott marked this conversation as resolved.
Show resolved Hide resolved
checkStmt :: Map LName Schema -> Map Name Type -> Pos -> Context -> Stmt -> Either [(Pos, String)] Stmt
checkStmt env tenv pos ctx stmt = do
-- Ugh. This should all be cleaned out further. XXX
RyanGlScott marked this conversation as resolved.
Show resolved Hide resolved
ln <- case ctx of
TopLevel -> return $ Located "<toplevel>" "<toplevel>" pos
ProofScript -> return $ Located "<proofscript>" "<proofscript>" pos
_ -> panic "checkStmt" ["Invalid monad context " ++ pShow ctx]
let ctxtype = TyCon pos (ContextCon ctx) []
case evalTIWithEnv env tenv (inferStmt ln pos ctxtype stmt) of
Left errs -> Left errs
Right (_wrapper, stmt', _type) -> Right stmt'


-- Check a single declaration. (This is an external interface.)
--
-- The first two arguments are the starting variable and typedef
Expand Down
Loading