[project @ 2001-06-25 08:09:57 by simonpj]
[ghc-hetmet.git] / ghc / compiler / deSugar / MatchCon.lhs
1
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[MatchCon]{Pattern-matching constructors}
5
6 \begin{code}
7 module MatchCon ( matchConFamily ) where
8
9 #include "HsVersions.h"
10
11 import {-# SOURCE #-} Match     ( match )
12
13 import HsSyn            ( OutPat(..) )
14
15 import DsMonad
16 import DsUtils
17
18 import Id               ( Id )
19 import CoreSyn
20 import TcType           ( mkTyVarTys )
21 import ListSetOps       ( equivClassesByUniq )
22 import Unique           ( Uniquable(..) )
23 \end{code}
24
25 We are confronted with the first column of patterns in a set of
26 equations, all beginning with constructors from one ``family'' (e.g.,
27 @[]@ and @:@ make up the @List@ ``family'').  We want to generate the
28 alternatives for a @Case@ expression.  There are several choices:
29 \begin{enumerate}
30 \item
31 Generate an alternative for every constructor in the family, whether
32 they are used in this set of equations or not; this is what the Wadler
33 chapter does.
34 \begin{description}
35 \item[Advantages:]
36 (a)~Simple.  (b)~It may also be that large sparsely-used constructor
37 families are mainly handled by the code for literals.
38 \item[Disadvantages:]
39 (a)~Not practical for large sparsely-used constructor families, e.g.,
40 the ASCII character set.  (b)~Have to look up a list of what
41 constructors make up the whole family.
42 \end{description}
43
44 \item
45 Generate an alternative for each constructor used, then add a default
46 alternative in case some constructors in the family weren't used.
47 \begin{description}
48 \item[Advantages:]
49 (a)~Alternatives aren't generated for unused constructors.  (b)~The
50 STG is quite happy with defaults.  (c)~No lookup in an environment needed.
51 \item[Disadvantages:]
52 (a)~A spurious default alternative may be generated.
53 \end{description}
54
55 \item
56 ``Do it right:'' generate an alternative for each constructor used,
57 and add a default alternative if all constructors in the family
58 weren't used.
59 \begin{description}
60 \item[Advantages:]
61 (a)~You will get cases with only one alternative (and no default),
62 which should be amenable to optimisation.  Tuples are a common example.
63 \item[Disadvantages:]
64 (b)~Have to look up constructor families in TDE (as above).
65 \end{description}
66 \end{enumerate}
67
68 We are implementing the ``do-it-right'' option for now.  The arguments
69 to @matchConFamily@ are the same as to @match@; the extra @Int@
70 returned is the number of constructors in the family.
71
72 The function @matchConFamily@ is concerned with this
73 have-we-used-all-the-constructors? question; the local function
74 @match_cons_used@ does all the real work.
75 \begin{code}
76 matchConFamily :: [Id]
77                -> [EquationInfo]
78                -> DsM MatchResult
79
80 matchConFamily (var:vars) eqns_info
81   = let
82         -- Sort into equivalence classes by the unique on the constructor
83         -- All the EqnInfos should start with a ConPat
84         eqn_groups = equivClassesByUniq get_uniq eqns_info
85         get_uniq (EqnInfo _ _ (ConPat data_con _ _ _ _ : _) _) = getUnique data_con
86     in
87         -- Now make a case alternative out of each group
88     mapDs (match_con vars) eqn_groups   `thenDs` \ alts ->
89
90     returnDs (mkCoAlgCaseMatchResult var alts)
91 \end{code}
92
93 And here is the local function that does all the work.  It is
94 more-or-less the @matchCon@/@matchClause@ functions on page~94 in
95 Wadler's chapter in SLPJ.
96
97 \begin{code}
98 match_con vars all_eqns@(EqnInfo n ctx (ConPat data_con _ ex_tvs ex_dicts arg_pats : pats1) match_result1 : other_eqns)
99   = -- Make new vars for the con arguments; avoid new locals where possible
100     mapDs selectMatchVar arg_pats                          `thenDs` \ arg_vars ->
101
102     -- Now do the business to make the alt for _this_ ConPat ...
103     match (ex_dicts ++ arg_vars ++ vars)
104           (map shift_con_pat all_eqns)  `thenDs` \ match_result ->
105
106         -- Substitute over the result
107     let
108         match_result' | null ex_tvs = match_result
109                       | otherwise   = adjustMatchResult subst_it match_result
110     in  
111     returnDs (data_con, ex_tvs ++ ex_dicts ++ arg_vars, match_result')
112   where
113     shift_con_pat :: EquationInfo -> EquationInfo
114     shift_con_pat (EqnInfo n ctx (ConPat _ _ ex_tvs' ex_dicts' arg_pats: pats) match_result)
115       = EqnInfo n ctx (new_pats  ++ pats) match_result
116       where
117         new_pats  = map VarPat ex_dicts' ++ arg_pats 
118
119         -- We 'substitute' by going: (/\ tvs' -> e) tvs
120     subst_it e = foldr subst_one e other_eqns
121     subst_one (EqnInfo _ _ (ConPat _ _ ex_tvs' _ _ : _) _) e = mkTyApps (mkLams ex_tvs' e) ex_tys
122     ex_tys = mkTyVarTys ex_tvs
123 \end{code}
124
125 Note on @shift_con_pats@ just above: does what the list comprehension in
126 @matchClause@ (SLPJ, p.~94) does, except things are trickier in real
127 life.  Works for @ConPats@, and we want it to fail catastrophically
128 for anything else (which a list comprehension wouldn't).
129 Cf.~@shift_lit_pats@ in @MatchLits@.