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

Better error message for when type variable instances are not found for :checking #1357

Merged
merged 3 commits into from
Jun 3, 2022
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
55 changes: 32 additions & 23 deletions src/Cryptol/REPL/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,17 @@ qcExpr ::
T.Schema ->
REPL TestReport
qcExpr qcMode exprDoc texpr schema =
do (val,ty) <- replEvalCheckedExpr texpr schema
do (val,ty) <- replEvalCheckedExpr texpr schema >>= \mb_res -> case mb_res of
robdockins marked this conversation as resolved.
Show resolved Hide resolved
Just res -> pure res
-- If instance is not found, doesn't necessarily mean that there is no instance.
-- And due to nondeterminism in result from the solver (for finding solution to
-- numeric type constraints), `:check` can get to this exception sometimes, but
-- successfully find an instance and test with it other times.
Nothing -> raise (InstantiationsNotFound schema)
testNum <- (toInteger :: Int -> Integer) <$> getKnownUser "tests"
tenv <- E.envTypes . M.deEnv <$> getDynEnv
let tyv = E.evalValType tenv ty
-- tv has already had polymorphism instantiated
percentRef <- io $ newIORef Nothing
testsRef <- io $ newIORef 0

Expand Down Expand Up @@ -801,7 +808,9 @@ proveSatExpr isSat rng exprDoc texpr schema = do
printSafetyViolation :: T.Expr -> T.Schema -> [E.GenValue Concrete] -> REPL ()
printSafetyViolation texpr schema vs =
catch
(do (fn,_) <- replEvalCheckedExpr texpr schema
(do fn <- replEvalCheckedExpr texpr schema >>= \mb_res -> case mb_res of
Just (fn, _) -> pure fn
Nothing -> raise (EvalPolyError schema)
rEval (E.forceValue =<< foldM (\f v -> E.fromVFun Concrete f (pure v)) fn vs))
(\case
EvalError eex -> rPutStrLn (show (pp eex))
Expand Down Expand Up @@ -1552,39 +1561,39 @@ replSpecExpr e = liftModuleCmd $ S.specialize e
replEvalExpr :: P.Expr P.PName -> REPL (Concrete.Value, T.Type)
replEvalExpr expr =
do (_,def,sig) <- replCheckExpr expr
replEvalCheckedExpr def sig
replEvalCheckedExpr def sig >>= \mb_res -> case mb_res of
Just res -> pure res
Nothing -> raise (EvalPolyError sig)

replEvalCheckedExpr :: T.Expr -> T.Schema -> REPL (Concrete.Value, T.Type)
replEvalCheckedExpr :: T.Expr -> T.Schema -> REPL (Maybe (Concrete.Value, T.Type))
replEvalCheckedExpr def sig =
do validEvalContext def
validEvalContext sig

s <- getTCSolver
mbDef <- io (defaultReplExpr s def sig)

(def1,ty) <-
case mbDef of
Nothing -> raise (EvalPolyError sig)
Just (tys,def1) ->
do warnDefaults tys
let su = T.listParamSubst tys
return (def1, T.apSubst su (T.sType sig))
case mbDef of
Nothing -> pure Nothing
Just (tys, def1) ->
do warnDefaults tys
let su = T.listParamSubst tys
let ty = T.apSubst su (T.sType sig)
whenDebug (rPutStrLn (dump def1))

whenDebug (rPutStrLn (dump def1))
tenv <- E.envTypes . M.deEnv <$> getDynEnv
let tyv = E.evalValType tenv ty

tenv <- E.envTypes . M.deEnv <$> getDynEnv
let tyv = E.evalValType tenv ty

-- add "it" to the namespace via a new declaration
itVar <- bindItVariable tyv def1
-- add "it" to the namespace via a new declaration
itVar <- bindItVariable tyv def1

let itExpr = case getLoc def of
Nothing -> T.EVar itVar
Just rng -> T.ELocated rng (T.EVar itVar)
let itExpr = case getLoc def of
Nothing -> T.EVar itVar
Just rng -> T.ELocated rng (T.EVar itVar)

-- evaluate the it variable
val <- liftModuleCmd (rethrowEvalError . M.evalExpr itExpr)
return (val,ty)
-- evaluate the it variable
val <- liftModuleCmd (rethrowEvalError . M.evalExpr itExpr)
return $ Just (val,ty)
where
warnDefaults ts =
case ts of
Expand Down
3 changes: 3 additions & 0 deletions src/Cryptol/REPL/Monad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ data REPLException
| Unsupported Unsupported
| ModuleSystemError NameDisp M.ModuleError
| EvalPolyError T.Schema
| InstantiationsNotFound T.Schema
| TypeNotTestable T.Type
| EvalInParamModule [M.Name]
| SBVError String
Expand Down Expand Up @@ -358,6 +359,8 @@ instance PP REPLException where
TooWide e -> pp e
EvalPolyError s -> text "Cannot evaluate polymorphic value."
$$ text "Type:" <+> pp s
InstantiationsNotFound s -> text "Cannot find instantiations for some type variables."
$$ text "Type:" <+> pp s
TypeNotTestable t -> text "The expression is not of a testable type."
$$ text "Type:" <+> pp t
EvalInParamModule xs -> nest 2 $ vsep $
Expand Down