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