2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[TcMonoType]{Typechecking user-specified @MonoTypes@}
12 kcHsTyVars, kcHsSigType, kcHsLiftedSigType,
13 kcCheckHsType, kcHsContext,
15 -- Typechecking kinded types
16 tcHsKindedContext, tcHsKindedType, tcTyVarBndrs, dsHsType,
20 TcSigInfo(..), tcTySig, mkTcSig, maybeSig, tcSigPolyId, tcSigMonoId
23 #include "HsVersions.h"
25 import HsSyn ( HsType(..), HsTyVarBndr(..), HsContext, Sig(..), HsPred(..) )
26 import RnHsSyn ( RenamedHsType, RenamedContext, RenamedSig, extractHsTyVars )
27 import TcHsSyn ( TcId )
30 import TcEnv ( tcExtendTyVarEnv, tcExtendTyVarKindEnv,
31 tcLookup, tcLookupClass, tcLookupTyCon,
32 TyThing(..), TcTyThing(..),
35 import TcMType ( newKindVar, tcInstType, newMutTyVar,
36 zonkTcType, zonkTcKindToKind,
37 checkValidType, UserTypeCtxt(..), pprUserTypeCtxt
39 import TcUnify ( unifyKind, unifyFunKind, unifyTypeKind )
40 import TcType ( Type, PredType(..), ThetaType, TyVarDetails(..),
41 TcTyVar, TcKind, TcThetaType, TcTauType,
42 mkTyVarTy, mkTyVarTys, mkFunTy,
43 mkForAllTys, mkFunTys, tcEqType, isPredTy,
44 mkSigmaTy, mkPredTy, mkGenTyConApp, mkTyConApp, mkAppTys,
45 liftedTypeKind, unliftedTypeKind, eqKind,
46 tcSplitFunTy_maybe, tcSplitForAllTys, tcSplitSigmaTy
48 import PprType ( pprKind, pprThetaArrow )
49 import qualified Type ( splitFunTys )
50 import Inst ( Inst, InstOrigin(..), newMethod, instToId )
52 import Id ( mkLocalId, idName, idType )
53 import Var ( TyVar, mkTyVar, tyVarKind )
54 import ErrUtils ( Message )
55 import TyCon ( TyCon, tyConKind )
56 import Class ( classTyCon )
59 import PrelNames ( genUnitTyConName )
60 import Subst ( deShadowTy )
61 import TysWiredIn ( mkListTy, mkPArrTy, mkTupleTy )
62 import BasicTypes ( Boxity(..) )
63 import SrcLoc ( SrcLoc )
69 ----------------------------
71 ----------------------------
73 Generally speaking we now type-check types in three phases
75 1. kcHsType: kind check the HsType
76 *includes* performing any TH type splices;
77 so it returns a translated, and kind-annotated, type
79 2. dsHsType: convert from HsType to Type:
81 expand type synonyms [mkGenTyApps]
82 hoist the foralls [tcHsType]
84 3. checkValidType: check the validity of the resulting type
86 Often these steps are done one after the other (tcHsSigType).
87 But in mutually recursive groups of type and class decls we do
88 1 kind-check the whole group
89 2 build TyCons/Classes in a knot-tied way
90 3 check the validity of types in the now-unknotted TyCons/Classes
92 For example, when we find
93 (forall a m. m a -> m a)
94 we bind a,m to kind varibles and kind-check (m a -> m a). This makes
95 a get kind *, and m get kind *->*. Now we typecheck (m a -> m a) in
96 an environment that binds a and m suitably.
98 The kind checker passed to tcHsTyVars needs to look at enough to
99 establish the kind of the tyvar:
100 * For a group of type and class decls, it's just the group, not
101 the rest of the program
102 * For a tyvar bound in a pattern type signature, its the types
103 mentioned in the other type signatures in that bunch of patterns
104 * For a tyvar bound in a RULE, it's the type signatures on other
105 universally quantified variables in the rule
107 Note that this may occasionally give surprising results. For example:
109 data T a b = MkT (a b)
111 Here we deduce a::*->*, b::*
112 But equally valid would be a::(*->*)-> *, b::*->*
117 Some of the validity check could in principle be done by the kind checker,
120 - During desugaring, we normalise by expanding type synonyms. Only
121 after this step can we check things like type-synonym saturation
122 e.g. type T k = k Int
124 Then (T S) is ok, because T is saturated; (T S) expands to (S Int);
125 and then S is saturated. This is a GHC extension.
127 - Similarly, also a GHC extension, we look through synonyms before complaining
128 about the form of a class or instance declaration
130 - Ambiguity checks involve functional dependencies, and it's easier to wait
131 until knots have been resolved before poking into them
133 Also, in a mutually recursive group of types, we can't look at the TyCon until we've
134 finished building the loop. So to keep things simple, we postpone most validity
135 checking until step (3).
139 During step (1) we might fault in a TyCon defined in another module, and it might
140 (via a loop) refer back to a TyCon defined in this module. So when we tie a big
141 knot around type declarations with ARecThing, so that the fault-in code can get
142 the TyCon being defined.
145 %************************************************************************
147 \subsection{Checking types}
149 %************************************************************************
152 tcHsSigType :: UserTypeCtxt -> RenamedHsType -> TcM Type
153 -- Do kind checking, and hoist for-alls to the top
154 tcHsSigType ctxt hs_ty
155 = addErrCtxt (checkHsTypeCtxt ctxt hs_ty) $
156 do { kinded_ty <- kcTypeType hs_ty
157 ; ty <- tcHsKindedType kinded_ty
158 ; checkValidType ctxt ty
161 -- tcHsPred is happy with a partial application, e.g. (ST s)
164 = do { (kinded_pred,_) <- kc_pred pred -- kc_pred rather than kcHsPred
165 -- to avoid the partial application check
166 ; dsHsPred kinded_pred }
169 checkHsTypeCtxt ctxt hs_ty
170 = vcat [ptext SLIT("In the type signature:") <+> ppr hs_ty,
171 ptext SLIT("While checking") <+> pprUserTypeCtxt ctxt ]
174 These functions are used during knot-tying in
175 type and class declarations, when we have to
176 separate kind-checking, desugaring, and validity checking
179 kcHsSigType, kcHsLiftedSigType :: HsType Name -> TcM (HsType Name)
180 -- Used for type signatures
181 kcHsSigType ty = kcTypeType ty
182 kcHsLiftedSigType ty = kcLiftedType ty
184 tcHsKindedType :: RenamedHsType -> TcM Type
185 -- Don't do kind checking, nor validity checking,
186 -- but do hoist for-alls to the top
187 -- This is used in type and class decls, where kinding is
188 -- done in advance, and validity checking is done later
189 -- [Validity checking done later because of knot-tying issues.]
191 = do { ty <- dsHsType hs_ty
192 ; return (hoistForAllTys ty) }
194 tcHsKindedContext :: RenamedContext -> TcM ThetaType
195 -- Used when we are expecting a ClassContext (i.e. no implicit params)
196 -- Does not do validity checking, like tcHsKindedType
197 tcHsKindedContext hs_theta = mappM dsHsPred hs_theta
201 %************************************************************************
203 The main kind checker: kcHsType
205 %************************************************************************
207 First a couple of simple wrappers for kcHsType
210 ---------------------------
211 kcLiftedType :: HsType Name -> TcM (HsType Name)
212 -- The type ty must be a *lifted* *type*
213 kcLiftedType ty = kcCheckHsType ty liftedTypeKind
215 ---------------------------
216 kcTypeType :: HsType Name -> TcM (HsType Name)
217 -- The type ty must be a *type*, but it can be lifted or unlifted
219 = kcHsType ty `thenM` \ (ty', kind) ->
220 unifyTypeKind kind `thenM_`
223 ---------------------------
224 kcCheckHsType :: HsType Name -> TcKind -> TcM (HsType Name)
225 -- Check that the type has the specified kind
226 kcCheckHsType ty exp_kind
227 = kcHsType ty `thenM` \ (ty', act_kind) ->
228 checkExpectedKind (ppr ty) act_kind exp_kind `thenM_`
232 Here comes the main function
235 kcHsType :: HsType Name -> TcM (HsType Name, TcKind)
236 -- kcHsType *returns* the kind of the type, rather than taking an expected
237 -- kind as argument as tcExpr does.
239 -- (a) the kind of (->) is
240 -- forall bx1 bx2. Type bx1 -> Type bx2 -> Type Boxed
241 -- so we'd need to generate huge numbers of bx variables.
242 -- (b) kinds are so simple that the error messages are fine
244 -- The translated type has explicitly-kinded type-variable binders
246 kcHsType (HsParTy ty)
247 = kcHsType ty `thenM` \ (ty', kind) ->
248 returnM (HsParTy ty', kind)
250 kcHsType (HsTyVar name)
251 = kcTyVar name `thenM` \ kind ->
252 returnM (HsTyVar name, kind)
254 kcHsType (HsListTy ty)
255 = kcLiftedType ty `thenM` \ ty' ->
256 returnM (HsListTy ty', liftedTypeKind)
258 kcHsType (HsPArrTy ty)
259 = kcLiftedType ty `thenM` \ ty' ->
260 returnM (HsPArrTy ty', liftedTypeKind)
263 = returnM (HsNumTy n, liftedTypeKind)
265 kcHsType (HsKindSig ty k)
266 = kcCheckHsType ty k `thenM` \ ty' ->
267 returnM (HsKindSig ty' k, k)
269 kcHsType (HsTupleTy Boxed tys)
270 = mappM kcLiftedType tys `thenM` \ tys' ->
271 returnM (HsTupleTy Boxed tys', liftedTypeKind)
273 kcHsType (HsTupleTy Unboxed tys)
274 = mappM kcTypeType tys `thenM` \ tys' ->
275 returnM (HsTupleTy Unboxed tys', unliftedTypeKind)
277 kcHsType (HsFunTy ty1 ty2)
278 = kcTypeType ty1 `thenM` \ ty1' ->
279 kcTypeType ty2 `thenM` \ ty2' ->
280 returnM (HsFunTy ty1' ty2', liftedTypeKind)
282 kcHsType ty@(HsOpTy ty1 op ty2)
283 = kcTyVar op `thenM` \ op_kind ->
284 kcApps op_kind (ppr op) [ty1,ty2] `thenM` \ ([ty1',ty2'], res_kind) ->
285 returnM (HsOpTy ty1' op ty2', res_kind)
287 kcHsType ty@(HsAppTy ty1 ty2)
288 = kcHsType fun_ty `thenM` \ (fun_ty', fun_kind) ->
289 kcApps fun_kind (ppr fun_ty) arg_tys `thenM` \ (arg_tys', res_kind) ->
290 returnM (foldl HsAppTy fun_ty' arg_tys', res_kind)
292 (fun_ty, arg_tys) = split ty1 [ty2]
293 split (HsAppTy f a) as = split f (a:as)
296 kcHsType (HsPredTy pred)
297 = kcHsPred pred `thenM` \ pred' ->
298 returnM (HsPredTy pred', liftedTypeKind)
300 kcHsType (HsForAllTy (Just tv_names) context ty)
301 = kcHsTyVars tv_names $ \ tv_names' ->
302 kcHsContext context `thenM` \ ctxt' ->
303 kcLiftedType ty `thenM` \ ty' ->
304 -- The body of a forall must be of kind *
305 -- In principle, I suppose, we could allow unlifted types,
306 -- but it seems simpler to stick to lifted types for now.
307 returnM (HsForAllTy (Just tv_names') ctxt' ty', liftedTypeKind)
309 ---------------------------
310 kcApps :: TcKind -- Function kind
312 -> [HsType Name] -- Arg types
313 -> TcM ([HsType Name], TcKind) -- Kind-checked args
314 kcApps fun_kind ppr_fun args
315 = split_fk fun_kind (length args) `thenM` \ (arg_kinds, res_kind) ->
316 mappM kc_arg (args `zip` arg_kinds) `thenM` \ args' ->
317 returnM (args', res_kind)
319 split_fk fk 0 = returnM ([], fk)
320 split_fk fk n = unifyFunKind fk `thenM` \ mb_fk ->
322 Nothing -> failWithTc too_many_args
323 Just (ak,fk') -> split_fk fk' (n-1) `thenM` \ (aks, rk) ->
326 kc_arg (arg, arg_kind) = kcCheckHsType arg arg_kind
328 too_many_args = ptext SLIT("Kind error:") <+> quotes ppr_fun <+>
329 ptext SLIT("is applied to too many type arguments")
331 ---------------------------
332 kcHsContext :: HsContext Name -> TcM (HsContext Name)
333 kcHsContext ctxt = mappM kcHsPred ctxt
335 kcHsPred pred -- Checks that the result is of kind liftedType
336 = kc_pred pred `thenM` \ (pred', kind) ->
337 checkExpectedKind (ppr pred) kind liftedTypeKind `thenM_`
340 ---------------------------
341 kc_pred :: HsPred Name -> TcM (HsPred Name, TcKind)
342 -- Does *not* check for a saturated
343 -- application (reason: used from TcDeriv)
344 kc_pred pred@(HsIParam name ty)
345 = kcHsType ty `thenM` \ (ty', kind) ->
346 returnM (HsIParam name ty', kind)
348 kc_pred pred@(HsClassP cls tys)
349 = kcClass cls `thenM` \ kind ->
350 kcApps kind (ppr cls) tys `thenM` \ (tys', res_kind) ->
351 returnM (HsClassP cls tys', res_kind)
353 ---------------------------
354 kcTyVar :: Name -> TcM TcKind
355 kcTyVar name -- Could be a tyvar or a tycon
356 = tcLookup name `thenM` \ thing ->
358 ATyVar tv -> returnM (tyVarKind tv)
359 ARecTyCon kind -> returnM kind
360 AGlobal (ATyCon tc) -> returnM (tyConKind tc)
361 other -> failWithTc (wrongThingErr "type" thing name)
363 kcClass :: Name -> TcM TcKind
364 kcClass cls -- Must be a class
365 = tcLookup cls `thenM` \ thing ->
367 ARecClass kind -> returnM kind
368 AGlobal (AClass cls) -> returnM (tyConKind (classTyCon cls))
369 other -> failWithTc (wrongThingErr "class" thing cls)
376 ---------------------------
377 -- We would like to get a decent error message from
378 -- (a) Under-applied type constructors
379 -- f :: (Maybe, Maybe)
380 -- (b) Over-applied type constructors
381 -- f :: Int x -> Int x
385 checkExpectedKind :: SDoc -> TcKind -> TcKind -> TcM TcKind
386 -- A fancy wrapper for 'unifyKind', which tries to give
387 -- decent error messages.
388 -- Returns the same kind that it is passed, exp_kind
389 checkExpectedKind pp_ty act_kind exp_kind
390 | act_kind `eqKind` exp_kind -- Short cut for a very common case
393 = tryTc (unifyKind exp_kind act_kind) `thenM` \ (errs, mb_r) ->
395 Just _ -> returnM exp_kind ; -- Unification succeeded
398 -- So there's definitely an error
399 -- Now to find out what sort
400 zonkTcType exp_kind `thenM` \ exp_kind ->
401 zonkTcType act_kind `thenM` \ act_kind ->
403 let (exp_as, _) = Type.splitFunTys exp_kind
404 (act_as, _) = Type.splitFunTys act_kind
405 -- Use the Type versions for kinds
406 n_exp_as = length exp_as
407 n_act_as = length act_as
409 err | n_exp_as < n_act_as -- E.g. [Maybe]
410 = quotes pp_ty <+> ptext SLIT("is not applied to enough type arguments")
412 -- Now n_exp_as >= n_act_as. In the next two cases,
413 -- n_exp_as == 0, and hence so is n_act_as
414 | exp_kind `eqKind` liftedTypeKind && act_kind `eqKind` unliftedTypeKind
415 = ptext SLIT("Expecting a lifted type, but") <+> quotes pp_ty
416 <+> ptext SLIT("is unlifted")
418 | exp_kind `eqKind` unliftedTypeKind && act_kind `eqKind` liftedTypeKind
419 = ptext SLIT("Expecting an unlifted type, but") <+> quotes pp_ty
420 <+> ptext SLIT("is lifted")
422 | otherwise -- E.g. Monad [Int]
423 = sep [ ptext SLIT("Expecting kind") <+> quotes (pprKind exp_kind) <> comma,
424 ptext SLIT("but") <+> quotes pp_ty <+>
425 ptext SLIT("has kind") <+> quotes (pprKind act_kind)]
427 failWithTc (ptext SLIT("Kind error:") <+> err)
431 %************************************************************************
435 %************************************************************************
439 * Transforms from HsType to Type
442 It cannot fail, and does no validity checking
445 dsHsType :: HsType Name -- All HsTyVarBndrs are kind-annotated
448 dsHsType ty@(HsTyVar name)
451 dsHsType (HsParTy ty) -- Remove the parentheses markers
454 dsHsType (HsKindSig ty k)
455 = dsHsType ty -- Kind checking done already
457 dsHsType (HsListTy ty)
458 = dsHsType ty `thenM` \ tau_ty ->
459 returnM (mkListTy tau_ty)
461 dsHsType (HsPArrTy ty)
462 = dsHsType ty `thenM` \ tau_ty ->
463 returnM (mkPArrTy tau_ty)
465 dsHsType (HsTupleTy boxity tys)
466 = dsHsTypes tys `thenM` \ tau_tys ->
467 returnM (mkTupleTy boxity (length tys) tau_tys)
469 dsHsType (HsFunTy ty1 ty2)
470 = dsHsType ty1 `thenM` \ tau_ty1 ->
471 dsHsType ty2 `thenM` \ tau_ty2 ->
472 returnM (mkFunTy tau_ty1 tau_ty2)
474 dsHsType (HsOpTy ty1 op ty2)
475 = dsHsType ty1 `thenM` \ tau_ty1 ->
476 dsHsType ty2 `thenM` \ tau_ty2 ->
477 ds_var_app op [tau_ty1,tau_ty2]
481 tcLookupTyCon genUnitTyConName `thenM` \ tc ->
482 returnM (mkTyConApp tc [])
484 dsHsType ty@(HsAppTy ty1 ty2)
487 dsHsType (HsPredTy pred)
488 = dsHsPred pred `thenM` \ pred' ->
489 returnM (mkPredTy pred')
491 dsHsType full_ty@(HsForAllTy (Just tv_names) ctxt ty)
492 = tcTyVarBndrs tv_names $ \ tyvars ->
493 mappM dsHsPred ctxt `thenM` \ theta ->
494 dsHsType ty `thenM` \ tau ->
495 returnM (mkSigmaTy tyvars theta tau)
497 dsHsTypes arg_tys = mappM dsHsType arg_tys
500 Help functions for type applications
501 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
504 ds_app :: HsType Name -> [HsType Name] -> TcM Type
505 ds_app (HsAppTy ty1 ty2) tys
506 = ds_app ty1 (ty2:tys)
509 = dsHsTypes tys `thenM` \ arg_tys ->
511 HsTyVar fun -> ds_var_app fun arg_tys
512 other -> dsHsType ty `thenM` \ fun_ty ->
513 returnM (mkAppTys fun_ty arg_tys)
515 ds_var_app :: Name -> [Type] -> TcM Type
516 ds_var_app name arg_tys
517 = tcLookup name `thenM` \ thing ->
519 ATyVar tv -> returnM (mkAppTys (mkTyVarTy tv) arg_tys)
520 AGlobal (ATyCon tc) -> returnM (mkGenTyConApp tc arg_tys)
521 ARecTyCon _ -> tcLookupTyCon name `thenM` \ tc ->
522 returnM (mkGenTyConApp tc arg_tys)
523 other -> pprPanic "ds_app_type" (ppr name <+> ppr arg_tys)
530 dsHsPred :: HsPred Name -> TcM PredType
531 dsHsPred pred@(HsClassP class_name tys)
532 = dsHsTypes tys `thenM` \ arg_tys ->
533 tcLookupClass class_name `thenM` \ clas ->
534 returnM (ClassP clas arg_tys)
536 dsHsPred (HsIParam name ty)
537 = dsHsType ty `thenM` \ arg_ty ->
538 returnM (IParam name arg_ty)
542 %************************************************************************
544 Type-variable binders
546 %************************************************************************
550 kcHsTyVars :: [HsTyVarBndr Name]
551 -> ([HsTyVarBndr Name] -> TcM r) -- These binders are kind-annotated
552 -- They scope over the thing inside
554 kcHsTyVars tvs thing_inside
555 = mappM kcHsTyVar tvs `thenM` \ bndrs ->
556 tcExtendTyVarKindEnv bndrs $
559 kcHsTyVar :: HsTyVarBndr Name -> TcM (HsTyVarBndr Name)
560 -- Return a *kind-annotated* binder, and a tyvar with a mutable kind in it
561 kcHsTyVar (UserTyVar name) = newKindVar `thenM` \ kind ->
562 returnM (KindedTyVar name kind)
563 kcHsTyVar (KindedTyVar name kind) = returnM (KindedTyVar name kind)
566 tcTyVarBndrs :: [HsTyVarBndr Name] -- Kind-annotated binders, which need kind-zonking
567 -> ([TyVar] -> TcM r)
569 -- Used when type-checking types/classes/type-decls
570 -- Brings into scope immutable TyVars, not mutable ones that require later zonking
571 tcTyVarBndrs bndrs thing_inside
572 = mapM zonk bndrs `thenM` \ tyvars ->
573 tcExtendTyVarEnv tyvars (thing_inside tyvars)
575 zonk (KindedTyVar name kind) = zonkTcKindToKind kind `thenM` \ kind' ->
576 returnM (mkTyVar name kind')
577 zonk (UserTyVar name) = pprTrace "BAD: Un-kinded tyvar" (ppr name) $
578 returnM (mkTyVar name liftedTypeKind)
582 %************************************************************************
584 Scoped type variables
586 %************************************************************************
589 tcAddScopedTyVars is used for scoped type variables added by pattern
591 e.g. \ ((x::a), (y::a)) -> x+y
592 They never have explicit kinds (because this is source-code only)
593 They are mutable (because they can get bound to a more specific type).
595 Usually we kind-infer and expand type splices, and then
596 tupecheck/desugar the type. That doesn't work well for scoped type
597 variables, because they scope left-right in patterns. (e.g. in the
598 example above, the 'a' in (y::a) is bound by the 'a' in (x::a).
600 The current not-very-good plan is to
601 * find all the types in the patterns
602 * find their free tyvars
604 * bring the kinded type vars into scope
605 * BUT throw away the kind-checked type
606 (we'll kind-check it again when we type-check the pattern)
608 This is bad because throwing away the kind checked type throws away
609 its splices. But too bad for now. [July 03]
612 We no longer specify that these type variables must be univerally
613 quantified (lots of email on the subject). If you want to put that
615 a) Do a checkSigTyVars after thing_inside
616 b) More insidiously, don't pass in expected_ty, else
617 we unify with it too early and checkSigTyVars barfs
618 Instead you have to pass in a fresh ty var, and unify
619 it with expected_ty afterwards
622 tcAddScopedTyVars :: [RenamedHsType] -> TcM a -> TcM a
623 tcAddScopedTyVars [] thing_inside
624 = thing_inside -- Quick get-out for the empty case
626 tcAddScopedTyVars sig_tys thing_inside
627 = getInLocalScope `thenM` \ in_scope ->
629 sig_tvs = [ UserTyVar n | ty <- sig_tys,
630 n <- nameSetToList (extractHsTyVars ty),
632 -- The tyvars we want are the free type variables of
633 -- the type that are not already in scope
635 -- Behave like kcHsType on a ForAll type
636 -- i.e. make kinded tyvars with mutable kinds,
637 -- and kind-check the enclosed types
638 kcHsTyVars sig_tvs (\ kinded_tvs -> do
639 { mappM kcTypeType sig_tys
640 ; return kinded_tvs }) `thenM` \ kinded_tvs ->
642 -- Zonk the mutable kinds and bring the tyvars into scope
643 -- Rather like tcTyVarBndrs, except that it brings *mutable*
644 -- tyvars into scope, not immutable ones
645 mapM zonk kinded_tvs `thenM` \ tyvars ->
646 tcExtendTyVarEnv tyvars thing_inside
649 zonk (KindedTyVar name kind) = zonkTcKindToKind kind `thenM` \ kind' ->
650 newMutTyVar name kind' VanillaTv
651 zonk (UserTyVar name) = pprTrace "BAD: Un-kinded tyvar" (ppr name) $
652 returnM (mkTyVar name liftedTypeKind)
656 %************************************************************************
658 \subsection{Signatures}
660 %************************************************************************
662 @tcSigs@ checks the signatures for validity, and returns a list of
663 {\em freshly-instantiated} signatures. That is, the types are already
664 split up, and have fresh type variables installed. All non-type-signature
665 "RenamedSigs" are ignored.
667 The @TcSigInfo@ contains @TcTypes@ because they are unified with
668 the variable's type, and after that checked to see whether they've
674 TcId -- *Polymorphic* binder for this value...
681 TcId -- *Monomorphic* binder for this value
682 -- Does *not* have name = N
685 [Inst] -- Empty if theta is null, or
686 -- (method mono_id) otherwise
688 SrcLoc -- Of the signature
690 instance Outputable TcSigInfo where
691 ppr (TySigInfo id tyvars theta tau _ inst loc) =
692 ppr id <+> ptext SLIT("::") <+> ppr tyvars <+> ppr theta <+> ptext SLIT("=>") <+> ppr tau
694 tcSigPolyId :: TcSigInfo -> TcId
695 tcSigPolyId (TySigInfo id _ _ _ _ _ _) = id
697 tcSigMonoId :: TcSigInfo -> TcId
698 tcSigMonoId (TySigInfo _ _ _ _ id _ _) = id
700 maybeSig :: [TcSigInfo] -> Name -> Maybe (TcSigInfo)
701 -- Search for a particular signature
702 maybeSig [] name = Nothing
703 maybeSig (sig@(TySigInfo sig_id _ _ _ _ _ _) : sigs) name
704 | name == idName sig_id = Just sig
705 | otherwise = maybeSig sigs name
710 tcTySig :: RenamedSig -> TcM TcSigInfo
712 tcTySig (Sig v ty src_loc)
713 = addSrcLoc src_loc $
714 tcHsSigType (FunSigCtxt v) ty `thenM` \ sigma_tc_ty ->
715 mkTcSig (mkLocalId v sigma_tc_ty) `thenM` \ sig ->
718 mkTcSig :: TcId -> TcM TcSigInfo
720 = -- Instantiate this type
721 -- It's important to do this even though in the error-free case
722 -- we could just split the sigma_tc_ty (since the tyvars don't
723 -- unified with anything). But in the case of an error, when
724 -- the tyvars *do* get unified with something, we want to carry on
725 -- typechecking the rest of the program with the function bound
726 -- to a pristine type, namely sigma_tc_ty
727 tcInstType SigTv (idType poly_id) `thenM` \ (tyvars', theta', tau') ->
729 getInstLoc SignatureOrigin `thenM` \ inst_loc ->
730 newMethod inst_loc poly_id
732 theta' tau' `thenM` \ inst ->
733 -- We make a Method even if it's not overloaded; no harm
734 -- But do not extend the LIE! We're just making an Id.
736 getSrcLocM `thenM` \ src_loc ->
737 returnM (TySigInfo poly_id tyvars' theta' tau'
738 (instToId inst) [inst] src_loc)
742 %************************************************************************
744 \subsection{Errors and contexts}
746 %************************************************************************
750 hoistForAllTys :: Type -> Type
751 -- Used for user-written type signatures only
752 -- Move all the foralls and constraints to the top
753 -- e.g. T -> forall a. a ==> forall a. T -> a
754 -- T -> (?x::Int) -> Int ==> (?x::Int) -> T -> Int
756 -- Also: eliminate duplicate constraints. These can show up
757 -- when hoisting constraints, notably implicit parameters.
759 -- We want to 'look through' type synonyms when doing this
760 -- so it's better done on the Type than the HsType
764 no_shadow_ty = deShadowTy ty
765 -- Running over ty with an empty substitution gives it the
766 -- no-shadowing property. This is important. For example:
767 -- type Foo r = forall a. a -> r
768 -- foo :: Foo (Foo ())
769 -- Here the hoisting should give
770 -- foo :: forall a a1. a -> a1 -> ()
772 -- What about type vars that are lexically in scope in the envt?
773 -- We simply rely on them having a different unique to any
774 -- binder in 'ty'. Otherwise we'd have to slurp the in-scope-tyvars
775 -- out of the envt, which is boring and (I think) not necessary.
777 case hoist no_shadow_ty of
778 (tvs, theta, body) -> mkForAllTys tvs (mkFunTys (nubBy tcEqType theta) body)
779 -- The 'nubBy' eliminates duplicate constraints,
780 -- notably implicit parameters
783 | (tvs1, body_ty) <- tcSplitForAllTys ty,
785 = case hoist body_ty of
786 (tvs2,theta,tau) -> (tvs1 ++ tvs2, theta, tau)
788 | Just (arg, res) <- tcSplitFunTy_maybe ty
790 arg' = hoistForAllTys arg -- Don't forget to apply hoist recursively
791 in -- to the argument type
792 if (isPredTy arg') then
794 (tvs,theta,tau) -> (tvs, arg':theta, tau)
797 (tvs,theta,tau) -> (tvs, theta, mkFunTy arg' tau)
799 | otherwise = ([], [], ty)
803 %************************************************************************
805 \subsection{Errors and contexts}
807 %************************************************************************
810 wrongThingErr expected thing name
811 = pp_thing thing <+> quotes (ppr name) <+> ptext SLIT("used as a") <+> text expected
813 pp_thing (AGlobal (ATyCon _)) = ptext SLIT("Type constructor")
814 pp_thing (AGlobal (AClass _)) = ptext SLIT("Class")
815 pp_thing (AGlobal (AnId _)) = ptext SLIT("Identifier")
816 pp_thing (AGlobal (ADataCon _)) = ptext SLIT("Data constructor")
817 pp_thing (ATyVar _) = ptext SLIT("Type variable")
818 pp_thing (ATcId _ _ _) = ptext SLIT("Local identifier")
819 pp_thing (ARecTyCon _) = ptext SLIT("Rec tycon")
820 pp_thing (ARecClass _) = ptext SLIT("Rec class")