2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1994
4 \section[TcPolyType]{Typechecking user-specified @PolyTypes@}
7 module TcPolyType ( tcPolyType ) where
9 #include "HsVersions.h"
11 import TcMonad -- typechecking monad machinery
12 import AbsSyn -- the stuff being typechecked
14 import AbsUniType ( mkTyVarTemplateTy, mkSysTyVarTemplate, mkSigmaTy,
15 mkForallTy, SigmaType(..)
18 import Maybes ( Maybe(..) )
19 import TCE ( TCE(..), UniqFM )
20 import TVE -- ( mkTVE, plusTVE, unitTVE, lookupTVE_NoFail, TVE(..) )
21 import TcContext ( tcContext )
22 import TcMonoType ( tcMonoType )
26 The TVE passed into @tcPolyType@ binds type variables which are
27 in scope; in practice this is always either empty (ordinary type sigs)
28 or a singleton (class signatures). @tcPolyType@ generates a type which
29 is polymorphic in all the {\em other} type varaibles mentioned in the
32 Very Important Note: when we have a type signature in an interface, say
36 which of the following polytypes do we return?
38 forall a b. a -> b -> a
40 forall b a. a -> b -> a
43 It makes a difference, because it affects the order in which f takes
44 its type arguments. Now this makes a difference in two ways:
47 It's essential to get it right if an inlining for f is also exported
50 It's essential to get it right if the interface tells that there's a specialised
51 version of f, because specialisations are known by their function-name/type-arg
55 By convention, the foralls on a type read in from somewhere (notably interfaces)
57 {\em in alphabetical order of their type variables}
59 When printing types we make sure that we assign print-names to the forall'd type
60 variables which are also in alphabetical order.
63 tcPolyType :: CE -> TCE -> TVE -> RenamedPolyType -> Baby_TcM UniType
65 tcPolyType ce tce tve (ForAllTy tvs ty)
67 new_tv_tmpls_w_uniqs = map tc_uf_tyvar_template tvs
68 new_tv_tmpls = map snd new_tv_tmpls_w_uniqs
71 [ unitTVE u (mkTyVarTemplateTy tv)
72 | (u, tv) <- new_tv_tmpls_w_uniqs ]
74 tcMonoType ce tce new_tve ty `thenB_Tc` \ new_ty ->
75 returnB_Tc (mkForallTy new_tv_tmpls new_ty)
77 tc_uf_tyvar_template (Short u _) = (u, mkSysTyVarTemplate u SLIT("a"))
79 tcPolyType ce tce tve (OverloadedTy ctxt ty) = tc_poly ce tce tve ctxt ty
80 tcPolyType ce tce tve (UnoverloadedTy ty) = tc_poly ce tce tve [] ty
82 tc_poly ce tce tve ctxt ty
83 = let -- BUILD THE NEW TVE
84 used_tyvar_names = extractMonoTyNames (==) ty
85 poly_tyvar_names = drop_tyvars_if_in_TVE used_tyvar_names
87 -- Sort them into alphabetical order; see notes above.
88 sorted_tyvar_names = sortLt lt_by_string poly_tyvar_names
90 (local_tve, tyvars, _) = mkTVE sorted_tyvar_names
91 new_tve = plusTVE tve local_tve
93 -- TYPE CHECK THE CONTEXT AND MONOTYPE
94 tcContext ce tce new_tve ctxt `thenB_Tc` \ theta ->
95 tcMonoType ce tce new_tve ty `thenB_Tc` \ tau_ty ->
97 -- BUILD THE POLYTYPE AND RETURN
98 returnB_Tc (mkSigmaTy tyvars theta tau_ty)
100 drop_tyvars_if_in_TVE [] = []
101 drop_tyvars_if_in_TVE (n:ns)
102 = let rest = drop_tyvars_if_in_TVE ns
104 case (lookupTVE_NoFail tve n) of
105 Just _ -> rest -- drop it
108 lt_by_string :: Name -> Name -> Bool
109 lt_by_string a b = getOccurrenceName a < getOccurrenceName b