Skip to content

Commit

Permalink
Merge pull request #4298 from IntersectMBO/PR-assertReified
Browse files Browse the repository at this point in the history
`constrained-generators`: utility function for asserting over a reified value
  • Loading branch information
MaximilianAlgehed committed Apr 25, 2024
2 parents 53cd7f9 + 914143b commit 2f0ad36
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions libs/constrained-generators/src/Constrained.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ module Constrained (
isJust,
reify,
reify',
reifies,
assertReified,
genHint,
dependsOn,
forAll,
Expand Down
7 changes: 7 additions & 0 deletions libs/constrained-generators/src/Constrained/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,8 +1034,12 @@ computeSpecSimplified x p = fromGE ErrorSpec $ case p of
Case t branches ->
let branchSpecs = mapList computeSpecBinderSimplified branches
in propagateSpec (caseSpec branchSpecs) <$> toCtx x t
Reifies (Lit a) (Lit val) f
| f val == a -> pure TrueSpec
| otherwise -> genError ["Value does not reify to literal: " ++ show val ++ " -/> " ++ show a]
Reifies t' (Lit val) f ->
propagateSpec (equalSpec (f val)) <$> toCtx x t'
Reifies Lit {} _ _ -> pure TrueSpec
-- Impossible cases that should be ruled out by the dependency analysis and linearizer
DependsOn {} -> fatalError ["The impossible happened in computeSpec: DependsOn"]
Reifies {} -> fatalError ["The impossible happened in computeSpec: Reifies", show x, show p]
Expand Down Expand Up @@ -3778,6 +3782,9 @@ reify t f body =
, toPred $ body x
]

assertReified :: HasSpec fn a => Term fn a -> (a -> Bool) -> Pred fn
assertReified = reifies (lit True)

reifies :: (HasSpec fn a, HasSpec fn b) => Term fn b -> Term fn a -> (a -> b) -> Pred fn
reifies = Reifies

Expand Down
6 changes: 6 additions & 0 deletions libs/constrained-generators/src/Constrained/Examples/Basic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,9 @@ basicSpec = constrained $ \x ->
unsafeExists $ \y ->
satisfies x $ constrained $ \x' ->
x' <=. 1 + y

assertReal :: Specification BaseFn Int
assertReal = constrained $ \x ->
[ assert $ x <=. 10
, assertReified x (<= 10)
]
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 @@ -44,6 +44,7 @@ testAll = hspec tests
tests :: Spec
tests =
describe "constrained" $ do
testSpec "assertReal" assertReal
testSpec "setSpec" setSpec
testSpec "leqPair" leqPair
testSpec "setPair" setPair
Expand Down

0 comments on commit 2f0ad36

Please sign in to comment.