[project @ 2002-03-27 12:09:00 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcTyDecls.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1996-1998
3 %
4 \section[TcTyDecls]{Typecheck type declarations}
5
6 \begin{code}
7 module TcTyDecls ( tcTyDecl, kcConDetails ) where
8
9 #include "HsVersions.h"
10
11 import HsSyn            ( TyClDecl(..), ConDecl(..), ConDetails(..), 
12                           getBangType, getBangStrictness, conDetailsTys
13                         )
14 import RnHsSyn          ( RenamedTyClDecl, RenamedConDecl, RenamedContext )
15 import BasicTypes       ( NewOrData(..) )
16
17 import TcMonoType       ( tcHsTyVars, tcHsTheta, tcHsType, 
18                           kcHsContext, kcHsSigType, kcHsLiftedSigType
19                         )
20 import TcEnv            ( tcExtendTyVarEnv, 
21                           tcLookupTyCon, tcLookupRecId, 
22                           TyThingDetails(..), RecTcEnv
23                         )
24 import TcType           ( tcEqType, tyVarsOfTypes, tyVarsOfPred, ThetaType )
25 import TcMonad
26
27 import DataCon          ( DataCon, mkDataCon, dataConFieldLabels )
28 import FieldLabel       ( fieldLabelName, fieldLabelType, allFieldLabelTags, mkFieldLabel )
29 import MkId             ( mkDataConId, mkDataConWrapId, mkRecordSelId )
30 import Var              ( TyVar )
31 import Name             ( Name, NamedThing(..) )
32 import Outputable
33 import TyCon            ( TyCon, DataConDetails(..), visibleDataCons,
34                           tyConName, tyConTheta, 
35                           tyConTyVars, isSynTyCon )
36 import VarSet           ( intersectVarSet, isEmptyVarSet )
37 import PrelNames        ( unpackCStringName, unpackCStringUtf8Name )
38 import List             ( nubBy )
39 \end{code}
40
41 %************************************************************************
42 %*                                                                      *
43 \subsection{Type checking}
44 %*                                                                      *
45 %************************************************************************
46
47 \begin{code}
48 tcTyDecl :: RecTcEnv -> RenamedTyClDecl -> TcM (Name, TyThingDetails)
49 tcTyDecl unf_env (TySynonym {tcdName = tycon_name, tcdSynRhs = rhs})
50   = tcLookupTyCon tycon_name                    `thenNF_Tc` \ tycon ->
51     tcExtendTyVarEnv (tyConTyVars tycon)        $
52     tcHsType rhs                                `thenTc` \ rhs_ty ->
53     returnTc (tycon_name, SynTyDetails rhs_ty)
54
55 tcTyDecl unf_env (TyData {tcdND = new_or_data, tcdCtxt = context,
56                           tcdName = tycon_name, tcdCons = con_decls})
57   = tcLookupTyCon tycon_name                    `thenNF_Tc` \ tycon ->
58     let
59         tyvars = tyConTyVars tycon
60     in
61     tcExtendTyVarEnv tyvars                             $
62     tcHsTheta context                                   `thenTc` \ ctxt ->
63     tcConDecls new_or_data tycon tyvars ctxt con_decls  `thenTc` \ data_cons ->
64     let
65         sel_ids = mkRecordSelectors unf_env tycon data_cons
66     in
67     returnTc (tycon_name, DataTyDetails ctxt data_cons sel_ids)
68
69 tcTyDecl unf_env (ForeignType {tcdName = tycon_name})
70   = returnTc (tycon_name, ForeignTyDetails)
71
72
73 mkRecordSelectors unf_env tycon data_cons
74   =     -- We'll check later that fields with the same name 
75         -- from different constructors have the same type.
76      [ mkRecordSelId tycon field unpack_id unpackUtf8_id
77      | field <- nubBy eq_name fields ]
78   where
79     fields = [ field | con <- visibleDataCons data_cons, 
80                        field <- dataConFieldLabels con ]
81     eq_name field1 field2 = fieldLabelName field1 == fieldLabelName field2
82
83     unpack_id     = tcLookupRecId unf_env unpackCStringName
84     unpackUtf8_id = tcLookupRecId unf_env unpackCStringUtf8Name
85 \end{code}
86
87
88 %************************************************************************
89 %*                                                                      *
90 \subsection{Kind and type check constructors}
91 %*                                                                      *
92 %************************************************************************
93
94 \begin{code}
95 kcConDetails :: NewOrData -> RenamedContext -> ConDetails Name -> TcM ()
96 kcConDetails new_or_data ex_ctxt details
97   = kcHsContext ex_ctxt         `thenTc_`
98     mapTc_ kc_sig_type (conDetailsTys details)
99   where
100     kc_sig_type = case new_or_data of
101                     DataType -> kcHsSigType
102                     NewType  -> kcHsLiftedSigType
103             -- Can't allow an unlifted type here, because we're effectively
104             -- going to remove the constructor while coercing it to a lifted type.
105
106
107 tcConDecls :: NewOrData -> TyCon -> [TyVar] -> ThetaType 
108            -> DataConDetails RenamedConDecl -> TcM (DataConDetails DataCon)
109
110 tcConDecls new_or_data tycon tyvars ctxt con_decls
111   = case con_decls of
112         Unknown     -> returnTc Unknown
113         HasCons n   -> returnTc (HasCons n)
114         DataCons cs -> mapTc tc_con_decl cs     `thenTc` \ data_cons ->
115                        returnTc (DataCons data_cons)
116   where
117     tc_con_decl (ConDecl name wkr_name ex_tvs ex_ctxt details src_loc)
118       = tcAddSrcLoc src_loc                                             $
119         tcHsTyVars ex_tvs (kcConDetails new_or_data ex_ctxt details)    $ \ ex_tyvars ->
120         tcHsTheta ex_ctxt                                               `thenTc` \ ex_theta ->
121         case details of
122             VanillaCon btys    -> tc_datacon ex_tyvars ex_theta btys
123             InfixCon bty1 bty2 -> tc_datacon ex_tyvars ex_theta [bty1,bty2]
124             RecCon fields      -> tc_rec_con ex_tyvars ex_theta fields
125       where
126         
127         tc_datacon ex_tyvars ex_theta btys
128           = mapTc tcHsType (map getBangType btys)       `thenTc` \ arg_tys ->
129             mk_data_con ex_tyvars ex_theta (map getBangStrictness btys) arg_tys []
130     
131         tc_rec_con ex_tyvars ex_theta fields
132           = checkTc (null ex_tyvars) (exRecConErr name) `thenTc_`
133             mapTc tc_field (fields `zip` allFieldLabelTags)     `thenTc` \ field_labels_s ->
134             let
135                 field_labels = concat field_labels_s
136                 arg_stricts = [str | (ns, bty) <- fields, 
137                                      let str = getBangStrictness bty, 
138                                      n <- ns    -- One for each.  E.g   x,y,z :: !Int
139                               ]
140             in
141             mk_data_con ex_tyvars ex_theta arg_stricts 
142                         (map fieldLabelType field_labels) field_labels
143     
144         tc_field ((field_label_names, bty), tag)
145           = tcHsType (getBangType bty)                  `thenTc` \ field_ty ->
146             returnTc [mkFieldLabel (getName name) tycon field_ty tag | name <- field_label_names]
147     
148         mk_data_con ex_tyvars ex_theta arg_stricts arg_tys fields
149           = let
150                data_con = mkDataCon name arg_stricts fields
151                                tyvars (thinContext arg_tys ctxt)
152                                ex_tyvars ex_theta
153                                arg_tys
154                                tycon data_con_id data_con_wrap_id
155     
156                data_con_id      = mkDataConId wkr_name data_con
157                data_con_wrap_id = mkDataConWrapId data_con
158             in
159             returnNF_Tc data_con
160
161 -- The context for a data constructor should be limited to
162 -- the type variables mentioned in the arg_tys
163 thinContext arg_tys ctxt
164   = filter in_arg_tys ctxt
165   where
166       arg_tyvars = tyVarsOfTypes arg_tys
167       in_arg_tys pred = not $ isEmptyVarSet $ 
168                         tyVarsOfPred pred `intersectVarSet` arg_tyvars
169 \end{code}
170
171
172 %************************************************************************
173 %*                                                                      *
174 \subsection{Errors and contexts}
175 %*                                                                      *
176 %************************************************************************
177
178
179 \begin{code}
180 exRecConErr name
181   = ptext SLIT("Can't combine named fields with locally-quantified type variables")
182     $$
183     (ptext SLIT("In the declaration of data constructor") <+> ppr name)
184 \end{code}