
On 2009 Feb 24, at 20:32, Patrick LeBoutillier wrote:
I'm having problems implementing the equivalent of this function in haskell. Inside a do block, is there a way to terminate the function immediately and return a result ("return" in the imperative sense, not
Take a look at MonadCont. But cleaner is to use Maybe (or MaybeT from Hackage):
-- you don't need to define this, it's in the Prelude instance Monad Maybe where return = Just Nothing >>= _ = Nothing (Just x) >>= f = f x
So if a test produces Nothing, you short-circuit past the remaining tests in the (>>=)-chain. But since you need to propagate the TAP state even when you are given Nothing, you want to wrap the Maybe in a StateT (and since you have IO at the bottom, you need MaybeT from Hackage):
type TAP a = StateT TAPState (MaybeT IO a)
withTAPplan myPlan $ do qTAP propertyOne -- Test.QuickCheck.quickCheck sTAP propertyTwo -- Test.SmallCheck.test sTAPToDepth 5 property3 -- Test.SmallCheck.smallCheck -- need wrappers because e.g. quickCheck won't expect a TAPState
BTW, are you integrating this with QuickCheck and/or SmallCheck? It might be nice to have passed in
-- or you could define (>>=) in your monad to DTRT
Also, in Haskell it is preferable to stay pure, perhaps especially while testing, so I would ditch the IO in the default case and provide a secondary function for tests that require IO. (hm, I sure hope I got this right, since my sinuses are trying to squeeze my brain out my ears...) -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH