[Renamed function' to transform and changed the way it is used. Nils Anders Danielsson **20050623145443] { hunk ./Test/ChasingBottoms/ContinuousFunctions.hs 3 --- TODO: Rename function'. hunk ./Test/ChasingBottoms/ContinuousFunctions.hs 79 --- 'function''. +-- 'transform'. hunk ./Test/ChasingBottoms/ContinuousFunctions.hs 94 --- > tree pms size +-- > tree pms size = transform (tree' size) pms +-- > tree' size pms hunk ./Test/ChasingBottoms/ContinuousFunctions.hs 99 --- > tree' = function' pms (flip tree (size `div` 2)) +-- > tree' = tree pms (size `div` 2) hunk ./Test/ChasingBottoms/ContinuousFunctions.hs 103 --- > , (2, liftM Leaf (function' pms makeResult)) +-- > , (2, liftM Leaf (makeResult pms)) hunk ./Test/ChasingBottoms/ContinuousFunctions.hs 106 --- Note the use of 'function''. To use this function to generate +-- Note the use of 'transform'. To use this function to generate hunk ./Test/ChasingBottoms/ContinuousFunctions.hs 122 - , function' + , transform hunk ./Test/ChasingBottoms/ContinuousFunctions.hs 166 --- | A @'MakeResult' a@ should be implemented as other generators for --- the type @a@, with the following difference: Recursive calls to --- generators (typically for each new constructor) should be made via --- 'function'', which takes the 'PatternMatches' and a 'MakeResult' as --- parameters and modifies the generated result. +-- | A @'MakeResult' a@ should be implemented almost as other generators for +-- the type @a@, with the difference that 'transform' should be +-- used wherever the resulting function should be allowed to pattern +-- match (typically for each constructor emitted). See example above. +-- +-- The 'PatternMatches' are currently passed around manually. A reader +-- monad could be wrapped around the 'Gen' monad, but that will not be +-- convenient unless some framework is built up to support the use of +-- the new monad. hunk ./Test/ChasingBottoms/ContinuousFunctions.hs 181 --- 'PatternMatches' except for passing it along to 'function''. +-- 'PatternMatches' except for passing it along to 'transform'. hunk ./Test/ChasingBottoms/ContinuousFunctions.hs 200 - promote $ \a -> - function' (PMs $ singleton $ makePM a) makeResult + promote $ \a -> makeResult (PMs $ singleton $ makePM a) hunk ./Test/ChasingBottoms/ContinuousFunctions.hs 202 --- | 'function'' makes sure that the pattern matches get to influence +-- | 'transform' makes sure that the pattern matches get to influence hunk ./Test/ChasingBottoms/ContinuousFunctions.hs 205 -function' :: PatternMatches -> MakeResult a -> Gen a -function' pms makeResult = do - (GenT transform, keep) <- getMatches (unPMs pms) - transform (makeResult (PMs keep)) +transform :: MakeResult a -> MakeResult a +transform makeResult pms = do + (GenT trans, keep) <- getMatches (unPMs pms) + trans (makeResult (PMs keep)) hunk ./Test/ChasingBottoms/ContinuousFunctions.hs 260 -flat _ = +flat = transform $ \_ -> hunk ./Test/ChasingBottoms/ContinuousFunctions.hs 270 - list pms size + list pms size = transform (list' size) pms + list' size pms hunk ./Test/ChasingBottoms/ContinuousFunctions.hs 275 - , (9, liftM2 (:) (function' pms makeResult) - (function' pms (flip list (size - 1))) + , (9, liftM2 (:) (makeResult pms) + (list pms (size - 1)) hunk ./Test/ChasingBottoms/ContinuousFunctions.hs 289 -infiniteListOf makeResult = \pms -> - liftM2 (:) (function' pms makeResult) - (function' pms (infiniteListOf makeResult)) +infiniteListOf makeResult = transform $ \pms -> + liftM2 (:) (makeResult pms) + (infiniteListOf makeResult pms) hunk ./Test/ChasingBottoms/ContinuousFunctions.hs 296 -listOf makeResult = \pms -> oneof [ finiteListOf makeResult pms - , infiniteListOf makeResult pms - ] + -- Not really necessary to have a transform here... +listOf makeResult = transform $ \pms -> + oneof [ finiteListOf makeResult pms + , infiniteListOf makeResult pms + ] hunk ./Test/ChasingBottoms/ContinuousFunctions.hs 314 -makeResult pms = frequency $ (1, return bottom) : others +makeResult = transform res hunk ./Test/ChasingBottoms/ContinuousFunctions.hs 316 - others = case dataTypeRep (dataTypeOf (undefined :: a)) of - AlgRep constrs -> - map (handle (L.genericLength constrs)) constrs - IntRep -> [(9, cast' (arbitrary :: Gen Integer))] - FloatRep -> [(9, cast' (arbitrary :: Gen Double))] - StringRep -> nonBottomError "makeResult: StringRep." - NoRep -> nonBottomError "makeResult: NoRep." + res pms = frequency $ (1, return bottom) : others + where + others = case dataTypeRep (dataTypeOf (undefined :: a)) of + AlgRep constrs -> + map (handle (L.genericLength constrs)) constrs + IntRep -> [(9, cast' (arbitrary :: Gen Integer))] + FloatRep -> [(9, cast' (arbitrary :: Gen Double))] + StringRep -> nonBottomError "makeResult: StringRep." + NoRep -> nonBottomError "makeResult: NoRep." hunk ./Test/ChasingBottoms/ContinuousFunctions.hs 326 - handle noConstrs con = - (freq, fromConstrM (function' pms makeResult) con :: Gen a) - where noArgs = glength (fromConstr con :: a) - -- Aim for at most 10% bottoms (on average). - freq = 1 `max` ceiling (9 / noConstrs) + handle noConstrs con = + (freq, fromConstrM (makeResult pms) con :: Gen a) + where noArgs = glength (fromConstr con :: a) + -- Aim for at most 10% bottoms (on average). + freq = 1 `max` ceiling (9 / noConstrs) hunk ./Test/ChasingBottoms/ContinuousFunctions.hs 332 - cast' gen = flip fmap gen $ \x -> case cast x of - Just x' -> x' - Nothing -> nonBottomError $ - "makeResult: Cannot handle Int and Float." ++ - " Use Integer or Double instead." + cast' gen = flip fmap gen $ \x -> case cast x of + Just x' -> x' + Nothing -> nonBottomError $ + "makeResult: Cannot handle Int and Float." ++ + " Use Integer or Double instead." hunk ./Test/ChasingBottoms/ContinuousFunctions/Tests.hs 28 - tree pms size + tree pms size = transform (tree' size) pms + tree' size pms hunk ./Test/ChasingBottoms/ContinuousFunctions/Tests.hs 33 - tree' = function' pms (flip tree (size `div` 2)) + tree' = tree pms (size `div` 2) hunk ./Test/ChasingBottoms/ContinuousFunctions/Tests.hs 37 - , (2, liftM Leaf (function' pms makeResult)) + , (2, liftM Leaf (makeResult pms)) }