1
1
mirror of https://github.com/github/semantic.git synced 2024-11-24 08:54:07 +03:00
semantic/semantic-core/test/Generators.hs

74 lines
2.2 KiB
Haskell
Raw Normal View History

2019-06-04 18:26:42 +03:00
{-# LANGUAGE ScopedTypeVariables #-}
module Generators
( literal
, name
, variable
, boolean
, lambda
2019-07-22 22:26:02 +03:00
, record
2019-06-04 18:26:42 +03:00
, apply
, ifthenelse
, expr
2019-06-04 18:26:42 +03:00
) where
import Prelude hiding (span)
import Hedgehog hiding (Var)
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
2019-07-22 22:13:14 +03:00
import qualified Data.Core as Core
2019-06-04 18:26:42 +03:00
import Data.Name
2019-07-17 19:44:27 +03:00
import Data.Term
2019-06-04 18:26:42 +03:00
-- The 'prune' call here ensures that we don't spend all our time just generating
-- fresh names for variables, since the length of variable names is not an
-- interesting property as they parse regardless.
2019-07-17 19:55:05 +03:00
name :: MonadGen m => m (Named User)
name = Gen.prune (named' <$> names) where
2019-06-25 21:21:36 +03:00
names = Gen.text (Range.linear 1 10) Gen.lower
2019-06-04 18:26:42 +03:00
2019-07-22 22:13:14 +03:00
boolean :: MonadGen m => m (Term Core.Core User)
boolean = Core.bool <$> Gen.bool
2019-06-04 18:26:42 +03:00
2019-07-22 22:13:14 +03:00
variable :: MonadGen m => m (Term Core.Core User)
2019-07-17 19:44:27 +03:00
variable = pure . namedValue <$> name
2019-06-04 18:26:42 +03:00
2019-07-22 22:13:14 +03:00
ifthenelse :: MonadGen m => m (Term Core.Core User) -> m (Term Core.Core User)
ifthenelse bod = Gen.subterm3 boolean bod bod Core.if'
2019-06-04 18:26:42 +03:00
2019-07-22 22:13:14 +03:00
apply :: MonadGen m => m (Term Core.Core User) -> m (Term Core.Core User)
2019-06-04 18:26:42 +03:00
apply gen = go where
go = Gen.recursive
Gen.choice
2019-07-22 22:13:14 +03:00
[ Gen.subterm2 gen gen (Core.$$)]
[ Gen.subterm2 go go (Core.$$) -- balanced
, Gen.subtermM go (\x -> Core.lam <$> name <*> pure x)
2019-06-04 18:26:42 +03:00
]
2019-07-22 22:13:14 +03:00
lambda :: MonadGen m => m (Term Core.Core User) -> m (Term Core.Core User)
2019-06-04 18:26:42 +03:00
lambda bod = do
arg <- name
2019-07-22 22:13:14 +03:00
Gen.subterm bod (Core.lam arg)
2019-06-04 18:26:42 +03:00
2019-07-22 22:26:02 +03:00
record :: MonadGen m => m (Term Core.Core User) -> m (Term Core.Core User)
record bod = Core.record <$> Gen.list (Range.linear 0 5) ((,) . namedValue <$> name <*> bod)
2019-07-22 22:13:14 +03:00
atoms :: MonadGen m => [m (Term Core.Core User)]
atoms = [boolean, variable, pure Core.unit]
2019-06-04 18:26:42 +03:00
2019-07-22 22:13:14 +03:00
literal :: MonadGen m => m (Term Core.Core User)
2019-07-22 22:26:02 +03:00
literal = Gen.recursive Gen.choice atoms [lambda literal, record literal]
expr :: MonadGen m => m (Term Core.Core User)
expr = Gen.recursive Gen.choice atoms
[ lambda expr
, record expr
, Gen.subterm2 expr expr (Core.$$)
, Gen.subterm3 expr expr expr Core.if'
, Gen.subterm2 expr expr (Core.>>>)
, Gen.subtermM2 expr expr (\ x y -> (Core.>>>= y) . (Core.:<- x) <$> name)
, Gen.subtermM expr (\ x -> (x Core....) . namedValue <$> name)
]