On Dec 4, 2007 11:51 AM, Don Stewart <[EMAIL PROTECTED]> wrote:
> Awesome. We can use this in Data.Bits, if you've got some QuickChecks
> for it.

Hear hear.  But is there any way to just make the compiler use
fastTestBit in place of testBit :: (Bits a) => a -> Int -> Bool when a
= Integer?  (That is, without having to introduce a new function to
the public interface of Data.Bits.)  Some kind of SPECIALIZE pragma,
perhaps?

I've attached a program with two QuickCheck properties.  Unfortunately
they fail on negative Integers.  I can't figure out why.
{-# OPTIONS_GHC -fglasgow-exts #-}

module Main where

import Test.QuickCheck
import Data.Bits
import GHC.Exts

fastTestBit :: Integer -> Int -> Bool
fastTestBit n i = case n of
   S# m -> testBit (I# m) i
   J# l d -> let I# w = shiftR i 6
                 b = i .&. 63
             in testBit (I# (indexIntArray# d w)) b

prop_testBit1 :: Integer -> Int -> Bool
prop_testBit1 i n = testBit i n == fastTestBit i n

-- Test fastTestBit on large Integers
prop_testBit2 :: Integer -> Int -> Int -> Bool
prop_testBit2 i j n = testBit k n == fastTestBit k n where k = i ^ (abs j)

main = do
  putStrLn "Testing prop_testBit1:"
  quickCheck prop_testBit1
  putStrLn "Testing prop_testBit2:"
  quickCheck prop_testBit2
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to