|
| 1 | +module Utils.ArbitrarySetMap |
| 2 | + ( |
| 3 | + -- MonadGen |
| 4 | + MonadGen(..) |
| 5 | + |
| 6 | + -- Set |
| 7 | + , mkArbSet |
| 8 | + , setFromList |
| 9 | + |
| 10 | + -- Map |
| 11 | + , mkArbMap |
| 12 | + , mapFromKeysList |
| 13 | + ) where |
| 14 | + |
| 15 | +import Control.Monad (liftM, liftM3, liftM4) |
| 16 | +import Control.Monad.Trans.State.Strict |
| 17 | +import Control.Monad.Trans.Class |
| 18 | +import Test.QuickCheck |
| 19 | + |
| 20 | +import Data.Set (Set) |
| 21 | +import qualified Data.Set.Internal as S |
| 22 | +import Data.Map (Map) |
| 23 | +import qualified Data.Map.Internal as M |
| 24 | + |
| 25 | +{-------------------------------------------------------------------- |
| 26 | + MonadGen |
| 27 | +--------------------------------------------------------------------} |
| 28 | + |
| 29 | +class Monad m => MonadGen m where |
| 30 | + liftGen :: Gen a -> m a |
| 31 | +instance MonadGen Gen where |
| 32 | + liftGen = id |
| 33 | +instance MonadGen m => MonadGen (StateT s m) where |
| 34 | + liftGen = lift . liftGen |
| 35 | + |
| 36 | +{-------------------------------------------------------------------- |
| 37 | + Set |
| 38 | +--------------------------------------------------------------------} |
| 39 | + |
| 40 | +-- | Given an action that produces successively larger elements and |
| 41 | +-- a size, produce a set of arbitrary shape with exactly that size. |
| 42 | +mkArbSet :: MonadGen m => m a -> Int -> m (Set a) |
| 43 | +mkArbSet step n |
| 44 | + | n <= 0 = return S.Tip |
| 45 | + | n == 1 = S.singleton `liftM` step |
| 46 | + | n == 2 = do |
| 47 | + dir <- liftGen arbitrary |
| 48 | + p <- step |
| 49 | + q <- step |
| 50 | + if dir |
| 51 | + then return (S.Bin 2 q (S.singleton p) S.Tip) |
| 52 | + else return (S.Bin 2 p S.Tip (S.singleton q)) |
| 53 | + | otherwise = do |
| 54 | + -- This assumes a balance factor of delta = 3 |
| 55 | + let upper = (3*(n - 1)) `quot` 4 |
| 56 | + let lower = (n + 2) `quot` 4 |
| 57 | + ln <- liftGen $ choose (lower, upper) |
| 58 | + let rn = n - ln - 1 |
| 59 | + liftM3 |
| 60 | + (\lt x rt -> S.Bin n x lt rt) |
| 61 | + (mkArbSet step ln) |
| 62 | + step |
| 63 | + (mkArbSet step rn) |
| 64 | +{-# INLINABLE mkArbSet #-} |
| 65 | + |
| 66 | +-- | Given a strictly increasing list of elements, produce an arbitrarily |
| 67 | +-- shaped set with exactly those elements. |
| 68 | +setFromList :: [a] -> Gen (Set a) |
| 69 | +setFromList xs = flip evalStateT xs $ mkArbSet step (length xs) |
| 70 | + where |
| 71 | + step = do |
| 72 | + xxs <- get |
| 73 | + case xxs of |
| 74 | + x : xs -> do |
| 75 | + put xs |
| 76 | + pure x |
| 77 | + [] -> error "setFromList" |
| 78 | + |
| 79 | +{-------------------------------------------------------------------- |
| 80 | + Map |
| 81 | +--------------------------------------------------------------------} |
| 82 | + |
| 83 | +-- | Given an action that produces successively larger keys and |
| 84 | +-- a size, produce a map of arbitrary shape with exactly that size. |
| 85 | +mkArbMap :: (MonadGen m, Arbitrary v) => m k -> Int -> m (Map k v) |
| 86 | +mkArbMap step n |
| 87 | + | n <= 0 = return M.Tip |
| 88 | + | n == 1 = do |
| 89 | + k <- step |
| 90 | + v <- liftGen arbitrary |
| 91 | + return (M.singleton k v) |
| 92 | + | n == 2 = do |
| 93 | + dir <- liftGen arbitrary |
| 94 | + p <- step |
| 95 | + q <- step |
| 96 | + vOuter <- liftGen arbitrary |
| 97 | + vInner <- liftGen arbitrary |
| 98 | + if dir |
| 99 | + then return (M.Bin 2 q vOuter (M.singleton p vInner) M.Tip) |
| 100 | + else return (M.Bin 2 p vOuter M.Tip (M.singleton q vInner)) |
| 101 | + | otherwise = do |
| 102 | + -- This assumes a balance factor of delta = 3 |
| 103 | + let upper = (3*(n - 1)) `quot` 4 |
| 104 | + let lower = (n + 2) `quot` 4 |
| 105 | + ln <- liftGen $ choose (lower, upper) |
| 106 | + let rn = n - ln - 1 |
| 107 | + liftM4 |
| 108 | + (\lt x v rt -> M.Bin n x v lt rt) |
| 109 | + (mkArbMap step ln) |
| 110 | + step |
| 111 | + (liftGen arbitrary) |
| 112 | + (mkArbMap step rn) |
| 113 | +{-# INLINABLE mkArbMap #-} |
| 114 | + |
| 115 | +-- | Given a strictly increasing list of keys, produce an arbitrarily |
| 116 | +-- shaped map with exactly those keys. |
| 117 | +mapFromKeysList :: Arbitrary a => [k] -> Gen (Map k a) |
| 118 | +mapFromKeysList xs = flip evalStateT xs $ mkArbMap step (length xs) |
| 119 | + where |
| 120 | + step = do |
| 121 | + xxs <- get |
| 122 | + case xxs of |
| 123 | + x : xs -> do |
| 124 | + put xs |
| 125 | + pure x |
| 126 | + [] -> error "mapFromKeysList" |
| 127 | +{-# INLINABLE mapFromKeysList #-} |
0 commit comments