2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
4 \section[TcMonadFns]{Auxilliary functions for typechecker monad}
7 #include "HsVersions.h"
10 newDict, newDicts, newMethod, newOverloadedLit,
13 newOpenTyVarTy, newPolyTyVarTy,
16 --UNUSED: newLocalWithOpenTyVarTy, newLocalWithPolyTyVarTy,
18 newSpecPragmaId, newSpecId,
20 newLocalsWithOpenTyVarTys, newLocalsWithPolyTyVarTys,
22 mkIdsWithOpenTyVarTys, mkIdsWithPolyTyVarTys,
25 applyTcSubstAndCollectTyVars,
26 applyTcSubstAndExpectTyVars,
28 -- and to make the interface self-sufficient...
29 Bag, Class, Binds, MonoBinds, TypecheckedPat, Id, Inst, SpecInfo,
30 OverloadedLit, InstOrigin, TcResult, Name, SrcLoc, Subst, Maybe,
31 Error(..), TyVar, UniType, UnifyErrContext, UniqueSupply,
32 PprStyle, Pretty(..), PrettyRep
35 import TcMonad -- the underlying monadery
39 import Id ( mkId, mkUserLocal, mkSpecPragmaId, mkSpecId, Id, DictVar(..) )
41 import Inst ( mkDict, mkMethod, mkLitInst,
42 Inst(..), -- .. for pragmas
43 OverloadedLit, InstOrigin
45 import Maybes ( Maybe(..) )
47 import Errors ( Error(..), UnifyErrInfo )
48 import Unique ( Unique, UniqueSupply )
52 %************************************************************************
54 \subsection[TcMonadFns-newNameThings]{Making new things from the name supply}
56 %************************************************************************
58 @newPolyTyVars@ takes list of ``old'' template type vars, and manufactures
59 a list of freshly-uniqued type vars.
62 copyTyVars :: [TyVarTemplate] -- Old type vars
64 ([(TyVarTemplate,TauType)],--Old-to-new assoc list
65 [TyVar], -- New type vars
66 [TauType]) -- New type vars wrapped in a UniTyVar
69 = getTyVarUniquesTc (length old_tyvars) `thenNF_Tc` \ new_uniqs ->
70 returnNF_Tc (instantiateTyVarTemplates old_tyvars new_uniqs)
72 newOpenTyVarTys :: Int -> NF_TcM [UniType]
74 = getTyVarUniquesTc n `thenNF_Tc` \ new_uniqs ->
75 returnNF_Tc [mkTyVarTy (mkOpenSysTyVar u) | u <- new_uniqs]
77 newPolyTyVarTys :: Int -> NF_TcM [UniType]
79 = getTyVarUniquesTc n `thenNF_Tc` \ new_uniqs ->
80 returnNF_Tc [mkTyVarTy (mkPolySysTyVar u) | u <- new_uniqs]
82 newOpenTyVarTy, newPolyTyVarTy :: NF_TcM UniType
84 = getTyVarUniqueTc `thenNF_Tc` \ new_uniq ->
85 returnNF_Tc (mkTyVarTy (mkOpenSysTyVar new_uniq))
88 = getTyVarUniqueTc `thenNF_Tc` \ new_uniq ->
89 returnNF_Tc (mkTyVarTy (mkPolySysTyVar new_uniq))
92 The functions @newDicts@, @newMethod@, and @newOverloadedLit@ build
96 newDicts :: InstOrigin -> ThetaType -> NF_TcM [Inst]
98 = getUniquesTc (length theta) `thenNF_Tc` \ new_uniqs ->
99 returnNF_Tc (zipWith mk_dict_var new_uniqs theta)
101 mk_dict_var u (clas, ty) = mkDict u clas ty orig
103 newDict :: InstOrigin -> Class -> UniType -> NF_TcM Inst
105 = getUniqueTc `thenNF_Tc` \ new_uniq ->
106 returnNF_Tc (mkDict new_uniq clas ty orig)
108 newMethod :: InstOrigin -> Id -> [UniType] -> NF_TcM Inst
109 newMethod orig id tys
110 = getUniqueTc `thenNF_Tc` \ new_uniq ->
111 returnNF_Tc (mkMethod new_uniq id tys orig)
113 newOverloadedLit :: InstOrigin -> OverloadedLit -> UniType -> NF_TcM Inst
114 newOverloadedLit orig lit ty
115 = getUniqueTc `thenNF_Tc` \ new_uniq ->
116 returnNF_Tc (mkLitInst new_uniq lit ty orig)
119 Make a fresh batch of locals, derived from name, each typed with a fresh
120 type variable, and return an LVE of them.
123 \item @mkIdsWithTyVarTys@ uses the supplied names directly (including their
124 uniques), and generates a @TopId@ or @Local@ depending on whether
125 the name is a @FullName@ or not.
127 \item @mkIdsWithGivenTys@ does as above, but the types are supplied.
131 mkIdsWithPolyTyVarTys, mkIdsWithOpenTyVarTys :: [Name] -> NF_TcM LVE
132 mkIdsWithPolyTyVarTys names
134 no_of_names = length names
136 newPolyTyVarTys no_of_names `thenNF_Tc` \ tys ->
137 returnNF_Tc (mkIdsWithGivenTys names tys (nOfThem no_of_names noIdInfo))
139 mkIdsWithOpenTyVarTys names
141 no_of_names = length names
143 newOpenTyVarTys no_of_names `thenNF_Tc` \ tys ->
144 returnNF_Tc (mkIdsWithGivenTys names tys (nOfThem no_of_names noIdInfo))
146 mkIdsWithGivenTys :: [Name] -> [UniType] -> [IdInfo] -> LVE
147 -- not monadic any more (WDP 94/05)
148 -- Not done w/ zips/etc for "efficiency" (?)
149 mkIdsWithGivenTys [] [] _ = []
150 mkIdsWithGivenTys (name:names) (ty:tys) (id_info:id_infos)
151 = (name, mkId name ty id_info) : mkIdsWithGivenTys names tys id_infos
153 newLocalsWithOpenTyVarTys, newLocalsWithPolyTyVarTys :: [Name] -> NF_TcM [Id]
154 newLocalsWithOpenTyVarTys = new_locals_given_tyvar_fun newOpenTyVarTys
155 newLocalsWithPolyTyVarTys = new_locals_given_tyvar_fun newPolyTyVarTys
157 new_locals_given_tyvar_fun new_tyvar_fun names
158 = new_tyvar_fun no_of_names `thenNF_Tc` \ tys ->
159 getUniquesTc no_of_names `thenNF_Tc` \ uniqs ->
160 let ids = zipWith3 mk_local names uniqs tys in
163 no_of_names = length names
164 mk_local name uniq ty = mkUserLocal (getOccurrenceName name) uniq ty
168 @newLocal*@ creates a new unique local variable with the given
169 string and type. @newLocals@ is similar, but works on lists of strings
174 newLocalWithOpenTyVarTy, newLocalWithPolyTyVarTy :: Name -> NF_TcM Id
176 newLocalWithOpenTyVarTy name
177 = newOpenTyVarTy `thenNF_Tc` \ ty ->
178 newLocalWithGivenTy name ty
180 newLocalWithPolyTyVarTy name
181 = newPolyTyVarTy `thenNF_Tc` \ ty ->
182 newLocalWithGivenTy name ty
185 newLocalWithGivenTy :: Name -> UniType -> NF_TcM Id
186 newLocalWithGivenTy name ty
187 = getUniqueTc `thenNF_Tc` \ uniq ->
188 returnNF_Tc (mkUserLocal (getOccurrenceName name) uniq ty (getSrcLoc name))
190 newSpecPragmaId :: Name -> UniType -> Maybe SpecInfo -> NF_TcM Id
191 newSpecPragmaId name ty specinfo
192 = getUniqueTc `thenNF_Tc` \ uniq ->
193 returnNF_Tc (mkSpecPragmaId (getOccurrenceName name) uniq ty specinfo (getSrcLoc name))
195 newSpecId :: Id -> [Maybe UniType] -> UniType -> NF_TcM Id
196 newSpecId unspec spec_tys ty
197 = getUniqueTc `thenNF_Tc` \ uniq ->
198 returnNF_Tc (mkSpecId uniq unspec spec_tys ty noIdInfo)
201 ToDo: This @newClassOpLocals@ is used only to make new ClassOps. Pretty yukky.
204 newClassOpLocals :: [(TyVarTemplate, TauType)]
205 -- The class type variable mapped to
206 -- the instance type (an InstTyEnv)
207 -> [ClassOp] -- The class ops
208 -> NF_TcM [Id] -- Suitable Ids for the polymorphic
210 newClassOpLocals inst_env ops
211 = getSrcLocTc `thenNF_Tc` \ src_loc ->
212 getUniquesTc (length ops) `thenNF_Tc` \ uniqs ->
213 returnNF_Tc (zipWith (new_local src_loc) ops uniqs)
215 new_local src_loc op uniq
216 = mkUserLocal (getClassOpString op)
218 (instantiateTy inst_env (getClassOpLocalType op))
222 %************************************************************************
224 Back-substitution functions. These just apply the current
225 substitution to their argument(s).
227 %************************************************************************
229 @applyTcSubstAndCollectTyVars@ applies a substitution to a list of type
230 variables, takes the free type vars of the resulting types, and
231 returns all of them as list without duplications.
234 applyTcSubstAndCollectTyVars :: [TyVar] -> NF_TcM [TyVar]
235 applyTcSubstAndCollectTyVars tyvars
236 = applyTcSubstToTyVars tyvars `thenNF_Tc` \ tys ->
237 returnNF_Tc (extractTyVarsFromTys tys)
239 applyTcSubstAndExpectTyVars :: [TyVar] -> NF_TcM [TyVar]
240 applyTcSubstAndExpectTyVars tyvars
241 = applyTcSubstToTyVars tyvars `thenNF_Tc` \ tys ->
242 returnNF_Tc (map (getTyVar "applyTcSubstAndExpectTyVars") tys)