@@ -490,13 +490,13 @@ data Info = Info
492constrainDef :: Env.Env -> Info -> Canonical.Def -> IO Info
492constrainDef :: Env.Env -> Info -> Canonical.Def -> IO Info
493−constrainDef env info (Canonical.Def _ (A.A patternRegion pattern) expr maybeTipe) =
493+constrainDef env info (Canonical.Def defRegion (A.A patternRegion pattern) expr maybeTipe) =
494 case (pattern, maybeTipe) of
494 case (pattern, maybeTipe) of
495 (P.Var name, Nothing) ->
495 (P.Var name, Nothing) ->
496 constrainUnannotatedDef env patternRegion name expr info
496 constrainUnannotatedDef env patternRegion name expr info
498 (P.Var name, Just (A.A _ tipe)) ->
498 (P.Var name, Just (A.A _ tipe)) ->
499− constrainAnnotatedDef env patternRegion name expr tipe info
499+ constrainAnnotatedDef env defRegion patternRegion name expr tipe info
502 error "canonical definitions must not have complex patterns as names in the contstraint generation phase"
502 error "canonical definitions must not have complex patterns as names in the contstraint generation phase"
@@ -520,8 +520,16 @@ constrainUnannotatedDef env patternRegion name expr info =
523−constrainAnnotatedDef :: Env.Env -> R.Region -> String -> Canonical.Expr -> ST.Canonical -> Info -> IO Info
523+constrainAnnotatedDef
524−constrainAnnotatedDef env region name expr tipe info =
532+constrainAnnotatedDef env defRegion region name expr tipe info =
525 do (flexType, flexVars) <- Env.instantiateType Flex env tipe
533 do (flexType, flexVars) <- Env.instantiateType Flex env tipe
527 let scheme =
535 let scheme =
@@ -532,7 +540,7 @@ constrainAnnotatedDef env region name expr tipe info =
532 , _header = Map.singleton name (A.A region flexType)
540 , _header = Map.singleton name (A.A region flexType)
535− defCon <- constrainAnnDefHelp name expr tipe (ArgInfo env [] [])
543+ defCon <- constrainAnnDefHelp expr tipe (ArgInfo name defRegion env [] [])
537 return $ info
545 return $ info
538 { iSchemes = scheme : iSchemes info
546 { iSchemes = scheme : iSchemes info
@@ -542,21 +550,25 @@ constrainAnnotatedDef env region name expr tipe info =
543data ArgInfo =
551data ArgInfo =
545− { _env :: Env.Env
555+ , _env :: Env.Env
546 , _args :: [(P.Canonical, Type)]
556 , _args :: [(P.Canonical, Type)]
547 , _vars :: [Variable]
557 , _vars :: [Variable]
551−constrainAnnDefHelp :: String -> Canonical.Expr -> ST.Canonical -> ArgInfo -> IO TypeConstraint
561+constrainAnnDefHelp :: Canonical.Expr -> ST.Canonical -> ArgInfo -> IO TypeConstraint
552−constrainAnnDefHelp name expr tipe (ArgInfo env args vars) =
562+constrainAnnDefHelp expr tipe (ArgInfo name defRegion env args vars) =
553 case (expr, tipe) of
563 case (expr, tipe) of
554 (A.A _ (E.Lambda arg result), ST.Lambda argType resultType) ->
564 (A.A _ (E.Lambda arg result), ST.Lambda argType resultType) ->
555 do (rigidArgType, newVars) <- Env.instantiateType Rigid env argType
565 do (rigidArgType, newVars) <- Env.instantiateType Rigid env argType
557− constrainAnnDefHelp name result resultType $
567+ constrainAnnDefHelp result resultType $
559− { _env = Env.addValues newVars env
571+ , _env = Env.addValues newVars env
560 , _args = (arg, rigidArgType) : args
572 , _args = (arg, rigidArgType) : args
561 , _vars = Map.elems newVars ++ vars
573 , _vars = Map.elems newVars ++ vars
@@ -574,8 +586,13 @@ constrainAnnDefHelp name expr tipe (ArgInfo env args vars) =
574 resultVar <- mkVar Nothing
586 resultVar <- mkVar Nothing
575 let resultType = VarN resultVar
587 let resultType = VarN resultVar
576 defCon <- constrain finalEnv expr resultType
588 defCon <- constrain finalEnv expr resultType
590+ let sharedArity = length args
591+ let typeArity = sharedArity + length (ST.collectLambdas tipe) - 1
592+ let argsArity = sharedArity + length (fst (E.collectLambdas expr))
593+ let hint = Error.ReturnType name typeArity argsArity region
577 let resultCon =
594 let resultCon =
578− CEqual (Error.ReturnType name (length args)) region resultType rigidType
595+ CEqual hint defRegion rigidType resultType
580 return $ forall rigidVars $ ex (resultVar : argVars) $ CLet [monoscheme headers] $
597 return $ forall rigidVars $ ex (resultVar : argVars) $ CLet [monoscheme headers] $
581 CAnd [ argCons, defCon, resultCon ]
598 CAnd [ argCons, defCon, resultCon ]