Add {-# OPTIONS_GHC -w #-} and some blurb to all compiler modules
[ghc-hetmet.git] / compiler / basicTypes / MkId.lhs
index 6af89b7..76fd6e4 100644 (file)
@@ -1,7 +1,7 @@
 %
 %
+% (c) The University of Glasgow 2006
 % (c) The AQUA Project, Glasgow University, 1998
 %
 % (c) The AQUA Project, Glasgow University, 1998
 %
-\section[StdIdInfo]{Standard unfoldings}
 
 This module contains definitions for the IdInfo for things that
 have a standard form, namely:
 
 This module contains definitions for the IdInfo for things that
 have a standard form, namely:
@@ -12,15 +12,23 @@ have a standard form, namely:
        * primitive operations
 
 \begin{code}
        * primitive operations
 
 \begin{code}
+{-# OPTIONS_GHC -w #-}
+-- The above warning supression flag is a temporary kludge.
+-- While working on this module you are encouraged to remove it and fix
+-- any warnings in the module. See
+--     http://hackage.haskell.org/trac/ghc/wiki/WorkingConventions#Warnings
+-- for details
+
 module MkId (
        mkDictFunId, mkDefaultMethodId,
        mkDictSelId, 
 
        mkDataConIds,
        mkRecordSelId, 
 module MkId (
        mkDictFunId, mkDefaultMethodId,
        mkDictSelId, 
 
        mkDataConIds,
        mkRecordSelId, 
-       mkPrimOpId, mkFCallId,
+       mkPrimOpId, mkFCallId, mkTickBoxOpId, mkBreakPointOpId,
 
        mkReboxingAlt, wrapNewTypeBody, unwrapNewTypeBody,
 
        mkReboxingAlt, wrapNewTypeBody, unwrapNewTypeBody,
+        wrapFamInstBody, unwrapFamInstScrut,
         mkUnpackCase, mkProductBox,
 
        -- And some particular Ids; see below for why they are wired in
         mkUnpackCase, mkProductBox,
 
        -- And some particular Ids; see below for why they are wired in
@@ -38,69 +46,42 @@ module MkId (
 
 #include "HsVersions.h"
 
 
 #include "HsVersions.h"
 
-
-import BasicTypes      ( Arity, StrictnessMark(..), isMarkedUnboxed, isMarkedStrict )
-import Rules           ( mkSpecInfo )
-import TysPrim         ( openAlphaTyVars, alphaTyVar, alphaTy, 
-                         realWorldStatePrimTy, addrPrimTy
-                       )
-import TysWiredIn      ( charTy, mkListTy )
-import PrelRules       ( primOpRules )
-import Type            ( TyThing(..), mkForAllTy, tyVarsOfTypes, 
-                         newTyConInstRhs, mkTopTvSubst, substTyVar, substTy )
-import TcGadt           ( gadtRefine, refineType, emptyRefinement )
-import HsBinds          ( ExprCoFn(..), isIdCoercion )
-import Coercion         ( mkSymCoercion, mkUnsafeCoercion, isEqPred )
-import TcType          ( Type, ThetaType, mkDictTy, mkPredTys, mkPredTy, 
-                         mkTyConApp, mkTyVarTys, mkClassPred, isPredTy,
-                         mkFunTys, mkFunTy, mkSigmaTy, tcSplitSigmaTy, tcEqType,
-                         isUnLiftedType, mkForAllTys, mkTyVarTy, tyVarsOfType,
-                         tcSplitFunTys, tcSplitForAllTys, dataConsStupidTheta
-                       )
-import CoreUtils       ( exprType, dataConOrigInstPat, mkCoerce )
-import CoreUnfold      ( mkTopUnfolding, mkCompulsoryUnfolding )
-import Literal         ( nullAddrLit, mkStringLit )
-import TyCon           ( TyCon, isNewTyCon, tyConDataCons, FieldLabel,
-                          tyConStupidTheta, isProductTyCon, isDataTyCon,
-                          isRecursiveTyCon, tyConFamily_maybe, newTyConCo )
-import Class           ( Class, classTyCon, classSelIds )
-import Var             ( Id, TyVar, Var, setIdType )
-import VarSet          ( isEmptyVarSet, subVarSet, varSetElems )
-import Name            ( mkFCallName, mkWiredInName, Name, BuiltInSyntax(..))
-import OccName         ( mkOccNameFS, varName )
-import PrimOp          ( PrimOp, primOpSig, primOpOcc, primOpTag )
-import ForeignCall     ( ForeignCall )
-import DataCon         ( DataCon, DataConIds(..), dataConTyCon,
-                         dataConUnivTyVars, dataConInstTys,
-                         dataConFieldLabels, dataConRepArity, dataConResTys,
-                         dataConRepArgTys, dataConRepType, dataConFullSig,
-                         dataConStrictMarks, dataConExStricts, 
-                         splitProductType, isVanillaDataCon, dataConFieldType,
-                         deepSplitProductType, 
-                       )
-import Id              ( idType, mkGlobalId, mkVanillaGlobal, mkSysLocal, 
-                         mkTemplateLocals, mkTemplateLocalsNum, mkExportedLocalId,
-                         mkTemplateLocal, idName
-                       )
-import IdInfo          ( IdInfo, noCafIdInfo,  setUnfoldingInfo, 
-                         setArityInfo, setSpecInfo, setCafInfo,
-                         setAllStrictnessInfo, vanillaIdInfo,
-                         GlobalIdDetails(..), CafInfo(..)
-                       )
-import NewDemand       ( mkStrictSig, DmdResult(..),
-                         mkTopDmdType, topDmd, evalDmd, lazyDmd, retCPR,
-                         Demand(..), Demands(..) )
-import DmdAnal         ( dmdAnalTopRhs )
+import Rules
+import TysPrim
+import TysWiredIn
+import PrelRules
+import Type
+import TypeRep
+import TcGadt
+import Coercion
+import TcType
+import CoreUtils
+import CoreUnfold
+import Literal
+import TyCon
+import Class
+import VarSet
+import Name
+import OccName
+import PrimOp
+import ForeignCall
+import DataCon
+import Id
+import Var              ( Var, TyVar, mkCoVar)
+import IdInfo
+import NewDemand
+import DmdAnal
 import CoreSyn
 import CoreSyn
-import Unique          ( mkBuiltinUnique, mkPrimOpIdUnique )
-import Maybe           ( fromJust )
+import Unique
 import Maybes
 import PrelNames
 import Maybes
 import PrelNames
-import Util             ( dropList, isSingleton )
+import BasicTypes       hiding ( SuccessFlag(..) )
+import Util
 import Outputable
 import FastString
 import Outputable
 import FastString
-import ListSetOps      ( assoc, minusList )
-\end{code}             
+import ListSetOps
+import Module
+\end{code}
 
 %************************************************************************
 %*                                                                     *
 
 %************************************************************************
 %*                                                                     *
@@ -189,48 +170,69 @@ Notice that
   Making an explicit case expression allows the simplifier to eliminate
   it in the (common) case where the constructor arg is already evaluated.
 
   Making an explicit case expression allows the simplifier to eliminate
   it in the (common) case where the constructor arg is already evaluated.
 
+Note [Wrappers for data instance tycons]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+In the case of data instances, the wrapper also applies the coercion turning
+the representation type into the family instance type to cast the result of
+the wrapper.  For example, consider the declarations
+
+  data family Map k :: * -> *
+  data instance Map (a, b) v = MapPair (Map a (Pair b v))
+
+The tycon to which the datacon MapPair belongs gets a unique internal
+name of the form :R123Map, and we call it the representation tycon.
+In contrast, Map is the family tycon (accessible via
+tyConFamInst_maybe). A coercion allows you to move between
+representation and family type.  It is accessible from :R123Map via
+tyConFamilyCoercion_maybe and has kind
+
+  Co123Map a b v :: {Map (a, b) v :=: :R123Map a b v}
+
+The wrapper and worker of MapPair get the types
+
+       -- Wrapper
+  $WMapPair :: forall a b v. Map a (Map a b v) -> Map (a, b) v
+  $WMapPair a b v = MapPair a b v `cast` sym (Co123Map a b v)
+
+       -- Worker
+  MapPair :: forall a b v. Map a (Map a b v) -> :R123Map a b v
+
+This coercion is conditionally applied by wrapFamInstBody.
+
+It's a bit more complicated if the data instance is a GADT as well!
+
+   data instance T [a] where
+       T1 :: forall b. b -> T [Maybe b]
+Hence
+   Co7T a :: T [a] ~ :R7T a
+
+Now we want
+
+       -- Wrapper
+  $WT1 :: forall b. b -> T [Maybe b]
+  $WT1 b v = T1 (Maybe b) b (Maybe b) v
+                       `cast` sym (Co7T (Maybe b))
+
+       -- Worker
+  T1 :: forall c b. (c ~ Maybe b) => b -> :R7T c
 
 \begin{code}
 mkDataConIds :: Name -> Name -> DataCon -> DataConIds
 mkDataConIds wrap_name wkr_name data_con
 
 \begin{code}
 mkDataConIds :: Name -> Name -> DataCon -> DataConIds
 mkDataConIds wrap_name wkr_name data_con
-  | isNewTyCon tycon
-  = DCIds Nothing nt_work_id                    -- Newtype, only has a worker
+  | isNewTyCon tycon                   -- Newtype, only has a worker
+  = DCIds Nothing nt_work_id                 
 
 
-  | any isMarkedStrict all_strict_marks                -- Algebraic, needs wrapper
-    || not (null eq_spec)
-    || isInst
+  | any isMarkedStrict all_strict_marks             -- Algebraic, needs wrapper
+    || not (null eq_spec)                   -- NB: LoadIface.ifaceDeclSubBndrs
+    || isFamInstTyCon tycon                 --     depends on this test
   = DCIds (Just alg_wrap_id) wrk_id
 
   = DCIds (Just alg_wrap_id) wrk_id
 
-  | otherwise                                  -- Algebraic, no wrapper
+  | otherwise                               -- Algebraic, no wrapper
   = DCIds Nothing wrk_id
   where
     (univ_tvs, ex_tvs, eq_spec, 
   = DCIds Nothing wrk_id
   where
     (univ_tvs, ex_tvs, eq_spec, 
-     theta, orig_arg_tys)          = dataConFullSig data_con
-    tycon                          = dataConTyCon data_con
-    (isInst, instTys, familyTyCon) = 
-      case dataConInstTys data_con of
-        Nothing      -> (False, []     , familyTyCon)
-       Just instTys -> (True , instTys, familyTyCon)
-         where
-           familyTyCon = fromJust $ tyConFamily_maybe tycon
-                         -- this is defined whenever `isInst'
-
-       ----------- Wrapper --------------
-       -- We used to include the stupid theta in the wrapper's args
-       -- but now we don't.  Instead the type checker just injects these
-       -- extra constraints where necessary.
-    wrap_tvs = (univ_tvs `minusList` map fst eq_spec) ++ ex_tvs
-    subst         = mkTopTvSubst eq_spec
-    dict_tys       = mkPredTys theta
-    result_ty_args = map (substTyVar subst) univ_tvs
-    familyArgs     = map (substTy    subst) instTys
-    result_ty      = if isInst
-                    then mkTyConApp familyTyCon familyArgs  -- instance con
-                    else mkTyConApp tycon result_ty_args    -- ordinary con
-    wrap_ty        = mkForAllTys wrap_tvs $ mkFunTys dict_tys $
-                    mkFunTys orig_arg_tys $ result_ty
-       -- NB: watch out here if you allow user-written equality 
-       --     constraints in data constructor signatures
+     eq_theta, dict_theta, orig_arg_tys, res_ty) = dataConFullSig data_con
+    tycon = dataConTyCon data_con      -- The representation TyCon (not family)
 
        ----------- Worker (algebraic data types only) --------------
        -- The *worker* for the data constructor is the function that
 
        ----------- Worker (algebraic data types only) --------------
        -- The *worker* for the data constructor is the function that
@@ -246,6 +248,7 @@ mkDataConIds wrap_name wkr_name data_con
                                                        -- even if arity = 0
 
     wkr_sig = mkStrictSig (mkTopDmdType (replicate wkr_arity topDmd) cpr_info)
                                                        -- even if arity = 0
 
     wkr_sig = mkStrictSig (mkTopDmdType (replicate wkr_arity topDmd) cpr_info)
+       --      Note [Data-con worker strictness]
        -- Notice that we do *not* say the worker is strict
        -- even if the data constructor is declared strict
        --      e.g.    data T = MkT !(Int,Int)
        -- Notice that we do *not* say the worker is strict
        -- even if the data constructor is declared strict
        --      e.g.    data T = MkT !(Int,Int)
@@ -275,24 +278,44 @@ mkDataConIds wrap_name wkr_name data_con
     nt_work_info = noCafIdInfo         -- The NoCaf-ness is set by noCafIdInfo
                  `setArityInfo` 1      -- Arity 1
                  `setUnfoldingInfo`     newtype_unf
     nt_work_info = noCafIdInfo         -- The NoCaf-ness is set by noCafIdInfo
                  `setArityInfo` 1      -- Arity 1
                  `setUnfoldingInfo`     newtype_unf
-    newtype_unf  = ASSERT( isVanillaDataCon data_con &&
-                          isSingleton orig_arg_tys )
+    newtype_unf  = -- The assertion below is no longer correct:
+                  --   there may be a dict theta rather than a singleton orig_arg_ty
+                  -- ASSERT( isVanillaDataCon data_con &&
+                  --      isSingleton orig_arg_tys )
+                  --
                   -- No existentials on a newtype, but it can have a context
                   -- e.g.      newtype Eq a => T a = MkT (...)
                   mkCompulsoryUnfolding $ 
                   mkLams wrap_tvs $ Lam id_arg1 $ 
                   -- No existentials on a newtype, but it can have a context
                   -- e.g.      newtype Eq a => T a = MkT (...)
                   mkCompulsoryUnfolding $ 
                   mkLams wrap_tvs $ Lam id_arg1 $ 
-                  wrapNewTypeBody tycon result_ty_args
+                  wrapNewTypeBody tycon res_ty_args
                        (Var id_arg1)
 
                        (Var id_arg1)
 
-    id_arg1 = mkTemplateLocal 1 (head orig_arg_tys)
+    id_arg1 = mkTemplateLocal 1 
+               (if null orig_arg_tys
+                   then ASSERT(not (null $ dataConDictTheta data_con)) mkPredTy $ head (dataConDictTheta data_con)
+                   else head orig_arg_tys
+               )
+
+       ----------- Wrapper --------------
+       -- We used to include the stupid theta in the wrapper's args
+       -- but now we don't.  Instead the type checker just injects these
+       -- extra constraints where necessary.
+    wrap_tvs    = (univ_tvs `minusList` map fst eq_spec) ++ ex_tvs
+    res_ty_args        = substTyVars (mkTopTvSubst eq_spec) univ_tvs
+    eq_tys   = mkPredTys eq_theta
+    dict_tys = mkPredTys dict_theta
+    wrap_ty  = mkForAllTys wrap_tvs $ mkFunTys eq_tys $ mkFunTys dict_tys $
+              mkFunTys orig_arg_tys $ res_ty
+       -- NB: watch out here if you allow user-written equality 
+       --     constraints in data constructor signatures
 
        ----------- Wrappers for algebraic data types -------------- 
     alg_wrap_id = mkGlobalId (DataConWrapId data_con) wrap_name wrap_ty alg_wrap_info
     alg_wrap_info = noCafIdInfo                -- The NoCaf-ness is set by noCafIdInfo
 
        ----------- Wrappers for algebraic data types -------------- 
     alg_wrap_id = mkGlobalId (DataConWrapId data_con) wrap_name wrap_ty alg_wrap_info
     alg_wrap_info = noCafIdInfo                -- The NoCaf-ness is set by noCafIdInfo
-                   `setArityInfo`         alg_arity
+                   `setArityInfo`         wrap_arity
                        -- It's important to specify the arity, so that partial
                        -- applications are treated as values
                        -- It's important to specify the arity, so that partial
                        -- applications are treated as values
-                   `setUnfoldingInfo`     alg_unf
+                   `setUnfoldingInfo`     wrap_unf
                    `setAllStrictnessInfo` Just wrap_sig
 
     all_strict_marks = dataConExStricts data_con ++ dataConStrictMarks data_con
                    `setAllStrictnessInfo` Just wrap_sig
 
     all_strict_marks = dataConExStricts data_con ++ dataConStrictMarks data_con
@@ -309,21 +332,30 @@ mkDataConIds wrap_name wkr_name data_con
        --      ...(let w = C x in ...(w p q)...)...
        -- we want to see that w is strict in its two arguments
 
        --      ...(let w = C x in ...(w p q)...)...
        -- we want to see that w is strict in its two arguments
 
-    alg_unf = mkTopUnfolding $ Note InlineMe $
+    wrap_unf = mkTopUnfolding $ Note InlineMe $
              mkLams wrap_tvs $ 
              mkLams wrap_tvs $ 
+             mkLams eq_args $
              mkLams dict_args $ mkLams id_args $
              foldr mk_case con_app 
                    (zip (dict_args ++ id_args) all_strict_marks)
                    i3 []
 
              mkLams dict_args $ mkLams id_args $
              foldr mk_case con_app 
                    (zip (dict_args ++ id_args) all_strict_marks)
                    i3 []
 
-    con_app _ rep_ids = Var wrk_id `mkTyApps`  result_ty_args
-                                  `mkVarApps` ex_tvs
-                                  `mkTyApps`  map snd eq_spec
-                                  `mkVarApps` reverse rep_ids
+    con_app _ rep_ids = wrapFamInstBody tycon res_ty_args $
+                         Var wrk_id `mkTyApps`  res_ty_args
+                                    `mkVarApps` ex_tvs                 
+                                    `mkTyApps`  map snd eq_spec        -- Equality evidence 
+                                    `mkVarApps` eq_args
+                                    `mkVarApps` reverse rep_ids
 
     (dict_args,i2) = mkLocals 1  dict_tys
     (id_args,i3)   = mkLocals i2 orig_arg_tys
 
     (dict_args,i2) = mkLocals 1  dict_tys
     (id_args,i3)   = mkLocals i2 orig_arg_tys
-    alg_arity     = i3-1
+    wrap_arity    = i3-1
+    (eq_args,_)    = mkCoVarLocals i3 eq_tys
+
+    mkCoVarLocals i []     = ([],i)
+    mkCoVarLocals i (x:xs) = let (ys,j) = mkCoVarLocals (i+1) xs
+                                 y      = mkCoVar (mkSysTvName (mkBuiltinUnique i) FSLIT("dc_co")) x
+                             in (y:ys,j)
 
     mk_case 
           :: (Id, StrictnessMark)      -- Arg, strictness
 
     mk_case 
           :: (Id, StrictnessMark)      -- Arg, strictness
@@ -337,7 +369,7 @@ mkDataConIds wrap_name wkr_name data_con
                MarkedStrict 
                   | isUnLiftedType (idType arg) -> body i (arg:rep_args)
                   | otherwise ->
                MarkedStrict 
                   | isUnLiftedType (idType arg) -> body i (arg:rep_args)
                   | otherwise ->
-                       Case (Var arg) arg result_ty [(DEFAULT,[], body i (arg:rep_args))]
+                       Case (Var arg) arg res_ty [(DEFAULT,[], body i (arg:rep_args))]
 
                MarkedUnboxed
                   -> unboxProduct i (Var arg) (idType arg) the_body 
 
                MarkedUnboxed
                   -> unboxProduct i (Var arg) (idType arg) the_body 
@@ -438,23 +470,42 @@ Note the forall'd tyvars of the selector are just the free tyvars
 of the result type; there may be other tyvars in the constructor's
 type (e.g. 'b' in T2).
 
 of the result type; there may be other tyvars in the constructor's
 type (e.g. 'b' in T2).
 
-\begin{code}
+Note [Selector running example]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+It's OK to combine GADTs and type families.  Here's a running example:
+
+       data instance T [a] where 
+         T1 { fld :: b } :: T [Maybe b]
+
+The representation type looks like this
+       data :R7T a where
+         T1 { fld :: b } :: :R7T (Maybe b)
 
 
--- Steps for handling "naughty" vs "non-naughty" selectors:
---  1. Determine naughtiness by comparing field type vs result type
---  2. Install naughty ones with selector_ty of type _|_ and fill in mzero for info
---  3. If it's not naughty, do the normal plan.
+and there's coercion from the family type to the representation type
+       :CoR7T a :: T [a] ~ :R7T a
 
 
+The selector we want for fld looks like this:
+
+       fld :: forall b. T [Maybe b] -> b
+       fld = /\b. \(d::T [Maybe b]).
+             case d `cast` :CoR7T (Maybe b) of 
+               T1 (x::b) -> x
+
+The scrutinee of the case has type :R7T (Maybe b), which can be
+gotten by appying the eq_spec to the univ_tvs of the data con.
+
+\begin{code}
 mkRecordSelId :: TyCon -> FieldLabel -> Id
 mkRecordSelId tycon field_label
        -- Assumes that all fields with the same field label have the same type
   | is_naughty = naughty_id
   | otherwise  = sel_id
   where
 mkRecordSelId :: TyCon -> FieldLabel -> Id
 mkRecordSelId tycon field_label
        -- Assumes that all fields with the same field label have the same type
   | is_naughty = naughty_id
   | otherwise  = sel_id
   where
-    is_naughty = not (tyVarsOfType field_ty `subVarSet` res_tv_set)
-    sel_id_details = RecordSelId tycon field_label is_naughty
+    is_naughty = not (tyVarsOfType field_ty `subVarSet` data_tv_set)
+    sel_id_details = RecordSelId { sel_tycon = tycon, sel_label = field_label, sel_naughty = is_naughty }
+       -- For a data type family, the tycon is the *instance* TyCon
 
 
-    -- Escapist case here for naughty construcotrs
+    -- Escapist case here for naughty constructors
     -- We give it no IdInfo, and a type of forall a.a (never looked at)
     naughty_id = mkGlobalId sel_id_details field_label forall_a_a noCafIdInfo
     forall_a_a = mkForAllTy alphaTyVar (mkTyVarTy alphaTyVar)
     -- We give it no IdInfo, and a type of forall a.a (never looked at)
     naughty_id = mkGlobalId sel_id_details field_label forall_a_a noCafIdInfo
     forall_a_a = mkForAllTy alphaTyVar (mkTyVarTy alphaTyVar)
@@ -465,11 +516,12 @@ mkRecordSelId tycon field_label
     data_cons_w_field = filter has_field data_cons     -- Can't be empty!
     has_field con     = field_label `elem` dataConFieldLabels con
 
     data_cons_w_field = filter has_field data_cons     -- Can't be empty!
     has_field con     = field_label `elem` dataConFieldLabels con
 
-    con1       = head data_cons_w_field
-    res_tys    = dataConResTys con1
-    res_tv_set = tyVarsOfTypes res_tys
-    res_tvs    = varSetElems res_tv_set
-    data_ty    = mkTyConApp tycon res_tys
+    con1       = ASSERT( not (null data_cons_w_field) ) head data_cons_w_field
+    (univ_tvs, _, eq_spec, _, _, _, data_ty) = dataConFullSig con1
+       -- For a data type family, the data_ty (and hence selector_ty) mentions
+       -- only the family TyCon, not the instance TyCon
+    data_tv_set        = tyVarsOfType data_ty
+    data_tvs   = varSetElems data_tv_set
     field_ty   = dataConFieldType con1 field_label
     
        -- *Very* tiresomely, the selectors are (unnecessarily!) overloaded over
     field_ty   = dataConFieldType con1 field_label
     
        -- *Very* tiresomely, the selectors are (unnecessarily!) overloaded over
@@ -484,10 +536,9 @@ mkRecordSelId tycon field_label
     n_stupid_dicts  = length stupid_dict_tys
 
     (field_tyvars,pre_field_theta,field_tau) = tcSplitSigmaTy field_ty
     n_stupid_dicts  = length stupid_dict_tys
 
     (field_tyvars,pre_field_theta,field_tau) = tcSplitSigmaTy field_ty
-  
-    field_theta  = filter (not . isEqPred) pre_field_theta
-    field_dict_tys                      = mkPredTys field_theta
-    n_field_dict_tys                    = length field_dict_tys
+    field_theta       = filter (not . isEqPred) pre_field_theta
+    field_dict_tys    = mkPredTys field_theta
+    n_field_dict_tys  = length field_dict_tys
        -- If the field has a universally quantified type we have to 
        -- be a bit careful.  Suppose we have
        --      data R = R { op :: forall a. Foo a => a -> a }
        -- If the field has a universally quantified type we have to 
        -- be a bit careful.  Suppose we have
        --      data R = R { op :: forall a. Foo a => a -> a }
@@ -504,7 +555,7 @@ mkRecordSelId tycon field_label
        --      op (R op) = op
 
     selector_ty :: Type
        --      op (R op) = op
 
     selector_ty :: Type
-    selector_ty  = mkForAllTys res_tvs $ mkForAllTys field_tyvars $
+    selector_ty  = mkForAllTys data_tvs $ mkForAllTys field_tyvars $
                   mkFunTys stupid_dict_tys  $  mkFunTys field_dict_tys $
                   mkFunTy data_ty field_tau
       
                   mkFunTys stupid_dict_tys  $  mkFunTys field_dict_tys $
                   mkFunTy data_ty field_tau
       
@@ -531,7 +582,8 @@ mkRecordSelId tycon field_label
     field_dict_ids   = mkTemplateLocalsNum field_dict_base field_dict_tys
     dict_id_base     = field_dict_base + n_field_dict_tys
     data_id         = mkTemplateLocal dict_id_base data_ty
     field_dict_ids   = mkTemplateLocalsNum field_dict_base field_dict_tys
     dict_id_base     = field_dict_base + n_field_dict_tys
     data_id         = mkTemplateLocal dict_id_base data_ty
-    arg_base        = dict_id_base + 1
+    scrut_id        = mkTemplateLocal (dict_id_base+1) scrut_ty
+    arg_base        = dict_id_base + 2
 
     the_alts :: [CoreAlt]
     the_alts   = map mk_alt data_cons_w_field  -- Already sorted by data-con
 
     the_alts :: [CoreAlt]
     the_alts   = map mk_alt data_cons_w_field  -- Already sorted by data-con
@@ -544,14 +596,19 @@ mkRecordSelId tycon field_label
     caf_info    | no_default = NoCafRefs
                | otherwise  = MayHaveCafRefs
 
     caf_info    | no_default = NoCafRefs
                | otherwise  = MayHaveCafRefs
 
-    sel_rhs = mkLams res_tvs $ mkLams field_tyvars $ 
+    sel_rhs = mkLams data_tvs $ mkLams field_tyvars $ 
              mkLams stupid_dict_ids $ mkLams field_dict_ids $
              mkLams stupid_dict_ids $ mkLams field_dict_ids $
-             Lam data_id     $ mk_result sel_body
+             Lam data_id $ mk_result sel_body
+
+    scrut_ty_args = substTyVars (mkTopTvSubst eq_spec) univ_tvs
+    scrut_ty     = mkTyConApp tycon scrut_ty_args
+    scrut = unwrapFamInstScrut tycon scrut_ty_args (Var data_id)
+       -- First coerce from the type family to the representation type
 
        -- NB: A newtype always has a vanilla DataCon; no existentials etc
 
        -- NB: A newtype always has a vanilla DataCon; no existentials etc
-       --     res_tys will simply be the dataConUnivTyVars
-    sel_body | isNewTyCon tycon = unwrapNewTypeBody tycon res_tys (Var data_id)
-            | otherwise        = Case (Var data_id) data_id field_ty (default_alt ++ the_alts)
+       --     data_tys will simply be the dataConUnivTyVars
+    sel_body | isNewTyCon tycon = unwrapNewTypeBody tycon scrut_ty_args scrut
+            | otherwise        = Case scrut scrut_id field_ty (default_alt ++ the_alts)
 
     mk_result poly_result = mkVarApps (mkVarApps poly_result field_tyvars) field_dict_ids
        -- We pull the field lambdas to the top, so we need to 
 
     mk_result poly_result = mkVarApps (mkVarApps poly_result field_tyvars) field_dict_ids
        -- We pull the field lambdas to the top, so we need to 
@@ -562,12 +619,12 @@ mkRecordSelId tycon field_label
        --      foo = /\a. \t:T. case t of { MkT f -> f a }
 
     mk_alt data_con 
        --      foo = /\a. \t:T. case t of { MkT f -> f a }
 
     mk_alt data_con 
-      =   ASSERT2( res_ty `tcEqType` field_ty, ppr data_con $$ ppr res_ty $$ ppr field_ty )
+      =   ASSERT2( data_ty `tcEqType` field_ty, ppr data_con $$ ppr data_ty $$ ppr field_ty )
          mkReboxingAlt rebox_uniqs data_con (ex_tvs ++ co_tvs ++ arg_vs) rhs
       where
            -- get pattern binders with types appropriately instantiated
        arg_uniqs = map mkBuiltinUnique [arg_base..]
          mkReboxingAlt rebox_uniqs data_con (ex_tvs ++ co_tvs ++ arg_vs) rhs
       where
            -- get pattern binders with types appropriately instantiated
        arg_uniqs = map mkBuiltinUnique [arg_base..]
-        (ex_tvs, co_tvs, arg_vs) = dataConOrigInstPat arg_uniqs data_con res_tys
+        (ex_tvs, co_tvs, arg_vs) = dataConOrigInstPat arg_uniqs data_con scrut_ty_args
 
        rebox_base  = arg_base + length ex_tvs + length co_tvs + length arg_vs
        rebox_uniqs = map mkBuiltinUnique [rebox_base..]
 
        rebox_base  = arg_base + length ex_tvs + length co_tvs + length arg_vs
        rebox_uniqs = map mkBuiltinUnique [rebox_base..]
@@ -579,14 +636,14 @@ mkRecordSelId tycon field_label
        --              T1 b' (c : [b]=[b']) (x:Maybe b') 
        --                      -> x `cast` Maybe (sym (right c))
 
        --              T1 b' (c : [b]=[b']) (x:Maybe b') 
        --                      -> x `cast` Maybe (sym (right c))
 
-        Succeeded refinement = gadtRefine emptyRefinement ex_tvs co_tvs
-        (co_fn, res_ty) = refineType refinement (idType the_arg_id)
+
                -- Generate the refinement for b'=b, 
                -- and apply to (Maybe b'), to get (Maybe b)
                -- Generate the refinement for b'=b, 
                -- and apply to (Maybe b'), to get (Maybe b)
-
-        rhs = case co_fn of
-               ExprCoFn co -> Cast (Var the_arg_id) co
-               id_co       -> ASSERT(isIdCoercion id_co) Var the_arg_id
+        Succeeded refinement = gadtRefine emptyRefinement ex_tvs co_tvs
+       the_arg_id_ty = idType the_arg_id
+        (rhs, data_ty) = case refineType refinement the_arg_id_ty of
+                         Just (co, data_ty) -> (Cast (Var the_arg_id) co, data_ty)
+                         Nothing            -> (Var the_arg_id, the_arg_id_ty)
 
        field_vs    = filter (not . isPredTy . idType) arg_vs 
        the_arg_id  = assoc "mkRecordSelId:mk_alt" (field_lbls `zip` field_vs) field_label
 
        field_vs    = filter (not . isPredTy . idType) arg_vs 
        the_arg_id  = assoc "mkRecordSelId:mk_alt" (field_lbls `zip` field_vs) field_label
@@ -759,7 +816,7 @@ mkDictSelId name clas
        --      C a -> C a
        -- for a single-op class (after all, the selector is the identity)
        -- But it's type must expose the representation of the dictionary
        --      C a -> C a
        -- for a single-op class (after all, the selector is the identity)
        -- But it's type must expose the representation of the dictionary
-       -- to gat (say)         C a -> (a -> a)
+       -- to get (say)         C a -> (a -> a)
 
     info = noCafIdInfo
                `setArityInfo`          1
 
     info = noCafIdInfo
                `setArityInfo`          1
@@ -781,17 +838,34 @@ mkDictSelId name clas
     tycon      = classTyCon clas
     [data_con] = tyConDataCons tycon
     tyvars     = dataConUnivTyVars data_con
     tycon      = classTyCon clas
     [data_con] = tyConDataCons tycon
     tyvars     = dataConUnivTyVars data_con
-    arg_tys    = ASSERT( isVanillaDataCon data_con ) dataConRepArgTys data_con
+    arg_tys    = {- ASSERT( isVanillaDataCon data_con ) -} dataConRepArgTys data_con
+    eq_theta   = dataConEqTheta        data_con
     the_arg_id = assoc "MkId.mkDictSelId" (map idName (classSelIds clas) `zip` arg_ids) name
 
     the_arg_id = assoc "MkId.mkDictSelId" (map idName (classSelIds clas) `zip` arg_ids) name
 
-    pred             = mkClassPred clas (mkTyVarTys tyvars)
-    (dict_id:arg_ids) = mkTemplateLocals (mkPredTy pred : arg_tys)
+    pred       = mkClassPred clas (mkTyVarTys tyvars)
+    dict_id    = mkTemplateLocal     1 $ mkPredTy pred
+    (eq_ids,n) = mkCoVarLocals 2 $ mkPredTys eq_theta
+    arg_ids    = mkTemplateLocalsNum n arg_tys
 
 
-    rhs = mkLams tyvars (Lam dict_id rhs_body)
+    mkCoVarLocals i []     = ([],i)
+    mkCoVarLocals i (x:xs) = let (ys,j) = mkCoVarLocals (i+1) xs
+                                 y      = mkCoVar (mkSysTvName (mkBuiltinUnique i) FSLIT("dc_co")) x
+                             in (y:ys,j)
+
+    rhs = mkLams tyvars  (Lam dict_id   rhs_body)
     rhs_body | isNewTyCon tycon = unwrapNewTypeBody tycon (map mkTyVarTy tyvars) (Var dict_id)
             | otherwise        = Case (Var dict_id) dict_id (idType the_arg_id)
     rhs_body | isNewTyCon tycon = unwrapNewTypeBody tycon (map mkTyVarTy tyvars) (Var dict_id)
             | otherwise        = Case (Var dict_id) dict_id (idType the_arg_id)
-                                      [(DataAlt data_con, arg_ids, Var the_arg_id)]
+                                      [(DataAlt data_con, eq_ids ++ arg_ids, Var the_arg_id)]
+\end{code}
+
+
+%************************************************************************
+%*                                                                     *
+       Wrapping and unwrapping newtypes and type families
+%*                                                                     *
+%************************************************************************
 
 
+\begin{code}
 wrapNewTypeBody :: TyCon -> [Type] -> CoreExpr -> CoreExpr
 -- The wrapper for the data constructor for a newtype looks like this:
 --     newtype T a = MkT (a,Int)
 wrapNewTypeBody :: TyCon -> [Type] -> CoreExpr -> CoreExpr
 -- The wrapper for the data constructor for a newtype looks like this:
 --     newtype T a = MkT (a,Int)
@@ -803,23 +877,53 @@ wrapNewTypeBody :: TyCon -> [Type] -> CoreExpr -> CoreExpr
 -- body of the wrapper, namely
 --     e `cast` (CoT [a])
 --
 -- body of the wrapper, namely
 --     e `cast` (CoT [a])
 --
--- If a coercion constructor is prodivided in the newtype, then we use
+-- If a coercion constructor is provided in the newtype, then we use
 -- it, otherwise the wrap/unwrap are both no-ops 
 --
 -- it, otherwise the wrap/unwrap are both no-ops 
 --
+-- If the we are dealing with a newtype *instance*, we have a second coercion
+-- identifying the family instance with the constructor of the newtype
+-- instance.  This coercion is applied in any case (ie, composed with the
+-- coercion constructor of the newtype or applied by itself).
+
 wrapNewTypeBody tycon args result_expr
 wrapNewTypeBody tycon args result_expr
-  | Just co_con <- newTyConCo tycon
-  = mkCoerce (mkSymCoercion (mkTyConApp co_con args)) result_expr
-  | otherwise
-  = result_expr
+  = wrapFamInstBody tycon args inner
+  where
+    inner
+      | Just co_con <- newTyConCo_maybe tycon
+      = mkCoerce (mkSymCoercion (mkTyConApp co_con args)) result_expr
+      | otherwise
+      = result_expr
+
+-- When unwrapping, we do *not* apply any family coercion, because this will
+-- be done via a CoPat by the type checker.  We have to do it this way as
+-- computing the right type arguments for the coercion requires more than just
+-- a spliting operation (cf, TcPat.tcConPat).
 
 unwrapNewTypeBody :: TyCon -> [Type] -> CoreExpr -> CoreExpr
 unwrapNewTypeBody tycon args result_expr
 
 unwrapNewTypeBody :: TyCon -> [Type] -> CoreExpr -> CoreExpr
 unwrapNewTypeBody tycon args result_expr
-  | Just co_con <- newTyConCo tycon
+  | Just co_con <- newTyConCo_maybe tycon
   = mkCoerce (mkTyConApp co_con args) result_expr
   | otherwise
   = result_expr
 
   = mkCoerce (mkTyConApp co_con args) result_expr
   | otherwise
   = result_expr
 
+-- If the type constructor is a representation type of a data instance, wrap
+-- the expression into a cast adjusting the expression type, which is an
+-- instance of the representation type, to the corresponding instance of the
+-- family instance type.
+-- See Note [Wrappers for data instance tycons]
+wrapFamInstBody :: TyCon -> [Type] -> CoreExpr -> CoreExpr
+wrapFamInstBody tycon args body
+  | Just co_con <- tyConFamilyCoercion_maybe tycon
+  = mkCoerce (mkSymCoercion (mkTyConApp co_con args)) body
+  | otherwise
+  = body
 
 
+unwrapFamInstScrut :: TyCon -> [Type] -> CoreExpr -> CoreExpr
+unwrapFamInstScrut tycon args scrut
+  | Just co_con <- tyConFamilyCoercion_maybe tycon
+  = mkCoerce (mkTyConApp co_con args) scrut
+  | otherwise
+  = scrut
 \end{code}
 
 
 \end{code}
 
 
@@ -838,7 +942,7 @@ mkPrimOpId prim_op
     ty   = mkForAllTys tyvars (mkFunTys arg_tys res_ty)
     name = mkWiredInName gHC_PRIM (primOpOcc prim_op) 
                         (mkPrimOpIdUnique (primOpTag prim_op))
     ty   = mkForAllTys tyvars (mkFunTys arg_tys res_ty)
     name = mkWiredInName gHC_PRIM (primOpOcc prim_op) 
                         (mkPrimOpIdUnique (primOpTag prim_op))
-                        Nothing (AnId id) UserSyntax
+                        (AnId id) UserSyntax
     id   = mkGlobalId (PrimOpId prim_op) name ty info
                
     info = noCafIdInfo
     id   = mkGlobalId (PrimOpId prim_op) name ty info
                
     info = noCafIdInfo
@@ -876,6 +980,29 @@ mkFCallId uniq fcall ty
     (arg_tys, _) = tcSplitFunTys tau
     arity       = length arg_tys
     strict_sig   = mkStrictSig (mkTopDmdType (replicate arity evalDmd) TopRes)
     (arg_tys, _) = tcSplitFunTys tau
     arity       = length arg_tys
     strict_sig   = mkStrictSig (mkTopDmdType (replicate arity evalDmd) TopRes)
+
+-- Tick boxes and breakpoints are both represented as TickBoxOpIds,
+-- except for the type:
+--
+--    a plain HPC tick box has type (State# RealWorld)
+--    a breakpoint Id has type forall a.a
+--
+-- The breakpoint Id will be applied to a list of arbitrary free variables,
+-- which is why it needs a polymorphic type.
+
+mkTickBoxOpId :: Unique -> Module -> TickBoxId -> Id
+mkTickBoxOpId uniq mod ix = mkTickBox' uniq mod ix realWorldStatePrimTy
+
+mkBreakPointOpId :: Unique -> Module -> TickBoxId -> Id
+mkBreakPointOpId uniq mod ix = mkTickBox' uniq mod ix ty
+ where ty = mkSigmaTy [openAlphaTyVar] [] openAlphaTy
+
+mkTickBox' uniq mod ix ty = mkGlobalId (TickBoxOpId tickbox) name ty info    
+  where
+    tickbox = TickBox mod ix
+    occ_str = showSDoc (braces (ppr tickbox))
+    name    = mkTickBoxOpName uniq occ_str
+    info    = noCafIdInfo
 \end{code}
 
 
 \end{code}
 
 
@@ -977,7 +1104,7 @@ another gun with which to shoot yourself in the foot.
 
 \begin{code}
 mkWiredInIdName mod fs uniq id
 
 \begin{code}
 mkWiredInIdName mod fs uniq id
- = mkWiredInName mod (mkOccNameFS varName fs) uniq Nothing (AnId id) UserSyntax
+ = mkWiredInName mod (mkOccNameFS varName fs) uniq (AnId id) UserSyntax
 
 unsafeCoerceName = mkWiredInIdName gHC_PRIM FSLIT("unsafeCoerce#") unsafeCoerceIdKey  unsafeCoerceId
 nullAddrName     = mkWiredInIdName gHC_PRIM FSLIT("nullAddr#")    nullAddrIdKey      nullAddrId
 
 unsafeCoerceName = mkWiredInIdName gHC_PRIM FSLIT("unsafeCoerce#") unsafeCoerceIdKey  unsafeCoerceId
 nullAddrName     = mkWiredInIdName gHC_PRIM FSLIT("nullAddr#")    nullAddrIdKey      nullAddrId
@@ -1010,8 +1137,7 @@ unsafeCoerceId
                      (mkFunTy openAlphaTy openBetaTy)
     [x] = mkTemplateLocals [openAlphaTy]
     rhs = mkLams [openAlphaTyVar,openBetaTyVar,x] $
                      (mkFunTy openAlphaTy openBetaTy)
     [x] = mkTemplateLocals [openAlphaTy]
     rhs = mkLams [openAlphaTyVar,openBetaTyVar,x] $
---       Note (Coerce openBetaTy openAlphaTy) (Var x)
-         Cast (Var x) (mkUnsafeCoercion openAlphaTy openBetaTy)
+          Cast (Var x) (mkUnsafeCoercion openAlphaTy openBetaTy)
 
 -- nullAddr# :: Addr#
 -- The reason is is here is because we don't provide 
 
 -- nullAddr# :: Addr#
 -- The reason is is here is because we don't provide 
@@ -1040,7 +1166,7 @@ seqId
 -- not from GHC.Base.hi.   This is important, because the strictness
 -- analyser will spot it as strict!
 --
 -- not from GHC.Base.hi.   This is important, because the strictness
 -- analyser will spot it as strict!
 --
--- Also no unfolding in lazyId: it gets "inlined" by a HACK in the worker/wrapper pass
+-- Also no unfolding in lazyId: it gets "inlined" by a HACK in the worker/wrapperpass
 --     (see WorkWrap.wwExpr)   
 -- We could use inline phases to do this, but that would be vulnerable to changes in 
 -- phase numbering....we must inline precisely after strictness analysis.
 --     (see WorkWrap.wwExpr)   
 -- We could use inline phases to do this, but that would be vulnerable to changes in 
 -- phase numbering....we must inline precisely after strictness analysis.
@@ -1169,9 +1295,5 @@ pc_bottoming_Id name ty
 
     strict_sig    = mkStrictSig (mkTopDmdType [evalDmd] BotRes)
        -- These "bottom" out, no matter what their arguments
 
     strict_sig    = mkStrictSig (mkTopDmdType [evalDmd] BotRes)
        -- These "bottom" out, no matter what their arguments
-
-(openAlphaTyVar:openBetaTyVar:_) = openAlphaTyVars
-openAlphaTy  = mkTyVarTy openAlphaTyVar
-openBetaTy   = mkTyVarTy openBetaTyVar
 \end{code}
 
 \end{code}