-
Notifications
You must be signed in to change notification settings - Fork 157
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
Improve the propagation of explanations when a Spec fails. #4739
base: master
Are you sure you want to change the base?
Conversation
libs/cardano-ledger-test/src/Test/Cardano/Ledger/Constrained/Conway/WitnessUniverse.hs
Outdated
Show resolved
Hide resolved
libs/cardano-ledger-test/src/Test/Cardano/Ledger/Constrained/Conway/ParametricSpec.hs
Outdated
Show resolved
Hide resolved
Subset :: (Ord a, Show a, Typeable a) => SetFn fn '[Set a, Set a] Bool | ||
Disjoint :: (Ord a, Show a, Typeable a) => SetFn fn '[Set a, Set a] Bool | ||
Member :: (Ord a, Show a, Typeable a) => SetFn fn '[a, Set a] Bool | ||
Singleton :: Ord a => SetFn fn '[a] (Set a) | ||
Union :: Ord a => SetFn fn '[Set a, Set a] (Set a) | ||
Elem :: Eq a => SetFn fn '[a, [a]] Bool | ||
Union :: (Ord a, Show a, Typeable a) => SetFn fn '[Set a, Set a] (Set a) | ||
Elem :: (Eq a, Show a, Typeable a) => SetFn fn '[a, [a]] Bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's actually gained by introducing all these extra constraints?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without them I cannot introduce the construtor form of the patterns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I get why that's the case? Also, where are the patterns used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the patterns are used in the Pretty instance for Terms.
As we currently use the patterns to match, and not construct, we can get rid of some of the constraints.
f4a840b
to
aa9bbf2
Compare
9ba2f7c
to
19c01e0
Compare
The function explainSpec no longer drops explanations on TypeSpec Added guardTypeSpec, which catches errors earlier, so there is less noise. function symbols like subset_ and member_ print a elided version of their set arguments when they are large literals. Improves readabilty as irrelevant information no longer overwhelms the user. Add the constructor ExplainSpec to Specification Assert now has type Assert :: Term fn Bool -> Pred fn
19c01e0
to
9074791
Compare
shrinkFromPreds :: HasSpec fn a => Pred fn -> Var a -> a -> [a] | ||
shrinkFromPreds p | ||
| Result _ plan <- prepareLinearization p = \x a -> listFromGE $ do | ||
-- NOTE: we do this to e.g. guard against bad construction functions in Exists | ||
xaGood <- checkPred (singletonEnv x a) p | ||
unless xaGood $ | ||
fatalError1 "Trying to shrink a bad value, don't do that!" | ||
-- Get an `env` for the original value | ||
initialEnv <- envFromPred (singletonEnv x a) p | ||
return | ||
[ a' | ||
| -- Shrink the initialEnv | ||
env' <- shrinkEnvFromPlan initialEnv plan | ||
, -- Get the value of the constrained variable `x` in the shrunk env | ||
Just a' <- [lookupEnv env' x] | ||
, -- NOTE: this is necessary because it's possible that changing | ||
-- a particular value in the env during shrinking might not result | ||
-- in the value of `x` changing and there is no better way to know than | ||
-- to do this. | ||
a' /= a | ||
] | ||
| otherwise = error "Bad pred" | ||
|
||
-- Start with a valid Env for the plan and try to shrink it | ||
shrinkEnvFromPlan :: Env -> SolverPlan fn -> [Env] | ||
shrinkEnvFromPlan initialEnv SolverPlan {..} = go mempty solverPlan | ||
where | ||
go _ [] = [] -- In this case we decided to keep every variable the same so nothing to return | ||
go env ((substStage env -> SolverStage {..}) : plan) = do | ||
Just a <- [lookupEnv initialEnv stageVar] | ||
-- Two cases: | ||
-- - either we shrink this value and try to fixup every value later on in the plan or | ||
[ env' <> fixedEnv | ||
| a' <- shrinkWithSpec stageSpec a | ||
, let env' = extendEnv stageVar a' env | ||
, Just fixedEnv <- [fixupPlan env' plan] | ||
] | ||
-- - we keep this value the way it is and try to shrink some later value | ||
++ go (extendEnv stageVar a env) plan | ||
|
||
-- Fix the rest of the plan given an environment `env` for the plan so far | ||
fixupPlan env [] = pure env | ||
fixupPlan env ((substStage env -> SolverStage {..}) : plan) = | ||
case lookupEnv initialEnv stageVar >>= fixupWithSpec stageSpec of | ||
Nothing -> Nothing | ||
Just a -> fixupPlan (extendEnv stageVar a env) plan | ||
|
||
-- Try to fix a value w.r.t a specification | ||
fixupWithSpec :: forall fn a. HasSpec fn a => Specification fn a -> a -> Maybe a | ||
fixupWithSpec spec a | ||
| a `conformsToSpec` spec = Just a | ||
| otherwise = case spec of | ||
MemberSpec (a :| _) -> Just a | ||
_ -> listToMaybe $ filter (`conformsToSpec` spec) (shrinkWithSpec @fn TrueSpec a) | ||
|
||
-- Construct an environment for all variables that show up on the top level | ||
-- (i.e. ones bound in `let` and `exists`) from an environment for all the free | ||
-- variables in the pred. The environment you get out of this function is | ||
-- _bigger_ than the environment you put in. From | ||
-- ``` | ||
-- let y = x + 1 in let z = y + 1 in foo x y z | ||
-- ``` | ||
-- and an environment with `{x -> 1}` you would get `{x -> 1, y -> 2, z -> 3}` | ||
-- out. | ||
envFromPred :: Env -> Pred fn -> GE Env | ||
envFromPred env p = case p of | ||
-- NOTE: these don't bind anything | ||
Assert {} -> pure env | ||
DependsOn {} -> pure env | ||
Monitor {} -> pure env | ||
TruePred {} -> pure env | ||
FalsePred {} -> pure env | ||
GenHint {} -> pure env | ||
-- NOTE: this is ok because the variables either come from an `Exists`, a `Let`, or from | ||
-- the top level | ||
Reifies {} -> pure env | ||
-- NOTE: variables in here shouldn't escape to the top level | ||
ForAll {} -> pure env | ||
Case {} -> pure env | ||
-- These can introduce binders that show up in the plan | ||
When _ p -> envFromPred env p | ||
Subst x a p -> envFromPred env (substitutePred x a p) | ||
Let t (x :-> p) -> do | ||
v <- runTerm env t | ||
envFromPred (extendEnv x v env) p | ||
Explain _ p -> envFromPred env p | ||
Exists c (x :-> p) -> do | ||
v <- c (errorGE . explain1 "envFromPred: Exists" . runTerm env) | ||
envFromPred (extendEnv x v env) p | ||
Block [] -> pure env | ||
Block (p : ps) -> do | ||
env' <- envFromPred env p | ||
envFromPred env' (Block ps) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this being removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a merge conflict that I messed up?
-- | Temporarily extend the stack while executing 'm', and revert to the old stack if successful | ||
push :: forall m a. MonadGenError m => [String] -> GenT GE a -> GenT m a | ||
push [] m = dropGen m | ||
push (x : xs) m = | ||
case explain (x NE.:| xs) m of | ||
GenT f -> (GenT g) | ||
where | ||
g :: GenMode -> Gen (m a) | ||
g mode = do | ||
result <- f mode | ||
case result of | ||
Result (_ : ys) a -> pure $ runGE (Result ys a) | ||
other -> pure $ runGE other | ||
|
||
pushGE :: forall a. [String] -> GE a -> GE a | ||
pushGE [] x = x | ||
pushGE (x : xs) m = do | ||
case explain (x NE.:| xs) m of | ||
Result (_ : ys) a -> Result ys a | ||
other -> other |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get at all why this is necessary. This is what explain
already does??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
explain, adds an explanation, and it is never removed. This code adds a explanation for one computation, and then if it succeeds, it is rolled back to the state before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, that's a bug in explain
. It shouldn't do that for Result
!
@@ -4,7 +4,7 @@ | |||
{-# LANGUAGE FlexibleContexts #-} | |||
{-# LANGUAGE FlexibleInstances #-} | |||
{-# LANGUAGE GADTs #-} | |||
{-# LANGUAGE ImportQualifiedPost #-} | |||
-- {-# LANGUAGE ImportQualifiedPost #-} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-- {-# LANGUAGE ImportQualifiedPost #-} |
Improved the error messages when a Spec fails.
The function explainSpec no longer drops explanations.
Added guardTypeSpec, which catches errors earlier, so there is less noise.
Function symbols like subset_ and member_ print more compactly.
Description
Checklist
CHANGELOG.md
for the affected packages.New section is never added with the code changes. (See RELEASING.md)
.cabal
andCHANGELOG.md
files according to theversioning process.
.cabal
files for all affected packages are updated.If you change the bounds in a cabal file, that package itself must have a version increase. (See RELEASING.md)
fourmolu
(usescripts/fourmolize.sh
)scripts/cabal-format.sh
)hie.yaml
has been updated (usescripts/gen-hie.sh
)