@@ -14,7 +14,6 @@
1414-----------------------------------------------------------------------------
1515
1616{-# OPTIONS_GHC -fno-warn-orphans #-}
17−{-# LANGUAGE DeriveDataTypeable #-}
1817{-# LANGUAGE FlexibleContexts #-}
1918{-# LANGUAGE MultiParamTypeClasses #-}
2019
@@ -60,7 +59,6 @@ import Language.PureScript.Pretty
6059
6160import Control.Monad.State
6261import Control.Monad.Error
63−import Control.Monad.Reader
6462import Control.Monad.Unify
6563
6664import Control.Applicative
@@ -82,7 +80,7 @@ instance Unifiable Check Type where
8280-- Unify two types, updating the current substitution
8381--
8482unifyTypes :: Type -> Type -> UnifyT Type Check ()
85−unifyTypes t1 t2 = rethrow (\e -> "Error unifying type " ++ prettyPrintType t1 ++ " with type " ++ prettyPrintType t2 ++ ":\n" ++ e) $ do
83+unifyTypes t1 t2 = rethrow (\e -> "Error unifying type " ++ prettyPrintType t1 ++ " with type " ++ prettyPrintType t2 ++ ":\n" ++ e) $
8684 unifyTypes' t1 t2
8785 where
8886 unifyTypes' (TUnknown u1) (TUnknown u2) | u1 == u2 = return ()
@@ -104,8 +102,8 @@ unifyTypes t1 t2 = rethrow (\e -> "Error unifying type " ++ prettyPrintType t1 +
104102 sko <- newSkolemConstant
105103 let sk = skolemize ident sko sc ty1
106104 sk `unifyTypes` ty2
107− unifyTypes' (ForAll _ _ _) _ = throwError "Skolem variable scope is unspecified"
108− unifyTypes' ty f@(ForAll _ _ _) = f `unifyTypes` ty
105+ unifyTypes' ForAll{} _ = throwError "Skolem variable scope is unspecified"
106+ unifyTypes' ty f@ForAll{} = f `unifyTypes` ty
109107 unifyTypes' (Object row1) (Object row2) = row1 =?= row2
110108 unifyTypes' (TypeVar v1) (TypeVar v2) | v1 == v2 = return ()
111109 unifyTypes' (TypeConstructor c1) (TypeConstructor c2) = do
@@ -116,8 +114,8 @@ unifyTypes t1 t2 = rethrow (\e -> "Error unifying type " ++ prettyPrintType t1 +
116114 t3 `unifyTypes` t5
117115 t4 `unifyTypes` t6
118116 unifyTypes' (Skolem s1 _) (Skolem s2 _) | s1 == s2 = return ()
119− unifyTypes' r1@(RCons _ _ _) r2 = unifyRows r1 r2
120− unifyTypes' r1 r2@(RCons _ _ _) = unifyRows r1 r2
117+ unifyTypes' r1@RCons{} r2 = unifyRows r1 r2
118+ unifyTypes' r1 r2@RCons{} = unifyRows r1 r2
121119 unifyTypes' r1@REmpty r2 = unifyRows r1 r2
122120 unifyTypes' r1 r2@REmpty = unifyRows r1 r2
123121 unifyTypes' t3 t4 = throwError $ "Cannot unify " ++ prettyPrintType t3 ++ " with " ++ prettyPrintType t4 ++ "."
@@ -187,30 +185,30 @@ typesOf mainModuleName moduleName vals = do
187185 -- If the declaration is a function, it has access to other values in the binding group.
188186 -- If not, the generated code might fail at runtime since those values might be undefined.
189187 let dict' = if isFunction val then dict else M.empty
190− triple@(_, (val, ty)) <- case e of
188+ triple@(_, (val', ty)) <- case e of
191189 -- Typed declarations
192− (ident, (val, Just (ty, checkType))) -> do
190+ (ident, (val', Just (ty, checkType))) -> do
193191 -- Kind check
194192 kind <- liftCheck $ kindOf moduleName ty
195193 guardWith ("Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
196194 -- Check the type with the new names in scope
197195 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms $ ty
198− val' <- bindNames dict' $ if checkType
199− then TypedValue True <$> check val ty' <*> pure ty'
200− else return (TypedValue False val ty')
201− return (ident, (val', ty'))
196+ val'' <- bindNames dict' $ if checkType
197+ then TypedValue True <$> check val' ty' <*> pure ty'
198+ else return (TypedValue False val' ty')
199+ return (ident, (val'', ty'))
202200 -- Untyped declarations
203− (ident, (val, Nothing)) -> do
201+ (ident, (val', Nothing)) -> do
204202 -- Infer the type with the new names in scope
205− TypedValue _ val' ty <- bindNames dict' $ infer val
203+ TypedValue _ val'' ty <- bindNames dict' $ infer val'
206204 ty =?= fromMaybe (error "name not found in dictionary") (lookup ident untypedDict)
207− return (ident, (TypedValue True val' ty, ty))
205+ return (ident, (TypedValue True val'' ty, ty))
208206 -- If --main is enabled, need to check that `main` has type Eff eff a for some eff, a
209207 when (Just moduleName == mainModuleName && fst e == Ident "main") $ do
210208 [eff, a] <- replicateM 2 fresh
211209 ty =?= TypeApp (TypeApp (TypeConstructor (Qualified (Just (ModuleName [ProperName "Control", ProperName "Monad", ProperName "Eff"])) (ProperName "Eff"))) eff) a
212210 -- Make sure unification variables do not escape
213− escapeCheck val ty
211+ escapeCheck val' ty
214212 return triple
215213 forM tys $ \(ident, (val, ty)) -> do
216214 -- Replace type class dictionary placeholders with actual dictionaries
@@ -221,7 +219,7 @@ typesOf mainModuleName moduleName vals = do
221219 -- top-level unification variables with named type variables.
222220 let val'' = overTypes (desaturateAllTypeSynonyms . setifyAll) val'
223221 ty' = varIfUnknown . desaturateAllTypeSynonyms . setifyAll $ ty
224− return $ (ident, (val'', ty'))
222+ return (ident, (val'', ty'))
225223 where
226224 -- Apply the substitution that was returned from runUnify to both types and (type-annotated) values
227225 tidyUp (ts, sub) = map (\(i, (val, ty)) -> (i, (overTypes (sub $?) val, sub $? ty))) ts
@@ -284,7 +282,7 @@ entails moduleName context goal@(className, tys) = do
284282 solveSubgoals :: Environment -> [(String, Type)] -> Maybe [(Qualified ProperName, [Type])] -> [Maybe [Value]]
285283 solveSubgoals _ _ Nothing = return Nothing
286284 solveSubgoals env subst (Just subgoals) = do
287− dict <- mapM (go env) (map (id *** map (replaceAllTypeVars subst)) subgoals)
285+ dict <- mapM (go env . second (map (replaceAllTypeVars subst))) subgoals
288286 return $ Just dict
289287 -- Make a dictionary from subgoal dictionaries by applying the correct function
290288 mkDictionary :: Qualified Ident -> Maybe [Value] -> Value
@@ -320,7 +318,7 @@ typeHeadsAreEqual m e (TypeConstructor c1) (TypeConstructor c2) | typeConstructo
320318typeHeadsAreEqual m e (TypeApp h1 (TypeVar v)) (TypeApp h2 arg) = (:) (v, arg) <$> typeHeadsAreEqual m e h1 h2
321319typeHeadsAreEqual m e t1@(TypeApp _ _) t2@(TypeApp _ (TypeVar _)) = typeHeadsAreEqual m e t2 t1
322320typeHeadsAreEqual m e (SaturatedTypeSynonym name args) t2 = case expandTypeSynonym' e m name args of
323− Left err -> Nothing
321+ Left _ -> Nothing
324322 Right t1 -> typeHeadsAreEqual m e t1 t2
325323typeHeadsAreEqual _ _ _ _ = Nothing
326324
@@ -350,7 +348,7 @@ findAllTypes = everything (++) (mkQ [] go)
350348--
351349skolemEscapeCheck :: Value -> Check ()
352350skolemEscapeCheck (TypedValue False _ _) = return ()
353−skolemEscapeCheck root@(TypedValue _ _ _) =
351+skolemEscapeCheck root@TypedValue{} =
354352 -- Every skolem variable is created when a ForAll type is skolemized.
355353 -- This determines the scope of that skolem variable, which is copied from the SkolemScope
356354 -- field of the ForAll constructor.
@@ -374,10 +372,10 @@ skolemEscapeCheck root@(TypedValue _ _ _) =
374372 collect _ = []
375373 go _ scos = ([], scos)
376374 findBindingScope :: SkolemScope -> Maybe Value
377− findBindingScope sco = something (mkQ Nothing go) root
375+ findBindingScope sco = something (mkQ Nothing go') root
378376 where
379− go val@(TypedValue _ _ (ForAll _ _ (Just sco'))) | sco == sco' = Just val
380− go _ = Nothing
377+ go' val@(TypedValue _ _ (ForAll _ _ (Just sco'))) | sco == sco' = Just val
378+ go' _ = Nothing
381379skolemEscapeCheck val = throwError $ "Untyped value passed to skolemEscapeCheck: " ++ prettyPrintValue val
382380
383381-- |
@@ -399,7 +397,7 @@ varIfUnknown :: Type -> Type
399397varIfUnknown ty =
400398 let unks = nub $ unknowns ty
401399 toName = (:) 't' . show
402− ty' = everywhere (mkT typeToVar) $ ty
400+ ty' = everywhere (mkT typeToVar) ty
403401 typeToVar :: Type -> Type
404402 typeToVar (TUnknown (Unknown u)) = TypeVar (toName u)
405403 typeToVar t = t
@@ -430,7 +428,7 @@ instantiatePolyTypeWithUnknowns val ty = return (val, ty)
430428replaceVarWithUnknown :: String -> Type -> UnifyT Type Check Type
431429replaceVarWithUnknown ident ty = do
432430 tu <- fresh
433− return $ replaceTypeVars ident tu $ ty
431+ return $ replaceTypeVars ident tu ty
434432
435433-- |
436434-- Replace fully applied type synonyms with the @SaturatedTypeSynonym@ data constructor, which helps generate
@@ -603,11 +601,11 @@ inferBinder val (ConstructorBinder ctor binders) = do
603601 (_, fn) <- instantiatePolyTypeWithUnknowns (error "Data constructor types cannot contains constraints") ty
604602 go binders fn
605603 where
606− go [] ty = do
607− subsumes Nothing val ty
604+ go [] ty' = do
605+ subsumes Nothing val ty'
608606 return M.empty
609− go (binder : binders) (TypeApp (TypeApp t obj) ret) | t == tyFunction =
610− M.union <$> inferBinder obj binder <*> go binders ret
607+ go (binder : binders') (TypeApp (TypeApp t obj) ret) | t == tyFunction =
608+ M.union <$> inferBinder obj binder <*> go binders' ret
611609 go _ _ = throwError $ "Wrong number of arguments to constructor " ++ show ctor
612610 _ -> throwError $ "Constructor " ++ show ctor ++ " is not defined"
613611inferBinder val (ObjectBinder props) = do
@@ -713,7 +711,7 @@ check val ty = rethrow errorMessage $ check' val ty
713711-- Check the type of a value
714712--
715713check' :: Value -> Type -> UnifyT Type Check Value
716−check' val t@(ForAll ident ty _) = do
714+check' val (ForAll ident ty _) = do
717715 scope <- newSkolemScope
718716 sko <- newSkolemConstant
719717 let sk = skolemize ident sko scope ty
@@ -722,15 +720,15 @@ check' val t@(ForAll ident ty _) = do
722720check' val t@(ConstrainedType constraints ty) = do
723721 env <- getEnv
724722 Just moduleName <- checkCurrentModule <$> get
725− dictNames <- flip mapM constraints $ \(Qualified _ (ProperName className), _) -> do
723+ dictNames <- forM constraints $ \(Qualified _ (ProperName className), _) -> do
726724 n <- liftCheck freshDictionaryName
727725 return $ Ident $ "__dict_" ++ className ++ "_" ++ show n
728726 val' <- withTypeClassDictionaries (zipWith (\name (className, instanceTy) ->
729727 TypeClassDictionaryInScope name className instanceTy Nothing TCDRegular) (map (Qualified Nothing) dictNames)
730728 (qualifyAllUnqualifiedNames moduleName env constraints)) $
731729 check val ty
732− return $ TypedValue True (foldr Abs val' (map Left dictNames)) t
733−check' val t@(SaturatedTypeSynonym name args) = do
730+ return $ TypedValue True (foldr (Abs . Left) val' dictNames) t
731+check' val (SaturatedTypeSynonym name args) = do
734732 ty <- introduceSkolemScope <=< expandTypeSynonym name $ args
735733 val' <- check val ty
736734 return $ TypedValue True val' ty
@@ -747,8 +745,8 @@ check' v@(StringLiteral _) t | t == tyString =
747745check' v@(BooleanLiteral _) t | t == tyBoolean =
748746 return $ TypedValue True v t
749747check' (ArrayLiteral vals) t@(TypeApp a ty) | a == tyArray = do
750− arr <- ArrayLiteral <$> forM vals (\val -> check val ty)
751− return $ TypedValue True arr t
748+ array <- ArrayLiteral <$> forM vals (`check` ty)
749+ return $ TypedValue True array t
752750check' (Abs (Left arg) ret) ty@(TypeApp (TypeApp t argTy) retTy) | t == tyFunction = do
753751 Just moduleName <- checkCurrentModule <$> get
754752 ret' <- bindLocalVariables moduleName [(arg, argTy)] $ check ret retTy
@@ -944,17 +942,17 @@ subsumes' val (Object r1) (Object r2) = do
944942 go ts1' ts2' r1' r2'
945943 return val
946944 where
947− go [] ts2 r1 r2 = r1 =?= rowFromList (ts2, r2)
948− go ts1 [] r1 r2 = r2 =?= rowFromList (ts1, r1)
949− go ((p1, ty1) : ts1) ((p2, ty2) : ts2) r1 r2
945+ go [] ts2 r1' r2' = r1' =?= rowFromList (ts2, r2')
946+ go ts1 [] r1' r2' = r2' =?= rowFromList (ts1, r1')
947+ go ((p1, ty1) : ts1) ((p2, ty2) : ts2) r1' r2'
950948 | p1 == p2 = do subsumes Nothing ty1 ty2
951− go ts1 ts2 r1 r2
949+ go ts1 ts2 r1' r2'
952950 | p1 < p2 = do rest <- fresh
953− r2 =?= RCons p1 ty1 rest
954− go ts1 ((p2, ty2) : ts2) r1 rest
951+ r2' =?= RCons p1 ty1 rest
952+ go ts1 ((p2, ty2) : ts2) r1' rest
955953 | p1 > p2 = do rest <- fresh
956− r1 =?= RCons p2 ty2 rest
957− go ((p1, ty1) : ts1) ts2 rest r2
954+ r1' =?= RCons p2 ty2 rest
955+ go ((p1, ty1) : ts1) ts2 rest r2'
958956subsumes' val ty1 ty2@(Object _) = subsumes val ty2 ty1
959957subsumes' val ty1 ty2 = do
960958 ty1 =?= ty2