Skip to content

Commit d246376

Browse files
committed
new: add fold with arrow tests
1 parent 684352a commit d246376

File tree

2 files changed

+75
-9
lines changed

2 files changed

+75
-9
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ module LambdaBuffers.Compiler.KindCheck (
77

88
-- * Testing Utils.
99
foldWithSum,
10-
foldWithArrow,
10+
foldWithArrowToType,
1111
foldWithProduct,
12+
foldWithApp,
1213
) where
1314

1415
import Control.Lens (view, (&), (.~), (^.))
@@ -326,12 +327,24 @@ tyDef2NameAndKind curModName tyDef = do
326327
pure (name, k)
327328

328329
tyAbsLHS2Kind :: PC.TyAbs -> Kind
329-
tyAbsLHS2Kind tyAbs = foldWithArrow $ pKind2Kind . (\x -> x ^. #argKind) <$> toList (tyAbs ^. #tyArgs)
330+
tyAbsLHS2Kind tyAbs = foldWithArrowToType $ pKind2Kind . (\x -> x ^. #argKind) <$> toList (tyAbs ^. #tyArgs)
330331

331-
foldWithArrow :: [Kind] -> Kind
332-
foldWithArrow = foldr (:->:) Type
332+
{- | Folds kinds and appends them to a Kind result type. In essence creates a
333+
curried function with a Type final kind.
333334
334-
-- ================================================================================
335+
ghc> foldWithArrowToType []
336+
Type
337+
338+
ghc> foldWithArrowToType [Type]
339+
Type -> Type
340+
341+
ghc> foldWithArrowToType [Type, (Type -> Type)]
342+
Type -> (Type -> Type) -> Type
343+
-}
344+
foldWithArrowToType :: [Kind] -> Kind
345+
foldWithArrowToType = foldr (:->:) Type
346+
347+
-- =============================================================================
335348
-- To Kind Conversion functions
336349

337350
pKind2Kind :: PC.Kind -> Kind

lambda-buffers-compiler/test/Test/KindCheck.hs

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Test.KindCheck (test) where
33
import Data.Text (Text)
44
import LambdaBuffers.Compiler.KindCheck (
55
check_,
6+
foldWithArrowToType,
67
foldWithProduct,
78
foldWithSum,
89
)
@@ -11,6 +12,7 @@ import LambdaBuffers.Compiler.KindCheck.Variable (
1112
Variable (LocalRef),
1213
)
1314

15+
import LambdaBuffers.Compiler.KindCheck.Inference (Kind (Type, (:->:)))
1416
import Test.KindCheck.Errors (testGKindCheckErrors)
1517
import Test.QuickCheck (
1618
Arbitrary (arbitrary),
@@ -30,13 +32,15 @@ import Test.Utils.Constructors (_CompilerInput)
3032
-- Top Level tests
3133

3234
test :: TestTree
33-
test = testGroup "Compiler tests" [testCheck, testFolds, testGKindCheckErrors]
35+
test =
36+
testGroup "Compiler tests" [testCheck, testFolds, testGKindCheckErrors]
3437

3538
--------------------------------------------------------------------------------
3639
-- Module tests
3740

3841
testCheck :: TestTree
39-
testCheck = testGroup "KindChecker Tests" [trivialKCTest, kcTestMaybe, kcTestFailing]
42+
testCheck =
43+
testGroup "KindChecker Tests" [trivialKCTest, kcTestMaybe, kcTestFailing]
4044

4145
trivialKCTest :: TestTree
4246
trivialKCTest =
@@ -61,8 +65,15 @@ testFolds :: TestTree
6165
testFolds =
6266
testGroup
6367
"Test Folds"
64-
[ testGroup "Test Product Folds" [testFoldProd0, testFoldProd1, testFoldProd2, testFoldProd3, testPProdFoldTotal]
65-
, testGroup "Test Sum Folds" [testSumFold0, testSumFold1, testSumFold2, testSumFold3]
68+
[ testGroup
69+
"Test Product Folds"
70+
[testFoldProd0, testFoldProd1, testFoldProd2, testFoldProd3, testPProdFoldTotal]
71+
, testGroup
72+
"Test Sum Folds"
73+
[testSumFold0, testSumFold1, testSumFold2, testSumFold3]
74+
, testGroup
75+
"Test Arrow Folds"
76+
[testArrowFold0, testArrowFold1, testArrowFold2, testArrowFold3HK, testArrowFold4HK, testArrowFoldHHK]
6677
]
6778

6879
prod :: Type -> Type -> Type
@@ -135,6 +146,48 @@ testSumFold3 =
135146
foldWithSum [lVar "c", lVar "b", lVar "a"]
136147
@?= sum' (sum' (sum' void' (lVar "c")) (lVar "b")) (lVar "a")
137148

149+
ty :: Kind
150+
ty = Type
151+
152+
-- | [ ] -> *
153+
testArrowFold0 :: TestTree
154+
testArrowFold0 =
155+
testCase "Fold 0 kinds" $
156+
foldWithArrowToType [] @?= ty
157+
158+
-- | [*] => * -> *
159+
testArrowFold1 :: TestTree
160+
testArrowFold1 =
161+
testCase "Fold 1 kinds" $
162+
foldWithArrowToType [ty] @?= ty :->: ty
163+
164+
-- | [*,*] => * -> * -> *
165+
testArrowFold2 :: TestTree
166+
testArrowFold2 =
167+
testCase "Fold 2 kinds" $
168+
foldWithArrowToType [ty, ty] @?= ty :->: (ty :->: ty)
169+
170+
-- | [* -> *, * ] => (* -> *) -> * -> *
171+
testArrowFold3HK :: TestTree
172+
testArrowFold3HK =
173+
testCase "Fold 2 HKT" $
174+
foldWithArrowToType [ty :->: ty, ty]
175+
@?= ((ty :->: ty) :->: (ty :->: ty))
176+
177+
-- | [*, * -> *, * ] => * -> (* -> *) -> * -> *
178+
testArrowFold4HK :: TestTree
179+
testArrowFold4HK =
180+
testCase "Fold 2 HKT" $
181+
foldWithArrowToType [ty, ty :->: ty, ty]
182+
@?= (ty :->: ((ty :->: ty) :->: (ty :->: ty)))
183+
184+
-- | [*, * -> *, * ] => * -> ((* -> *) -> *) -> * -> *
185+
testArrowFoldHHK :: TestTree
186+
testArrowFoldHHK =
187+
testCase "Fold 2 HKT" $
188+
foldWithArrowToType [ty, (ty :->: ty) :->: ty, ty]
189+
@?= (ty :->: (((ty :->: ty) :->: ty) :->: (ty :->: ty)))
190+
138191
-- | TyDef to Kind Canonical representation - sums not folded - therefore we get constructor granularity. Might use in a different implementation for more granular errors.
139192
lVar :: Text -> Type
140193
lVar = Var . LocalRef

0 commit comments

Comments
 (0)