git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge branch 'master' into auto-prelude

Conflicts: src/Language/PureScript/Sugar/Operators.hs

commitcb906fa8066a5a3f470510ae2d0a7c231a68b373 authorPhil Freeman <paf31@cantab.net> dateTue 18 Mar 2014, 15:26 (13 years ago) parent1fceac348bae90
7 files changed+96−68
RELEASE-0.5.md+3 src/Language/PureScript/Parser/Common.hs+1−1 src/Language/PureScript/Sugar.hs+2−2 src/Language/PureScript/Sugar/Names.hs+8−2 src/Language/PureScript/Sugar/Operators.hs+40−46 src/Language/PureScript/TypeChecker.hs+3−2 src/Language/PureScript/TypeChecker/Types.hs+39−15
RELEASE-0.5.md+3
@@ -40,6 +40,8 @@
4040- Imported type class instances are now exported (paf31)
4141- Some error messages were improved (paf31)
4242- Qualfied module imports are now supported as `import qualified M as X` (garyb).
43+- The escape check was removed, since it was too restrictive (paf31)
44+- The binary operator reordering step was greatly simplified (paf31)
4345
4446### Bug Fixes
4547
@@ -51,6 +53,7 @@
5153- A type checker bug related to array literals was fixed (paf31)
5254- Externs files (`--externs`) are now working again (paf31)
5355- Precedence of backticked infix functions have been corrected (paf31)
56+- A bug which allowed some incorrect type class instances to pass the type checker was fixed (paf31)
5457
5558### Libraries
5659
src/Language/PureScript/Parser/Common.hs+1−1
@@ -328,7 +328,7 @@ buildPostfixParser fs first = do
328328-- Parse an identifier in backticks or an operator
329329--
330330parseIdentInfix :: P.Parsec String ParseState (Qualified Ident)
331−parseIdentInfix = P.between tick tick (parseQualified (Ident <$> identifier)) <|> Qualified Nothing <$> (Op <$> operator)
331+parseIdentInfix = P.between tick tick (parseQualified (Ident <$> identifier)) <|> (parseQualified (Op <$> operator))
332332
333333-- |
334334-- Mark the current indentation level
src/Language/PureScript/Sugar.hs+2−2
@@ -49,11 +49,11 @@ import Language.PureScript.Sugar.Names as S
4949-- * Qualify any unqualified names and types
5050--
5151desugar :: [Module] -> Either String [Module]
52−desugar = rebracket
53− >=> desugarDo
52+desugar = desugarDo
5453 >=> desugarLetBindings
5554 >>> desugarCasesModule
5655 >=> desugarImports
56+ >=> rebracket
5757 >=> desugarTypeDeclarationsModule
5858 >=> desugarTypeClasses
5959 >=> createBindingGroupsModule
src/Language/PureScript/Sugar/Names.hs+8−2
@@ -190,8 +190,14 @@ renameInModule imports exports (Module mn decls exps) =
190190 ValueDeclaration name nameKind [] Nothing <$> everywhereWithContextM' [] (mkS bindFunctionArgs `extS` bindBinders) val
191191 where
192192 bindFunctionArgs bound (Abs (Left arg) val') = return (arg : bound, Abs (Left arg) val')
193− bindFunctionArgs bound (Var name'@(Qualified Nothing ident)) | ident `notElem` bound = (,) bound <$> (Var <$> updateValueName name')
194− bindFunctionArgs bound (Var name'@(Qualified (Just _) _)) = (,) bound <$> (Var <$> updateValueName name')
193+ bindFunctionArgs bound (Var name'@(Qualified Nothing ident)) | ident `notElem` bound =
194+ (,) bound <$> (Var <$> updateValueName name')
195+ bindFunctionArgs bound (Var name'@(Qualified (Just _) _)) =
196+ (,) bound <$> (Var <$> updateValueName name')
197+ bindFunctionArgs bound (BinaryNoParens name'@(Qualified Nothing ident) v1 v2) | ident `notElem` bound =
198+ (,) bound <$> (BinaryNoParens <$> updateValueName name' <*> pure v1 <*> pure v2)
199+ bindFunctionArgs bound (BinaryNoParens name'@(Qualified (Just _) _) v1 v2) =
200+ (,) bound <$> (BinaryNoParens <$> updateValueName name' <*> pure v1 <*> pure v2)
195201 bindFunctionArgs bound other = return (bound, other)
196202 bindBinders :: [Ident] -> CaseAlternative -> Either String ([Ident], CaseAlternative)
197203 bindBinders bound c@(CaseAlternative bs _ _) = return (binderNames bs ++ bound, c)
src/Language/PureScript/Sugar/Operators.hs+40−46
@@ -28,15 +28,15 @@ import Language.PureScript.Declarations
2828import Language.PureScript.Values
2929
3030import Control.Applicative
31−import Control.Arrow (first)
3231import Control.Monad.State
32+import Control.Monad.Error.Class
3333
3434import Data.Maybe (fromMaybe, mapMaybe)
3535import Data.Function (on)
3636import Data.Functor.Identity
37−import Data.List (groupBy, sortBy)
37+import Data.List (sort, groupBy, sortBy)
3838
39−import qualified Data.Map as M
39+import qualified Data.Data as D
4040import qualified Data.Generics as G
4141import qualified Data.Generics.Extras as G
4242
@@ -48,28 +48,41 @@ import qualified Text.Parsec.Expr as P
4848-- Remove explicit parentheses and reorder binary operator applications
4949--
5050rebracket :: [Module] -> Either String [Module]
51−rebracket = go M.empty []
51+rebracket ms = do
52+ let fixities = concatMap collectFixities ms
53+ ensureNoDuplicates $ map fst fixities
54+ let opTable = customOperatorTable fixities
55+ mapM (rebracketModule opTable) ms
56+
57+rebracketModule :: [[(Qualified Ident, Value -> Value -> Value, Associativity)]] -> Module -> Either String Module
58+rebracketModule opTable (Module mn ds exts) = Module mn <$> (removeParens <$> G.everywhereM' (G.mkM (matchOperators opTable)) ds) <*> pure exts
59+
60+removeParens :: (D.Data d) => d -> d
61+removeParens = G.everywhere (G.mkT go)
62+ where
63+ go (Parens val) = val
64+ go val = val
65+
66+collectFixities :: Module -> [(Qualified Ident, Fixity)]
67+collectFixities (Module moduleName ds _) = concatMap collect ds
68+ where
69+ collect :: Declaration -> [(Qualified Ident, Fixity)]
70+ collect (FixityDeclaration fixity name) = [(Qualified (Just moduleName) (Op name), fixity)]
71+ collect _ = []
72+
73+ensureNoDuplicates :: [Qualified Ident] -> Either String ()
74+ensureNoDuplicates m = go $ sort m
5275 where
53− go _ rb [] = return . reverse $ rb
54− go m rb (Module name ds exps : ms) = do
55− m' <- M.union m <$> collectFixities m name ds
56− let opTable = customOperatorTable m'
57− ds' <- G.everywhereM' (G.mkM (matchOperators name opTable)) ds
58− go m' (Module name (G.everywhere (G.mkT removeParens) ds') exps : rb) ms
59−
60−removeParens :: Value -> Value
61−removeParens (Parens val) = val
62−removeParens val = val
63−
64−customOperatorTable :: M.Map (Qualified Ident) Fixity -> [[(Qualified Ident, Value -> Value -> Value, Associativity)]]
76+ go [] = return ()
77+ go [_] = return ()
78+ go (x : y : _) | x == y = throwError $ "Redefined fixity for " ++ show x
79+ go (_ : rest) = go rest
80+
81+customOperatorTable :: [(Qualified Ident, Fixity)] -> [[(Qualified Ident, Value -> Value -> Value, Associativity)]]
6582customOperatorTable fixities =
6683 let
67− -- We make the assumption here that infix operators are not qualified. The parser currently enforces this.
68− -- The fixity map can therefore map from module name/ident pairs to fixities, where the module name is the name
69− -- of the module imported into, not from. This is useful in matchOp, but here we have to discard the module name to
70− -- make sure that the generated code is correct.
71− applyUserOp (Qualified _ name) t1 = App (App (Var (Qualified Nothing name)) t1)
72− userOps = map (\(name, Fixity a p) -> (name, applyUserOp name, p, a)) . M.toList $ fixities
84+ applyUserOp ident t1 = App (App (Var ident) t1)
85+ userOps = map (\(name, Fixity a p) -> (name, applyUserOp name, p, a)) fixities
7386 sorted = sortBy (flip compare `on` (\(_, _, p, _) -> p)) userOps
7487 groups = groupBy ((==) `on` (\(_, _, p, _) -> p)) sorted
7588 in
@@ -77,8 +90,8 @@ customOperatorTable fixities =
7790
7891type Chain = [Either Value (Qualified Ident)]
7992
80−matchOperators :: ModuleName -> [[(Qualified Ident, Value -> Value -> Value, Associativity)]] -> Value -> Either String Value
81−matchOperators moduleName ops = G.everywhereM' (G.mkM parseChains)
93+matchOperators :: [[(Qualified Ident, Value -> Value -> Value, Associativity)]] -> Value -> Either String Value
94+matchOperators ops = parseChains
8295 where
8396 parseChains :: Value -> Either String Value
8497 parseChains b@BinaryNoParens{} = bracketChain (extendChain b)
@@ -89,7 +102,7 @@ matchOperators moduleName ops = G.everywhereM' (G.mkM parseChains)
89102 bracketChain :: Chain -> Either String Value
90103 bracketChain = either (Left . show) Right . P.parse (P.buildExpressionParser opTable parseValue <* P.eof) "operator expression"
91104 opTable = [P.Infix (P.try (parseTicks >>= \ident -> return (\t1 t2 -> App (App (Var ident) t1) t2))) P.AssocLeft]
92− : map (map (\(name, f, a) -> P.Infix (P.try (matchOp moduleName name) >> return f) (toAssoc a))) ops
105+ : map (map (\(name, f, a) -> P.Infix (P.try (matchOp name) >> return f) (toAssoc a))) ops
93106 ++ [[ P.Infix (P.try (parseOp >>= \ident -> return (\t1 t2 -> App (App (Var ident) t1) t2))) P.AssocLeft ]]
94107
95108toAssoc :: Associativity -> P.Assoc
@@ -114,27 +127,8 @@ parseTicks = token (either (const Nothing) fromOp) P.<?> "infix function"
114127 fromOp q@(Qualified _ (Ident _)) = Just q
115128 fromOp _ = Nothing
116129
117−matchOp :: ModuleName -> Qualified Ident -> P.Parsec Chain () ()
118−matchOp moduleName op = do
130+matchOp :: Qualified Ident -> P.Parsec Chain () ()
131+matchOp op = do
119132 ident <- parseOp
120133 guard (qualify moduleName ident == qualify moduleName op)
121134
122−collectFixities :: M.Map (Qualified Ident) Fixity -> ModuleName -> [Declaration] -> Either String (M.Map (Qualified Ident) Fixity)
123−collectFixities m _ [] = return m
124−collectFixities m moduleName (FixityDeclaration fixity name : rest) = do
125− let qual = Qualified (Just moduleName) (Op name)
126− when (qual `M.member` m) (Left $ "redefined fixity for " ++ show name)
127− collectFixities (M.insert qual fixity m) moduleName rest
128−collectFixities m moduleName (ImportDeclaration importedModule explImports qual : rest) = do
129− let qualName = fromMaybe moduleName qual
130− let fs = [ (i, fixity) | (Qualified mn i, fixity) <- M.toList m, mn == Just importedModule && isImported explImports i ]
131− let m' = M.fromList (map (first (Qualified (Just qualName))) fs)
132− collectFixities (m' `M.union` m) moduleName rest
133− where
134− isImported Nothing _ = True
135− isImported (Just imps) i = i `elem` (mapMaybe getExportedValue imps)
136− getExportedValue (ValueRef name) = Just name
137− getExportedValue _ = Nothing
138−
139−collectFixities m moduleName (_:ds) = collectFixities m moduleName ds
140−
src/Language/PureScript/TypeChecker.hs+3−2
@@ -36,6 +36,7 @@ import Language.PureScript.Values
3636import Language.PureScript.Kinds
3737import Language.PureScript.Declarations
3838import Language.PureScript.Environment
39+import Language.PureScript.Pretty.Types
3940
4041addDataType :: ModuleName -> ProperName -> [String] -> [(ProperName, [Type])] -> Kind -> Check ()
4142addDataType moduleName name args dctors ctorKind = do
@@ -86,8 +87,8 @@ checkTypeClassInstance _ (TypeConstructor ctor) = do
8687 env <- getEnv
8788 when (ctor `M.member` typeSynonyms env) $ throwError "Type synonym instances are disallowed"
8889 return ()
89−checkTypeClassInstance m (TypeApp ty (TypeVar _)) = checkTypeClassInstance m ty
90−checkTypeClassInstance _ _ = throwError "Type class instance must be of the form T a1 ... an"
90+checkTypeClassInstance m (TypeApp t1 t2) = checkTypeClassInstance m t1 >> checkTypeClassInstance m t2
91+checkTypeClassInstance _ ty = throwError $ "Type class instance head is invalid: " ++ prettyPrintType ty
9192
9293-- |
9394-- Type check all declarations in a module
src/Language/PureScript/TypeChecker/Types.hs+39−15
@@ -246,21 +246,22 @@ overTypes f = everywhere (mkT f)
246246replaceTypeClassDictionaries :: ModuleName -> Value -> Check Value
247247replaceTypeClassDictionaries mn = everywhereM' (mkM go)
248248 where
249− go (TypeClassDictionary constraint dicts) = entails mn dicts constraint
249+ go (TypeClassDictionary constraint dicts) = do
250+ env <- getEnv
251+ entails env mn dicts constraint
250252 go other = return other
251253
252254-- |
253255-- Check that the current set of type class dictionaries entail the specified type class goal, and, if so,
254256-- return a type class dictionary reference.
255257--
256−entails :: ModuleName -> [TypeClassDictionaryInScope] -> (Qualified ProperName, [Type]) -> Check Value
257−entails moduleName context goal@(className, tys) = do
258− env <- getEnv
259− case go env goal of
258+entails :: Environment -> ModuleName -> [TypeClassDictionaryInScope] -> (Qualified ProperName, [Type]) -> Check Value
259+entails env moduleName context goal@(className, tys) = do
260+ case go goal of
260261 [] -> throwError $ "No " ++ show className ++ " instance found for " ++ intercalate ", " (map prettyPrintType tys)
261262 (dict : _) -> return dict
262263 where
263− go env (className', tys') =
264+ go (className', tys') =
264265 [ mkDictionary (canonicalizeDictionary tcd) args
265266 | tcd <- context
266267 -- Choose type class dictionaries in scope in the current module
@@ -270,14 +271,14 @@ entails moduleName context goal@(className, tys) = do
270271 -- Make sure the type unifies with the type in the type instance definition
271272 , subst <- maybeToList . (>>= verifySubstitution) . fmap concat $ zipWithM (typeHeadsAreEqual moduleName env) tys' (tcdInstanceTypes tcd)
272273 -- Solve any necessary subgoals
273− , args <- solveSubgoals env subst (tcdDependencies tcd) ]
274+ , args <- solveSubgoals subst (tcdDependencies tcd) ]
274275 -- Create dictionaries for subgoals which still need to be solved by calling go recursively
275276 -- E.g. the goal (Show a, Show b) => Show (Either a b) can be satisfied if the current type
276277 -- unifies with Either a b, and we can satisfy the subgoals Show a and Show b recursively.
277− solveSubgoals :: Environment -> [(String, Type)] -> Maybe [(Qualified ProperName, [Type])] -> [Maybe [Value]]
278− solveSubgoals _ _ Nothing = return Nothing
279− solveSubgoals env subst (Just subgoals) = do
280− dict <- mapM (go env . second (map (replaceAllTypeVars subst))) subgoals
278+ solveSubgoals :: [(String, Type)] -> Maybe [(Qualified ProperName, [Type])] -> [Maybe [Value]]
279+ solveSubgoals _ Nothing = return Nothing
280+ solveSubgoals subst (Just subgoals) = do
281+ dict <- mapM (go . second (map (replaceAllTypeVars subst))) subgoals
281282 return $ Just dict
282283 -- Make a dictionary from subgoal dictionaries by applying the correct function
283284 mkDictionary :: Qualified Ident -> Maybe [Value] -> Value
@@ -293,20 +294,43 @@ entails moduleName context goal@(className, tys) = do
293294 verifySubstitution :: [(String, Type)] -> Maybe [(String, Type)]
294295 verifySubstitution subst = do
295296 let grps = groupBy ((==) `on` fst) subst
296− guard (all ((==) 1 . length . nubBy ((==) `on` snd)) grps)
297+ guard (all (pairwise (unifiesWith env) . map snd) grps)
297298 return $ map head grps
298299
300+-- |
301+-- Check all values in a list pairwise match a predicate
302+--
303+pairwise :: (a -> a -> Bool) -> [a] -> Bool
304+pairwise _ [] = True
305+pairwise _ [_] = True
306+pairwise p (x : xs) = all (p x) xs && pairwise p xs
307+
308+-- |
309+-- Check that two types unify
310+--
311+unifiesWith :: Environment -> Type -> Type -> Bool
312+unifiesWith _ (TUnknown _) _ = True
313+unifiesWith _ _ (TUnknown _) = True
314+unifiesWith _ (Skolem s1 _) (Skolem s2 _) | s1 == s2 = True
315+unifiesWith _ (TypeVar v1) (TypeVar v2) | v1 == v2 = True
316+unifiesWith _ (TypeConstructor c1) (TypeConstructor c2) | c1 == c2 = True
317+unifiesWith e (TypeApp h1 t1) (TypeApp h2 t2) = unifiesWith e h1 h2 && unifiesWith e t1 t2
318+unifiesWith e (SaturatedTypeSynonym name args) t2 =
319+ case expandTypeSynonym' e name args of
320+ Left _ -> False
321+ Right t1 -> unifiesWith e t1 t2
322+unifiesWith e t1 t2@(SaturatedTypeSynonym _ _) = unifiesWith e t2 t1
323+unifiesWith _ _ _ = False
324+
299325-- |
300326-- Check whether the type heads of two types are equal (for the purposes of type class dictionary lookup),
301327-- and return a substitution from type variables to types which makes the type heads unify.
302328--
303329typeHeadsAreEqual :: ModuleName -> Environment -> Type -> Type -> Maybe [(String, Type)]
304330typeHeadsAreEqual _ _ (Skolem s1 _) (Skolem s2 _) | s1 == s2 = Just []
305−typeHeadsAreEqual _ _ (TypeVar v) t = Just [(v, t)]
306331typeHeadsAreEqual _ _ t (TypeVar v) = Just [(v, t)]
307332typeHeadsAreEqual _ _ (TypeConstructor c1) (TypeConstructor c2) | c1 == c2 = Just []
308−typeHeadsAreEqual m e (TypeApp h1 (TypeVar v)) (TypeApp h2 arg) = (:) (v, arg) <$> typeHeadsAreEqual m e h1 h2
309−typeHeadsAreEqual m e t1@(TypeApp _ _) t2@(TypeApp _ (TypeVar _)) = typeHeadsAreEqual m e t2 t1
333+typeHeadsAreEqual m e (TypeApp h1 t1) (TypeApp h2 t2) = (++) <$> typeHeadsAreEqual m e h1 h2 <*> typeHeadsAreEqual m e t1 t2
310334typeHeadsAreEqual m e (SaturatedTypeSynonym name args) t2 = case expandTypeSynonym' e name args of
311335 Left _ -> Nothing
312336 Right t1 -> typeHeadsAreEqual m e t1 t2