git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Break up Declarations
a0542afGary Burgess
psci/Main.hs+1−1
@@ -45,7 +45,7 @@ import qualified System.IO.UTF8 as U (writeFile, putStrLn, print, readFile)
45import Text.Parsec (ParseError)
45import Text.Parsec (ParseError)
46
46
47import qualified Language.PureScript as P
47import qualified Language.PureScript as P
48−import qualified Language.PureScript.Declarations as D
48+import qualified Language.PureScript.AST as D
49import qualified Language.PureScript.Names as N
49import qualified Language.PureScript.Names as N
50import qualified Paths_purescript as Paths
50import qualified Paths_purescript as Paths
51
51
purescript.cabal+6−1
@@ -39,6 +39,12 @@ library
39 file-embed >= 0.0.7 && < 0.0.8,
39 file-embed >= 0.0.7 && < 0.0.8,
40 time -any
40 time -any
41 exposed-modules: Language.PureScript
41 exposed-modules: Language.PureScript
42+ Language.PureScript.AST
43+ Language.PureScript.AST.Binders
44+ Language.PureScript.AST.Declarations
45+ Language.PureScript.AST.Operators
46+ Language.PureScript.AST.SourcePos
47+ Language.PureScript.AST.Traversals
42 Language.PureScript.CodeGen
48 Language.PureScript.CodeGen
43 Language.PureScript.CodeGen.Common
49 Language.PureScript.CodeGen.Common
44 Language.PureScript.CodeGen.Externs
50 Language.PureScript.CodeGen.Externs
@@ -46,7 +52,6 @@ library
46 Language.PureScript.CodeGen.JS.AST
52 Language.PureScript.CodeGen.JS.AST
47 Language.PureScript.Constants
53 Language.PureScript.Constants
48 Language.PureScript.DeadCodeElimination
54 Language.PureScript.DeadCodeElimination
49− Language.PureScript.Declarations
50 Language.PureScript.Environment
55 Language.PureScript.Environment
51 Language.PureScript.Errors
56 Language.PureScript.Errors
52 Language.PureScript.Kinds
57 Language.PureScript.Kinds
src/Language/PureScript.hs+3−3
@@ -19,7 +19,7 @@ module Language.PureScript (module P, compile, compile', RebuildPolicy(..), Mona
19
19
20import Language.PureScript.Types as P
20import Language.PureScript.Types as P
21import Language.PureScript.Kinds as P
21import Language.PureScript.Kinds as P
22−import Language.PureScript.Declarations as P
22+import Language.PureScript.AST as P
23import Language.PureScript.Names as P
23import Language.PureScript.Names as P
24import Language.PureScript.Parser as P
24import Language.PureScript.Parser as P
25import Language.PureScript.CodeGen as P
25import Language.PureScript.CodeGen as P
@@ -139,7 +139,7 @@ class MonadMake m where
139data RebuildPolicy
139data RebuildPolicy
140 -- | Never rebuild this module
140 -- | Never rebuild this module
141 = RebuildNever
141 = RebuildNever
142− -- | Always rebuild this module
142+ -- | Always rebuild this module
143 | RebuildAlways deriving (Show, Eq, Ord)
143 | RebuildAlways deriving (Show, Eq, Ord)
144
144
145-- |
145-- |
@@ -163,7 +163,7 @@ make outputDir opts ms prefix = do
163
163
164 jsTimestamp <- getTimestamp jsFile
164 jsTimestamp <- getTimestamp jsFile
165 externsTimestamp <- getTimestamp externsFile
165 externsTimestamp <- getTimestamp externsFile
166− inputTimestamp <- traverse getTimestamp inputFile
166+ inputTimestamp <- traverse getTimestamp inputFile
167
167
168 return $ case (inputTimestamp, jsTimestamp, externsTimestamp) of
168 return $ case (inputTimestamp, jsTimestamp, externsTimestamp) of
169 (Right (Just t1), Just t2, Just t3) | t1 < min t2 t3 -> s
169 (Right (Just t1), Just t2, Just t3) | t1 < min t2 t3 -> s
src/Language/PureScript/AST.hsnew file+23
@@ -0,0 +1,23 @@
1+-----------------------------------------------------------------------------
2+--
3+-- Module : Language.PureScript.AST
4+-- Copyright : (c) 2013-14 Phil Freeman, (c) 2014 Gary Burgess, and other contributors
5+-- License : MIT
6+--
7+-- Maintainer : Phil Freeman <paf31@cantab.net>
8+-- Stability : experimental
9+-- Portability :
10+--
11+-- | The initial PureScript AST
12+--
13+-----------------------------------------------------------------------------
14+
15+module Language.PureScript.AST (
16+ module AST
17+) where
18+
19+import Language.PureScript.AST.Binders as AST
20+import Language.PureScript.AST.Declarations as AST
21+import Language.PureScript.AST.Operators as AST
22+import Language.PureScript.AST.SourcePos as AST
23+import Language.PureScript.AST.Traversals as AST
src/Language/PureScript/AST/Binders.hsnew file+86
@@ -0,0 +1,86 @@
1+-----------------------------------------------------------------------------
2+--
3+-- Module : Language.PureScript.AST.Binders
4+-- Copyright : (c) 2013-14 Phil Freeman, (c) 2014 Gary Burgess, and other contributors
5+-- License : MIT
6+--
7+-- Maintainer : Phil Freeman <paf31@cantab.net>
8+-- Stability : experimental
9+-- Portability :
10+--
11+-- | Case binders
12+--
13+-----------------------------------------------------------------------------
14+
15+{-# LANGUAGE DeriveDataTypeable #-}
16+
17+module Language.PureScript.AST.Binders where
18+
19+import qualified Data.Data as D
20+
21+import Language.PureScript.AST.SourcePos
22+import Language.PureScript.Names
23+
24+-- |
25+-- Data type for binders
26+--
27+data Binder
28+ -- |
29+ -- Wildcard binder
30+ --
31+ = NullBinder
32+ -- |
33+ -- A binder which matches a boolean literal
34+ --
35+ | BooleanBinder Bool
36+ -- |
37+ -- A binder which matches a string literal
38+ --
39+ | StringBinder String
40+ -- |
41+ -- A binder which matches a numeric literal
42+ --
43+ | NumberBinder (Either Integer Double)
44+ -- |
45+ -- A binder which binds an identifier
46+ --
47+ | VarBinder Ident
48+ -- |
49+ -- A binder which matches a data constructor
50+ --
51+ | ConstructorBinder (Qualified ProperName) [Binder]
52+ -- |
53+ -- A binder which matches a record and binds its properties
54+ --
55+ | ObjectBinder [(String, Binder)]
56+ -- |
57+ -- A binder which matches an array and binds its elements
58+ --
59+ | ArrayBinder [Binder]
60+ -- |
61+ -- A binder which matches an array and binds its head and tail
62+ --
63+ | ConsBinder Binder Binder
64+ -- |
65+ -- A binder which binds its input to an identifier
66+ --
67+ | NamedBinder Ident Binder
68+ -- |
69+ -- A binder with source position information
70+ --
71+ | PositionedBinder SourcePos Binder deriving (Show, D.Data, D.Typeable)
72+
73+-- |
74+-- Collect all names introduced in binders in an expression
75+--
76+binderNames :: Binder -> [Ident]
77+binderNames = go []
78+ where
79+ go ns (VarBinder name) = name : ns
80+ go ns (ConstructorBinder _ bs) = foldl go ns bs
81+ go ns (ObjectBinder bs) = foldl go ns (map snd bs)
82+ go ns (ArrayBinder bs) = foldl go ns bs
83+ go ns (ConsBinder b1 b2) = go (go ns b1) b2
84+ go ns (NamedBinder name b) = go (name : ns) b
85+ go ns (PositionedBinder _ b) = go ns b
86+ go ns _ = ns
src/Language/PureScript/AST/Declarations.hsnew file+362
@@ -0,0 +1,362 @@
1+-----------------------------------------------------------------------------
2+--
3+-- Module : Language.PureScript.AST.Declarations
4+-- Copyright : (c) 2013-14 Phil Freeman, (c) 2014 Gary Burgess, and other contributors
5+-- License : MIT
6+--
7+-- Maintainer : Phil Freeman <paf31@cantab.net>
8+-- Stability : experimental
9+-- Portability :
10+--
11+-- | Data types for modules and declarations
12+--
13+-----------------------------------------------------------------------------
14+
15+{-# LANGUAGE DeriveDataTypeable, ScopedTypeVariables #-}
16+
17+module Language.PureScript.AST.Declarations where
18+
19+import qualified Data.Data as D
20+
21+import Language.PureScript.AST.Binders
22+import Language.PureScript.AST.Operators
23+import Language.PureScript.AST.SourcePos
24+import Language.PureScript.Types
25+import Language.PureScript.Names
26+import Language.PureScript.Kinds
27+import Language.PureScript.TypeClassDictionaries
28+import Language.PureScript.CodeGen.JS.AST
29+import Language.PureScript.Environment
30+
31+-- |
32+-- A module declaration, consisting of a module name, a list of declarations, and a list of the
33+-- declarations that are explicitly exported. If the export list is Nothing, everything is exported.
34+--
35+data Module = Module ModuleName [Declaration] (Maybe [DeclarationRef]) deriving (Show, D.Data, D.Typeable)
36+
37+-- |
38+-- An item in a list of explicit imports or exports
39+--
40+data DeclarationRef
41+ -- |
42+ -- A type constructor with data constructors
43+ --
44+ = TypeRef ProperName (Maybe [ProperName])
45+ -- |
46+ -- A value
47+ --
48+ | ValueRef Ident
49+ -- |
50+ -- A type class
51+ --
52+ | TypeClassRef ProperName
53+ -- |
54+ -- A type class instance, created during typeclass desugaring (name, class name, instance types)
55+ --
56+ | TypeInstanceRef Ident
57+ -- |
58+ -- A declaration reference with source position information
59+ --
60+ | PositionedDeclarationRef SourcePos DeclarationRef
61+ deriving (Show, D.Data, D.Typeable)
62+
63+instance Eq DeclarationRef where
64+ (TypeRef name dctors) == (TypeRef name' dctors') = name == name' && dctors == dctors'
65+ (ValueRef name) == (ValueRef name') = name == name'
66+ (TypeClassRef name) == (TypeClassRef name') = name == name'
67+ (TypeInstanceRef name) == (TypeInstanceRef name') = name == name'
68+ (PositionedDeclarationRef _ r) == r' = r == r'
69+ r == (PositionedDeclarationRef _ r') = r == r'
70+ _ == _ = False
71+
72+-- |
73+-- The data type which specifies type of import declaration
74+--
75+data ImportDeclarationType
76+ -- |
77+ -- Unqualified import
78+ --
79+ = Unqualified
80+ -- |
81+ -- Qualified import with a list of references to import
82+ --
83+ | Qualifying [DeclarationRef]
84+ -- |
85+ -- Import with hiding clause with a list of references to hide
86+ --
87+ | Hiding [DeclarationRef]
88+ deriving (Show, D.Data, D.Typeable)
89+
90+-- |
91+-- The data type of declarations
92+--
93+data Declaration
94+ -- |
95+ -- A data type declaration (data or newtype, name, arguments, data constructors)
96+ --
97+ = DataDeclaration DataDeclType ProperName [(String, Maybe Kind)] [(ProperName, [Type])]
98+ -- |
99+ -- A minimal mutually recursive set of data type declarations
100+ --
101+ | DataBindingGroupDeclaration [Declaration]
102+ -- |
103+ -- A type synonym declaration (name, arguments, type)
104+ --
105+ | TypeSynonymDeclaration ProperName [(String, Maybe Kind)] Type
106+ -- |
107+ -- A type declaration for a value (name, ty)
108+ --
109+ | TypeDeclaration Ident Type
110+ -- |
111+ -- A value declaration (name, top-level binders, optional guard, value)
112+ --
113+ | ValueDeclaration Ident NameKind [Binder] (Either [(Guard, Expr)] Expr)
114+ -- |
115+ -- A minimal mutually recursive set of value declarations
116+ --
117+ | BindingGroupDeclaration [(Ident, NameKind, Expr)]
118+ -- |
119+ -- A foreign import declaration (type, name, optional inline Javascript, type)
120+ --
121+ | ExternDeclaration ForeignImportType Ident (Maybe JS) Type
122+ -- |
123+ -- A data type foreign import (name, kind)
124+ --
125+ | ExternDataDeclaration ProperName Kind
126+ -- |
127+ -- A type class instance foreign import
128+ --
129+ | ExternInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type]
130+ -- |
131+ -- A fixity declaration (fixity data, operator name)
132+ --
133+ | FixityDeclaration Fixity String
134+ -- |
135+ -- A module import (module name, qualified/unqualified/hiding, optional "qualified as" name)
136+ --
137+ | ImportDeclaration ModuleName ImportDeclarationType (Maybe ModuleName)
138+ -- |
139+ -- A type class declaration (name, argument, implies, member declarations)
140+ --
141+ | TypeClassDeclaration ProperName [(String, Maybe Kind)] [(Qualified ProperName, [Type])] [Declaration]
142+ -- |
143+ -- A type instance declaration (name, dependencies, class name, instance types, member
144+ -- declarations)
145+ --
146+ | TypeInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type] [Declaration]
147+ -- |
148+ -- A declaration with source position information
149+ --
150+ | PositionedDeclaration SourcePos Declaration
151+ deriving (Show, D.Data, D.Typeable)
152+
153+-- |
154+-- Test if a declaration is a value declaration
155+--
156+isValueDecl :: Declaration -> Bool
157+isValueDecl ValueDeclaration{} = True
158+isValueDecl (PositionedDeclaration _ d) = isValueDecl d
159+isValueDecl _ = False
160+
161+-- |
162+-- Test if a declaration is a data type or type synonym declaration
163+--
164+isDataDecl :: Declaration -> Bool
165+isDataDecl DataDeclaration{} = True
166+isDataDecl TypeSynonymDeclaration{} = True
167+isDataDecl (PositionedDeclaration _ d) = isDataDecl d
168+isDataDecl _ = False
169+
170+-- |
171+-- Test if a declaration is a module import
172+--
173+isImportDecl :: Declaration -> Bool
174+isImportDecl ImportDeclaration{} = True
175+isImportDecl (PositionedDeclaration _ d) = isImportDecl d
176+isImportDecl _ = False
177+
178+-- |
179+-- Test if a declaration is a data type foreign import
180+--
181+isExternDataDecl :: Declaration -> Bool
182+isExternDataDecl ExternDataDeclaration{} = True
183+isExternDataDecl (PositionedDeclaration _ d) = isExternDataDecl d
184+isExternDataDecl _ = False
185+
186+-- |
187+-- Test if a declaration is a type class instance foreign import
188+--
189+isExternInstanceDecl :: Declaration -> Bool
190+isExternInstanceDecl ExternInstanceDeclaration{} = True
191+isExternInstanceDecl (PositionedDeclaration _ d) = isExternInstanceDecl d
192+isExternInstanceDecl _ = False
193+
194+-- |
195+-- Test if a declaration is a fixity declaration
196+--
197+isFixityDecl :: Declaration -> Bool
198+isFixityDecl FixityDeclaration{} = True
199+isFixityDecl (PositionedDeclaration _ d) = isFixityDecl d
200+isFixityDecl _ = False
201+
202+-- |
203+-- Test if a declaration is a foreign import
204+--
205+isExternDecl :: Declaration -> Bool
206+isExternDecl ExternDeclaration{} = True
207+isExternDecl (PositionedDeclaration _ d) = isExternDecl d
208+isExternDecl _ = False
209+
210+-- |
211+-- Test if a declaration is a type class or instance declaration
212+--
213+isTypeClassDeclaration :: Declaration -> Bool
214+isTypeClassDeclaration TypeClassDeclaration{} = True
215+isTypeClassDeclaration TypeInstanceDeclaration{} = True
216+isTypeClassDeclaration (PositionedDeclaration _ d) = isTypeClassDeclaration d
217+isTypeClassDeclaration _ = False
218+
219+-- |
220+-- A guard is just a boolean-valued expression that appears alongside a set of binders
221+--
222+type Guard = Expr
223+
224+-- |
225+-- Data type for expressions and terms
226+--
227+data Expr
228+ -- |
229+ -- A numeric literal
230+ --
231+ = NumericLiteral (Either Integer Double)
232+ -- |
233+ -- A string literal
234+ --
235+ | StringLiteral String
236+ -- |
237+ -- A boolean literal
238+ --
239+ | BooleanLiteral Bool
240+ -- |
241+ -- A prefix -, will be desugared
242+ --
243+ | UnaryMinus Expr
244+ -- |
245+ -- Binary operator application. During the rebracketing phase of desugaring, this data constructor
246+ -- will be removed.
247+ --
248+ | BinaryNoParens (Qualified Ident) Expr Expr
249+ -- |
250+ -- Explicit parentheses. During the rebracketing phase of desugaring, this data constructor
251+ -- will be removed.
252+ --
253+ | Parens Expr
254+ -- |
255+ -- An array literal
256+ --
257+ | ArrayLiteral [Expr]
258+ -- |
259+ -- An object literal
260+ --
261+ | ObjectLiteral [(String, Expr)]
262+ -- |
263+ -- An record property accessor expression
264+ --
265+ | Accessor String Expr
266+ -- |
267+ -- Partial record update
268+ --
269+ | ObjectUpdate Expr [(String, Expr)]
270+ -- |
271+ -- Function introduction
272+ --
273+ | Abs (Either Ident Binder) Expr
274+ -- |
275+ -- Function application
276+ --
277+ | App Expr Expr
278+ -- |
279+ -- Variable
280+ --
281+ | Var (Qualified Ident)
282+ -- |
283+ -- Conditional (if-then-else expression)
284+ --
285+ | IfThenElse Expr Expr Expr
286+ -- |
287+ -- A data constructor
288+ --
289+ | Constructor (Qualified ProperName)
290+ -- |
291+ -- A case expression. During the case expansion phase of desugaring, top-level binders will get
292+ -- desugared into case expressions, hence the need for guards and multiple binders per branch here.
293+ --
294+ | Case [Expr] [CaseAlternative]
295+ -- |
296+ -- A value with a type annotation
297+ --
298+ | TypedValue Bool Expr Type
299+ -- |
300+ -- A let binding
301+ --
302+ | Let [Declaration] Expr
303+ -- |
304+ -- A do-notation block
305+ --
306+ | Do [DoNotationElement]
307+ -- |
308+ -- An application of a typeclass dictionary constructor. The value should be
309+ -- an ObjectLiteral.
310+ --
311+ | TypeClassDictionaryConstructorApp (Qualified ProperName) Expr
312+ -- |
313+ -- A placeholder for a type class dictionary to be inserted later. At the end of type checking, these
314+ -- placeholders will be replaced with actual expressions representing type classes dictionaries which
315+ -- can be evaluated at runtime. The constructor arguments represent (in order): whether or not to look
316+ -- at superclass implementations when searching for a dictionary, the type class name and
317+ -- instance type, and the type class dictionaries in scope.
318+ --
319+ | TypeClassDictionary Bool (Qualified ProperName, [Type]) [TypeClassDictionaryInScope]
320+ -- |
321+ -- A placeholder for a superclass dictionary to be turned into a TypeClassDictionary during typechecking
322+ --
323+ | SuperClassDictionary (Qualified ProperName) [Type]
324+ -- |
325+ -- A value with source position information
326+ --
327+ | PositionedValue SourcePos Expr deriving (Show, D.Data, D.Typeable)
328+
329+-- |
330+-- An alternative in a case statement
331+--
332+data CaseAlternative = CaseAlternative
333+ { -- |
334+ -- A collection of binders with which to match the inputs
335+ --
336+ caseAlternativeBinders :: [Binder]
337+ -- |
338+ -- The result expression or a collect of guarded expressions
339+ --
340+ , caseAlternativeResult :: Either [(Guard, Expr)] Expr
341+ } deriving (Show, D.Data, D.Typeable)
342+
343+-- |
344+-- A statement in a do-notation block
345+--
346+data DoNotationElement
347+ -- |
348+ -- A monadic value without a binder
349+ --
350+ = DoNotationValue Expr
351+ -- |
352+ -- A monadic value with a binder
353+ --
354+ | DoNotationBind Binder Expr
355+ -- |
356+ -- A let statement, i.e. a pure value with a binder
357+ --
358+ | DoNotationLet [Declaration]
359+ -- |
360+ -- A do notation element with source position information
361+ --
362+ | PositionedDoNotationElement SourcePos DoNotationElement deriving (Show, D.Data, D.Typeable)
src/Language/PureScript/AST/Operators.hsnew file+39
@@ -0,0 +1,39 @@
1+-----------------------------------------------------------------------------
2+--
3+-- Module : Language.PureScript.AST.Operators
4+-- Copyright : (c) 2013-14 Phil Freeman, (c) 2014 Gary Burgess, and other contributors
5+-- License : MIT
6+--
7+-- Maintainer : Phil Freeman <paf31@cantab.net>
8+-- Stability : experimental
9+-- Portability :
10+--
11+-- | Operators fixity and associativity
12+--
13+-----------------------------------------------------------------------------
14+
15+{-# LANGUAGE DeriveDataTypeable #-}
16+
17+module Language.PureScript.AST.Operators where
18+
19+import qualified Data.Data as D
20+
21+-- |
22+-- A precedence level for an infix operator
23+--
24+type Precedence = Integer
25+
26+-- |
27+-- Associativity for infix operators
28+--
29+data Associativity = Infixl | Infixr | Infix deriving (D.Data, D.Typeable)
30+
31+instance Show Associativity where
32+ show Infixl = "infixl"
33+ show Infixr = "infixr"
34+ show Infix = "infix"
35+
36+-- |
37+-- Fixity data for infix operators
38+--
39+data Fixity = Fixity Associativity Precedence deriving (Show, D.Data, D.Typeable)
src/Language/PureScript/AST/SourcePos.hsnew file+40
@@ -0,0 +1,40 @@
1+-----------------------------------------------------------------------------
2+--
3+-- Module : Language.PureScript.AST.SourcePos
4+-- Copyright : (c) 2013-14 Phil Freeman, (c) 2014 Gary Burgess, and other contributors
5+-- License : MIT
6+--
7+-- Maintainer : Phil Freeman <paf31@cantab.net>
8+-- Stability : experimental
9+-- Portability :
10+--
11+-- | Source position information
12+--
13+-----------------------------------------------------------------------------
14+
15+{-# LANGUAGE DeriveDataTypeable, ScopedTypeVariables #-}
16+
17+module Language.PureScript.AST.SourcePos where
18+
19+import qualified Data.Data as D
20+
21+-- |
22+-- Source position information
23+--
24+data SourcePos = SourcePos
25+ { -- |
26+ -- Source name
27+ --
28+ sourceName :: String
29+ -- |
30+ -- Line number
31+ --
32+ , sourcePosLine :: Int
33+ -- |
34+ -- Column number
35+ --
36+ , sourcePosColumn :: Int
37+ } deriving (D.Data, D.Typeable)
38+
39+instance Show SourcePos where
40+ show sp = sourceName sp ++ " line " ++ show (sourcePosLine sp) ++ ", column " ++ show (sourcePosColumn sp)
src/Language/PureScript/Declarations.hs → src/Language/PureScript/AST/Traversals.hsrenamed+7−465
@@ -1,486 +1,30 @@
1-----------------------------------------------------------------------------
1-----------------------------------------------------------------------------
2--
2--
3−-- Module : Language.PureScript.Declarations
3+-- Module : Language.PureScript.AST.Traversals
4−-- Copyright : (c) Phil Freeman 2013
4+-- Copyright : (c) 2013-14 Phil Freeman, (c) 2014 Gary Burgess, and other contributors
5-- License : MIT
5-- License : MIT
6--
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
8-- Stability : experimental
9-- Portability :
9-- Portability :
10--
10--
11−-- | Data types for modules and declarations
11+-- | AST traversal helpers
12--
12--
13-----------------------------------------------------------------------------
13-----------------------------------------------------------------------------
14
14
15−{-# LANGUAGE DeriveDataTypeable, ScopedTypeVariables #-}
16−
17−module Language.PureScript.Declarations where
15+module Language.PureScript.AST.Traversals where
18
16
19import Data.Monoid (Monoid(..), mconcat)
17import Data.Monoid (Monoid(..), mconcat)
20
18
21−import qualified Data.Data as D
22−
23import Control.Applicative
19import Control.Applicative
24import Control.Monad
20import Control.Monad
25import Control.Arrow ((***), (+++))
21import Control.Arrow ((***), (+++))
26
22
23+import Language.PureScript.AST.Binders
24+import Language.PureScript.AST.Declarations
27import Language.PureScript.Types
25import Language.PureScript.Types
28−import Language.PureScript.Names
29−import Language.PureScript.Kinds
30−import Language.PureScript.TypeClassDictionaries
31−import Language.PureScript.CodeGen.JS.AST
32−import Language.PureScript.Environment
33import Language.PureScript.Traversals
26import Language.PureScript.Traversals
34
27
35−-- |
36−-- A precedence level for an infix operator
37−--
38−type Precedence = Integer
39−
40−-- |
41−-- Associativity for infix operators
42−--
43−data Associativity = Infixl | Infixr | Infix deriving (D.Data, D.Typeable)
44−
45−instance Show Associativity where
46− show Infixl = "infixl"
47− show Infixr = "infixr"
48− show Infix = "infix"
49−
50−-- |
51−-- Source position information
52−--
53−data SourcePos = SourcePos
54− { -- |
55− -- Source name
56− --
57− sourceName :: String
58− -- |
59− -- Line number
60− --
61− , sourcePosLine :: Int
62− -- |
63− -- Column number
64− --
65− , sourcePosColumn :: Int
66− } deriving (D.Data, D.Typeable)
67−
68−instance Show SourcePos where
69− show sp = (sourceName sp) ++ " line " ++ show (sourcePosLine sp) ++ ", column " ++ show (sourcePosColumn sp)
70−
71−-- |
72−-- Fixity data for infix operators
73−--
74−data Fixity = Fixity Associativity Precedence deriving (Show, D.Data, D.Typeable)
75−
76−-- |
77−-- A module declaration, consisting of a module name, a list of declarations, and a list of the
78−-- declarations that are explicitly exported. If the export list is Nothing, everything is exported.
79−--
80−data Module = Module ModuleName [Declaration] (Maybe [DeclarationRef]) deriving (Show, D.Data, D.Typeable)
81−
82−-- |
83−-- An item in a list of explicit imports or exports
84−--
85−data DeclarationRef
86− -- |
87− -- A type constructor with data constructors
88− --
89− = TypeRef ProperName (Maybe [ProperName])
90− -- |
91− -- A value
92− --
93− | ValueRef Ident
94− -- |
95− -- A type class
96− --
97− | TypeClassRef ProperName
98− -- |
99− -- A type class instance, created during typeclass desugaring (name, class name, instance types)
100− --
101− | TypeInstanceRef Ident
102− -- |
103− -- A declaration reference with source position information
104− --
105− | PositionedDeclarationRef SourcePos DeclarationRef
106− deriving (Show, D.Data, D.Typeable)
107−
108−instance Eq DeclarationRef where
109− (TypeRef name dctors) == (TypeRef name' dctors') = name == name' && dctors == dctors'
110− (ValueRef name) == (ValueRef name') = name == name'
111− (TypeClassRef name) == (TypeClassRef name') = name == name'
112− (TypeInstanceRef name) == (TypeInstanceRef name') = name == name'
113− (PositionedDeclarationRef _ r) == r' = r == r'
114− r == (PositionedDeclarationRef _ r') = r == r'
115− _ == _ = False
116−
117−-- |
118−-- The data type which specifies type of import declaration
119−--
120−data ImportDeclarationType
121− -- |
122− -- Unqualified import
123− --
124− = Unqualified
125− -- |
126− -- Qualified import with a list of references to import
127− --
128− | Qualifying [DeclarationRef]
129− -- |
130− -- Import with hiding clause with a list of references to hide
131− --
132− | Hiding [DeclarationRef]
133− deriving (Show, D.Data, D.Typeable)
134−
135−-- |
136−-- The data type of declarations
137−--
138−data Declaration
139− -- |
140− -- A data type declaration (data or newtype, name, arguments, data constructors)
141− --
142− = DataDeclaration DataDeclType ProperName [(String, Maybe Kind)] [(ProperName, [Type])]
143− -- |
144− -- A minimal mutually recursive set of data type declarations
145− --
146− | DataBindingGroupDeclaration [Declaration]
147− -- |
148− -- A type synonym declaration (name, arguments, type)
149− --
150− | TypeSynonymDeclaration ProperName [(String, Maybe Kind)] Type
151− -- |
152− -- A type declaration for a value (name, ty)
153− --
154− | TypeDeclaration Ident Type
155− -- |
156− -- A value declaration (name, top-level binders, optional guard, value)
157− --
158− | ValueDeclaration Ident NameKind [Binder] (Either [(Guard, Expr)] Expr)
159− -- |
160− -- A minimal mutually recursive set of value declarations
161− --
162− | BindingGroupDeclaration [(Ident, NameKind, Expr)]
163− -- |
164− -- A foreign import declaration (type, name, optional inline Javascript, type)
165− --
166− | ExternDeclaration ForeignImportType Ident (Maybe JS) Type
167− -- |
168− -- A data type foreign import (name, kind)
169− --
170− | ExternDataDeclaration ProperName Kind
171− -- |
172− -- A type class instance foreign import
173− --
174− | ExternInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type]
175− -- |
176− -- A fixity declaration (fixity data, operator name)
177− --
178− | FixityDeclaration Fixity String
179− -- |
180− -- A module import (module name, qualified/unqualified/hiding, optional "qualified as" name)
181− --
182− | ImportDeclaration ModuleName ImportDeclarationType (Maybe ModuleName)
183− -- |
184− -- A type class declaration (name, argument, implies, member declarations)
185− --
186− | TypeClassDeclaration ProperName [(String, Maybe Kind)] [(Qualified ProperName, [Type])] [Declaration]
187− -- |
188− -- A type instance declaration (name, dependencies, class name, instance types, member
189− -- declarations)
190− --
191− | TypeInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type] [Declaration]
192− -- |
193− -- A declaration with source position information
194− --
195− | PositionedDeclaration SourcePos Declaration
196− deriving (Show, D.Data, D.Typeable)
197−
198−-- |
199−-- Test if a declaration is a value declaration
200−--
201−isValueDecl :: Declaration -> Bool
202−isValueDecl ValueDeclaration{} = True
203−isValueDecl (PositionedDeclaration _ d) = isValueDecl d
204−isValueDecl _ = False
205−
206−-- |
207−-- Test if a declaration is a data type or type synonym declaration
208−--
209−isDataDecl :: Declaration -> Bool
210−isDataDecl DataDeclaration{} = True
211−isDataDecl TypeSynonymDeclaration{} = True
212−isDataDecl (PositionedDeclaration _ d) = isDataDecl d
213−isDataDecl _ = False
214−
215−-- |
216−-- Test if a declaration is a module import
217−--
218−isImportDecl :: Declaration -> Bool
219−isImportDecl ImportDeclaration{} = True
220−isImportDecl (PositionedDeclaration _ d) = isImportDecl d
221−isImportDecl _ = False
222−
223−-- |
224−-- Test if a declaration is a data type foreign import
225−--
226−isExternDataDecl :: Declaration -> Bool
227−isExternDataDecl ExternDataDeclaration{} = True
228−isExternDataDecl (PositionedDeclaration _ d) = isExternDataDecl d
229−isExternDataDecl _ = False
230−
231−-- |
232−-- Test if a declaration is a type class instance foreign import
233−--
234−isExternInstanceDecl :: Declaration -> Bool
235−isExternInstanceDecl ExternInstanceDeclaration{} = True
236−isExternInstanceDecl (PositionedDeclaration _ d) = isExternInstanceDecl d
237−isExternInstanceDecl _ = False
238−
239−-- |
240−-- Test if a declaration is a fixity declaration
241−--
242−isFixityDecl :: Declaration -> Bool
243−isFixityDecl FixityDeclaration{} = True
244−isFixityDecl (PositionedDeclaration _ d) = isFixityDecl d
245−isFixityDecl _ = False
246−
247−-- |
248−-- Test if a declaration is a foreign import
249−--
250−isExternDecl :: Declaration -> Bool
251−isExternDecl ExternDeclaration{} = True
252−isExternDecl (PositionedDeclaration _ d) = isExternDecl d
253−isExternDecl _ = False
254−
255−-- |
256−-- Test if a declaration is a type class or instance declaration
257−--
258−isTypeClassDeclaration :: Declaration -> Bool
259−isTypeClassDeclaration TypeClassDeclaration{} = True
260−isTypeClassDeclaration TypeInstanceDeclaration{} = True
261−isTypeClassDeclaration (PositionedDeclaration _ d) = isTypeClassDeclaration d
262−isTypeClassDeclaration _ = False
263−
264−-- |
265−-- A guard is just a boolean-valued expression that appears alongside a set of binders
266−--
267−type Guard = Expr
268−
269−-- |
270−-- Data type for expressions and terms
271−--
272−data Expr
273− -- |
274− -- A numeric literal
275− --
276− = NumericLiteral (Either Integer Double)
277− -- |
278− -- A string literal
279− --
280− | StringLiteral String
281− -- |
282− -- A boolean literal
283− --
284− | BooleanLiteral Bool
285− -- |
286− -- A prefix -, will be desugared
287− --
288− | UnaryMinus Expr
289− -- |
290− -- Binary operator application. During the rebracketing phase of desugaring, this data constructor
291− -- will be removed.
292− --
293− | BinaryNoParens (Qualified Ident) Expr Expr
294− -- |
295− -- Explicit parentheses. During the rebracketing phase of desugaring, this data constructor
296− -- will be removed.
297− --
298− | Parens Expr
299− -- |
300− -- An array literal
301− --
302− | ArrayLiteral [Expr]
303− -- |
304− -- An object literal
305− --
306− | ObjectLiteral [(String, Expr)]
307− -- |
308− -- An record property accessor expression
309− --
310− | Accessor String Expr
311− -- |
312− -- Partial record update
313− --
314− | ObjectUpdate Expr [(String, Expr)]
315− -- |
316− -- Function introduction
317− --
318− | Abs (Either Ident Binder) Expr
319− -- |
320− -- Function application
321− --
322− | App Expr Expr
323− -- |
324− -- Variable
325− --
326− | Var (Qualified Ident)
327− -- |
328− -- Conditional (if-then-else expression)
329− --
330− | IfThenElse Expr Expr Expr
331− -- |
332− -- A data constructor
333− --
334− | Constructor (Qualified ProperName)
335− -- |
336− -- A case expression. During the case expansion phase of desugaring, top-level binders will get
337− -- desugared into case expressions, hence the need for guards and multiple binders per branch here.
338− --
339− | Case [Expr] [CaseAlternative]
340− -- |
341− -- A value with a type annotation
342− --
343− | TypedValue Bool Expr Type
344− -- |
345− -- A let binding
346− --
347− | Let [Declaration] Expr
348− -- |
349− -- A do-notation block
350− --
351− | Do [DoNotationElement]
352− -- |
353− -- An application of a typeclass dictionary constructor. The value should be
354− -- an ObjectLiteral.
355− --
356− | TypeClassDictionaryConstructorApp (Qualified ProperName) Expr
357− -- |
358− -- A placeholder for a type class dictionary to be inserted later. At the end of type checking, these
359− -- placeholders will be replaced with actual expressions representing type classes dictionaries which
360− -- can be evaluated at runtime. The constructor arguments represent (in order): whether or not to look
361− -- at superclass implementations when searching for a dictionary, the type class name and
362− -- instance type, and the type class dictionaries in scope.
363− --
364− | TypeClassDictionary Bool (Qualified ProperName, [Type]) [TypeClassDictionaryInScope]
365− -- |
366− -- A placeholder for a superclass dictionary to be turned into a TypeClassDictionary during typechecking
367− --
368− | SuperClassDictionary (Qualified ProperName) [Type]
369− -- |
370− -- A value with source position information
371− --
372− | PositionedValue SourcePos Expr deriving (Show, D.Data, D.Typeable)
373−
374−-- |
375−-- An alternative in a case statement
376−--
377−data CaseAlternative = CaseAlternative
378− { -- |
379− -- A collection of binders with which to match the inputs
380− --
381− caseAlternativeBinders :: [Binder]
382− -- |
383− -- The result expression or a collect of guarded expressions
384− --
385− , caseAlternativeResult :: Either [(Guard, Expr)] Expr
386− } deriving (Show, D.Data, D.Typeable)
387−
388−-- |
389−-- Find the original dictionary which a type class dictionary in scope refers to
390−--
391−canonicalizeDictionary :: TypeClassDictionaryInScope -> Qualified Ident
392−canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDRegular, tcdName = nm }) = nm
393−canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDAlias nm }) = nm
394−
395−-- |
396−-- A statement in a do-notation block
397−--
398−data DoNotationElement
399− -- |
400− -- A monadic value without a binder
401− --
402− = DoNotationValue Expr
403− -- |
404− -- A monadic value with a binder
405− --
406− | DoNotationBind Binder Expr
407− -- |
408− -- A let statement, i.e. a pure value with a binder
409− --
410− | DoNotationLet [Declaration]
411− -- |
412− -- A do notation element with source position information
413− --
414− | PositionedDoNotationElement SourcePos DoNotationElement deriving (Show, D.Data, D.Typeable)
415−
416−-- |
417−-- Data type for binders
418−--
419−data Binder
420− -- |
421− -- Wildcard binder
422− --
423− = NullBinder
424− -- |
425− -- A binder which matches a boolean literal
426− --
427− | BooleanBinder Bool
428− -- |
429− -- A binder which matches a string literal
430− --
431− | StringBinder String
432− -- |
433− -- A binder which matches a numeric literal
434− --
435− | NumberBinder (Either Integer Double)
436− -- |
437− -- A binder which binds an identifier
438− --
439− | VarBinder Ident
440− -- |
441− -- A binder which matches a data constructor
442− --
443− | ConstructorBinder (Qualified ProperName) [Binder]
444− -- |
445− -- A binder which matches a record and binds its properties
446− --
447− | ObjectBinder [(String, Binder)]
448− -- |
449− -- A binder which matches an array and binds its elements
450− --
451− | ArrayBinder [Binder]
452− -- |
453− -- A binder which matches an array and binds its head and tail
454− --
455− | ConsBinder Binder Binder
456− -- |
457− -- A binder which binds its input to an identifier
458− --
459− | NamedBinder Ident Binder
460− -- |
461− -- A binder with source position information
462− --
463− | PositionedBinder SourcePos Binder deriving (Show, D.Data, D.Typeable)
464−
465−-- |
466−-- Collect all names introduced in binders in an expression
467−--
468−binderNames :: Binder -> [Ident]
469−binderNames = go []
470− where
471− go ns (VarBinder name) = name : ns
472− go ns (ConstructorBinder _ bs) = foldl go ns bs
473− go ns (ObjectBinder bs) = foldl go ns (map snd bs)
474− go ns (ArrayBinder bs) = foldl go ns bs
475− go ns (ConsBinder b1 b2) = go (go ns b1) b2
476− go ns (NamedBinder name b) = go (name : ns) b
477− go ns (PositionedBinder _ b) = go ns b
478− go ns _ = ns
479−
480−--
481−-- Traversals
482−--
483−
484everywhereOnValues :: (Declaration -> Declaration) ->
28everywhereOnValues :: (Declaration -> Declaration) ->
485 (Expr -> Expr) ->
29 (Expr -> Expr) ->
486 (Binder -> Binder) ->
30 (Binder -> Binder) ->
@@ -545,7 +89,7 @@ everywhereOnValuesTopDownM :: (Functor m, Applicative m, Monad m) =>
545everywhereOnValuesTopDownM f g h = (f' <=< f, g' <=< g, h' <=< h)
89everywhereOnValuesTopDownM f g h = (f' <=< f, g' <=< g, h' <=< h)
546 where
90 where
547 f' (DataBindingGroupDeclaration ds) = DataBindingGroupDeclaration <$> mapM (f' <=< f) ds
91 f' (DataBindingGroupDeclaration ds) = DataBindingGroupDeclaration <$> mapM (f' <=< f) ds
548− f' (ValueDeclaration name nameKind bs val) = ValueDeclaration name nameKind <$> mapM (h' <=< h) bs <*> eitherM (mapM (pairM (g' <=< g) (g' <=< g))) (g' <=< g) val
92+ f' (ValueDeclaration name nameKind bs val) = ValueDeclaration name nameKind <$> mapM (h' <=< h) bs <*> eitherM (mapM (pairM (g' <=< g) (g' <=< g))) (g' <=< g) val
549 f' (BindingGroupDeclaration ds) = BindingGroupDeclaration <$> mapM (\(name, nameKind, val) -> (,,) name nameKind <$> (g val >>= g')) ds
93 f' (BindingGroupDeclaration ds) = BindingGroupDeclaration <$> mapM (\(name, nameKind, val) -> (,,) name nameKind <$> (g val >>= g')) ds
550 f' (TypeClassDeclaration name args implies ds) = TypeClassDeclaration name args implies <$> mapM (f' <=< f) ds
94 f' (TypeClassDeclaration name args implies ds) = TypeClassDeclaration name args implies <$> mapM (f' <=< f) ds
551 f' (TypeInstanceDeclaration name cs className args ds) = TypeInstanceDeclaration name cs className args <$> mapM (f' <=< f) ds
95 f' (TypeInstanceDeclaration name cs className args ds) = TypeInstanceDeclaration name cs className args <$> mapM (f' <=< f) ds
@@ -837,5 +381,3 @@ accumTypes f = everythingOnValues mappend forDecls forValues (const mempty) (con
837 forValues (SuperClassDictionary _ tys) = mconcat (map f tys)
381 forValues (SuperClassDictionary _ tys) = mconcat (map f tys)
838 forValues (TypedValue _ _ ty) = f ty
382 forValues (TypedValue _ _ ty) = f ty
839 forValues _ = mempty
383 forValues _ = mempty
840−
841−
src/Language/PureScript/CodeGen/Externs.hs+3−3
@@ -26,7 +26,7 @@ import Control.Monad.Writer
26
26
27import Language.PureScript.CodeGen.Common
27import Language.PureScript.CodeGen.Common
28import Language.PureScript.TypeClassDictionaries
28import Language.PureScript.TypeClassDictionaries
29−import Language.PureScript.Declarations
29+import Language.PureScript.AST
30import Language.PureScript.Pretty
30import Language.PureScript.Pretty
31import Language.PureScript.Names
31import Language.PureScript.Names
32import Language.PureScript.Kinds
32import Language.PureScript.Kinds
@@ -87,8 +87,8 @@ moduleToPs (Module moduleName ds (Just exts)) env = intercalate "\n" . execWrite
87 case Qualified (Just moduleName) className `M.lookup` typeClasses env of
87 case Qualified (Just moduleName) className `M.lookup` typeClasses env of
88 Nothing -> error $ show className ++ " has no type class definition in exportToPs"
88 Nothing -> error $ show className ++ " has no type class definition in exportToPs"
89 Just (args, members, implies) -> do
89 Just (args, members, implies) -> do
90− let impliesString = if null implies
90+ let impliesString = if null implies
91− then ""
91+ then ""
92 else "(" ++ intercalate ", " (map (\(pn, tys') -> show pn ++ " " ++ unwords (map prettyPrintTypeAtom tys')) implies) ++ ") <= "
92 else "(" ++ intercalate ", " (map (\(pn, tys') -> show pn ++ " " ++ unwords (map prettyPrintTypeAtom tys')) implies) ++ ") <= "
93 typeName = prettyPrintType $ foldl TypeApp (TypeConstructor (Qualified Nothing className)) (map toTypeVar args)
93 typeName = prettyPrintType $ foldl TypeApp (TypeConstructor (Qualified Nothing className)) (map toTypeVar args)
94 tell ["class " ++ impliesString ++ typeName ++ " where"]
94 tell ["class " ++ impliesString ++ typeName ++ " where"]
src/Language/PureScript/CodeGen/JS.hs+1−1
@@ -33,7 +33,7 @@ import Control.Applicative
33import Control.Arrow (second)
33import Control.Arrow (second)
34
34
35import Language.PureScript.Names
35import Language.PureScript.Names
36−import Language.PureScript.Declarations
36+import Language.PureScript.AST
37import Language.PureScript.Options
37import Language.PureScript.Options
38import Language.PureScript.CodeGen.JS.AST as AST
38import Language.PureScript.CodeGen.JS.AST as AST
39import Language.PureScript.Optimizer
39import Language.PureScript.Optimizer
src/Language/PureScript/DeadCodeElimination.hs+1−1
@@ -21,7 +21,7 @@ import Data.Graph
21import Data.Maybe (mapMaybe)
21import Data.Maybe (mapMaybe)
22
22
23import Language.PureScript.Names
23import Language.PureScript.Names
24−import Language.PureScript.Declarations
24+import Language.PureScript.AST
25
25
26-- |
26-- |
27-- Eliminate all declarations which are not a transitive dependency of the entry point module
27-- Eliminate all declarations which are not a transitive dependency of the entry point module
src/Language/PureScript/Errors.hs+1−1
@@ -23,7 +23,7 @@ import Data.Monoid
23import Control.Monad.Error
23import Control.Monad.Error
24import Control.Applicative ((<$>))
24import Control.Applicative ((<$>))
25
25
26−import Language.PureScript.Declarations
26+import Language.PureScript.AST
27import Language.PureScript.Pretty
27import Language.PureScript.Pretty
28import Language.PureScript.Types
28import Language.PureScript.Types
29
29
src/Language/PureScript/ModuleDependencies.hs+1−1
@@ -21,7 +21,7 @@ import Data.Graph
21import Data.List (nub)
21import Data.List (nub)
22import Data.Maybe (mapMaybe)
22import Data.Maybe (mapMaybe)
23
23
24−import Language.PureScript.Declarations
24+import Language.PureScript.AST
25import Language.PureScript.Names
25import Language.PureScript.Names
26import Language.PureScript.Types
26import Language.PureScript.Types
27
27
src/Language/PureScript/Parser/Declarations.hs+8−8
@@ -35,7 +35,7 @@ import Control.Arrow ((+++))
35import Language.PureScript.Kinds
35import Language.PureScript.Kinds
36import Language.PureScript.Parser.State
36import Language.PureScript.Parser.State
37import Language.PureScript.Parser.Common
37import Language.PureScript.Parser.Common
38−import Language.PureScript.Declarations
38+import Language.PureScript.AST
39import Language.PureScript.Parser.Types
39import Language.PureScript.Parser.Types
40import Language.PureScript.Parser.Kinds
40import Language.PureScript.Parser.Kinds
41import Language.PureScript.CodeGen.JS.AST
41import Language.PureScript.CodeGen.JS.AST
@@ -54,7 +54,7 @@ sourcePos = toSourcePos <$> P.getPosition
54 toSourcePos p = SourcePos (P.sourceName p) (P.sourceLine p) (P.sourceColumn p)
54 toSourcePos p = SourcePos (P.sourceName p) (P.sourceLine p) (P.sourceColumn p)
55
55
56kindedIdent :: P.Parsec String ParseState (String, Maybe Kind)
56kindedIdent :: P.Parsec String ParseState (String, Maybe Kind)
57−kindedIdent = (, Nothing) <$> identifier
57+kindedIdent = (, Nothing) <$> identifier
58 <|> parens ((,) <$> identifier <*> (Just <$> (indented *> lexeme (P.string "::") *> indented *> parseKind)))
58 <|> parens ((,) <$> identifier <*> (Just <$> (indented *> lexeme (P.string "::") *> indented *> parseKind)))
59
59
60parseDataDeclaration :: P.Parsec String ParseState Declaration
60parseDataDeclaration :: P.Parsec String ParseState Declaration
@@ -82,8 +82,8 @@ parseValueDeclaration :: P.Parsec String ParseState Declaration
82parseValueDeclaration = do
82parseValueDeclaration = do
83 name <- parseIdent
83 name <- parseIdent
84 binders <- P.many parseBinderNoParens
84 binders <- P.many parseBinderNoParens
85− value <- Left <$> (C.indented *>
85+ value <- Left <$> (C.indented *>
86− C.mark (P.many1 ((,) <$> (C.same *> parseGuard)
86+ C.mark (P.many1 ((,) <$> (C.same *> parseGuard)
87 <*> (lexeme (indented *> P.char '=') *> parseValueWithWhereClause)
87 <*> (lexeme (indented *> P.char '=') *> parseValueWithWhereClause)
88 )))
88 )))
89 <|> Right <$> (lexeme (indented *> P.char '=') *> parseValueWithWhereClause)
89 <|> Right <$> (lexeme (indented *> P.char '=') *> parseValueWithWhereClause)
@@ -241,10 +241,10 @@ parseModule = do
241 return $ Module name decls exports
241 return $ Module name decls exports
242
242
243-- |
243-- |
244−-- Parse a collection of modules
244+-- Parse a collection of modules
245--
245--
246parseModulesFromFiles :: (k -> String) -> [(k, String)] -> Either P.ParseError [(k, Module)]
246parseModulesFromFiles :: (k -> String) -> [(k, String)] -> Either P.ParseError [(k, Module)]
247−parseModulesFromFiles toFilePath input =
247+parseModulesFromFiles toFilePath input =
248 fmap collect . forM input $ \(filename, content) -> do
248 fmap collect . forM input $ \(filename, content) -> do
249 ms <- runIndentParser (toFilePath filename) parseModules content
249 ms <- runIndentParser (toFilePath filename) parseModules content
250 return (filename, ms)
250 return (filename, ms)
@@ -303,8 +303,8 @@ parseCase = Case <$> P.between (P.try (C.reserved "case")) (C.indented *> C.rese
303
303
304parseCaseAlternative :: P.Parsec String ParseState CaseAlternative
304parseCaseAlternative :: P.Parsec String ParseState CaseAlternative
305parseCaseAlternative = CaseAlternative <$> (return <$> parseBinder)
305parseCaseAlternative = CaseAlternative <$> (return <$> parseBinder)
306− <*> (Left <$> (C.indented *>
306+ <*> (Left <$> (C.indented *>
307− C.mark (P.many1 ((,) <$> (C.same *> parseGuard)
307+ C.mark (P.many1 ((,) <$> (C.same *> parseGuard)
308 <*> (lexeme (indented *> C.reservedOp "->") *> parseValue)
308 <*> (lexeme (indented *> C.reservedOp "->") *> parseValue)
309 )))
309 )))
310 <|> Right <$> (lexeme (indented *> C.reservedOp "->") *> parseValue))
310 <|> Right <$> (lexeme (indented *> C.reservedOp "->") *> parseValue))
src/Language/PureScript/Pretty/Values.hs+3−3
@@ -26,7 +26,7 @@ import Control.PatternArrows
26import Control.Monad.State
26import Control.Monad.State
27import Control.Applicative
27import Control.Applicative
28
28
29−import Language.PureScript.Declarations
29+import Language.PureScript.AST
30import Language.PureScript.Pretty.Common
30import Language.PureScript.Pretty.Common
31import Language.PureScript.Pretty.Types (prettyPrintType)
31import Language.PureScript.Pretty.Types (prettyPrintType)
32
32
@@ -102,9 +102,9 @@ prettyPrintCaseAlternative (CaseAlternative binders result) =
102 where
102 where
103 prettyPrintResult (Left gs) = concat <$> mapM prettyPrintGuardedValue gs
103 prettyPrintResult (Left gs) = concat <$> mapM prettyPrintGuardedValue gs
104 prettyPrintResult (Right v) = (" -> " ++) <$> prettyPrintValue' v
104 prettyPrintResult (Right v) = (" -> " ++) <$> prettyPrintValue' v
105−
105+
106 prettyPrintGuardedValue (grd, val) =
106 prettyPrintGuardedValue (grd, val) =
107− fmap concat $ sequence
107+ fmap concat $ sequence
108 [ return "| "
108 [ return "| "
109 , prettyPrintValue' grd
109 , prettyPrintValue' grd
110 , return " -> "
110 , return " -> "
src/Language/PureScript/Renamer.hs+2−2
@@ -23,7 +23,7 @@ import Data.List (find)
23import qualified Data.Map as M
23import qualified Data.Map as M
24import qualified Data.Set as S
24import qualified Data.Set as S
25
25
26−import Language.PureScript.Declarations
26+import Language.PureScript.AST
27import Language.PureScript.Environment
27import Language.PureScript.Environment
28import Language.PureScript.Names
28import Language.PureScript.Names
29import Language.PureScript.Traversals
29import Language.PureScript.Traversals
@@ -188,7 +188,7 @@ renameInValue v = return v
188--
188--
189renameInCaseAlternative :: CaseAlternative -> Rename CaseAlternative
189renameInCaseAlternative :: CaseAlternative -> Rename CaseAlternative
190renameInCaseAlternative (CaseAlternative bs v) =
190renameInCaseAlternative (CaseAlternative bs v) =
191− CaseAlternative <$> mapM renameInBinder bs
191+ CaseAlternative <$> mapM renameInBinder bs
192 <*> eitherM (mapM (pairM renameInValue renameInValue)) renameInValue v
192 <*> eitherM (mapM (pairM renameInValue renameInValue)) renameInValue v
193
193
194-- |
194-- |
src/Language/PureScript/Sugar.hs+1−1
@@ -19,7 +19,7 @@ import Control.Monad
19import Control.Category ((>>>))
19import Control.Category ((>>>))
20import Control.Monad.Trans.Class
20import Control.Monad.Trans.Class
21
21
22−import Language.PureScript.Declarations
22+import Language.PureScript.AST
23import Language.PureScript.Errors
23import Language.PureScript.Errors
24import Language.PureScript.Supply
24import Language.PureScript.Supply
25
25
src/Language/PureScript/Sugar/BindingGroups.hs+5−5
@@ -29,7 +29,7 @@ import Control.Applicative ((<$>), (<*>), pure)
29
29
30import qualified Data.Set as S
30import qualified Data.Set as S
31
31
32−import Language.PureScript.Declarations
32+import Language.PureScript.AST
33import Language.PureScript.Names
33import Language.PureScript.Names
34import Language.PureScript.Types
34import Language.PureScript.Types
35import Language.PureScript.Environment
35import Language.PureScript.Environment
@@ -98,13 +98,13 @@ usedIdents moduleName =
98 in nub . f
98 in nub . f
99 where
99 where
100 def s _ = (s, [])
100 def s _ = (s, [])
101−
101+
102 usedNamesE :: S.Set Ident -> Expr -> (S.Set Ident, [Ident])
102 usedNamesE :: S.Set Ident -> Expr -> (S.Set Ident, [Ident])
103 usedNamesE scope (Var (Qualified Nothing name)) | name `S.notMember` scope = (scope, [name])
103 usedNamesE scope (Var (Qualified Nothing name)) | name `S.notMember` scope = (scope, [name])
104 usedNamesE scope (Var (Qualified (Just moduleName') name)) | moduleName == moduleName' && name `S.notMember` scope = (scope, [name])
104 usedNamesE scope (Var (Qualified (Just moduleName') name)) | moduleName == moduleName' && name `S.notMember` scope = (scope, [name])
105 usedNamesE scope (Abs (Left name) _) = (name `S.insert` scope, [])
105 usedNamesE scope (Abs (Left name) _) = (name `S.insert` scope, [])
106 usedNamesE scope _ = (scope, [])
106 usedNamesE scope _ = (scope, [])
107−
107+
108 usedNamesB :: S.Set Ident -> Binder -> (S.Set Ident, [Ident])
108 usedNamesB :: S.Set Ident -> Binder -> (S.Set Ident, [Ident])
109 usedNamesB scope binder = (scope `S.union` S.fromList (binderNames binder), [])
109 usedNamesB scope binder = (scope `S.union` S.fromList (binderNames binder), [])
110
110
@@ -114,7 +114,7 @@ usedImmediateIdents moduleName =
114 in nub . f
114 in nub . f
115 where
115 where
116 def s _ = (s, [])
116 def s _ = (s, [])
117−
117+
118 usedNamesE :: Bool -> Expr -> (Bool, [Ident])
118 usedNamesE :: Bool -> Expr -> (Bool, [Ident])
119 usedNamesE True (Var (Qualified Nothing name)) = (True, [name])
119 usedNamesE True (Var (Qualified Nothing name)) = (True, [name])
120 usedNamesE True (Var (Qualified (Just moduleName') name)) | moduleName == moduleName' = (True, [name])
120 usedNamesE True (Var (Qualified (Just moduleName') name)) | moduleName == moduleName' = (True, [name])
@@ -147,7 +147,7 @@ getProperName _ = error "Expected DataDeclaration"
147
147
148-- |
148-- |
149-- Convert a group of mutually-recursive dependencies into a BindingGroupDeclaration (or simple ValueDeclaration).
149-- Convert a group of mutually-recursive dependencies into a BindingGroupDeclaration (or simple ValueDeclaration).
150−--
150+--
151--
151--
152toBindingGroup :: ModuleName -> SCC Declaration -> Either ErrorStack Declaration
152toBindingGroup :: ModuleName -> SCC Declaration -> Either ErrorStack Declaration
153toBindingGroup _ (AcyclicSCC d) = return d
153toBindingGroup _ (AcyclicSCC d) = return d
src/Language/PureScript/Sugar/CaseDeclarations.hs+1−1
@@ -28,7 +28,7 @@ import Control.Monad ((<=<), forM, join, unless, replicateM)
28import Control.Monad.Error.Class
28import Control.Monad.Error.Class
29
29
30import Language.PureScript.Names
30import Language.PureScript.Names
31−import Language.PureScript.Declarations
31+import Language.PureScript.AST
32import Language.PureScript.Environment
32import Language.PureScript.Environment
33import Language.PureScript.Errors
33import Language.PureScript.Errors
34import Language.PureScript.Supply
34import Language.PureScript.Supply
src/Language/PureScript/Sugar/DoNotation.hs+1−1
@@ -19,7 +19,7 @@ module Language.PureScript.Sugar.DoNotation (
19) where
19) where
20
20
21import Language.PureScript.Names
21import Language.PureScript.Names
22−import Language.PureScript.Declarations
22+import Language.PureScript.AST
23import Language.PureScript.Errors
23import Language.PureScript.Errors
24import Language.PureScript.Supply
24import Language.PureScript.Supply
25
25
src/Language/PureScript/Sugar/Names.hs+1−1
@@ -25,7 +25,7 @@ import Control.Monad.Error
25
25
26import qualified Data.Map as M
26import qualified Data.Map as M
27
27
28−import Language.PureScript.Declarations
28+import Language.PureScript.AST
29import Language.PureScript.Names
29import Language.PureScript.Names
30import Language.PureScript.Types
30import Language.PureScript.Types
31import Language.PureScript.Environment
31import Language.PureScript.Environment
src/Language/PureScript/Sugar/Operators.hs+1−1
@@ -25,7 +25,7 @@ module Language.PureScript.Sugar.Operators (
25) where
25) where
26
26
27import Language.PureScript.Names
27import Language.PureScript.Names
28−import Language.PureScript.Declarations
28+import Language.PureScript.AST
29import Language.PureScript.Errors
29import Language.PureScript.Errors
30
30
31import Control.Applicative
31import Control.Applicative
src/Language/PureScript/Sugar/TypeClasses.hs+1−1
@@ -18,7 +18,7 @@ module Language.PureScript.Sugar.TypeClasses (
18 desugarTypeClasses
18 desugarTypeClasses
19) where
19) where
20
20
21−import Language.PureScript.Declarations
21+import Language.PureScript.AST
22import Language.PureScript.Names
22import Language.PureScript.Names
23import Language.PureScript.Types
23import Language.PureScript.Types
24import Language.PureScript.Kinds
24import Language.PureScript.Kinds
src/Language/PureScript/Sugar/TypeDeclarations.hs+1−1
@@ -25,7 +25,7 @@ import Control.Applicative
25import Control.Monad.Error.Class
25import Control.Monad.Error.Class
26import Control.Monad (forM)
26import Control.Monad (forM)
27
27
28−import Language.PureScript.Declarations
28+import Language.PureScript.AST
29import Language.PureScript.Names
29import Language.PureScript.Names
30import Language.PureScript.Environment
30import Language.PureScript.Environment
31import Language.PureScript.Errors
31import Language.PureScript.Errors
src/Language/PureScript/TypeChecker.hs+6−6
@@ -37,7 +37,7 @@ import Control.Monad.Error
37import Language.PureScript.Types
37import Language.PureScript.Types
38import Language.PureScript.Names
38import Language.PureScript.Names
39import Language.PureScript.Kinds
39import Language.PureScript.Kinds
40−import Language.PureScript.Declarations
40+import Language.PureScript.AST
41import Language.PureScript.TypeClassDictionaries
41import Language.PureScript.TypeClassDictionaries
42import Language.PureScript.Environment
42import Language.PureScript.Environment
43import Language.PureScript.Errors
43import Language.PureScript.Errors
@@ -145,7 +145,7 @@ typeCheckAll mainModuleName moduleName exps = go
145 (syn_ks, data_ks) <- kindsOfAll moduleName syns (map (\(_, name, args, dctors) -> (name, args, concatMap snd dctors)) dataDecls)
145 (syn_ks, data_ks) <- kindsOfAll moduleName syns (map (\(_, name, args, dctors) -> (name, args, concatMap snd dctors)) dataDecls)
146 forM_ (zip dataDecls data_ks) $ \((dtype, name, args, dctors), ctorKind) -> do
146 forM_ (zip dataDecls data_ks) $ \((dtype, name, args, dctors), ctorKind) -> do
147 checkDuplicateTypeArguments $ map fst args
147 checkDuplicateTypeArguments $ map fst args
148− let args' = args `withKinds` ctorKind
148+ let args' = args `withKinds` ctorKind
149 addDataType moduleName dtype name args' dctors ctorKind
149 addDataType moduleName dtype name args' dctors ctorKind
150 forM_ (zip syns syn_ks) $ \((name, args, ty), kind) -> do
150 forM_ (zip syns syn_ks) $ \((name, args, ty), kind) -> do
151 checkDuplicateTypeArguments $ map fst args
151 checkDuplicateTypeArguments $ map fst args
@@ -183,10 +183,10 @@ typeCheckAll mainModuleName moduleName exps = go
183 forM_ (map (\(ident, _, _) -> ident) vals) $ \name ->
183 forM_ (map (\(ident, _, _) -> ident) vals) $ \name ->
184 valueIsNotDefined moduleName name
184 valueIsNotDefined moduleName name
185 tys <- typesOf mainModuleName moduleName $ map (\(ident, _, ty) -> (ident, ty)) vals
185 tys <- typesOf mainModuleName moduleName $ map (\(ident, _, ty) -> (ident, ty)) vals
186− vals' <- forM [ (name, val, nameKind, ty)
186+ vals' <- forM [ (name, val, nameKind, ty)
187 | (name, nameKind, _) <- vals
187 | (name, nameKind, _) <- vals
188 , (name', (val, ty)) <- tys
188 , (name', (val, ty)) <- tys
189− , name == name'
189+ , name == name'
190 ] $ \(name, val, nameKind, ty) -> do
190 ] $ \(name, val, nameKind, ty) -> do
191 addValue moduleName name ty nameKind
191 addValue moduleName name ty nameKind
192 return (name, nameKind, val)
192 return (name, nameKind, val)
@@ -248,9 +248,9 @@ typeCheckAll mainModuleName moduleName exps = go
248 rethrowWithPosition pos $ do
248 rethrowWithPosition pos $ do
249 (d' : rest') <- go (d : rest)
249 (d' : rest') <- go (d : rest)
250 return (PositionedDeclaration pos d' : rest')
250 return (PositionedDeclaration pos d' : rest')
251−
251+
252 -- |
252 -- |
253− -- This function adds the argument kinds for a type constructor so that they may appear in the externs file,
253+ -- This function adds the argument kinds for a type constructor so that they may appear in the externs file,
254 -- extracted from the kind of the type constructor itself.
254 -- extracted from the kind of the type constructor itself.
255 --
255 --
256 withKinds :: [(String, Maybe Kind)] -> Kind -> [(String, Maybe Kind)]
256 withKinds :: [(String, Maybe Kind)] -> Kind -> [(String, Maybe Kind)]
src/Language/PureScript/TypeChecker/Entailment.hs+1−1
@@ -26,7 +26,7 @@ import Control.Applicative
26import Control.Arrow (Arrow(..))
26import Control.Arrow (Arrow(..))
27import Control.Monad.Error
27import Control.Monad.Error
28
28
29−import Language.PureScript.Declarations
29+import Language.PureScript.AST
30import Language.PureScript.Environment
30import Language.PureScript.Environment
31import Language.PureScript.Names
31import Language.PureScript.Names
32import Language.PureScript.Pretty
32import Language.PureScript.Pretty
src/Language/PureScript/TypeChecker/Monad.hs−1
@@ -26,7 +26,6 @@ import Control.Monad.Error
26import Control.Monad.State
26import Control.Monad.State
27import Control.Monad.Unify
27import Control.Monad.Unify
28
28
29−import Language.PureScript.Declarations (canonicalizeDictionary)
30import Language.PureScript.Environment
29import Language.PureScript.Environment
31import Language.PureScript.Errors
30import Language.PureScript.Errors
32import Language.PureScript.Kinds
31import Language.PureScript.Kinds
src/Language/PureScript/TypeChecker/Rows.hs+1−1
@@ -21,7 +21,7 @@ import Data.List
21
21
22import Control.Monad.Error
22import Control.Monad.Error
23
23
24−import Language.PureScript.Declarations
24+import Language.PureScript.AST
25import Language.PureScript.Errors
25import Language.PureScript.Errors
26import Language.PureScript.TypeChecker.Monad
26import Language.PureScript.TypeChecker.Monad
27import Language.PureScript.Types
27import Language.PureScript.Types
src/Language/PureScript/TypeChecker/Skolems.hs+1−1
@@ -29,7 +29,7 @@ import Control.Applicative
29import Control.Monad.Error
29import Control.Monad.Error
30import Control.Monad.Unify
30import Control.Monad.Unify
31
31
32−import Language.PureScript.Declarations
32+import Language.PureScript.AST
33import Language.PureScript.Errors
33import Language.PureScript.Errors
34import Language.PureScript.Pretty
34import Language.PureScript.Pretty
35import Language.PureScript.TypeChecker.Monad
35import Language.PureScript.TypeChecker.Monad
src/Language/PureScript/TypeChecker/Subsumption.hs+1−1
@@ -25,7 +25,7 @@ import Control.Applicative
25import Control.Monad.Error
25import Control.Monad.Error
26import Control.Monad.Unify
26import Control.Monad.Unify
27
27
28−import Language.PureScript.Declarations
28+import Language.PureScript.AST
29import Language.PureScript.Environment
29import Language.PureScript.Environment
30import Language.PureScript.Errors
30import Language.PureScript.Errors
31import Language.PureScript.Pretty
31import Language.PureScript.Pretty
src/Language/PureScript/TypeChecker/Types.hs+1−1
@@ -46,7 +46,7 @@ import Control.Monad.Error
46import Control.Monad.State
46import Control.Monad.State
47import Control.Monad.Unify
47import Control.Monad.Unify
48
48
49−import Language.PureScript.Declarations
49+import Language.PureScript.AST
50import Language.PureScript.Environment
50import Language.PureScript.Environment
51import Language.PureScript.Errors
51import Language.PureScript.Errors
52import Language.PureScript.Kinds
52import Language.PureScript.Kinds
src/Language/PureScript/TypeClassDictionaries.hs+7
@@ -64,3 +64,10 @@ data TypeClassDictionaryType
64 -- A type class dictionary which is an alias for an imported dictionary from another module
64 -- A type class dictionary which is an alias for an imported dictionary from another module
65 --
65 --
66 | TCDAlias (Qualified Ident) deriving (Show, Eq, Data, Typeable)
66 | TCDAlias (Qualified Ident) deriving (Show, Eq, Data, Typeable)
67+
68+-- |
69+-- Find the original dictionary which a type class dictionary in scope refers to
70+--
71+canonicalizeDictionary :: TypeClassDictionaryInScope -> Qualified Ident
72+canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDRegular, tcdName = nm }) = nm
73+canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDAlias nm }) = nm