[project @ 1997-09-05 16:23:41 by simonpj]
[ghc-hetmet.git] / ghc / compiler / main / MkIface.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1996
3 %
4 \section[MkIface]{Print an interface for a module}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module MkIface (
10         startIface, endIface,
11         ifaceMain,
12         ifaceDecls
13     ) where
14
15 IMP_Ubiq(){-uitous-}
16 IMPORT_1_3(IO(Handle,hPutStr,openFile,hClose,IOMode(..)))
17
18 import HsSyn
19 import RdrHsSyn         ( RdrName(..) )
20 import RnHsSyn          ( SYN_IE(RenamedHsModule) )
21 import BasicTypes       ( Fixity(..), FixityDirection(..), NewOrData(..), IfaceFlavour(..) )
22 import RnMonad
23 import RnEnv            ( availName, ifaceFlavour )
24
25 import TcInstUtil       ( InstInfo(..) )
26
27 import CmdLineOpts
28 import Id               ( idType, dataConRawArgTys, dataConFieldLabels, 
29                           getIdInfo, getInlinePragma, omitIfaceSigForId,
30                           dataConStrictMarks, StrictnessMark(..), 
31                           SYN_IE(IdSet), idSetToList, unionIdSets, unitIdSet, minusIdSet, 
32                           isEmptyIdSet, elementOfIdSet, emptyIdSet, mkIdSet, pprId,
33                           GenId{-instance NamedThing/Outputable-}, SYN_IE(Id)
34
35                         )
36 import IdInfo           ( StrictnessInfo, ArityInfo, 
37                           arityInfo, ppArityInfo, strictnessInfo, ppStrictnessInfo, 
38                           workerExists, bottomIsGuaranteed, IdInfo
39                         )
40 import CoreSyn          ( SYN_IE(CoreExpr), SYN_IE(CoreBinding), GenCoreExpr, GenCoreBinding(..) )
41 import CoreUnfold       ( calcUnfoldingGuidance, UnfoldingGuidance(..), Unfolding )
42 import FreeVars         ( addExprFVs )
43 import WorkWrap         ( getWorkerIdAndCons )
44 import Name             ( isLocallyDefined, isWiredInName, modAndOcc, nameModule, pprOccName,
45                           OccName, occNameString, nameOccName, nameString, isExported,
46                           Name {-instance NamedThing-}, Provenance, NamedThing(..)
47                         )
48 import TyCon            ( TyCon {-instance NamedThing-},
49                           isSynTyCon, isAlgTyCon, isNewTyCon, tyConDataCons,
50                           tyConTheta, tyConTyVars,
51                           getSynTyConDefn
52                         )
53 import Class            ( GenClass(..){-instance NamedThing-}, SYN_IE(Class), classBigSig )
54 import FieldLabel       ( FieldLabel{-instance NamedThing-}, 
55                           fieldLabelName, fieldLabelType )
56 import Type             ( mkSigmaTy, mkDictTy, getAppTyCon, splitSigmaTy,
57                           mkTyVarTy, SYN_IE(Type)
58                         )
59 import TyVar            ( GenTyVar {- instance Eq -} )
60 import Unique           ( Unique {- instance Eq -} )
61
62 import PprEnv           -- not sure how much...
63 import Outputable       ( PprStyle(..), Outputable(..) )
64 import PprType
65 import PprCore          ( pprIfaceUnfolding )
66 import Pretty
67 import Outputable       ( printDoc )
68
69
70 import Bag              ( bagToList, isEmptyBag )
71 import Maybes           ( catMaybes, maybeToBool )
72 import FiniteMap        ( emptyFM, addToFM, addToFM_C, lookupFM, fmToList, eltsFM, FiniteMap )
73 import UniqFM           ( UniqFM, lookupUFM, listToUFM )
74 import Util             ( sortLt, zipWithEqual, zipWith3Equal, mapAccumL,
75                           assertPanic, panic{-ToDo:rm-}, pprTrace,
76                           pprPanic 
77                         )
78 \end{code}
79
80 We have a function @startIface@ to open the output file and put
81 (something like) ``interface Foo'' in it.  It gives back a handle
82 for subsequent additions to the interface file.
83
84 We then have one-function-per-block-of-interface-stuff, e.g.,
85 @ifaceExportList@ produces the @__exports__@ section; it appends
86 to the handle provided by @startIface@.
87
88 \begin{code}
89 startIface  :: Module
90             -> IO (Maybe Handle) -- Nothing <=> don't do an interface
91
92 ifaceMain   :: Maybe Handle
93             -> InterfaceDetails
94             -> IO ()
95
96
97 ifaceDecls :: Maybe Handle
98            -> [TyCon] -> [Class]
99            -> Bag InstInfo 
100            -> [Id]              -- Ids used at code-gen time; they have better pragma info!
101            -> [CoreBinding]     -- In dependency order, later depend on earlier
102            -> IO ()
103
104 endIface    :: Maybe Handle -> IO ()
105 \end{code}
106
107 \begin{code}
108 startIface mod
109   = case opt_ProduceHi of
110       Nothing -> return Nothing -- not producing any .hi file
111       Just fn ->
112         openFile fn WriteMode   >>= \ if_hdl ->
113         hPutStr if_hdl ("{-# GHC_PRAGMA INTERFACE VERSION 20 #-}\n_interface_ "++ _UNPK_ mod ++ "\n") >>
114         return (Just if_hdl)
115
116 endIface Nothing        = return ()
117 endIface (Just if_hdl)  = hPutStr if_hdl "\n" >> hClose if_hdl
118 \end{code}
119
120
121 \begin{code}
122 ifaceMain Nothing iface_stuff = return ()
123 ifaceMain (Just if_hdl)
124           (import_usages, ExportEnv avails fixities, instance_modules)
125   =
126     ifaceInstanceModules        if_hdl instance_modules         >>
127     ifaceUsages                 if_hdl import_usages            >>
128     ifaceExports                if_hdl avails                   >>
129     ifaceFixities               if_hdl fixities                 >>
130     return ()
131
132 ifaceDecls Nothing tycons classes inst_info final_ids simplified = return ()
133 ifaceDecls (Just hdl)
134            tycons classes
135            inst_infos
136            final_ids binds
137   | null_decls = return ()               
138         --  You could have a module with just (re-)exports/instances in it
139   | otherwise
140   = ifaceInstances hdl inst_infos               >>= \ needed_ids ->
141     hPutStr hdl "_declarations_\n"              >>
142     ifaceClasses hdl classes                    >>
143     ifaceTyCons hdl tycons                      >>
144     ifaceBinds hdl needed_ids final_ids binds   >>
145     return ()
146   where
147      null_decls = null binds      && 
148                   null tycons     &&
149                   null classes    && 
150                   isEmptyBag inst_infos
151 \end{code}
152
153 \begin{code}
154 ifaceUsages if_hdl import_usages
155   = hPutStr if_hdl "_usages_\n"   >>
156     hPutCol if_hdl upp_uses (sortLt lt_imp_vers import_usages)
157   where
158     upp_uses (m, hif, mv, versions)
159       = hsep [upp_module m, pp_hif hif, int mv, ptext SLIT("::"),
160               upp_import_versions (sort_versions versions)
161         ] <> semi
162
163         -- For imported versions we do print the version number
164     upp_import_versions nvs
165       = hsep [ hsep [ppr_unqual_name n, int v] | (n,v) <- nvs ]
166
167
168 ifaceInstanceModules if_hdl [] = return ()
169 ifaceInstanceModules if_hdl imods
170   = hPutStr if_hdl "_instance_modules_\n" >>
171     printDoc OneLineMode if_hdl (hsep (map ptext (sortLt (<) imods))) >>
172     hPutStr if_hdl "\n"
173
174 ifaceExports if_hdl [] = return ()
175 ifaceExports if_hdl avails
176   = hPutStr if_hdl "_exports_\n"                        >>
177     hPutCol if_hdl do_one_module (fmToList export_fm)
178   where
179         -- Sort them into groups by module
180     export_fm :: FiniteMap Module [AvailInfo]
181     export_fm = foldr insert emptyFM avails
182
183     insert NotAvailable efm = efm
184     insert avail efm = addToFM_C (++) efm mod [avail] 
185                      where
186                        mod = nameModule (availName avail)
187
188         -- Print one module's worth of stuff
189     do_one_module (mod_name, avails@(avail1:_))
190         = hsep [pp_hif (ifaceFlavour (availName avail1)), 
191                 upp_module mod_name,
192                 hsep (map upp_avail (sortLt lt_avail avails))
193           ] <> semi
194
195 -- The "!" indicates that the exported things came from a hi-boot interface 
196 pp_hif HiFile     = empty
197 pp_hif HiBootFile = char '!'
198
199 ifaceFixities if_hdl [] = return ()
200 ifaceFixities if_hdl fixities 
201   = hPutStr if_hdl "_fixities_\n"               >>
202     hPutCol if_hdl upp_fixity fixities
203 \end{code}                       
204
205 %************************************************************************
206 %*                                                                      *
207 \subsection{Instance declarations}
208 %*                                                                      *
209 %************************************************************************
210
211
212 \begin{code}                     
213 ifaceInstances :: Handle -> Bag InstInfo -> IO IdSet            -- The IdSet is the needed dfuns
214 ifaceInstances if_hdl inst_infos
215   | null togo_insts = return emptyIdSet          
216   | otherwise       = hPutStr if_hdl "_instances_\n" >>
217                       hPutCol if_hdl pp_inst (sortLt lt_inst togo_insts) >>
218                       return needed_ids
219   where                          
220     togo_insts  = filter is_togo_inst (bagToList inst_infos)
221     needed_ids  = mkIdSet [dfun_id | InstInfo _ _ _ _ _ dfun_id _ _ _ <- togo_insts]
222     is_togo_inst (InstInfo _ _ _ _ _ dfun_id _ _ _) = isLocallyDefined dfun_id
223                                  
224     -------                      
225     lt_inst (InstInfo _ _ _ _ _ dfun_id1 _ _ _)
226             (InstInfo _ _ _ _ _ dfun_id2 _ _ _)
227       = getOccName dfun_id1 < getOccName dfun_id2
228         -- The dfuns are assigned names df1, df2, etc, in order of original textual
229         -- occurrence, and this makes as good a sort order as any
230
231     -------                      
232     pp_inst (InstInfo clas tvs ty theta _ dfun_id _ _ _)
233       = let                      
234             forall_ty     = mkSigmaTy tvs theta (mkDictTy clas ty)
235             renumbered_ty = nmbrGlobalType forall_ty
236         in                       
237         hcat [ptext SLIT("instance "), ppr_ty renumbered_ty, 
238                     ptext SLIT(" = "), ppr_unqual_name dfun_id, semi]
239 \end{code}
240
241
242 %************************************************************************
243 %*                                                                      *
244 \subsection{Printing values}
245 %*                                                                      *
246 %************************************************************************
247
248 \begin{code}
249 ifaceId :: (Id -> IdInfo)               -- This function "knows" the extra info added
250                                         -- by the STG passes.  Sigh
251
252             -> IdSet                    -- Set of Ids that are needed by earlier interface
253                                         -- file emissions.  If the Id isn't in this set, and isn't
254                                         -- exported, there's no need to emit anything
255             -> Bool                     -- True <=> recursive, so don't print unfolding
256             -> Id
257             -> CoreExpr                 -- The Id's right hand side
258             -> Maybe (Doc, IdSet)       -- The emitted stuff, plus a possibly-augmented set of needed Ids
259
260 ifaceId get_idinfo needed_ids is_rec id rhs
261   | not (id `elementOfIdSet` needed_ids ||              -- Needed [no id in needed_ids has omitIfaceSigForId]
262          (isExported id && not (omitIfaceSigForId id))) -- or exported and not to be omitted
263   = Nothing             -- Well, that was easy!
264
265 ifaceId get_idinfo needed_ids is_rec id rhs
266   = Just (hsep [sig_pretty, pp_double_semi, prag_pretty], new_needed_ids)
267   where
268     pp_double_semi = ptext SLIT(";;")
269     idinfo         = get_idinfo id
270     inline_pragma  = getInlinePragma id 
271
272     ty_pretty  = pprType PprInterface (nmbrGlobalType (idType id))
273     sig_pretty = hcat [ppr PprInterface (getOccName id), ptext SLIT(" _:_ "), ty_pretty]
274
275     prag_pretty 
276      | opt_OmitInterfacePragmas = empty
277      | otherwise                = hsep [arity_pretty, strict_pretty, unfold_pretty, pp_double_semi]
278
279     ------------  Arity  --------------
280     arity_pretty  = ppArityInfo PprInterface (arityInfo idinfo)
281
282     ------------  Strictness  --------------
283     strict_info   = strictnessInfo idinfo
284     has_worker    = workerExists strict_info
285     strict_pretty = ppStrictnessInfo PprInterface strict_info <+> wrkr_pretty
286
287     wrkr_pretty | not has_worker = empty
288                 | null con_list  = pprId PprInterface work_id
289                 | otherwise      = pprId PprInterface work_id <+> braces (hsep (map (pprId PprInterface) con_list))
290
291     (work_id, wrapper_cons) = getWorkerIdAndCons id rhs
292     con_list               = idSetToList wrapper_cons
293
294     ------------  Unfolding  --------------
295     unfold_pretty | show_unfold = hsep [ptext SLIT("_U_"), pprIfaceUnfolding rhs]
296                   | otherwise   = empty
297
298     show_unfold = not implicit_unfolding &&             -- Not unnecessary
299                   not dodgy_unfolding                   -- Not dangerous
300
301     implicit_unfolding = has_worker ||
302                          bottomIsGuaranteed strict_info
303
304     dodgy_unfolding = case guidance of                  -- True <=> too big to show, or the Inline pragma
305                         UnfoldNever -> True             -- says it shouldn't be inlined
306                         other       -> False
307
308     guidance    = calcUnfoldingGuidance inline_pragma
309                                         opt_InterfaceUnfoldThreshold
310                                         rhs
311
312     
313     ------------  Extra free Ids  --------------
314     new_needed_ids = (needed_ids `minusIdSet` unitIdSet id)     `unionIdSets` 
315                      extra_ids
316
317     extra_ids | opt_OmitInterfacePragmas = emptyIdSet
318               | otherwise                = worker_ids   `unionIdSets`
319                                            unfold_ids
320
321     worker_ids | has_worker = unitIdSet work_id
322                | otherwise  = emptyIdSet
323
324     unfold_ids | show_unfold = free_vars
325                | otherwise   = emptyIdSet
326                              where
327                                (_,free_vars) = addExprFVs interesting emptyIdSet rhs
328                                interesting bound id = isLocallyDefined id &&
329                                                       not (id `elementOfIdSet` bound) &&
330                                                       not (omitIfaceSigForId id)
331 \end{code}
332
333 \begin{code}
334 ifaceBinds :: Handle
335            -> IdSet             -- These Ids are needed already
336            -> [Id]              -- Ids used at code-gen time; they have better pragma info!
337            -> [CoreBinding]     -- In dependency order, later depend on earlier
338            -> IO ()
339
340 ifaceBinds hdl needed_ids final_ids binds
341   = mapIO (printDoc OneLineMode hdl) pretties >>
342     hPutStr hdl "\n"
343   where
344     final_id_map  = listToUFM [(id,id) | id <- final_ids]
345     get_idinfo id = case lookupUFM final_id_map id of
346                         Just id' -> getIdInfo id'
347                         Nothing  -> pprTrace "ifaceBinds not found:" (ppr PprDebug id) $
348                                     getIdInfo id
349
350     pretties = go needed_ids (reverse binds)    -- Reverse so that later things will 
351                                                 -- provoke earlier ones to be emitted
352     go needed [] = if not (isEmptyIdSet needed) then
353                         pprTrace "ifaceBinds: free vars:" 
354                                   (sep (map (ppr PprDebug) (idSetToList needed))) $
355                         []
356                    else
357                         []
358
359     go needed (NonRec id rhs : binds)
360         = case ifaceId get_idinfo needed False id rhs of
361                 Nothing                -> go needed binds
362                 Just (pretty, needed') -> pretty : go needed' binds
363
364         -- Recursive groups are a bit more of a pain.  We may only need one to
365         -- start with, but it may call out the next one, and so on.  So we
366         -- have to look for a fixed point.
367     go needed (Rec pairs : binds)
368         = pretties ++ go needed'' binds
369         where
370           (needed', pretties) = go_rec needed pairs
371           needed'' = needed' `minusIdSet` mkIdSet (map fst pairs)
372                 -- Later ones may spuriously cause earlier ones to be "needed" again
373
374     go_rec :: IdSet -> [(Id,CoreExpr)] -> (IdSet, [Doc])
375     go_rec needed pairs
376         | null pretties = (needed, [])
377         | otherwise     = (final_needed, more_pretties ++ pretties)
378         where
379           reduced_pairs                 = [pair | (pair,Nothing) <- pairs `zip` maybes]
380           pretties                      = catMaybes maybes
381           (needed', maybes)             = mapAccumL do_one needed pairs
382           (final_needed, more_pretties) = go_rec needed' reduced_pairs
383
384           do_one needed (id,rhs) = case ifaceId get_idinfo needed True id rhs of
385                                         Nothing                -> (needed,  Nothing)
386                                         Just (pretty, needed') -> (needed', Just pretty)
387 \end{code}
388
389
390 %************************************************************************
391 %*                                                                      *
392 \subsection{Random small things}
393 %*                                                                      *
394 %************************************************************************
395
396 \begin{code}
397 ifaceTyCons hdl tycons   = hPutCol hdl upp_tycon (sortLt (<) (filter (for_iface_name . getName) tycons ))
398 ifaceClasses hdl classes = hPutCol hdl upp_class (sortLt (<) (filter (for_iface_name . getName) classes))
399
400 for_iface_name name = isLocallyDefined name && 
401                       not (isWiredInName name)
402
403 upp_tycon tycon = ifaceTyCon PprInterface tycon
404 upp_class clas  = ifaceClass PprInterface clas
405 \end{code}
406
407
408 \begin{code}
409 ifaceTyCon :: PprStyle -> TyCon -> Doc  
410
411 ifaceTyCon sty tycon
412   | isSynTyCon tycon
413   = hsep [ ptext SLIT("type"),
414            ppr sty (getName tycon),
415            hsep (map (pprTyVarBndr sty) tyvars),
416            ptext SLIT("="),
417            ppr sty ty,
418            semi
419     ]
420   where
421     (tyvars, ty) = getSynTyConDefn tycon
422
423 ifaceTyCon sty tycon
424   | isAlgTyCon tycon
425   = hsep [ ptext keyword,
426            ppr_decl_context sty (tyConTheta tycon),
427            ppr sty (getName tycon),
428            hsep (map (pprTyVarBndr sty) (tyConTyVars tycon)),
429            ptext SLIT("="),
430            hsep (punctuate (ptext SLIT(" | ")) (map ppr_con (tyConDataCons tycon))),
431            semi
432     ]
433   where
434     keyword | isNewTyCon tycon = SLIT("newtype")
435             | otherwise        = SLIT("data")
436
437     ppr_con data_con 
438         | null field_labels
439         = hsep [ ppr sty name,
440                   hsep (map ppr_arg_ty (strict_marks `zip` arg_tys))
441                 ]
442
443         | otherwise
444         = hsep [ ppr sty name,
445                   braces $ hsep $ punctuate comma (map ppr_field (strict_marks `zip` field_labels))
446                 ]
447           where
448            field_labels   = dataConFieldLabels data_con
449            arg_tys        = dataConRawArgTys   data_con
450            strict_marks   = dataConStrictMarks data_con
451            name           = getName            data_con
452
453     ppr_arg_ty (strict_mark, ty) = ppr_strict_mark strict_mark <> pprParendType sty ty
454
455     ppr_strict_mark NotMarkedStrict = empty
456     ppr_strict_mark MarkedStrict    = ptext SLIT("! ")
457                                 -- The extra space helps the lexical analyser that lexes
458                                 -- interface files; it doesn't make the rigid operator/identifier
459                                 -- distinction, so "!a" is a valid identifier so far as it is concerned
460
461     ppr_field (strict_mark, field_label)
462         = hsep [ ppr sty (fieldLabelName field_label),
463                   ptext SLIT("::"),
464                   ppr_strict_mark strict_mark <> pprParendType sty (fieldLabelType field_label)
465                 ]
466
467 ifaceTyCon sty tycon
468   = pprPanic "pprIfaceTyDecl" (ppr PprDebug tycon)
469
470 ifaceClass sty clas
471   = hsep [ptext SLIT("class"),
472            ppr_decl_context sty theta,
473            ppr sty clas,                        -- Print the name
474            pprTyVarBndr sty clas_tyvar,
475            pp_ops,
476            semi
477           ]
478    where
479      (clas_tyvar, super_classes, _, sel_ids, defms) = classBigSig clas
480      theta = super_classes `zip` repeat (mkTyVarTy clas_tyvar)
481
482      pp_ops | null sel_ids  = empty
483             | otherwise = hsep [ptext SLIT("where"),
484                                  braces (hsep (punctuate semi (zipWith ppr_classop sel_ids defms)))
485                           ]
486
487      ppr_classop sel_id maybe_defm
488         = ASSERT( sel_tyvars == [clas_tyvar])
489           hsep [ppr sty (getOccName sel_id),
490                 if maybeToBool maybe_defm then equals else empty,
491                 ptext SLIT("::"),
492                 ppr sty op_ty
493           ]
494         where
495           (sel_tyvars, _, op_ty) = splitSigmaTy (idType sel_id)
496
497 ppr_decl_context :: PprStyle -> [(Class,Type)] -> Doc
498 ppr_decl_context sty [] = empty
499 ppr_decl_context sty theta
500   = braces (hsep (punctuate comma (map (ppr_dict) theta)))
501     <> 
502     ptext SLIT(" =>")
503   where
504     ppr_dict (clas,ty) = hsep [ppr sty clas, ppr sty ty]
505 \end{code}
506
507 %************************************************************************
508 %*                                                                      *
509 \subsection{Random small things}
510 %*                                                                      *
511 %************************************************************************
512
513 When printing export lists, we print like this:
514         Avail   f               f
515         AvailTC C [C, x, y]     C(x,y)
516         AvailTC C [x, y]        C!(x,y)         -- Exporting x, y but not C
517
518 \begin{code}
519 upp_avail NotAvailable      = empty
520 upp_avail (Avail name)      = upp_occname (getOccName name)
521 upp_avail (AvailTC name []) = empty
522 upp_avail (AvailTC name ns) = hcat [upp_occname (getOccName name), bang, upp_export ns']
523                             where
524                               bang | name `elem` ns = empty
525                                    | otherwise      = char '|'
526                               ns' = filter (/= name) ns
527
528 upp_export []    = empty
529 upp_export names = parens (hsep (map (upp_occname . getOccName) names)) 
530
531 upp_fixity (occ, (Fixity prec dir, prov)) = hcat [upp_dir dir, space, 
532                                                         int prec, space, 
533                                                         upp_occname occ, semi]
534 upp_dir InfixR = ptext SLIT("infixr")
535 upp_dir InfixL = ptext SLIT("infixl")
536 upp_dir InfixN = ptext SLIT("infix")
537
538 ppr_unqual_name :: NamedThing a => a -> Doc             -- Just its occurrence name
539 ppr_unqual_name name = upp_occname (getOccName name)
540
541 ppr_name :: NamedThing a => a -> Doc            -- Its full name
542 ppr_name   n = ptext (nameString (getName n))
543
544 upp_occname :: OccName -> Doc
545 upp_occname occ = ptext (occNameString occ)
546
547 upp_module :: Module -> Doc
548 upp_module mod = ptext mod
549
550 uppSemid   x = ppr PprInterface x <> semi -- micro util
551
552 ppr_ty    ty = pprType PprInterface ty
553 ppr_tyvar tv = ppr PprInterface tv
554 ppr_tyvar_bndr tv = pprTyVarBndr PprInterface tv
555
556 ppr_decl decl = ppr PprInterface decl <> semi
557 \end{code}
558
559
560 %************************************************************************
561 %*                                                                      *
562 \subsection{Comparisons
563 %*                                                                      *
564 %************************************************************************
565                                  
566
567 The various sorts above simply prevent unnecessary "wobbling" when
568 things change that don't have to.  We therefore compare lexically, not
569 by unique
570
571 \begin{code}
572 lt_avail :: AvailInfo -> AvailInfo -> Bool
573
574 a1 `lt_avail` a2 = availName a1 `lt_name` availName a2
575
576 lt_name :: Name -> Name -> Bool
577 n1 `lt_name` n2 = modAndOcc n1 < modAndOcc n2
578
579 lt_lexical :: NamedThing a => a -> a -> Bool
580 lt_lexical a1 a2 = getName a1 `lt_name` getName a2
581
582 lt_imp_vers :: ImportVersion a -> ImportVersion a -> Bool
583 lt_imp_vers (m1,_,_,_) (m2,_,_,_) = m1 < m2
584
585 sort_versions vs = sortLt lt_vers vs
586
587 lt_vers :: LocalVersion Name -> LocalVersion Name -> Bool
588 lt_vers (n1,v1) (n2,v2) = n1 `lt_name` n2
589 \end{code}
590
591
592 \begin{code}
593 hPutCol :: Handle 
594         -> (a -> Doc)
595         -> [a]
596         -> IO ()
597 hPutCol hdl fmt xs = mapIO (printDoc OneLineMode hdl . fmt) xs
598
599 mapIO :: (a -> IO b) -> [a] -> IO ()
600 mapIO f []     = return ()
601 mapIO f (x:xs) = f x >> mapIO f xs
602 \end{code}