This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
prop_gcd :: Integer -> Integer -> Bool | |
prop_gcd a b = a * b == gcd a b * lcm a b | |
rapidCheck prop_gcd | |
> Success |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
newtype Gen a = Gen { | |
runGen :: StdGen -> a | |
} | |
class Arbitrary a where | |
arbitrary :: Gen a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
instance Monoid Result where | |
mempty = Success | |
mappend lhs@Failure{} _ = lhs | |
mappend _ rhs = rhs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
newtype Property = Property { | |
getGen :: Gen Result | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |