Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximilianAlgehed committed May 24, 2022
1 parent 4377c2f commit fd13cb4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 9 additions & 7 deletions plutus-core/plutus-ir/test/GeneratorSpec.hs
Expand Up @@ -102,11 +102,12 @@ prop_genKindCorrect =
-- | Check that shrinking types maintains kinds
prop_shrinkTypeSound :: Property
prop_shrinkTypeSound =
forAllDoc "k,ty" genKindAndType (shrinkKindAndType Map.empty) $ \ (k, ty) ->
forAllDoc "ctx" arbitrary (const []) $ \ ctx ->
forAllDoc "k,ty" (genKindAndTypeWithCtx ctx) (shrinkKindAndType ctx) $ \ (k, ty) ->
-- See discussion about the same trick in `prop_shrinkTermSound`
checkKind Map.empty ty k ==>
assertNoCounterexamples [ (k, ty) | (k, ty) <- shrinkKindAndType Map.empty (k, ty)
, not $ checkKind Map.empty ty k ]
checkKind ctx ty k ==>
assertNoCounterexamples [ (k, ty) | (k, ty) <- shrinkKindAndType ctx (k, ty)
, not $ checkKind ctx ty k ]

-- | Test that our generators only result in well-typed terms.
-- Note, the counterexamples from this property are not shrunk (see why below).
Expand Down Expand Up @@ -245,11 +246,12 @@ prop_shrinkKindSmaller =
-- | Test that fixKind actually gives you something of the right kind
prop_fixKind :: Property
prop_fixKind =
forAllDoc "k,ty" genKindAndType (shrinkKindAndType Map.empty) $ \ (k, ty) ->
forAllDoc "ctx" genCtx (const []) $ \ ctx ->
forAllDoc "k,ty" genKindAndType (shrinkKindAndType ctx) $ \ (k, ty) ->
-- Note, fixKind only works on smaller kinds, so we use shrink to get a definitely smaller kind
assertNoCounterexamples [ (ty', k') | k' <- shrink k
, let ty' = fixKind Map.empty ty k'
, not $ checkKind Map.empty ty' k' ]
, let ty' = fixKind ctx ty k'
, not $ checkKind ctx ty' k' ]

-- * Tests for unification and substitution

Expand Down
6 changes: 6 additions & 0 deletions plutus-core/testlib/PlutusCore/Generators/PIR.hs
Expand Up @@ -514,6 +514,12 @@ genClosedType_ = genTypeWithCtx mempty
genTypeWithCtx :: Map TyName (Kind ()) -> Kind () -> Gen (Type TyName DefaultUni ())
genTypeWithCtx ctx k = runGenTm $ local (\ e -> e { geTypes = ctx }) (genType k)

-- | Generate a well-kinded term in a given context
genKindAndTypeWithCtx :: Map TyName (Kind ()) -> Gen (Type TyName DefaultUni ())
genKindAndTypeWithCtx ctx = do
k <- arbitrary
runGenTm $ local (\ e -> e { geTypes = ctx }) ((k,) <$> genType k)

-- CODE REVIEW: does this exist anywhere??
-- | `substClosedType x sub ty` substitutes the closed type `sub` for `x` in `ty`
substClosedType :: TyName -> Type TyName DefaultUni () -> Type TyName DefaultUni () -> Type TyName DefaultUni ()
Expand Down

0 comments on commit fd13cb4

Please sign in to comment.