extrapolate-0.3.3: generalize counter-examples of test properties

Copyright(c) 2017 Rudy Matela
License3-Clause BSD (see the file LICENSE)
MaintainerRudy Matela <rudy@matela.com.br>
Safe HaskellNone
LanguageHaskell2010

Test.Extrapolate.Exprs

Description

This module is part of Extrapolate, a library for generalization of counter-examples.

This module re-exports some functionality from Test.Speculate.Expr, but instead of working on single expressions it works in lists of expressions (the choosen representation for counter-examples).

Synopsis

Documentation

type Exprs = [Expr] #

grounds :: Instances -> [Expr] -> [[Expr]] #

vassignments :: [Expr] -> [[Expr]] #

vars :: [Expr] -> [(TypeRep, String)] #

fold :: [Expr] -> Expr #

unfold :: Expr -> [Expr] #

type TypeRep = SomeTypeRep #

A quantified type representation.

false :: Instances -> Int -> Expr -> Bool #

Is an expression ALWAYS false? This is *NOT* the same as not true

trueRatio :: Instances -> Int -> Expr -> Ratio Int #

Under a maximum number of tests, returns the ratio for which an expression holds true.

trueBinds :: Instances -> Int -> Expr -> [Binds] #

List variable bindings for which an expression holds true.

true :: Instances -> Int -> Expr -> Bool #

Is a boolean expression true for all variable assignments?

inequal :: Instances -> Int -> Expr -> Expr -> Bool #

Are two expressions inequal for *all* variable assignments? Note this is different than not . equal.

less :: Instances -> Int -> Expr -> Expr -> Bool #

Are two expressions less-than for a given number of tests?

lessOrEqual :: Instances -> Int -> Expr -> Expr -> Bool #

Are two expressions less-than-or-equal for a given number of tests?

condEqualM :: Instances -> Int -> Int -> Expr -> Expr -> Expr -> Bool #

Are two expressions equal under a given condition for a given number of tests and a minimum amount of tests

condEqual :: Instances -> Int -> Expr -> Expr -> Expr -> Bool #

Are two expressions equal under a given condition for a given number of tests?

equal :: Instances -> Int -> Expr -> Expr -> Bool #

Are two expressions equal for a given number of tests?

groundAndBinds :: Instances -> Expr -> [(Binds, Expr)] #

List all possible variable bindings and valuations to an expression

groundAndBinds ti e == zipWith (,) (grounds ti e) (groundBinds ti e)

groundBinds :: Instances -> Expr -> [Binds] #

List all possible variable bindings to an expression

take 3 $ groundBinds preludeInstances ((x + x) + y)
  == [ [("x",0),("y",0)]
     , [("x",0),("y",1)]
     , [("x",1),("y",0)] ]

uselessEquation :: Expr -> Bool #

Given an equation encoded as an Expr. Checks if both sides of an equation are the same. If the Expr is not an equation, this raises an error.

listableWith :: (Typeable a, Show a) => [[a]] -> Instances #

ordWith :: (Typeable a, Ord a) => (a -> a -> Bool) -> Instances #

eqWith :: (Typeable a, Eq a) => (a -> a -> Bool) -> Instances #

listable :: (Typeable a, Show a, Listable a) => a -> Instances #

eqOrd :: (Typeable a, Eq a, Ord a) => a -> Instances #

ord :: (Typeable a, Ord a) => a -> Instances #

eq :: (Typeable a, Eq a) => a -> Instances #

data Instance #

Type information needed to Speculate expressions (single type / single class).

Constructors

Instance String TypeRep [Expr] 

type Instances = [Instance] #

Type information needed to Speculate expressions.

matchWith :: Binds -> Expr -> Expr -> Maybe Binds #

List matches with preexisting bindings:

0 + 1 `matchWith [(x,0)]` x + y = Just [x=0, y=1]
0 + 1 `matchWith [(x,1)]` x + y = Nothing

match2 :: (Expr, Expr) -> (Expr, Expr) -> Maybe Binds #

List matches of pairs of expressions if possible

(0,1)   `match2` (x,y)   = Just [x=0, y=1]
(0,1+2) `match2` (x,y+y) = Nothing

match :: Expr -> Expr -> Maybe Binds #

List matches if possible

0 + 1       `match` x + y       = Just [x=0, y=1]
0 + (1 + 2) `match` x + y       = Just [x=0, y=1 + 2]
0 + (1 + 2) `match` x + (y + y) = Nothing
(x + x) + (1 + 2) `match` x + (y + y) = Nothing

renameBy :: (String -> String) -> Expr -> Expr #

Primeify variable names in an expression.

