2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[MatchCon]{Pattern-matching constructors}
7 #include "HsVersions.h"
9 module MatchCon ( matchConFamily ) where
12 import DsLoop ( match ) -- break match-ish loop
14 import HsSyn ( OutPat(..), HsLit, HsExpr )
15 import DsHsSyn ( outPatType )
20 import Id ( isDataCon, GenId{-instances-} )
21 import Util ( panic, assertPanic )
24 We are confronted with the first column of patterns in a set of
25 equations, all beginning with constructors from one ``family'' (e.g.,
26 @[]@ and @:@ make up the @List@ ``family''). We want to generate the
27 alternatives for a @Case@ expression. There are several choices:
30 Generate an alternative for every constructor in the family, whether
31 they are used in this set of equations or not; this is what the Wadler
35 (a)~Simple. (b)~It may also be that large sparsely-used constructor
36 families are mainly handled by the code for literals.
38 (a)~Not practical for large sparsely-used constructor families, e.g.,
39 the ASCII character set. (b)~Have to look up a list of what
40 constructors make up the whole family.
44 Generate an alternative for each constructor used, then add a default
45 alternative in case some constructors in the family weren't used.
48 (a)~Alternatives aren't generated for unused constructors. (b)~The
49 STG is quite happy with defaults. (c)~No lookup in an environment needed.
51 (a)~A spurious default alternative may be generated.
55 ``Do it right:'' generate an alternative for each constructor used,
56 and add a default alternative if all constructors in the family
60 (a)~You will get cases with only one alternative (and no default),
61 which should be amenable to optimisation. Tuples are a common example.
63 (b)~Have to look up constructor families in TDE (as above).
67 We are implementing the ``do-it-right'' option for now. The arguments
68 to @matchConFamily@ are the same as to @match@; the extra @Int@
69 returned is the number of constructors in the family.
71 The function @matchConFamily@ is concerned with this
72 have-we-used-all-the-constructors? question; the local function
73 @match_cons_used@ does all the real work.
75 matchConFamily :: [Id]
77 -> [EquationInfo] -- Shadows
80 matchConFamily (var:vars) eqns_info shadows
81 = match_cons_used vars eqns_info shadows `thenDs` \ alts ->
82 mkCoAlgCaseMatchResult var alts
85 And here is the local function that does all the work. It is
86 more-or-less the @matchCon@/@matchClause@ functions on page~94 in
87 Wadler's chapter in SLPJ.
89 match_cons_used _ [{- no more eqns -}] _ = returnDs []
91 match_cons_used vars eqns_info@(EqnInfo (ConPat data_con _ arg_pats : ps1) _ : eqns) shadows
92 = ASSERT(isDataCon data_con)
94 (eqns_for_this_con, eqns_not_for_this_con) = splitByCon eqns_info
95 (shadows_for_this_con, shadows_not_for_this_con) = splitByCon shadows
97 -- Go ahead and do the recursive call to make the alts
98 -- for the other ConPats in this con family...
99 match_cons_used vars eqns_not_for_this_con shadows_not_for_this_con `thenDs` \ rest_of_alts ->
101 -- Make new vars for the con arguments; avoid new locals where possible
102 selectMatchVars arg_pats `thenDs` \ new_vars ->
104 -- Now do the business to make the alt for _this_ ConPat ...
105 match (new_vars++vars)
106 (map shift_con_pat eqns_for_this_con)
107 (map shift_con_pat shadows_for_this_con) `thenDs` \ match_result ->
110 (data_con, new_vars, match_result)
114 splitByCon :: [EquationInfo] -> ([EquationInfo], [EquationInfo])
115 splitByCon [] = ([],[])
116 splitByCon (info@(EqnInfo (pat : _) _) : rest)
118 ConPat n _ _ | n == data_con -> (info:rest_yes, rest_no)
119 WildPat _ -> (info:rest_yes, info:rest_no)
120 -- WildPats will be in the shadows only,
121 -- and they go into both groups
122 other_pat -> (rest_yes, info:rest_no)
124 (rest_yes, rest_no) = splitByCon rest
126 shift_con_pat :: EquationInfo -> EquationInfo
127 shift_con_pat (EqnInfo (ConPat _ _ pats': pats) match_result)
128 = EqnInfo (pats' ++ pats) match_result
129 shift_con_pat (EqnInfo (WildPat _: pats) match_result) -- Will only happen in shadow
130 = EqnInfo ([WildPat (outPatType arg_pat) | arg_pat <- arg_pats] ++ pats) match_result
131 shift_con_pat other = panic "matchConFamily:match_cons_used:shift_con_pat"
134 Note on @shift_con_pats@ just above: does what the list comprehension in
135 @matchClause@ (SLPJ, p.~94) does, except things are trickier in real
136 life. Works for @ConPats@, and we want it to fail catastrophically
137 for anything else (which a list comprehension wouldn't).
138 Cf.~@shift_lit_pats@ in @MatchLits@.