2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
4 \section[TCE]{Type constructor environment}
7 #include "HsVersions.h"
14 plusTCE, checkTypeCycles,
15 -- NOT REALLY USED: printTypeInfoForPop,
17 -- and to make the interface self-sufficient...
18 MaybeErr, Name, TyCon,
19 Error(..), SrcLoc, Pretty(..), PrettyRep
21 IF_ATTACK_PRAGMAS(COMMA emptyUFM COMMA plusUFM)
22 IF_ATTACK_PRAGMAS(COMMA eltsUFM COMMA singletonDirectlyUFM)
23 IF_ATTACK_PRAGMAS(COMMA u2i)
26 import AbsUniType ( getMentionedTyCons, isDataTyCon, getTyConDataCons,
27 TyCon, Arity(..), Class, UniType
28 IF_ATTACK_PRAGMAS(COMMA cmpTyCon COMMA cmpClass)
29 IF_ATTACK_PRAGMAS(COMMA cmpUniType)
31 import Digraph ( topologicalSort )
32 import Errors -- notably typeCycleErr
33 import Id ( getDataConArity, Id, DataCon(..) )
34 import Maybes ( Maybe(..), MaybeErr(..) )
38 import UniqFM -- basic environment handling
39 import Unique ( Unique )
44 --data TCE = MkTCE (UniqFM TyCon)
45 type TCE = UniqFM TyCon
47 -- also killed instance TCE, exported non-abstractly
50 nullTCE = MkTCE emptyUFM
52 unitTCE :: Unique -> TyCon -> TCE
53 unitTCE uniq tycon = MkTCE (singletonDirectlyUFM uniq tycon)
55 rngTCE :: TCE -> [TyCon]
56 rngTCE (MkTCE tce) = eltsUFM tce
58 lookupTCE :: TCE -> Name -> TyCon
59 lookupTCE (MkTCE tce) name
61 WiredInTyCon tycon -> tycon
62 PreludeTyCon key _ _ _ -> case (lookupDirectlyUFM tce key) of
65 OtherTyCon uniq _ _ _ _ -> case (lookupDirectlyUFM tce uniq) of
69 err_msg = error ("ERROR: in looking up a type constructor! "++(ppShow 80 (ppr PprDebug name))++"\n(This can happen if you use `-fno-implicit-prelude'\nor you hide or change the system's Prelude.hi in some way.\nA -fhaskell-1.3 flag, or lack thereof, can trigger this error.)\n")
71 plusTCE :: TCE -> TCE -> TCE
72 plusTCE (MkTCE tce1) (MkTCE tce2) = MkTCE (plusUFM tce1 tce2)
76 checkTypeCycles :: TCE -> MaybeErr () Error
78 = case (topologicalSort (==) edges vertices) of
79 Succeeded ordering -> Succeeded ()
81 -> Failed (typeCycleErr (map (\ c -> map fmt_tycon c) cycles))
83 fmt_tycon c = (ppr PprForUser c, getSrcLoc c)
85 vertices = [ vertex1 | (vertex1, vertex2) <- edges]
86 edges = concat (map get_edges (rngTCE tce))
88 get_edges tycon = [(tycon, dep) | dep <- getMentionedTyCons tycon]
89 -- Make an arc for every dependency
94 printTypeInfoForPop :: TCE -> Pretty
96 printTypeInfoForPop (MkTCE tce)
97 = ppAboves [ pp_type tc | tc <- eltsUFM tce, isDataTyCon tc ]
102 ppr PprForUser tycon, ppSP,
103 ppInterleave ppSP (map pp_data_con (getTyConDataCons tycon)),
108 = ppCat [ppr PprForUser data_con, ppInt (getDataConArity data_con)]