[project @ 1996-05-01 18:36:59 by partain]
[ghc-hetmet.git] / ghc / compiler / deSugar / DsUtils.lhs
index e6b80f2..411a7c1 100644 (file)
@@ -43,14 +43,22 @@ import PprStyle             ( PprStyle(..) )
 import PrelInfo                ( stringTy, iRREFUT_PAT_ERROR_ID )
 import Pretty          ( ppShow )
 import Id              ( idType, dataConArgTys, mkTupleCon,
+                         pprId{-ToDo:rm-},
                          DataCon(..), DictVar(..), Id(..), GenId )
 import Literal         ( Literal(..) )
-import TyCon           ( mkTupleTyCon )
+import TyCon           ( mkTupleTyCon, isNewTyCon, tyConDataCons )
 import Type            ( mkTyVarTys, mkRhoTy, mkForAllTys, mkFunTys,
-                         isUnboxedType, applyTyCon, getAppDataTyCon
+                         isUnboxedType, applyTyCon,
+                         getAppDataTyCon, getAppTyCon
                        )
 import UniqSet         ( mkUniqSet, minusUniqSet, uniqSetToList, UniqSet(..) )
-import Util            ( panic, assertPanic )
+import Util            ( panic, assertPanic, pprTrace{-ToDo:rm-} )
+import PprCore{-ToDo:rm-}
+--import PprType--ToDo:rm
+import Pretty--ToDo:rm
+import TyVar--ToDo:rm
+import Unique--ToDo:rm
+import Usage--ToDo:rm
 
 splitDictType = panic "DsUtils.splitDictType"
 \end{code}
@@ -129,7 +137,13 @@ mkCoPrimCaseMatchResult var alts
 mkCoAlgCaseMatchResult :: Id                           -- Scrutinee
                    -> [(DataCon, [Id], MatchResult)]   -- Alternatives
                    -> DsM MatchResult
+
 mkCoAlgCaseMatchResult var alts
+  | isNewTyCon tycon           -- newtype case; use a let
+  = ASSERT( newtype_sanity )
+    returnDs (mkCoLetsMatchResult [coercion_bind] match_result)
+
+  | otherwise                  -- datatype case  
   =        -- Find all the constructors in the type which aren't
            -- explicitly mentioned in the alternatives:
     case un_mentioned_constructors of
@@ -163,8 +177,21 @@ mkCoAlgCaseMatchResult var alts
                                      (mk_case alts (\fail_expr -> BindDefault wild fail_expr))
                                      cxt1)
   where
+       -- Common stuff
     scrut_ty = idType var
-    (tycon, tycon_arg_tys, data_cons) = getAppDataTyCon scrut_ty
+    (tycon, tycon_arg_tys) = --pprTrace "CoAlgCase:" (pprType PprDebug scrut_ty) $ 
+                            getAppTyCon scrut_ty
+
+       -- Stuff for newtype
+    (con_id, arg_ids, match_result) = head alts
+    arg_id                         = head arg_ids
+    coercion_bind                  = NonRec arg_id (Coerce (CoerceOut con_id) 
+                                                           (idType arg_id)
+                                                           (Var var))
+    newtype_sanity                 = null (tail alts) && null (tail arg_ids)
+
+       -- Stuff for data types
+    data_cons = tyConDataCons tycon
 
     un_mentioned_constructors
       = uniqSetToList (mkUniqSet data_cons `minusUniqSet` mkUniqSet [ con | (con, _, _) <- alts] )
@@ -395,7 +422,9 @@ The general case:
 
 \begin{code}
 mkTupleBind tyvars dicts local_global_prs tuple_expr
-  = newSysLocalDs tuple_var_ty `thenDs` \ tuple_var ->
+  = --pprTrace "mkTupleBind:\n" (ppAboves [ppCat (map (pprId PprShowAll) locals), ppCat (map (pprId PprShowAll) globals), {-ppr PprDebug local_tuple, pprType PprDebug res_ty,-} ppr PprDebug tuple_expr]) $
+
+    newSysLocalDs tuple_var_ty `thenDs` \ tuple_var ->
 
     zipWithDs (mk_selector (Var tuple_var))
              local_global_prs