X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2FdeSugar%2FMatchCon.lhs;h=e828999c8af4a780fca1012ef9c64f97b15eb0e0;hb=438596897ebbe25a07e1c82085cfbc5bdb00f09e;hp=7abf1850c16960dd9f194ef342d63fb17b1cd9b1;hpb=b54a4ee4cf8512acedb75d6f54e1074303e42b71;p=ghc-hetmet.git diff --git a/ghc/compiler/deSugar/MatchCon.lhs b/ghc/compiler/deSugar/MatchCon.lhs index 7abf185..e828999 100644 --- a/ghc/compiler/deSugar/MatchCon.lhs +++ b/ghc/compiler/deSugar/MatchCon.lhs @@ -1,24 +1,26 @@ % -% (c) The GRASP/AQUA Project, Glasgow University, 1992-1996 +% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998 % \section[MatchCon]{Pattern-matching constructors} \begin{code} -#include "HsVersions.h" - module MatchCon ( matchConFamily ) where -IMP_Ubiq() -IMPORT_DELOOPER(DsLoop) ( match ) -- break match-ish loop +#include "HsVersions.h" + +import {-# SOURCE #-} Match ( match ) -import HsSyn ( OutPat(..), HsLit, HsExpr ) -import DsHsSyn ( outPatType ) +import HsSyn ( OutPat(..) ) import DsMonad import DsUtils -import Id ( GenId{-instances-}, SYN_IE(Id) ) -import Util ( panic, assertPanic ) +import Id ( Id ) +import CoreSyn +import Type ( mkTyVarTys ) +import Unique ( Uniquable(..), Unique ) +import UniqFM -- Until equivClassesUniq moves to Util +import Outputable \end{code} We are confronted with the first column of patterns in a set of @@ -74,60 +76,66 @@ have-we-used-all-the-constructors? question; the local function \begin{code} matchConFamily :: [Id] -> [EquationInfo] - -> [EquationInfo] -- Shadows -> DsM MatchResult -matchConFamily (var:vars) eqns_info shadows - = match_cons_used vars eqns_info shadows `thenDs` \ alts -> - mkCoAlgCaseMatchResult var alts +matchConFamily (var:vars) eqns_info + = let + -- Sort into equivalence classes by the unique on the constructor + -- All the EqnInfos should start with a ConPat + eqn_groups = equivClassesByUniq get_uniq eqns_info + get_uniq (EqnInfo _ _ (ConPat data_con _ _ _ _ : _) _) = getUnique data_con + in + -- Now make a case alternative out of each group + mapDs (match_con vars) eqn_groups `thenDs` \ alts -> + + returnDs (mkCoAlgCaseMatchResult var alts) \end{code} And here is the local function that does all the work. It is more-or-less the @matchCon@/@matchClause@ functions on page~94 in Wadler's chapter in SLPJ. -\begin{code} -match_cons_used _ [{- no more eqns -}] _ = returnDs [] - -match_cons_used vars eqns_info@(EqnInfo (ConPat data_con _ arg_pats : ps1) _ : eqns) shadows - = let - (eqns_for_this_con, eqns_not_for_this_con) = splitByCon eqns_info - (shadows_for_this_con, shadows_not_for_this_con) = splitByCon shadows - in - -- Go ahead and do the recursive call to make the alts - -- for the other ConPats in this con family... - match_cons_used vars eqns_not_for_this_con shadows_not_for_this_con `thenDs` \ rest_of_alts -> - -- Make new vars for the con arguments; avoid new locals where possible - selectMatchVars arg_pats `thenDs` \ new_vars -> +\begin{code} +match_con vars all_eqns@(EqnInfo n ctx (ConPat data_con _ ex_tvs ex_dicts arg_pats : pats1) match_result1 : other_eqns) + = -- Make new vars for the con arguments; avoid new locals where possible + mapDs selectMatchVar arg_pats `thenDs` \ arg_vars -> -- Now do the business to make the alt for _this_ ConPat ... - match (new_vars++vars) - (map shift_con_pat eqns_for_this_con) - (map shift_con_pat shadows_for_this_con) `thenDs` \ match_result -> - - returnDs ( - (data_con, new_vars, match_result) - : rest_of_alts - ) + match (ex_dicts ++ arg_vars ++ vars) + (map shift_con_pat all_eqns) `thenDs` \ match_result -> + + -- Substitute over the result + let + match_result' | null ex_tvs = match_result + | otherwise = adjustMatchResult subst_it match_result + in + returnDs (data_con, ex_tvs ++ ex_dicts ++ arg_vars, match_result') where - splitByCon :: [EquationInfo] -> ([EquationInfo], [EquationInfo]) - splitByCon [] = ([],[]) - splitByCon (info@(EqnInfo (pat : _) _) : rest) - = case pat of - ConPat n _ _ | n == data_con -> (info:rest_yes, rest_no) - WildPat _ -> (info:rest_yes, info:rest_no) - -- WildPats will be in the shadows only, - -- and they go into both groups - other_pat -> (rest_yes, info:rest_no) - where - (rest_yes, rest_no) = splitByCon rest - shift_con_pat :: EquationInfo -> EquationInfo - shift_con_pat (EqnInfo (ConPat _ _ pats': pats) match_result) - = EqnInfo (pats' ++ pats) match_result - shift_con_pat (EqnInfo (WildPat _: pats) match_result) -- Will only happen in shadow - = EqnInfo ([WildPat (outPatType arg_pat) | arg_pat <- arg_pats] ++ pats) match_result - shift_con_pat other = panic "matchConFamily:match_cons_used:shift_con_pat" + shift_con_pat (EqnInfo n ctx (ConPat _ _ ex_tvs' ex_dicts' arg_pats: pats) match_result) + = EqnInfo n ctx (new_pats ++ pats) match_result + where + new_pats = map VarPat ex_dicts' ++ arg_pats + + -- We 'substitute' by going: (/\ tvs' -> e) tvs + subst_it e = foldr subst_one e other_eqns + subst_one (EqnInfo _ _ (ConPat _ _ ex_tvs' _ _ : _) _) e = mkTyApps (mkLams ex_tvs' e) ex_tys + ex_tys = mkTyVarTys ex_tvs + + +-- Belongs in Util.lhs +equivClassesByUniq :: (a -> Unique) -> [a] -> [[a]] + -- NB: it's *very* important that if we have the input list [a,b,c], + -- where a,b,c all have the same unique, then we get back the list + -- [a,b,c] + -- not + -- [c,b,a] + -- Hence the use of foldr, plus the reversed-args tack_on below +equivClassesByUniq get_uniq xs + = eltsUFM (foldr add emptyUFM xs) + where + add a ufm = addToUFM_C tack_on ufm (get_uniq a) [a] + tack_on old new = new++old \end{code} Note on @shift_con_pats@ just above: does what the list comprehension in