git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Reinstate -Wall, fix warnings.
commita178e07bcdaafe4025986a745eff201cecec6339 authorPhil Freeman <paf31@cantab.net> dateWed 5 Mar 2014, 23:30 (13 years ago) parent973babe
16 files changed+127−176
purescript.cabal+1 src/Data/Generics/Extras.hs−1 src/Language/PureScript.hs+1−1 src/Language/PureScript/CodeGen/Externs.hs+1−1 src/Language/PureScript/CodeGen/JS.hs+2−3 src/Language/PureScript/CodeGen/Optimize.hs+37−42 src/Language/PureScript/DeadCodeElimination.hs+5−6 src/Language/PureScript/Sugar/DoNotation.hs−2 src/Language/PureScript/Sugar/Names.hs+50−52 src/Language/PureScript/Sugar/TypeClasses.hs+1−5 src/Language/PureScript/TypeChecker.hs+2−6 src/Language/PureScript/TypeChecker/Kinds.hs−1 src/Language/PureScript/TypeChecker/Monad.hs−2 src/Language/PureScript/TypeChecker/Synonyms.hs+7−8 src/Language/PureScript/TypeChecker/Types.hs+19−46 src/Language/PureScript/Types.hs+1
purescript.cabal+1
@@ -74,6 +74,7 @@ library
7474 buildable: True
7575 hs-source-dirs: src
7676 other-modules:
77+ ghc-options: -Wall -O2
7778
7879executable psc
7980 build-depends: base >=4 && <5, cmdtheline -any, containers -any,
src/Data/Generics/Extras.hs−1
@@ -18,7 +18,6 @@
1818module Data.Generics.Extras where
1919
2020import Data.Data
21−import Data.Maybe (fromMaybe)
2221
2322-- |
2423-- Apply a top-down monadic transformation everywhere
src/Language/PureScript.hs+1−1
@@ -64,7 +64,7 @@ compile opts ms = do
6464 Module moduleName' <$> typeCheckAll mainModuleIdent moduleName' decls <*> pure exps
6565 regrouped <- createBindingGroupsModule . collapseBindingGroupsModule $ elaborated
6666 let entryPoints = moduleNameFromString `map` optionsModules opts
67− let elim = if null entryPoints then regrouped else eliminateDeadCode env entryPoints regrouped
67+ let elim = if null entryPoints then regrouped else eliminateDeadCode entryPoints regrouped
6868 let js = mapMaybe (flip (moduleToJs opts) env) elim
6969 let exts = intercalate "\n" . map (`moduleToPs` env) $ elim
7070 js' <- case mainModuleIdent of
src/Language/PureScript/CodeGen/Externs.hs+1−1
@@ -30,7 +30,7 @@ import Data.List (intercalate)
3030-- TODO: only expose items listed in "exps"
3131--
3232moduleToPs :: Module -> Environment -> String
33−moduleToPs (Module mn decls exps) env =
33+moduleToPs (Module mn decls _) env =
3434 "module " ++ runModuleName mn ++ " where\n" ++
3535 (intercalate "\n" . map (" " ++) . concatMap (declToPs mn env) $ decls)
3636
src/Language/PureScript/CodeGen/JS.hs+2−3
@@ -24,8 +24,6 @@ module Language.PureScript.CodeGen.JS (
2424
2525import Data.Maybe (fromMaybe, mapMaybe)
2626import Data.Function (on)
27−import Data.Data (Data)
28−import Data.Generics (mkQ, everything)
2927
3028import Control.Arrow (second)
3129import Control.Monad (replicateM, forM)
@@ -50,7 +48,7 @@ import Language.PureScript.Prim
5048-- module.
5149--
5250moduleToJs :: Options -> Module -> Environment -> Maybe JS
53−moduleToJs opts (Module name decls exps) env =
51+moduleToJs opts (Module name decls _) env =
5452 case jsDecls of
5553 [] -> Nothing
5654 _ -> Just $ JSAssignment (JSAccessor (moduleNameToJs name) (JSVar "_ps")) $
@@ -73,6 +71,7 @@ declToJs _ mp (DataDeclaration _ _ ctors) _ =
7371 Just $ flip concatMap ctors $ \(pn@(ProperName ctor), tys) ->
7472 export (Escaped ctor) $ JSVariableIntroduction ctor (Just (go pn 0 tys []))
7573 where
74+ go :: ProperName -> Integer -> [Type] -> [JS] -> JS
7675 go pn _ [] values =
7776 JSObjectLiteral [ ("ctor", JSStringLiteral (show (Qualified (Just mp) pn))), ("values", JSArrayLiteral $ reverse values) ]
7877 go pn index (_ : tys') values =
src/Language/PureScript/CodeGen/Optimize.hs+37−42
@@ -46,8 +46,6 @@ import Language.PureScript.Names
4646import Language.PureScript.CodeGen.JS.AST
4747import Language.PureScript.Options
4848import Language.PureScript.CodeGen.Common (identToJs)
49−import Language.PureScript.Types
50−import Language.PureScript.Prim
5149
5250-- |
5351-- Apply a series of optimizer passes to simplified Javascript code
@@ -311,9 +309,6 @@ magicDo' = everywhere (mkT undo) . everywhere' (mkT convert)
311309 -- Check if an expression represents a function in the Ef module
312310 isEffFunc name (JSAccessor name' (JSAccessor "Control_Monad_Eff" (JSVar "_ps"))) | name == name' = True
313311 isEffFunc _ _ = False
314− -- Module names
315− prelude = ModuleName [ProperName "Prelude"]
316− effModule = ModuleName [ProperName "Control", ProperName "Monad", ProperName "Eff"]
317312 -- The name of the type class dictionary for the Monad Eff instance
318313 effDictName = "monadEff"
319314 -- Check if an expression represents the Monad Eff dictionary
@@ -402,62 +397,62 @@ inlineOperator op f = everywhere (mkT convert)
402397
403398inlineCommonOperators :: JS -> JS
404399inlineCommonOperators = applyAll
405− [ binary "numNumber" "+" "Num" tyNumber Add
406− , binary "numNumber" "-" "Num" tyNumber Subtract
407− , binary "numNumber" "*" "Num" tyNumber Multiply
408− , binary "numNumber" "/" "Num" tyNumber Divide
409− , binary "numNumber" "%" "Num" tyNumber Modulus
410− , unary "numNumber" "negate" "Num" tyNumber Negate
400+ [ binary "numNumber" "+" Add
401+ , binary "numNumber" "-" Subtract
402+ , binary "numNumber" "*" Multiply
403+ , binary "numNumber" "/" Divide
404+ , binary "numNumber" "%" Modulus
405+ , unary "numNumber" "negate" Negate
411406
412− , binary "ordNumber" "<" "Ord" tyNumber LessThan
413− , binary "ordNumber" ">" "Ord" tyNumber GreaterThan
414− , binary "ordNumber" "<=" "Ord" tyNumber LessThanOrEqualTo
415− , binary "ordNumber" ">=" "Ord" tyNumber GreaterThanOrEqualTo
407+ , binary "ordNumber" "<" LessThan
408+ , binary "ordNumber" ">" GreaterThan
409+ , binary "ordNumber" "<=" LessThanOrEqualTo
410+ , binary "ordNumber" ">=" GreaterThanOrEqualTo
416411
417− , binary "eqNumber" "==" "Eq" tyNumber EqualTo
418− , binary "eqNumber" "/=" "Eq" tyNumber NotEqualTo
419− , binary "eqString" "==" "Eq" tyString EqualTo
420− , binary "eqString" "/=" "Eq" tyString NotEqualTo
421− , binary "eqBoolean" "==" "Eq" tyBoolean EqualTo
422− , binary "eqBoolean" "/=" "Eq" tyBoolean NotEqualTo
412+ , binary "eqNumber" "==" EqualTo
413+ , binary "eqNumber" "/=" NotEqualTo
414+ , binary "eqString" "==" EqualTo
415+ , binary "eqString" "/=" NotEqualTo
416+ , binary "eqBoolean" "==" EqualTo
417+ , binary "eqBoolean" "/=" NotEqualTo
423418
424− , binaryFunction "bitsNumber" "shl" "Bits" tyNumber ShiftLeft
425− , binaryFunction "bitsNumber" "shr" "Bits" tyNumber ShiftRight
426− , binaryFunction "bitsNumber" "zshr" "Bits" tyNumber ZeroFillShiftRight
427− , binary "bitsNumber" "&" "Bits" tyNumber BitwiseAnd
428− , binary "bitsNumber" "|" "Bits" tyNumber BitwiseOr
429− , binary "bitsNumber" "^" "Bits" tyNumber BitwiseXor
430− , unary "bitsNumber" "complement" "Bits" tyNumber BitwiseNot
419+ , binaryFunction "bitsNumber" "shl" ShiftLeft
420+ , binaryFunction "bitsNumber" "shr" ShiftRight
421+ , binaryFunction "bitsNumber" "zshr" ZeroFillShiftRight
422+ , binary "bitsNumber" "&" BitwiseAnd
423+ , binary "bitsNumber" "|" BitwiseOr
424+ , binary "bitsNumber" "^" BitwiseXor
425+ , unary "bitsNumber" "complement" BitwiseNot
431426
432− , binary "boolLikeBoolean" "&&" "BoolLike" tyBoolean And
433− , binary "boolLikeBoolean" "||" "BoolLike" tyBoolean Or
434− , unary "boolLikeBoolean" "not" "BoolLike" tyBoolean Not
427+ , binary "boolLikeBoolean" "&&" And
428+ , binary "boolLikeBoolean" "||" Or
429+ , unary "boolLikeBoolean" "not" Not
435430 ]
436431 where
437− binary :: String -> String -> String -> Type -> BinaryOperator -> JS -> JS
438− binary dictName opString className classTy op = everywhere (mkT convert)
432+ binary :: String -> String -> BinaryOperator -> JS -> JS
433+ binary dictName opString op = everywhere (mkT convert)
439434 where
440435 convert :: JS -> JS
441− convert (JSApp (JSApp (JSApp fn [dict]) [x]) [y]) | isOp fn && isOpDict dictName className classTy dict = JSBinary op x y
436+ convert (JSApp (JSApp (JSApp fn [dict]) [x]) [y]) | isOp fn && isOpDict dictName dict = JSBinary op x y
442437 convert other = other
443438 isOp (JSAccessor longForm (JSAccessor "Prelude" (JSVar _))) | longForm == identToJs (Op opString) = True
444439 isOp (JSIndexer (JSStringLiteral op') (JSAccessor "Prelude" (JSVar "_ps"))) | opString == op' = True
445440 isOp _ = False
446− binaryFunction :: String -> String -> String -> Type -> BinaryOperator -> JS -> JS
447− binaryFunction dictName fnName className classTy op = everywhere (mkT convert)
441+ binaryFunction :: String -> String -> BinaryOperator -> JS -> JS
442+ binaryFunction dictName fnName op = everywhere (mkT convert)
448443 where
449444 convert :: JS -> JS
450− convert (JSApp (JSApp (JSApp fn [dict]) [x]) [y]) | isOp fn && isOpDict dictName className classTy dict = JSBinary op x y
445+ convert (JSApp (JSApp (JSApp fn [dict]) [x]) [y]) | isOp fn && isOpDict dictName dict = JSBinary op x y
451446 convert other = other
452447 isOp (JSAccessor fnName' (JSAccessor "Prelude" (JSVar "_ps"))) | fnName == fnName' = True
453448 isOp _ = False
454− unary :: String -> String -> String -> Type -> UnaryOperator -> JS -> JS
455− unary dictName fnName className classTy op = everywhere (mkT convert)
449+ unary :: String -> String -> UnaryOperator -> JS -> JS
450+ unary dictName fnName op = everywhere (mkT convert)
456451 where
457452 convert :: JS -> JS
458− convert (JSApp (JSApp fn [dict]) [x]) | isOp fn && isOpDict dictName className classTy dict = JSUnary op x
453+ convert (JSApp (JSApp fn [dict]) [x]) | isOp fn && isOpDict dictName dict = JSUnary op x
459454 convert other = other
460455 isOp (JSAccessor fnName' (JSAccessor "Prelude" (JSVar "_ps"))) | fnName' == fnName = True
461456 isOp _ = False
462− isOpDict dictName className ty (JSApp (JSAccessor prop (JSAccessor "Prelude" (JSVar "_ps"))) [JSObjectLiteral []]) | prop == dictName = True
463− isOpDict _ _ _ _ = False
457+ isOpDict dictName (JSApp (JSAccessor prop (JSAccessor "Prelude" (JSVar "_ps"))) [JSObjectLiteral []]) | prop == dictName = True
458+ isOpDict _ _ = False
src/Language/PureScript/DeadCodeElimination.hs+5−6
@@ -25,22 +25,21 @@ import Data.Maybe (mapMaybe)
2525import Language.PureScript.Names
2626import Language.PureScript.Values
2727import Language.PureScript.Declarations
28−import Language.PureScript.TypeChecker.Monad
2928
3029-- |
3130-- Eliminate all declarations which are not a transitive dependency of the entry point module
3231--
33−eliminateDeadCode :: Environment -> [ModuleName] -> [Module] -> [Module]
34−eliminateDeadCode env entryPoints ms =
35− let declarations = concatMap (declarationsByModule env) ms
32+eliminateDeadCode :: [ModuleName] -> [Module] -> [Module]
33+eliminateDeadCode entryPoints ms =
34+ let declarations = concatMap declarationsByModule ms
3635 (graph, _, vertexFor) = graphFromEdges $ map (\(key, deps) -> (key, key, deps)) declarations
3736 entryPointVertices = mapMaybe (vertexFor . fst) . filter (\((mn, _), _) -> mn `elem` entryPoints) $ declarations
3837 in flip map ms $ \(Module moduleName ds exps) -> Module moduleName (filter (isUsed moduleName graph vertexFor entryPointVertices) ds) exps
3938
4039type Key = (ModuleName, Either Ident ProperName)
4140
42−declarationsByModule :: Environment -> Module -> [(Key, [Key])]
43−declarationsByModule env (Module moduleName ds _) = concatMap go ds
41+declarationsByModule :: Module -> [(Key, [Key])]
42+declarationsByModule (Module moduleName ds _) = concatMap go ds
4443 where
4544 go :: Declaration -> [(Key, [Key])]
4645 go d@(ValueDeclaration name _ _ _) = [((moduleName, Left name), dependencies moduleName d)]
src/Language/PureScript/Sugar/DoNotation.hs−2
@@ -34,8 +34,6 @@ desugarDo = everywhereM (mkM replace)
3434 where
3535 prelude :: ModuleName
3636 prelude = ModuleName [ProperName "Prelude"]
37− ret :: Value
38− ret = Var (Qualified (Just prelude) (Ident "ret"))
3937 bind :: Value
4038 bind = Var (Qualified (Just prelude) (Op ">>="))
4139 replace :: Value -> Either String Value
src/Language/PureScript/Sugar/Names.hs+50−52
@@ -17,13 +17,10 @@ module Language.PureScript.Sugar.Names (
1717) where
1818
1919import Data.Maybe (fromMaybe)
20−import Data.Data
2120import Data.Generics (extM, mkM, everywhereM)
2221import Data.Generics.Extras (mkS, extS, everywhereWithContextM')
23−import Data.List (find)
2422
2523import Control.Applicative (Applicative(..), (<$>), (<*>))
26−import Control.Monad (foldM)
2724import Control.Monad.Error
2825
2926import qualified Data.Map as M
@@ -163,9 +160,9 @@ renameInModule imports exports (Module mn decls exps) =
163160 updateVars (ValueDeclaration name [] Nothing val) =
164161 ValueDeclaration name [] Nothing <$> everywhereWithContextM' [] (mkS bindFunctionArgs `extS` bindBinders) val
165162 where
166− bindFunctionArgs bound (Abs (Left arg) val) = return (arg : bound, Abs (Left arg) val)
167− bindFunctionArgs bound (Var name@(Qualified Nothing ident)) | ident `notElem` bound = (,) bound <$> (Var <$> updateValueName name)
168− bindFunctionArgs bound (Var name@(Qualified (Just _) _)) = (,) bound <$> (Var <$> updateValueName name)
163+ bindFunctionArgs bound (Abs (Left arg) val') = return (arg : bound, Abs (Left arg) val')
164+ bindFunctionArgs bound (Var name'@(Qualified Nothing ident)) | ident `notElem` bound = (,) bound <$> (Var <$> updateValueName name')
165+ bindFunctionArgs bound (Var name'@(Qualified (Just _) _)) = (,) bound <$> (Var <$> updateValueName name')
169166 bindFunctionArgs bound other = return (bound, other)
170167 bindBinders :: [Ident] -> CaseAlternative -> Either String ([Ident], CaseAlternative)
171168 bindBinders bound c@(CaseAlternative bs _ _) = return (binderNames bs ++ bound, c)
@@ -182,25 +179,25 @@ renameInModule imports exports (Module mn decls exps) =
182179 updateConstraints = mapM (\(name, ts) -> (,) <$> updateClassName name <*> pure ts)
183180
184181 updateTypeName (Qualified Nothing name) = update "type" importedTypes name
185− updateTypeName (Qualified (Just mn) name) = do
186− modExports <- getExports mn
182+ updateTypeName (Qualified (Just mn') name) = do
183+ modExports <- getExports mn'
187184 case name `lookup` exportedTypes modExports of
188− Nothing -> throwError $ "Unknown type '" ++ show (Qualified (Just mn) name) ++ "'"
189− _ -> return $ Qualified (Just mn) name
185+ Nothing -> throwError $ "Unknown type '" ++ show (Qualified (Just mn') name) ++ "'"
186+ _ -> return $ Qualified (Just mn') name
190187
191188 updateDataConstructorName (Qualified Nothing name) = update "data constructor" importedDataConstructors name
192− updateDataConstructorName (Qualified (Just mn) name) = do
193− modExports <- getExports mn
189+ updateDataConstructorName (Qualified (Just mn') name) = do
190+ modExports <- getExports mn'
194191 let allDcons = join $ snd `map` exportedTypes modExports
195192 if name `elem` allDcons
196− then return $ Qualified (Just mn) name
197− else throwError $ "Unknown data constructor '" ++ show (Qualified (Just mn) name) ++ "'"
198−
193+ then return $ Qualified (Just mn') name
194+ else throwError $ "Unknown data constructor '" ++ show (Qualified (Just mn') name) ++ "'"
195+
199196 updateClassName (Qualified Nothing name) = update "type class" importedTypeClasses name
200− updateClassName (Qualified (Just mn) name) = check "type class" exportedTypeClasses mn name
201−
197+ updateClassName (Qualified (Just mn') name) = check "type class" exportedTypeClasses mn' name
198+
202199 updateValueName (Qualified Nothing name) = update "value" importedValues name
203− updateValueName (Qualified (Just mn) name) = check "value" exportedValues mn name
200+ updateValueName (Qualified (Just mn') name) = check "value" exportedValues mn' name
204201
205202 -- Replace an unqualified name with a qualified
206203 update :: (Ord a, Show a) => String -> (ImportEnvironment -> M.Map a (Qualified a)) -> a -> Either String (Qualified a)
@@ -208,15 +205,15 @@ renameInModule imports exports (Module mn decls exps) =
208205
209206 -- Check that a qualified name is valid
210207 check :: (Show a, Eq a) => String -> (Exports -> [a]) -> ModuleName -> a -> Either String (Qualified a)
211− check t get mn name = do
212− modExports <- getExports mn
208+ check t get mn' name = do
209+ modExports <- getExports mn'
213210 if name `elem` get modExports
214− then return $ Qualified (Just mn) name
215− else throwError $ "Unknown " ++ t ++ " '" ++ show (Qualified (Just mn) name) ++ "'"
211+ then return $ Qualified (Just mn') name
212+ else throwError $ "Unknown " ++ t ++ " '" ++ show (Qualified (Just mn') name) ++ "'"
216213
217214 -- Gets the exports for a module, or an error message if the module doesn't exist
218215 getExports :: ModuleName -> Either String Exports
219− getExports mn = maybe (throwError $ "Unknown module '" ++ show mn ++ "'") return $ M.lookup mn exports
216+ getExports mn' = maybe (throwError $ "Unknown module '" ++ show mn' ++ "'") return $ M.lookup mn' exports
220217
221218-- |
222219-- Finds all exported declarations in a set of modules.
@@ -256,13 +253,13 @@ findExports = foldM addModule $ M.singleton (ModuleName [ProperName "Prim"]) pri
256253filterExports :: ModuleName -> [DeclarationRef] -> ExportEnvironment -> Either String ExportEnvironment
257254filterExports mn exps env = do
258255 let moduleExports = fromMaybe (error "Module is missing") (mn `M.lookup` env)
259− moduleExports' <- filterModule exps moduleExports
256+ moduleExports' <- filterModule moduleExports
260257 return $ M.insert mn moduleExports' env
261258 where
262259
263260 -- Filter the exports for the specific module
264− filterModule :: [DeclarationRef] -> Exports -> Either String Exports
265− filterModule exps exported = do
261+ filterModule :: Exports -> Either String Exports
262+ filterModule exported = do
266263 types <- foldM (filterTypes $ exportedTypes exported) [] exps
267264 values <- foldM (filterValues $ exportedValues exported) [] exps
268265 classes <- foldM (filterClasses $ exportedTypeClasses exported) [] exps
@@ -271,34 +268,34 @@ filterExports mn exps env = do
271268 -- Ensure the exported types and data constructors exist in the module and add them to the set of
272269 -- exports
273270 filterTypes :: [(ProperName, [ProperName])] -> [(ProperName, [ProperName])] -> DeclarationRef -> Either String [(ProperName, [ProperName])]
274− filterTypes exps result (TypeRef name expDcons) = do
275− dcons <- maybe (throwError $ "Cannot export undefined type '" ++ show name ++ "'") return $ name `lookup` exps
271+ filterTypes expTys result (TypeRef name expDcons) = do
272+ dcons <- maybe (throwError $ "Cannot export undefined type '" ++ show name ++ "'") return $ name `lookup` expTys
276273 dcons' <- maybe (return dcons) (foldM (filterDcons name dcons) []) expDcons
277274 return $ (name, dcons') : result
278− filterTypes exps result _ = return result
275+ filterTypes _ result _ = return result
279276
280277 -- Ensure the exported data constructors exists for a type and add them to the list of exports
281278 filterDcons :: ProperName -> [ProperName] -> [ProperName] -> ProperName -> Either String [ProperName]
282− filterDcons tcon exps result name =
283− if name `elem` exps
279+ filterDcons tcon exps' result name =
280+ if name `elem` exps'
284281 then return $ name : result
285282 else throwError $ "Cannot export undefined data constructor '" ++ show name ++ "' for type '" ++ show tcon ++ "'"
286283
287284 -- Ensure the exported classes exist in the module and add them to the set of exports
288285 filterClasses :: [ProperName] -> [ProperName] -> DeclarationRef -> Either String [ProperName]
289− filterClasses exps result (TypeClassRef name) =
290− if name `elem` exps
286+ filterClasses exps' result (TypeClassRef name) =
287+ if name `elem` exps'
291288 then return $ name : result
292289 else throwError $ "Cannot export undefined type class '" ++ show name ++ "'"
293− filterClasses exps result _ = return result
290+ filterClasses _ result _ = return result
294291
295292 -- Ensure the exported values exist in the module and add them to the set of exports
296293 filterValues :: [Ident] -> [Ident] -> DeclarationRef -> Either String [Ident]
297− filterValues exps result (ValueRef name) =
298− if name `elem` exps
294+ filterValues exps' result (ValueRef name) =
295+ if name `elem` exps'
299296 then return $ name : result
300297 else throwError $ "Cannot export undefined value '" ++ show name ++ "'"
301− filterValues exps result _ = return result
298+ filterValues _ result _ = return result
302299
303300-- |
304301-- Type representing a set of declarations being explicitly imported from a module
@@ -333,53 +330,54 @@ resolveImports env (Module currentModule decls _) =
333330-- Extends the local environment for a module by resolving an import of another module.
334331--
335332resolveImport :: ModuleName -> ModuleName -> Exports -> ImportEnvironment -> Maybe ExplicitImports -> Either String ImportEnvironment
336−resolveImport currentModule importModule exp imp i = case i of
337− Nothing -> importAll imp
338− (Just expl) -> foldM importExplicit imp expl
333+resolveImport currentModule importModule exps imps i = case i of
334+ Nothing -> importAll
335+ Just expl -> foldM importExplicit imps expl
339336 where
340337
341338 -- Import everything from a module
342− importAll :: ImportEnvironment -> Either String ImportEnvironment
343− importAll imp = do
344− imp' <- foldM (\m (name, dctors) -> importExplicit m (TypeRef name (Just dctors))) imp (exportedTypes exp)
345− imp'' <- foldM (\m name -> importExplicit m (ValueRef name)) imp' (exportedValues exp)
346− foldM (\m name -> importExplicit m (TypeClassRef name)) imp'' (exportedTypeClasses exp)
339+ importAll :: Either String ImportEnvironment
340+ importAll = do
341+ imp' <- foldM (\m (name, dctors) -> importExplicit m (TypeRef name (Just dctors))) imps (exportedTypes exps)
342+ imp'' <- foldM (\m name -> importExplicit m (ValueRef name)) imp' (exportedValues exps)
343+ foldM (\m name -> importExplicit m (TypeClassRef name)) imp'' (exportedTypeClasses exps)
347344
348345 -- Import something explicitly
349346 importExplicit :: ImportEnvironment -> DeclarationRef -> Either String ImportEnvironment
350347 importExplicit imp (ValueRef name) = do
351− checkImportExists "value" values name
348+ _ <- checkImportExists "value" values name
352349 values' <- updateImports (importedValues imp) name
353350 return $ imp { importedValues = values' }
354351 importExplicit imp (TypeRef name dctors) = do
355− checkImportExists "type" types name
352+ _ <- checkImportExists "type" types name
356353 types' <- updateImports (importedTypes imp) name
357354 let allDctors = allExportedDataConstructors name
358355 dctors' <- maybe (return allDctors) (mapM $ checkDctorExists allDctors) dctors
359356 dctors'' <- foldM updateImports (importedDataConstructors imp) dctors'
360357 return $ imp { importedTypes = types', importedDataConstructors = dctors'' }
361358 importExplicit imp (TypeClassRef name) = do
362− checkImportExists "type class" classes name
359+ _ <- checkImportExists "type class" classes name
363360 typeClasses' <- updateImports (importedTypeClasses imp) name
364361 return $ imp { importedTypeClasses = typeClasses' }
365362
366363 -- Find all exported data constructors for a given type
367364 allExportedDataConstructors :: ProperName -> [ProperName]
368− allExportedDataConstructors name = fromMaybe [] $ name `lookup` exportedTypes exp
365+ allExportedDataConstructors name = fromMaybe [] $ name `lookup` exportedTypes exps
369366
370367 -- Add something to the ImportEnvironment if it does not already exist there
371368 updateImports :: (Ord a, Show a) => M.Map a (Qualified a) -> a -> Either String (M.Map a (Qualified a))
372369 updateImports m name = case M.lookup name m of
373370 Nothing -> return $ M.insert name (Qualified (Just importModule) name) m
371+ Just (Qualified Nothing _) -> error "Invalid state in updateImports"
374372 Just x@(Qualified (Just mn) _) -> throwError $
375373 if mn == currentModule || importModule == currentModule
376374 then "Definition '" ++ show name ++ "' conflicts with import '" ++ show (Qualified (Just importModule) name) ++ "'"
377375 else "Conflicting imports for '" ++ show name ++ "': '" ++ show x ++ "', '" ++ show (Qualified (Just importModule) name) ++ "'"
378376
379377 -- The available values, types, and classes in the module being imported
380− values = exportedValues exp
381− types = fst `map` exportedTypes exp
382− classes = exportedTypeClasses exp
378+ values = exportedValues exps
379+ types = fst `map` exportedTypes exps
380+ classes = exportedTypeClasses exps
383381
384382 -- Ensure that an explicitly imported data constructor exists for the type it is being imported
385383 -- from
src/Language/PureScript/Sugar/TypeClasses.hs+1−5
@@ -33,8 +33,7 @@ import Control.Applicative
3333import Control.Monad.State
3434import Control.Arrow (second)
3535
36−import Data.List (intercalate)
37−import Language.PureScript.CodeGen.Common (identToJs, moduleNameToJs)
36+import Language.PureScript.CodeGen.Common (identToJs)
3837
3938type MemberMap = M.Map (ModuleName, ProperName) ([String], [(String, Type)])
4039
@@ -155,9 +154,6 @@ typeInstanceDictionaryEntryDeclaration name mn deps className tys (ValueDeclarat
155154 lookupIdent members = maybe (Left $ "Type class " ++ show className ++ " does not have method " ++ show ident) Right $ lookup (identToJs ident) members
156155typeInstanceDictionaryEntryDeclaration _ _ _ _ _ _ = error "Invalid declaration in type instance definition"
157156
158−qualifiedToString :: ModuleName -> Qualified ProperName -> String
159−qualifiedToString mn (Qualified _ pn) = moduleNameToJs mn ++ "_" ++ runProperName pn
160−
161157-- |
162158-- Generate a name for a type class dictionary member, based on the module name, class name, type name and
163159-- member name
src/Language/PureScript/TypeChecker.hs+2−6
@@ -29,14 +29,12 @@ import Data.Maybe
2929import qualified Data.Map as M
3030import Control.Monad.State
3131import Control.Monad.Error
32−import Data.Either (rights, lefts)
3332
3433import Language.PureScript.Types
3534import Language.PureScript.Names
3635import Language.PureScript.Values
3736import Language.PureScript.Kinds
3837import Language.PureScript.Declarations
39−import Language.PureScript.Sugar.TypeClasses
4038import Language.PureScript.Prim
4139
4240addDataType :: ModuleName -> ProperName -> [String] -> [(ProperName, [Type])] -> Kind -> Check ()
@@ -79,7 +77,7 @@ addTypeClassDictionaries entries =
7977
8078checkTypeClassInstance :: ModuleName -> Type -> Check ()
8179checkTypeClassInstance _ (TypeVar _) = return ()
82−checkTypeClassInstance m (TypeConstructor ctor) = do
80+checkTypeClassInstance _ (TypeConstructor ctor) = do
8381 env <- getEnv
8482 when (ctor `M.member` typeSynonyms env) $ throwError "Type synonym instances are disallowed"
8583 return ()
@@ -170,7 +168,7 @@ typeCheckAll mainModuleName moduleName (d@(FixityDeclaration _ name) : rest) = d
170168 env <- getEnv
171169 guardWith ("Fixity declaration with no binding: " ++ name) $ M.member (moduleName, Op name) $ names env
172170 return $ d : ds
173−typeCheckAll mainModuleName currentModule (d@(ImportDeclaration moduleName idents) : rest) = do
171+typeCheckAll mainModuleName currentModule (d@(ImportDeclaration moduleName _) : rest) = do
174172 env <- getEnv
175173 let instances = filter (\tcd ->
176174 let Qualified (Just mn) _ = tcdName tcd in
@@ -182,11 +180,9 @@ typeCheckAll mainModuleName currentModule (d@(ImportDeclaration moduleName ident
182180 ds <- typeCheckAll mainModuleName currentModule rest
183181 return $ d : ds
184182typeCheckAll mainModuleName moduleName (d@TypeClassDeclaration{} : rest) = do
185− env <- getEnv
186183 ds <- typeCheckAll mainModuleName moduleName rest
187184 return $ d : ds
188185typeCheckAll mainModuleName moduleName (d@(TypeInstanceDeclaration dictName deps className tys _) : rest) = do
189− env <- getEnv
190186 mapM_ (checkTypeClassInstance moduleName) tys
191187 forM_ deps $ mapM_ (checkTypeClassInstance moduleName) . snd
192188 addTypeClassDictionaries [TypeClassDictionaryInScope (Qualified (Just moduleName) dictName) className tys (Just deps) TCDRegular]
src/Language/PureScript/TypeChecker/Kinds.hs−1
@@ -132,7 +132,6 @@ infer (TypeVar v) = do
132132 UnifyT . lift $ lookupTypeVariable moduleName (Qualified Nothing (ProperName v))
133133infer (TypeConstructor v) = do
134134 env <- liftCheck getEnv
135− Just moduleName <- checkCurrentModule <$> get
136135 case M.lookup v (types env) of
137136 Nothing -> UnifyT . lift . throwError $ "Unknown type constructor '" ++ show v ++ "'" ++ show (M.keys (types env))
138137 Just kind -> return kind
src/Language/PureScript/TypeChecker/Monad.hs−2
@@ -25,9 +25,7 @@ import Language.PureScript.Names
2525import Language.PureScript.Declarations
2626import Language.PureScript.Prim
2727
28−import Data.Data
2928import Data.Maybe
30−import Data.Generics (mkT, everywhere)
3129
3230import Control.Applicative
3331import Control.Monad.State
src/Language/PureScript/TypeChecker/Synonyms.hs+7−8
@@ -20,7 +20,6 @@ module Language.PureScript.TypeChecker.Synonyms (
2020
2121import Language.PureScript.Types
2222import Language.PureScript.Names
23−import Language.PureScript.TypeChecker.Monad (Environment(..))
2423
2524import Control.Applicative ((<$>))
2625import Data.Maybe (fromMaybe)
@@ -33,8 +32,8 @@ import Control.Monad.Error
3332-- |
3433-- Build a type substitution for a type synonym
3534--
36−buildTypeSubstitution :: Environment -> ModuleName -> Qualified ProperName -> Int -> Type -> Either String (Maybe Type)
37−buildTypeSubstitution env moduleName name n = go n []
35+buildTypeSubstitution :: Qualified ProperName -> Int -> Type -> Either String (Maybe Type)
36+buildTypeSubstitution name n = go n []
3837 where
3938 go :: Int -> [Type] -> Type -> Either String (Maybe Type)
4039 go 0 args (TypeConstructor ctor) | name == ctor = return (Just $ SaturatedTypeSynonym ctor args)
@@ -45,16 +44,16 @@ buildTypeSubstitution env moduleName name n = go n []
4544-- |
4645-- Replace all instances of a specific type synonym with the @SaturatedTypeSynonym@ data constructor
4746--
48−saturateTypeSynonym :: (Data d) => Environment -> ModuleName -> Qualified ProperName -> Int -> d -> Either String d
49−saturateTypeSynonym env moduleName name n = everywhereM' (mkM replace)
47+saturateTypeSynonym :: (Data d) => Qualified ProperName -> Int -> d -> Either String d
48+saturateTypeSynonym name n = everywhereM' (mkM replace)
5049 where
51− replace t = fromMaybe t <$> buildTypeSubstitution env moduleName name n t
50+ replace t = fromMaybe t <$> buildTypeSubstitution name n t
5251
5352-- |
5453-- Replace all type synonyms with the @SaturatedTypeSynonym@ data constructor
5554--
56−saturateAllTypeSynonyms :: (Data d) => Environment -> ModuleName -> [(Qualified ProperName, Int)] -> d -> Either String d
57−saturateAllTypeSynonyms env moduleName syns d = foldM (\result (name, n) -> saturateTypeSynonym env moduleName name n result) d syns
55+saturateAllTypeSynonyms :: (Data d) => [(Qualified ProperName, Int)] -> d -> Either String d
56+saturateAllTypeSynonyms syns d = foldM (\result (name, n) -> saturateTypeSynonym name n result) d syns
5857
5958
6059
src/Language/PureScript/TypeChecker/Types.hs+19−46
@@ -107,9 +107,7 @@ unifyTypes t1 t2 = rethrow (\e -> "Error unifying type " ++ prettyPrintType t1 +
107107 unifyTypes' ty f@ForAll{} = f `unifyTypes` ty
108108 unifyTypes' (Object row1) (Object row2) = row1 =?= row2
109109 unifyTypes' (TypeVar v1) (TypeVar v2) | v1 == v2 = return ()
110− unifyTypes' (TypeConstructor c1) (TypeConstructor c2) = do
111− env <- getEnv
112− Just moduleName <- checkCurrentModule <$> get
110+ unifyTypes' (TypeConstructor c1) (TypeConstructor c2) =
113111 guardWith ("Cannot unify " ++ show c1 ++ " with " ++ show c2 ++ ".") (c1 == c2)
114112 unifyTypes' (TypeApp t3 t4) (TypeApp t5 t6) = do
115113 t3 `unifyTypes` t5
@@ -307,10 +305,10 @@ typeHeadsAreEqual :: ModuleName -> Environment -> Type -> Type -> Maybe [(String
307305typeHeadsAreEqual _ _ (Skolem s1 _) (Skolem s2 _) | s1 == s2 = Just []
308306typeHeadsAreEqual _ _ (TypeVar v) t = Just [(v, t)]
309307typeHeadsAreEqual _ _ t (TypeVar v) = Just [(v, t)]
310−typeHeadsAreEqual m e (TypeConstructor c1) (TypeConstructor c2) | c1 == c2 = Just []
308+typeHeadsAreEqual _ _ (TypeConstructor c1) (TypeConstructor c2) | c1 == c2 = Just []
311309typeHeadsAreEqual m e (TypeApp h1 (TypeVar v)) (TypeApp h2 arg) = (:) (v, arg) <$> typeHeadsAreEqual m e h1 h2
312310typeHeadsAreEqual m e t1@(TypeApp _ _) t2@(TypeApp _ (TypeVar _)) = typeHeadsAreEqual m e t2 t1
313−typeHeadsAreEqual m e (SaturatedTypeSynonym name args) t2 = case expandTypeSynonym' e m name args of
311+typeHeadsAreEqual m e (SaturatedTypeSynonym name args) t2 = case expandTypeSynonym' e name args of
314312 Left _ -> Nothing
315313 Right t1 -> typeHeadsAreEqual m e t1 t2
316314typeHeadsAreEqual _ _ _ _ = Nothing
@@ -408,8 +406,6 @@ instantiatePolyTypeWithUnknowns val (ForAll ident ty _) = do
408406 ty' <- replaceVarWithUnknown ident ty
409407 instantiatePolyTypeWithUnknowns val ty'
410408instantiatePolyTypeWithUnknowns val (ConstrainedType constraints ty) = do
411− env <- getEnv
412− Just moduleName <- checkCurrentModule <$> get
413409 dicts <- getTypeClassDictionaries
414410 (_, ty') <- instantiatePolyTypeWithUnknowns (error "Types under a constraint cannot themselves be constrained") ty
415411 return (foldl App val (map (flip TypeClassDictionary dicts) constraints), ty')
@@ -427,18 +423,17 @@ replaceVarWithUnknown ident ty = do
427423-- Replace fully applied type synonyms with the @SaturatedTypeSynonym@ data constructor, which helps generate
428424-- better error messages during unification.
429425--
430−replaceAllTypeSynonyms' :: (D.Data d) => Environment -> ModuleName -> d -> Either String d
431−replaceAllTypeSynonyms' env moduleName d =
426+replaceAllTypeSynonyms' :: (D.Data d) => Environment -> d -> Either String d
427+replaceAllTypeSynonyms' env d =
432428 let
433429 syns = map (\(name, (args, _)) -> (name, length args)) . M.toList $ typeSynonyms env
434430 in
435− saturateAllTypeSynonyms env moduleName syns d
431+ saturateAllTypeSynonyms syns d
436432
437433replaceAllTypeSynonyms :: (Functor m, Monad m, MonadState CheckState m, MonadError String m) => (D.Data d) => d -> m d
438434replaceAllTypeSynonyms d = do
439435 env <- getEnv
440− Just moduleName <- checkCurrentModule <$> get
441− either throwError return $ replaceAllTypeSynonyms' env moduleName d
436+ either throwError return $ replaceAllTypeSynonyms' env d
442437
443438-- |
444439-- \"Desaturate\" @SaturatedTypeSynonym@s
@@ -452,19 +447,18 @@ desaturateAllTypeSynonyms = everywhere (mkT replaceSaturatedTypeSynonym)
452447-- |
453448-- Replace a type synonym and its arguments with the aliased type
454449--
455−expandTypeSynonym' :: Environment -> ModuleName -> Qualified ProperName -> [Type] -> Either String Type
456−expandTypeSynonym' env moduleName name args =
450+expandTypeSynonym' :: Environment -> Qualified ProperName -> [Type] -> Either String Type
451+expandTypeSynonym' env name args =
457452 case M.lookup name (typeSynonyms env) of
458453 Just (synArgs, body) -> do
459454 let repl = replaceAllTypeVars (zip synArgs args) body
460− replaceAllTypeSynonyms' env moduleName repl
455+ replaceAllTypeSynonyms' env repl
461456 Nothing -> error "Type synonym was not defined"
462457
463458expandTypeSynonym :: (Functor m, Monad m, MonadState CheckState m, MonadError String m) => Qualified ProperName -> [Type] -> m Type
464459expandTypeSynonym name args = do
465460 env <- getEnv
466− Just moduleName <- checkCurrentModule <$> get
467− either throwError return $ expandTypeSynonym' env moduleName name args
461+ either throwError return $ expandTypeSynonym' env name args
468462
469463-- |
470464-- Ensure a set of property names and value does not contain duplicate labels
@@ -531,13 +525,11 @@ infer' (Var var) = do
531525 ty <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< lookupVariable moduleName $ var
532526 case ty of
533527 ConstrainedType constraints ty' -> do
534− env <- getEnv
535528 dicts <- getTypeClassDictionaries
536529 return $ TypedValue True (foldl App (Var var) (map (flip TypeClassDictionary dicts) constraints)) ty'
537530 _ -> return $ TypedValue True (Var var) ty
538531infer' v@(Constructor c) = do
539532 env <- getEnv
540− Just moduleName <- checkCurrentModule <$> get
541533 case M.lookup c (dataConstructors env) of
542534 Nothing -> throwError $ "Constructor " ++ show c ++ " is undefined"
543535 Just ty -> do ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms $ ty
@@ -588,14 +580,13 @@ inferBinder val (BooleanBinder _) = val =?= tyBoolean >> return M.empty
588580inferBinder val (VarBinder name) = return $ M.singleton name val
589581inferBinder val (ConstructorBinder ctor binders) = do
590582 env <- getEnv
591− Just moduleName <- checkCurrentModule <$> get
592583 case M.lookup ctor (dataConstructors env) of
593584 Just ty -> do
594585 (_, fn) <- instantiatePolyTypeWithUnknowns (error "Data constructor types cannot contains constraints") ty
595586 go binders fn
596587 where
597588 go [] ty' = do
598− subsumes Nothing val ty'
589+ _ <- subsumes Nothing val ty'
599590 return M.empty
600591 go (binder : binders') (TypeApp (TypeApp t obj) ret) | t == tyFunction =
601592 M.union <$> inferBinder obj binder <*> go binders' ret
@@ -648,17 +639,6 @@ checkBinders nvals ret (CaseAlternative binders grd val : bs) = do
648639 rs <- checkBinders nvals ret bs
649640 return $ r : rs
650641
651−-- |
652−-- Check that a local variable name is not already used
653−--
654−assignVariable :: Ident -> UnifyT Type Check ()
655−assignVariable name = do
656− env <- checkEnv <$> get
657− Just moduleName <- checkCurrentModule <$> get
658− case M.lookup (moduleName, name) (names env) of
659− Just _ -> UnifyT . lift . throwError $ "Variable with name " ++ show name ++ " already exists."
660− _ -> return ()
661−
662642-- |
663643-- Generate a new skolem constant
664644--
@@ -711,8 +691,6 @@ check' val (ForAll ident ty _) = do
711691 val' <- check val sk
712692 return $ TypedValue True val' (ForAll ident ty (Just scope))
713693check' val t@(ConstrainedType constraints ty) = do
714− env <- getEnv
715− Just moduleName <- checkCurrentModule <$> get
716694 dictNames <- forM constraints $ \(Qualified _ (ProperName className), _) -> do
717695 n <- liftCheck freshDictionaryName
718696 return $ Ident $ "__dict_" ++ className ++ "_" ++ show n
@@ -747,7 +725,7 @@ check' (Abs (Right _) _) _ = error "Binder was not desugared"
747725check' (App f arg) ret = do
748726 f'@(TypedValue _ _ ft) <- infer f
749727 (ret', app) <- checkFunctionApplication f' ft arg
750− subsumes Nothing ret' ret
728+ _ <- subsumes Nothing ret' ret
751729 return $ TypedValue True app ret
752730check' v@(Var var) ty = do
753731 Just moduleName <- checkCurrentModule <$> get
@@ -797,7 +775,6 @@ check' (Accessor prop val) ty = do
797775 return $ TypedValue True (Accessor prop val') ty
798776check' (Constructor c) ty = do
799777 env <- getEnv
800− Just moduleName <- checkCurrentModule <$> get
801778 case M.lookup c (dataConstructors env) of
802779 Nothing -> throwError $ "Constructor " ++ show c ++ " is undefined"
803780 Just ty1 -> do
@@ -875,9 +852,7 @@ checkFunctionApplication' fn (SaturatedTypeSynonym name tyArgs) arg = do
875852 ty <- introduceSkolemScope <=< expandTypeSynonym name $ tyArgs
876853 checkFunctionApplication fn ty arg
877854checkFunctionApplication' fn (ConstrainedType constraints fnTy) arg = do
878− env <- getEnv
879855 dicts <- getTypeClassDictionaries
880− Just moduleName <- checkCurrentModule <$> get
881856 checkFunctionApplication' (foldl App fn (map (flip TypeClassDictionary dicts) constraints)) fnTy arg
882857checkFunctionApplication' _ fnTy arg = throwError $ "Cannot apply a function of type "
883858 ++ prettyPrintType fnTy
@@ -910,8 +885,8 @@ subsumes' val ty1 (ForAll ident ty2 sco) =
910885 subsumes val ty1 sk
911886 Nothing -> throwError "Skolem variable scope is unspecified"
912887subsumes' val (TypeApp (TypeApp f1 arg1) ret1) (TypeApp (TypeApp f2 arg2) ret2) | f1 == tyFunction && f2 == tyFunction = do
913− subsumes Nothing arg2 arg1
914− subsumes Nothing ret1 ret2
888+ _ <- subsumes Nothing arg2 arg1
889+ _ <- subsumes Nothing ret1 ret2
915890 return val
916891subsumes' val (SaturatedTypeSynonym name tyArgs) ty2 = do
917892 ty1 <- introduceSkolemScope <=< expandTypeSynonym name $ tyArgs
@@ -920,9 +895,7 @@ subsumes' val ty1 (SaturatedTypeSynonym name tyArgs) = do
920895 ty2 <- introduceSkolemScope <=< expandTypeSynonym name $ tyArgs
921896 subsumes val ty1 ty2
922897subsumes' (Just val) (ConstrainedType constraints ty1) ty2 = do
923− env <- getEnv
924898 dicts <- getTypeClassDictionaries
925− Just moduleName <- checkCurrentModule <$> get
926899 _ <- subsumes' Nothing ty1 ty2
927900 return . Just $ foldl App val (map (flip TypeClassDictionary dicts) constraints)
928901subsumes' val (Object r1) (Object r2) = do
@@ -937,14 +910,14 @@ subsumes' val (Object r1) (Object r2) = do
937910 go [] ts2 r1' r2' = r1' =?= rowFromList (ts2, r2')
938911 go ts1 [] r1' r2' = r2' =?= rowFromList (ts1, r1')
939912 go ((p1, ty1) : ts1) ((p2, ty2) : ts2) r1' r2'
940− | p1 == p2 = do subsumes Nothing ty1 ty2
913+ | p1 == p2 = do _ <- subsumes Nothing ty1 ty2
941914 go ts1 ts2 r1' r2'
942915 | p1 < p2 = do rest <- fresh
943916 r2' =?= RCons p1 ty1 rest
944917 go ts1 ((p2, ty2) : ts2) r1' rest
945− | p1 > p2 = do rest <- fresh
946− r1' =?= RCons p2 ty2 rest
947− go ((p1, ty1) : ts1) ts2 rest r2'
918+ | otherwise = do rest <- fresh
919+ r1' =?= RCons p2 ty2 rest
920+ go ((p1, ty1) : ts1) ts2 rest r2'
948921subsumes' val ty1 ty2@(Object _) = subsumes val ty2 ty1
949922subsumes' val ty1 ty2 = do
950923 ty1 =?= ty2
src/Language/PureScript/Types.hs+1
@@ -146,6 +146,7 @@ replaceTypeVars = replaceTypeVars' []
146146 go _ ty = ty
147147 genName orig inUse = try 0
148148 where
149+ try :: Integer -> String
149150 try n | (orig ++ show n) `elem` inUse = try (n + 1)
150151 | otherwise = orig ++ show n
151152