haskell - How can I constraint QuickCheck parameters, e.g. only use non-negative ints? -


i'm new haskell. it's nice far, i'm running copy-pasting quickcheck properties, , i'd fix that.

here's made-up example:

prop_myfunc :: [int] -> (int,int) -> bool prop_myfunc ints (i,j) = ints !! == ints !! j 

this won't work because quickcheck generates negative numbers, get

*** failed! (after 2 tests , 2 shrinks):                                exception:   prelude.(!!): negative index 

i've tried google solutions this, , i've found e.g. nonnegative , ==>, don't understand how work.

how can restrict above example , j never negative? , also, neither high? is: 0 <= i,j < length ints

constraining wrappers (from test.quickcheck.modifiers, if aren't reexported implicitly) can used in way:

prop_myfunc :: [int] -> (nonnegative int, nonnegative int) -> bool prop_myfunc ints (nonnegative i, nonnegative j) = ints !! == ints !! j 

you can treat somewrapper a a modified distribution. example, nonnegative a ensures a >= 0. after wrapper generated, value can pattern-matching or explicit accessor (getnonnegative in case).

as constraining top margin of indices, think it's not possible wrappers (it's impossible in haskkell type system parameterise type value, list length in case). however, ==> operator can add arbitrary boolean constraint test:

prop_myfunc ints (nonnegative i, nonnegative j) = < l && j < l ==> ints !! == ints !! j     l = length ints 

it works in other way: when condition isn't true, discards current test case. careful: if there many thrown cases (the condition restrictive), test becomes less useful. „lossless“ behaviour can achieved shrinking test data, it's whole other topic.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -