@@ -67,22 +67,24 @@ import Control.Arrow (Arrow(..))
67import qualified Data.Map as M
67import qualified Data.Map as M
68import Data.Function (on)
68import Data.Function (on)
70−instance Unifiable Check Type where
70+instance Partial Type where
71 unknown = TUnknown
71 unknown = TUnknown
72 isUnknown (TUnknown u) = Just u
72 isUnknown (TUnknown u) = Just u
73 isUnknown _ = Nothing
73 isUnknown _ = Nothing
75+instance Unifiable Check Type where
74− (?=) = unifyTypes
76+ (=?=) = unifyTypes
77-- Unify two types, updating the current substitution
79-- Unify two types, updating the current substitution
79−unifyTypes :: Type -> Type -> UnifyT Check ()
81+unifyTypes :: Type -> Type -> UnifyT Type Check ()
80unifyTypes t1 t2 = rethrow (\e -> "Error unifying type " ++ prettyPrintType t1 ++ " with type " ++ prettyPrintType t2 ++ ":\n" ++ e) $ do
82unifyTypes t1 t2 = rethrow (\e -> "Error unifying type " ++ prettyPrintType t1 ++ " with type " ++ prettyPrintType t2 ++ ":\n" ++ e) $ do
81 unifyTypes' t1 t2
83 unifyTypes' t1 t2
83 unifyTypes' (TUnknown u1) (TUnknown u2) | u1 == u2 = return ()
85 unifyTypes' (TUnknown u1) (TUnknown u2) | u1 == u2 = return ()
84− unifyTypes' (TUnknown u) t = replace u t
86+ unifyTypes' (TUnknown u) t = u =:= t
85− unifyTypes' t (TUnknown u) = replace u t
87+ unifyTypes' t (TUnknown u) = u =:= t
86 unifyTypes' (SaturatedTypeSynonym name args) ty = do
88 unifyTypes' (SaturatedTypeSynonym name args) ty = do
87 ty1 <- expandTypeSynonym name args
89 ty1 <- expandTypeSynonym name args
88 ty1 `unifyTypes` ty
90 ty1 `unifyTypes` ty
@@ -95,7 +97,7 @@ unifyTypes t1 t2 = rethrow (\e -> "Error unifying type " ++ prettyPrintType t1 +
95 sk <- skolemize ident ty1
97 sk <- skolemize ident ty1
96 sk `unifyTypes` ty2
98 sk `unifyTypes` ty2
97 unifyTypes' ty f@(ForAll _ _) = f `unifyTypes` ty
99 unifyTypes' ty f@(ForAll _ _) = f `unifyTypes` ty
98− unifyTypes' (Object row1) (Object row2) = row1 ?= row2
100+ unifyTypes' (Object row1) (Object row2) = row1 =?= row2
99 unifyTypes' (TypeVar v1) (TypeVar v2) | v1 == v2 = return ()
101 unifyTypes' (TypeVar v1) (TypeVar v2) | v1 == v2 = return ()
100 unifyTypes' (TypeConstructor c1) (TypeConstructor c2) = do
102 unifyTypes' (TypeConstructor c1) (TypeConstructor c2) = do
101 env <- getEnv
103 env <- getEnv
@@ -118,7 +120,7 @@ unifyTypes t1 t2 = rethrow (\e -> "Error unifying type " ++ prettyPrintType t1 +
118-- trailing row unification variable, if appropriate, otherwise leftover labels result in a unification
120-- trailing row unification variable, if appropriate, otherwise leftover labels result in a unification
121−unifyRows :: Type -> Type -> UnifyT Check ()
123+unifyRows :: Type -> Type -> UnifyT Type Check ()
122unifyRows r1 r2 =
124unifyRows r1 r2 =
124 (s1, r1') = rowToList r1
126 (s1, r1') = rowToList r1
@@ -127,17 +129,17 @@ unifyRows r1 r2 =
127 sd1 = [ (name, t1) | (name, t1) <- s1, name `notElem` map fst s2 ]
129 sd1 = [ (name, t1) | (name, t1) <- s1, name `notElem` map fst s2 ]
128 sd2 = [ (name, t2) | (name, t2) <- s2, name `notElem` map fst s1 ]
130 sd2 = [ (name, t2) | (name, t2) <- s2, name `notElem` map fst s1 ]
130− forM_ int (uncurry (?=))
132+ forM_ int (uncurry (=?=))
131 unifyRows' sd1 r1' sd2 r2'
133 unifyRows' sd1 r1' sd2 r2'
133− unifyRows' :: [(String, Type)] -> Type -> [(String, Type)] -> Type -> UnifyT Check ()
135+ unifyRows' :: [(String, Type)] -> Type -> [(String, Type)] -> Type -> UnifyT Type Check ()
134− unifyRows' [] (TUnknown u) sd r = replace u (rowFromList (sd, r))
136+ unifyRows' [] (TUnknown u) sd r = u =:= rowFromList (sd, r)
135− unifyRows' sd r [] (TUnknown u) = replace u (rowFromList (sd, r))
137+ unifyRows' sd r [] (TUnknown u) = u =:= rowFromList (sd, r)
136 unifyRows' ((name, ty):row) r others u@(TUnknown un) = do
138 unifyRows' ((name, ty):row) r others u@(TUnknown un) = do
137 occursCheck un ty
139 occursCheck un ty
138 forM_ row $ \(_, t) -> occursCheck un t
140 forM_ row $ \(_, t) -> occursCheck un t
139 u' <- fresh
141 u' <- fresh
140− u ?= RCons name ty u'
142+ u =?= RCons name ty u'
141 unifyRows' row r others u'
143 unifyRows' row r others u'
142 unifyRows' [] REmpty [] REmpty = return ()
144 unifyRows' [] REmpty [] REmpty = return ()
143 unifyRows' [] (TypeVar v1) [] (TypeVar v2) | v1 == v2 = return ()
145 unifyRows' [] (TypeVar v1) [] (TypeVar v2) | v1 == v2 = return ()
@@ -156,7 +158,7 @@ typeConstructorsAreEqual env moduleName = (==) `on` canonicalizeType moduleName
157typesOf :: ModuleName -> [(Ident, Value)] -> Check [(Ident, (Value, Type))]
159typesOf :: ModuleName -> [(Ident, Value)] -> Check [(Ident, (Value, Type))]
158typesOf moduleName vals = do
160typesOf moduleName vals = do
159− tys <- liftUnify $ do
161+ tys <- fmap tidyUp . liftUnify $ do
160 let es = map isTyped vals
162 let es = map isTyped vals
161 typed = filter (isJust . snd . snd) es
163 typed = filter (isJust . snd . snd) es
162 untyped = filter (isNothing . snd . snd) es
164 untyped = filter (isNothing . snd . snd) es
@@ -174,11 +176,11 @@ typesOf moduleName vals = do
174 return (ident, (val', ty'))
176 return (ident, (val', ty'))
175 (ident, (val, Nothing)) -> do
177 (ident, (val, Nothing)) -> do
176 TypedValue _ val' ty <- bindNames dict $ infer val
178 TypedValue _ val' ty <- bindNames dict $ infer val
177− ty ?= fromMaybe (error "name not found in dictionary") (lookup ident untypedDict)
179+ ty =?= fromMaybe (error "name not found in dictionary") (lookup ident untypedDict)
178 return (ident, (val', ty))
180 return (ident, (val', ty))
179 when (moduleName == ModuleName (ProperName "Main") && fst e == Ident "main") $ do
181 when (moduleName == ModuleName (ProperName "Main") && fst e == Ident "main") $ do
180 [eff, a] <- replicateM 2 fresh
182 [eff, a] <- replicateM 2 fresh
181− ty ?= TypeApp (TypeApp (TypeConstructor (Qualified (Just (ModuleName (ProperName "Eff"))) (ProperName "Eff"))) eff) a
183+ ty =?= TypeApp (TypeApp (TypeConstructor (Qualified (Just (ModuleName (ProperName "Eff"))) (ProperName "Eff"))) eff) a
182 escapeCheck val ty
184 escapeCheck val ty
183 return triple
185 return triple
184 forM_ tys $ skolemEscapeCheck . snd . snd
186 forM_ tys $ skolemEscapeCheck . snd . snd
@@ -186,6 +188,8 @@ typesOf moduleName vals = do
186 val' <- replaceTypeClassDictionaries moduleName val
188 val' <- replaceTypeClassDictionaries moduleName val
187 return (ident, (overTypes (desaturateAllTypeSynonyms . setifyAll) $ val'
189 return (ident, (overTypes (desaturateAllTypeSynonyms . setifyAll) $ val'
188 , varIfUnknown . desaturateAllTypeSynonyms . setifyAll $ ty))
190 , varIfUnknown . desaturateAllTypeSynonyms . setifyAll $ ty))
192+ tidyUp (ts, sub) = map (\(i, (val, ty)) -> (i, (overTypes (sub $?) val, sub $? ty))) ts
191-- Check if a value contains a type annotation
195-- Check if a value contains a type annotation
@@ -257,14 +261,14 @@ typeHeadsAreEqual _ _ _ _ = Nothing
258-- Ensure unsolved unification variables do not escape
262-- Ensure unsolved unification variables do not escape
260−escapeCheck :: Value -> Type -> UnifyT Check ()
264+escapeCheck :: Value -> Type -> UnifyT Type Check ()
261escapeCheck value ty = do
265escapeCheck value ty = do
262 subst <- unifyCurrentSubstitution <$> UnifyT get
266 subst <- unifyCurrentSubstitution <$> UnifyT get
263− let visibleUnknowns = nub $ unknowns $ runSubstitution subst ty
267+ let visibleUnknowns = nub $ unknowns $ subst $? ty
264 let allUnknowns = findAllTypes value
268 let allUnknowns = findAllTypes value
265 forM_ allUnknowns $ \t -> do
269 forM_ allUnknowns $ \t -> do
266− let unsolvedUnknowns = nub . unknowns $ runSubstitution subst t
270+ let unsolvedUnknowns = nub . unknowns $ subst $? t
267− guardWith ("Escape check fails" ++ show ( runSubstitution subst ty, runSubstitution subst t)) $ null $ unsolvedUnknowns \\ visibleUnknowns
271+ guardWith ("Escape check fails" ++ show (subst $? ty, subst $? t)) $ null $ unsolvedUnknowns \\ visibleUnknowns
270-- Find all type annotations occuring inside a value
274-- Find all type annotations occuring inside a value
@@ -308,7 +312,7 @@ varIfUnknown ty =
308 toName = (:) 't' . show
312 toName = (:) 't' . show
309 ty' = everywhere (mkT typeToVar) $ ty
313 ty' = everywhere (mkT typeToVar) $ ty
310 typeToVar :: Type -> Type
314 typeToVar :: Type -> Type
311− typeToVar (TUnknown (TypedUnknown (Unknown u))) = TypeVar (toName u)
315+ typeToVar (TUnknown (Unknown u)) = TypeVar (toName u)
312 typeToVar t = t
316 typeToVar t = t
313 in mkForAll (sort . map (toName . runUnknown) $ unks) ty'
317 in mkForAll (sort . map (toName . runUnknown) $ unks) ty'
@@ -321,14 +325,14 @@ replaceAllTypeVars = foldl' (\f (name, ty) -> replaceTypeVars name ty . f) id
322-- Replace named type variables with new unification variables
326-- Replace named type variables with new unification variables
324−replaceAllVarsWithUnknowns :: Type -> UnifyT Check Type
328+replaceAllVarsWithUnknowns :: Type -> UnifyT Type Check Type
325replaceAllVarsWithUnknowns (ForAll ident ty) = replaceVarWithUnknown ident ty >>= replaceAllVarsWithUnknowns
329replaceAllVarsWithUnknowns (ForAll ident ty) = replaceVarWithUnknown ident ty >>= replaceAllVarsWithUnknowns
326replaceAllVarsWithUnknowns ty = return ty
330replaceAllVarsWithUnknowns ty = return ty
329-- Replace a single type variable with a new unification variable
333-- Replace a single type variable with a new unification variable
331−replaceVarWithUnknown :: String -> Type -> UnifyT Check Type
335+replaceVarWithUnknown :: String -> Type -> UnifyT Type Check Type
332replaceVarWithUnknown ident ty = do
336replaceVarWithUnknown ident ty = do
333 tu <- fresh
337 tu <- fresh
334 return $ replaceTypeVars ident tu $ ty
338 return $ replaceTypeVars ident tu $ ty
@@ -356,7 +360,7 @@ desaturateAllTypeSynonyms = everywhere (mkT replaceSaturatedTypeSynonym)
357-- Replace a type synonym and its arguments with the aliased type
361-- Replace a type synonym and its arguments with the aliased type
359−expandTypeSynonym :: Qualified ProperName -> [Type] -> UnifyT Check Type
363+expandTypeSynonym :: Qualified ProperName -> [Type] -> UnifyT Type Check Type
360expandTypeSynonym name args = do
364expandTypeSynonym name args = do
361 env <- getEnv
365 env <- getEnv
362 Just moduleName <- checkCurrentModule <$> get
366 Just moduleName <- checkCurrentModule <$> get
@@ -373,20 +377,20 @@ ensureNoDuplicateProperties ps = guardWith "Duplicate property names" $ length (
374-- Infer a type for a value, rethrowing any error to provide a more useful error message
378-- Infer a type for a value, rethrowing any error to provide a more useful error message
376−infer :: Value -> UnifyT Check Value
380+infer :: Value -> UnifyT Type Check Value
377infer val = rethrow (\e -> "Error inferring type of term " ++ prettyPrintValue val ++ ":\n" ++ e) $ infer' val
381infer val = rethrow (\e -> "Error inferring type of term " ++ prettyPrintValue val ++ ":\n" ++ e) $ infer' val
380-- Infer a type for a value
384-- Infer a type for a value
382−infer' :: Value -> UnifyT Check Value
386+infer' :: Value -> UnifyT Type Check Value
383infer' v@(NumericLiteral _) = return $ TypedValue True v tyNumber
387infer' v@(NumericLiteral _) = return $ TypedValue True v tyNumber
384infer' v@(StringLiteral _) = return $ TypedValue True v tyString
388infer' v@(StringLiteral _) = return $ TypedValue True v tyString
385infer' v@(BooleanLiteral _) = return $ TypedValue True v tyBoolean
389infer' v@(BooleanLiteral _) = return $ TypedValue True v tyBoolean
386infer' (ArrayLiteral vals) = do
390infer' (ArrayLiteral vals) = do
387 ts <- mapM infer vals
391 ts <- mapM infer vals
388 els <- fresh
392 els <- fresh
389− forM_ ts $ \(TypedValue _ _ t) -> els ?= TypeApp tyArray t
393+ forM_ ts $ \(TypedValue _ _ t) -> els =?= TypeApp tyArray t
390 return $ TypedValue True (ArrayLiteral ts) els
394 return $ TypedValue True (ArrayLiteral ts) els
391infer' (ObjectLiteral ps) = do
395infer' (ObjectLiteral ps) = do
392 ensureNoDuplicateProperties ps
396 ensureNoDuplicateProperties ps
@@ -454,7 +458,7 @@ infer' (IfThenElse cond th el) = do
454 cond' <- check cond tyBoolean
458 cond' <- check cond tyBoolean
455 v2@(TypedValue _ _ t2) <- infer th
459 v2@(TypedValue _ _ t2) <- infer th
456 v3@(TypedValue _ _ t3) <- infer el
460 v3@(TypedValue _ _ t3) <- infer el
457− t2 ?= t3
461+ t2 =?= t3
458 return $ TypedValue True (IfThenElse cond' v2 v3) t2
462 return $ TypedValue True (IfThenElse cond' v2 v3) t2
459infer' (TypedValue checkType val ty) = do
463infer' (TypedValue checkType val ty) = do
460 Just moduleName <- checkCurrentModule <$> get
464 Just moduleName <- checkCurrentModule <$> get
@@ -468,7 +472,7 @@ infer' _ = error "Invalid argument to infer"
469-- Infer the type of a property inside a record with a given type
473-- Infer the type of a property inside a record with a given type
471−inferProperty :: Type -> String -> UnifyT Check (Maybe Type)
475+inferProperty :: Type -> String -> UnifyT Type Check (Maybe Type)
472inferProperty (Object row) prop = do
476inferProperty (Object row) prop = do
473 let (props, _) = rowToList row
477 let (props, _) = rowToList row
474 return $ lookup prop props
478 return $ lookup prop props
@@ -483,11 +487,11 @@ inferProperty _ _ = return Nothing
484-- Infer the types of variables brought into scope by a binder
488-- Infer the types of variables brought into scope by a binder
486−inferBinder :: Type -> Binder -> UnifyT Check (M.Map Ident Type)
490+inferBinder :: Type -> Binder -> UnifyT Type Check (M.Map Ident Type)
487inferBinder _ NullBinder = return M.empty
491inferBinder _ NullBinder = return M.empty
488−inferBinder val (StringBinder _) = val ?= tyString >> return M.empty
492+inferBinder val (StringBinder _) = val =?= tyString >> return M.empty
489−inferBinder val (NumberBinder _) = val ?= tyNumber >> return M.empty
493+inferBinder val (NumberBinder _) = val =?= tyNumber >> return M.empty
490−inferBinder val (BooleanBinder _) = val ?= tyBoolean >> return M.empty
494+inferBinder val (BooleanBinder _) = val =?= tyBoolean >> return M.empty
491inferBinder val (VarBinder name) = return $ M.singleton name val
495inferBinder val (VarBinder name) = return $ M.singleton name val
492inferBinder val (NullaryBinder ctor) = do
496inferBinder val (NullaryBinder ctor) = do
493 env <- getEnv
497 env <- getEnv
@@ -513,11 +517,11 @@ inferBinder val (ObjectBinder props) = do
513 row <- fresh
517 row <- fresh
514 rest <- fresh
518 rest <- fresh
515 m1 <- inferRowProperties row rest props
519 m1 <- inferRowProperties row rest props
516− val ?= Object row
520+ val =?= Object row
517 return m1
521 return m1
519− inferRowProperties :: Type -> Type -> [(String, Binder)] -> UnifyT Check (M.Map Ident Type)
523+ inferRowProperties :: Type -> Type -> [(String, Binder)] -> UnifyT Type Check (M.Map Ident Type)
520− inferRowProperties nrow row [] = nrow ?= row >> return M.empty
524+ inferRowProperties nrow row [] = nrow =?= row >> return M.empty
521 inferRowProperties nrow row ((name, binder):binders) = do
525 inferRowProperties nrow row ((name, binder):binders) = do
522 propTy <- fresh
526 propTy <- fresh
523 m1 <- inferBinder propTy binder
527 m1 <- inferBinder propTy binder
@@ -526,13 +530,13 @@ inferBinder val (ObjectBinder props) = do
526inferBinder val (ArrayBinder binders) = do
530inferBinder val (ArrayBinder binders) = do
527 el <- fresh
531 el <- fresh
528 m1 <- M.unions <$> mapM (inferBinder el) binders
532 m1 <- M.unions <$> mapM (inferBinder el) binders
529− val ?= TypeApp tyArray el
533+ val =?= TypeApp tyArray el
530 return m1
534 return m1
531inferBinder val (ConsBinder headBinder tailBinder) = do
535inferBinder val (ConsBinder headBinder tailBinder) = do
532 el <- fresh
536 el <- fresh
533 m1 <- inferBinder el headBinder
537 m1 <- inferBinder el headBinder
534 m2 <- inferBinder val tailBinder
538 m2 <- inferBinder val tailBinder
535− val ?= TypeApp tyArray el
539+ val =?= TypeApp tyArray el
536 return $ m1 `M.union` m2
540 return $ m1 `M.union` m2
537inferBinder val (NamedBinder name binder) = do
541inferBinder val (NamedBinder name binder) = do
538 m <- inferBinder val binder
542 m <- inferBinder val binder
@@ -541,7 +545,7 @@ inferBinder val (NamedBinder name binder) = do
542-- Check the types of the return values in a set of binders in a case statement
546-- Check the types of the return values in a set of binders in a case statement
544−checkBinders :: [Type] -> Type -> [([Binder], Maybe Guard, Value)] -> UnifyT Check [([Binder], Maybe Guard, Value)]
548+checkBinders :: [Type] -> Type -> [([Binder], Maybe Guard, Value)] -> UnifyT Type Check [([Binder], Maybe Guard, Value)]
545checkBinders _ _ [] = return []
549checkBinders _ _ [] = return []
546checkBinders nvals ret ((binders, grd, val):bs) = do
550checkBinders nvals ret ((binders, grd, val):bs) = do
547 Just moduleName <- checkCurrentModule <$> get
551 Just moduleName <- checkCurrentModule <$> get
@@ -559,7 +563,7 @@ checkBinders nvals ret ((binders, grd, val):bs) = do
560-- Check that a local variable name is not already used
564-- Check that a local variable name is not already used
562−assignVariable :: Ident -> UnifyT Check ()
566+assignVariable :: Ident -> UnifyT Type Check ()
563assignVariable name = do
567assignVariable name = do
564 env <- checkEnv <$> get
568 env <- checkEnv <$> get
565 Just moduleName <- checkCurrentModule <$> get
569 Just moduleName <- checkCurrentModule <$> get
@@ -571,7 +575,7 @@ assignVariable name = do
571-- Check the type of the return values of a statement, returning whether or not the statement returns on
575-- Check the type of the return values of a statement, returning whether or not the statement returns on
572-- all code paths
576-- all code paths
574−checkStatement :: M.Map Ident Type -> Type -> Statement -> UnifyT Check (Bool, M.Map Ident Type, Statement)
578+checkStatement :: M.Map Ident Type -> Type -> Statement -> UnifyT Type Check (Bool, M.Map Ident Type, Statement)
575checkStatement mass _ (VariableIntroduction name val) = do
579checkStatement mass _ (VariableIntroduction name val) = do
576 assignVariable name
580 assignVariable name
577 val'@(TypedValue _ _ t) <- infer val
581 val'@(TypedValue _ _ t) <- infer val
@@ -580,7 +584,7 @@ checkStatement mass _ (Assignment ident val) = do
580 val'@(TypedValue _ _ t) <- infer val
584 val'@(TypedValue _ _ t) <- infer val
581 case M.lookup ident mass of
585 case M.lookup ident mass of
582 Nothing -> throwError $ "No local variable with name " ++ show ident
586 Nothing -> throwError $ "No local variable with name " ++ show ident
583− Just ty -> do t ?= ty
587+ Just ty -> do t =?= ty
584 return (False, mass, Assignment ident val')
588 return (False, mass, Assignment ident val')
585checkStatement mass ret (While val inner) = do
589checkStatement mass ret (While val inner) = do
586 val' <- check val tyBoolean
590 val' <- check val tyBoolean
@@ -603,7 +607,7 @@ checkStatement mass ret (Return val) = do
604-- Check the type of an if-then-else statement
608-- Check the type of an if-then-else statement
606−checkIfStatement :: M.Map Ident Type -> Type -> IfStatement -> UnifyT Check (Bool, IfStatement)
610+checkIfStatement :: M.Map Ident Type -> Type -> IfStatement -> UnifyT Type Check (Bool, IfStatement)
607checkIfStatement mass ret (IfStatement val thens Nothing) = do
611checkIfStatement mass ret (IfStatement val thens Nothing) = do
608 val' <- check val tyBoolean
612 val' <- check val tyBoolean
609 (_, _, thens') <- checkBlock mass ret thens
613 (_, _, thens') <- checkBlock mass ret thens
@@ -617,7 +621,7 @@ checkIfStatement mass ret (IfStatement val thens (Just elses)) = do
618-- Check the type of an else statement
622-- Check the type of an else statement
620−checkElseStatement :: M.Map Ident Type -> Type -> ElseStatement -> UnifyT Check (Bool, ElseStatement)
624+checkElseStatement :: M.Map Ident Type -> Type -> ElseStatement -> UnifyT Type Check (Bool, ElseStatement)
621checkElseStatement mass ret (Else elses) = do
625checkElseStatement mass ret (Else elses) = do
622 (allCodePathsReturn, _, elses') <- checkBlock mass ret elses
626 (allCodePathsReturn, _, elses') <- checkBlock mass ret elses
623 return (allCodePathsReturn, Else elses')
627 return (allCodePathsReturn, Else elses')
@@ -626,7 +630,7 @@ checkElseStatement mass ret (ElseIf ifst) = (id *** ElseIf) <$> checkIfStatement
627-- Check the type of the return value of a block of statements
631-- Check the type of the return value of a block of statements
629−checkBlock :: M.Map Ident Type -> Type -> [Statement] -> UnifyT Check (Bool, M.Map Ident Type, [Statement])
633+checkBlock :: M.Map Ident Type -> Type -> [Statement] -> UnifyT Type Check (Bool, M.Map Ident Type, [Statement])
630checkBlock mass _ [] = return (False, mass, [])
634checkBlock mass _ [] = return (False, mass, [])
631checkBlock mass ret (s:ss) = do
635checkBlock mass ret (s:ss) = do
632 Just moduleName <- checkCurrentModule <$> get
636 Just moduleName <- checkCurrentModule <$> get
@@ -641,7 +645,7 @@ checkBlock mass ret (s:ss) = do
642-- Skolemize a type variable by replacing its instances with fresh skolem constants
646-- Skolemize a type variable by replacing its instances with fresh skolem constants
644−skolemize :: String -> Type -> UnifyT Check Type
648+skolemize :: String -> Type -> UnifyT Type Check Type
645skolemize ident ty = do
649skolemize ident ty = do
646 tsk <- Skolem . runUnknown <$> fresh'
650 tsk <- Skolem . runUnknown <$> fresh'
647 return $ replaceTypeVars ident tsk ty
651 return $ replaceTypeVars ident tsk ty
@@ -649,7 +653,7 @@ skolemize ident ty = do
650-- Check the type of a value, rethrowing errors to provide a better error message
654-- Check the type of a value, rethrowing errors to provide a better error message
652−check :: Value -> Type -> UnifyT Check Value
656+check :: Value -> Type -> UnifyT Type Check Value
653check val ty = rethrow errorMessage $ check' val ty
657check val ty = rethrow errorMessage $ check' val ty
655 errorMessage msg =
659 errorMessage msg =
@@ -663,7 +667,7 @@ check val ty = rethrow errorMessage $ check' val ty
664-- Check the type of a value
668-- Check the type of a value
666−check' :: Value -> Type -> UnifyT Check Value
670+check' :: Value -> Type -> UnifyT Type Check Value
667check' val (ForAll idents ty) = do
671check' val (ForAll idents ty) = do
668 sk <- skolemize idents ty
672 sk <- skolemize idents ty
669 check val sk
673 check val sk
@@ -682,7 +686,7 @@ check' val u@(TUnknown _) = do
682 val'@(TypedValue _ _ ty) <- infer val
686 val'@(TypedValue _ _ ty) <- infer val
683 -- Don't unify an unknown with an inferred polytype
687 -- Don't unify an unknown with an inferred polytype
684 ty' <- replaceAllVarsWithUnknowns ty
688 ty' <- replaceAllVarsWithUnknowns ty
685− ty' ?= u
689+ ty' =?= u
686 return val'
690 return val'
687check' v@(NumericLiteral _) t | t == tyNumber = return v
691check' v@(NumericLiteral _) t | t == tyNumber = return v
688check' v@(StringLiteral _) t | t == tyString = return v
692check' v@(StringLiteral _) t | t == tyString = return v
@@ -765,10 +769,10 @@ check' val ty = throwError $ prettyPrintValue val ++ " does not have type " ++ p
766-- The @lax@ parameter controls whether or not every record member has to be provided. For object updates, this is not the case.
770-- The @lax@ parameter controls whether or not every record member has to be provided. For object updates, this is not the case.
768−checkProperties :: [(String, Value)] -> Type -> Bool -> UnifyT Check [(String, Value)]
772+checkProperties :: [(String, Value)] -> Type -> Bool -> UnifyT Type Check [(String, Value)]
769checkProperties ps row lax = let (ts, r') = rowToList row in go ps ts r' where
773checkProperties ps row lax = let (ts, r') = rowToList row in go ps ts r' where
770 go [] [] REmpty = return []
774 go [] [] REmpty = return []
771− go [] [] u@(TUnknown _) = do u ?= REmpty
775+ go [] [] u@(TUnknown _) = do u =?= REmpty
772 return []
776 return []
773 go [] [] (Skolem _) | lax = return []
777 go [] [] (Skolem _) | lax = return []
774 go [] ((p, _): _) _ | lax = return []
778 go [] ((p, _): _) _ | lax = return []
@@ -777,7 +781,7 @@ checkProperties ps row lax = let (ts, r') = rowToList row in go ps ts r' where
777 go ((p,v):ps') [] u@(TUnknown _) = do
781 go ((p,v):ps') [] u@(TUnknown _) = do
778 v'@(TypedValue _ _ ty) <- infer v
782 v'@(TypedValue _ _ ty) <- infer v
779 rest <- fresh
783 rest <- fresh
780− u ?= RCons p ty rest
784+ u =?= RCons p ty rest
781 ps'' <- go ps' [] rest
785 ps'' <- go ps' [] rest
782 return $ (p, v') : ps''
786 return $ (p, v') : ps''
783 go ((p,v):ps') ts r =
787 go ((p,v):ps') ts r =
@@ -785,7 +789,7 @@ checkProperties ps row lax = let (ts, r') = rowToList row in go ps ts r' where
785 Nothing -> do
789 Nothing -> do
786 v'@(TypedValue _ _ ty) <- infer v
790 v'@(TypedValue _ _ ty) <- infer v
787 rest <- fresh
791 rest <- fresh
788− r ?= RCons p ty rest
792+ r =?= RCons p ty rest
789 ps'' <- go ps' ts rest
793 ps'' <- go ps' ts rest
790 return $ (p, v') : ps''
794 return $ (p, v') : ps''
791 Just ty -> do
795 Just ty -> do
@@ -797,7 +801,7 @@ checkProperties ps row lax = let (ts, r') = rowToList row in go ps ts r' where
798-- Check the type of a function application, rethrowing errors to provide a better error message
802-- Check the type of a function application, rethrowing errors to provide a better error message
800−checkFunctionApplication :: Value -> Type -> Value -> Type -> UnifyT Check Value
804+checkFunctionApplication :: Value -> Type -> Value -> Type -> UnifyT Type Check Value
801checkFunctionApplication fn fnTy arg ret = rethrow errorMessage $ checkFunctionApplication' fn fnTy arg ret
805checkFunctionApplication fn fnTy arg ret = rethrow errorMessage $ checkFunctionApplication' fn fnTy arg ret
803 errorMessage msg = "Error applying function of type "
807 errorMessage msg = "Error applying function of type "
@@ -808,9 +812,9 @@ checkFunctionApplication fn fnTy arg ret = rethrow errorMessage $ checkFunctionA
809-- Check the type of a function application
813-- Check the type of a function application
811−checkFunctionApplication' :: Value -> Type -> Value -> Type -> UnifyT Check Value
815+checkFunctionApplication' :: Value -> Type -> Value -> Type -> UnifyT Type Check Value
812checkFunctionApplication' fn (TypeApp (TypeApp tyFunction' argTy) retTy) arg ret = do
816checkFunctionApplication' fn (TypeApp (TypeApp tyFunction' argTy) retTy) arg ret = do
813− tyFunction' ?= tyFunction
817+ tyFunction' =?= tyFunction
814 arg' <- check arg argTy
818 arg' <- check arg argTy
815 _ <- subsumes Nothing retTy ret
819 _ <- subsumes Nothing retTy ret
816 return $ App fn arg'
820 return $ App fn arg'
@@ -822,7 +826,7 @@ checkFunctionApplication' fn u@(TUnknown _) arg ret = do
822 TypedValue _ v t <- infer arg
826 TypedValue _ v t <- infer arg
823 TypedValue True v <$> replaceAllVarsWithUnknowns t
827 TypedValue True v <$> replaceAllVarsWithUnknowns t
824 let ty = (\(TypedValue _ _ t) -> t) arg'
828 let ty = (\(TypedValue _ _ t) -> t) arg'
825− u ?= function ty ret
829+ u =?= function ty ret
826 return $ App fn arg'
830 return $ App fn arg'
827checkFunctionApplication' fn (SaturatedTypeSynonym name tyArgs) arg ret = do
831checkFunctionApplication' fn (SaturatedTypeSynonym name tyArgs) arg ret = do
828 ty <- expandTypeSynonym name tyArgs
832 ty <- expandTypeSynonym name tyArgs
@@ -840,7 +844,7 @@ checkFunctionApplication' _ fnTy arg ret = throwError $ "Applying a function of
841-- Check whether one type subsumes another, rethrowing errors to provide a better error message
845-- Check whether one type subsumes another, rethrowing errors to provide a better error message
843−subsumes :: Maybe Value -> Type -> Type -> UnifyT Check (Maybe Value)
847+subsumes :: Maybe Value -> Type -> Type -> UnifyT Type Check (Maybe Value)
844subsumes val ty1 ty2 = rethrow errorMessage $ subsumes' val ty1 ty2
848subsumes val ty1 ty2 = rethrow errorMessage $ subsumes' val ty1 ty2
846 errorMessage msg = "Error checking that type "
850 errorMessage msg = "Error checking that type "
@@ -852,7 +856,7 @@ subsumes val ty1 ty2 = rethrow errorMessage $ subsumes' val ty1 ty2
853-- Check whether one type subsumes another
857-- Check whether one type subsumes another
855−subsumes' :: Maybe Value -> Type -> Type -> UnifyT Check (Maybe Value)
859+subsumes' :: Maybe Value -> Type -> Type -> UnifyT Type Check (Maybe Value)
856subsumes' val (ForAll ident ty1) ty2 = do
860subsumes' val (ForAll ident ty1) ty2 = do
857 replaced <- replaceVarWithUnknown ident ty1
861 replaced <- replaceVarWithUnknown ident ty1
858 subsumes val replaced ty2
862 subsumes val replaced ty2
@@ -873,6 +877,6 @@ subsumes' (Just val) (ConstrainedType constraints ty1) ty2 = do
873 _ <- subsumes' Nothing ty1 ty2
877 _ <- subsumes' Nothing ty1 ty2
874 return . Just $ foldl App val (map (flip TypeClassDictionary dicts) (qualifyAllUnqualifiedNames moduleName env constraints))
878 return . Just $ foldl App val (map (flip TypeClassDictionary dicts) (qualifyAllUnqualifiedNames moduleName env constraints))
875subsumes' val ty1 ty2 = do
879subsumes' val ty1 ty2 = do
876− ty1 ?= ty2
880+ ty1 =?= ty2
877 return val
881 return val