Skip to content

Commit

Permalink
Merge pull request #4300 from IntersectMBO/PR-assert-hotfix
Browse files Browse the repository at this point in the history
`constrained-generators`: hotfix of latest derp...
  • Loading branch information
MaximilianAlgehed committed Apr 25, 2024
2 parents 2f0ad36 + f01af51 commit a2d7a81
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
23 changes: 20 additions & 3 deletions libs/constrained-generators/src/Constrained/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,15 @@ linearize ::
(MonadGenError m, BaseUniverse fn) => [Pred fn] -> DependGraph fn -> m [(Name fn, [Pred fn])]
linearize preds graph = do
sorted <- case topsort graph of
Left cycle -> fatalError ["dependency cycle in graph", show cycle, show graph]
Left cycle ->
fatalError $
[ "linearize: Dependency cycle in graph: "
, " cycle: " ++ show cycle
, " preds:"
]
++ [show $ indent 4 (pretty p) | p <- preds]
++ [ " graph: " ++ show graph
]
Right sorted -> pure sorted
go sorted [(freeVarSet ps, ps) | ps <- filter isRelevantPred preds]
where
Expand All @@ -995,7 +1003,12 @@ linearize preds graph = do
then pure []
else genError ["Linearize const False"]
| otherwise =
fatalError $ "Dependency error in `linearize`: " : show graph : map (mappend " " . show) ps
fatalError $
[ "Dependency error in `linearize`: "
, " graph: " ++ show graph
, " the following left-over constraints are not defining constraints for a unique variable: "
]
++ [show $ indent 4 (pretty p) | (_, p) <- ps]
go (n : ns) ps = do
let (nps, ops) = partition (isLastVariable n . fst) ps
((n, map snd nps) :) <$> go ns ops
Expand Down Expand Up @@ -3783,7 +3796,11 @@ reify t f body =
]

assertReified :: HasSpec fn a => Term fn a -> (a -> Bool) -> Pred fn
assertReified = reifies (lit True)
-- Note, it is necessary to introduce the extra variable from the `exists` here
-- to make things like `assertRealMultiple` work, if you don't have it then the
-- `reifies` isn't a defining constraint for anything any more.
assertReified t f =
reify t f assert

reifies :: (HasSpec fn a, HasSpec fn b) => Term fn b -> Term fn a -> (a -> b) -> Pred fn
reifies = Reifies
Expand Down
7 changes: 7 additions & 0 deletions libs/constrained-generators/src/Constrained/Examples/Basic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,10 @@ assertReal = constrained $ \x ->
[ assert $ x <=. 10
, assertReified x (<= 10)
]

assertRealMultiple :: Specification BaseFn (Int, Int)
assertRealMultiple = constrained' $ \x y ->
[ assert $ x <=. 10
, assert $ 11 <=. y
, assertReified (pair_ x y) $ uncurry (/=)
]
1 change: 1 addition & 0 deletions libs/constrained-generators/test/Constrained/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ tests :: Spec
tests =
describe "constrained" $ do
testSpec "assertReal" assertReal
testSpec "assertRealMultiple" assertRealMultiple
testSpec "setSpec" setSpec
testSpec "leqPair" leqPair
testSpec "setPair" setPair
Expand Down

0 comments on commit a2d7a81

Please sign in to comment.