[project @ 2000-11-21 09:57:02 by sewardj]
[ghc-hetmet.git] / ghc / compiler / types / TypeRep.lhs
index 26e5078..a533cd5 100644 (file)
@@ -5,8 +5,10 @@
 
 \begin{code}
 module TypeRep (
-       Type(..), TyNote(..), UsageAnn(..),             -- Representation visible to friends
-       Kind, TyVarSubst,
+       Type(..), TyNote(..), PredType(..),             -- Representation visible to friends
+       
+       Kind, ThetaType, RhoType, TauType, SigmaType,           -- Synonyms
+       TyVarSubst,
 
        superKind, superBoxity,                         -- KX and BX respectively
        boxedBoxity, unboxedBoxity,                     -- :: BX
@@ -15,28 +17,30 @@ module TypeRep (
        boxedTypeKind, unboxedTypeKind, openTypeKind,   -- :: KX
        mkArrowKind, mkArrowKinds,                      -- :: KX -> KX -> KX
 
+        usageKindCon,                                  -- :: KX
+        usageTypeKind,                                 -- :: KX
+        usOnceTyCon, usManyTyCon,                      -- :: $
+        usOnce, usMany,                                        -- :: $
+
        funTyCon
     ) where
 
 #include "HsVersions.h"
 
 -- friends:
-import Var     ( TyVar, UVar )
+import Var     ( TyVar )
 import VarEnv
 import VarSet
 
-import Name    ( Name, Provenance(..), ExportFlag(..),
-                 mkWiredInTyConName, mkGlobalName, mkKindOccFS, tcName,
-               )
-import TyCon   ( TyCon, KindCon,
-                 mkFunTyCon, mkKindCon, mkSuperKindCon,
-               )
+import Name    ( Name, tcName )
+import TyCon   ( TyCon, KindCon, mkFunTyCon, mkKindCon, mkSuperKindCon )
+import Class   ( Class )
 
 -- others
-import SrcLoc          ( mkBuiltinSrcLoc )
-import PrelNames       ( pREL_GHC )
-import Unique          -- quite a few *Keys
-import Util            ( thenCmp )
+import PrelNames       ( superKindName, superBoxityName, boxedConName, 
+                         unboxedConName, typeConName, openKindConName, funTyConName,
+                         usageKindConName, usOnceTyConName, usManyTyConName
+                       )
 \end{code}
 
 %************************************************************************
@@ -107,36 +111,73 @@ data Type
        Type            -- Function is *not* a TyConApp
        Type
 
-  | TyConApp                   -- Application of a TyCon
-       TyCon                   -- *Invariant* saturated appliations of FunTyCon and
-                               --      synonyms have their own constructors, below.
+  | TyConApp           -- Application of a TyCon
+       TyCon           -- *Invariant* saturated appliations of FunTyCon and
+                       --      synonyms have their own constructors, below.
        [Type]          -- Might not be saturated.
 
-  | FunTy                      -- Special case of TyConApp: TyConApp FunTyCon [t1,t2]
+  | FunTy              -- Special case of TyConApp: TyConApp FunTyCon [t1,t2]
        Type
        Type
 
-  | NoteTy                     -- Saturated application of a type synonym
+  | ForAllTy           -- A polymorphic type
+       TyVar
+       Type    
+
+  | PredTy             -- A Haskell predicate
+       PredType
+
+  | UsageTy            -- A usage-annotated type
+       Type            --   - Annotation of kind $ (i.e., usage annotation)
+       Type            --   - Annotated type
+
+  | NoteTy             -- A type with a note attached
        TyNote
        Type            -- The expanded version
 
-  | ForAllTy
-       TyVar
-       Type            -- TypeKind
-
 data TyNote
   = SynNote Type       -- The unexpanded version of the type synonym; always a TyConApp
   | FTVNote TyVarSet   -- The free type variables of the noted expression
-  | UsgNote UsageAnn    -- The usage annotation at this node
-  | UsgForAll UVar      -- Annotation variable binder
-  | IPNote Name                -- It's an implicit parameter
-
-data UsageAnn
-  = UsOnce             -- Used at most once
-  | UsMany             -- Used possibly many times (no info; this annotation can be omitted)
-  | UsVar    UVar      -- Annotation is variable (unbound OK only inside analysis)
+
+type ThetaType           = [PredType]
+type RhoType             = Type
+type TauType             = Type
+type SigmaType    = Type
 \end{code}
 
+INVARIANT: UsageTys are optional, but may *only* appear immediately
+under a FunTy (either argument), or at top-level of a Type permitted
+to be annotated (such as the type of an Id).  NoteTys are transparent
+for the purposes of this rule.
+
+-------------------------------------
+               Predicates
+
+Consider these examples:
+       f :: (Eq a) => a -> Int
+       g :: (?x :: Int -> Int) => a -> Int
+       h :: (r\l) => {r} => {l::Int | r}
+
+Here the "Eq a" and "?x :: Int -> Int" and "r\l" are all called *predicates*
+Predicates are represented inside GHC by PredType:
+
+\begin{code}
+data PredType  = Class  Class [Type]
+              | IParam Name  Type
+\end{code}
+
+(We don't support TREX records yet, but the setup is designed
+to expand to allow them.)
+
+A Haskell qualified type, such as that for f,g,h above, is
+represented using 
+       * a FunTy for the double arrow
+       * with a PredTy as the function argument
+
+The predicate really does turn into a real extra argument to the
+function.  If the argument has type (PredTy p) then the predicate p is
+represented by evidence (a dictionary, for example, of type (predRepTy p).
+
 
 %************************************************************************
 %*                                                                     *
@@ -147,9 +188,12 @@ data UsageAnn
 Kinds
 ~~~~~
 kind :: KX = kind -> kind
+
            | Type boxity       -- (Type *) is printed as just *
                                -- (Type #) is printed as just #
 
+           | UsageKind         -- Printed '$'; used for usage annotations
+
            | OpenKind          -- Can be boxed or unboxed
                                -- Printed '?'
 
@@ -162,7 +206,7 @@ boxity :: BX = *    -- Boxed
 There's a little subtyping at the kind level:  
        forall b. Type b <: OpenKind
 
-That is, a type of kind (Type b) OK in a context requiring an AnyBox.
+That is, a type of kind (Type b) is OK in a context requiring an OpenKind
 
 OpenKind, written '?', is used as the kind for certain type variables,
 in two situations:
@@ -182,26 +226,15 @@ in two situations:
     present in an inferred type.
 
 
-\begin{code}
-mk_kind_name key str = mkGlobalName key pREL_GHC (mkKindOccFS tcName str)
-                                   (LocalDef mkBuiltinSrcLoc NotExported)
-       -- mk_kind_name is a bit of a hack
-       -- The LocalDef means that we print the name without
-       -- a qualifier, which is what we want for these kinds.
-       -- It's used for both Kinds and Boxities
-\end{code}
-
 ------------------------------------------
 Define  KX, the type of a kind
        BX, the type of a boxity
 
 \begin{code}
 superKind :: SuperKind                 -- KX, the type of all kinds
-superKindName = mk_kind_name kindConKey SLIT("KX")
 superKind = TyConApp (mkSuperKindCon superKindName) []
 
 superBoxity :: SuperKind               -- BX, the type of all boxities
-superBoxityName = mk_kind_name boxityConKey SLIT("BX")
 superBoxity = TyConApp (mkSuperKindCon superBoxityName) []
 \end{code}
 
@@ -210,20 +243,16 @@ Define boxities: @*@ and @#@
 
 \begin{code}
 boxedBoxity, unboxedBoxity :: Kind             -- :: BX
-
-boxedConName = mk_kind_name boxedConKey SLIT("*")
 boxedBoxity  = TyConApp (mkKindCon boxedConName superBoxity) []
 
-unboxedConName = mk_kind_name unboxedConKey SLIT("#")
 unboxedBoxity  = TyConApp (mkKindCon unboxedConName superBoxity) []
 \end{code}
 
 ------------------------------------------
-Define kinds: Type, Type *, Type #, and OpenKind
+Define kinds: Type, Type *, Type #, OpenKind, and UsageKind
 
 \begin{code}
 typeCon :: KindCon     -- :: BX -> KX
-typeConName = mk_kind_name typeConKey SLIT("Type")
 typeCon     = mkKindCon typeConName (superBoxity `FunTy` superKind)
 
 boxedTypeKind, unboxedTypeKind, openTypeKind :: Kind   -- Of superkind superKind
@@ -231,9 +260,11 @@ boxedTypeKind, unboxedTypeKind, openTypeKind :: Kind       -- Of superkind superKind
 boxedTypeKind   = TyConApp typeCon [boxedBoxity]
 unboxedTypeKind = TyConApp typeCon [unboxedBoxity]
 
-openKindConName = mk_kind_name anyBoxConKey SLIT("?")
 openKindCon     = mkKindCon openKindConName superKind
 openTypeKind    = TyConApp openKindCon []
+
+usageKindCon     = mkKindCon usageKindConName superKind
+usageTypeKind    = TyConApp usageKindCon []
 \end{code}
 
 ------------------------------------------
@@ -257,66 +288,20 @@ mkArrowKinds arg_kinds result_kind = foldr mkArrowKind result_kind arg_kinds
 We define a few wired-in type constructors here to avoid module knots
 
 \begin{code}
-funTyConName = mkWiredInTyConName funTyConKey pREL_GHC SLIT("(->)") funTyCon
 funTyCon = mkFunTyCon funTyConName (mkArrowKinds [boxedTypeKind, boxedTypeKind] boxedTypeKind)
 \end{code}
 
+------------------------------------------
+Usage tycons @.@ and @!@
 
-%************************************************************************
-%*                                                                     *
-\subsection{Equality on types}
-%*                                                                     *
-%************************************************************************
-
-For the moment at least, type comparisons don't work if 
-there are embedded for-alls.
+The usage tycons are of kind usageTypeKind (`$').  The types contain
+no values, and are used purely for usage annotation.  
 
 \begin{code}
-instance Eq Type where
-  ty1 == ty2 = case ty1 `cmpTy` ty2 of { EQ -> True; other -> False }
-
-instance Ord Type where
-  compare ty1 ty2 = cmpTy ty1 ty2
-
-cmpTy :: Type -> Type -> Ordering
-cmpTy ty1 ty2
-  = cmp emptyVarEnv ty1 ty2
-  where
-  -- The "env" maps type variables in ty1 to type variables in ty2
-  -- So when comparing for-alls.. (forall tv1 . t1) (forall tv2 . t2)
-  -- we in effect substitute tv2 for tv1 in t1 before continuing
-    lookup env tv1 = case lookupVarEnv env tv1 of
-                         Just tv2 -> tv2
-                         Nothing  -> tv1
-
-    -- Get rid of NoteTy
-    cmp env (NoteTy _ ty1) ty2 = cmp env ty1 ty2
-    cmp env ty1 (NoteTy _ ty2) = cmp env ty1 ty2
-    
-    -- Deal with equal constructors
-    cmp env (TyVarTy tv1) (TyVarTy tv2) = lookup env tv1 `compare` tv2
-    cmp env (AppTy f1 a1) (AppTy f2 a2) = cmp env f1 f2 `thenCmp` cmp env a1 a2
-    cmp env (FunTy f1 a1) (FunTy f2 a2) = cmp env f1 f2 `thenCmp` cmp env a1 a2
-    cmp env (TyConApp tc1 tys1) (TyConApp tc2 tys2) = (tc1 `compare` tc2) `thenCmp` (cmps env tys1 tys2)
-    cmp env (ForAllTy tv1 t1)   (ForAllTy tv2 t2)   = cmp (extendVarEnv env tv1 tv2) t1 t2
-    
-    -- Deal with the rest: TyVarTy < AppTy < FunTy < TyConApp < ForAllTy
-    cmp env (AppTy _ _) (TyVarTy _) = GT
-    
-    cmp env (FunTy _ _) (TyVarTy _) = GT
-    cmp env (FunTy _ _) (AppTy _ _) = GT
-    
-    cmp env (TyConApp _ _) (TyVarTy _) = GT
-    cmp env (TyConApp _ _) (AppTy _ _) = GT
-    cmp env (TyConApp _ _) (FunTy _ _) = GT
-    
-    cmp env (ForAllTy _ _) other       = GT
-    
-    cmp env _ _                               = LT
-
-    cmps env []     [] = EQ
-    cmps env (t:ts) [] = GT
-    cmps env [] (t:ts) = LT
-    cmps env (t1:t1s) (t2:t2s) = cmp env t1 t2 `thenCmp` cmps env t1s t2s
+usOnceTyCon     = mkKindCon usOnceTyConName usageTypeKind
+usOnce          = TyConApp usOnceTyCon []
+
+usManyTyCon     = mkKindCon usManyTyConName usageTypeKind
+usMany          = TyConApp usManyTyCon []
 \end{code}