PageRenderTime 58ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/compiler/iface/IfaceType.hs

http://github.com/ghc/ghc
Haskell | 1376 lines | 1043 code | 185 blank | 148 comment | 38 complexity | feb1a4c747325c7b0f36fa8f09854429 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, GPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. {-
  2. (c) The University of Glasgow 2006
  3. (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
  4. This module defines interface types and binders
  5. -}
  6. {-# LANGUAGE CPP, FlexibleInstances #-}
  7. -- FlexibleInstances for Binary (DefMethSpec IfaceType)
  8. module IfaceType (
  9. IfExtName, IfLclName,
  10. IfaceType(..), IfacePredType, IfaceKind, IfaceCoercion(..),
  11. IfaceUnivCoProv(..),
  12. IfaceTyCon(..), IfaceTyConInfo(..),
  13. IfaceTyLit(..), IfaceTcArgs(..),
  14. IfaceContext, IfaceBndr(..), IfaceOneShot(..), IfaceLamBndr,
  15. IfaceTvBndr, IfaceIdBndr, IfaceTyConBinder,
  16. IfaceForAllBndr, ArgFlag(..),
  17. ifConstraintKind, ifTyConBinderTyVar, ifTyConBinderName,
  18. -- Equality testing
  19. IfRnEnv2, emptyIfRnEnv2, eqIfaceType, eqIfaceTypes,
  20. eqIfaceTcArgs, eqIfaceTvBndrs, isIfaceLiftedTypeKind,
  21. -- Conversion from Type -> IfaceType
  22. toIfaceType, toIfaceTypes, toIfaceKind, toIfaceTyVar,
  23. toIfaceContext, toIfaceBndr, toIfaceIdBndr,
  24. toIfaceTyCon, toIfaceTyCon_name,
  25. toIfaceTcArgs, toIfaceTvBndr, toIfaceTvBndrs,
  26. toIfaceForAllBndr,
  27. -- Conversion from IfaceTcArgs -> IfaceType
  28. tcArgsIfaceTypes,
  29. -- Conversion from Coercion -> IfaceCoercion
  30. toIfaceCoercion,
  31. -- Printing
  32. pprIfaceType, pprParendIfaceType,
  33. pprIfaceContext, pprIfaceContextArr,
  34. pprIfaceIdBndr, pprIfaceLamBndr, pprIfaceTvBndr, pprIfaceTyConBinders,
  35. pprIfaceBndrs, pprIfaceTcArgs, pprParendIfaceTcArgs,
  36. pprIfaceForAllPart, pprIfaceForAll, pprIfaceSigmaType,
  37. pprIfaceCoercion, pprParendIfaceCoercion,
  38. splitIfaceSigmaTy, pprIfaceTypeApp, pprUserIfaceForAll,
  39. suppressIfaceInvisibles,
  40. stripIfaceInvisVars,
  41. stripInvisArgs,
  42. substIfaceType, substIfaceTyVar, substIfaceTcArgs, mkIfaceTySubst,
  43. eqIfaceTvBndr
  44. ) where
  45. #include "HsVersions.h"
  46. import Coercion
  47. import DataCon ( isTupleDataCon )
  48. import TcType
  49. import DynFlags
  50. import TyCoRep -- needs to convert core types to iface types
  51. import TyCon hiding ( pprPromotionQuote )
  52. import CoAxiom
  53. import Id
  54. import Var
  55. -- import RnEnv( FastStringEnv, mkFsEnv, lookupFsEnv )
  56. import TysWiredIn
  57. import TysPrim
  58. import PrelNames
  59. import Name
  60. import BasicTypes
  61. import Binary
  62. import Outputable
  63. import FastString
  64. import UniqSet
  65. import VarEnv
  66. import UniqFM
  67. import Util
  68. {-
  69. ************************************************************************
  70. * *
  71. Local (nested) binders
  72. * *
  73. ************************************************************************
  74. -}
  75. type IfLclName = FastString -- A local name in iface syntax
  76. type IfExtName = Name -- An External or WiredIn Name can appear in IfaceSyn
  77. -- (However Internal or System Names never should)
  78. data IfaceBndr -- Local (non-top-level) binders
  79. = IfaceIdBndr {-# UNPACK #-} !IfaceIdBndr
  80. | IfaceTvBndr {-# UNPACK #-} !IfaceTvBndr
  81. type IfaceIdBndr = (IfLclName, IfaceType)
  82. type IfaceTvBndr = (IfLclName, IfaceKind)
  83. ifaceTvBndrName :: IfaceTvBndr -> IfLclName
  84. ifaceTvBndrName (n,_) = n
  85. type IfaceLamBndr = (IfaceBndr, IfaceOneShot)
  86. data IfaceOneShot -- See Note [Preserve OneShotInfo] in CoreTicy
  87. = IfaceNoOneShot -- and Note [The oneShot function] in MkId
  88. | IfaceOneShot
  89. {-
  90. %************************************************************************
  91. %* *
  92. IfaceType
  93. %* *
  94. %************************************************************************
  95. -}
  96. -------------------------------
  97. type IfaceKind = IfaceType
  98. data IfaceType -- A kind of universal type, used for types and kinds
  99. = IfaceTyVar IfLclName -- Type/coercion variable only, not tycon
  100. | IfaceLitTy IfaceTyLit
  101. | IfaceAppTy IfaceType IfaceType
  102. | IfaceFunTy IfaceType IfaceType
  103. | IfaceDFunTy IfaceType IfaceType
  104. | IfaceForAllTy IfaceForAllBndr IfaceType
  105. | IfaceTyConApp IfaceTyCon IfaceTcArgs -- Not necessarily saturated
  106. -- Includes newtypes, synonyms, tuples
  107. | IfaceCastTy IfaceType IfaceCoercion
  108. | IfaceCoercionTy IfaceCoercion
  109. | IfaceTupleTy -- Saturated tuples (unsaturated ones use IfaceTyConApp)
  110. TupleSort IfaceTyConInfo -- A bit like IfaceTyCon
  111. IfaceTcArgs -- arity = length args
  112. -- For promoted data cons, the kind args are omitted
  113. type IfacePredType = IfaceType
  114. type IfaceContext = [IfacePredType]
  115. data IfaceTyLit
  116. = IfaceNumTyLit Integer
  117. | IfaceStrTyLit FastString
  118. deriving (Eq)
  119. type IfaceTyConBinder = TyVarBndr IfaceTvBndr TyConBndrVis
  120. type IfaceForAllBndr = TyVarBndr IfaceTvBndr ArgFlag
  121. -- See Note [Suppressing invisible arguments]
  122. -- We use a new list type (rather than [(IfaceType,Bool)], because
  123. -- it'll be more compact and faster to parse in interface
  124. -- files. Rather than two bytes and two decisions (nil/cons, and
  125. -- type/kind) there'll just be one.
  126. data IfaceTcArgs
  127. = ITC_Nil
  128. | ITC_Vis IfaceType IfaceTcArgs -- "Vis" means show when pretty-printing
  129. | ITC_Invis IfaceKind IfaceTcArgs -- "Invis" means don't show when pretty-printing
  130. -- except with -fprint-explicit-kinds
  131. -- Encodes type constructors, kind constructors,
  132. -- coercion constructors, the lot.
  133. -- We have to tag them in order to pretty print them
  134. -- properly.
  135. data IfaceTyCon = IfaceTyCon { ifaceTyConName :: IfExtName
  136. , ifaceTyConInfo :: IfaceTyConInfo }
  137. deriving (Eq)
  138. data IfaceTyConInfo -- Used to guide pretty-printing
  139. -- and to disambiguate D from 'D (they share a name)
  140. = NoIfaceTyConInfo
  141. | IfacePromotedDataCon
  142. deriving (Eq)
  143. data IfaceCoercion
  144. = IfaceReflCo Role IfaceType
  145. | IfaceFunCo Role IfaceCoercion IfaceCoercion
  146. | IfaceTyConAppCo Role IfaceTyCon [IfaceCoercion]
  147. | IfaceAppCo IfaceCoercion IfaceCoercion
  148. | IfaceForAllCo IfaceTvBndr IfaceCoercion IfaceCoercion
  149. | IfaceCoVarCo IfLclName
  150. | IfaceAxiomInstCo IfExtName BranchIndex [IfaceCoercion]
  151. | IfaceUnivCo IfaceUnivCoProv Role IfaceType IfaceType
  152. | IfaceSymCo IfaceCoercion
  153. | IfaceTransCo IfaceCoercion IfaceCoercion
  154. | IfaceNthCo Int IfaceCoercion
  155. | IfaceLRCo LeftOrRight IfaceCoercion
  156. | IfaceInstCo IfaceCoercion IfaceCoercion
  157. | IfaceCoherenceCo IfaceCoercion IfaceCoercion
  158. | IfaceKindCo IfaceCoercion
  159. | IfaceSubCo IfaceCoercion
  160. | IfaceAxiomRuleCo IfLclName [IfaceCoercion]
  161. data IfaceUnivCoProv
  162. = IfaceUnsafeCoerceProv
  163. | IfacePhantomProv IfaceCoercion
  164. | IfaceProofIrrelProv IfaceCoercion
  165. | IfacePluginProv String
  166. -- this constant is needed for dealing with pretty-printing classes
  167. ifConstraintKind :: IfaceKind
  168. ifConstraintKind = IfaceTyConApp (IfaceTyCon { ifaceTyConName = getName constraintKindTyCon
  169. , ifaceTyConInfo = NoIfaceTyConInfo })
  170. ITC_Nil
  171. {-
  172. %************************************************************************
  173. %* *
  174. Functions over IFaceTypes
  175. * *
  176. ************************************************************************
  177. -}
  178. eqIfaceTvBndr :: IfaceTvBndr -> IfaceTvBndr -> Bool
  179. eqIfaceTvBndr (occ1, _) (occ2, _) = occ1 == occ2
  180. isIfaceLiftedTypeKind :: IfaceKind -> Bool
  181. isIfaceLiftedTypeKind (IfaceTyConApp tc ITC_Nil)
  182. = isLiftedTypeKindTyConName (ifaceTyConName tc)
  183. isIfaceLiftedTypeKind (IfaceTyConApp tc
  184. (ITC_Vis (IfaceTyConApp ptr_rep_lifted ITC_Nil) ITC_Nil))
  185. = ifaceTyConName tc == tYPETyConName
  186. && ifaceTyConName ptr_rep_lifted `hasKey` ptrRepLiftedDataConKey
  187. isIfaceLiftedTypeKind _ = False
  188. splitIfaceSigmaTy :: IfaceType -> ([IfaceForAllBndr], [IfacePredType], IfaceType)
  189. -- Mainly for printing purposes
  190. splitIfaceSigmaTy ty
  191. = (bndrs, theta, tau)
  192. where
  193. (bndrs, rho) = split_foralls ty
  194. (theta, tau) = split_rho rho
  195. split_foralls (IfaceForAllTy bndr ty)
  196. = case split_foralls ty of { (bndrs, rho) -> (bndr:bndrs, rho) }
  197. split_foralls rho = ([], rho)
  198. split_rho (IfaceDFunTy ty1 ty2)
  199. = case split_rho ty2 of { (ps, tau) -> (ty1:ps, tau) }
  200. split_rho tau = ([], tau)
  201. suppressIfaceInvisibles :: DynFlags -> [IfaceTyConBinder] -> [a] -> [a]
  202. suppressIfaceInvisibles dflags tys xs
  203. | gopt Opt_PrintExplicitKinds dflags = xs
  204. | otherwise = suppress tys xs
  205. where
  206. suppress _ [] = []
  207. suppress [] a = a
  208. suppress (k:ks) a@(_:xs)
  209. | isInvisibleTyConBinder k = suppress ks xs
  210. | otherwise = a
  211. stripIfaceInvisVars :: DynFlags -> [IfaceTyConBinder] -> [IfaceTyConBinder]
  212. stripIfaceInvisVars dflags tyvars
  213. | gopt Opt_PrintExplicitKinds dflags = tyvars
  214. | otherwise = filterOut isInvisibleTyConBinder tyvars
  215. -- | Extract a IfaceTvBndr from a IfaceTyConBinder
  216. ifTyConBinderTyVar :: IfaceTyConBinder -> IfaceTvBndr
  217. ifTyConBinderTyVar = binderVar
  218. -- | Extract the variable name from a IfaceTyConBinder
  219. ifTyConBinderName :: IfaceTyConBinder -> IfLclName
  220. ifTyConBinderName tcb = ifaceTvBndrName (ifTyConBinderTyVar tcb)
  221. ifTyVarsOfType :: IfaceType -> UniqSet IfLclName
  222. ifTyVarsOfType ty
  223. = case ty of
  224. IfaceTyVar v -> unitUniqSet v
  225. IfaceAppTy fun arg
  226. -> ifTyVarsOfType fun `unionUniqSets` ifTyVarsOfType arg
  227. IfaceFunTy arg res
  228. -> ifTyVarsOfType arg `unionUniqSets` ifTyVarsOfType res
  229. IfaceDFunTy arg res
  230. -> ifTyVarsOfType arg `unionUniqSets` ifTyVarsOfType res
  231. IfaceForAllTy bndr ty
  232. -> let (free, bound) = ifTyVarsOfForAllBndr bndr in
  233. delListFromUniqSet (ifTyVarsOfType ty) bound `unionUniqSets` free
  234. IfaceTyConApp _ args -> ifTyVarsOfArgs args
  235. IfaceLitTy _ -> emptyUniqSet
  236. IfaceCastTy ty co
  237. -> ifTyVarsOfType ty `unionUniqSets` ifTyVarsOfCoercion co
  238. IfaceCoercionTy co -> ifTyVarsOfCoercion co
  239. IfaceTupleTy _ _ args -> ifTyVarsOfArgs args
  240. ifTyVarsOfForAllBndr :: IfaceForAllBndr
  241. -> ( UniqSet IfLclName -- names used free in the binder
  242. , [IfLclName] ) -- names bound by this binder
  243. ifTyVarsOfForAllBndr (TvBndr (name, kind) _) = (ifTyVarsOfType kind, [name])
  244. ifTyVarsOfArgs :: IfaceTcArgs -> UniqSet IfLclName
  245. ifTyVarsOfArgs args = argv emptyUniqSet args
  246. where
  247. argv vs (ITC_Vis t ts) = argv (vs `unionUniqSets` (ifTyVarsOfType t)) ts
  248. argv vs (ITC_Invis k ks) = argv (vs `unionUniqSets` (ifTyVarsOfType k)) ks
  249. argv vs ITC_Nil = vs
  250. ifTyVarsOfCoercion :: IfaceCoercion -> UniqSet IfLclName
  251. ifTyVarsOfCoercion = go
  252. where
  253. go (IfaceReflCo _ ty) = ifTyVarsOfType ty
  254. go (IfaceFunCo _ c1 c2) = go c1 `unionUniqSets` go c2
  255. go (IfaceTyConAppCo _ _ cos) = ifTyVarsOfCoercions cos
  256. go (IfaceAppCo c1 c2) = go c1 `unionUniqSets` go c2
  257. go (IfaceForAllCo (bound, _) kind_co co)
  258. = go co `delOneFromUniqSet` bound `unionUniqSets` go kind_co
  259. go (IfaceCoVarCo cv) = unitUniqSet cv
  260. go (IfaceAxiomInstCo _ _ cos) = ifTyVarsOfCoercions cos
  261. go (IfaceUnivCo p _ ty1 ty2) = go_prov p `unionUniqSets`
  262. ifTyVarsOfType ty1 `unionUniqSets`
  263. ifTyVarsOfType ty2
  264. go (IfaceSymCo co) = go co
  265. go (IfaceTransCo c1 c2) = go c1 `unionUniqSets` go c2
  266. go (IfaceNthCo _ co) = go co
  267. go (IfaceLRCo _ co) = go co
  268. go (IfaceInstCo c1 c2) = go c1 `unionUniqSets` go c2
  269. go (IfaceCoherenceCo c1 c2) = go c1 `unionUniqSets` go c2
  270. go (IfaceKindCo co) = go co
  271. go (IfaceSubCo co) = go co
  272. go (IfaceAxiomRuleCo rule cos)
  273. = unionManyUniqSets
  274. [ unitUniqSet rule
  275. , ifTyVarsOfCoercions cos ]
  276. go_prov IfaceUnsafeCoerceProv = emptyUniqSet
  277. go_prov (IfacePhantomProv co) = go co
  278. go_prov (IfaceProofIrrelProv co) = go co
  279. go_prov (IfacePluginProv _) = emptyUniqSet
  280. ifTyVarsOfCoercions :: [IfaceCoercion] -> UniqSet IfLclName
  281. ifTyVarsOfCoercions = foldr (unionUniqSets . ifTyVarsOfCoercion) emptyUniqSet
  282. {-
  283. Substitutions on IfaceType. This is only used during pretty-printing to construct
  284. the result type of a GADT, and does not deal with binders (eg IfaceForAll), so
  285. it doesn't need fancy capture stuff.
  286. -}
  287. type IfaceTySubst = FastStringEnv IfaceType
  288. mkIfaceTySubst :: [IfaceTvBndr] -> [IfaceType] -> IfaceTySubst
  289. mkIfaceTySubst tvs tys = mkFsEnv $ zipWithEqual "mkIfaceTySubst" (\(fs,_) ty -> (fs,ty)) tvs tys
  290. substIfaceType :: IfaceTySubst -> IfaceType -> IfaceType
  291. substIfaceType env ty
  292. = go ty
  293. where
  294. go (IfaceTyVar tv) = substIfaceTyVar env tv
  295. go (IfaceAppTy t1 t2) = IfaceAppTy (go t1) (go t2)
  296. go (IfaceFunTy t1 t2) = IfaceFunTy (go t1) (go t2)
  297. go (IfaceDFunTy t1 t2) = IfaceDFunTy (go t1) (go t2)
  298. go ty@(IfaceLitTy {}) = ty
  299. go (IfaceTyConApp tc tys) = IfaceTyConApp tc (substIfaceTcArgs env tys)
  300. go (IfaceTupleTy s i tys) = IfaceTupleTy s i (substIfaceTcArgs env tys)
  301. go (IfaceForAllTy {}) = pprPanic "substIfaceType" (ppr ty)
  302. go (IfaceCastTy ty co) = IfaceCastTy (go ty) (go_co co)
  303. go (IfaceCoercionTy co) = IfaceCoercionTy (go_co co)
  304. go_co (IfaceReflCo r ty) = IfaceReflCo r (go ty)
  305. go_co (IfaceFunCo r c1 c2) = IfaceFunCo r (go_co c1) (go_co c2)
  306. go_co (IfaceTyConAppCo r tc cos) = IfaceTyConAppCo r tc (go_cos cos)
  307. go_co (IfaceAppCo c1 c2) = IfaceAppCo (go_co c1) (go_co c2)
  308. go_co (IfaceForAllCo {}) = pprPanic "substIfaceCoercion" (ppr ty)
  309. go_co (IfaceCoVarCo cv) = IfaceCoVarCo cv
  310. go_co (IfaceAxiomInstCo a i cos) = IfaceAxiomInstCo a i (go_cos cos)
  311. go_co (IfaceUnivCo prov r t1 t2) = IfaceUnivCo (go_prov prov) r (go t1) (go t2)
  312. go_co (IfaceSymCo co) = IfaceSymCo (go_co co)
  313. go_co (IfaceTransCo co1 co2) = IfaceTransCo (go_co co1) (go_co co2)
  314. go_co (IfaceNthCo n co) = IfaceNthCo n (go_co co)
  315. go_co (IfaceLRCo lr co) = IfaceLRCo lr (go_co co)
  316. go_co (IfaceInstCo c1 c2) = IfaceInstCo (go_co c1) (go_co c2)
  317. go_co (IfaceCoherenceCo c1 c2) = IfaceCoherenceCo (go_co c1) (go_co c2)
  318. go_co (IfaceKindCo co) = IfaceKindCo (go_co co)
  319. go_co (IfaceSubCo co) = IfaceSubCo (go_co co)
  320. go_co (IfaceAxiomRuleCo n cos) = IfaceAxiomRuleCo n (go_cos cos)
  321. go_cos = map go_co
  322. go_prov IfaceUnsafeCoerceProv = IfaceUnsafeCoerceProv
  323. go_prov (IfacePhantomProv co) = IfacePhantomProv (go_co co)
  324. go_prov (IfaceProofIrrelProv co) = IfaceProofIrrelProv (go_co co)
  325. go_prov (IfacePluginProv str) = IfacePluginProv str
  326. substIfaceTcArgs :: IfaceTySubst -> IfaceTcArgs -> IfaceTcArgs
  327. substIfaceTcArgs env args
  328. = go args
  329. where
  330. go ITC_Nil = ITC_Nil
  331. go (ITC_Vis ty tys) = ITC_Vis (substIfaceType env ty) (go tys)
  332. go (ITC_Invis ty tys) = ITC_Invis (substIfaceType env ty) (go tys)
  333. substIfaceTyVar :: IfaceTySubst -> IfLclName -> IfaceType
  334. substIfaceTyVar env tv
  335. | Just ty <- lookupFsEnv env tv = ty
  336. | otherwise = IfaceTyVar tv
  337. {-
  338. ************************************************************************
  339. * *
  340. Equality over IfaceTypes
  341. * *
  342. ************************************************************************
  343. Note [No kind check in ifaces]
  344. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  345. We check iface types for equality only when checking the consistency
  346. between two user-written signatures. In these cases, there is no possibility
  347. for a kind mismatch. So we omit the kind check (which would be impossible to
  348. write, anyway.)
  349. -}
  350. -- Like an RnEnv2, but mapping from FastString to deBruijn index
  351. -- DeBruijn; see eqTypeX
  352. type BoundVar = Int
  353. data IfRnEnv2
  354. = IRV2 { ifenvL :: UniqFM BoundVar -- from FastString
  355. , ifenvR :: UniqFM BoundVar
  356. , ifenv_next :: BoundVar
  357. }
  358. emptyIfRnEnv2 :: IfRnEnv2
  359. emptyIfRnEnv2 = IRV2 { ifenvL = emptyUFM
  360. , ifenvR = emptyUFM
  361. , ifenv_next = 0 }
  362. rnIfOccL :: IfRnEnv2 -> IfLclName -> Maybe BoundVar
  363. rnIfOccL env = lookupUFM (ifenvL env)
  364. rnIfOccR :: IfRnEnv2 -> IfLclName -> Maybe BoundVar
  365. rnIfOccR env = lookupUFM (ifenvR env)
  366. extendIfRnEnv2 :: IfRnEnv2 -> IfLclName -> IfLclName -> IfRnEnv2
  367. extendIfRnEnv2 IRV2 { ifenvL = lenv
  368. , ifenvR = renv
  369. , ifenv_next = n } tv1 tv2
  370. = IRV2 { ifenvL = addToUFM lenv tv1 n
  371. , ifenvR = addToUFM renv tv2 n
  372. , ifenv_next = n + 1
  373. }
  374. -- See Note [No kind check in ifaces]
  375. eqIfaceType :: IfRnEnv2 -> IfaceType -> IfaceType -> Bool
  376. eqIfaceType env (IfaceTyVar tv1) (IfaceTyVar tv2) =
  377. case (rnIfOccL env tv1, rnIfOccR env tv2) of
  378. (Just v1, Just v2) -> v1 == v2
  379. (Nothing, Nothing) -> tv1 == tv2
  380. _ -> False
  381. eqIfaceType _ (IfaceLitTy l1) (IfaceLitTy l2) = l1 == l2
  382. eqIfaceType env (IfaceAppTy t11 t12) (IfaceAppTy t21 t22)
  383. = eqIfaceType env t11 t21 && eqIfaceType env t12 t22
  384. eqIfaceType env (IfaceFunTy t11 t12) (IfaceFunTy t21 t22)
  385. = eqIfaceType env t11 t21 && eqIfaceType env t12 t22
  386. eqIfaceType env (IfaceDFunTy t11 t12) (IfaceDFunTy t21 t22)
  387. = eqIfaceType env t11 t21 && eqIfaceType env t12 t22
  388. eqIfaceType env (IfaceForAllTy bndr1 t1) (IfaceForAllTy bndr2 t2)
  389. = eqIfaceForAllBndr env bndr1 bndr2 (\env' -> eqIfaceType env' t1 t2)
  390. eqIfaceType env (IfaceTyConApp tc1 tys1) (IfaceTyConApp tc2 tys2)
  391. = tc1 == tc2 && eqIfaceTcArgs env tys1 tys2
  392. eqIfaceType env (IfaceTupleTy s1 tc1 tys1) (IfaceTupleTy s2 tc2 tys2)
  393. = s1 == s2 && tc1 == tc2 && eqIfaceTcArgs env tys1 tys2
  394. eqIfaceType env (IfaceCastTy t1 _) (IfaceCastTy t2 _)
  395. = eqIfaceType env t1 t2
  396. eqIfaceType _ (IfaceCoercionTy {}) (IfaceCoercionTy {})
  397. = True
  398. eqIfaceType _ _ _ = False
  399. eqIfaceTypes :: IfRnEnv2 -> [IfaceType] -> [IfaceType] -> Bool
  400. eqIfaceTypes env tys1 tys2 = and (zipWith (eqIfaceType env) tys1 tys2)
  401. eqIfaceForAllBndr :: IfRnEnv2 -> IfaceForAllBndr -> IfaceForAllBndr
  402. -> (IfRnEnv2 -> Bool) -- continuation
  403. -> Bool
  404. eqIfaceForAllBndr env (TvBndr (tv1, k1) vis1) (TvBndr (tv2, k2) vis2) k
  405. = eqIfaceType env k1 k2 && vis1 == vis2 &&
  406. k (extendIfRnEnv2 env tv1 tv2)
  407. eqIfaceTcArgs :: IfRnEnv2 -> IfaceTcArgs -> IfaceTcArgs -> Bool
  408. eqIfaceTcArgs _ ITC_Nil ITC_Nil = True
  409. eqIfaceTcArgs env (ITC_Vis ty1 tys1) (ITC_Vis ty2 tys2)
  410. = eqIfaceType env ty1 ty2 && eqIfaceTcArgs env tys1 tys2
  411. eqIfaceTcArgs env (ITC_Invis ty1 tys1) (ITC_Invis ty2 tys2)
  412. = eqIfaceType env ty1 ty2 && eqIfaceTcArgs env tys1 tys2
  413. eqIfaceTcArgs _ _ _ = False
  414. -- | Similar to 'eqTyVarBndrs', checks that tyvar lists
  415. -- are the same length and have matching kinds; if so, extend the
  416. -- 'IfRnEnv2'. Returns 'Nothing' if they don't match.
  417. eqIfaceTvBndrs :: IfRnEnv2 -> [IfaceTvBndr] -> [IfaceTvBndr] -> Maybe IfRnEnv2
  418. eqIfaceTvBndrs env [] [] = Just env
  419. eqIfaceTvBndrs env ((tv1, k1):tvs1) ((tv2, k2):tvs2)
  420. | eqIfaceType env k1 k2
  421. = eqIfaceTvBndrs (extendIfRnEnv2 env tv1 tv2) tvs1 tvs2
  422. eqIfaceTvBndrs _ _ _ = Nothing
  423. {-
  424. ************************************************************************
  425. * *
  426. Functions over IFaceTcArgs
  427. * *
  428. ************************************************************************
  429. -}
  430. stripInvisArgs :: DynFlags -> IfaceTcArgs -> IfaceTcArgs
  431. stripInvisArgs dflags tys
  432. | gopt Opt_PrintExplicitKinds dflags = tys
  433. | otherwise = suppress_invis tys
  434. where
  435. suppress_invis c
  436. = case c of
  437. ITC_Invis _ ts -> suppress_invis ts
  438. _ -> c
  439. toIfaceTcArgs :: TyCon -> [Type] -> IfaceTcArgs
  440. -- See Note [Suppressing invisible arguments]
  441. toIfaceTcArgs tc ty_args
  442. = go (mkEmptyTCvSubst in_scope) (tyConKind tc) ty_args
  443. where
  444. in_scope = mkInScopeSet (tyCoVarsOfTypes ty_args)
  445. go _ _ [] = ITC_Nil
  446. go env ty ts
  447. | Just ty' <- coreView ty
  448. = go env ty' ts
  449. go env (ForAllTy (TvBndr tv vis) res) (t:ts)
  450. | isVisibleArgFlag vis = ITC_Vis t' ts'
  451. | otherwise = ITC_Invis t' ts'
  452. where
  453. t' = toIfaceType t
  454. ts' = go (extendTvSubst env tv t) res ts
  455. go env (FunTy _ res) (t:ts) -- No type-class args in tycon apps
  456. = ITC_Vis (toIfaceType t) (go env res ts)
  457. go env (TyVarTy tv) ts
  458. | Just ki <- lookupTyVar env tv = go env ki ts
  459. go env kind (t:ts) = WARN( True, ppr tc $$ ppr (tyConKind tc) $$ ppr ty_args )
  460. ITC_Vis (toIfaceType t) (go env kind ts) -- Ill-kinded
  461. tcArgsIfaceTypes :: IfaceTcArgs -> [IfaceType]
  462. tcArgsIfaceTypes ITC_Nil = []
  463. tcArgsIfaceTypes (ITC_Invis t ts) = t : tcArgsIfaceTypes ts
  464. tcArgsIfaceTypes (ITC_Vis t ts) = t : tcArgsIfaceTypes ts
  465. {-
  466. Note [Suppressing invisible arguments]
  467. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  468. We use the IfaceTcArgs to specify which of the arguments to a type
  469. constructor should be displayed when pretty-printing, under
  470. the control of -fprint-explicit-kinds.
  471. See also Type.filterOutInvisibleTypes.
  472. For example, given
  473. T :: forall k. (k->*) -> k -> * -- Ordinary kind polymorphism
  474. 'Just :: forall k. k -> 'Maybe k -- Promoted
  475. we want
  476. T * Tree Int prints as T Tree Int
  477. 'Just * prints as Just *
  478. ************************************************************************
  479. * *
  480. Pretty-printing
  481. * *
  482. ************************************************************************
  483. -}
  484. pprIfaceInfixApp :: (TyPrec -> a -> SDoc) -> TyPrec -> SDoc -> a -> a -> SDoc
  485. pprIfaceInfixApp pp p pp_tc ty1 ty2
  486. = maybeParen p FunPrec $
  487. sep [pp FunPrec ty1, pprInfixVar True pp_tc <+> pp FunPrec ty2]
  488. pprIfacePrefixApp :: TyPrec -> SDoc -> [SDoc] -> SDoc
  489. pprIfacePrefixApp p pp_fun pp_tys
  490. | null pp_tys = pp_fun
  491. | otherwise = maybeParen p TyConPrec $
  492. hang pp_fun 2 (sep pp_tys)
  493. -- ----------------------------- Printing binders ------------------------------------
  494. instance Outputable IfaceBndr where
  495. ppr (IfaceIdBndr bndr) = pprIfaceIdBndr bndr
  496. ppr (IfaceTvBndr bndr) = char '@' <+> pprIfaceTvBndr bndr
  497. pprIfaceBndrs :: [IfaceBndr] -> SDoc
  498. pprIfaceBndrs bs = sep (map ppr bs)
  499. pprIfaceLamBndr :: IfaceLamBndr -> SDoc
  500. pprIfaceLamBndr (b, IfaceNoOneShot) = ppr b
  501. pprIfaceLamBndr (b, IfaceOneShot) = ppr b <> text "[OneShot]"
  502. pprIfaceIdBndr :: (IfLclName, IfaceType) -> SDoc
  503. pprIfaceIdBndr (name, ty) = parens (ppr name <+> dcolon <+> ppr ty)
  504. pprIfaceTvBndr :: IfaceTvBndr -> SDoc
  505. pprIfaceTvBndr (tv, ki)
  506. | isIfaceLiftedTypeKind ki = ppr tv
  507. | otherwise = parens (ppr tv <+> dcolon <+> ppr ki)
  508. pprIfaceTyConBinders :: [IfaceTyConBinder] -> SDoc
  509. pprIfaceTyConBinders = sep . map go
  510. where
  511. go tcb = pprIfaceTvBndr (ifTyConBinderTyVar tcb)
  512. instance Binary IfaceBndr where
  513. put_ bh (IfaceIdBndr aa) = do
  514. putByte bh 0
  515. put_ bh aa
  516. put_ bh (IfaceTvBndr ab) = do
  517. putByte bh 1
  518. put_ bh ab
  519. get bh = do
  520. h <- getByte bh
  521. case h of
  522. 0 -> do aa <- get bh
  523. return (IfaceIdBndr aa)
  524. _ -> do ab <- get bh
  525. return (IfaceTvBndr ab)
  526. instance Binary IfaceOneShot where
  527. put_ bh IfaceNoOneShot = do
  528. putByte bh 0
  529. put_ bh IfaceOneShot = do
  530. putByte bh 1
  531. get bh = do
  532. h <- getByte bh
  533. case h of
  534. 0 -> do return IfaceNoOneShot
  535. _ -> do return IfaceOneShot
  536. -- ----------------------------- Printing IfaceType ------------------------------------
  537. ---------------------------------
  538. instance Outputable IfaceType where
  539. ppr ty = pprIfaceType ty
  540. pprIfaceType, pprParendIfaceType ::IfaceType -> SDoc
  541. pprIfaceType = ppr_ty TopPrec
  542. pprParendIfaceType = ppr_ty TyConPrec
  543. ppr_ty :: TyPrec -> IfaceType -> SDoc
  544. ppr_ty _ (IfaceTyVar tyvar) = ppr tyvar
  545. ppr_ty ctxt_prec (IfaceTyConApp tc tys) = sdocWithDynFlags (pprTyTcApp ctxt_prec tc tys)
  546. ppr_ty _ (IfaceTupleTy s i tys) = pprTuple s i tys
  547. ppr_ty _ (IfaceLitTy n) = ppr_tylit n
  548. -- Function types
  549. ppr_ty ctxt_prec (IfaceFunTy ty1 ty2)
  550. = -- We don't want to lose synonyms, so we mustn't use splitFunTys here.
  551. maybeParen ctxt_prec FunPrec $
  552. sep [ppr_ty FunPrec ty1, sep (ppr_fun_tail ty2)]
  553. where
  554. ppr_fun_tail (IfaceFunTy ty1 ty2)
  555. = (arrow <+> ppr_ty FunPrec ty1) : ppr_fun_tail ty2
  556. ppr_fun_tail other_ty
  557. = [arrow <+> pprIfaceType other_ty]
  558. ppr_ty ctxt_prec (IfaceAppTy ty1 ty2)
  559. = maybeParen ctxt_prec TyConPrec $
  560. ppr_ty FunPrec ty1 <+> pprParendIfaceType ty2
  561. ppr_ty ctxt_prec (IfaceCastTy ty co)
  562. = maybeParen ctxt_prec FunPrec $
  563. sep [ppr_ty FunPrec ty, text "`cast`", ppr_co FunPrec co]
  564. ppr_ty ctxt_prec (IfaceCoercionTy co)
  565. = ppr_co ctxt_prec co
  566. ppr_ty ctxt_prec ty
  567. = maybeParen ctxt_prec FunPrec (ppr_iface_sigma_type True ty)
  568. instance Outputable IfaceTcArgs where
  569. ppr tca = pprIfaceTcArgs tca
  570. pprIfaceTcArgs, pprParendIfaceTcArgs :: IfaceTcArgs -> SDoc
  571. pprIfaceTcArgs = ppr_tc_args TopPrec
  572. pprParendIfaceTcArgs = ppr_tc_args TyConPrec
  573. ppr_tc_args :: TyPrec -> IfaceTcArgs -> SDoc
  574. ppr_tc_args ctx_prec args
  575. = let pprTys t ts = ppr_ty ctx_prec t <+> ppr_tc_args ctx_prec ts
  576. in case args of
  577. ITC_Nil -> empty
  578. ITC_Vis t ts -> pprTys t ts
  579. ITC_Invis t ts -> pprTys t ts
  580. -------------------
  581. ppr_iface_sigma_type :: Bool -> IfaceType -> SDoc
  582. ppr_iface_sigma_type show_foralls_unconditionally ty
  583. = ppr_iface_forall_part show_foralls_unconditionally tvs theta (ppr tau)
  584. where
  585. (tvs, theta, tau) = splitIfaceSigmaTy ty
  586. -------------------
  587. pprIfaceForAllPart :: [IfaceForAllBndr] -> [IfaceType] -> SDoc -> SDoc
  588. pprIfaceForAllPart tvs ctxt sdoc = ppr_iface_forall_part False tvs ctxt sdoc
  589. pprIfaceForAllCoPart :: [(IfLclName, IfaceCoercion)] -> SDoc -> SDoc
  590. pprIfaceForAllCoPart tvs sdoc = sep [ pprIfaceForAllCo tvs
  591. , sdoc ]
  592. ppr_iface_forall_part :: Outputable a
  593. => Bool -> [IfaceForAllBndr] -> [a] -> SDoc -> SDoc
  594. ppr_iface_forall_part show_foralls_unconditionally tvs ctxt sdoc
  595. = sep [ if show_foralls_unconditionally
  596. then pprIfaceForAll tvs
  597. else pprUserIfaceForAll tvs
  598. , pprIfaceContextArr ctxt
  599. , sdoc]
  600. -- | Render the "forall ... ." or "forall ... ->" bit of a type.
  601. pprIfaceForAll :: [IfaceForAllBndr] -> SDoc
  602. pprIfaceForAll [] = empty
  603. pprIfaceForAll bndrs@(TvBndr _ vis : _)
  604. = add_separator (text "forall" <+> doc) <+> pprIfaceForAll bndrs'
  605. where
  606. (bndrs', doc) = ppr_itv_bndrs bndrs vis
  607. add_separator stuff = case vis of
  608. Required -> stuff <+> arrow
  609. _inv -> stuff <> dot
  610. -- | Render the ... in @(forall ... .)@ or @(forall ... ->)@.
  611. -- Returns both the list of not-yet-rendered binders and the doc.
  612. -- No anonymous binders here!
  613. ppr_itv_bndrs :: [IfaceForAllBndr]
  614. -> ArgFlag -- ^ visibility of the first binder in the list
  615. -> ([IfaceForAllBndr], SDoc)
  616. ppr_itv_bndrs all_bndrs@(bndr@(TvBndr _ vis) : bndrs) vis1
  617. | vis `sameVis` vis1 = let (bndrs', doc) = ppr_itv_bndrs bndrs vis1 in
  618. (bndrs', pprIfaceForAllBndr bndr <+> doc)
  619. | otherwise = (all_bndrs, empty)
  620. ppr_itv_bndrs [] _ = ([], empty)
  621. pprIfaceForAllCo :: [(IfLclName, IfaceCoercion)] -> SDoc
  622. pprIfaceForAllCo [] = empty
  623. pprIfaceForAllCo tvs = text "forall" <+> pprIfaceForAllCoBndrs tvs <> dot
  624. pprIfaceForAllCoBndrs :: [(IfLclName, IfaceCoercion)] -> SDoc
  625. pprIfaceForAllCoBndrs bndrs = hsep $ map pprIfaceForAllCoBndr bndrs
  626. pprIfaceForAllBndr :: IfaceForAllBndr -> SDoc
  627. pprIfaceForAllBndr (TvBndr tv Inferred) = sdocWithDynFlags $ \dflags ->
  628. if gopt Opt_PrintExplicitForalls dflags
  629. then braces $ pprIfaceTvBndr tv
  630. else pprIfaceTvBndr tv
  631. pprIfaceForAllBndr (TvBndr tv _) = pprIfaceTvBndr tv
  632. pprIfaceForAllCoBndr :: (IfLclName, IfaceCoercion) -> SDoc
  633. pprIfaceForAllCoBndr (tv, kind_co)
  634. = parens (ppr tv <+> dcolon <+> pprIfaceCoercion kind_co)
  635. pprIfaceSigmaType :: IfaceType -> SDoc
  636. pprIfaceSigmaType ty = ppr_iface_sigma_type False ty
  637. pprUserIfaceForAll :: [IfaceForAllBndr] -> SDoc
  638. pprUserIfaceForAll tvs
  639. = sdocWithDynFlags $ \dflags ->
  640. ppWhen (any tv_has_kind_var tvs || gopt Opt_PrintExplicitForalls dflags) $
  641. pprIfaceForAll tvs
  642. where
  643. tv_has_kind_var bndr
  644. = not (isEmptyUniqSet (fst (ifTyVarsOfForAllBndr bndr)))
  645. -------------------
  646. -- See equivalent function in TyCoRep.hs
  647. pprIfaceTyList :: TyPrec -> IfaceType -> IfaceType -> SDoc
  648. -- Given a type-level list (t1 ': t2), see if we can print
  649. -- it in list notation [t1, ...].
  650. -- Precondition: Opt_PrintExplicitKinds is off
  651. pprIfaceTyList ctxt_prec ty1 ty2
  652. = case gather ty2 of
  653. (arg_tys, Nothing)
  654. -> char '\'' <> brackets (fsep (punctuate comma
  655. (map (ppr_ty TopPrec) (ty1:arg_tys))))
  656. (arg_tys, Just tl)
  657. -> maybeParen ctxt_prec FunPrec $ hang (ppr_ty FunPrec ty1)
  658. 2 (fsep [ colon <+> ppr_ty FunPrec ty | ty <- arg_tys ++ [tl]])
  659. where
  660. gather :: IfaceType -> ([IfaceType], Maybe IfaceType)
  661. -- (gather ty) = (tys, Nothing) means ty is a list [t1, .., tn]
  662. -- = (tys, Just tl) means ty is of form t1:t2:...tn:tl
  663. gather (IfaceTyConApp tc tys)
  664. | tcname == consDataConName
  665. , (ITC_Invis _ (ITC_Vis ty1 (ITC_Vis ty2 ITC_Nil))) <- tys
  666. , (args, tl) <- gather ty2
  667. = (ty1:args, tl)
  668. | tcname == nilDataConName
  669. = ([], Nothing)
  670. where tcname = ifaceTyConName tc
  671. gather ty = ([], Just ty)
  672. pprIfaceTypeApp :: IfaceTyCon -> IfaceTcArgs -> SDoc
  673. pprIfaceTypeApp tc args = sdocWithDynFlags (pprTyTcApp TopPrec tc args)
  674. pprTyTcApp :: TyPrec -> IfaceTyCon -> IfaceTcArgs -> DynFlags -> SDoc
  675. pprTyTcApp ctxt_prec tc tys dflags
  676. | ifaceTyConName tc `hasKey` ipClassKey
  677. , ITC_Vis (IfaceLitTy (IfaceStrTyLit n)) (ITC_Vis ty ITC_Nil) <- tys
  678. = char '?' <> ftext n <> text "::" <> ppr_ty TopPrec ty
  679. | ifaceTyConName tc == consDataConName
  680. , not (gopt Opt_PrintExplicitKinds dflags)
  681. , ITC_Invis _ (ITC_Vis ty1 (ITC_Vis ty2 ITC_Nil)) <- tys
  682. = pprIfaceTyList ctxt_prec ty1 ty2
  683. | ifaceTyConName tc == tYPETyConName
  684. , ITC_Vis (IfaceTyConApp ptr_rep ITC_Nil) ITC_Nil <- tys
  685. , ifaceTyConName ptr_rep `hasKey` ptrRepLiftedDataConKey
  686. = char '*'
  687. | ifaceTyConName tc == tYPETyConName
  688. , ITC_Vis (IfaceTyConApp ptr_rep ITC_Nil) ITC_Nil <- tys
  689. , ifaceTyConName ptr_rep `hasKey` ptrRepUnliftedDataConKey
  690. = char '#'
  691. | otherwise
  692. = ppr_iface_tc_app ppr_ty ctxt_prec tc tys_wo_kinds
  693. where
  694. tys_wo_kinds = tcArgsIfaceTypes $ stripInvisArgs dflags tys
  695. pprIfaceCoTcApp :: TyPrec -> IfaceTyCon -> [IfaceCoercion] -> SDoc
  696. pprIfaceCoTcApp ctxt_prec tc tys = ppr_iface_tc_app ppr_co ctxt_prec tc tys
  697. ppr_iface_tc_app :: (TyPrec -> a -> SDoc) -> TyPrec -> IfaceTyCon -> [a] -> SDoc
  698. ppr_iface_tc_app pp _ tc [ty]
  699. | n == listTyConName = pprPromotionQuote tc <> brackets (pp TopPrec ty)
  700. | n == parrTyConName = pprPromotionQuote tc <> paBrackets (pp TopPrec ty)
  701. where
  702. n = ifaceTyConName tc
  703. ppr_iface_tc_app pp ctxt_prec tc tys
  704. | not (isSymOcc (nameOccName tc_name))
  705. = pprIfacePrefixApp ctxt_prec (ppr tc) (map (pp TyConPrec) tys)
  706. | [ty1,ty2] <- tys -- Infix, two arguments;
  707. -- we know nothing of precedence though
  708. = pprIfaceInfixApp pp ctxt_prec (ppr tc) ty1 ty2
  709. | tc_name == starKindTyConName || tc_name == unliftedTypeKindTyConName
  710. || tc_name == unicodeStarKindTyConName
  711. = ppr tc -- Do not wrap *, # in parens
  712. | otherwise
  713. = pprIfacePrefixApp ctxt_prec (parens (ppr tc)) (map (pp TyConPrec) tys)
  714. where
  715. tc_name = ifaceTyConName tc
  716. pprTuple :: TupleSort -> IfaceTyConInfo -> IfaceTcArgs -> SDoc
  717. pprTuple sort info args
  718. = -- drop the RuntimeRep vars.
  719. -- See Note [Unboxed tuple RuntimeRep vars] in TyCon
  720. let tys = tcArgsIfaceTypes args
  721. args' = case sort of
  722. UnboxedTuple -> drop (length tys `div` 2) tys
  723. _ -> tys
  724. in
  725. pprPromotionQuoteI info <>
  726. tupleParens sort (pprWithCommas pprIfaceType args')
  727. ppr_tylit :: IfaceTyLit -> SDoc
  728. ppr_tylit (IfaceNumTyLit n) = integer n
  729. ppr_tylit (IfaceStrTyLit n) = text (show n)
  730. pprIfaceCoercion, pprParendIfaceCoercion :: IfaceCoercion -> SDoc
  731. pprIfaceCoercion = ppr_co TopPrec
  732. pprParendIfaceCoercion = ppr_co TyConPrec
  733. ppr_co :: TyPrec -> IfaceCoercion -> SDoc
  734. ppr_co _ (IfaceReflCo r ty) = angleBrackets (ppr ty) <> ppr_role r
  735. ppr_co ctxt_prec (IfaceFunCo r co1 co2)
  736. = maybeParen ctxt_prec FunPrec $
  737. sep (ppr_co FunPrec co1 : ppr_fun_tail co2)
  738. where
  739. ppr_fun_tail (IfaceFunCo r co1 co2)
  740. = (arrow <> ppr_role r <+> ppr_co FunPrec co1) : ppr_fun_tail co2
  741. ppr_fun_tail other_co
  742. = [arrow <> ppr_role r <+> pprIfaceCoercion other_co]
  743. ppr_co _ (IfaceTyConAppCo r tc cos)
  744. = parens (pprIfaceCoTcApp TopPrec tc cos) <> ppr_role r
  745. ppr_co ctxt_prec (IfaceAppCo co1 co2)
  746. = maybeParen ctxt_prec TyConPrec $
  747. ppr_co FunPrec co1 <+> pprParendIfaceCoercion co2
  748. ppr_co ctxt_prec co@(IfaceForAllCo {})
  749. = maybeParen ctxt_prec FunPrec (pprIfaceForAllCoPart tvs (pprIfaceCoercion inner_co))
  750. where
  751. (tvs, inner_co) = split_co co
  752. split_co (IfaceForAllCo (name, _) kind_co co')
  753. = let (tvs, co'') = split_co co' in ((name,kind_co):tvs,co'')
  754. split_co co' = ([], co')
  755. ppr_co _ (IfaceCoVarCo covar) = ppr covar
  756. ppr_co ctxt_prec (IfaceUnivCo IfaceUnsafeCoerceProv r ty1 ty2)
  757. = maybeParen ctxt_prec TyConPrec $
  758. text "UnsafeCo" <+> ppr r <+>
  759. pprParendIfaceType ty1 <+> pprParendIfaceType ty2
  760. ppr_co _ (IfaceUnivCo _ _ ty1 ty2)
  761. = angleBrackets ( ppr ty1 <> comma <+> ppr ty2 )
  762. ppr_co ctxt_prec (IfaceInstCo co ty)
  763. = maybeParen ctxt_prec TyConPrec $
  764. text "Inst" <+> pprParendIfaceCoercion co
  765. <+> pprParendIfaceCoercion ty
  766. ppr_co ctxt_prec (IfaceAxiomRuleCo tc cos)
  767. = maybeParen ctxt_prec TyConPrec $ ppr tc <+> parens (interpp'SP cos)
  768. ppr_co ctxt_prec (IfaceAxiomInstCo n i cos)
  769. = ppr_special_co ctxt_prec (ppr n <> brackets (ppr i)) cos
  770. ppr_co ctxt_prec (IfaceSymCo co)
  771. = ppr_special_co ctxt_prec (text "Sym") [co]
  772. ppr_co ctxt_prec (IfaceTransCo co1 co2)
  773. = ppr_special_co ctxt_prec (text "Trans") [co1,co2]
  774. ppr_co ctxt_prec (IfaceNthCo d co)
  775. = ppr_special_co ctxt_prec (text "Nth:" <> int d) [co]
  776. ppr_co ctxt_prec (IfaceLRCo lr co)
  777. = ppr_special_co ctxt_prec (ppr lr) [co]
  778. ppr_co ctxt_prec (IfaceSubCo co)
  779. = ppr_special_co ctxt_prec (text "Sub") [co]
  780. ppr_co ctxt_prec (IfaceCoherenceCo co1 co2)
  781. = ppr_special_co ctxt_prec (text "Coh") [co1,co2]
  782. ppr_co ctxt_prec (IfaceKindCo co)
  783. = ppr_special_co ctxt_prec (text "Kind") [co]
  784. ppr_special_co :: TyPrec -> SDoc -> [IfaceCoercion] -> SDoc
  785. ppr_special_co ctxt_prec doc cos
  786. = maybeParen ctxt_prec TyConPrec
  787. (sep [doc, nest 4 (sep (map pprParendIfaceCoercion cos))])
  788. ppr_role :: Role -> SDoc
  789. ppr_role r = underscore <> pp_role
  790. where pp_role = case r of
  791. Nominal -> char 'N'
  792. Representational -> char 'R'
  793. Phantom -> char 'P'
  794. -------------------
  795. instance Outputable IfaceTyCon where
  796. ppr tc = pprPromotionQuote tc <> ppr (ifaceTyConName tc)
  797. pprPromotionQuote :: IfaceTyCon -> SDoc
  798. pprPromotionQuote tc = pprPromotionQuoteI (ifaceTyConInfo tc)
  799. pprPromotionQuoteI :: IfaceTyConInfo -> SDoc
  800. pprPromotionQuoteI NoIfaceTyConInfo = empty
  801. pprPromotionQuoteI IfacePromotedDataCon = char '\''
  802. instance Outputable IfaceCoercion where
  803. ppr = pprIfaceCoercion
  804. instance Binary IfaceTyCon where
  805. put_ bh (IfaceTyCon n i) = put_ bh n >> put_ bh i
  806. get bh = do n <- get bh
  807. i <- get bh
  808. return (IfaceTyCon n i)
  809. instance Binary IfaceTyConInfo where
  810. put_ bh NoIfaceTyConInfo = putByte bh 0
  811. put_ bh IfacePromotedDataCon = putByte bh 1
  812. get bh =
  813. do i <- getByte bh
  814. case i of
  815. 0 -> return NoIfaceTyConInfo
  816. _ -> return IfacePromotedDataCon
  817. instance Outputable IfaceTyLit where
  818. ppr = ppr_tylit
  819. instance Binary IfaceTyLit where
  820. put_ bh (IfaceNumTyLit n) = putByte bh 1 >> put_ bh n
  821. put_ bh (IfaceStrTyLit n) = putByte bh 2 >> put_ bh n
  822. get bh =
  823. do tag <- getByte bh
  824. case tag of
  825. 1 -> do { n <- get bh
  826. ; return (IfaceNumTyLit n) }
  827. 2 -> do { n <- get bh
  828. ; return (IfaceStrTyLit n) }
  829. _ -> panic ("get IfaceTyLit " ++ show tag)
  830. instance Binary IfaceTcArgs where
  831. put_ bh tk =
  832. case tk of
  833. ITC_Vis t ts -> putByte bh 0 >> put_ bh t >> put_ bh ts
  834. ITC_Invis t ts -> putByte bh 1 >> put_ bh t >> put_ bh ts
  835. ITC_Nil -> putByte bh 2
  836. get bh =
  837. do c <- getByte bh
  838. case c of
  839. 0 -> do
  840. t <- get bh
  841. ts <- get bh
  842. return $! ITC_Vis t ts
  843. 1 -> do
  844. t <- get bh
  845. ts <- get bh
  846. return $! ITC_Invis t ts
  847. 2 -> return ITC_Nil
  848. _ -> panic ("get IfaceTcArgs " ++ show c)
  849. -------------------
  850. pprIfaceContextArr :: Outputable a => [a] -> SDoc
  851. -- Prints "(C a, D b) =>", including the arrow
  852. pprIfaceContextArr [] = empty
  853. pprIfaceContextArr preds = pprIfaceContext preds <+> darrow
  854. pprIfaceContext :: Outputable a => [a] -> SDoc
  855. pprIfaceContext [] = parens empty
  856. pprIfaceContext [pred] = ppr pred -- No parens
  857. pprIfaceContext preds = parens (fsep (punctuate comma (map ppr preds)))
  858. instance Binary IfaceType where
  859. put_ bh (IfaceForAllTy aa ab) = do
  860. putByte bh 0
  861. put_ bh aa
  862. put_ bh ab
  863. put_ bh (IfaceTyVar ad) = do
  864. putByte bh 1
  865. put_ bh ad
  866. put_ bh (IfaceAppTy ae af) = do
  867. putByte bh 2
  868. put_ bh ae
  869. put_ bh af
  870. put_ bh (IfaceFunTy ag ah) = do
  871. putByte bh 3
  872. put_ bh ag
  873. put_ bh ah
  874. put_ bh (IfaceDFunTy ag ah) = do
  875. putByte bh 4
  876. put_ bh ag
  877. put_ bh ah
  878. put_ bh (IfaceTyConApp tc tys)
  879. = do { putByte bh 5; put_ bh tc; put_ bh tys }
  880. put_ bh (IfaceCastTy a b)
  881. = do { putByte bh 6; put_ bh a; put_ bh b }
  882. put_ bh (IfaceCoercionTy a)
  883. = do { putByte bh 7; put_ bh a }
  884. put_ bh (IfaceTupleTy s i tys)
  885. = do { putByte bh 8; put_ bh s; put_ bh i; put_ bh tys }
  886. put_ bh (IfaceLitTy n)
  887. = do { putByte bh 9; put_ bh n }
  888. get bh = do
  889. h <- getByte bh
  890. case h of
  891. 0 -> do aa <- get bh
  892. ab <- get bh
  893. return (IfaceForAllTy aa ab)
  894. 1 -> do ad <- get bh
  895. return (IfaceTyVar ad)
  896. 2 -> do ae <- get bh
  897. af <- get bh
  898. return (IfaceAppTy ae af)
  899. 3 -> do ag <- get bh
  900. ah <- get bh
  901. return (IfaceFunTy ag ah)
  902. 4 -> do ag <- get bh
  903. ah <- get bh
  904. return (IfaceDFunTy ag ah)
  905. 5 -> do { tc <- get bh; tys <- get bh
  906. ; return (IfaceTyConApp tc tys) }
  907. 6 -> do { a <- get bh; b <- get bh
  908. ; return (IfaceCastTy a b) }
  909. 7 -> do { a <- get bh
  910. ; return (IfaceCoercionTy a) }
  911. 8 -> do { s <- get bh; i <- get bh; tys <- get bh
  912. ; return (IfaceTupleTy s i tys) }
  913. _ -> do n <- get bh
  914. return (IfaceLitTy n)
  915. instance Binary IfaceCoercion where
  916. put_ bh (IfaceReflCo a b) = do
  917. putByte bh 1
  918. put_ bh a
  919. put_ bh b
  920. put_ bh (IfaceFunCo a b c) = do
  921. putByte bh 2
  922. put_ bh a
  923. put_ bh b
  924. put_ bh c
  925. put_ bh (IfaceTyConAppCo a b c) = do
  926. putByte bh 3
  927. put_ bh a
  928. put_ bh b
  929. put_ bh c
  930. put_ bh (IfaceAppCo a b) = do
  931. putByte bh 4
  932. put_ bh a
  933. put_ bh b
  934. put_ bh (IfaceForAllCo a b c) = do
  935. putByte bh 5
  936. put_ bh a
  937. put_ bh b
  938. put_ bh c
  939. put_ bh (IfaceCoVarCo a) = do
  940. putByte bh 6
  941. put_ bh a
  942. put_ bh (IfaceAxiomInstCo a b c) = do
  943. putByte bh 7
  944. put_ bh a
  945. put_ bh b
  946. put_ bh c
  947. put_ bh (IfaceUnivCo a b c d) = do
  948. putByte bh 8
  949. put_ bh a
  950. put_ bh b
  951. put_ bh c
  952. put_ bh d
  953. put_ bh (IfaceSymCo a) = do
  954. putByte bh 9
  955. put_ bh a
  956. put_ bh (IfaceTransCo a b) = do
  957. putByte bh 10
  958. put_ bh a
  959. put_ bh b
  960. put_ bh (IfaceNthCo a b) = do
  961. putByte bh 11
  962. put_ bh a
  963. put_ bh b
  964. put_ bh (IfaceLRCo a b) = do
  965. putByte bh 12
  966. put_ bh a
  967. put_ bh b
  968. put_ bh (IfaceInstCo a b) = do
  969. putByte bh 13
  970. put_ bh a
  971. put_ bh b
  972. put_ bh (IfaceCoherenceCo a b) = do
  973. putByte bh 14
  974. put_ bh a
  975. put_ bh b
  976. put_ bh (IfaceKindCo a) = do
  977. putByte bh 15
  978. put_ bh a
  979. put_ bh (IfaceSubCo a) = do
  980. putByte bh 16
  981. put_ bh a
  982. put_ bh (IfaceAxiomRuleCo a b) = do
  983. putByte bh 17
  984. put_ bh a
  985. put_ bh b
  986. get bh = do
  987. tag <- getByte bh
  988. case tag of
  989. 1 -> do a <- get bh
  990. b <- get bh
  991. return $ IfaceReflCo a b
  992. 2 -> do a <- get bh
  993. b <- get bh
  994. c <- get bh
  995. return $ IfaceFunCo a b c
  996. 3 -> do a <- get bh
  997. b <- get bh
  998. c <- get bh
  999. return $ IfaceTyConAppCo a b c
  1000. 4 -> do a <- get bh
  1001. b <- get bh
  1002. return $ IfaceAppCo a b
  1003. 5 -> do a <- get bh
  1004. b <- get bh
  1005. c <- get bh
  1006. return $ IfaceForAllCo a b c
  1007. 6 -> do a <- get bh
  1008. return $ IfaceCoVarCo a
  1009. 7 -> do a <- get bh
  1010. b <- get bh
  1011. c <- get bh
  1012. return $ IfaceAxiomInstCo a b c
  1013. 8 -> do a <- get bh
  1014. b <- get bh
  1015. c <- get bh
  1016. d <- get bh
  1017. return $ IfaceUnivCo a b c d
  1018. 9 -> do a <- get bh
  1019. return $ IfaceSymCo a
  1020. 10-> do a <- get bh
  1021. b <- get bh
  1022. return $ IfaceTransCo a b
  1023. 11-> do a <- get bh
  1024. b <- get bh
  1025. return $ IfaceNthCo a b
  1026. 12-> do a <- get bh
  1027. b <- get bh
  1028. return $ IfaceLRCo a b
  1029. 13-> do a <- get bh
  1030. b <- get bh
  1031. return $ IfaceInstCo a b
  1032. 14-> do a <- get bh
  1033. b <- get bh
  1034. return $ IfaceCoherenceCo a b
  1035. 15-> do a <- get bh
  1036. return $ IfaceKindCo a
  1037. 16-> do a <- get bh
  1038. return $ IfaceSubCo a
  1039. 17-> do a <- get bh
  1040. b <- get bh
  1041. return $ IfaceAxiomRuleCo a b
  1042. _ -> panic ("get IfaceCoercion " ++ show tag)
  1043. instance Binary IfaceUnivCoProv where
  1044. put_ bh IfaceUnsafeCoerceProv = putByte bh 1
  1045. put_ bh (IfacePhantomProv a) = do
  1046. putByte bh 2
  1047. put_ bh a
  1048. put_ bh (IfaceProofIrrelProv a) = do
  1049. putByte bh 3
  1050. put_ bh a
  1051. put_ bh (IfacePluginProv a) = do
  1052. putByte bh 4
  1053. put_ bh a
  1054. get bh = do
  1055. tag <- getByte bh
  1056. case tag of
  1057. 1 -> return $ IfaceUnsafeCoerceProv
  1058. 2 -> do a <- get bh
  1059. return $ IfacePhantomProv a
  1060. 3 -> do a <- get bh
  1061. return $ IfaceProofIrrelProv a
  1062. 4 -> do a <- get bh
  1063. return $ IfacePluginProv a
  1064. _ -> panic ("get IfaceUnivCoProv " ++ show tag)
  1065. instance Binary (DefMethSpec IfaceType) where
  1066. put_ bh VanillaDM = putByte bh 0
  1067. put_ bh (GenericDM t) = putByte bh 1 >> put_ bh t
  1068. get bh = do
  1069. h <- getByte bh
  1070. case h of
  1071. 0 -> return VanillaDM
  1072. _ -> do { t <- get bh; return (GenericDM t) }
  1073. {-
  1074. ************************************************************************
  1075. * *
  1076. Conversion from Type to IfaceType
  1077. * *
  1078. ************************************************************************
  1079. -}
  1080. ----------------
  1081. toIfaceTvBndr :: TyVar -> IfaceTvBndr
  1082. toIfaceTvBndr tyvar = ( occNameFS (getOccName tyvar)
  1083. , toIfaceKind (tyVarKind tyvar)
  1084. )
  1085. toIfaceIdBndr :: Id -> (IfLclName, IfaceType)
  1086. toIfaceIdBndr id = (occNameFS (getOccName id), toIfaceType (idType id))
  1087. toIfaceTvBndrs :: [TyVar] -> [IfaceTvBndr]
  1088. toIfaceTvBndrs = map toIfaceTvBndr
  1089. toIfaceBndr :: Var -> IfaceBndr
  1090. toIfaceBndr var
  1091. | isId var = IfaceIdBndr (toIfaceIdBndr var)
  1092. | otherwise = IfaceTvBndr (toIfaceTvBndr var)
  1093. toIfaceKind :: Type -> IfaceType
  1094. toIfaceKind = toIfaceType
  1095. ---------------------
  1096. toIfaceType :: Type -> IfaceType
  1097. -- Synonyms are retained in the interface type
  1098. toIfaceType (TyVarTy tv) = IfaceTyVar (toIfaceTyVar tv)
  1099. toIfaceType (AppTy t1 t2) = IfaceAppTy (toIfaceType t1) (toIfaceType t2)
  1100. toIfaceType (LitTy n) = IfaceLitTy (toIfaceTyLit n)
  1101. toIfaceType (ForAllTy b t) = IfaceForAllTy (toIfaceForAllBndr b) (toIfaceType t)
  1102. toIfaceType (FunTy t1 t2)
  1103. | isPredTy t1 = IfaceDFunTy (toIfaceType t1) (toIfaceType t2)
  1104. | otherwise = IfaceFunTy (toIfaceType t1) (toIfaceType t2)
  1105. toIfaceType (CastTy ty co) = IfaceCastTy (toIfaceType ty) (toIfaceCoercion co)
  1106. toIfaceType (CoercionTy co) = IfaceCoercionTy (toIfaceCoercion co)
  1107. toIfaceType (TyConApp tc tys) -- Look for the two sorts of saturated tuple
  1108. | Just sort <- tyConTuple_maybe tc
  1109. , n_tys == arity
  1110. = IfaceTupleTy sort NoIfaceTyConInfo (toIfaceTcArgs tc tys)
  1111. | Just dc <- isPromotedDataCon_maybe tc
  1112. , isTupleDataCon dc
  1113. , n_tys == 2*arity
  1114. = IfaceTupleTy BoxedTuple IfacePromotedDataCon (toIfaceTcArgs tc (drop arity tys))
  1115. | otherwise
  1116. = IfaceTyConApp (toIfaceTyCon tc) (toIfaceTcArgs tc tys)
  1117. where
  1118. arity = tyConArity tc
  1119. n_tys = length tys
  1120. toIfaceTyVar :: TyVar -> FastString
  1121. toIfaceTyVar = occNameFS . getOccName
  1122. toIfaceCoVar :: CoVar -> FastString
  1123. toIfaceCoVar = occNameFS . getOccName
  1124. toIfaceForAllBndr :: TyVarBinder -> IfaceForAllBndr
  1125. toIfaceForAllBndr (TvBndr v vis) = TvBndr (toIfaceTvBndr v) vis
  1126. ----------------
  1127. toIfaceTyCon :: TyCon -> IfaceTyCon
  1128. toIfaceTyCon tc
  1129. = IfaceTyCon tc_name info
  1130. where
  1131. tc_name = tyConName tc
  1132. info | isPromotedDataCon tc = IfacePromotedDataCon
  1133. | otherwise = NoIfaceTyConInfo
  1134. toIfaceTyCon_name :: Name -> IfaceTyCon
  1135. toIfaceTyCon_name n = IfaceTyCon n NoIfaceTyConInfo
  1136. -- Used for the "rough-match" tycon stuff,
  1137. -- where pretty-printing is not an issue
  1138. toIfaceTyLit :: TyLit -> IfaceTyLit
  1139. toIfaceTyLit (NumTyLit x) = IfaceNumTyLit x
  1140. toIfaceTyLit (StrTyLit x) = IfaceStrTyLit x
  1141. ----------------
  1142. toIfaceTypes :: [Type] -> [IfaceType]
  1143. toIfaceTypes ts = map toIfaceType ts
  1144. ----------------
  1145. toIfaceContext :: ThetaType -> IfaceContext
  1146. toIfaceContext = toIfaceTypes
  1147. ----------------
  1148. toIfaceCoercion :: Coercion -> IfaceCoercion
  1149. toIfaceCoercion (Refl r ty) = IfaceReflCo r (toIfaceType ty)
  1150. toIfaceCoercion (TyConAppCo r tc cos)
  1151. | tc `hasKey` funTyConKey
  1152. , [arg,res] <- cos = IfaceFunCo r (toIfaceCoercion arg) (toIfaceCoercion res)
  1153. | otherwise = IfaceTyConAppCo r (toIfaceTyCon tc)
  1154. (map toIfaceCoercion cos)
  1155. toIfaceCoercion (AppCo co1 co2) = IfaceAppCo (toIfaceCoercion co1)
  1156. (toIfaceCoercion co2)
  1157. toIfaceCoercion (ForAllCo tv k co) = IfaceForAllCo (toIfaceTvBndr tv)
  1158. (toIfaceCoercion k)
  1159. (toIfaceCoercion co)
  1160. toIfaceCoercion (CoVarCo cv) = IfaceCoVarCo (toIfaceCoVar cv)
  1161. toIfaceCoercion (AxiomInstCo con ind cos)
  1162. = IfaceAxiomInstCo (coAxiomName con) ind
  1163. (map toIfaceCoercion cos)
  1164. toIfaceCoercion (UnivCo p r t1 t2) = IfaceUnivCo (toIfaceUnivCoProv p) r
  1165. (toIfaceType t1)
  1166. (toIfaceType t2)
  1167. toIfaceCoercion (SymCo co) = IfaceSymCo (toIfaceCoercion co)
  1168. toIfaceCoercion (TransCo co1 co2) = IfaceTransCo (toIface

Large files files are truncated, but you can click here to view the full file