@@ -354,9 +354,17 @@ replaceAllTypeVars = foldl' (\f (name, ty) -> replaceTypeVars name ty . f) id
355-- Replace named type variables with new unification variables
355-- Replace named type variables with new unification variables
357−replaceAllVarsWithUnknowns :: Type -> UnifyT Type Check Type
357+instantiatePolyTypeWithUnknowns :: Value -> Type -> UnifyT Type Check (Value, Type)
358+instantiatePolyTypeWithUnknowns val (ForAll ident ty _) = do
358−replaceAllVarsWithUnknowns (ForAll ident ty _) = replaceVarWithUnknown ident ty >>= replaceAllVarsWithUnknowns
359+ ty' <- replaceVarWithUnknown ident ty
359−replaceAllVarsWithUnknowns ty = return ty
360+ instantiatePolyTypeWithUnknowns val ty'
361+instantiatePolyTypeWithUnknowns val (ConstrainedType constraints ty) = do
363+ Just moduleName <- checkCurrentModule <$> get
364+ dicts <- getTypeClassDictionaries
365+ (_, ty') <- instantiatePolyTypeWithUnknowns (error "Types under a constraint cannot themselves be constrained") ty
366+ return (foldl App val (map (flip TypeClassDictionary dicts) (qualifyAllUnqualifiedNames moduleName env constraints)), ty')
367+instantiatePolyTypeWithUnknowns val ty = return (val, ty)
362-- Replace a single type variable with a new unification variable
370-- Replace a single type variable with a new unification variable
@@ -460,10 +468,10 @@ infer' (Var var) = do
460 Just moduleName <- checkCurrentModule <$> get
468 Just moduleName <- checkCurrentModule <$> get
461 ty <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< lookupVariable moduleName $ var
469 ty <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< lookupVariable moduleName $ var
462 case ty of
470 case ty of
463− ConstrainedType constraints _ -> do
471+ ConstrainedType constraints ty' -> do
464 env <- getEnv
472 env <- getEnv
465 dicts <- getTypeClassDictionaries
473 dicts <- getTypeClassDictionaries
466− return $ TypedValue True (foldl App (Var var) (map (flip TypeClassDictionary dicts) (qualifyAllUnqualifiedNames moduleName env constraints))) ty
474+ return $ TypedValue True (foldl App (Var var) (map (flip TypeClassDictionary dicts) (qualifyAllUnqualifiedNames moduleName env constraints))) ty'
467 _ -> return $ TypedValue True (Var var) ty
475 _ -> return $ TypedValue True (Var var) ty
468infer' (Block ss) = do
476infer' (Block ss) = do
469 ret <- fresh
477 ret <- fresh
@@ -534,7 +542,7 @@ inferBinder val (UnaryBinder ctor binder) = do
534 Just moduleName <- checkCurrentModule <$> get
542 Just moduleName <- checkCurrentModule <$> get
535 case M.lookup (qualify moduleName ctor) (dataConstructors env) of
543 case M.lookup (qualify moduleName ctor) (dataConstructors env) of
536 Just (ty, _) -> do
544 Just (ty, _) -> do
537− fn <- replaceAllVarsWithUnknowns ty
545+ (_, fn) <- instantiatePolyTypeWithUnknowns (error "Data constructor types cannot contains constraints") ty
538 case fn of
546 case fn of
539 TypeApp (TypeApp t obj) ret | t == tyFunction -> do
547 TypeApp (TypeApp t obj) ret | t == tyFunction -> do
540 _ <- subsumes Nothing val ret
548 _ <- subsumes Nothing val ret
@@ -739,9 +747,9 @@ check' val t@(SaturatedTypeSynonym name args) = do
739check' val u@(TUnknown _) = do
747check' val u@(TUnknown _) = do
740 val'@(TypedValue _ _ ty) <- infer val
748 val'@(TypedValue _ _ ty) <- infer val
741 -- Don't unify an unknown with an inferred polytype
749 -- Don't unify an unknown with an inferred polytype
742− ty' <- replaceAllVarsWithUnknowns ty
750+ (val'', ty') <- instantiatePolyTypeWithUnknowns val' ty
743 ty' =?= u
751 ty' =?= u
744− return $ TypedValue True val' ty'
752+ return $ TypedValue True val'' ty'
745check' v@(NumericLiteral _) t | t == tyNumber =
753check' v@(NumericLiteral _) t | t == tyNumber =
746 return $ TypedValue True v t
754 return $ TypedValue True v t
747check' v@(StringLiteral _) t | t == tyString =
755check' v@(StringLiteral _) t | t == tyString =
@@ -879,7 +887,8 @@ checkFunctionApplication' fn (ForAll ident ty _) arg = do
879checkFunctionApplication' fn u@(TUnknown _) arg = do
887checkFunctionApplication' fn u@(TUnknown _) arg = do
880 arg' <- do
888 arg' <- do
881 TypedValue _ v t <- infer arg
889 TypedValue _ v t <- infer arg
890+ (v', t') <- instantiatePolyTypeWithUnknowns arg t
882− TypedValue True v <$> replaceAllVarsWithUnknowns t
891+ return $ TypedValue True v' t'
883 let ty = (\(TypedValue _ _ t) -> t) arg'
892 let ty = (\(TypedValue _ _ t) -> t) arg'
884 ret <- fresh
893 ret <- fresh
885 u =?= function ty ret
894 u =?= function ty ret