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