PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/compiler/types/FamInstEnv.lhs

https://bitbucket.org/khibino/ghc-hack
Haskell | 594 lines | 396 code | 107 blank | 91 comment | 13 complexity | cea7cb02e4ed7fffefa46fd0710e5d98 MD5 | raw file
Possible License(s): BSD-3-Clause, BSD-2-Clause, LGPL-3.0
  1. %
  2. % (c) The University of Glasgow 2006
  3. %
  4. FamInstEnv: Type checked family instance declarations
  5. \begin{code}
  6. {-# OPTIONS -fno-warn-tabs #-}
  7. -- The above warning supression flag is a temporary kludge.
  8. -- While working on this module you are encouraged to remove it and
  9. -- detab the module (please do the detabbing in a separate patch). See
  10. -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#TabsvsSpaces
  11. -- for details
  12. module FamInstEnv (
  13. FamInst(..), famInstTyCon, famInstTyVars,
  14. pprFamInst, pprFamInstHdr, pprFamInsts,
  15. famInstHead, mkLocalFamInst, mkImportedFamInst,
  16. FamInstEnvs, FamInstEnv, emptyFamInstEnv, emptyFamInstEnvs,
  17. extendFamInstEnv, overwriteFamInstEnv, extendFamInstEnvList,
  18. famInstEnvElts, familyInstances,
  19. lookupFamInstEnv, lookupFamInstEnvConflicts, lookupFamInstEnvConflicts',
  20. -- Normalisation
  21. topNormaliseType, normaliseType, normaliseTcApp
  22. ) where
  23. #include "HsVersions.h"
  24. import InstEnv
  25. import Unify
  26. import Type
  27. import TypeRep
  28. import TyCon
  29. import Coercion
  30. import VarSet
  31. import Name
  32. import UniqFM
  33. import Outputable
  34. import Maybes
  35. import Util
  36. import FastString
  37. \end{code}
  38. %************************************************************************
  39. %* *
  40. \subsection{Type checked family instance heads}
  41. %* *
  42. %************************************************************************
  43. \begin{code}
  44. data FamInst
  45. = FamInst { fi_fam :: Name -- Family name
  46. -- INVARIANT: fi_fam = case tyConFamInst_maybe fi_tycon of
  47. -- Just (tc, tys) -> tc
  48. -- Used for "rough matching"; same idea as for class instances
  49. , fi_tcs :: [Maybe Name] -- Top of type args
  50. -- INVARIANT: fi_tcs = roughMatchTcs fi_tys
  51. -- Used for "proper matching"; ditto
  52. , fi_tvs :: TyVarSet -- Template tyvars for full match
  53. , fi_tys :: [Type] -- Full arg types
  54. -- INVARIANT: fi_tvs = tyConTyVars fi_tycon
  55. -- fi_tys = case tyConFamInst_maybe fi_tycon of
  56. -- Just (_, tys) -> tys
  57. , fi_tycon :: TyCon -- Representation tycon
  58. }
  59. -- Obtain the representation tycon of a family instance.
  60. --
  61. famInstTyCon :: FamInst -> TyCon
  62. famInstTyCon = fi_tycon
  63. famInstTyVars :: FamInst -> TyVarSet
  64. famInstTyVars = fi_tvs
  65. \end{code}
  66. \begin{code}
  67. instance NamedThing FamInst where
  68. getName = getName . fi_tycon
  69. instance Outputable FamInst where
  70. ppr = pprFamInst
  71. -- Prints the FamInst as a family instance declaration
  72. pprFamInst :: FamInst -> SDoc
  73. pprFamInst famInst
  74. = hang (pprFamInstHdr famInst)
  75. 2 (vcat [ ifPprDebug (ptext (sLit "Coercion axiom:") <+> pp_ax)
  76. , ptext (sLit "--") <+> pprDefinedAt (getName famInst)])
  77. where
  78. pp_ax = case tyConFamilyCoercion_maybe (fi_tycon famInst) of
  79. Just ax -> ppr ax
  80. Nothing -> ptext (sLit "<not there!>")
  81. pprFamInstHdr :: FamInst -> SDoc
  82. pprFamInstHdr (FamInst {fi_tycon = rep_tc})
  83. = pprTyConSort <+> pp_instance <+> pprHead
  84. where
  85. Just (fam_tc, tys) = tyConFamInst_maybe rep_tc
  86. -- For *associated* types, say "type T Int = blah"
  87. -- For *top level* type instances, say "type instance T Int = blah"
  88. pp_instance
  89. | isTyConAssoc fam_tc = empty
  90. | otherwise = ptext (sLit "instance")
  91. pprHead = pprTypeApp fam_tc tys
  92. pprTyConSort | isDataTyCon rep_tc = ptext (sLit "data")
  93. | isNewTyCon rep_tc = ptext (sLit "newtype")
  94. | isSynTyCon rep_tc = ptext (sLit "type")
  95. | isAbstractTyCon rep_tc = ptext (sLit "data")
  96. | otherwise = panic "FamInstEnv.pprFamInstHdr"
  97. pprFamInsts :: [FamInst] -> SDoc
  98. pprFamInsts finsts = vcat (map pprFamInst finsts)
  99. famInstHead :: FamInst -> ([TyVar], TyCon, [Type])
  100. famInstHead (FamInst {fi_tycon = tycon})
  101. = case tyConFamInst_maybe tycon of
  102. Nothing -> panic "FamInstEnv.famInstHead"
  103. Just (fam, tys) -> (tyConTyVars tycon, fam, tys)
  104. -- Make a family instance representation from a tycon. This is used for local
  105. -- instances, where we can safely pull on the tycon.
  106. --
  107. mkLocalFamInst :: TyCon -> FamInst
  108. mkLocalFamInst tycon
  109. = case tyConFamInst_maybe tycon of
  110. Nothing -> panic "FamInstEnv.mkLocalFamInst"
  111. Just (fam, tys) ->
  112. FamInst {
  113. fi_fam = tyConName fam,
  114. fi_tcs = roughMatchTcs tys,
  115. fi_tvs = mkVarSet . tyConTyVars $ tycon,
  116. fi_tys = tys,
  117. fi_tycon = tycon
  118. }
  119. -- Make a family instance representation from the information found in an
  120. -- unterface file. In particular, we get the rough match info from the iface
  121. -- (instead of computing it here).
  122. --
  123. mkImportedFamInst :: Name -> [Maybe Name] -> TyCon -> FamInst
  124. mkImportedFamInst fam mb_tcs tycon
  125. = FamInst {
  126. fi_fam = fam,
  127. fi_tcs = mb_tcs,
  128. fi_tvs = mkVarSet . tyConTyVars $ tycon,
  129. fi_tys = case tyConFamInst_maybe tycon of
  130. Nothing -> panic "FamInstEnv.mkImportedFamInst"
  131. Just (_, tys) -> tys,
  132. fi_tycon = tycon
  133. }
  134. \end{code}
  135. %************************************************************************
  136. %* *
  137. FamInstEnv
  138. %* *
  139. %************************************************************************
  140. Note [FamInstEnv]
  141. ~~~~~~~~~~~~~~~~~~~~~
  142. A FamInstEnv maps a family name to the list of known instances for that family.
  143. The same FamInstEnv includes both 'data family' and 'type family' instances.
  144. Type families are reduced during type inference, but not data families;
  145. the user explains when to use a data family instance by using contructors
  146. and pattern matching.
  147. Neverthless it is still useful to have data families in the FamInstEnv:
  148. - For finding overlaps and conflicts
  149. - For finding the representation type...see FamInstEnv.topNormaliseType
  150. and its call site in Simplify
  151. - In standalone deriving instance Eq (T [Int]) we need to find the
  152. representation type for T [Int]
  153. \begin{code}
  154. type FamInstEnv = UniqFM FamilyInstEnv -- Maps a family to its instances
  155. -- See Note [FamInstEnv]
  156. type FamInstEnvs = (FamInstEnv, FamInstEnv)
  157. -- External package inst-env, Home-package inst-env
  158. data FamilyInstEnv
  159. = FamIE [FamInst] -- The instances for a particular family, in any order
  160. Bool -- True <=> there is an instance of form T a b c
  161. -- If *not* then the common case of looking up
  162. -- (T a b c) can fail immediately
  163. instance Outputable FamilyInstEnv where
  164. ppr (FamIE fs b) = ptext (sLit "FamIE") <+> ppr b <+> vcat (map ppr fs)
  165. -- INVARIANTS:
  166. -- * The fs_tvs are distinct in each FamInst
  167. -- of a range value of the map (so we can safely unify them)
  168. emptyFamInstEnvs :: (FamInstEnv, FamInstEnv)
  169. emptyFamInstEnvs = (emptyFamInstEnv, emptyFamInstEnv)
  170. emptyFamInstEnv :: FamInstEnv
  171. emptyFamInstEnv = emptyUFM
  172. famInstEnvElts :: FamInstEnv -> [FamInst]
  173. famInstEnvElts fi = [elt | FamIE elts _ <- eltsUFM fi, elt <- elts]
  174. familyInstances :: (FamInstEnv, FamInstEnv) -> TyCon -> [FamInst]
  175. familyInstances (pkg_fie, home_fie) fam
  176. = get home_fie ++ get pkg_fie
  177. where
  178. get env = case lookupUFM env fam of
  179. Just (FamIE insts _) -> insts
  180. Nothing -> []
  181. extendFamInstEnvList :: FamInstEnv -> [FamInst] -> FamInstEnv
  182. extendFamInstEnvList inst_env fis = foldl extendFamInstEnv inst_env fis
  183. extendFamInstEnv :: FamInstEnv -> FamInst -> FamInstEnv
  184. extendFamInstEnv inst_env ins_item@(FamInst {fi_fam = cls_nm, fi_tcs = mb_tcs})
  185. = addToUFM_C add inst_env cls_nm (FamIE [ins_item] ins_tyvar)
  186. where
  187. add (FamIE items tyvar) _ = FamIE (ins_item:items)
  188. (ins_tyvar || tyvar)
  189. ins_tyvar = not (any isJust mb_tcs)
  190. overwriteFamInstEnv :: FamInstEnv -> FamInst -> FamInstEnv
  191. overwriteFamInstEnv inst_env ins_item@(FamInst {fi_fam = cls_nm, fi_tcs = mb_tcs})
  192. = addToUFM_C add inst_env cls_nm (FamIE [ins_item] ins_tyvar)
  193. where
  194. add (FamIE items tyvar) _ = FamIE (replaceFInst items)
  195. (ins_tyvar || tyvar)
  196. ins_tyvar = not (any isJust mb_tcs)
  197. match _ tpl_tvs tpl_tys tys = tcMatchTys tpl_tvs tpl_tys tys
  198. inst_tycon = famInstTyCon ins_item
  199. (fam, tys) = expectJust "FamInstEnv.lookuFamInstEnvConflicts"
  200. (tyConFamInst_maybe inst_tycon)
  201. arity = tyConArity fam
  202. n_tys = length tys
  203. match_tys
  204. | arity > n_tys = take arity tys
  205. | otherwise = tys
  206. rough_tcs = roughMatchTcs match_tys
  207. replaceFInst [] = [ins_item]
  208. replaceFInst (item@(FamInst { fi_tcs = mb_tcs, fi_tvs = tpl_tvs,
  209. fi_tys = tpl_tys }) : rest)
  210. -- Fast check for no match, uses the "rough match" fields
  211. | instanceCantMatch rough_tcs mb_tcs
  212. = item : replaceFInst rest
  213. -- Proper check
  214. | Just _ <- match item tpl_tvs tpl_tys match_tys
  215. = ins_item : rest
  216. -- No match => try next
  217. | otherwise
  218. = item : replaceFInst rest
  219. \end{code}
  220. %************************************************************************
  221. %* *
  222. Looking up a family instance
  223. %* *
  224. %************************************************************************
  225. @lookupFamInstEnv@ looks up in a @FamInstEnv@, using a one-way match.
  226. Multiple matches are only possible in case of type families (not data
  227. families), and then, it doesn't matter which match we choose (as the
  228. instances are guaranteed confluent).
  229. We return the matching family instances and the type instance at which it
  230. matches. For example, if we lookup 'T [Int]' and have a family instance
  231. data instance T [a] = ..
  232. desugared to
  233. data :R42T a = ..
  234. coe :Co:R42T a :: T [a] ~ :R42T a
  235. we return the matching instance '(FamInst{.., fi_tycon = :R42T}, Int)'.
  236. \begin{code}
  237. type FamInstMatch = (FamInst, [Type]) -- Matching type instance
  238. -- See Note [Over-saturated matches]
  239. lookupFamInstEnv
  240. :: FamInstEnvs
  241. -> TyCon -> [Type] -- What we are looking for
  242. -> [FamInstMatch] -- Successful matches
  243. -- Precondition: the tycon is saturated (or over-saturated)
  244. lookupFamInstEnv
  245. = lookup_fam_inst_env match True
  246. where
  247. match _ tpl_tvs tpl_tys tys = tcMatchTys tpl_tvs tpl_tys tys
  248. lookupFamInstEnvConflicts
  249. :: FamInstEnvs
  250. -> FamInst -- Putative new instance
  251. -> [TyVar] -- Unique tyvars, matching arity of FamInst
  252. -> [FamInstMatch] -- Conflicting matches
  253. -- E.g. when we are about to add
  254. -- f : type instance F [a] = a->a
  255. -- we do (lookupFamInstConflicts f [b])
  256. -- to find conflicting matches
  257. -- The skolem tyvars are needed because we don't have a
  258. -- unique supply to hand
  259. --
  260. -- Precondition: the tycon is saturated (or over-saturated)
  261. lookupFamInstEnvConflicts envs fam_inst skol_tvs
  262. = lookup_fam_inst_env my_unify False envs fam tys1
  263. where
  264. inst_tycon = famInstTyCon fam_inst
  265. (fam, tys) = expectJust "FamInstEnv.lookuFamInstEnvConflicts"
  266. (tyConFamInst_maybe inst_tycon)
  267. skol_tys = mkTyVarTys skol_tvs
  268. tys1 = substTys (zipTopTvSubst (tyConTyVars inst_tycon) skol_tys) tys
  269. -- In example above, fam tys' = F [b]
  270. my_unify old_fam_inst tpl_tvs tpl_tys match_tys
  271. = ASSERT2( tyVarsOfTypes tys1 `disjointVarSet` tpl_tvs,
  272. (ppr fam <+> ppr tys1) $$
  273. (ppr tpl_tvs <+> ppr tpl_tys) )
  274. -- Unification will break badly if the variables overlap
  275. -- They shouldn't because we allocate separate uniques for them
  276. case tcUnifyTys instanceBindFun tpl_tys match_tys of
  277. Just subst | conflicting old_fam_inst subst -> Just subst
  278. _other -> Nothing
  279. -- Note [Family instance overlap conflicts]
  280. conflicting old_fam_inst subst
  281. | isAlgTyCon fam = True
  282. | otherwise = not (old_rhs `eqType` new_rhs)
  283. where
  284. old_tycon = famInstTyCon old_fam_inst
  285. old_tvs = tyConTyVars old_tycon
  286. old_rhs = mkTyConApp old_tycon (substTyVars subst old_tvs)
  287. new_rhs = mkTyConApp inst_tycon (substTyVars subst skol_tvs)
  288. -- This variant is called when we want to check if the conflict is only in the
  289. -- home environment (see FamInst.addLocalFamInst)
  290. lookupFamInstEnvConflicts' :: FamInstEnv -> FamInst -> [TyVar] -> [FamInstMatch]
  291. lookupFamInstEnvConflicts' env fam_inst skol_tvs
  292. = lookupFamInstEnvConflicts (emptyFamInstEnv, env) fam_inst skol_tvs
  293. \end{code}
  294. Note [Family instance overlap conflicts]
  295. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  296. - In the case of data family instances, any overlap is fundamentally a
  297. conflict (as these instances imply injective type mappings).
  298. - In the case of type family instances, overlap is admitted as long as
  299. the right-hand sides of the overlapping rules coincide under the
  300. overlap substitution. eg
  301. type instance F a Int = a
  302. type instance F Int b = b
  303. These two overlap on (F Int Int) but then both RHSs are Int,
  304. so all is well. We require that they are syntactically equal;
  305. anything else would be difficult to test for at this stage.
  306. While @lookupFamInstEnv@ uses a one-way match, the next function
  307. @lookupFamInstEnvConflicts@ uses two-way matching (ie, unification). This is
  308. needed to check for overlapping instances.
  309. For class instances, these two variants of lookup are combined into one
  310. function (cf, @InstEnv@). We don't do that for family instances as the
  311. results of matching and unification are used in two different contexts.
  312. Moreover, matching is the wildly more frequently used operation in the case of
  313. indexed synonyms and we don't want to slow that down by needless unification.
  314. \begin{code}
  315. ------------------------------------------------------------
  316. -- Might be a one-way match or a unifier
  317. type MatchFun = FamInst -- The FamInst template
  318. -> TyVarSet -> [Type] -- fi_tvs, fi_tys of that FamInst
  319. -> [Type] -- Target to match against
  320. -> Maybe TvSubst
  321. type OneSidedMatch = Bool -- Are optimisations that are only valid for
  322. -- one sided matches allowed?
  323. lookup_fam_inst_env' -- The worker, local to this module
  324. :: MatchFun
  325. -> OneSidedMatch
  326. -> FamInstEnv
  327. -> TyCon -> [Type] -- What we are looking for
  328. -> [FamInstMatch] -- Successful matches
  329. lookup_fam_inst_env' match_fun one_sided ie fam tys
  330. | not (isFamilyTyCon fam)
  331. = []
  332. | otherwise
  333. = ASSERT2( n_tys >= arity, ppr fam <+> ppr tys ) -- Family type applications must be saturated
  334. lookup ie
  335. where
  336. -- See Note [Over-saturated matches]
  337. arity = tyConArity fam
  338. n_tys = length tys
  339. extra_tys = drop arity tys
  340. (match_tys, add_extra_tys)
  341. | arity > n_tys = (take arity tys, \res_tys -> res_tys ++ extra_tys)
  342. | otherwise = (tys, \res_tys -> res_tys)
  343. -- The second case is the common one, hence functional representation
  344. --------------
  345. rough_tcs = roughMatchTcs match_tys
  346. all_tvs = all isNothing rough_tcs && one_sided
  347. --------------
  348. lookup env = case lookupUFM env fam of
  349. Nothing -> [] -- No instances for this class
  350. Just (FamIE insts has_tv_insts)
  351. -- Short cut for common case:
  352. -- The thing we are looking up is of form (C a
  353. -- b c), and the FamIE has no instances of
  354. -- that form, so don't bother to search
  355. | all_tvs && not has_tv_insts -> []
  356. | otherwise -> find insts
  357. --------------
  358. find [] = []
  359. find (item@(FamInst { fi_tcs = mb_tcs, fi_tvs = tpl_tvs,
  360. fi_tys = tpl_tys, fi_tycon = tycon }) : rest)
  361. -- Fast check for no match, uses the "rough match" fields
  362. | instanceCantMatch rough_tcs mb_tcs
  363. = find rest
  364. -- Proper check
  365. | Just subst <- match_fun item tpl_tvs tpl_tys match_tys
  366. = (item, add_extra_tys $ substTyVars subst (tyConTyVars tycon)) : find rest
  367. -- No match => try next
  368. | otherwise
  369. = find rest
  370. -- Precondition: the tycon is saturated (or over-saturated)
  371. lookup_fam_inst_env -- The worker, local to this module
  372. :: MatchFun
  373. -> OneSidedMatch
  374. -> FamInstEnvs
  375. -> TyCon -> [Type] -- What we are looking for
  376. -> [FamInstMatch] -- Successful matches
  377. -- Precondition: the tycon is saturated (or over-saturated)
  378. lookup_fam_inst_env match_fun one_sided (pkg_ie, home_ie) fam tys =
  379. lookup_fam_inst_env' match_fun one_sided home_ie fam tys ++
  380. lookup_fam_inst_env' match_fun one_sided pkg_ie fam tys
  381. \end{code}
  382. Note [Over-saturated matches]
  383. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  384. It's ok to look up an over-saturated type constructor. E.g.
  385. type family F a :: * -> *
  386. type instance F (a,b) = Either (a->b)
  387. The type instance gives rise to a newtype TyCon (at a higher kind
  388. which you can't do in Haskell!):
  389. newtype FPair a b = FP (Either (a->b))
  390. Then looking up (F (Int,Bool) Char) will return a FamInstMatch
  391. (FPair, [Int,Bool,Char])
  392. The "extra" type argument [Char] just stays on the end.
  393. %************************************************************************
  394. %* *
  395. Looking up a family instance
  396. %* *
  397. %************************************************************************
  398. \begin{code}
  399. topNormaliseType :: FamInstEnvs
  400. -> Type
  401. -> Maybe (Coercion, Type)
  402. -- Get rid of *outermost* (or toplevel)
  403. -- * type functions
  404. -- * newtypes
  405. -- using appropriate coercions.
  406. -- By "outer" we mean that toplevelNormaliseType guarantees to return
  407. -- a type that does not have a reducible redex (F ty1 .. tyn) as its
  408. -- outermost form. It *can* return something like (Maybe (F ty)), where
  409. -- (F ty) is a redex.
  410. -- Its a bit like Type.repType, but handles type families too
  411. topNormaliseType env ty
  412. = go [] ty
  413. where
  414. go :: [TyCon] -> Type -> Maybe (Coercion, Type)
  415. go rec_nts ty | Just ty' <- coreView ty -- Expand synonyms
  416. = go rec_nts ty'
  417. go rec_nts (TyConApp tc tys)
  418. | isNewTyCon tc -- Expand newtypes
  419. = if tc `elem` rec_nts -- See Note [Expanding newtypes] in Type.lhs
  420. then Nothing
  421. else let nt_co = mkAxInstCo (newTyConCo tc) tys
  422. in add_co nt_co rec_nts' nt_rhs
  423. | isFamilyTyCon tc -- Expand open tycons
  424. , (co, ty) <- normaliseTcApp env tc tys
  425. -- Note that normaliseType fully normalises 'tys',
  426. -- It has do to so to be sure that nested calls like
  427. -- F (G Int)
  428. -- are correctly top-normalised
  429. , not (isReflCo co)
  430. = add_co co rec_nts ty
  431. where
  432. nt_rhs = newTyConInstRhs tc tys
  433. rec_nts' | isRecursiveTyCon tc = tc:rec_nts
  434. | otherwise = rec_nts
  435. go _ _ = Nothing
  436. add_co co rec_nts ty
  437. = case go rec_nts ty of
  438. Nothing -> Just (co, ty)
  439. Just (co', ty') -> Just (mkTransCo co co', ty')
  440. ---------------
  441. normaliseTcApp :: FamInstEnvs -> TyCon -> [Type] -> (Coercion, Type)
  442. normaliseTcApp env tc tys
  443. | isFamilyTyCon tc
  444. , tyConArity tc <= length tys -- Unsaturated data families are possible
  445. , [(fam_inst, inst_tys)] <- lookupFamInstEnv env tc ntys
  446. = let -- A matching family instance exists
  447. rep_tc = famInstTyCon fam_inst
  448. co_tycon = expectJust "lookupFamInst" (tyConFamilyCoercion_maybe rep_tc)
  449. co = mkAxInstCo co_tycon inst_tys
  450. first_coi = mkTransCo tycon_coi co
  451. (rest_coi,nty) = normaliseType env (mkTyConApp rep_tc inst_tys)
  452. fix_coi = mkTransCo first_coi rest_coi
  453. in
  454. (fix_coi, nty)
  455. | otherwise -- No unique matching family instance exists;
  456. -- we do not do anything
  457. = (tycon_coi, TyConApp tc ntys)
  458. where
  459. -- Normalise the arg types so that they'll match
  460. -- when we lookup in in the instance envt
  461. (cois, ntys) = mapAndUnzip (normaliseType env) tys
  462. tycon_coi = mkTyConAppCo tc cois
  463. ---------------
  464. normaliseType :: FamInstEnvs -- environment with family instances
  465. -> Type -- old type
  466. -> (Coercion, Type) -- (coercion,new type), where
  467. -- co :: old-type ~ new_type
  468. -- Normalise the input type, by eliminating *all* type-function redexes
  469. -- Returns with Refl if nothing happens
  470. normaliseType env ty
  471. | Just ty' <- coreView ty = normaliseType env ty'
  472. normaliseType env (TyConApp tc tys)
  473. = normaliseTcApp env tc tys
  474. normaliseType env (AppTy ty1 ty2)
  475. = let (coi1,nty1) = normaliseType env ty1
  476. (coi2,nty2) = normaliseType env ty2
  477. in (mkAppCo coi1 coi2, mkAppTy nty1 nty2)
  478. normaliseType env (FunTy ty1 ty2)
  479. = let (coi1,nty1) = normaliseType env ty1
  480. (coi2,nty2) = normaliseType env ty2
  481. in (mkFunCo coi1 coi2, mkFunTy nty1 nty2)
  482. normaliseType env (ForAllTy tyvar ty1)
  483. = let (coi,nty1) = normaliseType env ty1
  484. in (mkForAllCo tyvar coi, ForAllTy tyvar nty1)
  485. normaliseType _ ty@(TyVarTy _)
  486. = (Refl ty,ty)
  487. \end{code}