[project @ 2000-10-24 07:35:00 by simonpj]
[ghc-hetmet.git] / ghc / compiler / hsSyn / HsDecls.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[HsDecls]{Abstract syntax: global declarations}
5
6 Definitions for: @TyDecl@ and @ConDecl@, @ClassDecl@,
7 @InstDecl@, @DefaultDecl@ and @ForeignDecl@.
8
9 \begin{code}
10 module HsDecls (
11         HsDecl(..), TyClDecl(..), InstDecl(..), RuleDecl(..), RuleBndr(..),
12         DefaultDecl(..), ForeignDecl(..), ForKind(..),
13         ExtName(..), isDynamicExtName, extNameStatic,
14         ConDecl(..), ConDetails(..), 
15         BangType(..), getBangType,
16         IfaceSig(..),  
17         DeprecDecl(..), DeprecTxt,
18         hsDeclName, instDeclName, tyClDeclName, tyClDeclNames,
19         isClassDecl, isSynDecl, isDataDecl, countTyClDecls, toHsRule,
20         mkClassDeclSysNames,
21         getClassDeclSysNames
22     ) where
23
24 #include "HsVersions.h"
25
26 -- friends:
27 import HsBinds          ( HsBinds, MonoBinds, Sig(..), FixitySig(..) )
28 import HsExpr           ( HsExpr )
29 import HsTypes
30 import PprCore          ( pprCoreRule )
31 import HsCore           ( UfExpr(UfVar), UfBinder, IfaceSig(..), ifaceSigName,
32                           eq_ufBinders, eq_ufExpr, pprUfExpr, toUfExpr, toUfBndr
33                         )
34 import CoreSyn          ( CoreRule(..) )
35 import BasicTypes       ( NewOrData(..) )
36 import CallConv         ( CallConv, pprCallConv )
37 import Name             ( getName )
38
39 -- others:
40 import FunDeps          ( pprFundeps )
41 import Class            ( FunDep )
42 import CStrings         ( CLabelString, pprCLabelString )
43 import Outputable       
44 import SrcLoc           ( SrcLoc, noSrcLoc )
45 \end{code}
46
47
48 %************************************************************************
49 %*                                                                      *
50 \subsection[HsDecl]{Declarations}
51 %*                                                                      *
52 %************************************************************************
53
54 \begin{code}
55 data HsDecl name pat
56   = TyClD       (TyClDecl name pat)
57   | InstD       (InstDecl  name pat)
58   | DefD        (DefaultDecl name)
59   | ValD        (HsBinds name pat)
60   | ForD        (ForeignDecl name)
61   | SigD        (IfaceSig name)
62   | FixD        (FixitySig name)
63   | DeprecD     (DeprecDecl name)
64   | RuleD       (RuleDecl name pat)
65
66 -- NB: all top-level fixity decls are contained EITHER
67 -- EITHER FixDs
68 -- OR     in the ClassDecls in TyClDs
69 --
70 -- The former covers
71 --      a) data constructors
72 --      b) class methods (but they can be also done in the
73 --              signatures of class decls)
74 --      c) imported functions (that have an IfacSig)
75 --      d) top level decls
76 --
77 -- The latter is for class methods only
78 \end{code}
79
80 \begin{code}
81 #ifdef DEBUG
82 hsDeclName :: (Outputable name, Outputable pat)
83            => HsDecl name pat -> name
84 #endif
85 hsDeclName (TyClD decl)                             = tyClDeclName decl
86 hsDeclName (InstD   decl)                           = instDeclName decl
87 hsDeclName (SigD    decl)                           = ifaceSigName decl
88 hsDeclName (ForD    (ForeignDecl name _ _ _ _ _))   = name
89 hsDeclName (FixD    (FixitySig name _ _))           = name
90 -- Others don't make sense
91 #ifdef DEBUG
92 hsDeclName x                                  = pprPanic "HsDecls.hsDeclName" (ppr x)
93 #endif
94
95
96 instDeclName :: InstDecl name pat -> name
97 instDeclName (InstDecl _ _ _ (Just name) _) = name
98
99 \end{code}
100
101 \begin{code}
102 instance (Outputable name, Outputable pat)
103         => Outputable (HsDecl name pat) where
104
105     ppr (TyClD dcl)  = ppr dcl
106     ppr (SigD sig)   = ppr sig
107     ppr (ValD binds) = ppr binds
108     ppr (DefD def)   = ppr def
109     ppr (InstD inst) = ppr inst
110     ppr (ForD fd)    = ppr fd
111     ppr (FixD fd)    = ppr fd
112     ppr (RuleD rd)   = ppr rd
113     ppr (DeprecD dd) = ppr dd
114 \end{code}
115
116 \begin{code}
117 instance Ord name => Eq (HsDecl name pat) where
118         -- Used only when comparing interfaces, 
119         -- at which time only signature and type/class decls
120    (SigD s1)  == (SigD s2)  = s1 == s2
121    (TyClD d1) == (TyClD d2) = d1 == d2
122    _          == _          = False
123 \end{code}
124
125
126 %************************************************************************
127 %*                                                                      *
128 \subsection[TyDecl]{@data@, @newtype@ or @type@ (synonym) type declaration}
129 %*                                                                      *
130 %************************************************************************
131
132 Type and class declarations carry 'implicit names'.  In particular:
133
134 Type A.  
135 ~~~~~~~
136   Each data type decl defines 
137         a worker name for each constructor
138         to-T and from-T convertors
139   Each class decl defines
140         a tycon for the class
141         a data constructor for that tycon
142         the worker for that constructor
143         a selector for each superclass
144
145 All have occurrence names that are derived uniquely from their parent declaration.
146
147 None of these get separate definitions in an interface file; they are
148 fully defined by the data or class decl.  But they may *occur* in
149 interface files, of course.  Any such occurrence must haul in the
150 relevant type or class decl.
151
152 Plan of attack:
153  - Make up their occurrence names immediately
154
155  - Ensure they "point to" the parent data/class decl 
156    when loading that decl from an interface file
157
158  - When renaming the decl look them up in the name cache,
159    ensure correct module and provenance is set
160
161 Type B: Default methods and dictionary functions
162 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
163 Have their own binding in an interface file.
164
165 Default methods : occurrence name is derived uniquely from the class decl.
166 Dict functions  : occurrence name is derived from the instance decl, plus a unique number.
167
168 Plan of attack: 
169   - Do *not* make them point to the parent class decl
170   - Interface-file decls: treat just like Type A
171   - Source-file decls:    the names aren't in the decl at all; 
172                           instead the typechecker makes them up
173
174 \begin{code}
175 data TyClDecl name pat
176   = TyData      NewOrData
177                 (HsContext name) -- context
178                 name             -- type constructor
179                 [HsTyVarBndr name]       -- type variables
180                 [ConDecl name]   -- data constructors (empty if abstract)
181                 Int              -- Number of data constructors (valid even if type is abstract)
182                 (Maybe [name])   -- derivings; Nothing => not specified
183                                  -- (i.e., derive default); Just [] => derive
184                                  -- *nothing*; Just <list> => as you would
185                                  -- expect...
186                 SrcLoc
187                 name             -- generic converter functions
188                 name             -- generic converter functions
189
190   | TySynonym   name                    -- type constructor
191                 [HsTyVarBndr name]      -- type variables
192                 (HsType name)           -- synonym expansion
193                 SrcLoc
194
195   | ClassDecl   (HsContext name)        -- context...
196                 name                    -- name of the class
197                 [HsTyVarBndr name]      -- the class type variables
198                 [FunDep name]           -- functional dependencies
199                 [Sig name]              -- methods' signatures
200                 (MonoBinds name pat)    -- default methods
201                 (ClassDeclSysNames name)
202                 SrcLoc
203
204 tyClDeclName :: TyClDecl name pat -> name
205 tyClDeclName (TyData _ _ name _ _ _ _ _ _ _) = name
206 tyClDeclName (TySynonym name _ _ _)          = name
207 tyClDeclName (ClassDecl _ name _ _ _ _ _ _)  = name
208
209
210 tyClDeclNames :: Eq name => TyClDecl name pat -> [(name, SrcLoc)]
211 -- Returns all the binding names of the decl, along with their SrcLocs
212 -- The first one is guaranteed to be the name of the decl
213 -- For record fields, the first one counts as the SrcLoc
214 -- We use the equality to filter out duplicate field names
215
216 tyClDeclNames (TySynonym name _ _ loc)
217   = [(name,loc)]
218
219 tyClDeclNames (ClassDecl _ name _ _ sigs _ _ loc)
220   = (name,loc) : [(name,loc) | ClassOpSig n _ _ loc <- sigs]
221
222 tyClDeclNames (TyData _ _ name _ cons _ _ loc _ _)
223   = (name,loc) : conDeclsNames cons
224
225
226 type ClassDeclSysNames name = [name]
227         --      [tycon, datacon wrapper, datacon worker, 
228         --       superclass selector 1, ..., superclass selector n]
229         -- They are kept in a list rather than a tuple to make the
230         -- renamer easier.
231
232 mkClassDeclSysNames  :: (name, name, name, [name]) -> [name]
233 getClassDeclSysNames :: [name] -> (name, name, name, [name])
234 mkClassDeclSysNames  (a,b,c,ds) = a:b:c:ds
235 getClassDeclSysNames (a:b:c:ds) = (a,b,c,ds)
236 \end{code}
237
238
239 \begin{code}
240 isDataDecl, isSynDecl, isClassDecl :: TyClDecl name pat -> Bool
241
242 isSynDecl (TySynonym _ _ _ _) = True
243 isSynDecl other               = False
244
245 isDataDecl (TyData _ _ _ _ _ _ _ _ _ _) = True
246 isDataDecl other                        = False
247
248 isClassDecl (ClassDecl _ _ _ _ _ _ _ _ ) = True
249 isClassDecl other                        = False
250 \end{code}
251
252 \begin{code}
253 instance Ord name => Eq (TyClDecl name pat) where
254         -- Used only when building interface files
255   (==) (TyData nd1 cxt1 n1 tvs1 cons1 _ _ _ _ _)
256        (TyData nd2 cxt2 n2 tvs2 cons2 _ _ _ _ _)
257     = n1 == n2 &&
258       nd1 == nd2 &&
259       eqWithHsTyVars tvs1 tvs2 (\ env -> 
260           eq_hsContext env cxt1 cxt2  &&
261           eqListBy (eq_ConDecl env) cons1 cons2
262       )
263
264   (==) (TySynonym n1 tvs1 ty1 _)
265        (TySynonym n2 tvs2 ty2 _)
266     =  n1 == n2 &&
267        eqWithHsTyVars tvs1 tvs2 (\ env -> eq_hsType env ty1 ty2)
268
269   (==) (ClassDecl cxt1 n1 tvs1 fds1 sigs1 _ _ _ )
270        (ClassDecl cxt2 n2 tvs2 fds2 sigs2 _ _ _ )
271     =  n1 == n2 &&
272        eqWithHsTyVars tvs1 tvs2 (\ env -> 
273           eq_hsContext env cxt1 cxt2 &&
274           eqListBy (eq_hsFD env) fds1 fds2 &&
275           eqListBy (eq_cls_sig env) sigs1 sigs2
276        )
277
278   (==) _ _ = False      -- default case
279
280
281 eq_hsFD env (ns1,ms1) (ns2,ms2)
282   = eqListBy (eq_hsVar env) ns1 ns2 && eqListBy (eq_hsVar env) ms1 ms2
283
284 eq_cls_sig env (ClassOpSig n1 dm1 ty1 _) (ClassOpSig n2 dm2 ty2 _)
285   = n1==n2 && dm1 `eq_dm` dm2 && eq_hsType env ty1 ty2
286   where
287         -- Ignore the name of the default method.
288         -- This is used for comparing declarations before putting
289         -- them into interface files, and the name of the default 
290         -- method isn't relevant
291     (Just (explicit_dm1)) `eq_dm` (Just (explicit_dm2)) = explicit_dm1 == explicit_dm2
292     Nothing                 `eq_dm` Nothing                 = True
293     dm1                     `eq_dm` dm2                     = False
294 \end{code}
295
296 \begin{code}
297 countTyClDecls :: [TyClDecl name pat] -> (Int, Int, Int, Int)
298         -- class, data, newtype, synonym decls
299 countTyClDecls decls 
300  = (length [() | ClassDecl _ _ _ _ _ _ _ _         <- decls],
301     length [() | TyData DataType _ _ _ _ _ _ _ _ _ <- decls],
302     length [() | TyData NewType  _ _ _ _ _ _ _ _ _ <- decls],
303     length [() | TySynonym _ _ _ _                 <- decls])
304 \end{code}
305
306 \begin{code}
307 instance (Outputable name, Outputable pat)
308               => Outputable (TyClDecl name pat) where
309
310     ppr (TySynonym tycon tyvars mono_ty src_loc)
311       = hang (ptext SLIT("type") <+> pp_decl_head [] tycon tyvars <+> equals)
312              4 (ppr mono_ty)
313
314     ppr (TyData new_or_data context tycon tyvars condecls ncons 
315                 derivings src_loc gen_conv1 gen_conv2) -- The generic names are not printed out ATM
316       = pp_tydecl
317                   (ptext keyword <+> pp_decl_head context tycon tyvars <+> equals)
318                   (pp_condecls condecls ncons)
319                   derivings
320       where
321         keyword = case new_or_data of
322                         NewType  -> SLIT("newtype")
323                         DataType -> SLIT("data")
324
325     ppr (ClassDecl context clas tyvars fds sigs methods _ src_loc)
326       | null sigs       -- No "where" part
327       = top_matter
328
329       | otherwise       -- Laid out
330       = sep [hsep [top_matter, ptext SLIT("where {")],
331              nest 4 (sep [sep (map ppr_sig sigs), pp_methods, char '}'])]
332       where
333         top_matter  = ptext SLIT("class") <+> pp_decl_head context clas tyvars <+> pprFundeps fds
334         ppr_sig sig = ppr sig <> semi
335         pp_methods = getPprStyle $ \ sty ->
336                      if ifaceStyle sty then empty else ppr methods
337         
338 pp_decl_head :: Outputable name => HsContext name -> name -> [HsTyVarBndr name] -> SDoc
339 pp_decl_head context thing tyvars = hsep [pprHsContext context, ppr thing, interppSP tyvars]
340
341 pp_condecls []     ncons = ptext SLIT("{- abstract with") <+> int ncons <+> ptext SLIT("constructors -}")
342 pp_condecls (c:cs) ncons = sep (ppr c : map (\ c -> ptext SLIT("|") <+> ppr c) cs)
343
344 pp_tydecl pp_head pp_decl_rhs derivings
345   = hang pp_head 4 (sep [
346         pp_decl_rhs,
347         case derivings of
348           Nothing          -> empty
349           Just ds          -> hsep [ptext SLIT("deriving"), parens (interpp'SP ds)]
350     ])
351 \end{code}
352
353
354 %************************************************************************
355 %*                                                                      *
356 \subsection[ConDecl]{A data-constructor declaration}
357 %*                                                                      *
358 %************************************************************************
359
360 \begin{code}
361 data ConDecl name
362   = ConDecl     name                    -- Constructor name; this is used for the
363                                         -- DataCon itself, and for the user-callable wrapper Id
364
365                 name                    -- Name of the constructor's 'worker Id'
366                                         -- Filled in as the ConDecl is built
367
368                 [HsTyVarBndr name]              -- Existentially quantified type variables
369                 (HsContext name)        -- ...and context
370                                         -- If both are empty then there are no existentials
371
372                 (ConDetails name)
373                 SrcLoc
374
375 data ConDetails name
376   = VanillaCon                  -- prefix-style con decl
377                 [BangType name]
378
379   | InfixCon                    -- infix-style con decl
380                 (BangType name)
381                 (BangType name)
382
383   | RecCon                      -- record-style con decl
384                 [([name], BangType name)]       -- list of "fields"
385 \end{code}
386
387 \begin{code}
388 conDeclsNames :: Eq name => [ConDecl name] -> [(name,SrcLoc)]
389   -- See tyClDeclNames for what this does
390   -- The function is boringly complicated because of the records
391   -- And since we only have equality, we have to be a little careful
392 conDeclsNames cons
393   = snd (foldl do_one ([], []) cons)
394   where
395     do_one (flds_seen, acc) (ConDecl name _ _ _ details loc)
396         = do_details ((name,loc):acc) details
397         where
398           do_details acc (RecCon flds) = foldl do_fld (flds_seen, acc) flds
399           do_details acc other         = (flds_seen, acc)
400
401           do_fld acc (flds, _) = foldl do_fld1 acc flds
402
403           do_fld1 (flds_seen, acc) fld
404                 | fld `elem` flds_seen = (flds_seen,acc)
405                 | otherwise            = (fld:flds_seen, (fld,loc):acc)
406 \end{code}
407
408 \begin{code}
409 eq_ConDecl env (ConDecl n1 _ tvs1 cxt1 cds1 _)
410                (ConDecl n2 _ tvs2 cxt2 cds2 _)
411   = n1 == n2 &&
412     (eqWithHsTyVars tvs1 tvs2   $ \ env ->
413      eq_hsContext env cxt1 cxt2 &&
414      eq_ConDetails env cds1 cds2)
415
416 eq_ConDetails env (VanillaCon bts1) (VanillaCon bts2)
417   = eqListBy (eq_btype env) bts1 bts2
418 eq_ConDetails env (InfixCon bta1 btb1) (InfixCon bta2 btb2)
419   = eq_btype env bta1 bta2 && eq_btype env btb1 btb2
420 eq_ConDetails env (RecCon fs1) (RecCon fs2)
421   = eqListBy (eq_fld env) fs1 fs2
422 eq_ConDetails env _ _ = False
423
424 eq_fld env (ns1,bt1) (ns2, bt2) = ns1==ns2 && eq_btype env bt1 bt2
425 \end{code}
426   
427 \begin{code}
428 data BangType name
429   = Banged   (HsType name)      -- HsType: to allow Haskell extensions
430   | Unbanged (HsType name)      -- (MonoType only needed for straight Haskell)
431   | Unpacked (HsType name)      -- Field is strict and to be unpacked if poss.
432
433 getBangType (Banged ty)   = ty
434 getBangType (Unbanged ty) = ty
435 getBangType (Unpacked ty) = ty
436
437 eq_btype env (Banged t1)   (Banged t2)   = eq_hsType env t1 t2
438 eq_btype env (Unbanged t1) (Unbanged t2) = eq_hsType env t1 t2
439 eq_btype env (Unpacked t1) (Unpacked t2) = eq_hsType env t1 t2
440 eq_btype env _             _             = False
441 \end{code}
442
443 \begin{code}
444 instance (Outputable name) => Outputable (ConDecl name) where
445     ppr (ConDecl con _ tvs cxt con_details  loc)
446       = sep [pprHsForAll tvs cxt, ppr_con_details con con_details]
447
448 ppr_con_details con (InfixCon ty1 ty2)
449   = hsep [ppr_bang ty1, ppr con, ppr_bang ty2]
450
451 ppr_con_details con (VanillaCon tys)
452   = ppr con <+> hsep (map (ppr_bang) tys)
453
454 ppr_con_details con (RecCon fields)
455   = ppr con <+> braces (hsep (punctuate comma (map ppr_field fields)))
456   where
457     ppr_field (ns, ty) = hsep (map (ppr) ns) <+> 
458                          dcolon <+>
459                          ppr_bang ty
460
461 instance Outputable name => Outputable (BangType name) where
462     ppr = ppr_bang
463
464 ppr_bang (Banged   ty) = ptext SLIT("!") <> pprParendHsType ty
465 ppr_bang (Unbanged ty) = pprParendHsType ty
466 ppr_bang (Unpacked ty) = ptext SLIT("! !") <> pprParendHsType ty
467 \end{code}
468
469
470 %************************************************************************
471 %*                                                                      *
472 \subsection[InstDecl]{An instance declaration
473 %*                                                                      *
474 %************************************************************************
475
476 \begin{code}
477 data InstDecl name pat
478   = InstDecl    (HsType name)   -- Context => Class Instance-type
479                                 -- Using a polytype means that the renamer conveniently
480                                 -- figures out the quantified type variables for us.
481
482                 (MonoBinds name pat)
483
484                 [Sig name]              -- User-supplied pragmatic info
485
486                 (Maybe name)            -- Name for the dictionary function
487                                         -- Nothing for source-file instance decls
488
489                 SrcLoc
490 \end{code}
491
492 \begin{code}
493 instance (Outputable name, Outputable pat)
494               => Outputable (InstDecl name pat) where
495
496     ppr (InstDecl inst_ty binds uprags maybe_dfun_name src_loc)
497       = getPprStyle $ \ sty ->
498         if ifaceStyle sty then
499            hsep [ptext SLIT("instance"), ppr inst_ty, equals, pp_dfun]
500         else
501            vcat [hsep [ptext SLIT("instance"), ppr inst_ty, ptext SLIT("where")],
502                  nest 4 (ppr uprags),
503                  nest 4 (ppr binds) ]
504       where
505         pp_dfun = case maybe_dfun_name of
506                     Just df -> ppr df
507                     Nothing -> empty
508 \end{code}
509
510 \begin{code}
511 instance Ord name => Eq (InstDecl name pat) where
512         -- Used for interface comparison only, so don't compare bindings
513   (==) (InstDecl inst_ty1 _ _ dfun1 _) (InstDecl inst_ty2 _ _ dfun2 _)
514        = inst_ty1 == inst_ty2 && dfun1 == dfun2
515 \end{code}
516
517
518 %************************************************************************
519 %*                                                                      *
520 \subsection[DefaultDecl]{A @default@ declaration}
521 %*                                                                      *
522 %************************************************************************
523
524 There can only be one default declaration per module, but it is hard
525 for the parser to check that; we pass them all through in the abstract
526 syntax, and that restriction must be checked in the front end.
527
528 \begin{code}
529 data DefaultDecl name
530   = DefaultDecl [HsType name]
531                 SrcLoc
532
533 instance (Outputable name)
534               => Outputable (DefaultDecl name) where
535
536     ppr (DefaultDecl tys src_loc)
537       = ptext SLIT("default") <+> parens (interpp'SP tys)
538 \end{code}
539
540 %************************************************************************
541 %*                                                                      *
542 \subsection{Foreign function interface declaration}
543 %*                                                                      *
544 %************************************************************************
545
546 \begin{code}
547 data ForeignDecl name = 
548    ForeignDecl 
549         name 
550         ForKind   
551         (HsType name)
552         ExtName
553         CallConv
554         SrcLoc
555
556 instance (Outputable name)
557               => Outputable (ForeignDecl name) where
558
559     ppr (ForeignDecl nm imp_exp ty ext_name cconv src_loc)
560       = ptext SLIT("foreign") <+> ppr_imp_exp <+> pprCallConv cconv <+> 
561         ppr ext_name <+> ppr_unsafe <+> ppr nm <+> dcolon <+> ppr ty
562         where
563          (ppr_imp_exp, ppr_unsafe) =
564            case imp_exp of
565              FoLabel     -> (ptext SLIT("label"), empty)
566              FoExport    -> (ptext SLIT("export"), empty)
567              FoImport us 
568                 | us        -> (ptext SLIT("import"), ptext SLIT("unsafe"))
569                 | otherwise -> (ptext SLIT("import"), empty)
570
571 data ForKind
572  = FoLabel
573  | FoExport
574  | FoImport Bool -- True  => unsafe call.
575
576 data ExtName
577  = Dynamic 
578  | ExtName CLabelString         -- The external name of the foreign thing,
579            (Maybe CLabelString) -- and optionally its DLL or module name
580                                 -- Both of these are completely unencoded; 
581                                 -- we just print them as they are
582
583 isDynamicExtName :: ExtName -> Bool
584 isDynamicExtName Dynamic = True
585 isDynamicExtName _       = False
586
587 extNameStatic :: ExtName -> CLabelString
588 extNameStatic (ExtName f _) = f
589 extNameStatic Dynamic       = panic "staticExtName: Dynamic - shouldn't ever happen."
590
591 instance Outputable ExtName where
592   ppr Dynamic      = ptext SLIT("dynamic")
593   ppr (ExtName nm mb_mod) = 
594      case mb_mod of { Nothing -> empty; Just m -> doubleQuotes (ptext m) } <+> 
595      doubleQuotes (pprCLabelString nm)
596 \end{code}
597
598 %************************************************************************
599 %*                                                                      *
600 \subsection{Transformation rules}
601 %*                                                                      *
602 %************************************************************************
603
604 \begin{code}
605 data RuleDecl name pat
606   = HsRule                      -- Source rule
607         FAST_STRING             -- Rule name
608         [name]                  -- Forall'd tyvars, filled in by the renamer with
609                                 -- tyvars mentioned in sigs; then filled out by typechecker
610         [RuleBndr name]         -- Forall'd term vars
611         (HsExpr name pat)       -- LHS
612         (HsExpr name pat)       -- RHS
613         SrcLoc          
614
615   | IfaceRule                   -- One that's come in from an interface file; pre-typecheck
616         FAST_STRING
617         [UfBinder name]         -- Tyvars and term vars
618         name                    -- Head of lhs
619         [UfExpr name]           -- Args of LHS
620         (UfExpr name)           -- Pre typecheck
621         SrcLoc          
622
623   | IfaceRuleOut                -- Post typecheck
624         name                    -- Head of LHS
625         CoreRule
626
627
628 data RuleBndr name
629   = RuleBndr name
630   | RuleBndrSig name (HsType name)
631
632 instance Ord name => Eq (RuleDecl name pat) where
633   -- Works for IfaceRules only; used when comparing interface file versions
634   (IfaceRule n1 bs1 f1 es1 rhs1 _) == (IfaceRule n2 bs2 f2 es2 rhs2 _)
635      = n1==n2 && f1 == f2 && 
636        eq_ufBinders emptyEqHsEnv bs1 bs2 (\env -> 
637        eqListBy (eq_ufExpr env) (rhs1:es1) (rhs2:es2))
638
639 instance (Outputable name, Outputable pat)
640               => Outputable (RuleDecl name pat) where
641   ppr (HsRule name tvs ns lhs rhs loc)
642         = sep [text "{-# RULES" <+> doubleQuotes (ptext name),
643                pp_forall, ppr lhs, equals <+> ppr rhs,
644                text "#-}" ]
645         where
646           pp_forall | null tvs && null ns = empty
647                     | otherwise           = text "forall" <+> 
648                                             fsep (map ppr tvs ++ map ppr ns)
649                                             <> dot
650
651   ppr (IfaceRule name tpl_vars fn tpl_args rhs loc) 
652     = hsep [ doubleQuotes (ptext name),
653            ptext SLIT("__forall") <+> braces (interppSP tpl_vars),
654            ppr fn <+> sep (map (pprUfExpr parens) tpl_args),
655            ptext SLIT("=") <+> ppr rhs
656       ] <+> semi
657
658   ppr (IfaceRuleOut fn rule) = pprCoreRule (ppr fn) rule
659
660 instance Outputable name => Outputable (RuleBndr name) where
661    ppr (RuleBndr name) = ppr name
662    ppr (RuleBndrSig name ty) = ppr name <> dcolon <> ppr ty
663
664 toHsRule id (BuiltinRule _)
665   = pprTrace "toHsRule: builtin" (ppr id) (bogusIfaceRule id)
666
667 toHsRule id (Rule name bndrs args rhs)
668   = IfaceRule name (map toUfBndr bndrs) (getName id)
669               (map toUfExpr args) (toUfExpr rhs) noSrcLoc
670
671 bogusIfaceRule id
672   = IfaceRule SLIT("bogus") [] (getName id) [] (UfVar (getName id)) noSrcLoc
673 \end{code}
674
675
676 %************************************************************************
677 %*                                                                      *
678 \subsection[DeprecDecl]{Deprecations}
679 %*                                                                      *
680 %************************************************************************
681
682 We use exported entities for things to deprecate.
683
684 \begin{code}
685 data DeprecDecl name = Deprecation name DeprecTxt SrcLoc
686
687 type DeprecTxt = FAST_STRING    -- reason/explanation for deprecation
688
689 instance Outputable name => Outputable (DeprecDecl name) where
690     ppr (Deprecation thing txt _)
691       = hsep [text "{-# DEPRECATED", ppr thing, doubleQuotes (ppr txt), text "#-}"]
692 \end{code}