Fix comments about data constructors
[ghc-hetmet.git] / compiler / basicTypes / OccName.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %
5
6 \begin{code}
7 module OccName (
8         -- * The NameSpace type; abstact
9         NameSpace, tcName, clsName, tcClsName, dataName, varName, 
10         tvName, srcDataName,
11
12         -- ** Printing
13         pprNameSpace, pprNonVarNameSpace, pprNameSpaceBrief,
14
15         -- * The OccName type
16         OccName,        -- Abstract, instance of Outputable
17         pprOccName, 
18
19         -- ** Construction      
20         mkOccName, mkOccNameFS, 
21         mkVarOcc, mkVarOccFS,
22         mkTyVarOcc,
23         mkDFunOcc,
24         mkTupleOcc, 
25         setOccNameSpace,
26
27         -- ** Derived OccNames
28         mkDataConWrapperOcc, mkWorkerOcc, mkDefaultMethodOcc,
29         mkDerivedTyConOcc, mkNewTyCoOcc,
30         mkClassTyConOcc, mkClassDataConOcc, mkDictOcc, mkIPOcc, 
31         mkSpecOcc, mkForeignExportOcc, mkGenOcc1, mkGenOcc2,
32         mkDataTOcc, mkDataCOcc, mkDataConWorkerOcc,
33         mkSuperDictSelOcc, mkLocalOcc, mkMethodOcc, mkInstTyTcOcc,
34         mkInstTyCoOcc, mkEqPredCoOcc,
35
36         -- ** Deconstruction
37         occNameFS, occNameString, occNameSpace, 
38
39         isVarOcc, isTvOcc, isTcOcc, isDataOcc, isDataSymOcc, isSymOcc, isValOcc,
40         parenSymOcc, reportIfUnused, isTcClsName, isVarName,
41
42         isTupleOcc_maybe,
43
44         -- The OccEnv type
45         OccEnv, emptyOccEnv, unitOccEnv, extendOccEnv, mapOccEnv,
46         lookupOccEnv, mkOccEnv, mkOccEnv_C, extendOccEnvList, elemOccEnv,
47         occEnvElts, foldOccEnv, plusOccEnv, plusOccEnv_C, extendOccEnv_C,
48
49         -- The OccSet type
50         OccSet, emptyOccSet, unitOccSet, mkOccSet, extendOccSet, 
51         extendOccSetList,
52         unionOccSets, unionManyOccSets, minusOccSet, elemOccSet, occSetElts, 
53         foldOccSet, isEmptyOccSet, intersectOccSet, intersectsOccSet,
54
55         -- Tidying up
56         TidyOccEnv, emptyTidyOccEnv, tidyOccName, initTidyOccEnv,
57
58         -- The basic form of names
59         isLexCon, isLexVar, isLexId, isLexSym,
60         isLexConId, isLexConSym, isLexVarId, isLexVarSym,
61         startsVarSym, startsVarId, startsConSym, startsConId
62     ) where
63
64 #include "HsVersions.h"
65
66 import Util
67 import Unique
68 import BasicTypes
69 import StaticFlags
70 import UniqFM
71 import UniqSet
72 import FastString
73 import Outputable
74 import Binary
75
76 import GHC.Exts
77 import Data.Char
78
79 -- Unicode TODO: put isSymbol in libcompat
80 #if __GLASGOW_HASKELL__ > 604
81 import Data.Char        ( isSymbol )
82 #else
83 isSymbol = const False
84 #endif
85
86 \end{code}
87
88 %************************************************************************
89 %*                                                                      *
90 \subsection{Name space}
91 %*                                                                      *
92 %************************************************************************
93
94 \begin{code}
95 data NameSpace = VarName        -- Variables, including "real" data constructors
96                | DataName       -- "Source" data constructors 
97                | TvName         -- Type variables
98                | TcClsName      -- Type constructors and classes; Haskell has them
99                                 -- in the same name space for now.
100                deriving( Eq, Ord )
101    {-! derive: Binary !-}
102
103 -- Note [Data Constructors]  
104 -- see also: Note [Data Constructor Naming] in DataCon.lhs
105 -- 
106 --      "Source" data constructors are the data constructors mentioned
107 --      in Haskell source code
108 --
109 --      "Real" data constructors are the data constructors of the
110 --      representation type, which may not be the same as the source
111 --      type
112
113 -- Example:
114 --      data T = T !(Int,Int)
115 --
116 -- The source datacon has type (Int,Int) -> T
117 -- The real   datacon has type Int -> Int -> T
118 -- GHC chooses a representation based on the strictness etc.
119
120
121 -- Though type constructors and classes are in the same name space now,
122 -- the NameSpace type is abstract, so we can easily separate them later
123 tcName    = TcClsName           -- Type constructors
124 clsName   = TcClsName           -- Classes
125 tcClsName = TcClsName           -- Not sure which!
126
127 dataName    = DataName
128 srcDataName = DataName  -- Haskell-source data constructors should be
129                         -- in the Data name space
130
131 tvName      = TvName
132 varName     = VarName
133
134 isTcClsName :: NameSpace -> Bool
135 isTcClsName TcClsName = True
136 isTcClsName _         = False
137
138 isVarName :: NameSpace -> Bool  -- Variables or type variables, but not constructors
139 isVarName TvName  = True
140 isVarName VarName = True
141 isVarName other   = False
142
143 pprNameSpace :: NameSpace -> SDoc
144 pprNameSpace DataName  = ptext SLIT("data constructor")
145 pprNameSpace VarName   = ptext SLIT("variable")
146 pprNameSpace TvName    = ptext SLIT("type variable")
147 pprNameSpace TcClsName = ptext SLIT("type constructor or class")
148
149 pprNonVarNameSpace :: NameSpace -> SDoc
150 pprNonVarNameSpace VarName = empty
151 pprNonVarNameSpace ns = pprNameSpace ns
152
153 pprNameSpaceBrief DataName  = char 'd'
154 pprNameSpaceBrief VarName   = char 'v'
155 pprNameSpaceBrief TvName    = ptext SLIT("tv")
156 pprNameSpaceBrief TcClsName = ptext SLIT("tc")
157 \end{code}
158
159
160 %************************************************************************
161 %*                                                                      *
162 \subsection[Name-pieces-datatypes]{The @OccName@ datatypes}
163 %*                                                                      *
164 %************************************************************************
165
166 \begin{code}
167 data OccName = OccName 
168     { occNameSpace  :: !NameSpace
169     , occNameFS     :: !FastString
170     }
171 \end{code}
172
173
174 \begin{code}
175 instance Eq OccName where
176     (OccName sp1 s1) == (OccName sp2 s2) = s1 == s2 && sp1 == sp2
177
178 instance Ord OccName where
179         -- Compares lexicographically, *not* by Unique of the string
180     compare (OccName sp1 s1) (OccName sp2 s2) 
181         = (s1  `compare` s2) `thenCmp` (sp1 `compare` sp2)
182 \end{code}
183
184
185 %************************************************************************
186 %*                                                                      *
187 \subsection{Printing}
188 %*                                                                      *
189 %************************************************************************
190  
191 \begin{code}
192 instance Outputable OccName where
193     ppr = pprOccName
194
195 pprOccName :: OccName -> SDoc
196 pprOccName (OccName sp occ) 
197   = getPprStyle $ \ sty ->
198     if codeStyle sty 
199         then ftext (zEncodeFS occ)
200         else ftext occ <> if debugStyle sty 
201                             then braces (pprNameSpaceBrief sp)
202                             else empty
203 \end{code}
204
205
206 %************************************************************************
207 %*                                                                      *
208 \subsection{Construction}
209 %*                                                                      *
210 %************************************************************************
211
212 \begin{code}
213 mkOccName :: NameSpace -> String -> OccName
214 mkOccName occ_sp str = OccName occ_sp (mkFastString str)
215
216 mkOccNameFS :: NameSpace -> FastString -> OccName
217 mkOccNameFS occ_sp fs = OccName occ_sp fs
218
219 mkVarOcc :: String -> OccName
220 mkVarOcc s = mkOccName varName s
221
222 mkVarOccFS :: FastString -> OccName
223 mkVarOccFS fs = mkOccNameFS varName fs
224
225 mkTyVarOcc :: FastString -> OccName
226 mkTyVarOcc fs = mkOccNameFS tvName fs
227 \end{code}
228
229
230 %************************************************************************
231 %*                                                                      *
232                 Environments
233 %*                                                                      *
234 %************************************************************************
235
236 OccEnvs are used mainly for the envts in ModIfaces.
237
238 They are efficient, because FastStrings have unique Int# keys.  We assume
239 this key is less than 2^24, so we can make a Unique using
240         mkUnique ns key  :: Unique
241 where 'ns' is a Char reprsenting the name space.  This in turn makes it
242 easy to build an OccEnv.
243
244 \begin{code}
245 instance Uniquable OccName where
246   getUnique (OccName ns fs)
247       = mkUnique char (I# (uniqueOfFS fs))
248       where     -- See notes above about this getUnique function
249         char = case ns of
250                 VarName   -> 'i'
251                 DataName  -> 'd'
252                 TvName    -> 'v'
253                 TcClsName -> 't'
254
255 type OccEnv a = UniqFM a
256
257 emptyOccEnv :: OccEnv a
258 unitOccEnv  :: OccName -> a -> OccEnv a
259 extendOccEnv :: OccEnv a -> OccName -> a -> OccEnv a
260 extendOccEnvList :: OccEnv a -> [(OccName, a)] -> OccEnv a
261 lookupOccEnv :: OccEnv a -> OccName -> Maybe a
262 mkOccEnv     :: [(OccName,a)] -> OccEnv a
263 mkOccEnv_C   :: (a -> a -> a) -> [(OccName,a)] -> OccEnv a
264 elemOccEnv   :: OccName -> OccEnv a -> Bool
265 foldOccEnv   :: (a -> b -> b) -> b -> OccEnv a -> b
266 occEnvElts   :: OccEnv a -> [a]
267 extendOccEnv_C :: (a->a->a) -> OccEnv a -> OccName -> a -> OccEnv a
268 plusOccEnv     :: OccEnv a -> OccEnv a -> OccEnv a
269 plusOccEnv_C   :: (a->a->a) -> OccEnv a -> OccEnv a -> OccEnv a
270 mapOccEnv      :: (a->b) -> OccEnv a -> OccEnv b
271
272 emptyOccEnv      = emptyUFM
273 unitOccEnv       = unitUFM
274 extendOccEnv     = addToUFM
275 extendOccEnvList = addListToUFM
276 lookupOccEnv     = lookupUFM
277 mkOccEnv         = listToUFM
278 elemOccEnv       = elemUFM
279 foldOccEnv       = foldUFM
280 occEnvElts       = eltsUFM
281 plusOccEnv       = plusUFM
282 plusOccEnv_C     = plusUFM_C
283 extendOccEnv_C   = addToUFM_C
284 mapOccEnv        = mapUFM
285
286 mkOccEnv_C comb l = addListToUFM_C comb emptyOccEnv l
287
288 type OccSet = UniqFM OccName
289
290 emptyOccSet       :: OccSet
291 unitOccSet        :: OccName -> OccSet
292 mkOccSet          :: [OccName] -> OccSet
293 extendOccSet      :: OccSet -> OccName -> OccSet
294 extendOccSetList  :: OccSet -> [OccName] -> OccSet
295 unionOccSets      :: OccSet -> OccSet -> OccSet
296 unionManyOccSets  :: [OccSet] -> OccSet
297 minusOccSet       :: OccSet -> OccSet -> OccSet
298 elemOccSet        :: OccName -> OccSet -> Bool
299 occSetElts        :: OccSet -> [OccName]
300 foldOccSet        :: (OccName -> b -> b) -> b -> OccSet -> b
301 isEmptyOccSet     :: OccSet -> Bool
302 intersectOccSet   :: OccSet -> OccSet -> OccSet
303 intersectsOccSet  :: OccSet -> OccSet -> Bool
304
305 emptyOccSet       = emptyUniqSet
306 unitOccSet        = unitUniqSet
307 mkOccSet          = mkUniqSet
308 extendOccSet      = addOneToUniqSet
309 extendOccSetList  = addListToUniqSet
310 unionOccSets      = unionUniqSets
311 unionManyOccSets  = unionManyUniqSets
312 minusOccSet       = minusUniqSet
313 elemOccSet        = elementOfUniqSet
314 occSetElts        = uniqSetToList
315 foldOccSet        = foldUniqSet
316 isEmptyOccSet     = isEmptyUniqSet
317 intersectOccSet   = intersectUniqSets
318 intersectsOccSet s1 s2 = not (isEmptyOccSet (s1 `intersectOccSet` s2))
319 \end{code}
320
321
322 %************************************************************************
323 %*                                                                      *
324 \subsection{Predicates and taking them apart}
325 %*                                                                      *
326 %************************************************************************
327
328 \begin{code}
329 occNameString :: OccName -> String
330 occNameString (OccName _ s) = unpackFS s
331
332 setOccNameSpace :: NameSpace -> OccName -> OccName
333 setOccNameSpace sp (OccName _ occ) = OccName sp occ
334
335 isVarOcc, isTvOcc, isDataSymOcc, isSymOcc, isTcOcc :: OccName -> Bool
336
337 isVarOcc (OccName VarName _) = True
338 isVarOcc other               = False
339
340 isTvOcc (OccName TvName _) = True
341 isTvOcc other              = False
342
343 isTcOcc (OccName TcClsName _) = True
344 isTcOcc other                 = False
345
346 isValOcc (OccName VarName  _) = True
347 isValOcc (OccName DataName _) = True
348 isValOcc other                = False
349
350 -- Data constructor operator (starts with ':', or '[]')
351 -- Pretty inefficient!
352 isDataSymOcc (OccName DataName s) = isLexConSym s
353 isDataSymOcc (OccName VarName s)  
354   | isLexConSym s = pprPanic "isDataSymOcc: check me" (ppr s)
355                 -- Jan06: I don't think this should happen
356 isDataSymOcc other                = False
357
358 isDataOcc (OccName DataName _) = True
359 isDataOcc (OccName VarName s)  
360   | isLexCon s = pprPanic "isDataOcc: check me" (ppr s)
361                 -- Jan06: I don't think this should happen
362 isDataOcc other                = False
363
364 -- Any operator (data constructor or variable)
365 -- Pretty inefficient!
366 isSymOcc (OccName DataName s)  = isLexConSym s
367 isSymOcc (OccName TcClsName s) = isLexConSym s
368 isSymOcc (OccName VarName s)   = isLexSym s
369 isSymOcc other                 = False
370
371 parenSymOcc :: OccName -> SDoc -> SDoc
372 -- Wrap parens around an operator
373 parenSymOcc occ doc | isSymOcc occ = parens doc
374                     | otherwise    = doc
375 \end{code}
376
377
378 \begin{code}
379 reportIfUnused :: OccName -> Bool
380   -- Haskell 98 encourages compilers to suppress warnings about
381   -- unused names in a pattern if they start with "_".
382 reportIfUnused occ = case occNameString occ of
383                         ('_' : _) -> False
384                         _other    -> True
385 \end{code}
386
387
388 %************************************************************************
389 %*                                                                      *
390 \subsection{Making system names}
391 %*                                                                      *
392 %************************************************************************
393
394 Here's our convention for splitting up the interface file name space:
395
396         d...            dictionary identifiers
397                         (local variables, so no name-clash worries)
398
399         $f...           dict-fun identifiers (from inst decls)
400         $dm...          default methods
401         $p...           superclass selectors
402         $w...           workers
403         :T...           compiler-generated tycons for dictionaries
404         :D...           ...ditto data cons
405         :Co...          ...ditto coercions
406         $sf..           specialised version of f
407
408         in encoded form these appear as Zdfxxx etc
409
410         :...            keywords (export:, letrec: etc.)
411 --- I THINK THIS IS WRONG!
412
413 This knowledge is encoded in the following functions.
414
415
416 @mk_deriv@ generates an @OccName@ from the prefix and a string.
417 NB: The string must already be encoded!
418
419 \begin{code}
420 mk_deriv :: NameSpace 
421          -> String              -- Distinguishes one sort of derived name from another
422          -> String
423          -> OccName
424
425 mk_deriv occ_sp sys_prefix str = mkOccName occ_sp (sys_prefix ++ str)
426 \end{code}
427
428 \begin{code}
429 mkDataConWrapperOcc, mkWorkerOcc, mkDefaultMethodOcc, mkDerivedTyConOcc,
430         mkClassTyConOcc, mkClassDataConOcc, mkDictOcc, mkIPOcc, 
431         mkSpecOcc, mkForeignExportOcc, mkGenOcc1, mkGenOcc2,
432         mkDataTOcc, mkDataCOcc, mkDataConWorkerOcc, mkNewTyCoOcc
433    :: OccName -> OccName
434
435 -- These derived variables have a prefix that no Haskell value could have
436 mkDataConWrapperOcc = mk_simple_deriv varName  "$W"
437 mkWorkerOcc         = mk_simple_deriv varName  "$w"
438 mkDefaultMethodOcc  = mk_simple_deriv varName  "$dm"
439 mkDerivedTyConOcc   = mk_simple_deriv tcName   ":"      -- The : prefix makes sure it classifies
440 mkClassTyConOcc     = mk_simple_deriv tcName   ":T"     -- as a tycon/datacon
441 mkClassDataConOcc   = mk_simple_deriv dataName ":D"     -- We go straight to the "real" data con
442                                                         -- for datacons from classes
443 mkDictOcc           = mk_simple_deriv varName  "$d"
444 mkIPOcc             = mk_simple_deriv varName  "$i"
445 mkSpecOcc           = mk_simple_deriv varName  "$s"
446 mkForeignExportOcc  = mk_simple_deriv varName  "$f"
447 mkNewTyCoOcc        = mk_simple_deriv tcName  ":Co"
448 mkInstTyCoOcc       = mk_simple_deriv tcName  ":Co"      -- derived from rep ty
449 mkEqPredCoOcc       = mk_simple_deriv tcName  "$co"
450
451 -- Generic derivable classes
452 mkGenOcc1           = mk_simple_deriv varName  "$gfrom"
453 mkGenOcc2           = mk_simple_deriv varName  "$gto" 
454
455 -- data T = MkT ... deriving( Data ) needs defintions for 
456 --      $tT   :: Data.Generics.Basics.DataType
457 --      $cMkT :: Data.Generics.Basics.Constr
458 mkDataTOcc = mk_simple_deriv varName  "$t"
459 mkDataCOcc = mk_simple_deriv varName  "$c"
460
461 mk_simple_deriv sp px occ = mk_deriv sp px (occNameString occ)
462
463 -- Data constructor workers are made by setting the name space
464 -- of the data constructor OccName (which should be a DataName)
465 -- to VarName
466 mkDataConWorkerOcc datacon_occ = setOccNameSpace varName datacon_occ 
467 \end{code}
468
469 \begin{code}
470 mkSuperDictSelOcc :: Int        -- Index of superclass, eg 3
471                   -> OccName    -- Class, eg "Ord"
472                   -> OccName    -- eg "$p3Ord"
473 mkSuperDictSelOcc index cls_occ
474   = mk_deriv varName "$p" (show index ++ occNameString cls_occ)
475
476 mkLocalOcc :: Unique            -- Unique
477            -> OccName           -- Local name (e.g. "sat")
478            -> OccName           -- Nice unique version ("$L23sat")
479 mkLocalOcc uniq occ
480    = mk_deriv varName ("$L" ++ show uniq) (occNameString occ)
481         -- The Unique might print with characters 
482         -- that need encoding (e.g. 'z'!)
483 \end{code}
484
485 Derive a name for the representation type constructor of a data/newtype
486 instance.
487
488 \begin{code}
489 mkInstTyTcOcc :: Int                    -- Index
490               -> OccName                -- Family name (e.g. "Map")
491               -> OccName                -- Nice unique version (":R23Map")
492 mkInstTyTcOcc index occ
493    = mk_deriv tcName (":R" ++ show index) (occNameString occ)
494 \end{code}
495
496 \begin{code}
497 mkDFunOcc :: String             -- Typically the class and type glommed together e.g. "OrdMaybe"
498                                 -- Only used in debug mode, for extra clarity
499           -> Bool               -- True <=> hs-boot instance dfun
500           -> Int                -- Unique index
501           -> OccName            -- "$f3OrdMaybe"
502
503 -- In hs-boot files we make dict funs like $fx7ClsTy, which get bound to the real
504 -- thing when we compile the mother module. Reason: we don't know exactly
505 -- what the  mother module will call it.
506
507 mkDFunOcc info_str is_boot index 
508   = mk_deriv VarName prefix string
509   where
510     prefix | is_boot   = "$fx"
511            | otherwise = "$f"
512     string | opt_PprStyle_Debug = show index ++ info_str
513            | otherwise          = show index
514 \end{code}
515
516 We used to add a '$m' to indicate a method, but that gives rise to bad
517 error messages from the type checker when we print the function name or pattern
518 of an instance-decl binding.  Why? Because the binding is zapped
519 to use the method name in place of the selector name.
520 (See TcClassDcl.tcMethodBind)
521
522 The way it is now, -ddump-xx output may look confusing, but
523 you can always say -dppr-debug to get the uniques.
524
525 However, we *do* have to zap the first character to be lower case,
526 because overloaded constructors (blarg) generate methods too.
527 And convert to VarName space
528
529 e.g. a call to constructor MkFoo where
530         data (Ord a) => Foo a = MkFoo a
531
532 If this is necessary, we do it by prefixing '$m'.  These 
533 guys never show up in error messages.  What a hack.
534
535 \begin{code}
536 mkMethodOcc :: OccName -> OccName
537 mkMethodOcc occ@(OccName VarName fs) = occ
538 mkMethodOcc occ                      = mk_simple_deriv varName "$m" occ
539 \end{code}
540
541
542 %************************************************************************
543 %*                                                                      *
544 \subsection{Tidying them up}
545 %*                                                                      *
546 %************************************************************************
547
548 Before we print chunks of code we like to rename it so that
549 we don't have to print lots of silly uniques in it.  But we mustn't
550 accidentally introduce name clashes!  So the idea is that we leave the
551 OccName alone unless it accidentally clashes with one that is already
552 in scope; if so, we tack on '1' at the end and try again, then '2', and
553 so on till we find a unique one.
554
555 There's a wrinkle for operators.  Consider '>>='.  We can't use '>>=1' 
556 because that isn't a single lexeme.  So we encode it to 'lle' and *then*
557 tack on the '1', if necessary.
558
559 \begin{code}
560 type TidyOccEnv = OccEnv Int    -- The in-scope OccNames
561         -- Range gives a plausible starting point for new guesses
562
563 emptyTidyOccEnv = emptyOccEnv
564
565 initTidyOccEnv :: [OccName] -> TidyOccEnv       -- Initialise with names to avoid!
566 initTidyOccEnv = foldl (\env occ -> extendOccEnv env occ 1) emptyTidyOccEnv
567
568 tidyOccName :: TidyOccEnv -> OccName -> (TidyOccEnv, OccName)
569
570 tidyOccName in_scope occ@(OccName occ_sp fs)
571   = case lookupOccEnv in_scope occ of
572         Nothing ->      -- Not already used: make it used
573                    (extendOccEnv in_scope occ 1, occ)
574
575         Just n  ->      -- Already used: make a new guess, 
576                         -- change the guess base, and try again
577                    tidyOccName  (extendOccEnv in_scope occ (n+1))
578                                 (mkOccName occ_sp (unpackFS fs ++ show n))
579 \end{code}
580
581 %************************************************************************
582 %*                                                                      *
583                 Stuff for dealing with tuples
584 %*                                                                      *
585 %************************************************************************
586
587 \begin{code}
588 mkTupleOcc :: NameSpace -> Boxity -> Arity -> OccName
589 mkTupleOcc ns bx ar = OccName ns (mkFastString str)
590   where
591         -- no need to cache these, the caching is done in the caller
592         -- (TysWiredIn.mk_tuple)
593     str = case bx of
594                 Boxed   -> '(' : commas ++ ")"
595                 Unboxed -> '(' : '#' : commas ++ "#)"
596
597     commas = take (ar-1) (repeat ',')
598
599 isTupleOcc_maybe :: OccName -> Maybe (NameSpace, Boxity, Arity)
600 -- Tuples are special, because there are so many of them!
601 isTupleOcc_maybe (OccName ns fs)
602   = case unpackFS fs of
603         '(':'#':',':rest -> Just (ns, Unboxed, 2 + count_commas rest)
604         '(':',':rest     -> Just (ns, Boxed,   2 + count_commas rest)
605         _other           -> Nothing
606   where
607     count_commas (',':rest) = 1 + count_commas rest
608     count_commas _          = 0
609 \end{code}
610
611 %************************************************************************
612 %*                                                                      *
613 \subsection{Lexical categories}
614 %*                                                                      *
615 %************************************************************************
616
617 These functions test strings to see if they fit the lexical categories
618 defined in the Haskell report.
619
620 \begin{code}
621 isLexCon,   isLexVar,    isLexId,    isLexSym    :: FastString -> Bool
622 isLexConId, isLexConSym, isLexVarId, isLexVarSym :: FastString -> Bool
623
624 isLexCon cs = isLexConId  cs || isLexConSym cs
625 isLexVar cs = isLexVarId  cs || isLexVarSym cs
626
627 isLexId  cs = isLexConId  cs || isLexVarId  cs
628 isLexSym cs = isLexConSym cs || isLexVarSym cs
629
630 -------------
631
632 isLexConId cs                           -- Prefix type or data constructors
633   | nullFS cs         = False           --      e.g. "Foo", "[]", "(,)" 
634   | cs == FSLIT("[]") = True
635   | otherwise         = startsConId (headFS cs)
636
637 isLexVarId cs                           -- Ordinary prefix identifiers
638   | nullFS cs         = False           --      e.g. "x", "_x"
639   | otherwise         = startsVarId (headFS cs)
640
641 isLexConSym cs                          -- Infix type or data constructors
642   | nullFS cs         = False           --      e.g. ":-:", ":", "->"
643   | cs == FSLIT("->") = True
644   | otherwise         = startsConSym (headFS cs)
645
646 isLexVarSym cs                          -- Infix identifiers
647   | nullFS cs         = False           --      e.g. "+"
648   | otherwise         = startsVarSym (headFS cs)
649
650 -------------
651 startsVarSym, startsVarId, startsConSym, startsConId :: Char -> Bool
652 startsVarSym c = isSymbolASCII c || (ord c > 0x7f && isSymbol c) -- Infix Ids
653 startsConSym c = c == ':'                               -- Infix data constructors
654 startsVarId c  = isLower c || c == '_'  -- Ordinary Ids
655 startsConId c  = isUpper c || c == '('  -- Ordinary type constructors and data constructors
656
657 isSymbolASCII c = c `elem` "!#$%&*+./<=>?@\\^|~-"
658 \end{code}
659
660 %************************************************************************
661 %*                                                                      *
662                 Binary instance
663     Here rather than BinIface because OccName is abstract
664 %*                                                                      *
665 %************************************************************************
666
667 \begin{code}
668 instance Binary NameSpace where
669     put_ bh VarName = do
670             putByte bh 0
671     put_ bh DataName = do
672             putByte bh 1
673     put_ bh TvName = do
674             putByte bh 2
675     put_ bh TcClsName = do
676             putByte bh 3
677     get bh = do
678             h <- getByte bh
679             case h of
680               0 -> do return VarName
681               1 -> do return DataName
682               2 -> do return TvName
683               _ -> do return TcClsName
684
685 instance Binary OccName where
686     put_ bh (OccName aa ab) = do
687             put_ bh aa
688             put_ bh ab
689     get bh = do
690           aa <- get bh
691           ab <- get bh
692           return (OccName aa ab)
693 \end{code}