Skip to content

Commit 419f3a9

Browse files
authored
Merge pull request #34 from mlabs-haskell/compiler/arbitrary-instances
Compiler: Arbitrary Instances + Property Test
2 parents 45577f0 + 391f2c5 commit 419f3a9

File tree

11 files changed

+224
-91
lines changed

11 files changed

+224
-91
lines changed

lambda-buffers-compiler/lambda-buffers-compiler.cabal

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ common common-language
3737
DeriveLift
3838
DeriveTraversable
3939
DerivingStrategies
40+
DerivingVia
4041
DoAndIfThenElse
4142
DuplicateRecordFields
4243
EmptyCase
@@ -94,12 +95,15 @@ library
9495
build-depends:
9596
, containers >=0.6.5.1
9697
, freer-simple >=1.2
98+
, generic-arbitrary
9799
, generic-lens >=2.2
98100
, lambda-buffers-compiler-pb >=0.1.0.0
99101
, mtl >=2.2
100102
, parsec >=3.1
101103
, prettyprinter >=1.7
102104
, proto-lens >=0.7
105+
, QuickCheck >=2.14
106+
, quickcheck-instances >=0.3
103107
, text >=1.2
104108

105109
exposed-modules:
@@ -147,16 +151,17 @@ test-suite tests
147151
, lambda-buffers-compiler
148152
, lambda-buffers-compiler-pb >=0.1
149153
, proto-lens >=0.7
154+
, QuickCheck >=2.14
150155
, tasty >=1.4
151156
, tasty-hunit >=0.10
157+
, tasty-quickcheck >=0.10
152158
, text >=1.2
153159

154160
other-modules:
155161
Test.KindCheck
156-
Test.Samples
157-
Test.Samples.Proto.CompilerInput
158-
Test.Samples.Proto.Helpers
159-
Test.Samples.Proto.Module
160-
Test.Samples.Proto.SourceInfo
161-
Test.Samples.Proto.TyDef
162162
Test.TypeClassCheck
163+
Test.Utils.CompilerInput
164+
Test.Utils.Constructors
165+
Test.Utils.Module
166+
Test.Utils.SourceInfo
167+
Test.Utils.TyDef

lambda-buffers-compiler/src/LambdaBuffers/Compiler/KindCheck/Type.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module LambdaBuffers.Compiler.KindCheck.Type (
88

99
import LambdaBuffers.Compiler.KindCheck.Variable (Variable (LocalRef))
1010
import Prettyprinter (Doc, Pretty (pretty), parens, (<+>))
11+
import Test.QuickCheck (Arbitrary, Gen, arbitrary, oneof, sized)
1112

1213
data Type
1314
= Var Variable
@@ -32,3 +33,16 @@ instance Pretty Type where
3233
Var a -> pretty a
3334
App t1 t2 -> parens $ show' t1 <+> show' t2
3435
Abs a t1 -> parens $ "λ" <> pretty a <> "." <> show' t1
36+
37+
instance Arbitrary Type where
38+
arbitrary = sized f
39+
where
40+
f :: Integral a => a -> Gen Type
41+
f n
42+
| n <= 0 = Var <$> arbitrary
43+
| otherwise =
44+
oneof
45+
[ Var <$> arbitrary
46+
, App <$> f (n `div` 2) <*> f (n `div` 2)
47+
, Abs <$> arbitrary <*> f (n - 1)
48+
]

lambda-buffers-compiler/src/LambdaBuffers/Compiler/KindCheck/Variable.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
module LambdaBuffers.Compiler.KindCheck.Variable (Variable (LocalRef, ForeignRef), Atom) where
22

33
import Data.Text (Text)
4+
import GHC.Generics (Generic)
45
import Prettyprinter (Pretty (pretty), concatWith)
6+
import Test.QuickCheck (Arbitrary)
7+
import Test.QuickCheck.Arbitrary.Generic (GenericArbitrary (GenericArbitrary))
8+
import Test.QuickCheck.Instances.Text ()
59

610
type Atom = Text
711

812
data Variable
913
= LocalRef Text
1014
| ForeignRef [Text] Text
11-
deriving stock (Eq, Ord, Show)
15+
deriving stock (Eq, Ord, Show, Generic)
16+
deriving (Arbitrary) via GenericArbitrary Variable
1217

1318
instance Pretty Variable where
1419
pretty = \case

0 commit comments

Comments
 (0)