Skip to content

Instantly share code, notes, and snippets.

expression partial_eval(env const& env, expression const& e)
{
return cata<expression>(
[&env](expression_r<expression> const& e) -> expression {
return optimize_alg(partial_eval_alg(env)(e).get());
},
e);
}
expression e = add({
cst(1),
cst(2),
mul({cst(0), var("x"), var("y")}),
mul({cst(1), var("y"), cst(2)}),
add({cst(0), var("x")})
});
//Initial expression
std::cout << cata<std::string>(print_alg, e) << '\n';
expression e = add({
cst(1),
cst(2),
mul({cst(0), var("x"), var("y")}),
mul({cst(1), var("y"), cst(2)}),
add({cst(0), var("x")})
});
//Environment of evaluation
env full_env = {{"x", 1}, {"y", 2}};
prop_gcd :: Integer -> Integer -> Bool
prop_gcd a b = a * b == gcd a b * lcm a b
rapidCheck prop_gcd
> Success
prop_gcd_bad :: Integer -> Integer -> Bool
prop_gcd_bad a b = gcd a b > 1
rapidCheck prop_gcd_bad
> Failure {seed = -1437169021,
counterExample = ["1076253199","40866101"]}
newtype Gen a = Gen {
runGen :: StdGen -> a
}
class Arbitrary a where
arbitrary :: Gen a
data Result
= Success -- In case of success, no additional information
| Failure {
seed :: Int, -- The seed used to generate the counter example
counterExample :: [String] -- The counter example (failing inputs to string)
} deriving (Show, Eq, Ord) -- Useful instances to print and compare Results
instance Monoid Result where
mempty = Success
mappend lhs@Failure{} _ = lhs
mappend _ rhs = rhs
newtype Property = Property {
getGen :: Gen Result
}
-- Property is trivially convertible to Property
instance Testable Property where
property = id
-- Result is convertible to Property by creating
-- a generator that always return this same result
instance Testable Result where
property r = Property (Gen (const r))
-- Bool can be converted to Result