[project @ 1998-01-08 18:03:08 by simonm]
[ghc-hetmet.git] / ghc / compiler / types / PprType.lhs
index 3d03685..3762e63 100644 (file)
@@ -1,84 +1,69 @@
 %
 % (c) The AQUA Project, Glasgow University, 1996
 %
-\section[PprType]{Printing Types, TyVars, Classes, ClassOps, TyCons}
+\section[PprType]{Printing Types, TyVars, Classes, TyCons}
 
 \begin{code}
-#include "HsVersions.h"
-
 module PprType(
-       GenTyVar, pprGenTyVar, pprTyVarBndr,
+       GenTyVar, pprGenTyVar, pprTyVarBndr, pprTyVarBndrs,
        TyCon, pprTyCon, showTyCon,
        GenType,
        pprGenType, pprParendGenType,
        pprType, pprParendType,
        pprMaybeTy,
-       getTypeString,
-       specMaybeTysSuffix,
        getTyDescription,
-       GenClass, 
-       GenClassOp, pprGenClassOp,
-       
-       addTyVar{-ToDo:don't export-}, nmbrTyVar,
-       addUVar,  nmbrUsage,
-       nmbrType, nmbrTyCon, nmbrClass
+       pprConstraint, pprTheta,
+
+       nmbrType, nmbrGlobalType
  ) where
 
-IMP_Ubiq()
-IMPORT_DELOOPER(IdLoop)
---IMPORT_DELOOPER(TyLoop)      -- for paranoia checking
+#include "HsVersions.h"
 
 -- friends:
 -- (PprType can see all the representations it's trying to print)
-import Type            ( GenType(..), maybeAppTyCon,
-                         splitForAllTy, splitSigmaTy, splitRhoTy, splitAppTys )
-import TyVar           ( GenTyVar(..) )
-import TyCon           ( TyCon(..), NewOrData )
-import Class           ( SYN_IE(Class), GenClass(..),
-                         SYN_IE(ClassOp), GenClassOp(..) )
-import Kind            ( Kind(..), isBoxedTypeKind, pprParendKind )
-import Usage           ( pprUVar, GenUsage(..), SYN_IE(Usage), SYN_IE(UVar) )
+import Type            ( GenType(..), Type, ThetaType, splitFunTys, splitDictTy_maybe,
+                         splitForAllTys, splitSigmaTy, splitRhoTy, splitAppTys )
+import TyVar           ( GenTyVar(..), TyVar, cloneTyVar )
+import TyCon           ( TyCon, NewOrData, isFunTyCon, isTupleTyCon, tyConArity )
+import Class           ( Class )
+import Kind            ( GenKind(..), isBoxedTypeKind, pprParendKind )
 
 -- others:
-import CStrings                ( identToC )
-import CmdLineOpts     ( opt_OmitInterfacePragmas )
+import CmdLineOpts     ( opt_PprUserLength )
 import Maybes          ( maybeToBool )
-import Name            (  nameString, Name{-instance Outputable-}, 
-                          OccName, pprOccName, getOccString, pprNonSymOcc
-                       )
-import Outputable      ( ifPprShowAll, interpp'SP )
+import Name            ( nameString, pprOccName, getOccString, OccName, NamedThing(..) )
+import Outputable
 import PprEnv
-import PprStyle                ( PprStyle(..), codeStyle, showUserishTypes )
-import Pretty
-import UniqFM          ( addToUFM_Directly, lookupUFM_Directly{-, ufmToList ToDo:rm-} )
-import Unique  --TEMP: ( pprUnique10, pprUnique, incrUnique, listTyConKey )
+import BasicTypes      ( Unused )
+import UniqFM          ( UniqFM, addToUFM, emptyUFM, lookupUFM  )
+import Unique          ( Unique, Uniquable(..), pprUnique, 
+                         incrUnique, listTyConKey, initTyVarUnique 
+                       )
 import Util
 \end{code}
 
 \begin{code}
-instance (Eq tyvar, Outputable tyvar,
-         Eq uvar,  Outputable uvar  ) => Outputable (GenType tyvar uvar) where
-    ppr sty ty = pprGenType sty ty
+instance Outputable (GenType flexi) where
+    ppr ty = pprGenType ty
 
 instance Outputable TyCon where
-    ppr sty tycon = pprTyCon sty tycon
+    ppr tycon = pprTyCon tycon
 
-instance Outputable (GenClass tyvar uvar) where
+instance Outputable Class where
     -- we use pprIfaceClass for printing in interfaces
-    ppr sty (Class u n _ _ _ _ _ _ _ _) = ppr sty n
-
-instance Outputable ty => Outputable (GenClassOp ty) where
-    ppr sty clsop = pprGenClassOp sty clsop
+    ppr clas = ppr (getName clas)
 
 instance Outputable (GenTyVar flexi) where
-    ppr sty tv = pprGenTyVar sty tv
+    ppr tv = pprGenTyVar tv
 
 -- and two SPECIALIZEd ones:
-instance Outputable {-Type, i.e.:-}(GenType TyVar UVar) where
-    ppr sty ty = pprGenType sty ty
+{- 
+instance Outputable {-Type, i.e.:-}(GenType Unused) where
+    ppr ty = pprGenType ty
 
-instance Outputable {-TyVar, i.e.:-}(GenTyVar Usage) where
-    ppr sty ty = pprGenTyVar sty ty
+instance Outputable {-TyVar, i.e.:-}(GenTyVar Unused) where
+    ppr ty = pprGenTyVar ty
+-}
 \end{code}
 
 %************************************************************************
@@ -105,7 +90,7 @@ tYCON_PREC  = (2 :: Int)
 
 maybeParen ctxt_prec inner_prec pretty
   | ctxt_prec < inner_prec = pretty
-  | otherwise             = ppParens pretty
+  | otherwise             = parens pretty
 \end{code}
 
 @pprGenType@ is the std @Type@ printer; the overloaded @ppr@ function is
@@ -114,151 +99,133 @@ parens around the type, except for the atomic cases.  @pprParendGenType@
 works just by setting the initial context precedence very high.
 
 \begin{code}
-pprGenType, pprParendGenType :: (Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
-                      => PprStyle -> GenType tyvar uvar -> Pretty
+pprGenType, pprParendGenType :: GenType flexi -> SDoc
+
+pprGenType       ty = ppr_ty init_ppr_env tOP_PREC   ty
+pprParendGenType ty = ppr_ty init_ppr_env tYCON_PREC ty
 
-pprGenType       sty ty = ppr_ty (init_ppr_env sty) tOP_PREC   ty
-pprParendGenType sty ty = ppr_ty (init_ppr_env sty) tYCON_PREC ty
+pprType, pprParendType :: Type -> SDoc
+pprType         ty = ppr_ty init_ppr_env_type tOP_PREC   ty
+pprParendType   ty = ppr_ty init_ppr_env_type tYCON_PREC ty
 
-pprType, pprParendType :: PprStyle -> Type -> Pretty
-pprType         sty ty = ppr_ty (init_ppr_env_type sty) tOP_PREC   ty
-pprParendType   sty ty = ppr_ty (init_ppr_env_type sty) tYCON_PREC ty
+pprConstraint :: Class -> [GenType flexi] -> SDoc
+pprConstraint clas tys = hsep [ppr clas, hsep (map (pprParendGenType) tys)]
 
-pprMaybeTy :: (Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
-           => PprStyle -> Maybe (GenType tyvar uvar) -> Pretty
-pprMaybeTy sty Nothing   = ppChar '*'
-pprMaybeTy sty (Just ty) = pprParendGenType sty ty
+pprTheta :: ThetaType -> SDoc
+pprTheta theta = parens (hsep (punctuate comma (map ppr_dict theta)))
+              where
+                ppr_dict (c,tys) = pprConstraint c tys
+
+pprMaybeTy :: Maybe (GenType flexi) -> SDoc
+pprMaybeTy Nothing   = char '*'
+pprMaybeTy (Just ty) = pprParendGenType ty
 \end{code}
 
 \begin{code}
-ppr_ty :: PprEnv tyvar uvar bndr occ -> Int
-       -> GenType tyvar uvar
-       -> Pretty
+ppr_ty :: PprEnv flexi bndr occ -> Int
+       -> GenType flexi
+       -> SDoc
 
 ppr_ty env ctxt_prec (TyVarTy tyvar)
   = pTyVarO env tyvar
 
-ppr_ty env ctxt_prec (TyConTy tycon usage)
+       -- TUPLE CASE
+ppr_ty env ctxt_prec (TyConApp tycon tys)
+  |  isTupleTyCon tycon
+  && length tys == tyConArity tycon            -- no magic if partially applied
+  = parens tys_w_commas
+  where
+    tys_w_commas = hsep (punctuate comma (map (ppr_ty env tOP_PREC) tys))
+
+       -- LIST CASE
+ppr_ty env ctxt_prec (TyConApp tycon [ty])
+  |  uniqueOf tycon == listTyConKey
+  = brackets (ppr_ty env tOP_PREC ty)
+
+       -- DICTIONARY CASE, prints {C a}
+       -- This means that instance decls come out looking right in interfaces
+       -- and that in turn means they get "gated" correctly when being slurped in
+ppr_ty env ctxt_prec ty@(TyConApp tycon tys)
+  | maybeToBool maybe_dict
+  = braces (ppr_dict env tYCON_PREC ctys)
+  where
+    Just ctys = maybe_dict
+    maybe_dict = splitDictTy_maybe ty
+  
+       -- NO-ARGUMENT CASE (=> no parens)
+ppr_ty env ctxt_prec (TyConApp tycon [])
   = ppr_tycon env tycon
 
-ppr_ty env ctxt_prec ty@(ForAllTy _ _)
-  | show_forall = maybeParen ctxt_prec fUN_PREC $
-                 ppSep [ ppPStr SLIT("_forall_"), pp_tyvars, 
-                         pp_theta, ppPStr SLIT("=>"), pp_body
-                       ]
-  | null theta = ppr_ty env ctxt_prec body_ty
-  | otherwise  = maybeParen ctxt_prec fUN_PREC $
-                ppSep [pp_theta, ppPStr SLIT("=>"), pp_body]
+       -- GENERAL CASE
+ppr_ty env ctxt_prec (TyConApp tycon tys)
+  = maybeParen ctxt_prec tYCON_PREC (hsep [ppr_tycon env tycon, tys_w_spaces])
   where
-    (tyvars, rho_ty) = splitForAllTy ty
-    (theta, body_ty) | show_context = splitRhoTy rho_ty
-                    | otherwise    = ([], rho_ty)
+    tys_w_spaces = hsep (map (ppr_ty env tYCON_PREC) tys)
 
-    pp_tyvars = ppBracket (ppIntersperse ppSP (map (pTyVarB env) tyvars))
-    pp_theta  | null theta = ppNil
-             | otherwise  = ppCurlies (ppInterleave ppComma (map (ppr_dict env tOP_PREC) theta))
-    pp_body   = ppr_ty env tOP_PREC body_ty
 
-    sty = pStyle env
-    show_forall = case sty of
-                       PprForUser -> False
-                       other      -> True
+ppr_ty env ctxt_prec ty@(ForAllTy _ _)
+  = getPprStyle $ \ sty -> 
+    let
+       (tyvars, rho_ty) = splitForAllTys ty
+       (theta, body_ty) | show_context = splitRhoTy rho_ty
+                        | otherwise    = ([], rho_ty)
+    
+       pp_tyvars = brackets (hsep (map (pTyVarB env) tyvars))
+       pp_body   = ppr_ty env tOP_PREC body_ty
+    
+       show_forall  = not (userStyle sty)
+       show_context = ifaceStyle sty || userStyle sty
+    in
+    if show_forall then
+       maybeParen ctxt_prec fUN_PREC $
+       sep [ ptext SLIT("_forall_"), pp_tyvars, 
+            ppr_theta env theta, ptext SLIT("=>"), pp_body
+       ]
 
-    show_context = case sty of
-                       PprInterface -> True
-                       PprForUser   -> True
-                       other        -> False
+    else if null theta then
+       ppr_ty env ctxt_prec body_ty
 
-ppr_ty env ctxt_prec (ForAllUsageTy uv uvs ty)
-  = panic "ppr_ty:ForAllUsageTy"
+    else
+       maybeParen ctxt_prec fUN_PREC $
+       sep [ppr_theta env theta, ptext SLIT("=>"), pp_body]
 
-ppr_ty env ctxt_prec (FunTy ty1 ty2 usage)
+ppr_ty env ctxt_prec (FunTy ty1 ty2)
     -- We fiddle the precedences passed to left/right branches,
     -- so that right associativity comes out nicely...
-  = maybeParen ctxt_prec fUN_PREC
-       (ppCat [ppr_ty env fUN_PREC ty1,
-               ppStr "->",
-               ppr_ty env tOP_PREC ty2])
-
-ppr_ty env ctxt_prec ty@(AppTy _ _)
-  = ppr_corner env ctxt_prec fun_ty arg_tys
-  where
-    (fun_ty, arg_tys) = splitAppTys ty
-
-ppr_ty env ctxt_prec (SynTy tycon tys expansion)
-  | codeStyle (pStyle env)
-       -- always expand types that squeak into C-variable names
-  = ppr_ty env ctxt_prec expansion
-
-  | otherwise
-  = ppBeside
-     (ppr_app env ctxt_prec (ppr_tycon env tycon) tys)
-     (ifPprShowAll (pStyle env) (ppCat [ppStr " {- expansion:",
-                                       ppr_ty env tOP_PREC expansion,
-                                       ppStr "-}"]))
-
-ppr_ty env ctxt_prec (DictTy clas ty usage)
-  = ppCurlies (ppr_dict env tOP_PREC (clas, ty))
-       -- Curlies are temporary
-
-
--- Some help functions
-ppr_corner env ctxt_prec (TyConTy FunTyCon usage) arg_tys
-  | length arg_tys == 2
-  = ppr_ty env ctxt_prec (FunTy ty1 ty2 usage)
+  = maybeParen ctxt_prec fUN_PREC (sep (ppr_ty env fUN_PREC ty1 : pp_rest))
   where
-    (ty1:ty2:_) = arg_tys
+    (arg_tys, result_ty) = splitFunTys ty2
+    pp_rest = [ ptext SLIT("-> ") <> ppr_ty env fUN_PREC ty | ty <- arg_tys ++ [result_ty] ]
 
-ppr_corner env ctxt_prec (TyConTy (TupleTyCon _ _ a) usage) arg_tys
-  | not (codeStyle (pStyle env)) -- no magic in that case
-  = --ASSERT(length arg_tys == a)
-    --(if (length arg_tys /= a) then pprTrace "ppr_corner:" (ppCat [ppInt a, ppInterleave ppComma (map (pprGenType PprDebug) arg_tys)]) else id) $
-    ppBesides [ppLparen, arg_tys_w_commas, ppRparen]
-  where
-    arg_tys_w_commas = ppIntersperse pp'SP (map (ppr_ty env tOP_PREC) arg_tys)
+ppr_ty env ctxt_prec (AppTy ty1 ty2)
+  = maybeParen ctxt_prec tYCON_PREC $
+    ppr_ty env tOP_PREC ty1 <+> ppr_ty env tYCON_PREC ty2
 
-ppr_corner env ctxt_prec (TyConTy tycon usage) arg_tys
-  | not (codeStyle (pStyle env)) && uniqueOf tycon == listTyConKey
-  = ASSERT(length arg_tys == 1)
-    ppBesides [ppLbrack, ppr_ty env tOP_PREC ty1, ppRbrack]                
-  where
-    (ty1:_) = arg_tys
+ppr_ty env ctxt_prec (SynTy ty expansion)
+  = ppr_ty env ctxt_prec ty
 
-ppr_corner env ctxt_prec (TyConTy tycon usage) arg_tys
-  = ppr_app env ctxt_prec (ppr_tycon env tycon) arg_tys
-                     
-ppr_corner env ctxt_prec (TyVarTy tyvar) arg_tys
-  = ppr_app env ctxt_prec (pTyVarO env tyvar) arg_tys
-  
+ppr_theta env []    = empty
+ppr_theta env theta = braces (hsep (punctuate comma (map (ppr_dict env tOP_PREC) theta)))
 
-ppr_app env ctxt_prec pp_fun []      
-  = pp_fun
-ppr_app env ctxt_prec pp_fun arg_tys 
-  = maybeParen ctxt_prec tYCON_PREC (ppCat [pp_fun, arg_tys_w_spaces])
-  where
-    arg_tys_w_spaces = ppIntersperse ppSP (map (ppr_ty env tYCON_PREC) arg_tys)
-
-
-ppr_dict env ctxt_prec (clas, ty)
-  = maybeParen ctxt_prec tYCON_PREC
-       (ppCat [ppr_class env clas, ppr_ty env tYCON_PREC ty]) 
+ppr_dict env ctxt (clas, tys) = ppr_class env clas <+> 
+                               hsep (map (ppr_ty env tYCON_PREC) tys)
 \end{code}
 
 \begin{code}
        -- This one uses only "ppr"
-init_ppr_env sty
-  = initPprEnv sty b b b b (Just (ppr sty)) (Just (ppr sty)) (Just (ppr sty)) b b b b b
+init_ppr_env
+  = initPprEnv b b b b (Just ppr) (Just ppr) b b b
   where
     b = panic "PprType:init_ppr_env"
 
        -- This one uses pprTyVarBndr, and thus is specific to GenTyVar's types
-init_ppr_env_type sty
-  = initPprEnv sty b b b b (Just (pprTyVarBndr sty)) (Just (ppr sty)) (Just (ppr sty)) b b b b b
+init_ppr_env_type
+  = initPprEnv b b b b (Just pprTyVarBndr) (Just ppr) b b b
   where
     b = panic "PprType:init_ppr_env"
 
-ppr_tycon  env tycon = ppr (pStyle env) tycon
-ppr_class  env clas  = ppr (pStyle env) clas
+ppr_tycon  env tycon = ppr tycon
+ppr_class  env clas  = ppr clas
 \end{code}
 
 %************************************************************************
@@ -268,33 +235,33 @@ ppr_class  env clas  = ppr (pStyle env) clas
 %************************************************************************
 
 \begin{code}
-pprGenTyVar sty (TyVar uniq kind name usage)
-  | codeStyle sty
-  = pp_u
-  | otherwise
-  = case sty of
-      PprInterface -> pp_u
-      _                   -> ppBesides [pp_name, ppStr "{-", pp_u, ppStr "-}"]
-   where
-    pp_u    = pprUnique uniq
-    pp_name = case name of
-               Just n  -> pprOccName sty (getOccName n)
-               Nothing -> case kind of
-                               TypeKind        -> ppChar 'o'
-                               BoxedTypeKind   -> ppChar 't'
-                               UnboxedTypeKind -> ppChar 'u'
-                               ArrowKind _ _   -> ppChar 'a'
+pprGenTyVar (TyVar uniq kind maybe_name _)
+  = case maybe_name of
+       -- If the tyvar has a name we can safely use just it, I think
+       Just n  -> pprOccName (getOccName n) <> ifPprDebug pp_debug
+       Nothing -> pprUnique uniq
+  where
+    pp_debug = text "_" <> pp_kind <> pprUnique uniq
+
+    pp_kind = case kind of
+               TypeKind        -> char 'o'
+               BoxedTypeKind   -> char 't'
+               UnboxedTypeKind -> char 'u'
+               ArrowKind _ _   -> char 'a'
 \end{code}
 
 We print type-variable binders with their kinds in interface files.
 
 \begin{code}
-pprTyVarBndr sty@PprInterface tyvar@(TyVar uniq kind name usage)
-  | not (isBoxedTypeKind kind)
-  = ppBesides [pprGenTyVar sty tyvar, ppStr " :: ", pprParendKind kind]
+pprTyVarBndr tyvar@(TyVar uniq kind name _)
+  = getPprStyle $ \ sty ->
+    if ifaceStyle sty && not (isBoxedTypeKind kind) then
+        hcat [pprGenTyVar tyvar, text " :: ", pprParendKind kind]
        -- See comments with ppDcolon in PprCore.lhs
+    else
+        pprGenTyVar tyvar
 
-pprTyVarBndr sty tyvar = pprGenTyVar sty tyvar
+pprTyVarBndrs tyvars = hsep (map pprTyVarBndr tyvars)
 \end{code}
 
 %************************************************************************
@@ -306,319 +273,141 @@ pprTyVarBndr sty tyvar = pprGenTyVar sty tyvar
 ToDo; all this is suspiciously like getOccName!
 
 \begin{code}
-showTyCon :: PprStyle -> TyCon -> String
-showTyCon sty tycon = ppShow 80 (pprTyCon sty tycon)
+showTyCon :: TyCon -> String
+showTyCon tycon = showSDoc (pprTyCon tycon)
 
-maybe_code sty x
-  = if codeStyle sty
-    then ppBesides (ppPStr SLIT("Prelude_") : map mangle x)
-    else ppStr x
-  where
-    -- ToDo: really should be in CStrings
-    mangle '(' = ppPStr SLIT("Z40") -- decimal ascii #s
-    mangle ')' = ppPStr SLIT("Z41")
-    mangle '[' = ppPStr SLIT("Z91")
-    mangle ']' = ppPStr SLIT("Z93")
-    mangle ',' = ppPStr SLIT("Z44")
-    mangle '-' = ppPStr SLIT("Zm")
-    mangle '>' = ppPStr SLIT("Zg")
-
-pprTyCon :: PprStyle -> TyCon -> Pretty
-pprTyCon sty tycon = ppr sty (getName tycon)
-
-{-     This old code looks suspicious to me.  
-       Just printing the name should do the job; apart from the extra junk 
-       on SynTyCons etc. 
-
-       Let's try and live without all this...
-       Delete in due course.                           SLPJ Nov 96
-
-pprTyCon sty (PrimTyCon _ name _ _) = ppr sty name
-
-pprTyCon sty FunTyCon              = maybe_code sty "->"
-pprTyCon sty (TupleTyCon _ _ arity) = case arity of
-                                       0 -> maybe_code sty "()"
-                                       2 -> maybe_code sty "(,)"
-                                       3 -> maybe_code sty "(,,)"
-                                       4 -> maybe_code sty "(,,,)"
-                                       5 -> maybe_code sty "(,,,,)"
-                                       n -> maybe_code sty ( "(" ++ nOfThem (n-1) ',' ++ ")" )
-
-pprTyCon sty tycon@(DataTyCon uniq name kind tyvars ctxt cons derivings nd)
-  = ppr sty name
-
-pprTyCon sty (SpecTyCon tc ty_maybes)
-  = ppBeside (pprTyCon sty tc)
-            ((if (codeStyle sty) then identToC else ppPStr) tys_stuff)
-  where
-    tys_stuff = specMaybeTysSuffix ty_maybes
-
-pprTyCon sty (SynTyCon uniq name kind arity tyvars expansion)
-  = ppBeside (ppr sty name)
-            (ifPprShowAll sty
-               (ppCat [ ppPStr SLIT(" {-"), 
-                        ppInt arity, 
-                        interpp'SP sty tyvars,
-                        pprParendGenType sty expansion,
-                        ppPStr SLIT("-}")]))
--}
+pprTyCon :: TyCon -> SDoc
+pprTyCon tycon = ppr (getName tycon)
 \end{code}
 
 
+
 %************************************************************************
 %*                                                                     *
-\subsection[Class]{@Class@}
+\subsection{Mumbo jumbo}
 %*                                                                     *
 %************************************************************************
 
+Grab a name for the type. This is used to determine the type
+description for profiling.
 \begin{code}
-pprGenClassOp :: Outputable ty => PprStyle -> GenClassOp ty -> Pretty
-
-pprGenClassOp sty op = ppr_class_op sty [] op
+getTyDescription :: Type -> String
 
-ppr_class_op sty tyvars (ClassOp op_name i ty)
-  = case sty of
-      PprForC      -> pp_C
-      PprForAsm _ _ -> pp_C
-      PprInterface  -> pp_sigd
-      PprShowAll    -> pp_sigd
-      _                    -> pp_user
+getTyDescription ty
+  = case (splitSigmaTy ty) of { (_, _, tau_ty) ->
+    case tau_ty of
+      TyVarTy _               -> "*"
+      AppTy fun _      -> getTyDescription fun
+      FunTy _ res      -> '-' : '>' : fun_result res
+      TyConApp tycon _ -> getOccString tycon
+      SynTy ty1 _      -> getTyDescription ty1
+      ForAllTy _ ty    -> getTyDescription ty
+    }
   where
-    pp_C    = ppr sty op_name
-    pp_user = pprNonSymOcc sty op_name
-    pp_sigd = ppCat [pp_user, ppPStr SLIT("::"), ppr sty ty]
+    fun_result (FunTy _ res) = '>' : fun_result res
+    fun_result other        = getTyDescription other
 \end{code}
 
 
+
 %************************************************************************
 %*                                                                     *
-\subsection{Mumbo jumbo}
+\subsection{Renumbering types}
 %*                                                                     *
 %************************************************************************
 
-\begin{code}
-    -- Shallowly magical; converts a type into something
-    -- vaguely close to what can be used in C identifier.
-    -- Produces things like what we have in mkCompoundName,
-    -- which can be "dot"ted together...
-
-getTypeString :: Type -> FAST_STRING
+We tend to {\em renumber} everything before printing, so that we get
+consistent Uniques on everything from run to run.
 
-getTypeString ty
-  = case (splitAppTys ty) of { (tc, args) ->
-    _CONCAT_ (do_tc tc : map do_arg_ty args) }
-  where
-    do_tc (TyConTy tc _) = nameString (getName tc)
-    do_tc (SynTy _ _ ty) = do_tc ty
-    do_tc other = --pprTrace "getTypeString:do_tc:" (pprType PprDebug other) $
-                 (_PK_ (ppShow 1000 (pprType PprForC other)))
-
-    do_arg_ty (TyConTy tc _) = nameString (getName tc)
-    do_arg_ty (TyVarTy tv)   = _PK_ (ppShow 80 (ppr PprForC tv))
-    do_arg_ty (SynTy _ _ ty) = do_arg_ty ty
-    do_arg_ty other         = --pprTrace "getTypeString:do_arg_ty:" (pprType PprDebug other) $
-                              _PK_ (ppShow 1000 (pprType PprForC other))
-
-       -- PprForC expands type synonyms as it goes;
-       -- it also forces consistent naming of tycons
-       -- (e.g., can't have both "(,) a b" and "(a,b)":
-       -- must be consistent!
-
-specMaybeTysSuffix :: [Maybe Type] -> FAST_STRING
-specMaybeTysSuffix ty_maybes
-  = panic "PprType.specMaybeTysSuffix"
-{- LATER:
-  = let
-       ty_strs  = concat (map typeMaybeString ty_maybes)
-       dotted_tys = [ _CONS_ '.' str | str <- ty_strs ]
-    in
-    _CONCAT_ dotted_tys
--}
-\end{code}
 
-Grab a name for the type. This is used to determine the type
-description for profiling.
 \begin{code}
-getTyDescription :: Type -> String
+nmbrGlobalType :: Type -> Type         -- Renumber a top-level type
+nmbrGlobalType ty = nmbrType (\tyvar -> tyvar) initTyVarUnique ty
 
-getTyDescription ty
-  = case (splitSigmaTy ty) of { (_, _, tau_ty) ->
-    case tau_ty of
-      TyVarTy _              -> "*"
-      AppTy fun _     -> getTyDescription fun
-      FunTy _ res _   -> '-' : '>' : fun_result res
-      TyConTy tycon _ -> getOccString tycon
-      SynTy tycon _ _ -> getOccString tycon
-      DictTy _ _ _    -> "dict"
-      ForAllTy _ ty   -> getTyDescription ty
-      _                      -> pprPanic "getTyDescription: other" (pprType PprDebug tau_ty)
-    }
-  where
-    fun_result (FunTy _ res _) = '>' : fun_result res
-    fun_result other          = getTyDescription other
-\end{code}
+nmbrType :: (TyVar -> TyVar)           -- Mapping for free vars
+        -> Unique
+        -> Type
+        -> Type
 
-ToDo: possibly move:
-\begin{code}
-nmbrType :: Type -> NmbrM Type
+nmbrType tyvar_env uniq ty
+  = initNmbr tyvar_env uniq (nmbrTy ty)
 
-nmbrType (TyVarTy tv)
-  = nmbrTyVar tv    `thenNmbr` \ new_tv ->
+nmbrTy :: Type -> NmbrM Type
+
+nmbrTy (TyVarTy tv)
+  = lookupTyVar tv    `thenNmbr` \ new_tv ->
     returnNmbr (TyVarTy new_tv)
 
-nmbrType (AppTy t1 t2)
-  = nmbrType t1            `thenNmbr` \ new_t1 ->
-    nmbrType t2            `thenNmbr` \ new_t2 ->
+nmbrTy (AppTy t1 t2)
+  = nmbrTy t1      `thenNmbr` \ new_t1 ->
+    nmbrTy t2      `thenNmbr` \ new_t2 ->
     returnNmbr (AppTy new_t1 new_t2)
 
-nmbrType (TyConTy tc use)
-  = --nmbrTyCon tc    `thenNmbr` \ new_tc ->
-    nmbrUsage use   `thenNmbr` \ new_use ->
-    returnNmbr (TyConTy tc new_use)
+nmbrTy (TyConApp tc tys)
+  = nmbrTys tys                `thenNmbr` \ new_tys ->
+    returnNmbr (TyConApp tc new_tys)
 
-nmbrType (SynTy tc args expand)
-  = --nmbrTyCon tc         `thenNmbr` \ new_tc ->
-    mapNmbr nmbrType args   `thenNmbr` \ new_args ->
-    nmbrType expand        `thenNmbr` \ new_expand ->
-    returnNmbr (SynTy tc new_args new_expand)
+nmbrTy (SynTy ty1 ty2)
+  = nmbrTy ty1     `thenNmbr` \ new_ty1 ->
+    nmbrTy ty2     `thenNmbr` \ new_ty2 ->
+    returnNmbr (SynTy new_ty1 new_ty2)
 
-nmbrType (ForAllTy tv ty)
-  = addTyVar tv                `thenNmbr` \ new_tv ->
-    nmbrType ty                `thenNmbr` \ new_ty ->
+nmbrTy (ForAllTy tv ty)
+  = addTyVar tv                $ \ new_tv ->
+    nmbrTy ty          `thenNmbr` \ new_ty ->
     returnNmbr (ForAllTy new_tv new_ty)
 
-nmbrType (ForAllUsageTy u us ty)
-  = addUVar u              `thenNmbr` \ new_u  ->
-    mapNmbr nmbrUVar us     `thenNmbr` \ new_us ->
-    nmbrType ty                    `thenNmbr` \ new_ty ->
-    returnNmbr (ForAllUsageTy new_u new_us new_ty)
-
-nmbrType (FunTy t1 t2 use)
-  = nmbrType t1            `thenNmbr` \ new_t1 ->
-    nmbrType t2            `thenNmbr` \ new_t2 ->
-    nmbrUsage use   `thenNmbr` \ new_use ->
-    returnNmbr (FunTy new_t1 new_t2 new_use)
-
-nmbrType (DictTy c ty use)
-  = --nmbrClass c          `thenNmbr` \ new_c   ->
-    nmbrType  ty    `thenNmbr` \ new_ty  ->
-    nmbrUsage use   `thenNmbr` \ new_use ->
-    returnNmbr (DictTy c new_ty new_use)
-\end{code}
+nmbrTy (FunTy t1 t2)
+  = nmbrTy t1      `thenNmbr` \ new_t1 ->
+    nmbrTy t2      `thenNmbr` \ new_t2 ->
+    returnNmbr (FunTy new_t1 new_t2)
 
-\begin{code}
-addTyVar, nmbrTyVar :: TyVar -> NmbrM TyVar
-
-addTyVar tv@(TyVar u k maybe_name use) nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
-  = --pprTrace "addTyVar:" (ppCat [pprUnique u, pprUnique ut]) $
-    case (lookupUFM_Directly tvenv u) of
-      Just xx -> -- pprTrace "addTyVar: already in map!" (ppr PprDebug tv) $
-                -- (It gets triggered when we do a datatype: first we
-                -- "addTyVar" the tyvars for the datatype as a whole;
-                -- we will subsequently "addId" the data cons, including
-                -- the type for each of them -- each of which includes
-                -- _forall_ ...tvs..., which we will addTyVar.
-                -- Harmless, if that's all that happens....
-                (nenv, xx)
-      Nothing ->
-       let
-           nenv_plus_tv     = NmbrEnv ui (incrUnique ut) uu
-                                      idenv
-                                      (addToUFM_Directly tvenv u new_tv)
-                                      uvenv
-
-           (nenv2, new_use) = nmbrUsage use nenv_plus_tv
-
-           new_tv = TyVar ut k maybe_name new_use
-       in
-       (nenv2, new_tv)
-
-nmbrTyVar tv@(TyVar u _ _ _) nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
-  = case (lookupUFM_Directly tvenv u) of
-      Just xx -> (nenv, xx)
-      Nothing ->
-       --pprTrace "nmbrTyVar: lookup failed:" (ppCat (ppr PprDebug u : [ppCat [ppr PprDebug x, ppPStr SLIT("=>"), ppr PprDebug tv] | (x,tv) <- ufmToList tvenv])) $
-       (nenv, tv)
-\end{code}
 
-nmbrTyCon : only called from ``top-level'', if you know what I mean.
-\begin{code}
-nmbrTyCon tc@FunTyCon            = returnNmbr tc
-nmbrTyCon tc@(TupleTyCon _ _ _)          = returnNmbr tc
-nmbrTyCon tc@(PrimTyCon  _ _ _ _) = returnNmbr tc
-
-nmbrTyCon (DataTyCon u n k tvs theta cons clss nod)
-  = --pprTrace "nmbrDataTyCon:" (ppCat (map (ppr PprDebug) tvs)) $
-    mapNmbr addTyVar   tvs     `thenNmbr` \ new_tvs   ->
-    mapNmbr nmbr_theta theta   `thenNmbr` \ new_theta ->
-    mapNmbr nmbrId     cons    `thenNmbr` \ new_cons  ->
-    returnNmbr (DataTyCon u n k new_tvs new_theta new_cons clss nod)
+nmbrTys tys = mapNmbr nmbrTy tys
+
+lookupTyVar tyvar (NmbrEnv tv_fn tv_env) uniq
+  = (uniq, tyvar')
   where
-    nmbr_theta (c,t)
-      = --nmbrClass c  `thenNmbr` \ new_c ->
-        nmbrType  t    `thenNmbr` \ new_t ->
-       returnNmbr (c, new_t)
-
-nmbrTyCon (SynTyCon u n k a tvs expand)
-  = mapNmbr addTyVar   tvs     `thenNmbr` \ new_tvs ->
-    nmbrType          expand   `thenNmbr` \ new_expand ->
-    returnNmbr (SynTyCon u n k a new_tvs new_expand)
-
-nmbrTyCon (SpecTyCon tc specs)
-  = mapNmbr nmbrMaybeTy specs  `thenNmbr` \ new_specs ->
-    returnNmbr (SpecTyCon tc new_specs)
-
------------
-nmbrMaybeTy Nothing  = returnNmbr Nothing
-nmbrMaybeTy (Just t) = nmbrType t `thenNmbr` \ new_t ->
-                      returnNmbr (Just new_t)
-\end{code}
+    tyvar' = case lookupUFM tv_env tyvar of
+               Just tyvar' -> tyvar'
+               Nothing     -> tv_fn tyvar
 
-\begin{code}
-nmbrClass (Class u n tv supers ssels ops osels odefms instenv isupers)
-  = addTyVar tv                `thenNmbr` \ new_tv  ->
-    mapNmbr nmbr_op ops        `thenNmbr` \ new_ops ->
-    returnNmbr (Class u n new_tv supers ssels new_ops osels odefms instenv isupers)
+addTyVar tv m (NmbrEnv f_tv tv_ufm) u
+  = m tv' nenv u'
   where
-    nmbr_op (ClassOp n tag ty)
-      = nmbrType ty    `thenNmbr` \ new_ty ->
-       returnNmbr (ClassOp n tag new_ty)
+    nenv    = NmbrEnv f_tv tv_ufm'
+    tv_ufm' = addToUFM tv_ufm tv tv'
+    tv'            = cloneTyVar tv u
+    u'      = incrUnique u
 \end{code}
 
-\begin{code}
-nmbrUsage :: Usage -> NmbrM Usage
-
-nmbrUsage u = returnNmbr u
-{- LATER:
-nmbrUsage u@UsageOne   = returnNmbr u
-nmbrUsage u@UsageOmega = returnNmbr u
-nmbrUsage (UsageVar u)
-  = nmbrUVar u `thenNmbr` \ new_u ->
-    returnNmbr (UsageVar new_u)
--}
-\end{code}
+Monad stuff
 
 \begin{code}
-addUVar, nmbrUVar :: UVar -> NmbrM UVar
-
-addUVar u nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
-  = case (lookupUFM_Directly uvenv u) of
-      Just xx -> trace "addUVar: already in map!" $
-                (nenv, xx)
-      Nothing ->
-       let
-           nenv_plus_uv     = NmbrEnv ui ut (incrUnique uu)
-                                      idenv
-                                      tvenv
-                                      (addToUFM_Directly uvenv u new_uv)
-           new_uv = uu
-       in
-       (nenv_plus_uv, new_uv)
-
-nmbrUVar u nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
-  = case (lookupUFM_Directly uvenv u) of
-      Just xx -> (nenv, xx)
-      Nothing ->
-       trace "nmbrUVar: lookup failed" $
-       (nenv, u)
+data NmbrEnv
+  = NmbrEnv (TyVar -> TyVar) (UniqFM TyVar)            -- Global and local map for tyvars
+
+type NmbrM a = NmbrEnv -> Unique -> (Unique, a)                -- Unique is name supply
+
+initNmbr :: (TyVar -> TyVar) -> Unique -> NmbrM a -> a
+initNmbr tyvar_env uniq m
+  = let
+       init_nmbr_env = NmbrEnv tyvar_env emptyUFM
+    in
+    snd (m init_nmbr_env uniq)
+
+returnNmbr x nenv u = (u, x)
+
+thenNmbr m k nenv u
+  = let
+       (u', res) = m nenv u
+    in
+    k res nenv u'
+
+
+mapNmbr f []     = returnNmbr []
+mapNmbr f (x:xs)
+  = f x                    `thenNmbr` \ r  ->
+    mapNmbr f xs    `thenNmbr` \ rs ->
+    returnNmbr (r:rs)
 \end{code}