@@ -14,7 +14,9 @@
14-----------------------------------------------------------------------------
14-----------------------------------------------------------------------------
16{-# OPTIONS_GHC -fno-warn-orphans #-}
16{-# OPTIONS_GHC -fno-warn-orphans #-}
17−{-# LANGUAGE DeriveDataTypeable, FlexibleContexts #-}
17+{-# LANGUAGE DeriveDataTypeable #-}
18+{-# LANGUAGE FlexibleContexts #-}
19+{-# LANGUAGE MultiParamTypeClasses #-}
19module Language.PureScript.TypeChecker.Types (
21module Language.PureScript.TypeChecker.Types (
@@ -53,11 +55,11 @@ import Language.PureScript.TypeChecker.Monad
53import Language.PureScript.TypeChecker.Kinds
55import Language.PureScript.TypeChecker.Kinds
54import Language.PureScript.TypeChecker.Synonyms
56import Language.PureScript.TypeChecker.Synonyms
55import Language.PureScript.Pretty
57import Language.PureScript.Pretty
56−import Language.PureScript.Unknown
58import Control.Monad.State
59import Control.Monad.State
59import Control.Monad.Error
60import Control.Monad.Error
60import Control.Monad.Reader
61import Control.Monad.Reader
62+import Control.Monad.Unify
62import Control.Applicative
64import Control.Applicative
63import Control.Arrow (Arrow(..))
65import Control.Arrow (Arrow(..))
@@ -65,32 +67,16 @@ import Control.Arrow (Arrow(..))
65import qualified Data.Map as M
67import qualified Data.Map as M
66import Data.Function (on)
68import Data.Function (on)
68−instance Unifiable Type where
70+instance Unifiable Check Type where
69 unknown = TUnknown
71 unknown = TUnknown
70 isUnknown (TUnknown u) = Just u
72 isUnknown (TUnknown u) = Just u
71 isUnknown _ = Nothing
73 isUnknown _ = Nothing
72− (~~) = unifyTypes
74+ (?=) = unifyTypes
73− apply s (TUnknown u) = runSubstitution s u
74− apply s (SaturatedTypeSynonym name tys) = SaturatedTypeSynonym name $ map (apply s) tys
75− apply s (ForAll idents ty) = ForAll idents $ apply s ty
76− apply s (Object r) = Object (apply s r)
77− apply s (Function args ret) = Function (map (apply s) args) (apply s ret)
78− apply s (TypeApp t1 t2) = TypeApp (apply s t1) (apply s t2)
79− apply s (RCons name ty r) = RCons name (apply s ty) (apply s r)
81− unknowns (TUnknown (Unknown u)) = [u]
82− unknowns (SaturatedTypeSynonym _ tys) = concatMap unknowns tys
83− unknowns (ForAll _ ty) = unknowns ty
84− unknowns (Object r) = unknowns r
85− unknowns (Function args ret) = concatMap unknowns args ++ unknowns ret
86− unknowns (TypeApp t1 t2) = unknowns t1 ++ unknowns t2
87− unknowns (RCons _ ty r) = unknowns ty ++ unknowns r
91-- Unify two types, updating the current substitution
77-- Unify two types, updating the current substitution
93−unifyTypes :: Type -> Type -> Subst ()
79+unifyTypes :: Type -> Type -> UnifyT Check ()
94unifyTypes t1 t2 = rethrow (\e -> "Error unifying type " ++ prettyPrintType t1 ++ " with type " ++ prettyPrintType t2 ++ ":\n" ++ e) $ do
80unifyTypes t1 t2 = rethrow (\e -> "Error unifying type " ++ prettyPrintType t1 ++ " with type " ++ prettyPrintType t2 ++ ":\n" ++ e) $ do
95 unifyTypes' t1 t2
81 unifyTypes' t1 t2
@@ -113,7 +99,7 @@ unifyTypes t1 t2 = rethrow (\e -> "Error unifying type " ++ prettyPrintType t1 +
113 unifyTypes' String String = return ()
99 unifyTypes' String String = return ()
114 unifyTypes' Boolean Boolean = return ()
100 unifyTypes' Boolean Boolean = return ()
115 unifyTypes' Array Array = return ()
101 unifyTypes' Array Array = return ()
116− unifyTypes' (Object row1) (Object row2) = row1 ~~ row2
102+ unifyTypes' (Object row1) (Object row2) = row1 ?= row2
117 unifyTypes' (Function args1 ret1) (Function args2 ret2) = do
103 unifyTypes' (Function args1 ret1) (Function args2 ret2) = do
118 guardWith "Function applied to incorrect number of args" $ length args1 == length args2
104 guardWith "Function applied to incorrect number of args" $ length args1 == length args2
119 zipWithM_ unifyTypes args1 args2
105 zipWithM_ unifyTypes args1 args2
@@ -121,7 +107,7 @@ unifyTypes t1 t2 = rethrow (\e -> "Error unifying type " ++ prettyPrintType t1 +
121 unifyTypes' (TypeVar v1) (TypeVar v2) | v1 == v2 = return ()
107 unifyTypes' (TypeVar v1) (TypeVar v2) | v1 == v2 = return ()
122 unifyTypes' (TypeConstructor c1) (TypeConstructor c2) = do
108 unifyTypes' (TypeConstructor c1) (TypeConstructor c2) = do
123 env <- getEnv
109 env <- getEnv
124− moduleName <- substCurrentModule `fmap` ask
110+ Just moduleName <- checkCurrentModule <$> get
125 guardWith ("Cannot unify " ++ show c1 ++ " with " ++ show c2 ++ ".") (typeConstructorsAreEqual env moduleName c1 c2)
111 guardWith ("Cannot unify " ++ show c1 ++ " with " ++ show c2 ++ ".") (typeConstructorsAreEqual env moduleName c1 c2)
126 unifyTypes' (TypeApp t3 t4) (TypeApp t5 t6) = do
112 unifyTypes' (TypeApp t3 t4) (TypeApp t5 t6) = do
127 t3 `unifyTypes` t5
113 t3 `unifyTypes` t5
@@ -140,7 +126,7 @@ unifyTypes t1 t2 = rethrow (\e -> "Error unifying type " ++ prettyPrintType t1 +
140-- trailing row unification variable, if appropriate, otherwise leftover labels result in a unification
126-- trailing row unification variable, if appropriate, otherwise leftover labels result in a unification
143−unifyRows :: Type -> Type -> Subst ()
129+unifyRows :: Type -> Type -> UnifyT Check ()
144unifyRows r1 r2 =
130unifyRows r1 r2 =
146 (s1, r1') = rowToList r1
132 (s1, r1') = rowToList r1
@@ -149,17 +135,17 @@ unifyRows r1 r2 =
149 sd1 = [ (name, t1) | (name, t1) <- s1, name `notElem` map fst s2 ]
135 sd1 = [ (name, t1) | (name, t1) <- s1, name `notElem` map fst s2 ]
150 sd2 = [ (name, t2) | (name, t2) <- s2, name `notElem` map fst s1 ]
136 sd2 = [ (name, t2) | (name, t2) <- s2, name `notElem` map fst s1 ]
152− forM_ int (uncurry (~~))
138+ forM_ int (uncurry (?=))
153 unifyRows' sd1 r1' sd2 r2'
139 unifyRows' sd1 r1' sd2 r2'
155− unifyRows' :: [(String, Type)] -> Type -> [(String, Type)] -> Type -> Subst ()
141+ unifyRows' :: [(String, Type)] -> Type -> [(String, Type)] -> Type -> UnifyT Check ()
156 unifyRows' [] (TUnknown u) sd r = replace u (rowFromList (sd, r))
142 unifyRows' [] (TUnknown u) sd r = replace u (rowFromList (sd, r))
157 unifyRows' sd r [] (TUnknown u) = replace u (rowFromList (sd, r))
143 unifyRows' sd r [] (TUnknown u) = replace u (rowFromList (sd, r))
158 unifyRows' ((name, ty):row) r others u@(TUnknown un) = do
144 unifyRows' ((name, ty):row) r others u@(TUnknown un) = do
159 occursCheck un ty
145 occursCheck un ty
160 forM_ row $ \(_, t) -> occursCheck un t
146 forM_ row $ \(_, t) -> occursCheck un t
161 u' <- fresh
147 u' <- fresh
162− u ~~ RCons name ty u'
148+ u ?= RCons name ty u'
163 unifyRows' row r others u'
149 unifyRows' row r others u'
164 unifyRows' [] REmpty [] REmpty = return ()
150 unifyRows' [] REmpty [] REmpty = return ()
165 unifyRows' [] (TypeVar v1) [] (TypeVar v2) | v1 == v2 = return ()
151 unifyRows' [] (TypeVar v1) [] (TypeVar v2) | v1 == v2 = return ()
@@ -178,8 +164,7 @@ typeConstructorsAreEqual env moduleName = (==) `on` canonicalizeType moduleName
179typesOf :: ModuleName -> [(Ident, Value)] -> Check [(Ident, (Value, Type))]
165typesOf :: ModuleName -> [(Ident, Value)] -> Check [(Ident, (Value, Type))]
180typesOf moduleName vals = do
166typesOf moduleName vals = do
181− tys <- fmap (\(tys, s) -> map (\(ident, (val, ty)) -> (ident, (overTypes (apply s) val, apply s ty))) tys)
167+ tys <- liftUnify $ do
182− . runSubst (SubstContext moduleName) $ do
183 let es = map isTyped vals
168 let es = map isTyped vals
184 typed = filter (isJust . snd . snd) es
169 typed = filter (isJust . snd . snd) es
185 untyped = filter (isNothing . snd . snd) es
170 untyped = filter (isNothing . snd . snd) es
@@ -197,11 +182,11 @@ typesOf moduleName vals = do
197 return (ident, (val', ty'))
182 return (ident, (val', ty'))
198 (ident, (val, Nothing)) -> do
183 (ident, (val, Nothing)) -> do
199 TypedValue _ val' ty <- bindNames dict $ infer val
184 TypedValue _ val' ty <- bindNames dict $ infer val
200− ty ~~ fromMaybe (error "name not found in dictionary") (lookup ident untypedDict)
185+ ty ?= fromMaybe (error "name not found in dictionary") (lookup ident untypedDict)
201 return (ident, (val', ty))
186 return (ident, (val', ty))
202 when (moduleName == ModuleName (ProperName "Main") && fst e == Ident "main") $ do
187 when (moduleName == ModuleName (ProperName "Main") && fst e == Ident "main") $ do
203 [eff, a] <- replicateM 2 fresh
188 [eff, a] <- replicateM 2 fresh
204− ty ~~ TypeApp (TypeApp (TypeConstructor (Qualified (Just (ModuleName (ProperName "Eff"))) (ProperName "Eff"))) eff) a
189+ ty ?= TypeApp (TypeApp (TypeConstructor (Qualified (Just (ModuleName (ProperName "Eff"))) (ProperName "Eff"))) eff) a
205 escapeCheck val ty
190 escapeCheck val ty
206 return triple
191 return triple
207 forM_ tys $ skolemEscapeCheck . snd . snd
192 forM_ tys $ skolemEscapeCheck . snd . snd
@@ -279,14 +264,14 @@ typeHeadsAreEqual _ _ _ _ = Nothing
280-- Ensure unsolved unification variables do not escape
265-- Ensure unsolved unification variables do not escape
282−escapeCheck :: Value -> Type -> Subst ()
267+escapeCheck :: Value -> Type -> UnifyT Check ()
283escapeCheck value ty = do
268escapeCheck value ty = do
284− subst <- substSubst <$> getSubstState
269+ subst <- unifyCurrentSubstitution <$> UnifyT get
285− let visibleUnknowns = nub $ unknowns $ apply subst ty
270+ let visibleUnknowns = nub $ unknowns $ runSubstitution subst ty
286 let allUnknowns = findAllTypes value
271 let allUnknowns = findAllTypes value
287 forM_ allUnknowns $ \t -> do
272 forM_ allUnknowns $ \t -> do
288− let unsolvedUnknowns = nub . unknowns $ apply subst t
273+ let unsolvedUnknowns = nub . unknowns $ runSubstitution subst t
289− guardWith "Escape check fails" $ null $ unsolvedUnknowns \\ visibleUnknowns
274+ guardWith ("Escape check fails" ++ show ( runSubstitution subst ty, runSubstitution subst t)) $ null $ unsolvedUnknowns \\ visibleUnknowns
292-- Find all type annotations occuring inside a value
277-- Find all type annotations occuring inside a value
@@ -304,7 +289,7 @@ skolemEscapeCheck :: Type -> Check ()
304skolemEscapeCheck ty =
289skolemEscapeCheck ty =
305 case something (mkQ Nothing findSkolems) ty of
290 case something (mkQ Nothing findSkolems) ty of
306 Nothing -> return ()
291 Nothing -> return ()
307− Just _ -> throwError $ "Skolem variables cannot escape. Consider adding a type signature." ++ show ty
292+ Just _ -> throwError $ "Skolem variables cannot escape. Consider adding a type signature."
309 findSkolems (Skolem _) = return ()
294 findSkolems (Skolem _) = return ()
310 findSkolems _ = mzero
295 findSkolems _ = mzero
@@ -330,9 +315,9 @@ varIfUnknown ty =
330 toName = (:) 't' . show
315 toName = (:) 't' . show
331 ty' = everywhere (mkT typeToVar) $ ty
316 ty' = everywhere (mkT typeToVar) $ ty
332 typeToVar :: Type -> Type
317 typeToVar :: Type -> Type
333− typeToVar (TUnknown (Unknown u)) = TypeVar (toName u)
318+ typeToVar (TUnknown (TypedUnknown (Unknown u))) = TypeVar (toName u)
334 typeToVar t = t
319 typeToVar t = t
335− in mkForAll (sort . map toName $ unks) ty'
320+ in mkForAll (sort . map (toName . runUnknown) $ unks) ty'
338-- Replace named type variables with types
323-- Replace named type variables with types
@@ -343,14 +328,14 @@ replaceAllTypeVars = foldl' (\f (name, ty) -> replaceTypeVars name ty . f) id
344-- Replace named type variables with new unification variables
329-- Replace named type variables with new unification variables
346−replaceAllVarsWithUnknowns :: Type -> Subst Type
331+replaceAllVarsWithUnknowns :: Type -> UnifyT Check Type
347replaceAllVarsWithUnknowns (ForAll ident ty) = replaceVarWithUnknown ident ty >>= replaceAllVarsWithUnknowns
332replaceAllVarsWithUnknowns (ForAll ident ty) = replaceVarWithUnknown ident ty >>= replaceAllVarsWithUnknowns
348replaceAllVarsWithUnknowns ty = return ty
333replaceAllVarsWithUnknowns ty = return ty
351-- Replace a single type variable with a new unification variable
336-- Replace a single type variable with a new unification variable
353−replaceVarWithUnknown :: String -> Type -> Subst Type
338+replaceVarWithUnknown :: String -> Type -> UnifyT Check Type
354replaceVarWithUnknown ident ty = do
339replaceVarWithUnknown ident ty = do
355 tu <- fresh
340 tu <- fresh
356 return $ replaceTypeVars ident tu $ ty
341 return $ replaceTypeVars ident tu $ ty
@@ -359,10 +344,10 @@ replaceVarWithUnknown ident ty = do
359-- Replace fully applied type synonyms with the @SaturatedTypeSynonym@ data constructor, which helps generate
344-- Replace fully applied type synonyms with the @SaturatedTypeSynonym@ data constructor, which helps generate
360-- better error messages during unification.
345-- better error messages during unification.
362−replaceAllTypeSynonyms :: (Functor m, MonadState CheckState m, MonadReader SubstContext m, MonadError String m) => (D.Data d) => d -> m d
347+replaceAllTypeSynonyms :: (Functor m, MonadState CheckState m, MonadError String m) => (D.Data d) => d -> m d
363replaceAllTypeSynonyms d = do
348replaceAllTypeSynonyms d = do
364 env <- getEnv
349 env <- getEnv
365− moduleName <- substCurrentModule <$> ask
350+ Just moduleName <- checkCurrentModule <$> get
366 let syns = map (\((path, name), (args, _)) -> ((path, name), length args)) . M.toList $ typeSynonyms env
351 let syns = map (\((path, name), (args, _)) -> ((path, name), length args)) . M.toList $ typeSynonyms env
367 either throwError return $ saturateAllTypeSynonyms env moduleName syns d
352 either throwError return $ saturateAllTypeSynonyms env moduleName syns d
@@ -378,10 +363,10 @@ desaturateAllTypeSynonyms = everywhere (mkT replaceSaturatedTypeSynonym)
379-- Replace a type synonym and its arguments with the aliased type
364-- Replace a type synonym and its arguments with the aliased type
381−expandTypeSynonym :: Qualified ProperName -> [Type] -> Subst Type
366+expandTypeSynonym :: Qualified ProperName -> [Type] -> UnifyT Check Type
382expandTypeSynonym name args = do
367expandTypeSynonym name args = do
383 env <- getEnv
368 env <- getEnv
384− moduleName <- substCurrentModule `fmap` ask
369+ Just moduleName <- checkCurrentModule <$> get
385 case M.lookup (canonicalizeType moduleName env name) (typeSynonyms env) of
370 case M.lookup (canonicalizeType moduleName env name) (typeSynonyms env) of
386 Just (synArgs, body) -> return $ replaceAllTypeVars (zip synArgs args) body
371 Just (synArgs, body) -> return $ replaceAllTypeVars (zip synArgs args) body
387 Nothing -> error "Type synonym was not defined"
372 Nothing -> error "Type synonym was not defined"
@@ -395,20 +380,20 @@ ensureNoDuplicateProperties ps = guardWith "Duplicate property names" $ length (
396-- Infer a type for a value, rethrowing any error to provide a more useful error message
381-- Infer a type for a value, rethrowing any error to provide a more useful error message
398−infer :: Value -> Subst Value
383+infer :: Value -> UnifyT Check Value
399infer val = rethrow (\e -> "Error inferring type of term " ++ prettyPrintValue val ++ ":\n" ++ e) $ infer' val
384infer val = rethrow (\e -> "Error inferring type of term " ++ prettyPrintValue val ++ ":\n" ++ e) $ infer' val
402-- Infer a type for a value
387-- Infer a type for a value
404−infer' :: Value -> Subst Value
389+infer' :: Value -> UnifyT Check Value
405infer' v@(NumericLiteral _) = return $ TypedValue True v Number
390infer' v@(NumericLiteral _) = return $ TypedValue True v Number
406infer' v@(StringLiteral _) = return $ TypedValue True v String
391infer' v@(StringLiteral _) = return $ TypedValue True v String
407infer' v@(BooleanLiteral _) = return $ TypedValue True v Boolean
392infer' v@(BooleanLiteral _) = return $ TypedValue True v Boolean
408infer' (ArrayLiteral vals) = do
393infer' (ArrayLiteral vals) = do
409 ts <- mapM infer vals
394 ts <- mapM infer vals
410 els <- fresh
395 els <- fresh
411− forM_ ts $ \(TypedValue _ _ t) -> els ~~ TypeApp Array t
396+ forM_ ts $ \(TypedValue _ _ t) -> els ?= TypeApp Array t
412 return $ TypedValue True (ArrayLiteral ts) els
397 return $ TypedValue True (ArrayLiteral ts) els
413infer' (Unary op val) = do
398infer' (Unary op val) = do
414 v <- infer val
399 v <- infer val
@@ -448,7 +433,7 @@ infer' (Accessor prop val) = do
448 Just ty -> return $ TypedValue True (Accessor prop typed) ty
433 Just ty -> return $ TypedValue True (Accessor prop typed) ty
449infer' (Abs args ret) = do
434infer' (Abs args ret) = do
450 ts <- replicateM (length args) fresh
435 ts <- replicateM (length args) fresh
451− moduleName <- substCurrentModule <$> ask
436+ Just moduleName <- checkCurrentModule <$> get
452 bindLocalVariables moduleName (zip args ts) $ do
437 bindLocalVariables moduleName (zip args ts) $ do
453 body@(TypedValue _ _ bodyTy) <- infer' ret
438 body@(TypedValue _ _ bodyTy) <- infer' ret
454 return $ TypedValue True (Abs args body) $ Function ts bodyTy
439 return $ TypedValue True (Abs args body) $ Function ts bodyTy
@@ -458,7 +443,7 @@ infer' (App f args) = do
458 app <- checkFunctionApplication f' ft args ret
443 app <- checkFunctionApplication f' ft args ret
459 return $ TypedValue True app ret
444 return $ TypedValue True app ret
460infer' (Var var) = do
445infer' (Var var) = do
461− moduleName <- substCurrentModule <$> ask
446+ Just moduleName <- checkCurrentModule <$> get
462 ty <- lookupVariable moduleName var
447 ty <- lookupVariable moduleName var
463 ty' <- replaceAllTypeSynonyms ty
448 ty' <- replaceAllTypeSynonyms ty
464 case ty' of
449 case ty' of
@@ -474,7 +459,7 @@ infer' (Block ss) = do
474 return $ TypedValue True (Block ss') ret
459 return $ TypedValue True (Block ss') ret
475infer' v@(Constructor c) = do
460infer' v@(Constructor c) = do
476 env <- getEnv
461 env <- getEnv
477− moduleName <- substCurrentModule `fmap` ask
462+ Just moduleName <- checkCurrentModule <$> get
478 case M.lookup (qualify moduleName c) (dataConstructors env) of
463 case M.lookup (qualify moduleName c) (dataConstructors env) of
479 Nothing -> throwError $ "Constructor " ++ show c ++ " is undefined"
464 Nothing -> throwError $ "Constructor " ++ show c ++ " is undefined"
480 Just (ty, _) -> do ty' <- replaceAllTypeSynonyms ty
465 Just (ty, _) -> do ty' <- replaceAllTypeSynonyms ty
@@ -488,10 +473,10 @@ infer' (IfThenElse cond th el) = do
488 cond' <- check cond Boolean
473 cond' <- check cond Boolean
489 v2@(TypedValue _ _ t2) <- infer th
474 v2@(TypedValue _ _ t2) <- infer th
490 v3@(TypedValue _ _ t3) <- infer el
475 v3@(TypedValue _ _ t3) <- infer el
491− t2 ~~ t3
476+ t2 ?= t3
492 return $ TypedValue True (IfThenElse cond' v2 v3) t2
477 return $ TypedValue True (IfThenElse cond' v2 v3) t2
493infer' (TypedValue checkType val ty) = do
478infer' (TypedValue checkType val ty) = do
494− moduleName <- substCurrentModule <$> ask
479+ Just moduleName <- checkCurrentModule <$> get
495 kind <- liftCheck $ kindOf moduleName ty
480 kind <- liftCheck $ kindOf moduleName ty
496 guardWith ("Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
481 guardWith ("Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
497 ty' <- replaceAllTypeSynonyms ty
482 ty' <- replaceAllTypeSynonyms ty
@@ -502,7 +487,7 @@ infer' _ = error "Invalid argument to infer"
503-- Infer the type of a property inside a record with a given type
488-- Infer the type of a property inside a record with a given type
505−inferProperty :: Type -> String -> Subst (Maybe Type)
490+inferProperty :: Type -> String -> UnifyT Check (Maybe Type)
506inferProperty (Object row) prop = do
491inferProperty (Object row) prop = do
507 let (props, _) = rowToList row
492 let (props, _) = rowToList row
508 return $ lookup prop props
493 return $ lookup prop props
@@ -517,22 +502,22 @@ inferProperty _ _ = return Nothing
518-- Infer the type of a unary operator application
503-- Infer the type of a unary operator application
520−inferUnary :: UnaryOperator -> Value -> Subst Value
505+inferUnary :: UnaryOperator -> Value -> UnifyT Check Value
521inferUnary op (TypedValue _ val valTy) =
506inferUnary op (TypedValue _ val valTy) =
522 case fromMaybe (error "Invalid operator") $ lookup op unaryOps of
507 case fromMaybe (error "Invalid operator") $ lookup op unaryOps of
523 (valTy', resTy) -> do
508 (valTy', resTy) -> do
524− valTy' ~~ valTy
509+ valTy' ?= valTy
525 return $ TypedValue True (Unary op val) resTy
510 return $ TypedValue True (Unary op val) resTy
526inferUnary _ _ = error "Invalid arguments to inferUnary"
511inferUnary _ _ = error "Invalid arguments to inferUnary"
529-- Check the type of a unary operator application
514-- Check the type of a unary operator application
531−checkUnary :: UnaryOperator -> Value -> Type -> Subst Value
516+checkUnary :: UnaryOperator -> Value -> Type -> UnifyT Check Value
532checkUnary op val res =
517checkUnary op val res =
533 case fromMaybe (error "Invalid operator") $ lookup op unaryOps of
518 case fromMaybe (error "Invalid operator") $ lookup op unaryOps of
534 (valTy, resTy) -> do
519 (valTy, resTy) -> do
535− res ~~ resTy
520+ res ?= resTy
536 val' <- check val valTy
521 val' <- check val valTy
537 return $ Unary op val'
522 return $ Unary op val'
@@ -548,32 +533,32 @@ unaryOps = [ (Negate, (Number, Number))
549-- Infer the type of a binary operator application
534-- Infer the type of a binary operator application
551−inferBinary :: BinaryOperator -> Value -> Value -> Subst Value
536+inferBinary :: BinaryOperator -> Value -> Value -> UnifyT Check Value
552inferBinary op left@(TypedValue _ _ leftTy) right@(TypedValue _ _ rightTy) | isEqualityTest op = do
537inferBinary op left@(TypedValue _ _ leftTy) right@(TypedValue _ _ rightTy) | isEqualityTest op = do
553− leftTy ~~ rightTy
538+ leftTy ?= rightTy
554 return $ TypedValue True (Binary op left right) Boolean
539 return $ TypedValue True (Binary op left right) Boolean
555inferBinary op left@(TypedValue _ _ leftTy) right@(TypedValue _ _ rightTy) =
540inferBinary op left@(TypedValue _ _ leftTy) right@(TypedValue _ _ rightTy) =
556 case fromMaybe (error "Invalid operator") $ lookup op binaryOps of
541 case fromMaybe (error "Invalid operator") $ lookup op binaryOps of
557 (valTy, resTy) -> do
542 (valTy, resTy) -> do
558− leftTy ~~ valTy
543+ leftTy ?= valTy
559− rightTy ~~ valTy
544+ rightTy ?= valTy
560 return $ TypedValue True (Binary op left right) resTy
545 return $ TypedValue True (Binary op left right) resTy
561inferBinary _ _ _ = error "Invalid arguments to inferBinary"
546inferBinary _ _ _ = error "Invalid arguments to inferBinary"
564-- Check the type of a binary operator application
549-- Check the type of a binary operator application
566−checkBinary :: BinaryOperator -> Value -> Value -> Type -> Subst Value
551+checkBinary :: BinaryOperator -> Value -> Value -> Type -> UnifyT Check Value
567checkBinary op left right res | isEqualityTest op = do
552checkBinary op left right res | isEqualityTest op = do
568− res ~~ Boolean
553+ res ?= Boolean
569 left'@(TypedValue _ _ t1) <- infer left
554 left'@(TypedValue _ _ t1) <- infer left
570 right'@(TypedValue _ _ t2) <- infer right
555 right'@(TypedValue _ _ t2) <- infer right
571− t1 ~~ t2
556+ t1 ?= t2
572 return $ Binary op left' right'
557 return $ Binary op left' right'
573checkBinary op left right res =
558checkBinary op left right res =
574 case fromMaybe (error "Invalid operator") $ lookup op binaryOps of
559 case fromMaybe (error "Invalid operator") $ lookup op binaryOps of
575 (valTy, resTy) -> do
560 (valTy, resTy) -> do
576− res ~~ resTy
561+ res ?= resTy
577 left' <- check left valTy
562 left' <- check left valTy
578 right' <- check right valTy
563 right' <- check right valTy
579 return $ Binary op left' right'
564 return $ Binary op left' right'
@@ -613,15 +598,15 @@ binaryOps = [ (Add, (Number, Number))
614-- Infer the types of variables brought into scope by a binder
599-- Infer the types of variables brought into scope by a binder
616−inferBinder :: Type -> Binder -> Subst (M.Map Ident Type)
601+inferBinder :: Type -> Binder -> UnifyT Check (M.Map Ident Type)
617inferBinder _ NullBinder = return M.empty
602inferBinder _ NullBinder = return M.empty
618−inferBinder val (StringBinder _) = val ~~ String >> return M.empty
603+inferBinder val (StringBinder _) = val ?= String >> return M.empty
619−inferBinder val (NumberBinder _) = val ~~ Number >> return M.empty
604+inferBinder val (NumberBinder _) = val ?= Number >> return M.empty
620−inferBinder val (BooleanBinder _) = val ~~ Boolean >> return M.empty
605+inferBinder val (BooleanBinder _) = val ?= Boolean >> return M.empty
621inferBinder val (VarBinder name) = return $ M.singleton name val
606inferBinder val (VarBinder name) = return $ M.singleton name val
622inferBinder val (NullaryBinder ctor) = do
607inferBinder val (NullaryBinder ctor) = do
623 env <- getEnv
608 env <- getEnv
624− moduleName <- substCurrentModule <$> ask
609+ Just moduleName <- checkCurrentModule <$> get
625 case M.lookup (qualify moduleName ctor) (dataConstructors env) of
610 case M.lookup (qualify moduleName ctor) (dataConstructors env) of
626 Just (ty, _) -> do
611 Just (ty, _) -> do
627 ty `subsumes` val
612 ty `subsumes` val
@@ -629,7 +614,7 @@ inferBinder val (NullaryBinder ctor) = do
629 _ -> throwError $ "Constructor " ++ show ctor ++ " is not defined"
614 _ -> throwError $ "Constructor " ++ show ctor ++ " is not defined"
630inferBinder val (UnaryBinder ctor binder) = do
615inferBinder val (UnaryBinder ctor binder) = do
631 env <- getEnv
616 env <- getEnv
632− moduleName <- substCurrentModule <$> ask
617+ Just moduleName <- checkCurrentModule <$> get
633 case M.lookup (qualify moduleName ctor) (dataConstructors env) of
618 case M.lookup (qualify moduleName ctor) (dataConstructors env) of
634 Just (ty, _) -> do
619 Just (ty, _) -> do
635 fn <- replaceAllVarsWithUnknowns ty
620 fn <- replaceAllVarsWithUnknowns ty
@@ -643,11 +628,11 @@ inferBinder val (ObjectBinder props) = do
643 row <- fresh
628 row <- fresh
644 rest <- fresh
629 rest <- fresh
645 m1 <- inferRowProperties row rest props
630 m1 <- inferRowProperties row rest props
646− val ~~ Object row
631+ val ?= Object row
647 return m1
632 return m1
649− inferRowProperties :: Type -> Type -> [(String, Binder)] -> Subst (M.Map Ident Type)
634+ inferRowProperties :: Type -> Type -> [(String, Binder)] -> UnifyT Check (M.Map Ident Type)
650− inferRowProperties nrow row [] = nrow ~~ row >> return M.empty
635+ inferRowProperties nrow row [] = nrow ?= row >> return M.empty
651 inferRowProperties nrow row ((name, binder):binders) = do
636 inferRowProperties nrow row ((name, binder):binders) = do
652 propTy <- fresh
637 propTy <- fresh
653 m1 <- inferBinder propTy binder
638 m1 <- inferBinder propTy binder
@@ -656,13 +641,13 @@ inferBinder val (ObjectBinder props) = do
656inferBinder val (ArrayBinder binders) = do
641inferBinder val (ArrayBinder binders) = do
657 el <- fresh
642 el <- fresh
658 m1 <- M.unions <$> mapM (inferBinder el) binders
643 m1 <- M.unions <$> mapM (inferBinder el) binders
659− val ~~ TypeApp Array el
644+ val ?= TypeApp Array el
660 return m1
645 return m1
661inferBinder val (ConsBinder headBinder tailBinder) = do
646inferBinder val (ConsBinder headBinder tailBinder) = do
662 el <- fresh
647 el <- fresh
663 m1 <- inferBinder el headBinder
648 m1 <- inferBinder el headBinder
664 m2 <- inferBinder val tailBinder
649 m2 <- inferBinder val tailBinder
665− val ~~ TypeApp Array el
650+ val ?= TypeApp Array el
666 return $ m1 `M.union` m2
651 return $ m1 `M.union` m2
667inferBinder val (NamedBinder name binder) = do
652inferBinder val (NamedBinder name binder) = do
668 m <- inferBinder val binder
653 m <- inferBinder val binder
@@ -671,10 +656,10 @@ inferBinder val (NamedBinder name binder) = do
672-- Check the types of the return values in a set of binders in a case statement
657-- Check the types of the return values in a set of binders in a case statement
674−checkBinders :: [Type] -> Type -> [([Binder], Maybe Guard, Value)] -> Subst [([Binder], Maybe Guard, Value)]
659+checkBinders :: [Type] -> Type -> [([Binder], Maybe Guard, Value)] -> UnifyT Check [([Binder], Maybe Guard, Value)]
675checkBinders _ _ [] = return []
660checkBinders _ _ [] = return []
676checkBinders nvals ret ((binders, grd, val):bs) = do
661checkBinders nvals ret ((binders, grd, val):bs) = do
677− moduleName <- substCurrentModule <$> ask
662+ Just moduleName <- checkCurrentModule <$> get
678 m1 <- M.unions <$> zipWithM inferBinder nvals binders
663 m1 <- M.unions <$> zipWithM inferBinder nvals binders
679 r <- bindLocalVariables moduleName (M.toList m1) $ do
664 r <- bindLocalVariables moduleName (M.toList m1) $ do
680 val' <- check val ret
665 val' <- check val ret
@@ -689,19 +674,19 @@ checkBinders nvals ret ((binders, grd, val):bs) = do
690-- Check that a local variable name is not already used
675-- Check that a local variable name is not already used
692−assignVariable :: Ident -> Subst ()
677+assignVariable :: Ident -> UnifyT Check ()
693assignVariable name = do
678assignVariable name = do
694 env <- checkEnv <$> get
679 env <- checkEnv <$> get
695− moduleName <- substCurrentModule <$> ask
680+ Just moduleName <- checkCurrentModule <$> get
696 case M.lookup (moduleName, name) (names env) of
681 case M.lookup (moduleName, name) (names env) of
697− Just _ -> throwError $ "Variable with name " ++ show name ++ " already exists."
682+ Just _ -> UnifyT . lift . throwError $ "Variable with name " ++ show name ++ " already exists."
698 _ -> return ()
683 _ -> return ()
701-- Check the type of the return values of a statement, returning whether or not the statement returns on
686-- Check the type of the return values of a statement, returning whether or not the statement returns on
702-- all code paths
687-- all code paths
704−checkStatement :: M.Map Ident Type -> Type -> Statement -> Subst (Bool, M.Map Ident Type, Statement)
689+checkStatement :: M.Map Ident Type -> Type -> Statement -> UnifyT Check (Bool, M.Map Ident Type, Statement)
705checkStatement mass _ (VariableIntroduction name val) = do
690checkStatement mass _ (VariableIntroduction name val) = do
706 assignVariable name
691 assignVariable name
707 val'@(TypedValue _ _ t) <- infer val
692 val'@(TypedValue _ _ t) <- infer val
@@ -710,7 +695,7 @@ checkStatement mass _ (Assignment ident val) = do
710 val'@(TypedValue _ _ t) <- infer val
695 val'@(TypedValue _ _ t) <- infer val
711 case M.lookup ident mass of
696 case M.lookup ident mass of
712 Nothing -> throwError $ "No local variable with name " ++ show ident
697 Nothing -> throwError $ "No local variable with name " ++ show ident
713− Just ty -> do t ~~ ty
698+ Just ty -> do t ?= ty
714 return (False, mass, Assignment ident val')
699 return (False, mass, Assignment ident val')
715checkStatement mass ret (While val inner) = do
700checkStatement mass ret (While val inner) = do
716 val' <- check val Boolean
701 val' <- check val Boolean
@@ -720,7 +705,7 @@ checkStatement mass ret (If ifst) = do
720 (allCodePathsReturn, ifst') <- checkIfStatement mass ret ifst
705 (allCodePathsReturn, ifst') <- checkIfStatement mass ret ifst
721 return (allCodePathsReturn, mass, If ifst')
706 return (allCodePathsReturn, mass, If ifst')
722checkStatement mass ret (For ident start end inner) = do
707checkStatement mass ret (For ident start end inner) = do
723− moduleName <- substCurrentModule <$> ask
708+ Just moduleName <- checkCurrentModule <$> get
724 assignVariable ident
709 assignVariable ident
725 start' <- check start Number
710 start' <- check start Number
726 end' <- check end Number
711 end' <- check end Number
@@ -733,7 +718,7 @@ checkStatement mass ret (Return val) = do
734-- Check the type of an if-then-else statement
719-- Check the type of an if-then-else statement
736−checkIfStatement :: M.Map Ident Type -> Type -> IfStatement -> Subst (Bool, IfStatement)
721+checkIfStatement :: M.Map Ident Type -> Type -> IfStatement -> UnifyT Check (Bool, IfStatement)
737checkIfStatement mass ret (IfStatement val thens Nothing) = do
722checkIfStatement mass ret (IfStatement val thens Nothing) = do
738 val' <- check val Boolean
723 val' <- check val Boolean
739 (_, _, thens') <- checkBlock mass ret thens
724 (_, _, thens') <- checkBlock mass ret thens
@@ -747,7 +732,7 @@ checkIfStatement mass ret (IfStatement val thens (Just elses)) = do
748-- Check the type of an else statement
733-- Check the type of an else statement
750−checkElseStatement :: M.Map Ident Type -> Type -> ElseStatement -> Subst (Bool, ElseStatement)
735+checkElseStatement :: M.Map Ident Type -> Type -> ElseStatement -> UnifyT Check (Bool, ElseStatement)
751checkElseStatement mass ret (Else elses) = do
736checkElseStatement mass ret (Else elses) = do
752 (allCodePathsReturn, _, elses') <- checkBlock mass ret elses
737 (allCodePathsReturn, _, elses') <- checkBlock mass ret elses
753 return (allCodePathsReturn, Else elses')
738 return (allCodePathsReturn, Else elses')
@@ -756,10 +741,10 @@ checkElseStatement mass ret (ElseIf ifst) = (id *** ElseIf) <$> checkIfStatement
757-- Check the type of the return value of a block of statements
742-- Check the type of the return value of a block of statements
759−checkBlock :: M.Map Ident Type -> Type -> [Statement] -> Subst (Bool, M.Map Ident Type, [Statement])
744+checkBlock :: M.Map Ident Type -> Type -> [Statement] -> UnifyT Check (Bool, M.Map Ident Type, [Statement])
760checkBlock mass _ [] = return (False, mass, [])
745checkBlock mass _ [] = return (False, mass, [])
761checkBlock mass ret (s:ss) = do
746checkBlock mass ret (s:ss) = do
762− moduleName <- substCurrentModule <$> ask
747+ Just moduleName <- checkCurrentModule <$> get
763 (b1, mass1, s') <- checkStatement mass ret s
748 (b1, mass1, s') <- checkStatement mass ret s
764 bindLocalVariables moduleName (M.toList mass1) $ case (b1, ss) of
749 bindLocalVariables moduleName (M.toList mass1) $ case (b1, ss) of
765 (True, []) -> return (True, mass1, [s'])
750 (True, []) -> return (True, mass1, [s'])
@@ -771,15 +756,15 @@ checkBlock mass ret (s:ss) = do
772-- Skolemize a type variable by replacing its instances with fresh skolem constants
757-- Skolemize a type variable by replacing its instances with fresh skolem constants
774−skolemize :: String -> Type -> Subst Type
759+skolemize :: String -> Type -> UnifyT Check Type
775skolemize ident ty = do
760skolemize ident ty = do
776− tsk <- Skolem <$> fresh'
761+ tsk <- Skolem . runUnknown <$> fresh'
777 return $ replaceTypeVars ident tsk ty
762 return $ replaceTypeVars ident tsk ty
780-- Check the type of a value, rethrowing errors to provide a better error message
765-- Check the type of a value, rethrowing errors to provide a better error message
782−check :: Value -> Type -> Subst Value
767+check :: Value -> Type -> UnifyT Check Value
783check val ty = rethrow errorMessage $ check' val ty
768check val ty = rethrow errorMessage $ check' val ty
785 errorMessage msg =
770 errorMessage msg =
@@ -793,13 +778,13 @@ check val ty = rethrow errorMessage $ check' val ty
794-- Check the type of a value
779-- Check the type of a value
796−check' :: Value -> Type -> Subst Value
781+check' :: Value -> Type -> UnifyT Check Value
797check' val (ForAll idents ty) = do
782check' val (ForAll idents ty) = do
798 sk <- skolemize idents ty
783 sk <- skolemize idents ty
799 check val sk
784 check val sk
800check' val (ConstrainedType constraints ty) = do
785check' val (ConstrainedType constraints ty) = do
801 env <- getEnv
786 env <- getEnv
802− moduleName <- substCurrentModule <$> ask
787+ Just moduleName <- checkCurrentModule <$> get
803 dictNames <- flip mapM constraints $ \(Qualified _ (ProperName className), _) -> do
788 dictNames <- flip mapM constraints $ \(Qualified _ (ProperName className), _) -> do
804 n <- liftCheck freshDictionaryName
789 n <- liftCheck freshDictionaryName
805 return $ Ident $ "__dict_" ++ className ++ "_" ++ show n
790 return $ Ident $ "__dict_" ++ className ++ "_" ++ show n
@@ -812,7 +797,7 @@ check' val u@(TUnknown _) = do
812 val'@(TypedValue _ _ ty) <- infer val
797 val'@(TypedValue _ _ ty) <- infer val
813 -- Don't unify an unknown with an inferred polytype
798 -- Don't unify an unknown with an inferred polytype
814 ty' <- replaceAllVarsWithUnknowns ty
799 ty' <- replaceAllVarsWithUnknowns ty
815− ty' ~~ u
800+ ty' ?= u
816 return val'
801 return val'
817check' v@(NumericLiteral _) Number = return v
802check' v@(NumericLiteral _) Number = return v
818check' v@(StringLiteral _) String = return v
803check' v@(StringLiteral _) String = return v
@@ -825,7 +810,7 @@ check' (Indexer index vals) ty = do
825 vals' <- check vals (TypeApp Array ty)
810 vals' <- check vals (TypeApp Array ty)
826 return $ Indexer index' vals'
811 return $ Indexer index' vals'
827check' (Abs args ret) (Function argTys retTy) = do
812check' (Abs args ret) (Function argTys retTy) = do
828− moduleName <- substCurrentModule <$> ask
813+ Just moduleName <- checkCurrentModule <$> get
829 guardWith "Incorrect number of function arguments" (length args == length argTys)
814 guardWith "Incorrect number of function arguments" (length args == length argTys)
830 ret' <- bindLocalVariables moduleName (zip args argTys) $ check ret retTy
815 ret' <- bindLocalVariables moduleName (zip args argTys) $ check ret retTy
831 return $ Abs args ret'
816 return $ Abs args ret'
@@ -834,13 +819,13 @@ check' (App f args) ret = do
834 app <- checkFunctionApplication f' ft args ret
819 app <- checkFunctionApplication f' ft args ret
835 return $ app
820 return $ app
836check' (Var var) ty = do
821check' (Var var) ty = do
837− moduleName <- substCurrentModule <$> ask
822+ Just moduleName <- checkCurrentModule <$> get
838 ty1 <- lookupVariable moduleName var
823 ty1 <- lookupVariable moduleName var
839 repl <- replaceAllTypeSynonyms ty1
824 repl <- replaceAllTypeSynonyms ty1
840 repl `subsumes` ty
825 repl `subsumes` ty
841 return $ Var var
826 return $ Var var
842check' (TypedValue checkType val ty1) ty2 = do
827check' (TypedValue checkType val ty1) ty2 = do
843− moduleName <- substCurrentModule <$> ask
828+ Just moduleName <- checkCurrentModule <$> get
844 kind <- liftCheck $ kindOf moduleName ty1
829 kind <- liftCheck $ kindOf moduleName ty1
845 guardWith ("Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
830 guardWith ("Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
846 ty1 `subsumes` ty2
831 ty1 `subsumes` ty2
@@ -879,7 +864,7 @@ check' (Block ss) ret = do
879 return $ Block ss'
864 return $ Block ss'
880check' (Constructor c) ty = do
865check' (Constructor c) ty = do
881 env <- getEnv
866 env <- getEnv
882− moduleName <- substCurrentModule <$> ask
867+ Just moduleName <- checkCurrentModule <$> get
883 case M.lookup (qualify moduleName c) (dataConstructors env) of
868 case M.lookup (qualify moduleName c) (dataConstructors env) of
884 Nothing -> throwError $ "Constructor " ++ show c ++ " is undefined"
869 Nothing -> throwError $ "Constructor " ++ show c ++ " is undefined"
885 Just (ty1, _) -> do
870 Just (ty1, _) -> do
@@ -896,10 +881,10 @@ check' val ty = throwError $ prettyPrintValue val ++ " does not have type " ++ p
897-- The @lax@ parameter controls whether or not every record member has to be provided. For object updates, this is not the case.
882-- The @lax@ parameter controls whether or not every record member has to be provided. For object updates, this is not the case.
899−checkProperties :: [(String, Value)] -> Type -> Bool -> Subst [(String, Value)]
884+checkProperties :: [(String, Value)] -> Type -> Bool -> UnifyT Check [(String, Value)]
900checkProperties ps row lax = let (ts, r') = rowToList row in go ps ts r' where
885checkProperties ps row lax = let (ts, r') = rowToList row in go ps ts r' where
901 go [] [] REmpty = return []
886 go [] [] REmpty = return []
902− go [] [] u@(TUnknown _) = do u ~~ REmpty
887+ go [] [] u@(TUnknown _) = do u ?= REmpty
903 return []
888 return []
904 go [] [] (Skolem _) | lax = return []
889 go [] [] (Skolem _) | lax = return []
905 go [] ((p, _): _) _ | lax = return []
890 go [] ((p, _): _) _ | lax = return []
@@ -908,7 +893,7 @@ checkProperties ps row lax = let (ts, r') = rowToList row in go ps ts r' where
908 go ((p,v):ps') [] u@(TUnknown _) = do
893 go ((p,v):ps') [] u@(TUnknown _) = do
909 v'@(TypedValue _ _ ty) <- infer v
894 v'@(TypedValue _ _ ty) <- infer v
910 rest <- fresh
895 rest <- fresh
911− u ~~ RCons p ty rest
896+ u ?= RCons p ty rest
912 ps'' <- go ps' [] rest
897 ps'' <- go ps' [] rest
913 return $ (p, v') : ps''
898 return $ (p, v') : ps''
914 go ((p,v):ps') ts r =
899 go ((p,v):ps') ts r =
@@ -916,7 +901,7 @@ checkProperties ps row lax = let (ts, r') = rowToList row in go ps ts r' where
916 Nothing -> do
901 Nothing -> do
917 v'@(TypedValue _ _ ty) <- infer v
902 v'@(TypedValue _ _ ty) <- infer v
918 rest <- fresh
903 rest <- fresh
919− r ~~ RCons p ty rest
904+ r ?= RCons p ty rest
920 ps'' <- go ps' ts rest
905 ps'' <- go ps' ts rest
921 return $ (p, v') : ps''
906 return $ (p, v') : ps''
922 Just ty -> do
907 Just ty -> do
@@ -928,7 +913,7 @@ checkProperties ps row lax = let (ts, r') = rowToList row in go ps ts r' where
929-- Check the type of a function application, rethrowing errors to provide a better error message
914-- Check the type of a function application, rethrowing errors to provide a better error message
931−checkFunctionApplication :: Value -> Type -> [Value] -> Type -> Subst Value
916+checkFunctionApplication :: Value -> Type -> [Value] -> Type -> UnifyT Check Value
932checkFunctionApplication fn fnTy args ret = rethrow errorMessage $ checkFunctionApplication' fn fnTy args ret
917checkFunctionApplication fn fnTy args ret = rethrow errorMessage $ checkFunctionApplication' fn fnTy args ret
934 errorMessage msg = "Error applying function of type "
919 errorMessage msg = "Error applying function of type "
@@ -939,7 +924,7 @@ checkFunctionApplication fn fnTy args ret = rethrow errorMessage $ checkFunction
940-- Check the type of a function application
925-- Check the type of a function application
942−checkFunctionApplication' :: Value -> Type -> [Value] -> Type -> Subst Value
927+checkFunctionApplication' :: Value -> Type -> [Value] -> Type -> UnifyT Check Value
943checkFunctionApplication' fn (Function argTys retTy) args ret = do
928checkFunctionApplication' fn (Function argTys retTy) args ret = do
944 guardWith "Incorrect number of function arguments" (length args == length argTys)
929 guardWith "Incorrect number of function arguments" (length args == length argTys)
945 args' <- zipWithM check args argTys
930 args' <- zipWithM check args argTys
@@ -951,7 +936,7 @@ checkFunctionApplication' fn (ForAll ident ty) args ret = do
951checkFunctionApplication' fn u@(TUnknown _) args ret = do
936checkFunctionApplication' fn u@(TUnknown _) args ret = do
952 args' <- mapM (\arg -> infer arg >>= \(TypedValue _ v t) -> TypedValue True v <$> replaceAllVarsWithUnknowns t) args
937 args' <- mapM (\arg -> infer arg >>= \(TypedValue _ v t) -> TypedValue True v <$> replaceAllVarsWithUnknowns t) args
953 let tys = map (\(TypedValue _ _ t) -> t) args'
938 let tys = map (\(TypedValue _ _ t) -> t) args'
954− u ~~ Function tys ret
939+ u ?= Function tys ret
955 return $ App fn args'
940 return $ App fn args'
956checkFunctionApplication' fn (SaturatedTypeSynonym name tyArgs) args ret = do
941checkFunctionApplication' fn (SaturatedTypeSynonym name tyArgs) args ret = do
957 ty <- expandTypeSynonym name tyArgs
942 ty <- expandTypeSynonym name tyArgs
@@ -959,7 +944,7 @@ checkFunctionApplication' fn (SaturatedTypeSynonym name tyArgs) args ret = do
959checkFunctionApplication' fn (ConstrainedType constraints fnTy) args ret = do
944checkFunctionApplication' fn (ConstrainedType constraints fnTy) args ret = do
960 env <- getEnv
945 env <- getEnv
961 dicts <- getTypeClassDictionaries
946 dicts <- getTypeClassDictionaries
962− moduleName <- substCurrentModule <$> ask
947+ Just moduleName <- checkCurrentModule <$> get
963 checkFunctionApplication' (App fn (map (flip TypeClassDictionary dicts) (qualifyAllUnqualifiedNames moduleName env constraints))) fnTy args ret
948 checkFunctionApplication' (App fn (map (flip TypeClassDictionary dicts) (qualifyAllUnqualifiedNames moduleName env constraints))) fnTy args ret
964checkFunctionApplication' _ fnTy args ret = throwError $ "Applying a function of type "
949checkFunctionApplication' _ fnTy args ret = throwError $ "Applying a function of type "
965 ++ prettyPrintType fnTy
950 ++ prettyPrintType fnTy
@@ -969,7 +954,7 @@ checkFunctionApplication' _ fnTy args ret = throwError $ "Applying a function of
970-- Check whether one type subsumes another, rethrowing errors to provide a better error message
955-- Check whether one type subsumes another, rethrowing errors to provide a better error message
972−subsumes :: Type -> Type -> Subst ()
957+subsumes :: Type -> Type -> UnifyT Check ()
973subsumes ty1 ty2 = rethrow errorMessage $ subsumes' ty1 ty2
958subsumes ty1 ty2 = rethrow errorMessage $ subsumes' ty1 ty2
975 errorMessage msg = "Error checking that type "
960 errorMessage msg = "Error checking that type "
@@ -981,12 +966,12 @@ subsumes ty1 ty2 = rethrow errorMessage $ subsumes' ty1 ty2
982-- Check whether one type subsumes another
967-- Check whether one type subsumes another
984−subsumes' :: Type -> Type -> Subst ()
969+subsumes' :: Type -> Type -> UnifyT Check ()
985subsumes' (ForAll ident ty1) ty2 = do
970subsumes' (ForAll ident ty1) ty2 = do
986 replaced <- replaceVarWithUnknown ident ty1
971 replaced <- replaceVarWithUnknown ident ty1
987 replaced `subsumes` ty2
972 replaced `subsumes` ty2
988subsumes' (Function args1 ret1) (Function args2 ret2) = do
973subsumes' (Function args1 ret1) (Function args2 ret2) = do
989 zipWithM_ subsumes args2 args1
974 zipWithM_ subsumes args2 args1
990 ret1 `subsumes` ret2
975 ret1 `subsumes` ret2
991−subsumes' ty1 ty2 = ty1 ~~ ty2
976+subsumes' ty1 ty2 = ty1 ?= ty2