@@ -134,7 +134,7 @@ checkTypedBindingGroupElement mn (ident, (val', ty, checkType)) dict = do
134134 ty' <- replaceTypeWildcards ty
135135 -- Kind check
136136 (kind, args) <- liftCheck $ kindOfWithScopedVars ty
137− guardWith (strMsg $ "Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
137+ checkTypeKind kind
138138 -- Check the type with the new names in scope
139139 ty'' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty'
140140 val'' <- if checkType
@@ -183,10 +183,8 @@ replaceTypeClassDictionaries mn =
183183-- |
184184-- Check the kind of a type, failing if it is not of kind *.
185185--
186−checkTypeKind :: ModuleName -> Type -> UnifyT t Check ()
187−checkTypeKind moduleName ty = do
188− kind <- liftCheck $ kindOf moduleName ty
189− guardWith (strMsg $ "Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
186+checkTypeKind :: Kind -> UnifyT t Check ()
187+checkTypeKind kind = guardWith (strMsg $ "Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
190188
191189-- |
192190-- Remove any ForAlls and ConstrainedType constructors in a type by introducing new unknowns
@@ -293,9 +291,10 @@ infer' (SuperClassDictionary className tys) = do
293291 return $ TypeClassDictionary False (className, tys) dicts
294292infer' (TypedValue checkType val ty) = do
295293 Just moduleName <- checkCurrentModule <$> get
296− checkTypeKind moduleName ty
294+ (kind, args) <- liftCheck $ kindOfWithScopedVars ty
295+ checkTypeKind kind
297296 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty
298− val' <- if checkType then check val ty' else return val
297+ val' <- if checkType then withScopedTypeVars moduleName args (check val ty') else return val
299298 return $ TypedValue True val' ty'
300299infer' (PositionedValue pos _ val) = rethrowWithPosition pos $ infer' val
301300infer' _ = error "Invalid argument to infer"
@@ -304,10 +303,11 @@ inferLetBinding :: [Declaration] -> [Declaration] -> Expr -> (Expr -> UnifyT Typ
304303inferLetBinding seen [] ret j = (,) seen <$> makeBindingGroupVisible (j ret)
305304inferLetBinding seen (ValueDeclaration ident nameKind [] (Right (tv@(TypedValue checkType val ty))) : rest) ret j = do
306305 Just moduleName <- checkCurrentModule <$> get
307− checkTypeKind moduleName ty
306+ (kind, args) <- liftCheck $ kindOfWithScopedVars ty
307+ checkTypeKind kind
308308 let dict = M.singleton (moduleName, ident) (ty, nameKind, Undefined)
309309 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty
310− TypedValue _ val' ty'' <- if checkType then bindNames dict (check val ty') else return tv
310+ TypedValue _ val' ty'' <- if checkType then withScopedTypeVars moduleName args (bindNames dict (check val ty')) else return tv
311311 bindNames (M.singleton (moduleName, ident) (ty'', nameKind, Defined)) $ inferLetBinding (seen ++ [ValueDeclaration ident nameKind [] (Right (TypedValue checkType val' ty''))]) rest ret j
312312inferLetBinding seen (ValueDeclaration ident nameKind [] (Right val) : rest) ret j = do
313313 valTy <- fresh
@@ -507,14 +507,15 @@ check' (SuperClassDictionary className tys) _ = do
507507 return $ TypeClassDictionary False (className, tys) dicts
508508check' (TypedValue checkType val ty1) ty2 = do
509509 Just moduleName <- checkCurrentModule <$> get
510− checkTypeKind moduleName ty1
510+ (kind, args) <- liftCheck $ kindOfWithScopedVars ty1
511+ checkTypeKind kind
511512 ty1' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty1
512513 ty2' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty2
513514 val' <- subsumes (Just val) ty1' ty2'
514515 case val' of
515516 Nothing -> throwError . strMsg $ "Unable to check type subsumption"
516517 Just val'' -> do
517− val''' <- if checkType then check val'' ty2' else return val''
518+ val''' <- if checkType then withScopedTypeVars moduleName args (check val'' ty2') else return val''
518519 return $ TypedValue checkType (TypedValue True val''' ty1') ty2'
519520check' (Case vals binders) ret = do
520521 vals' <- mapM infer vals
@@ -562,7 +563,7 @@ check' val ty | containsTypeSynonyms ty = do
562563 ty' <- introduceSkolemScope <=< expandAllTypeSynonyms <=< replaceTypeWildcards $ ty
563564 check val ty'
564565check' val kt@(KindedType ty kind) = do
565− guardWith (strMsg $ "Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
566+ checkTypeKind kind
566567 val' <- check' val ty
567568 return $ TypedValue True val' kt
568569check' (PositionedValue pos _ val) ty =