Skip to content

Instantly share code, notes, and snippets.

joinTree :: Tree (Tree Result) -> Tree Result
joinTree (Tree (Tree innerArgResult innerArgShrinks) outerArgShrinks) =
Tree innerArgResult
(map joinTree outerArgShrinks ++ innerArgShrinks)
buildTree :: Shrink a -> a -> Tree a
buildTree shrinker = build where
build x = Tree x (map build (shrinker x))
addCounterExample :: (Show a) => a -> Tree Result -> Tree Result
addCounterExample arg = fmap (\r -> overFailure r addToFailure)
where addToFailure f = f { counterExample = show arg : counterExample f }
925396436 234647012
925436450 1835767207
0 1835767207
462718225 1835767207
0 1835767207
231359113 1835767207
...
4 1835767207
0 1835767207
2 1835767207
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE DeriveFunctor #-}
module RapidCheck where
import Data.List
import Data.Monoid((<>))
import System.Random
import Text.Show.Functions
let 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") ]
]
-- Pretty printing an expression
prn e
> "(+ 1 2 (* 0 x y) (* 1 y 2) (+ 0 x))"
genCst :: Gen Expr
genCst = fmap cst arbitrary
varNames :: [String]
varNames = [[v] | v <- ['a'..'z']]
genVar :: Gen Expr
genVar = fmap var (elements varNames)
genSimpleTerm :: Gen Expr
genSimpleTerm = oneof [genVar, genCst]
opsGen :: Gen Expr -> Int -> Gen Expr
opsGen simpleTermGen = go where
go n = do
m <- choose (0, n)
if m == 0
then simpleTermGen
else elements [add, mul] <*> replicateM m (go (div n (m + 1)))
genExpr :: Int -> Gen Expr
genExpr = opsGen genSimpleTerm
genCstExpr :: Int -> Gen Expr
genCstExpr = opsGen genCst
type Id = String -- Variable identifier
type Env = -- Environment of evaluation
Map Id Int
data OpType -- Types of operations
= Add -- * Addition
| Mul -- * Multiplication
deriving (Show, Eq, Ord)
data ExprR r -- Open recursive expression type