@@ -1,730 +0,0 @@
1−-----------------------------------------------------------------------------
3−-- Module : Language.PureScript.TypeChecker.Types
4−-- Copyright : (c) Phil Freeman 2013
7−-- Maintainer : Phil Freeman <paf31@cantab.net>
8−-- Stability : experimental
13−-----------------------------------------------------------------------------
15−{-# LANGUAGE DeriveDataTypeable #-}
17−module Language.PureScript.TypeChecker.Types (
22−import Data.Maybe (isJust, fromMaybe)
24−import qualified Data.Data as D
25−import Data.Generics (everywhere, everywhereM, everything, mkT, mkM, mkQ, extM, extQ)
27−import Language.PureScript.Values
28−import Language.PureScript.Types
29−import Language.PureScript.Kinds
30−import Language.PureScript.Names
31−import Language.PureScript.TypeChecker.Monad
32−import Language.PureScript.TypeChecker.Kinds
33−import Language.PureScript.TypeChecker.Synonyms
34−import Language.PureScript.Pretty
35−import Language.PureScript.Unknown
37−import Control.Monad.State
38−import Control.Monad.Error
40−import Control.Applicative
41−import Control.Arrow (Arrow(..), Kleisli(..), (***), (&&&), second)
42−import qualified Control.Category as C
44−import qualified Data.Map as M
46−instance Unifiable Type where
48− isUnknown (TUnknown u) = Just u
49− isUnknown _ = Nothing
51− apply s (TUnknown u) = runSubstitution s u
52− apply s (SaturatedTypeSynonym name tys) = SaturatedTypeSynonym name $ map (apply s) tys
53− apply s (ForAll idents ty) = ForAll idents $ apply s ty
54− apply s (Array t) = Array (apply s t)
55− apply s (Object r) = Object (apply s r)
56− apply s (Function args ret) = Function (map (apply s) args) (apply s ret)
57− apply s (TypeApp t1 t2) = TypeApp (apply s t1) (apply s t2)
59− unknowns (TUnknown (Unknown u)) = [u]
60− unknowns (SaturatedTypeSynonym _ tys) = concatMap unknowns tys
61− unknowns (ForAll idents ty) = unknowns ty
62− unknowns (Array t) = unknowns t
63− unknowns (Object r) = unknowns r
64− unknowns (Function args ret) = concatMap unknowns args ++ unknowns ret
65− unknowns (TypeApp t1 t2) = unknowns t1 ++ unknowns t2
68−instance Unifiable Row where
70− isUnknown (RUnknown u) = Just u
71− isUnknown _ = Nothing
74− (s1, r1') = rowToList r1
75− (s2, r2') = rowToList r2
76− int = [ (t1, t2) | (name, t1) <- s1, (name', t2) <- s2, name == name' ]
77− sd1 = [ (name, t1) | (name, t1) <- s1, name `notElem` map fst s2 ]
78− sd2 = [ (name, t2) | (name, t2) <- s2, name `notElem` map fst s1 ]
80− forM_ int (uncurry (~~))
81− unifyRows sd1 r1' sd2 r2'
83− unifyRows :: [(String, Type)] -> Row -> [(String, Type)] -> Row -> Subst Check ()
84− unifyRows [] (RUnknown u) sd r = replace u (rowFromList (sd, r))
85− unifyRows sd r [] (RUnknown u) = replace u (rowFromList (sd, r))
86− unifyRows ns@((name, ty):row) r others u@(RUnknown un) = do
88− forM row $ \(_, ty) -> occursCheck un ty
90− u ~~ RCons name ty u'
91− unifyRows row r others u'
92− unifyRows [] REmpty [] REmpty = return ()
93− unifyRows [] (RowVar v1) [] (RowVar v2) | v1 == v2 = return ()
94− unifyRows [] (RSkolem s1) [] (RSkolem s2) | s1 == s2 = return ()
95− unifyRows sd1 r1 sd2 r2 = throwError $ "Cannot unify " ++ prettyPrintRow (rowFromList (sd1, r1)) ++ " with " ++ prettyPrintRow (rowFromList (sd2, r2)) ++ "."
96− apply s (RUnknown u) = runSubstitution s u
97− apply s (RCons name ty r) = RCons name (apply s ty) (apply s r)
99− unknowns (RUnknown (Unknown u)) = [u]
100− unknowns (RCons _ ty r) = unknowns ty ++ unknowns r
103−unifyTypes :: Type -> Type -> Subst Check ()
104−unifyTypes t1 t2 = rethrow (\e -> "Error unifying type " ++ prettyPrintType t1 ++ " with type " ++ prettyPrintType t2 ++ ":\n" ++ e) $ do
107− unifyTypes' (TUnknown u1) (TUnknown u2) | u1 == u2 = return ()
108− unifyTypes' (TUnknown u) t = replace u t
109− unifyTypes' t (TUnknown u) = replace u t
110− unifyTypes' (SaturatedTypeSynonym name1 args1) (SaturatedTypeSynonym name2 args2)
111− | name1 == name2 = zipWithM_ unifyTypes args1 args2
112− unifyTypes' (SaturatedTypeSynonym name args) ty = do
114− modulePath <- checkModulePath `fmap` lift get
115− case M.lookup (qualify modulePath name) (typeSynonyms env) of
116− Just (synArgs, body) -> do
117− let m = M.fromList $ zip synArgs args
118− let replaced = replaceTypeVars m body
119− replaced `unifyTypes` ty
120− Nothing -> error "Type synonym was not defined"
121− unifyTypes' ty s@(SaturatedTypeSynonym _ _) = s `unifyTypes` ty
122− unifyTypes' (ForAll ident1 ty1) (ForAll ident2 ty2) = do
123− sk <- skolemize ident1 ty1
124− replaced <- replaceVarsWithUnknowns [ident2] ty2
125− sk `unifyTypes` replaced
126− unifyTypes' (ForAll ident ty1) ty2 = do
127− sk <- skolemize ident ty1
129− unifyTypes' ty f@(ForAll _ _) = f `unifyTypes` ty
130− unifyTypes' Number Number = return ()
131− unifyTypes' String String = return ()
132− unifyTypes' Boolean Boolean = return ()
133− unifyTypes' (Array s) (Array t) = s `unifyTypes` t
134− unifyTypes' (Object row1) (Object row2) = row1 ~~ row2
135− unifyTypes' (Function args1 ret1) (Function args2 ret2) = do
136− guardWith "Function applied to incorrect number of args" $ length args1 == length args2
137− zipWithM_ unifyTypes args1 args2
138− ret1 `unifyTypes` ret2
139− unifyTypes' (TypeVar v1) (TypeVar v2) | v1 == v2 = return ()
140− unifyTypes' (TypeConstructor c1) (TypeConstructor c2) = do
141− modulePath <- checkModulePath `fmap` lift get
142− guardWith ("Cannot unify " ++ show c1 ++ " with " ++ show c2 ++ ".") (qualify modulePath c1 == qualify modulePath c2)
143− unifyTypes' (TypeApp t1 t2) (TypeApp t3 t4) = do
146− unifyTypes' (Skolem s1) (Skolem s2) | s1 == s2 = return ()
147− unifyTypes' t1 t2 = throwError $ "Cannot unify " ++ prettyPrintType t1 ++ " with " ++ prettyPrintType t2 ++ "."
149−isFunction :: Value -> Bool
150−isFunction (Abs _ _) = True
151−isFunction (TypedValue untyped _) = isFunction untyped
154−typeOf :: Maybe Ident -> Value -> Check Type
156− (ty, sub, checks) <- runSubst $ case name of
157− Just ident | isFunction val ->
159− ty <- infer (M.singleton ident me) val
162− _ -> infer M.empty val
163− escapeCheck checks ty sub
164− return $ varIfUnknown $ desaturateAllTypeSynonyms $ setifyAll ty
166−escapeCheck :: [AnyUnifiable] -> Type -> Substitution -> Check ()
167−escapeCheck checks ty sub =
169− visibleUnknowns = nub $ unknowns ty
171− forM_ checks $ \check -> case check of
172− AnyUnifiable t -> do
173− let unsolvedUnknowns = nub . unknowns $ apply sub t
174− guardWith "Escape check fails" $ null $ unsolvedUnknowns \\ visibleUnknowns
177−setify = rowFromList . first (M.toList . M.fromList) . rowToList
179−setifyAll :: (D.Data d) => d -> d
180−setifyAll = everywhere (mkT setify)
182−findTypeVars :: (D.Data d) => d -> [String]
183−findTypeVars = everything (++) (mkQ [] f)
185− f :: Type -> [String]
189−varIfUnknown :: Type -> Type
192− (ty', m) = flip runState M.empty $ everywhereM (flip extM g $ mkM f) ty
194− mkForAll (sort $ nub $ M.elems m ++ findTypeVars ty) ty'
196− f :: Type -> State (M.Map Int String) Type
197− f (TUnknown (Unknown n)) = do
199− case M.lookup n m of
201− let name = 't' : show (M.size m)
202− put $ M.insert n name m
203− return $ TypeVar name
204− Just name -> return $ TypeVar name
206− g :: Row -> State (M.Map Int String) Row
207− g (RUnknown (Unknown n)) = do
209− case M.lookup n m of
211− let name = 'r' : show (M.size m)
212− put $ M.insert n name m
213− return $ RowVar name
214− Just name -> return $ RowVar name
217−replaceTypeVars :: M.Map String Type -> Type -> Type
218−replaceTypeVars m = everywhere (mkT replace)
220− replace (TypeVar v) = case M.lookup v m of
225−replaceRowVars :: M.Map String Row -> Type -> Type
226−replaceRowVars m = everywhere (mkT replace)
228− replace (RowVar v) = case M.lookup v m of
233−replaceVarsWithUnknowns :: [String] -> Type -> Subst Check Type
234−replaceVarsWithUnknowns idents = flip evalStateT M.empty . everywhereM (flip extM f $ mkM g)
236− f :: Type -> StateT (M.Map String Int) (Subst Check) Type
237− f (TypeVar var) | var `elem` idents = do
240− case M.lookup var m of
242− put (M.insert var n m)
243− return $ TUnknown (Unknown n)
244− Just u -> return $ TUnknown (Unknown u)
246− g :: Row -> StateT (M.Map String Int) (Subst Check) Row
247− g (RowVar var) | var `elem` idents = do
250− case M.lookup var m of
252− put (M.insert var n m)
253− return $ RUnknown (Unknown n)
254− Just u -> return $ RUnknown (Unknown u)
257−replaceAllTypeSynonyms :: (D.Data d) => d -> Check d
258−replaceAllTypeSynonyms d = do
260− let syns = map (\((path, name), (args, _)) -> (Qualified path name, length args)) . M.toList $ typeSynonyms env
261− either throwError return $ saturateAllTypeSynonyms syns d
263−desaturateAllTypeSynonyms :: (D.Data d) => d -> d
264−desaturateAllTypeSynonyms = everywhere (mkT replace)
266− replace (SaturatedTypeSynonym name args) = foldl TypeApp (TypeConstructor name) args
269−ensureNoDuplicateProperties :: [(String, Value)] -> Check ()
270−ensureNoDuplicateProperties ps = guardWith "Duplicate property names" $ length (nub . map fst $ ps) == length ps
272−infer :: M.Map Ident Type -> Value -> Subst Check Type
273−infer m val = rethrow (\e -> "Error inferring type of term " ++ prettyPrintValue val ++ ":\n" ++ e) $ do
278−infer' _ (NumericLiteral _) = return Number
279−infer' _ (StringLiteral _) = return String
280−infer' _ (BooleanLiteral _) = return Boolean
281−infer' m (ArrayLiteral vals) = do
282− ts <- mapM (infer m) vals
284− forM_ ts $ \t -> arr ~~ Array t
286−infer' m (Unary op val) = do
289−infer' m (Binary op left right) = do
292− inferBinary op t1 t2
293−infer' m (ObjectLiteral ps) = do
294− lift $ ensureNoDuplicateProperties ps
295− ts <- mapM (infer m . snd) ps
296− let fields = zipWith (\(name, _) t -> (name, t)) ps ts
297− return $ Object $ typesToRow fields
298−infer' m (ObjectUpdate o ps) = do
299− lift $ ensureNoDuplicateProperties ps
302− ts <- mapM (infer m . snd) ps
303− let tys = zipWith (\(name, _) t -> (name, t)) ps ts
304− obj ~~ Object (rowFromList (tys, row))
306−infer' m (Indexer index val) = do
308− check m index Number
309− check m val (Array el)
311−infer' m (Accessor prop val) = do
314− check m val (Object (RCons prop field rest))
316−infer' m (Abs args ret) = do
317− ts <- replicateM (length args) fresh
318− let m' = m `M.union` M.fromList (zip args ts)
319− body <- infer m' ret
320− return $ Function ts body
321−infer' m (App f xs) = do
324− checkFunctionApplication m t1 xs ret
326−infer' m (Var var@(Qualified mp name)) = do
329− case M.lookup name m of
330− Just ty -> lift $ replaceAllTypeSynonyms ty
331− Nothing -> lookupGlobal
336− modulePath <- checkModulePath `fmap` lift get
337− case M.lookup (qualify modulePath var) (names env) of
338− Nothing -> throwError $ show var ++ " is undefined"
339− Just (ty, _) -> lift $ replaceAllTypeSynonyms ty
340−infer' m (Block ss) = do
342− (allCodePathsReturn, _) <- checkBlock m M.empty ret ss
343− guardWith "Block is missing a return statement" allCodePathsReturn
345−infer' m (Constructor c) = do
347− modulePath <- checkModulePath `fmap` lift get
348− case M.lookup (qualify modulePath c) (dataConstructors env) of
349− Nothing -> throwError $ "Constructor " ++ show c ++ " is undefined"
350− Just ty -> lift $ replaceAllTypeSynonyms ty
351−infer' m (Case val binders) = do
354− checkBinders m t1 ret binders
356−infer' m (IfThenElse cond th el) = do
357− check m cond Boolean
362−infer' m (TypedValue val ty) = do
363− kind <- lift $ kindOf ty
364− guardWith ("Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
365− ty' <- lift $ replaceAllTypeSynonyms ty
369−inferUnary :: UnaryOperator -> Type -> Subst Check Type
371− case fromMaybe (error "Invalid operator") $ lookup op unaryOps of
372− (valTy, resTy) -> do
376−checkUnary :: M.Map Ident Type -> UnaryOperator -> Value -> Type -> Subst Check ()
377−checkUnary m op val res =
378− case fromMaybe (error "Invalid operator") $ lookup op unaryOps of
379− (valTy, resTy) -> do
383−unaryOps :: [(UnaryOperator, (Type, Type))]
384−unaryOps = [ (Negate, (Number, Number))
385− , (Not, (Boolean, Boolean))
386− , (BitwiseNot, (Number, Number))
389−inferBinary :: BinaryOperator -> Type -> Type -> Subst Check Type
390−inferBinary op left right | isEqualityTest op = do
393−inferBinary op left right =
394− case fromMaybe (error "Invalid operator") $ lookup op binaryOps of
395− (valTy, resTy) -> do
400−checkBinary :: M.Map Ident Type -> BinaryOperator -> Value -> Value -> Type -> Subst Check ()
401−checkBinary m op left right res | isEqualityTest op = do
406−checkBinary m op left right res =
407− case fromMaybe (error "Invalid operator") $ lookup op binaryOps of
408− (valTy, resTy) -> do
413−isEqualityTest :: BinaryOperator -> Bool
414−isEqualityTest EqualTo = True
415−isEqualityTest NotEqualTo = True
416−isEqualityTest _ = False
418−binaryOps :: [(BinaryOperator, (Type, Type))]
419−binaryOps = [ (Add, (Number, Number))
420− , (Subtract, (Number, Number))
421− , (Multiply, (Number, Number))
422− , (Divide, (Number, Number))
423− , (Modulus, (Number, Number))
424− , (BitwiseAnd, (Number, Number))
425− , (BitwiseOr, (Number, Number))
426− , (BitwiseXor, (Number, Number))
427− , (ShiftRight, (Number, Number))
428− , (ZeroFillShiftRight, (Number, Number))
429− , (And, (Boolean, Boolean))
430− , (Or, (Boolean, Boolean))
431− , (Concat, (String, String))
432− , (Modulus, (Number, Number))
433− , (LessThan, (Number, Boolean))
434− , (LessThanOrEqualTo, (Number, Boolean))
435− , (GreaterThan, (Number, Boolean))
436− , (GreaterThanOrEqualTo, (Number, Boolean))
439−inferBinder :: Type -> Binder -> Subst Check (M.Map Ident Type)
440−inferBinder _ NullBinder = return M.empty
441−inferBinder val (StringBinder _) = val ~~ String >> return M.empty
442−inferBinder val (NumberBinder _) = val ~~ Number >> return M.empty
443−inferBinder val (BooleanBinder _) = val ~~ Boolean >> return M.empty
444−inferBinder val (VarBinder name) = return $ M.singleton name val
445−inferBinder val (NullaryBinder ctor) = do
447− modulePath <- checkModulePath `fmap` lift get
448− case M.lookup (qualify modulePath ctor) (dataConstructors env) of
452− _ -> throwError $ "Constructor " ++ show ctor ++ " is not defined"
453−inferBinder val (UnaryBinder ctor binder) = do
455− modulePath <- checkModulePath `fmap` lift get
456− case M.lookup (qualify modulePath ctor) (dataConstructors env) of
459− ty `subsumes` (Function [obj] val)
460− inferBinder obj binder
461− _ -> throwError $ "Constructor " ++ show ctor ++ " is not defined"
462−inferBinder val (ObjectBinder props) = do
465− m1 <- inferRowProperties row rest props
469− inferRowProperties :: Row -> Row -> [(String, Binder)] -> Subst Check (M.Map Ident Type)
470− inferRowProperties nrow row [] = nrow ~~ row >> return M.empty
471− inferRowProperties nrow row ((name, binder):binders) = do
473− m1 <- inferBinder propTy binder
474− m2 <- inferRowProperties nrow (RCons name propTy row) binders
475− return $ m1 `M.union` m2
476−inferBinder val (ArrayBinder binders rest) = do
478− m1 <- M.unions <$> mapM (inferBinder el) binders
481− Nothing -> return m1
483− m2 <- inferBinder val binder
484− return $ m1 `M.union` m2
485−inferBinder val (NamedBinder name binder) = do
486− m <- inferBinder val binder
487− return $ M.insert name val m
489−inferGuardedBinder :: M.Map Ident Type -> Type -> Binder -> Subst Check (M.Map Ident Type)
490−inferGuardedBinder m val (GuardedBinder cond binder) = do
491− m1 <- inferBinder val binder
492− ty <- infer (m `M.union` m1) cond
495−inferGuardedBinder m val b = inferBinder val b
497−checkBinders :: M.Map Ident Type -> Type -> Type -> [(Binder, Value)] -> Subst Check ()
498−checkBinders _ _ _ [] = return ()
499−checkBinders m nval ret ((binder, val):bs) = do
500− m1 <- inferGuardedBinder m nval binder
501− check (m `M.union` m1) val ret
502− checkBinders m nval ret bs
504−assignVariable :: Ident -> M.Map Ident Type -> Subst Check ()
505−assignVariable name m =
506− case M.lookup name m of
507− Nothing -> return ()
508− Just _ -> throwError $ "Variable with name " ++ show name ++ " already exists."
510−checkStatement :: M.Map Ident Type -> M.Map Ident Type -> Type -> Statement -> Subst Check (Bool, M.Map Ident Type)
511−checkStatement m mass ret (VariableIntroduction name val) = do
512− assignVariable name (m `M.union` mass)
514− return (False, M.insert name t mass)
515−checkStatement m mass ret (Assignment ident val) = do
517− case M.lookup ident mass of
518− Nothing -> throwError $ "No local variable with name " ++ show ident
519− Just ty -> do t ~~ ty
520− return (False, mass)
521−checkStatement m mass ret (While val inner) = do
523− (allCodePathsReturn, _) <- checkBlock m mass ret inner
524− return (allCodePathsReturn, mass)
525−checkStatement m mass ret (If ifst) = do
526− allCodePathsReturn <- checkIfStatement m mass ret ifst
527− return (allCodePathsReturn, mass)
528−checkStatement m mass ret (For ident start end inner) = do
529− assignVariable ident (m `M.union` mass)
530− check (m `M.union` mass) start Number
531− check (m `M.union` mass) end Number
532− let mass1 = M.insert ident Number mass
533− (allCodePathsReturn, _) <- checkBlock (m `M.union` mass1) mass1 ret inner
534− return (allCodePathsReturn, mass)
535−checkStatement m mass ret (ForEach ident vals inner) = do
536− assignVariable ident (m `M.union` mass)
538− check (m `M.union` mass) vals (Array val)
539− let mass1 = M.insert ident val mass
540− (allCodePathsReturn, _) <- checkBlock (m `M.union` mass1) mass1 ret inner
541− guardWith "Cannot return from within a foreach block" $ not allCodePathsReturn
542− return (False, mass)
543−checkStatement m mass ret (Return val) = do
544− check (m `M.union` mass) val ret
547−checkIfStatement :: M.Map Ident Type -> M.Map Ident Type -> Type -> IfStatement -> Subst Check Bool
548−checkIfStatement m mass ret (IfStatement val thens Nothing) = do
550− _ <- checkBlock m mass ret thens
552−checkIfStatement m mass ret (IfStatement val thens (Just elses)) = do
554− (allCodePathsReturn1, _) <- checkBlock m mass ret thens
555− allCodePathsReturn2 <- checkElseStatement m mass ret elses
556− return $ allCodePathsReturn1 && allCodePathsReturn2
558−checkElseStatement :: M.Map Ident Type -> M.Map Ident Type -> Type -> ElseStatement -> Subst Check Bool
559−checkElseStatement m mass ret (Else elses) = fst <$> checkBlock m mass ret elses
560−checkElseStatement m mass ret (ElseIf ifst) = checkIfStatement m mass ret ifst
562−checkBlock :: M.Map Ident Type -> M.Map Ident Type -> Type -> [Statement] -> Subst Check (Bool, M.Map Ident Type)
563−checkBlock _ mass _ [] = return (False, mass)
564−checkBlock m mass ret (s:ss) = do
565− (b1, mass1) <- checkStatement (m `M.union` mass) mass ret s
567− (True, []) -> return (True, mass1)
568− (True, _) -> throwError "Unreachable code"
570− (b2, mass2) <- checkBlock m mass1 ret ss
573−skolemize :: String -> Type -> Subst Check Type
574−skolemize ident ty = do
575− tsk <- Skolem <$> fresh'
576− rsk <- RSkolem <$> fresh'
577− return $ replaceRowVars (M.singleton ident rsk) $ replaceTypeVars (M.singleton ident tsk) ty
579−check :: M.Map Ident Type -> Value -> Type -> Subst Check ()
580−check m val ty = rethrow errorMessage $ check' m val ty
583− "Error checking type of term " ++
584− prettyPrintValue val ++
586− prettyPrintType ty ++
590−check' :: M.Map Ident Type -> Value -> Type -> Subst Check ()
591−check' m val (ForAll idents ty) = do
592− sk <- skolemize idents ty
594−check' m val (SaturatedTypeSynonym name args) = do
596− modulePath <- checkModulePath `fmap` lift get
597− case M.lookup (qualify modulePath name) (typeSynonyms env) of
598− Just (synArgs, body) -> do
599− let replaced = replaceTypeVars (M.fromList (zip synArgs args)) body
600− check m val replaced
601− Nothing -> error "Type synonym was not defined"
602−check' m val u@(TUnknown _) = do
605−check' m (NumericLiteral _) Number = return ()
606−check' m (StringLiteral _) String = return ()
607−check' m (BooleanLiteral _) Boolean = return ()
608−check' m (Unary op val) ty = checkUnary m op val ty
609−check' m (Binary op left right) ty = checkBinary m op left right ty
610−check' m (ArrayLiteral vals) (Array ty) = forM_ vals (\val -> check m val ty)
611−check' m (Indexer index vals) ty = check m index Number >> check m vals (Array ty)
612−check' m (Abs args ret) (Function argTys retTy) = do
613− guardWith "Incorrect number of function arguments" (length args == length argTys)
614− let bindings = M.fromList (zip args argTys)
615− check (bindings `M.union` m) ret retTy
616−check' m (App f xs) ret = do
618− checkFunctionApplication m ft xs ret
619−check' m v@(Var var@(Qualified mp name)) ty = do
622− case M.lookup name m of
624− repl <- lift $ replaceAllTypeSynonyms ty1
626− Nothing -> lookupGlobal
631− modulePath <- checkModulePath `fmap` lift get
632− case M.lookup (qualify modulePath var) (names env) of
633− Nothing -> throwError $ show var ++ " is undefined"
635− repl <- lift $ replaceAllTypeSynonyms ty1
637−check' m (TypedValue val ty1) ty2 = do
638− kind <- lift $ kindOf ty1
639− guardWith ("Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
642−check' m (Case val binders) ret = do
644− checkBinders m t1 ret binders
645−check' m (IfThenElse cond th el) ty = do
646− check m cond Boolean
649−check' m (ObjectLiteral ps) (Object row) = do
650− lift $ ensureNoDuplicateProperties ps
651− checkProperties m ps row False
652−check' m (ObjectUpdate obj ps) objTy@(Object row) = do
653− lift $ ensureNoDuplicateProperties ps
655− checkProperties m ps row True
656−check' m (Accessor prop val) ty = do
658− check m val (Object (RCons prop ty rest))
659−check' m (Block ss) ret = do
660− (allCodePathsReturn, _) <- checkBlock m M.empty ret ss
661− guardWith "Block is missing a return statement" allCodePathsReturn
662−check' m (Constructor c) ty = do
664− modulePath <- checkModulePath `fmap` lift get
665− case M.lookup (qualify modulePath c) (dataConstructors env) of
666− Nothing -> throwError $ "Constructor " ++ show c ++ " is undefined"
668− repl <- lift $ replaceAllTypeSynonyms ty1
670−check' _ val ty = throwError $ prettyPrintValue val ++ " does not have type " ++ prettyPrintType ty
672−checkProperties :: M.Map Ident Type -> [(String, Value)] -> Row -> Bool -> Subst Check ()
673−checkProperties m ps row lax = let (ts, r') = rowToList row in go ps ts r' where
674− go [] [] REmpty = return ()
675− go [] [] u@(RUnknown _) = u ~~ REmpty
676− go [] [] (RSkolem _) | lax = return ()
677− go [] ((p, _): _) _ | lax = return ()
678− | otherwise = throwError $ prettyPrintValue (ObjectLiteral ps) ++ " does not have property " ++ p
679− go ((p,_):_) [] REmpty = throwError $ "Property " ++ p ++ " is not present in closed object type " ++ prettyPrintRow row
680− go ((p,v):ps) [] u@(RUnknown _) = do
683− u ~~ RCons p ty rest
685− go ((p,v):ps) ts r =
690− r ~~ RCons p ty rest
694− go ps (delete (p, ty) ts) r
695− go _ _ _ = throwError $ prettyPrintValue (ObjectLiteral ps) ++ " does not have type " ++ prettyPrintType (Object row)
697−checkFunctionApplication :: M.Map Ident Type -> Type -> [Value] -> Type -> Subst Check ()
698−checkFunctionApplication m fnTy args ret = rethrow errorMessage $ checkFunctionApplication' m fnTy args ret
700− errorMessage msg = "Error applying function of type "
701− ++ prettyPrintType fnTy
702− ++ " to arguments " ++ intercalate ", " (map prettyPrintValue args)
703− ++ ", expecting value of type "
704− ++ prettyPrintType ret ++ ":\n" ++ msg
706−checkFunctionApplication' :: M.Map Ident Type -> Type -> [Value] -> Type -> Subst Check ()
707−checkFunctionApplication' m (Function argTys retTy) args ret = do
708− guardWith "Incorrect number of function arguments" (length args == length argTys)
709− zipWithM (check m) args argTys
710− retTy `subsumes` ret
711−checkFunctionApplication' m (ForAll ident ty) args ret = do
712− replaced <- replaceVarsWithUnknowns [ident] ty
713− checkFunctionApplication m replaced args ret
714−checkFunctionApplication' m u@(TUnknown _) args ret = do
715− tyArgs <- mapM (infer m) args
716− u `subsumes` Function tyArgs ret
717−checkFunctionApplication' _ fnTy args ret = throwError $ "Cannot apply function of type "
718− ++ prettyPrintType fnTy
719− ++ " to arguments " ++ intercalate ", " (map prettyPrintValue args)
720− ++ ". Expecting value of type " ++ prettyPrintType ret ++ "."
722−subsumes :: Type -> Type -> Subst Check ()
723−subsumes (ForAll ident ty1) ty2 = do
724− replaced <- replaceVarsWithUnknowns [ident] ty1
725− replaced `subsumes` ty2
726−subsumes (Function args1 ret1) (Function args2 ret2) = do
727− zipWithM subsumes args2 args1
728− ret1 `subsumes` ret2
729−subsumes ty1 ty2 = ty1 ~~ ty2