@@ -129,16 +129,16 @@ typeDictionaryForBindingGroup moduleName vals = do
129 return (untyped, typed, dict, untypedDict)
129 return (untyped, typed, dict, untypedDict)
131checkTypedBindingGroupElement :: ModuleName -> (Ident, (Expr, Type, Bool)) -> TypeData -> UnifyT Type Check (Ident, (Expr, Type))
131checkTypedBindingGroupElement :: ModuleName -> (Ident, (Expr, Type, Bool)) -> TypeData -> UnifyT Type Check (Ident, (Expr, Type))
132−checkTypedBindingGroupElement moduleName (ident, (val', ty, checkType)) dict = do
132+checkTypedBindingGroupElement mn (ident, (val', ty, checkType)) dict = do
133 -- Replace type wildcards
133 -- Replace type wildcards
134 ty' <- replaceTypeWildcards ty
134 ty' <- replaceTypeWildcards ty
135 -- Kind check
135 -- Kind check
136− kind <- liftCheck $ kindOf moduleName ty
136+ (kind, args) <- liftCheck $ kindOfWithScopedVars ty
137− guardWith (strMsg $ "Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
137+ checkTypeKind kind
138 -- Check the type with the new names in scope
138 -- Check the type with the new names in scope
139 ty'' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty'
139 ty'' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty'
140 val'' <- if checkType
140 val'' <- if checkType
141− then bindNames dict $ TypedValue True <$> check val' ty'' <*> pure ty''
141+ then withScopedTypeVars mn args $ bindNames dict $ TypedValue True <$> check val' ty'' <*> pure ty''
142 else return (TypedValue False val' ty'')
142 else return (TypedValue False val' ty'')
143 return (ident, (val'', ty''))
143 return (ident, (val'', ty''))
@@ -183,10 +183,8 @@ replaceTypeClassDictionaries mn =
184-- Check the kind of a type, failing if it is not of kind *.
184-- Check the kind of a type, failing if it is not of kind *.
186−checkTypeKind :: ModuleName -> Type -> UnifyT t Check ()
186+checkTypeKind :: Kind -> 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
187+checkTypeKind kind = guardWith (strMsg $ "Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
192-- Remove any ForAlls and ConstrainedType constructors in a type by introducing new unknowns
190-- Remove any ForAlls and ConstrainedType constructors in a type by introducing new unknowns
@@ -293,9 +291,10 @@ infer' (SuperClassDictionary className tys) = do
293 return $ TypeClassDictionary False (className, tys) dicts
291 return $ TypeClassDictionary False (className, tys) dicts
294infer' (TypedValue checkType val ty) = do
292infer' (TypedValue checkType val ty) = do
295 Just moduleName <- checkCurrentModule <$> get
293 Just moduleName <- checkCurrentModule <$> get
296− checkTypeKind moduleName ty
294+ (kind, args) <- liftCheck $ kindOfWithScopedVars ty
297 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty
296 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
299 return $ TypedValue True val' ty'
298 return $ TypedValue True val' ty'
300infer' (PositionedValue pos _ val) = rethrowWithPosition pos $ infer' val
299infer' (PositionedValue pos _ val) = rethrowWithPosition pos $ infer' val
301infer' _ = error "Invalid argument to infer"
300infer' _ = error "Invalid argument to infer"
@@ -304,10 +303,11 @@ inferLetBinding :: [Declaration] -> [Declaration] -> Expr -> (Expr -> UnifyT Typ
304inferLetBinding seen [] ret j = (,) seen <$> makeBindingGroupVisible (j ret)
303inferLetBinding seen [] ret j = (,) seen <$> makeBindingGroupVisible (j ret)
305inferLetBinding seen (ValueDeclaration ident nameKind [] (Right (tv@(TypedValue checkType val ty))) : rest) ret j = do
304inferLetBinding seen (ValueDeclaration ident nameKind [] (Right (tv@(TypedValue checkType val ty))) : rest) ret j = do
306 Just moduleName <- checkCurrentModule <$> get
305 Just moduleName <- checkCurrentModule <$> get
307− checkTypeKind moduleName ty
306+ (kind, args) <- liftCheck $ kindOfWithScopedVars ty
308 let dict = M.singleton (moduleName, ident) (ty, nameKind, Undefined)
308 let dict = M.singleton (moduleName, ident) (ty, nameKind, Undefined)
309 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty
309 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
311 bindNames (M.singleton (moduleName, ident) (ty'', nameKind, Defined)) $ inferLetBinding (seen ++ [ValueDeclaration ident nameKind [] (Right (TypedValue checkType val' ty''))]) rest ret j
311 bindNames (M.singleton (moduleName, ident) (ty'', nameKind, Defined)) $ inferLetBinding (seen ++ [ValueDeclaration ident nameKind [] (Right (TypedValue checkType val' ty''))]) rest ret j
312inferLetBinding seen (ValueDeclaration ident nameKind [] (Right val) : rest) ret j = do
312inferLetBinding seen (ValueDeclaration ident nameKind [] (Right val) : rest) ret j = do
313 valTy <- fresh
313 valTy <- fresh
@@ -507,14 +507,15 @@ check' (SuperClassDictionary className tys) _ = do
507 return $ TypeClassDictionary False (className, tys) dicts
507 return $ TypeClassDictionary False (className, tys) dicts
508check' (TypedValue checkType val ty1) ty2 = do
508check' (TypedValue checkType val ty1) ty2 = do
509 Just moduleName <- checkCurrentModule <$> get
509 Just moduleName <- checkCurrentModule <$> get
510− checkTypeKind moduleName ty1
510+ (kind, args) <- liftCheck $ kindOfWithScopedVars ty1
511 ty1' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty1
512 ty1' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty1
512 ty2' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty2
513 ty2' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty2
513 val' <- subsumes (Just val) ty1' ty2'
514 val' <- subsumes (Just val) ty1' ty2'
514 case val' of
515 case val' of
515 Nothing -> throwError . strMsg $ "Unable to check type subsumption"
516 Nothing -> throwError . strMsg $ "Unable to check type subsumption"
516 Just val'' -> do
517 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''
518 return $ TypedValue checkType (TypedValue True val''' ty1') ty2'
519 return $ TypedValue checkType (TypedValue True val''' ty1') ty2'
519check' (Case vals binders) ret = do
520check' (Case vals binders) ret = do
520 vals' <- mapM infer vals
521 vals' <- mapM infer vals
@@ -562,7 +563,7 @@ check' val ty | containsTypeSynonyms ty = do
562 ty' <- introduceSkolemScope <=< expandAllTypeSynonyms <=< replaceTypeWildcards $ ty
563 ty' <- introduceSkolemScope <=< expandAllTypeSynonyms <=< replaceTypeWildcards $ ty
563 check val ty'
564 check val ty'
564check' val kt@(KindedType ty kind) = do
565check' val kt@(KindedType ty kind) = do
565− guardWith (strMsg $ "Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
566+ checkTypeKind kind
566 val' <- check' val ty
567 val' <- check' val ty
567 return $ TypedValue True val' kt
568 return $ TypedValue True val' kt
568check' (PositionedValue pos _ val) ty =
569check' (PositionedValue pos _ val) ty =