Skip to content

Commit 289bceb

Browse files
committed
abstract fn
1 parent a497d34 commit 289bceb

1 file changed

Lines changed: 18 additions & 13 deletions

File tree

src/Language/Fortran/Generate.hs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,24 @@ instance Arbitrary BaseType where
5656
]
5757

5858
instance Arbitrary a => Arbitrary (TypeSpec a) where
59-
arbitrary = do
60-
annotation <- arbitrary
61-
base <- arbitrary
62-
selector <-
63-
case base of
64-
TypeReal -> do
65-
-- For real types, we can optionally include a kind selector.
66-
kind :: Integer <- elements [4,8]
67-
let kindExpr = ExpValue annotation nullSpan (ValInteger (show kind) Nothing)
68-
return $ Just $ Selector annotation nullSpan Nothing (Just kindExpr)
69-
-- No selector
70-
_ -> return Nothing
71-
pure $ TypeSpec annotation nullSpan base selector
59+
arbitrary = arbitrary >>= genTypeSpecOfBase
60+
61+
-- | Generate a 'TypeSpec' with a specific 'BaseType' (e.g. so that an
62+
-- operator's operand type can be pinned to what it requires), filling in
63+
-- the rest (annotation, kind selector) arbitrarily.
64+
genTypeSpecOfBase :: Arbitrary a => BaseType -> Gen (TypeSpec a)
65+
genTypeSpecOfBase base = do
66+
annotation <- arbitrary
67+
selector <-
68+
case base of
69+
TypeReal -> do
70+
-- For real types, we can optionally include a kind selector.
71+
kind :: Integer <- elements [4,8]
72+
let kindExpr = ExpValue annotation nullSpan (ValInteger (show kind) Nothing)
73+
return $ Just $ Selector annotation nullSpan Nothing (Just kindExpr)
74+
-- No selector
75+
_ -> return Nothing
76+
pure $ TypeSpec annotation nullSpan base selector
7277

7378
maybeWrapper :: Gen a -> Gen (Maybe a)
7479
maybeWrapper gen = oneof [pure Nothing, Just <$> gen]

0 commit comments

Comments
 (0)