@@ -17,13 +17,15 @@ module Language.PureScript.BindingGroups (
17 createBindingGroupsModule
17 createBindingGroupsModule
20import Data.Graph
21import Data.Graph
21−import Data.List (intersect)
23+import Data.List (nub, intersect)
23import Language.PureScript.Declarations
25import Language.PureScript.Declarations
24import Language.PureScript.Names
26import Language.PureScript.Names
25import Language.PureScript.Values
27import Language.PureScript.Values
26−import Language.PureScript.Scope (usedNames)
28+import Language.PureScript.Types
28createBindingGroupsModule :: [Module] -> [Module]
30createBindingGroupsModule :: [Module] -> [Module]
29createBindingGroupsModule = map $ \(Module name ds) -> Module name (createBindingGroups ds)
31createBindingGroupsModule = map $ \(Module name ds) -> Module name (createBindingGroups ds)
@@ -32,27 +34,65 @@ createBindingGroups :: [Declaration] -> [Declaration]
32createBindingGroups ds =
34createBindingGroups ds =
34 values = filter isValueDecl ds
36 values = filter isValueDecl ds
37+ dataDecls = filter isDataDecl ds
35− nonValues = filter (not . isValueDecl) ds
38+ nonValues = filter (\d -> not (isValueDecl d) && not (isDataDecl d)) ds
39+ allProperNames = map getProperName dataDecls
40+ dataVerts = map (\d -> (d, getProperName d, usedProperNames d `intersect` allProperNames)) dataDecls
41+ dataBindingGroupDecls = map toDataBindingGroup $ stronglyConnComp dataVerts
36 allIdents = map getIdent values
42 allIdents = map getIdent values
37− verts = map (\d -> (d, getIdent d, usedNames d `intersect` allIdents)) values
43+ valueVerts = map (\d -> (d, getIdent d, usedIdents d `intersect` allIdents)) values
38− sorted = map toBindingGroup $ stronglyConnComp verts
44+ bindingGroupDecls = map toBindingGroup $ stronglyConnComp valueVerts
40− nonValues ++ sorted
46+ dataBindingGroupDecls ++ nonValues ++ bindingGroupDecls
48+usedIdents :: (Data d) => d -> [Ident]
49+usedIdents = nub . everything (++) (mkQ [] namesV `extQ` namesS)
51+ namesV :: Value -> [Ident]
52+ namesV (Var (Qualified Nothing name)) = [name]
54+ namesS :: Statement -> [Ident]
55+ namesS (VariableIntroduction name _) = [name]
58+usedProperNames :: (Data d) => d -> [ProperName]
59+usedProperNames = nub . everything (++) (mkQ [] names)
61+ names :: Type -> [ProperName]
62+ names (TypeConstructor (Qualified Nothing name)) = [name]
42isValueDecl :: Declaration -> Bool
65isValueDecl :: Declaration -> Bool
43isValueDecl (ValueDeclaration _ _ _ _) = True
66isValueDecl (ValueDeclaration _ _ _ _) = True
44isValueDecl _ = False
67isValueDecl _ = False
69+isDataDecl :: Declaration -> Bool
70+isDataDecl (DataDeclaration _ _ _) = True
46getIdent :: Declaration -> Ident
73getIdent :: Declaration -> Ident
47getIdent (ValueDeclaration ident _ _ _) = ident
74getIdent (ValueDeclaration ident _ _ _) = ident
48getIdent _ = error "Expected ValueDeclaration"
75getIdent _ = error "Expected ValueDeclaration"
77+getProperName :: Declaration -> ProperName
78+getProperName (DataDeclaration pn _ _) = pn
79+getProperName _ = error "Expected DataDeclaration"
50toBindingGroup :: SCC Declaration -> Declaration
81toBindingGroup :: SCC Declaration -> Declaration
51toBindingGroup (AcyclicSCC d) = d
82toBindingGroup (AcyclicSCC d) = d
52toBindingGroup (CyclicSCC [d]) = d
83toBindingGroup (CyclicSCC [d]) = d
53−toBindingGroup (CyclicSCC ds') = BindingGroupDeclaration (map fromValueDecl ds')
84+toBindingGroup (CyclicSCC ds') = BindingGroupDeclaration $ map fromValueDecl ds'
86+toDataBindingGroup :: SCC Declaration -> Declaration
87+toDataBindingGroup (AcyclicSCC d) = d
88+toDataBindingGroup (CyclicSCC [d]) = d
89+toDataBindingGroup (CyclicSCC ds') = DataBindingGroupDeclaration $ map fromDataDecl ds'
55fromValueDecl :: Declaration -> (Ident, Value)
91fromValueDecl :: Declaration -> (Ident, Value)
56fromValueDecl (ValueDeclaration ident [] Nothing val) = (ident, val)
92fromValueDecl (ValueDeclaration ident [] Nothing val) = (ident, val)
57fromValueDecl (ValueDeclaration _ _ _ _) = error "Binders should have been desugared"
93fromValueDecl (ValueDeclaration _ _ _ _) = error "Binders should have been desugared"
58fromValueDecl _ = error "Expected ValueDeclaration"
94fromValueDecl _ = error "Expected ValueDeclaration"
96+fromDataDecl :: Declaration -> (ProperName, [String], [(ProperName, Maybe PolyType)])
97+fromDataDecl (DataDeclaration pn args ctors) = (pn, args, ctors)
98+fromDataDecl _ = error "Expected DataDeclaration"