haskell - QuickCheck test randomly hangs -


i'm new haskell. i'm playing quickcheck tests, trying test simple function calculatestrengthsingle (see source of testee below)

# fighter.hs module fighter ( quantity(quantity) , fighter(teamplayer, lonewolf, polymorph) , strength(strength) , calculatestrengthsingle)  import system.random  data strength = strength double  instance eq strength     strength s1 == strength s2 =         s1 == s2  instance ord strength     strength s1 < strength s2 =         s1 < s2  data quantity = quantity int deriving (show)  instance random quantity     randomr (quantity lo, quantity hi) g =         let rand = randomr (lo, hi) g             (r, g1) = rand         in (quantity r, g1)     random g =         let rand = random g             (r, g1) = rand         in (quantity r, g1)  data fighter = teamplayer              | lonewolf              | polymorph     deriving (show)   calculatestrengthsingle :: quantity -> fighter -> strength calculatestrengthsingle (quantity number) teamplayer =     strength(log (fromintegral number)) calculatestrengthsingle _ _ = strength 0.0 

the test looks this

# testfighter.hs import qualified test.quickcheck quickcheck import fighter  prop_strengthpositive quantity fighter =     fighter.calculatestrengthsingle quantity fighter >= strength 0.0  instance quickcheck.arbitrary fighter.fighter     arbitrary = quickcheck.oneof([return fighter.teamplayer, return fighter.lonewolf, return fighter.polymorph])  instance quickcheck.arbitrary fighter.quantity     arbitrary = quickcheck.choose(fighter.quantity 1, fighter.quantity 10)  main :: io() main =     quickcheck.quickcheck prop_strengthpositive 

when runhaskell testfighter.hs there's output (1 test) (the number changing, it's 0 other times it's 4) , cpu 100% loaded. nothing happens minute or so. when interrupt program ctrl+c, spits out

^c*** failed! exception: 'user interrupt' (after 1 test):   quantity 2 teamplayer 

questions:

  1. where did mess up?
  2. how can debug cases of infinite calculation, one?

you haven't defined ord instance strength correctly. need define <= , not <.

with < defined function <= enters infinite loop defined in terms of compare , compare defined in terms of <=. minimal definition needs define either compare or <=.

here fixed code ord instance

instance ord strength     strength s1 <= strength s2 =         s1 <= s2 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -