[project @ 1997-06-18 23:52:36 by simonpj]
[ghc-hetmet.git] / ghc / compiler / hsSyn / HsTypes.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[HsTypes]{Abstract syntax: user-defined types}
5
6 If compiled without \tr{#define COMPILING_GHC}, you get
7 (part of) a Haskell-abstract-syntax library.  With it,
8 you get part of GHC.
9
10 \begin{code}
11 #include "HsVersions.h"
12
13 module HsTypes (
14         HsType(..), HsTyVar(..),
15         SYN_IE(Context), SYN_IE(ClassAssertion)
16
17         , mkHsForAllTy
18         , getTyVarName, replaceTyVarName
19         , pprParendHsType
20         , pprContext
21         , cmpHsType, cmpContext
22     ) where
23
24 IMP_Ubiq()
25
26 import CmdLineOpts      ( opt_PprUserLength )
27 import Outputable       ( Outputable(..), PprStyle(..), pprQuote, interppSP )
28 import Kind             ( Kind {- instance Outputable -} )
29 import Name             ( nameOccName )
30 import Pretty
31 import Util             ( thenCmp, cmpList, isIn, panic# )
32 \end{code}
33
34 This is the syntax for types as seen in type signatures.
35
36 \begin{code}
37 type Context name = [ClassAssertion name]
38
39 type ClassAssertion name = (name, HsType name)
40         -- The type is usually a type variable, but it
41         -- doesn't have to be when reading interface files
42
43 data HsType name
44   = HsPreForAllTy       (Context name)
45                         (HsType name)
46
47         -- The renamer turns HsPreForAllTys into HsForAllTys when they
48         -- occur in signatures, to make the binding of variables
49         -- explicit.  This distinction is made visible for
50         -- non-COMPILING_GHC code, because you probably want to do the
51         -- same thing.
52
53   | HsForAllTy          [HsTyVar name]
54                         (Context name)
55                         (HsType name)
56
57   | MonoTyVar           name            -- Type variable
58
59   | MonoTyApp           (HsType name)
60                         (HsType name)
61
62   | MonoFunTy           (HsType name) -- function type
63                         (HsType name)
64
65   | MonoListTy          name            -- The list TyCon name
66                         (HsType name)   -- Element type
67
68   | MonoTupleTy         name            -- The tuple TyCon name
69                         [HsType name]   -- Element types (length gives arity)
70
71   -- these next two are only used in unfoldings in interfaces
72   | MonoDictTy          name    -- Class
73                         (HsType name)
74
75 mkHsForAllTy []  []   ty = ty
76 mkHsForAllTy tvs ctxt ty = HsForAllTy tvs ctxt ty
77
78 data HsTyVar name
79   = UserTyVar name
80   | IfaceTyVar name Kind
81         -- *** NOTA BENE *** A "monotype" in a pragma can have
82         -- for-alls in it, (mostly to do with dictionaries).  These
83         -- must be explicitly Kinded.
84
85 getTyVarName (UserTyVar n)    = n
86 getTyVarName (IfaceTyVar n _) = n
87
88 replaceTyVarName :: HsTyVar name1 -> name2 -> HsTyVar name2
89 replaceTyVarName (UserTyVar n)    n' = UserTyVar n'
90 replaceTyVarName (IfaceTyVar n k) n' = IfaceTyVar n' k
91 \end{code}
92
93
94 %************************************************************************
95 %*                                                                      *
96 \subsection{Pretty printing}
97 %*                                                                      *
98 %************************************************************************
99
100 \begin{code}
101
102 instance (Outputable name) => Outputable (HsType name) where
103     ppr sty ty = pprQuote sty $ \ sty -> pprHsType sty ty
104
105 instance (Outputable name) => Outputable (HsTyVar name) where
106     ppr sty (UserTyVar name)       = ppr sty name
107     ppr sty (IfaceTyVar name kind) = pprQuote sty $ \ sty ->
108                                      hsep [ppr sty name, ptext SLIT("::"), ppr sty kind]
109
110 ppr_forall sty ctxt_prec [] [] ty
111    = ppr_mono_ty sty ctxt_prec ty
112 ppr_forall sty ctxt_prec tvs ctxt ty
113    = maybeParen (ctxt_prec >= pREC_FUN) $
114      sep [ptext SLIT("_forall_"), brackets (interppSP sty tvs),
115             pprContext sty ctxt,  ptext SLIT("=>"),
116             pprHsType sty ty]
117
118 pprContext :: (Outputable name) => PprStyle -> (Context name) -> Doc
119 pprContext sty []               = empty
120 pprContext sty context
121   = hsep [braces (hsep (punctuate comma (map ppr_assert context)))]
122   where
123     ppr_assert (clas, ty) = hsep [ppr sty clas, ppr sty ty]
124 \end{code}
125
126 \begin{code}
127 pREC_TOP = (0 :: Int)
128 pREC_FUN = (1 :: Int)
129 pREC_CON = (2 :: Int)
130
131 maybeParen :: Bool -> Doc -> Doc
132 maybeParen True  p = parens p
133 maybeParen False p = p
134         
135 -- printing works more-or-less as for Types
136
137 pprHsType, pprParendHsType :: (Outputable name) => PprStyle -> HsType name -> Doc
138
139 pprHsType sty ty       = ppr_mono_ty sty pREC_TOP ty
140 pprParendHsType sty ty = ppr_mono_ty sty pREC_CON ty
141
142 ppr_mono_ty sty ctxt_prec (HsPreForAllTy ctxt ty)     = ppr_forall sty ctxt_prec [] ctxt ty
143 ppr_mono_ty sty ctxt_prec (HsForAllTy tvs ctxt ty)    = ppr_forall sty ctxt_prec tvs ctxt ty
144
145 ppr_mono_ty sty ctxt_prec (MonoTyVar name) = ppr sty name
146
147 ppr_mono_ty sty ctxt_prec (MonoFunTy ty1 ty2)
148   = let p1 = ppr_mono_ty sty pREC_FUN ty1
149         p2 = ppr_mono_ty sty pREC_TOP ty2
150     in
151     maybeParen (ctxt_prec >= pREC_FUN)
152                (sep [p1, (<>) (ptext SLIT("-> ")) p2])
153
154 ppr_mono_ty sty ctxt_prec (MonoTupleTy _ tys)
155  = parens (sep (punctuate comma (map (ppr sty) tys)))
156
157 ppr_mono_ty sty ctxt_prec (MonoListTy _ ty)
158  = brackets (ppr_mono_ty sty pREC_TOP ty)
159
160 ppr_mono_ty sty ctxt_prec (MonoTyApp fun_ty arg_ty)
161   = maybeParen (ctxt_prec >= pREC_CON)
162                (hsep [ppr_mono_ty sty pREC_FUN fun_ty, ppr_mono_ty sty pREC_CON arg_ty])
163
164 ppr_mono_ty sty ctxt_prec (MonoDictTy clas ty)
165   = hsep [ppr sty clas, ppr_mono_ty sty pREC_CON ty]
166 \end{code}
167
168
169 %************************************************************************
170 %*                                                                      *
171 \subsection{Comparison}
172 %*                                                                      *
173 %************************************************************************
174
175 We do define a specialised equality for these \tr{*Type} types; used
176 in checking interfaces.  Most any other use is likely to be {\em
177 wrong}, so be careful!
178
179 \begin{code}
180 cmpHsTyVar :: (a -> a -> TAG_) -> HsTyVar a -> HsTyVar a -> TAG_
181 --cmpHsType :: (a -> a -> TAG_) -> HsType a -> HsType a -> TAG_
182 --cmpContext  :: (a -> a -> TAG_) -> Context  a -> Context  a -> TAG_
183
184 cmpHsTyVar cmp (UserTyVar v1)    (UserTyVar v2)    = v1 `cmp` v2
185 cmpHsTyVar cmp (IfaceTyVar v1 _) (IfaceTyVar v2 _) = v1 `cmp` v2
186 cmpHsTyVar cmp (UserTyVar _)     other             = LT_
187 cmpHsTyVar cmp other1            other2            = GT_
188
189
190 -- We assume that HsPreForAllTys have been smashed by now.
191 # ifdef DEBUG
192 cmpHsType _ (HsPreForAllTy _ _) _ = panic# "cmpHsType:HsPreForAllTy:1st arg"
193 cmpHsType _ _ (HsPreForAllTy _ _) = panic# "cmpHsType:HsPreForAllTy:2nd arg"
194 # endif
195
196 cmpHsType cmp (HsForAllTy tvs1 c1 t1) (HsForAllTy tvs2 c2 t2)
197   = cmpList (cmpHsTyVar cmp) tvs1 tvs2  `thenCmp`
198     cmpContext cmp c1 c2                `thenCmp`
199     cmpHsType cmp t1 t2
200
201 cmpHsType cmp (MonoTyVar n1) (MonoTyVar n2)
202   = cmp n1 n2
203
204 cmpHsType cmp (MonoTupleTy _ tys1) (MonoTupleTy _ tys2)
205   = cmpList (cmpHsType cmp) tys1 tys2
206 cmpHsType cmp (MonoListTy _ ty1) (MonoListTy _ ty2)
207   = cmpHsType cmp ty1 ty2
208
209 cmpHsType cmp (MonoTyApp fun_ty1 arg_ty1) (MonoTyApp fun_ty2 arg_ty2)
210   = cmpHsType cmp fun_ty1 fun_ty2 `thenCmp` cmpHsType cmp arg_ty1 arg_ty2
211
212 cmpHsType cmp (MonoFunTy a1 b1) (MonoFunTy a2 b2)
213   = cmpHsType cmp a1 a2 `thenCmp` cmpHsType cmp b1 b2
214
215 cmpHsType cmp (MonoDictTy c1 ty1)   (MonoDictTy c2 ty2)
216   = cmp c1 c2 `thenCmp` cmpHsType cmp ty1 ty2
217
218 cmpHsType cmp ty1 ty2 -- tags must be different
219   = let tag1 = tag ty1
220         tag2 = tag ty2
221     in
222     if tag1 _LT_ tag2 then LT_ else GT_
223   where
224     tag (MonoTyVar n1)          = (ILIT(1) :: FAST_INT)
225     tag (MonoTupleTy _ tys1)    = ILIT(2)
226     tag (MonoListTy _ ty1)      = ILIT(3)
227     tag (MonoTyApp tc1 tys1)    = ILIT(4)
228     tag (MonoFunTy a1 b1)       = ILIT(5)
229     tag (MonoDictTy c1 ty1)     = ILIT(7)
230     tag (HsForAllTy _ _ _)      = ILIT(8)
231     tag (HsPreForAllTy _ _)     = ILIT(9)
232
233 -------------------
234 cmpContext cmp a b
235   = cmpList cmp_ctxt a b
236   where
237     cmp_ctxt (c1, ty1) (c2, ty2)
238       = cmp c1 c2 `thenCmp` cmpHsType cmp ty1 ty2
239 \end{code}