renameBy (++ "'") (x + y) = (x' + y')
renameBy (++ "'") (y + (z + x)) = (y' + (z' + x'))
renameBy (++ "1") abs x = abs x1
renameBy (++ "2") abs (x + y) = abs (x2 + y2)

Note this will affect holes!

sub :: Expr -> Expr -> Expr -> Expr #

Substitute matching subexpressios.

sub (x + y) 0 ((x + y) + z) == (0 + z) sub (x + y) 0 (x + (y + z)) == (x + (y + z))

assigning :: Expr -> Binds -> Expr #

Assign all occurrences of several variables in an expression.

For single variables, this works as assign:

x + y `assigning` [("x",10)] = (10 + y)
((x + y) + (y + z)) `assigning` [("y",y+z)] = (x + (y + z)) + ((y + z) + z)

Note this is not equivalent to foldr (uncurry assign). Variables inside expressions being assigned will not be assigned.

assign :: String -> Expr -> Expr -> Expr #

Assign all occurences of a variable in an expression.

Examples in pseudo-Haskell:

assign "x" (10) (x + y) = (10 + y)
assign "y" (y + z) ((x + y) + (y + z)) = (x + (y + z)) + ((y + z) + z)

This respects the type (won't change occurrences of a similarly named variable of a different type).

fill :: Expr -> [Expr] -> Expr #

Fill holes in an expression. Silently skips holes that are not of the right type. Silently discard remaining expressions.

type Binds = [(String, Expr)] #

unfoldApp :: Expr -> [Expr] #

Unfold function application:

(((f :$ e1) :$ e2) :$ e3) = [f,e1,e2,e3]

isSub :: Expr -> Expr -> Bool #

Is a subexpression of.

subexprsV :: Expr -> [Expr] #

Sub-expressions of an expression including variables and the expression itself.

subexprs :: Expr -> [Expr] #

Non-variable sub-expressions of an expression

This includes the expression itself

countVar :: TypeRep -> String -> Expr -> Int #

Number of occurrences of a given variable name. In term rewriting terms: |s|_x

countHoles :: TypeRep -> Expr -> Int #

Number of occurrences of holes with a given type.

depthE :: Expr -> Int #

Returns the maximum depth of an expression.

lengthE :: Expr -> Int #

Returns the length of an expression. In term rewriting terms: |s|

consts :: Expr -> [Expr] #

List terminal constants in an expression. This does not repeat values.

holes :: Expr -> [TypeRep] #

List types holes (unamed variables) in an expression

arity :: Expr -> Int #

Type arity of an Expr

etyp :: Expr -> Either Expr TypeRep #

etyp returns either: the Right type a Left expression with holes with the structure of the I'll typed expression

typ :: Expr -> TypeRep #

The type of an expression. This raises errors, but those should not happen if expressions are smart-constructed.

eval :: Typeable a => a -> Expr -> a #

Evaluates an expression when possible (correct type, no holes). Returns a default value otherwise.

evaluate :: Typeable a => Expr -> Maybe a #

Just the value of an expression when possible (correct type, no holes), Nothing otherwise.

compareComplexity :: Expr -> Expr -> Ordering #

Compares two expressions first by their complexity: 1st length; 2nd number of variables (more variables is less complex); 3nd sum of number of variable occurrences; 4th their depth; 5th lexicompare.

compareComplexityThen :: (Expr -> Expr -> Ordering) -> Expr -> Expr -> Ordering #

Compares two expressions first by their complexity: 1st length; 2nd number of variables (more variables is less complex); 3nd sum of number of variable occurrences; 4th their depth; 5th normal compare.

lexicompare :: Expr -> Expr -> Ordering #

Compare two expressiosn lexicographically

1st their type arity; 2nd their type; 3rd var < constants < apps 4th lexicographic order on names

($$) :: Expr -> Expr -> Maybe Expr #

Just an Expr application if the types match, Nothing otherwise.

hole :: (Listable a, Typeable a) => a -> Expr #

(intended for advanced users)

hole (undefined :: Ty) returns a hole of type Ty

By convention, a Hole is a variable named with the empty string.

var :: (Listable a, Typeable a) => String -> a -> Expr #

var "x" (undefined :: Ty) returns a variable of type Ty named "x"

showConstant :: (Typeable a, Show a) => a -> Expr #

A shorthand for constant to be used on values that are Show instances. Examples:

showConstant 0     =  constant "0" 0
showConstant 'a'   =  constant "'a'" 'a' 
showConstant True  =  constant "True" True

constant :: Typeable a => String -> a -> Expr #

Encode a constant Haskell expression for use by Speculate. It takes a string representation of a value and a value, returning an Expr. Examples:

constant "0" 0
constant "'a'" 'a'
constant "True" True
constant "id" (id :: Int -> Int)
constant "(+)" ((+) :: Int -> Int -> Int)
constant "sort" (sort :: [Bool] -> [Bool])

data Expr #

An encoded Haskell functional-application expression for use by Speculate.

Instances
Eq Expr 
Instance details

Defined in Test.Speculate.Expr.Core

Methods

(==) :: Expr -> Expr -> Bool #

(/=) :: Expr -> Expr -> Bool #

Ord Expr 
Instance details

Defined in Test.Speculate.Expr.Core

Methods

compare :: Expr -> Expr -> Ordering #

(<) :: Expr -> Expr -> Bool #

(<=) :: Expr -> Expr -> Bool #

(>) :: Expr -> Expr -> Bool #

(>=) :: Expr -> Expr -> Bool #

max :: Expr -> Expr -> Expr #

min :: Expr -> Expr -> Expr #

Show Expr 
Instance details

Defined in Test.Speculate.Expr.Core

Methods

showsPrec :: Int -> Expr -> ShowS #

show :: Expr -> String #

showList :: [Expr] -> ShowS #