2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[MatchCon]{Pattern-matching constructors}
7 module MatchCon ( matchConFamily ) where
9 #include "HsVersions.h"
11 import {-# SOURCE #-} Match ( match )
13 import HsSyn ( OutPat(..) )
20 import Type ( mkTyVarTys )
21 import ListSetOps ( equivClassesByUniq )
22 import Unique ( Uniquable(..) )
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:
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
36 (a)~Simple. (b)~It may also be that large sparsely-used constructor
37 families are mainly handled by the code for literals.
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.
45 Generate an alternative for each constructor used, then add a default
46 alternative in case some constructors in the family weren't used.
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.
52 (a)~A spurious default alternative may be generated.
56 ``Do it right:'' generate an alternative for each constructor used,
57 and add a default alternative if all constructors in the family
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.
64 (b)~Have to look up constructor families in TDE (as above).
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.
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.
76 matchConFamily :: [Id]
80 matchConFamily (var:vars) eqns_info
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
87 -- Now make a case alternative out of each group
88 mapDs (match_con vars) eqn_groups `thenDs` \ alts ->
90 returnDs (mkCoAlgCaseMatchResult var alts)
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.
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 ->
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 ->
106 -- Substitute over the result
108 match_result' | null ex_tvs = match_result
109 | otherwise = adjustMatchResult subst_it match_result
111 returnDs (data_con, ex_tvs ++ ex_dicts ++ arg_vars, match_result')
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
117 new_pats = map VarPat ex_dicts' ++ arg_pats
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
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@.