[project @ 1999-04-14 04:07:57 by kglynn]
[ghc-hetmet.git] / ghc / compiler / main / MkIface.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
3 %
4 \section[MkIface]{Print an interface for a module}
5
6 \begin{code}
7 module MkIface (
8         startIface, endIface,
9         ifaceMain,
10         ifaceDecls
11     ) where
12
13 #include "HsVersions.h"
14
15 import IO               ( Handle, hPutStr, openFile, 
16                           hClose, hPutStrLn, IOMode(..) )
17
18 import HsSyn
19 import BasicTypes       ( Fixity(..), FixityDirection(..), NewOrData(..),
20                           StrictnessMark(..) 
21                         )
22 import RnMonad
23 import RnEnv            ( availName )
24
25 import TcInstUtil       ( InstInfo(..) )
26 import WorkWrap         ( getWorkerIdAndCons )
27
28 import CmdLineOpts
29 import Id               ( Id, idType, idInfo, omitIfaceSigForId,
30                           getIdSpecialisation
31                         )
32 import Var              ( isId )
33 import VarSet
34 import DataCon          ( dataConSig, dataConFieldLabels, dataConStrictMarks )
35 import IdInfo           ( IdInfo, StrictnessInfo, ArityInfo, InlinePragInfo(..), inlinePragInfo,
36                           arityInfo, ppArityInfo, 
37                           strictnessInfo, ppStrictnessInfo, 
38                           cafInfo, ppCafInfo,
39                           cprInfo, ppCprInfo,
40                           workerExists, workerInfo, isBottomingStrictness
41                         )
42 import CoreSyn          ( CoreExpr, CoreBind, Bind(..) )
43 import CoreUtils        ( exprSomeFreeVars )
44 import CoreUnfold       ( calcUnfoldingGuidance, UnfoldingGuidance(..), 
45                           Unfolding, okToUnfoldInHiFile )
46 import Module           ( moduleString, pprModule, pprModuleBoot )
47 import Name             ( isLocallyDefined, isWiredInName, nameRdrName, nameModule,
48                           isExported,
49                           Name, NamedThing(..)
50                         )
51 import OccName          ( OccName, pprOccName )
52 import TyCon            ( TyCon, getSynTyConDefn, isSynTyCon, isNewTyCon, isAlgTyCon,
53                           tyConTheta, tyConTyVars, tyConDataCons
54                         )
55 import Class            ( Class, classBigSig )
56 import SpecEnv          ( specEnvToList )
57 import FieldLabel       ( fieldLabelName, fieldLabelType )
58 import Type             ( mkSigmaTy, splitSigmaTy, mkDictTy, tidyTopType, deNoteType,
59                           Type, ThetaType
60                         )
61
62 import PprType
63 import PprCore          ( pprIfaceUnfolding )
64
65 import Bag              ( bagToList, isEmptyBag )
66 import Maybes           ( catMaybes, maybeToBool )
67 import FiniteMap        ( emptyFM, addToFM, addToFM_C, fmToList, FiniteMap )
68 import UniqFM           ( lookupUFM, listToUFM )
69 import UniqSet          ( uniqSetToList )
70 import Util             ( sortLt, mapAccumL )
71 import Outputable
72 \end{code}
73
74 We have a function @startIface@ to open the output file and put
75 (something like) ``interface Foo'' in it.  It gives back a handle
76 for subsequent additions to the interface file.
77
78 We then have one-function-per-block-of-interface-stuff, e.g.,
79 @ifaceExportList@ produces the @__exports__@ section; it appends
80 to the handle provided by @startIface@.
81
82 \begin{code}
83 startIface  :: Module
84             -> IO (Maybe Handle) -- Nothing <=> don't do an interface
85
86 ifaceMain   :: Maybe Handle
87             -> InterfaceDetails
88             -> IO ()
89
90
91 ifaceDecls :: Maybe Handle
92            -> [TyCon] -> [Class]
93            -> Bag InstInfo 
94            -> [Id]              -- Ids used at code-gen time; they have better pragma info!
95            -> [CoreBind]        -- In dependency order, later depend on earlier
96            -> IO ()
97
98 endIface    :: Maybe Handle -> IO ()
99 \end{code}
100
101 \begin{code}
102 startIface mod
103   = case opt_ProduceHi of
104       Nothing -> return Nothing -- not producing any .hi file
105       Just fn -> do
106         if_hdl <- openFile fn WriteMode
107         hPutStr if_hdl ("__interface " ++ moduleString mod ++ ' ':show (opt_HiVersion :: Int))
108         hPutStrLn if_hdl " where"
109         return (Just if_hdl)
110
111 endIface Nothing        = return ()
112 endIface (Just if_hdl)  = hPutStr if_hdl "\n" >> hClose if_hdl
113 \end{code}
114
115
116 \begin{code}
117 ifaceMain Nothing iface_stuff = return ()
118 ifaceMain (Just if_hdl)
119           (import_usages, ExportEnv avails fixities, instance_modules)
120   = do
121     ifaceImports                if_hdl import_usages
122     ifaceInstanceModules        if_hdl instance_modules
123     ifaceExports                if_hdl avails
124     ifaceFixities               if_hdl fixities
125     return ()
126
127 ifaceDecls Nothing tycons classes inst_info final_ids simplified = return ()
128 ifaceDecls (Just hdl)
129            tycons classes
130            inst_infos
131            final_ids binds
132   | null_decls = return ()               
133         --  You could have a module with just (re-)exports/instances in it
134   | otherwise
135   = ifaceClasses hdl classes                    >>
136     ifaceInstances hdl inst_infos               >>= \ needed_ids ->
137     ifaceTyCons hdl tycons                      >>
138     ifaceBinds hdl needed_ids final_ids binds   >>
139     return ()
140   where
141      null_decls = null binds      && 
142                   null tycons     &&
143                   null classes    && 
144                   isEmptyBag inst_infos
145 \end{code}
146
147 \begin{code}
148 ifaceImports if_hdl import_usages
149   = hPutCol if_hdl upp_uses (sortLt lt_imp_vers import_usages)
150   where
151     upp_uses (m, mv, whats_imported)
152       = ptext SLIT("import ") <>
153         hsep [pprModule m, pprModuleBoot m, int mv, dcolon,
154               upp_import_versions whats_imported
155         ] <> semi
156
157         -- Importing the whole module is indicated by an empty list
158     upp_import_versions Everything = empty
159
160         -- For imported versions we do print the version number
161     upp_import_versions (Specifically nvs)
162       = hsep [ hsep [ppr_unqual_name n, int v] | (n,v) <- sort_versions nvs ]
163
164 ifaceInstanceModules if_hdl [] = return ()
165 ifaceInstanceModules if_hdl imods
166   = let sorted = sortLt (<) imods
167         lines = map (\m -> ptext SLIT("__instimport ") <> pprModule m <>
168                            ptext SLIT(" ;")) sorted
169     in 
170     printForIface if_hdl (vcat lines) >>
171     hPutStr if_hdl "\n"
172
173 ifaceExports if_hdl [] = return ()
174 ifaceExports if_hdl avails
175   = hPutCol if_hdl do_one_module (fmToList export_fm)
176   where
177         -- Sort them into groups by module
178     export_fm :: FiniteMap Module [AvailInfo]
179     export_fm = foldr insert emptyFM avails
180
181     insert avail efm = addToFM_C (++) efm mod [avail] 
182                      where
183                        mod = nameModule (availName avail)
184
185         -- Print one module's worth of stuff
186     do_one_module :: (Module, [AvailInfo]) -> SDoc
187     do_one_module (mod_name, avails@(avail1:_))
188         = ptext SLIT("__export ") <>
189           hsep [pprModuleBoot (nameModule (availName avail1)), 
190                 pprModule mod_name,
191                 hsep (map upp_avail (sortLt lt_avail avails))
192           ] <> semi
193
194 ifaceFixities if_hdl [] = return ()
195 ifaceFixities if_hdl fixities 
196   = hPutCol if_hdl upp_fixity fixities
197 \end{code}                       
198
199 %************************************************************************
200 %*                                                                      *
201 \subsection{Instance declarations}
202 %*                                                                      *
203 %************************************************************************
204
205
206 \begin{code}                     
207 ifaceInstances :: Handle -> Bag InstInfo -> IO IdSet            -- The IdSet is the needed dfuns
208 ifaceInstances if_hdl inst_infos
209   | null togo_insts = return emptyVarSet                 
210   | otherwise       = hPutCol if_hdl pp_inst (sortLt lt_inst togo_insts) >>
211                       return needed_ids
212   where                          
213     togo_insts  = filter is_togo_inst (bagToList inst_infos)
214     needed_ids  = mkVarSet [dfun_id | InstInfo _ _ _ _ dfun_id _ _ _ <- togo_insts]
215     is_togo_inst (InstInfo _ _ _ _ dfun_id _ _ _) = isLocallyDefined dfun_id
216                                  
217     -------                      
218     lt_inst (InstInfo _ _ _ _ dfun_id1 _ _ _)
219             (InstInfo _ _ _ _ dfun_id2 _ _ _)
220       = getOccName dfun_id1 < getOccName dfun_id2
221         -- The dfuns are assigned names df1, df2, etc, in order of original textual
222         -- occurrence, and this makes as good a sort order as any
223
224     -------                      
225     pp_inst (InstInfo clas tvs tys theta dfun_id _ _ _)
226       = let                      
227                 -- The deNoteType is very important.   It removes all type
228                 -- synonyms from the instance type in interface files.
229                 -- That in turn makes sure that when reading in instance decls
230                 -- from interface files that the 'gating' mechanism works properly.
231                 -- Otherwise you could have
232                 --      type Tibble = T Int
233                 --      instance Foo Tibble where ...
234                 -- and this instance decl wouldn't get imported into a module
235                 -- that mentioned T but not Tibble.
236             forall_ty     = mkSigmaTy tvs theta (deNoteType (mkDictTy clas tys))
237             renumbered_ty = tidyTopType forall_ty
238         in                       
239         hcat [ptext SLIT("instance "), pprType renumbered_ty, 
240                     ptext SLIT(" = "), ppr_unqual_name dfun_id, semi]
241 \end{code}
242
243
244 %************************************************************************
245 %*                                                                      *
246 \subsection{Printing values}
247 %*                                                                      *
248 %************************************************************************
249
250 \begin{code}
251 ifaceId :: (Id -> IdInfo)               -- This function "knows" the extra info added
252                                         -- by the STG passes.  Sigh
253
254             -> IdSet                    -- Set of Ids that are needed by earlier interface
255                                         -- file emissions.  If the Id isn't in this set, and isn't
256                                         -- exported, there's no need to emit anything
257             -> Bool                     -- True <=> recursive, so don't print unfolding
258             -> Id
259             -> CoreExpr                 -- The Id's right hand side
260             -> Maybe (SDoc, IdSet)      -- The emitted stuff, plus a possibly-augmented set of needed Ids
261
262 ifaceId get_idinfo needed_ids is_rec id rhs
263   | not (id `elemVarSet` needed_ids ||          -- Needed [no id in needed_ids has omitIfaceSigForId]
264          (isExported id && not (omitIfaceSigForId id))) -- or exported and not to be omitted
265   = Nothing             -- Well, that was easy!
266
267 ifaceId get_idinfo needed_ids is_rec id rhs
268   = Just (hsep [sig_pretty, prag_pretty, char ';'], new_needed_ids)
269   where
270     idinfo         = get_idinfo id
271     inline_pragma  = inlinePragInfo idinfo
272
273     ty_pretty  = pprType (idType id)
274     sig_pretty = hsep [ppr (getOccName id), dcolon, ty_pretty]
275
276     prag_pretty 
277      | opt_OmitInterfacePragmas = empty
278      | otherwise                = hsep [ptext SLIT("{-##"),
279                                         arity_pretty, 
280                                         caf_pretty,
281                                         cpr_pretty,
282                                         strict_pretty, 
283                                         unfold_pretty, 
284                                         spec_pretty,
285                                         ptext SLIT("##-}")]
286
287     ------------  Arity  --------------
288     arity_pretty  = ppArityInfo (arityInfo idinfo)
289
290     ------------ Caf Info --------------
291     caf_pretty = ppCafInfo (cafInfo idinfo)
292
293     ------------ CPR Info --------------
294     cpr_pretty = ppCprInfo (cprInfo idinfo)
295
296     ------------  Strictness and Worker  --------------
297     strict_info   = strictnessInfo idinfo
298     work_info     = workerInfo idinfo
299     has_worker    = workerExists work_info
300     bottoming_fn  = isBottomingStrictness strict_info
301     strict_pretty = ppStrictnessInfo strict_info <+> wrkr_pretty
302
303     wrkr_pretty | not has_worker = empty
304                 | null con_list  = ppr work_id
305                 | otherwise      = ppr work_id <+> 
306                                    braces (hsep (map ppr con_list))
307
308 --    (Just work_id) = work_info
309 -- Temporary fix.  We can't use the worker id saved by the w/w
310 -- pass because later optimisations may have changed it.  So try
311 -- to snaffle from the wrapper code again ...
312     (work_id, wrapper_cons)   = getWorkerIdAndCons id rhs
313     con_list       = uniqSetToList wrapper_cons
314
315     ------------  Unfolding  --------------
316     unfold_pretty | show_unfold = unfold_herald <+> pprIfaceUnfolding rhs
317                   | otherwise   = empty
318
319     show_unfold = not has_worker        &&      -- Not unnecessary
320                   not bottoming_fn      &&      -- Not necessary
321                   unfolding_needed              -- Not dangerous
322
323     unfolding_needed =  case inline_pragma of
324                               IMustBeINLINEd    -> definitely_ok_to_unfold
325                               IWantToBeINLINEd  -> definitely_ok_to_unfold
326                               NoInlinePragInfo  -> rhs_is_small
327                               other             -> False
328
329
330     unfold_herald = case inline_pragma of
331                         NoInlinePragInfo -> ptext SLIT("__u")
332                         other            -> ppr inline_pragma
333
334     rhs_is_small = case calcUnfoldingGuidance opt_InterfaceUnfoldThreshold rhs of
335                         UnfoldNever -> False    -- Too big
336                         other       ->  definitely_ok_to_unfold -- Small enough
337
338     definitely_ok_to_unfold =  okToUnfoldInHiFile rhs
339
340     ------------  Specialisations --------------
341     spec_list = specEnvToList (getIdSpecialisation id)
342     spec_pretty = hsep (map pp_spec spec_list)
343     pp_spec (tyvars, tys, rhs) = hsep [ptext SLIT("__P"),
344                                        if null tyvars then ptext SLIT("[ ]")
345                                                       else brackets (interppSP tyvars),
346                                         -- The lexer interprets "[]" as a CONID.  Sigh.
347                                        hsep (map pprParendType tys),
348                                        ptext SLIT("="),
349                                        pprIfaceUnfolding rhs
350                                  ]
351     
352     ------------  Extra free Ids  --------------
353     new_needed_ids = (needed_ids `minusVarSet` unitVarSet id)   `unionVarSet` 
354                      extra_ids
355
356     extra_ids | opt_OmitInterfacePragmas = emptyVarSet
357               | otherwise                = worker_ids   `unionVarSet`
358                                            unfold_ids   `unionVarSet`
359                                            spec_ids
360
361     worker_ids | has_worker && interesting work_id = unitVarSet work_id
362                         -- Conceivably, the worker might come from
363                         -- another module
364                | otherwise                         = emptyVarSet
365
366     spec_ids = foldr add emptyVarSet spec_list
367              where
368                add (_, _, rhs) = unionVarSet (find_fvs rhs)
369
370     unfold_ids | show_unfold = find_fvs rhs
371                | otherwise   = emptyVarSet
372
373     find_fvs expr = free_vars
374                   where
375                     free_vars = exprSomeFreeVars interesting expr
376
377     interesting id = isId id && isLocallyDefined id &&
378                      not (omitIfaceSigForId id)
379 \end{code}
380
381 \begin{code}
382 ifaceBinds :: Handle
383            -> IdSet             -- These Ids are needed already
384            -> [Id]              -- Ids used at code-gen time; they have better pragma info!
385            -> [CoreBind]        -- In dependency order, later depend on earlier
386            -> IO ()
387
388 ifaceBinds hdl needed_ids final_ids binds
389   = mapIO (printForIface hdl) pretties >>
390     hPutStr hdl "\n"
391   where
392     final_id_map  = listToUFM [(id,id) | id <- final_ids]
393     get_idinfo id = case lookupUFM final_id_map id of
394                         Just id' -> idInfo id'
395                         Nothing  -> pprTrace "ifaceBinds not found:" (ppr id) $
396                                     idInfo id
397
398     pretties = go needed_ids (reverse binds)    -- Reverse so that later things will 
399                                                 -- provoke earlier ones to be emitted
400     go needed [] = if not (isEmptyVarSet needed) then
401                         pprTrace "ifaceBinds: free vars:" 
402                                   (sep (map ppr (varSetElems needed))) $
403                         []
404                    else
405                         []
406
407     go needed (NonRec id rhs : binds)
408         = case ifaceId get_idinfo needed False id rhs of
409                 Nothing                -> go needed binds
410                 Just (pretty, needed') -> pretty : go needed' binds
411
412         -- Recursive groups are a bit more of a pain.  We may only need one to
413         -- start with, but it may call out the next one, and so on.  So we
414         -- have to look for a fixed point.
415     go needed (Rec pairs : binds)
416         = pretties ++ go needed'' binds
417         where
418           (needed', pretties) = go_rec needed pairs
419           needed'' = needed' `minusVarSet` mkVarSet (map fst pairs)
420                 -- Later ones may spuriously cause earlier ones to be "needed" again
421
422     go_rec :: IdSet -> [(Id,CoreExpr)] -> (IdSet, [SDoc])
423     go_rec needed pairs
424         | null pretties = (needed, [])
425         | otherwise     = (final_needed, more_pretties ++ pretties)
426         where
427           reduced_pairs                 = [pair | (pair,Nothing) <- pairs `zip` maybes]
428           pretties                      = catMaybes maybes
429           (needed', maybes)             = mapAccumL do_one needed pairs
430           (final_needed, more_pretties) = go_rec needed' reduced_pairs
431
432           do_one needed (id,rhs) = case ifaceId get_idinfo needed True id rhs of
433                                         Nothing                -> (needed,  Nothing)
434                                         Just (pretty, needed') -> (needed', Just pretty)
435 \end{code}
436
437
438 %************************************************************************
439 %*                                                                      *
440 \subsection{Random small things}
441 %*                                                                      *
442 %************************************************************************
443
444 \begin{code}
445 ifaceTyCons hdl tycons   = hPutCol hdl upp_tycon (sortLt (<) (filter (for_iface_name . getName) tycons ))
446 ifaceClasses hdl classes = hPutCol hdl upp_class (sortLt (<) (filter (for_iface_name . getName) classes))
447
448 for_iface_name name = isLocallyDefined name && 
449                       not (isWiredInName name)
450
451 upp_tycon tycon = ifaceTyCon tycon
452 upp_class clas  = ifaceClass clas
453 \end{code}
454
455
456 \begin{code}
457 ifaceTyCon :: TyCon -> SDoc
458 ifaceTyCon tycon
459   | isSynTyCon tycon
460   = hsep [ ptext SLIT("type"),
461            ppr (getName tycon),
462            pprTyVarBndrs tyvars,
463            ptext SLIT("="),
464            ppr ty,
465            semi
466     ]
467   where
468     (tyvars, ty) = getSynTyConDefn tycon
469
470 ifaceTyCon tycon
471   | isAlgTyCon tycon
472   = hsep [ ptext keyword,
473            ppr_decl_context (tyConTheta tycon),
474            ppr (getName tycon),
475            pprTyVarBndrs (tyConTyVars tycon),
476            ptext SLIT("="),
477            hsep (punctuate (ptext SLIT(" | ")) (map ppr_con (tyConDataCons tycon))),
478            semi
479     ]
480   where
481     keyword | isNewTyCon tycon = SLIT("newtype")
482             | otherwise        = SLIT("data")
483
484     tyvars = tyConTyVars tycon
485
486     ppr_con data_con 
487         | null field_labels
488         = ASSERT( tycon == tycon1 && tyvars == tyvars1 )
489           hsep [  ppr_ex ex_tyvars ex_theta,
490                   ppr name,
491                   hsep (map ppr_arg_ty (strict_marks `zip` arg_tys))
492                 ]
493
494         | otherwise
495         = hsep [  ppr_ex ex_tyvars ex_theta,
496                   ppr name,
497                   braces $ hsep $ punctuate comma (map ppr_field (strict_marks `zip` field_labels))
498                 ]
499           where
500            (tyvars1, theta1, ex_tyvars, ex_theta, arg_tys, tycon1) = dataConSig data_con
501            field_labels   = dataConFieldLabels data_con
502            strict_marks   = dataConStrictMarks data_con
503            name           = getName            data_con
504
505     ppr_ex [] ex_theta = ASSERT( null ex_theta ) empty
506     ppr_ex ex_tvs ex_theta = ptext SLIT("__forall") <+> brackets (pprTyVarBndrs ex_tvs)
507                              <+> pprIfaceTheta ex_theta <+> ptext SLIT("=>")
508
509     ppr_arg_ty (strict_mark, ty) = ppr_strict_mark strict_mark <> pprParendType ty
510
511     ppr_strict_mark NotMarkedStrict        = empty
512     ppr_strict_mark (MarkedUnboxed _ _)    = ptext SLIT("! ! ")
513     ppr_strict_mark MarkedStrict           = ptext SLIT("! ")
514
515     ppr_field (strict_mark, field_label)
516         = hsep [ ppr (fieldLabelName field_label),
517                   dcolon,
518                   ppr_strict_mark strict_mark <> pprParendType (fieldLabelType field_label)
519                 ]
520
521 ifaceTyCon tycon
522   = pprPanic "pprIfaceTyDecl" (ppr tycon)
523
524 ifaceClass clas
525   = hsep [ptext SLIT("class"),
526            ppr_decl_context sc_theta,
527            ppr clas,                    -- Print the name
528            pprTyVarBndrs clas_tyvars,
529            pp_ops,
530            semi
531           ]
532    where
533      (clas_tyvars, sc_theta, _, sel_ids, defms) = classBigSig clas
534
535      pp_ops | null sel_ids  = empty
536             | otherwise = hsep [ptext SLIT("where"),
537                                  braces (hsep (punctuate semi (zipWith ppr_classop sel_ids defms)))
538                           ]
539
540      ppr_classop sel_id maybe_defm
541         = ASSERT( sel_tyvars == clas_tyvars)
542           hsep [ppr (getOccName sel_id),
543                 if maybeToBool maybe_defm then equals else empty,
544                 dcolon,
545                 ppr op_ty
546           ]
547         where
548           (sel_tyvars, _, op_ty) = splitSigmaTy (idType sel_id)
549
550 ppr_decl_context :: ThetaType -> SDoc
551 ppr_decl_context []    = empty
552 ppr_decl_context theta = pprIfaceTheta theta <+> ptext SLIT(" =>")
553
554 pprIfaceTheta :: ThetaType -> SDoc      -- Use braces rather than parens in interface files
555 pprIfaceTheta []    = empty
556 pprIfaceTheta theta = braces (hsep (punctuate comma [pprConstraint c tys | (c,tys) <- theta]))
557 \end{code}
558
559 %************************************************************************
560 %*                                                                      *
561 \subsection{Random small things}
562 %*                                                                      *
563 %************************************************************************
564
565 When printing export lists, we print like this:
566         Avail   f               f
567         AvailTC C [C, x, y]     C(x,y)
568         AvailTC C [x, y]        C!(x,y)         -- Exporting x, y but not C
569
570 \begin{code}
571 upp_avail :: AvailInfo -> SDoc
572 upp_avail (Avail name)      = pprOccName (getOccName name)
573 upp_avail (AvailTC name []) = empty
574 upp_avail (AvailTC name ns) = hcat [pprOccName (getOccName name), bang, upp_export ns']
575                             where
576                               bang | name `elem` ns = empty
577                                    | otherwise      = char '|'
578                               ns' = filter (/= name) ns
579
580 upp_export :: [Name] -> SDoc
581 upp_export []    = empty
582 upp_export names = braces (hsep (map (pprOccName . getOccName) names)) 
583
584 upp_fixity :: (Name, Fixity) -> SDoc
585 upp_fixity (name, fixity) = hsep [ptext SLIT("0"), ppr fixity, ppr name, semi]
586         -- Dummy version number!
587
588 ppr_unqual_name :: NamedThing a => a -> SDoc            -- Just its occurrence name
589 ppr_unqual_name name = pprOccName (getOccName name)
590 \end{code}
591
592
593 %************************************************************************
594 %*                                                                      *
595 \subsection{Comparisons
596 %*                                                                      *
597 %************************************************************************
598                                  
599
600 The various sorts above simply prevent unnecessary "wobbling" when
601 things change that don't have to.  We therefore compare lexically, not
602 by unique
603
604 \begin{code}
605 lt_avail :: AvailInfo -> AvailInfo -> Bool
606
607 a1 `lt_avail` a2 = availName a1 `lt_name` availName a2
608
609 lt_name :: Name -> Name -> Bool
610 n1 `lt_name` n2 = nameRdrName n1 < nameRdrName n2
611
612 lt_lexical :: NamedThing a => a -> a -> Bool
613 lt_lexical a1 a2 = getName a1 `lt_name` getName a2
614
615 lt_imp_vers :: ImportVersion a -> ImportVersion a -> Bool
616 lt_imp_vers (m1,_,_) (m2,_,_) = m1 < m2
617
618 sort_versions vs = sortLt lt_vers vs
619
620 lt_vers :: LocalVersion Name -> LocalVersion Name -> Bool
621 lt_vers (n1,v1) (n2,v2) = n1 `lt_name` n2
622 \end{code}
623
624
625 \begin{code}
626 hPutCol :: Handle 
627         -> (a -> SDoc)
628         -> [a]
629         -> IO ()
630 hPutCol hdl fmt xs = mapIO (printForIface hdl . fmt) xs
631
632 mapIO :: (a -> IO b) -> [a] -> IO ()
633 mapIO f []     = return ()
634 mapIO f (x:xs) = f x >> mapIO f xs
635 \end{code}