1
- {-# LANGUAGE DeriveDataTypeable #-}
2
- {-# LANGUAGE DeriveGeneric #-}
3
- {-# LANGUAGE FlexibleInstances #-}
1
+ {-# LANGUAGE DeriveDataTypeable #-}
2
+ {-# LANGUAGE DeriveGeneric #-}
3
+ {-# LANGUAGE FlexibleInstances #-}
4
4
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5
- {-# LANGUAGE PatternGuards #-}
6
- {-# LANGUAGE RankNTypes #-}
5
+ {-# LANGUAGE PatternGuards #-}
6
+ {-# LANGUAGE RankNTypes #-}
7
7
8
8
-- | This module defines the core data types for Backpack. For more
9
9
-- details, see:
10
10
--
11
11
-- <https://github.com/ezyang/ghc-proposals/blob/backpack/proposals/0000-backpack.rst>
12
-
13
- module Distribution.Backpack (
14
- -- * OpenUnitId
15
- OpenUnitId (.. ),
16
- openUnitIdFreeHoles ,
17
- mkOpenUnitId ,
12
+ module Distribution.Backpack
13
+ ( -- * OpenUnitId
14
+ OpenUnitId (.. )
15
+ , openUnitIdFreeHoles
16
+ , mkOpenUnitId
18
17
19
18
-- * DefUnitId
20
- DefUnitId ,
21
- unDefUnitId ,
22
- mkDefUnitId ,
19
+ , DefUnitId
20
+ , unDefUnitId
21
+ , mkDefUnitId
23
22
24
23
-- * OpenModule
25
- OpenModule (.. ),
26
- openModuleFreeHoles ,
24
+ , OpenModule (.. )
25
+ , openModuleFreeHoles
27
26
28
27
-- * OpenModuleSubst
29
- OpenModuleSubst ,
30
- dispOpenModuleSubst ,
31
- dispOpenModuleSubstEntry ,
32
- parsecOpenModuleSubst ,
33
- parsecOpenModuleSubstEntry ,
34
- openModuleSubstFreeHoles ,
28
+ , OpenModuleSubst
29
+ , dispOpenModuleSubst
30
+ , dispOpenModuleSubstEntry
31
+ , parsecOpenModuleSubst
32
+ , parsecOpenModuleSubstEntry
33
+ , openModuleSubstFreeHoles
35
34
36
35
-- * Conversions to 'UnitId'
37
- abstractUnitId ,
38
- hashModuleSubst ,
39
- ) where
36
+ , abstractUnitId
37
+ , hashModuleSubst
38
+ ) where
40
39
41
40
import Distribution.Compat.Prelude hiding (mod )
42
41
import Distribution.Parsec
43
42
import Distribution.Pretty
43
+ import Text.PrettyPrint (hcat )
44
44
import Prelude ()
45
- import Text.PrettyPrint (hcat )
46
45
47
46
import qualified Distribution.Compat.CharParsing as P
48
- import qualified Text.PrettyPrint as Disp
47
+ import qualified Text.PrettyPrint as Disp
49
48
50
49
import Distribution.ModuleName
51
50
import Distribution.Types.ComponentId
@@ -81,52 +80,53 @@ import qualified Data.Set as Set
81
80
--
82
81
-- For more details see the Backpack spec
83
82
-- <https://github.com/ezyang/ghc-proposals/blob/backpack/proposals/0000-backpack.rst>
84
- --
85
-
86
83
data OpenUnitId
87
- -- | Identifies a component which may have some unfilled holes;
84
+ = -- | Identifies a component which may have some unfilled holes;
88
85
-- specifying its 'ComponentId' and its 'OpenModuleSubst'.
89
86
-- TODO: Invariant that 'OpenModuleSubst' is non-empty?
90
87
-- See also the Text instance.
91
- = IndefFullUnitId ComponentId OpenModuleSubst
92
- -- | Identifies a fully instantiated component, which has
88
+ IndefFullUnitId ComponentId OpenModuleSubst
89
+ | -- | Identifies a fully instantiated component, which has
93
90
-- been compiled and abbreviated as a hash. The embedded 'UnitId'
94
91
-- MUST NOT be for an indefinite component; an 'OpenUnitId'
95
92
-- is guaranteed not to have any holes.
96
- | DefiniteUnitId DefUnitId
93
+ DefiniteUnitId DefUnitId
97
94
deriving (Generic , Read , Show , Eq , Ord , Typeable , Data )
95
+
98
96
-- TODO: cache holes?
99
97
100
98
instance Binary OpenUnitId
101
99
instance Structured OpenUnitId
102
100
instance NFData OpenUnitId where
103
- rnf (IndefFullUnitId cid subst) = rnf cid `seq` rnf subst
104
- rnf (DefiniteUnitId uid) = rnf uid
101
+ rnf (IndefFullUnitId cid subst) = rnf cid `seq` rnf subst
102
+ rnf (DefiniteUnitId uid) = rnf uid
105
103
106
104
instance Pretty OpenUnitId where
107
- pretty (IndefFullUnitId cid insts)
108
- -- TODO: arguably a smart constructor to enforce invariant would be
109
- -- better
110
- | Map. null insts = pretty cid
111
- | otherwise = pretty cid <<>> Disp. brackets (dispOpenModuleSubst insts)
112
- pretty (DefiniteUnitId uid) = pretty uid
105
+ pretty (IndefFullUnitId cid insts)
106
+ -- TODO: arguably a smart constructor to enforce invariant would be
107
+ -- better
108
+ | Map. null insts = pretty cid
109
+ | otherwise = pretty cid <<>> Disp. brackets (dispOpenModuleSubst insts)
110
+ pretty (DefiniteUnitId uid) = pretty uid
113
111
114
112
-- |
115
113
--
116
114
-- >>> eitherParsec "foobar" :: Either String OpenUnitId
117
- -- Right (DefiniteUnitId (DefUnitId {unDefUnitId = UnitId "foobar"}))
115
+ -- Right (DefiniteUnitId (DefUnitId {unDefUnitId = UnitId "foobar"}))
118
116
--
119
117
-- >>> eitherParsec "foo[Str=text-1.2.3:Data.Text.Text]" :: Either String OpenUnitId
120
118
-- Right (IndefFullUnitId (ComponentId "foo") (fromList [(ModuleName "Str",OpenModule (DefiniteUnitId (DefUnitId {unDefUnitId = UnitId "text-1.2.3"})) (ModuleName "Data.Text.Text"))]))
121
- --
122
119
instance Parsec OpenUnitId where
123
- parsec = P. try parseOpenUnitId <|> fmap DefiniteUnitId parsec
124
- where
125
- parseOpenUnitId = do
126
- cid <- parsec
127
- insts <- P. between (P. char ' [' ) (P. char ' ]' )
128
- parsecOpenModuleSubst
129
- return (IndefFullUnitId cid insts)
120
+ parsec = P. try parseOpenUnitId <|> fmap DefiniteUnitId parsec
121
+ where
122
+ parseOpenUnitId = do
123
+ cid <- parsec
124
+ insts <-
125
+ P. between
126
+ (P. char ' [' )
127
+ (P. char ' ]' )
128
+ parsecOpenModuleSubst
129
+ return (IndefFullUnitId cid insts)
130
130
131
131
-- | Get the set of holes ('ModuleVar') embedded in a 'UnitId'.
132
132
openUnitIdFreeHoles :: OpenUnitId -> Set ModuleName
@@ -137,9 +137,9 @@ openUnitIdFreeHoles _ = Set.empty
137
137
-- is if the instantiation is provided.
138
138
mkOpenUnitId :: UnitId -> ComponentId -> OpenModuleSubst -> OpenUnitId
139
139
mkOpenUnitId uid cid insts =
140
- if Set. null (openModuleSubstFreeHoles insts)
141
- then DefiniteUnitId (unsafeMkDefUnitId uid) -- invariant holds!
142
- else IndefFullUnitId cid insts
140
+ if Set. null (openModuleSubstFreeHoles insts)
141
+ then DefiniteUnitId (unsafeMkDefUnitId uid) -- invariant holds!
142
+ else IndefFullUnitId cid insts
143
143
144
144
-----------------------------------------------------------------------
145
145
-- DefUnitId
@@ -148,9 +148,12 @@ mkOpenUnitId uid cid insts =
148
148
-- with no holes.
149
149
mkDefUnitId :: ComponentId -> Map ModuleName Module -> DefUnitId
150
150
mkDefUnitId cid insts =
151
- unsafeMkDefUnitId (mkUnitId
152
- (unComponentId cid ++ maybe " " (" +" ++ ) (hashModuleSubst insts)))
153
- -- impose invariant!
151
+ unsafeMkDefUnitId
152
+ ( mkUnitId
153
+ (unComponentId cid ++ maybe " " (" +" ++ ) (hashModuleSubst insts))
154
+ )
155
+
156
+ -- impose invariant!
154
157
155
158
-----------------------------------------------------------------------
156
159
-- OpenModule
@@ -160,42 +163,41 @@ mkDefUnitId cid insts =
160
163
-- hole that needs to be filled in. Substitutions are over
161
164
-- module variables.
162
165
data OpenModule
163
- = OpenModule OpenUnitId ModuleName
164
- | OpenModuleVar ModuleName
166
+ = OpenModule OpenUnitId ModuleName
167
+ | OpenModuleVar ModuleName
165
168
deriving (Generic , Read , Show , Eq , Ord , Typeable , Data )
166
169
167
170
instance Binary OpenModule
168
171
instance Structured OpenModule
169
172
170
173
instance NFData OpenModule where
171
- rnf (OpenModule uid mod_name) = rnf uid `seq` rnf mod_name
172
- rnf (OpenModuleVar mod_name) = rnf mod_name
174
+ rnf (OpenModule uid mod_name) = rnf uid `seq` rnf mod_name
175
+ rnf (OpenModuleVar mod_name) = rnf mod_name
173
176
174
177
instance Pretty OpenModule where
175
- pretty (OpenModule uid mod_name) =
176
- hcat [pretty uid, Disp. text " :" , pretty mod_name]
177
- pretty (OpenModuleVar mod_name) =
178
- hcat [Disp. char ' <' , pretty mod_name, Disp. char ' >' ]
178
+ pretty (OpenModule uid mod_name) =
179
+ hcat [pretty uid, Disp. text " :" , pretty mod_name]
180
+ pretty (OpenModuleVar mod_name) =
181
+ hcat [Disp. char ' <' , pretty mod_name, Disp. char ' >' ]
179
182
180
183
-- |
181
184
--
182
185
-- >>> eitherParsec "Includes2-0.1.0.0-inplace-mysql:Database.MySQL" :: Either String OpenModule
183
186
-- Right (OpenModule (DefiniteUnitId (DefUnitId {unDefUnitId = UnitId "Includes2-0.1.0.0-inplace-mysql"})) (ModuleName "Database.MySQL"))
184
- --
185
187
instance Parsec OpenModule where
186
- parsec = parsecModuleVar <|> parsecOpenModule
187
- where
188
- parsecOpenModule = do
189
- uid <- parsec
190
- _ <- P. char ' :'
191
- mod_name <- parsec
192
- return (OpenModule uid mod_name)
193
-
194
- parsecModuleVar = do
195
- _ <- P. char ' <'
196
- mod_name <- parsec
197
- _ <- P. char ' >'
198
- return (OpenModuleVar mod_name)
188
+ parsec = parsecModuleVar <|> parsecOpenModule
189
+ where
190
+ parsecOpenModule = do
191
+ uid <- parsec
192
+ _ <- P. char ' :'
193
+ mod_name <- parsec
194
+ return (OpenModule uid mod_name)
195
+
196
+ parsecModuleVar = do
197
+ _ <- P. char ' <'
198
+ mod_name <- parsec
199
+ _ <- P. char ' >'
200
+ return (OpenModuleVar mod_name)
199
201
200
202
-- | Get the set of holes ('ModuleVar') embedded in a 'Module'.
201
203
openModuleFreeHoles :: OpenModule -> Set ModuleName
@@ -214,8 +216,8 @@ type OpenModuleSubst = Map ModuleName OpenModule
214
216
-- | Pretty-print the entries of a module substitution, suitable
215
217
-- for embedding into a 'OpenUnitId' or passing to GHC via @--instantiate-with@.
216
218
dispOpenModuleSubst :: OpenModuleSubst -> Disp. Doc
217
- dispOpenModuleSubst subst
218
- = Disp. hcat
219
+ dispOpenModuleSubst subst =
220
+ Disp. hcat
219
221
. Disp. punctuate Disp. comma
220
222
$ map dispOpenModuleSubstEntry (Map. toAscList subst)
221
223
@@ -227,19 +229,21 @@ dispOpenModuleSubstEntry (k, v) = pretty k <<>> Disp.char '=' <<>> pretty v
227
229
--
228
230
-- @since 2.2
229
231
parsecOpenModuleSubst :: CabalParsing m => m OpenModuleSubst
230
- parsecOpenModuleSubst = fmap Map. fromList
231
- . flip P. sepBy (P. char ' ,' )
232
- $ parsecOpenModuleSubstEntry
232
+ parsecOpenModuleSubst =
233
+ fmap Map. fromList
234
+ . flip P. sepBy (P. char ' ,' )
235
+ $ parsecOpenModuleSubstEntry
233
236
234
237
-- | Inverse to 'dispModSubstEntry'.
235
238
--
236
239
-- @since 2.2
237
240
parsecOpenModuleSubstEntry :: CabalParsing m => m (ModuleName , OpenModule )
238
241
parsecOpenModuleSubstEntry =
239
- do k <- parsec
240
- _ <- P. char ' ='
241
- v <- parsec
242
- return (k, v)
242
+ do
243
+ k <- parsec
244
+ _ <- P. char ' ='
245
+ v <- parsec
246
+ return (k, v)
243
247
244
248
-- | Get the set of holes ('ModuleVar') embedded in a 'OpenModuleSubst'.
245
249
-- This is NOT the domain of the substitution.
@@ -265,5 +269,7 @@ hashModuleSubst subst
265
269
| Map. null subst = Nothing
266
270
| otherwise =
267
271
Just . hashToBase62 $
268
- concat [ prettyShow mod_name ++ " =" ++ prettyShow m ++ " \n "
269
- | (mod_name, m) <- Map. toList subst]
272
+ concat
273
+ [ prettyShow mod_name ++ " =" ++ prettyShow m ++ " \n "
274
+ | (mod_name, m) <- Map. toList subst
275
+ ]
0 commit comments