[project @ 1997-12-02 18:56:38 by quintela]
[ghc-hetmet.git] / ghc / compiler / deSugar / MatchCon.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[MatchCon]{Pattern-matching constructors}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module MatchCon ( matchConFamily ) where
10
11 IMP_Ubiq()
12 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 201
13 IMPORT_DELOOPER(DsLoop)         ( match )       -- break match-ish loop
14 #else
15 import {-# SOURCE #-} Match
16 #endif
17
18 import HsSyn            ( OutPat(..), HsLit, HsExpr )
19 import DsHsSyn          ( outPatType )
20
21 import DsMonad
22 import DsUtils
23
24 import Id               ( GenId{-instances-}, SYN_IE(Id) )
25 import Util             ( panic, assertPanic )
26 \end{code}
27
28 We are confronted with the first column of patterns in a set of
29 equations, all beginning with constructors from one ``family'' (e.g.,
30 @[]@ and @:@ make up the @List@ ``family'').  We want to generate the
31 alternatives for a @Case@ expression.  There are several choices:
32 \begin{enumerate}
33 \item
34 Generate an alternative for every constructor in the family, whether
35 they are used in this set of equations or not; this is what the Wadler
36 chapter does.
37 \begin{description}
38 \item[Advantages:]
39 (a)~Simple.  (b)~It may also be that large sparsely-used constructor
40 families are mainly handled by the code for literals.
41 \item[Disadvantages:]
42 (a)~Not practical for large sparsely-used constructor families, e.g.,
43 the ASCII character set.  (b)~Have to look up a list of what
44 constructors make up the whole family.
45 \end{description}
46
47 \item
48 Generate an alternative for each constructor used, then add a default
49 alternative in case some constructors in the family weren't used.
50 \begin{description}
51 \item[Advantages:]
52 (a)~Alternatives aren't generated for unused constructors.  (b)~The
53 STG is quite happy with defaults.  (c)~No lookup in an environment needed.
54 \item[Disadvantages:]
55 (a)~A spurious default alternative may be generated.
56 \end{description}
57
58 \item
59 ``Do it right:'' generate an alternative for each constructor used,
60 and add a default alternative if all constructors in the family
61 weren't used.
62 \begin{description}
63 \item[Advantages:]
64 (a)~You will get cases with only one alternative (and no default),
65 which should be amenable to optimisation.  Tuples are a common example.
66 \item[Disadvantages:]
67 (b)~Have to look up constructor families in TDE (as above).
68 \end{description}
69 \end{enumerate}
70
71 We are implementing the ``do-it-right'' option for now.  The arguments
72 to @matchConFamily@ are the same as to @match@; the extra @Int@
73 returned is the number of constructors in the family.
74
75 The function @matchConFamily@ is concerned with this
76 have-we-used-all-the-constructors? question; the local function
77 @match_cons_used@ does all the real work.
78 \begin{code}
79 matchConFamily :: [Id]
80                -> [EquationInfo]
81                -> DsM MatchResult
82
83 matchConFamily (var:vars) eqns_info
84   = match_cons_used vars eqns_info `thenDs` \ alts ->
85     mkCoAlgCaseMatchResult var alts
86 \end{code}
87
88 And here is the local function that does all the work.  It is
89 more-or-less the @matchCon@/@matchClause@ functions on page~94 in
90 Wadler's chapter in SLPJ.
91 \begin{code}
92 match_cons_used _ [{- no more eqns -}] = returnDs []
93
94 match_cons_used vars eqns_info@(EqnInfo n ctx (ConPat data_con _ arg_pats : ps1) _ : eqns)
95   = let
96         (eqns_for_this_con, eqns_not_for_this_con)       = splitByCon eqns_info
97     in
98     -- Go ahead and do the recursive call to make the alts
99     -- for the other ConPats in this con family...
100     match_cons_used vars eqns_not_for_this_con            `thenDs` \ rest_of_alts ->
101
102     -- Make new vars for the con arguments; avoid new locals where possible
103     selectMatchVars arg_pats                               `thenDs` \ new_vars ->
104
105     -- Now do the business to make the alt for _this_ ConPat ...
106     match (new_vars++vars)
107           (map shift_con_pat eqns_for_this_con)            `thenDs` \ match_result ->
108
109     returnDs (
110         (data_con, new_vars, match_result)
111         : rest_of_alts
112     )
113   where
114     splitByCon :: [EquationInfo] -> ([EquationInfo], [EquationInfo])
115     splitByCon [] = ([],[])
116     splitByCon (info@(EqnInfo _ _ (pat : _) _) : rest)
117         = case pat of
118                 ConPat n _ _ | n == data_con -> (info:rest_yes, rest_no)
119                 other_pat                    -> (rest_yes,      info:rest_no)
120         where
121           (rest_yes, rest_no) = splitByCon rest
122
123     shift_con_pat :: EquationInfo -> EquationInfo
124     shift_con_pat (EqnInfo n ctx (ConPat _ _ pats': pats) match_result)
125       = EqnInfo n ctx (pats' ++ pats) match_result
126     shift_con_pat (EqnInfo n ctx (WildPat _: pats) match_result) -- Will only happen in shadow
127       = EqnInfo n ctx ([WildPat (outPatType arg_pat) | arg_pat <- arg_pats] ++ pats) match_result
128     shift_con_pat other = panic "matchConFamily:match_cons_used:shift_con_pat"
129 \end{code}
130
131 Note on @shift_con_pats@ just above: does what the list comprehension in
132 @matchClause@ (SLPJ, p.~94) does, except things are trickier in real
133 life.  Works for @ConPats@, and we want it to fail catastrophically
134 for anything else (which a list comprehension wouldn't).
135 Cf.~@shift_lit_pats@ in @MatchLits@.