[Haskell-beginners] Help with TAP implemation in haskell

Brandon S. Allbery KF8NH allbery at ece.cmu.edu
Tue Feb 24 21:42:09 EST 2009

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  

 > -- 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  

 > type TAP a = StateT TAPState (MaybeT IO a)

BTW, are you integrating this with QuickCheck and/or SmallCheck?  It  
might be nice to have

 > 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  
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 at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH

-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/beginners/attachments/20090224/3b85e11e/PGP.bin

More information about the Beginners mailing list