[project @ 1997-07-05 01:44:50 by sof]
[ghc-hetmet.git] / ghc / compiler / types / TyCon.lhs
index 02a7dd3..ada7c8d 100644 (file)
@@ -9,10 +9,10 @@
 module TyCon(
        TyCon(..),      -- NB: some pals need to see representation
 
-       Arity(..), NewOrData(..),
+       SYN_IE(Arity), NewOrData(..),
 
        isFunTyCon, isPrimTyCon, isBoxedTyCon,
-       isDataTyCon, isSynTyCon, isNewTyCon,
+       isAlgTyCon, isDataTyCon, isSynTyCon, isNewTyCon, maybeNewTyCon,
 
        mkDataTyCon,
        mkFunTyCon,
@@ -34,41 +34,44 @@ module TyCon(
        getSynTyConDefn,
 
         maybeTyConSingleCon,
-       isEnumerationTyCon,
-       derivedFor
+       isEnumerationTyCon, isTupleTyCon,
+       derivedClasses
 ) where
 
 CHK_Ubiq()     -- debugging consistency check
 
-IMPORT_DELOOPER(TyLoop)                ( Type(..), GenType,
-                         Class(..), GenClass,
-                         Id(..), GenId,
-                         mkTupleCon, isNullaryDataCon,
-                         specMaybeTysSuffix
+#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 201
+IMPORT_DELOOPER(TyLoop) ( SYN_IE(Type), GenType,
+                         SYN_IE(Class), GenClass,
+                         SYN_IE(Id), GenId,
+                         splitSigmaTy, splitFunTy,
+                         tupleCon, isNullaryDataCon, idType
+                         --LATER: specMaybeTysSuffix
                        )
+#else
+import {-# SOURCE #-} Type  ( Type, splitSigmaTy, splitFunTy  )
+import {-# SOURCE #-} Class ( Class )
+import {-# SOURCE #-} Id    ( Id, isNullaryDataCon, idType )
+import {-# SOURCE #-} TysWiredIn ( tupleCon )
+#endif
 
-import TyVar           ( GenTyVar, alphaTyVars, alphaTyVar, betaTyVar )
-import Usage           ( GenUsage, Usage(..) )
+import BasicTypes      ( SYN_IE(Arity), NewOrData(..) )
+import TyVar           ( GenTyVar, alphaTyVars, alphaTyVar, betaTyVar, SYN_IE(TyVar) )
+import Usage           ( GenUsage, SYN_IE(Usage) )
 import Kind            ( Kind, mkBoxedTypeKind, mkArrowKind, resultKind, argKind )
 
 import Maybes
-import Name            ( Name, RdrName(..), appendRdr, nameUnique,
-                         mkTupleTyConName, mkFunTyConName
-                       )
-import Unique          ( Unique, funTyConKey, mkTupleTyConUnique )
-import Pretty          ( Pretty(..), PrettyRep )
+import Name            ( Name, nameUnique, mkWiredInTyConName, NamedThing(getName) )
+import Unique          ( Unique, funTyConKey, Uniquable(..) )
+import Pretty          ( Doc )
 import PrimRep         ( PrimRep(..) )
+import PrelMods                ( gHC__, pREL_TUP, pREL_BASE )
+import Lex             ( mkTupNameStr )
 import SrcLoc          ( SrcLoc, mkBuiltinSrcLoc )
-import Util            ( panic, panic#, pprPanic{-ToDo:rm-}, nOfThem, isIn, Ord3(..) )
-import {-hide me-}
-       PprType (pprTyCon)
-import {-hide me-}
-       PprStyle--ToDo:rm
+import Util            ( nOfThem, isIn, Ord3(..), panic, panic#, assertPanic )
 \end{code}
 
 \begin{code}
-type Arity = Int
-
 data TyCon
   = FunTyCon           -- Kind = Type -> Type -> Type
 
@@ -77,7 +80,12 @@ data TyCon
                Kind
                [TyVar]
                [(Class,Type)]  -- Its context
-               [Id]            -- Its data constructors, with fully polymorphic types
+               [Id{-DataCon-}] -- Its data constructors, with fully polymorphic types
+                               --      This list can be empty, when we import a data type abstractly,
+                               --      either (a) the interface is hand-written and doesn't give
+                               --                 the constructors, or
+                               --             (b) in a quest for fast compilation we don't import 
+                               --                 the constructors
                [Class]         -- Classes which have derived instances
                NewOrData
 
@@ -116,28 +124,18 @@ data TyCon
        Type            -- Right-hand side, mentioning these type vars.
                        -- Acts as a template for the expansion when
                        -- the tycon is applied to some types.
-
-data NewOrData
-  = NewType        -- "newtype Blah ..."
-  | DataType       -- "data Blah ..."
 \end{code}
 
 \begin{code}
-mkFunTyCon   = FunTyCon
-mkSpecTyCon  = SpecTyCon
+mkFunTyCon     = FunTyCon
+mkFunTyConName = mkWiredInTyConName funTyConKey gHC__ SLIT("->") FunTyCon
 
-mkTupleTyCon arity
-  = TupleTyCon u n arity 
-  where
-    n = mkTupleTyConName arity
-    u = uniqueOf n
+mkSpecTyCon  = SpecTyCon
+mkTupleTyCon = TupleTyCon
 
-mkDataTyCon name
-  = DataTyCon (nameUnique name) name
-mkPrimTyCon name
-  = PrimTyCon (nameUnique name) name
-mkSynTyCon name
-  = SynTyCon (nameUnique name) name
+mkDataTyCon name = DataTyCon (nameUnique name) name
+mkPrimTyCon name = PrimTyCon (nameUnique name) name
+mkSynTyCon  name = SynTyCon  (nameUnique name) name
 
 isFunTyCon FunTyCon = True
 isFunTyCon _ = False
@@ -149,12 +147,26 @@ isPrimTyCon _ = False
 -- isBoxedTyCon is just the negation of isPrimTyCon.
 isBoxedTyCon = not . isPrimTyCon
 
+-- isAlgTyCon returns True for both @data@ and @newtype@
+isAlgTyCon (DataTyCon _ _ _ _ _ _ _ _) = True
+isAlgTyCon (TupleTyCon _ _ _)         = True
+isAlgTyCon other                      = False
+
 -- isDataTyCon returns False for @newtype@.
--- Not sure about this decision yet.
 isDataTyCon (DataTyCon _ _ _ _ _ _ _ DataType) = True
 isDataTyCon (TupleTyCon _ _ _)                = True
 isDataTyCon other                             = False
 
+maybeNewTyCon :: TyCon -> Maybe ([TyVar], Type)        -- Returns representation type info
+maybeNewTyCon (DataTyCon _ _ _ _ _ (con:null_cons) _ NewType) 
+  = ASSERT( null null_cons && null null_tys)
+    Just (tyvars, rep_ty)
+  where
+    (tyvars, theta, tau)      = splitSigmaTy (idType con)
+    (rep_ty:null_tys, res_ty) = splitFunTy tau
+
+maybeNewTyCon other = Nothing
+
 isNewTyCon (DataTyCon _ _ _ _ _ _ _ NewType) = True 
 isNewTyCon other                            = False
 
@@ -221,7 +233,7 @@ tyConDataCons :: TyCon -> [Id]
 tyConFamilySize  :: TyCon -> Int
 
 tyConDataCons (DataTyCon _ _ _ _ _ data_cons _ _) = data_cons
-tyConDataCons (TupleTyCon _ _ a)                 = [mkTupleCon a]
+tyConDataCons (TupleTyCon _ _ a)                 = [tupleCon a]
 tyConDataCons other                              = []
        -- You may think this last equation should fail,
        -- but it's quite convenient to return no constructors for
@@ -230,7 +242,7 @@ tyConDataCons other                           = []
 tyConFamilySize (DataTyCon _ _ _ _ _ data_cons _ _) = length data_cons
 tyConFamilySize (TupleTyCon _ _ _)                 = 1
 #ifdef DEBUG
-tyConFamilySize other = pprPanic "tyConFamilySize:" (pprTyCon PprDebug other)
+--tyConFamilySize other = pprPanic "tyConFamilySize:" (pprTyCon PprDebug other)
 #endif
 
 tyConPrimRep :: TyCon -> PrimRep
@@ -259,7 +271,7 @@ getSynTyConDefn (SynTyCon _ _ _ _ tyvars ty) = (tyvars,ty)
 \begin{code}
 maybeTyConSingleCon :: TyCon -> Maybe Id
 
-maybeTyConSingleCon (TupleTyCon _ _ arity)        = Just (mkTupleCon arity)
+maybeTyConSingleCon (TupleTyCon _ _ arity)        = Just (tupleCon arity)
 maybeTyConSingleCon (DataTyCon _ _ _ _ _ [c] _ _) = Just c
 maybeTyConSingleCon (DataTyCon _ _ _ _ _ _   _ _) = Nothing
 maybeTyConSingleCon (PrimTyCon _ _ _ _)                  = Nothing
@@ -270,6 +282,13 @@ isEnumerationTyCon (TupleTyCon _ _ arity)
   = arity == 0
 isEnumerationTyCon (DataTyCon _ _ _ _ _ data_cons _ _)
   = not (null data_cons) && all isNullaryDataCon data_cons
+
+
+isTupleTyCon (TupleTyCon _ _ arity) = arity >= 2    -- treat "0-tuple" specially
+isTupleTyCon (SpecTyCon tc tys)     = isTupleTyCon tc
+isTupleTyCon other                 = False
+
+
 \end{code}
 
 @derivedFor@ reports if we have an {\em obviously}-derived instance
@@ -280,9 +299,9 @@ function doesn't deal with that.
 ToDo: what about derivings for specialised tycons !!!
 
 \begin{code}
-derivedFor :: Class -> TyCon -> Bool
-derivedFor clas (DataTyCon _ _ _ _ _ _ derivs _) = isIn "derivedFor" clas derivs
-derivedFor clas something_weird                         = False
+derivedClasses :: TyCon -> [Class]
+derivedClasses (DataTyCon _ _ _ _ _ _ derivs _) = derivs
+derivedClasses something_weird                 = []
 \end{code}
 
 %************************************************************************
@@ -336,4 +355,5 @@ instance NamedThing TyCon where
     getName    other_tc           = moduleNamePair (expectJust "tycon1" (getName other_tc))
     getName other                           = Nothing
 -}
+
 \end{code}