@@ -491,14 +491,12 @@ data Info = Info
492constrainDef :: Env.Env -> Info -> Canonical.Def -> IO Info
492constrainDef :: Env.Env -> Info -> Canonical.Def -> IO Info
493constrainDef env info (Canonical.Def _ (A.A patternRegion pattern) expr maybeTipe) =
493constrainDef env info (Canonical.Def _ (A.A patternRegion pattern) expr maybeTipe) =
494− let qs = [] -- should come from the def, but I'm not sure what would live there...
496 case (pattern, maybeTipe) of
494 case (pattern, maybeTipe) of
497 (P.Var name, Nothing) ->
495 (P.Var name, Nothing) ->
498− constrainUnannotatedDef env info qs patternRegion name expr
496+ constrainUnannotatedDef env patternRegion name expr info
500− (P.Var name, Just (A.A typeRegion tipe)) ->
498+ (P.Var name, Just (A.A _ tipe)) ->
501− constrainAnnotatedDef env info qs patternRegion typeRegion name expr tipe
499+ constrainAnnotatedDef env patternRegion name expr tipe info
504 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"
@@ -506,66 +504,79 @@ constrainDef env info (Canonical.Def _ (A.A patternRegion pattern) expr maybeTip
507constrainUnannotatedDef
505constrainUnannotatedDef
508 :: Env.Env
506 :: Env.Env
511 -> R.Region
507 -> R.Region
512 -> String
508 -> String
513 -> Canonical.Expr
509 -> Canonical.Expr
514 -> IO Info
511 -> IO Info
515−constrainUnannotatedDef env info qs patternRegion name expr =
512+constrainUnannotatedDef env patternRegion name expr info =
516− do -- Some mistake may be happening here. Currently, qs is always [].
517− rigidVars <- mapM mkRigid qs
519− v <- mkVar Nothing
513+ do v <- mkVar Nothing
521 let tipe = VarN v
514 let tipe = VarN v
523− let env' = Env.addValues env (zip qs rigidVars)
525− con <- constrain env' expr tipe
515+ defCon <- constrain env expr tipe
527 return $ info
516 return $ info
528− { iRigid = rigidVars ++ iRigid info
529− , iFlex = v : iFlex info
517+ { iFlex = v : iFlex info
530 , iHeaders = Map.insert name (A.A patternRegion tipe) (iHeaders info)
518 , iHeaders = Map.insert name (A.A patternRegion tipe) (iHeaders info)
531− , iC2 = con /\ iC2 info
519+ , iC2 = defCon /\ iC2 info
535−constrainAnnotatedDef
523+constrainAnnotatedDef :: Env.Env -> R.Region -> String -> Canonical.Expr -> ST.Canonical -> Info -> IO Info
545−constrainAnnotatedDef env info qs patternRegion typeRegion name expr tipe =
524+constrainAnnotatedDef env region name expr tipe info =
546− do -- Some mistake may be happening here. Currently, qs is always [].
547− rigidVars <- mapM mkRigid qs
549− flexiVars <- mapM mkNamedVar qs
551− let env' = Env.addValues env (zip qs flexiVars)
553− (vars, typ) <- Env.instantiateType env tipe Map.empty
525+ do (flexType, flexVars) <- Env.instantiateType Flex env tipe
555 let scheme =
527 let scheme =
557 { _rigidQuantifiers = []
529 { _rigidQuantifiers = []
558− , _flexibleQuantifiers = flexiVars ++ vars
530+ , _flexibleQuantifiers = Map.elems flexVars
559 , _constraint = CTrue
531 , _constraint = CTrue
560− , _header = Map.singleton name (A.A patternRegion typ)
532+ , _header = Map.singleton name (A.A region flexType)
563− var <- mkVar Nothing
535+ defCon <- constrainAnnDefHelp name expr tipe (ArgInfo env [] [])
564− defCon <- constrain env' expr (VarN var)
566− CEqual (Error.BadTypeAnnotation name) typeRegion typ (VarN var)
568 return $ info
537 return $ info
569 { iSchemes = scheme : iSchemes info
538 { iSchemes = scheme : iSchemes info
570− , iC1 = iC1 info /\ ex [var] (defCon /\ fl rigidVars annCon)
539+ , iC1 = iC1 info /\ defCon
547+ , _args :: [(P.Canonical, Type)]
548+ , _vars :: [Variable]
552+constrainAnnDefHelp :: String -> Canonical.Expr -> ST.Canonical -> ArgInfo -> IO TypeConstraint
553+constrainAnnDefHelp name expr tipe (ArgInfo env args vars) =
554+ case (expr, tipe) of
555+ (A.A _ (E.Lambda arg result), ST.Lambda argType resultType) ->
556+ do (rigidArgType, newVars) <- Env.instantiateType Rigid env argType
558+ constrainAnnDefHelp name result resultType $
560+ { _env = Env.addValues newVars env
561+ , _args = (arg, rigidArgType) : args
562+ , _vars = Map.elems newVars ++ vars
565+ (A.A region _, _) ->
566+ do (rigidType, newVars) <- Env.instantiateType Rigid env tipe
568+ let finalEnv = Env.addValues newVars env
569+ let finalArgs = reverse args
570+ let rigidVars = Map.elems newVars ++ vars
572+ (Pattern.Info headers argVars argCons) <-
573+ Pattern.joinInfos <$> mapM (uncurry (Pattern.constrain finalEnv)) finalArgs
575+ resultVar <- mkVar Nothing
576+ let resultType = VarN resultVar
577+ defCon <- constrain finalEnv expr resultType
579+ CEqual (Error.ReturnType name (length args)) region resultType rigidType
581+ return $ forall rigidVars $ ex (resultVar : argVars) $ CLet [monoscheme headers] $
582+ CAnd [ argCons, defCon, resultCon ]