feat: instances for Arbitrary

This commit is contained in:
vegowotenks 2025-08-14 09:21:34 +02:00
parent 13a2577ae2
commit 8181113bfe
6 changed files with 83 additions and 16 deletions

View file

@ -14,6 +14,14 @@ import Language.Scalie.Domain.Type qualified as Scalie.Domain
import Language.Scalie.Ast.Expression (Expression)
import Text.Show (Show)
import Text.Read (Read)
import Data.Eq (Eq)
import Test.QuickCheck.Arbitrary (Arbitrary (arbitrary))
import Test.QuickCheck.Gen (Gen)
import Control.Applicative (Applicative((<*>)), (<$>))
import Data.Text qualified as Text
import Control.Category (Category((.)))
import Test.QuickCheck.Modifiers (UnicodeString(getUnicodeString))
import Data.Functor (Functor)
-- | The definition of a value or a function (which is also a value)
--
@ -37,7 +45,7 @@ type role Definition nominal
data Definition f = Definition
{ signature :: f Scalie.Domain.Type
-- ^ What is the type
, name :: f Text
, name :: f Text
-- ^ Which name can be used to refer to this definition
, body :: f Expression
-- ^ What needs to be evaluated to get the value
@ -45,9 +53,17 @@ data Definition f = Definition
deriving stock instance (Show (f Expression), Show (f Scalie.Domain.Type), Show (f Text)) => Show (Definition f)
deriving stock instance (Read (f Expression), Read (f Scalie.Domain.Type), Read (f Text)) => Read (Definition f)
deriving stock instance (Eq (f Expression) , Eq (f Scalie.Domain.Type) , Eq (f Text)) => Eq (Definition f)
instance ImplicitKeyOf (Definition f) where
type KeyType (Definition f) = f Text
keyOf :: Definition f -> KeyType (Definition f)
keyOf = name
instance (Functor f, Arbitrary (f UnicodeString), Arbitrary (f Scalie.Domain.Type), Arbitrary (f Expression)) => Arbitrary (Definition f) where
arbitrary :: Gen (Definition f)
arbitrary = Definition
<$> arbitrary
<*> ((Text.pack . getUnicodeString <$>) <$> arbitrary)
<*> arbitrary

View file

@ -1,12 +1,23 @@
{-# LANGUAGE Safe #-}
{-# LANGUAGE InstanceSigs #-}
module Language.Scalie.Ast.Expression (Expression(..)) where
import Prelude (Integer)
import Data.Kind (Type)
import Text.Show (Show)
import Text.Read (Read)
import Data.Eq (Eq)
import Test.QuickCheck (Arbitrary (arbitrary), Gen, oneof)
import Data.Functor ((<$>))
type Expression :: Type
data Expression
= RawInt Integer
deriving stock (Show, Read)
deriving stock (Show, Read, Eq)
instance Arbitrary Expression where
arbitrary :: Gen Expression
arbitrary = oneof
[ RawInt <$> arbitrary
]

View file

@ -1,12 +1,20 @@
{-# LANGUAGE Safe #-}
{-# LANGUAGE InstanceSigs #-} -- function signature in instances
module Language.Scalie.Domain.Type (Type(..)) where
import Data.Kind qualified
import Text.Show (Show)
import Text.Read (Read)
import Data.Eq (Eq)
import Test.QuickCheck (Arbitrary (arbitrary), Gen, oneof)
import Control.Applicative (Applicative(pure))
type Type :: Data.Kind.Type
data Type
= RawInt
deriving stock (Show, Read)
deriving stock (Show, Read, Eq)
instance Arbitrary Type where
arbitrary :: Gen Type
arbitrary = oneof [ pure RawInt ]