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 ( Pat(..), HsConDetails(..) )
19 import Subst ( mkSubst, mkInScopeSet, bindSubst, substExpr )
20 import CoreFVs ( exprFreeVars )
21 import VarEnv ( emptySubstEnv )
22 import ListSetOps ( equivClassesByUniq )
23 import SrcLoc ( unLoc )
24 import Unique ( Uniquable(..) )
27 We are confronted with the first column of patterns in a set of
28 equations, all beginning with constructors from one ``family'' (e.g.,
29 @[]@ and @:@ make up the @List@ ``family''). We want to generate the
30 alternatives for a @Case@ expression. There are several choices:
33 Generate an alternative for every constructor in the family, whether
34 they are used in this set of equations or not; this is what the Wadler
38 (a)~Simple. (b)~It may also be that large sparsely-used constructor
39 families are mainly handled by the code for literals.
41 (a)~Not practical for large sparsely-used constructor families, e.g.,
42 the ASCII character set. (b)~Have to look up a list of what
43 constructors make up the whole family.
47 Generate an alternative for each constructor used, then add a default
48 alternative in case some constructors in the family weren't used.
51 (a)~Alternatives aren't generated for unused constructors. (b)~The
52 STG is quite happy with defaults. (c)~No lookup in an environment needed.
54 (a)~A spurious default alternative may be generated.
58 ``Do it right:'' generate an alternative for each constructor used,
59 and add a default alternative if all constructors in the family
63 (a)~You will get cases with only one alternative (and no default),
64 which should be amenable to optimisation. Tuples are a common example.
66 (b)~Have to look up constructor families in TDE (as above).
70 We are implementing the ``do-it-right'' option for now. The arguments
71 to @matchConFamily@ are the same as to @match@; the extra @Int@
72 returned is the number of constructors in the family.
74 The function @matchConFamily@ is concerned with this
75 have-we-used-all-the-constructors? question; the local function
76 @match_cons_used@ does all the real work.
78 matchConFamily :: [Id]
82 matchConFamily (var:vars) eqns_info
84 -- Sort into equivalence classes by the unique on the constructor
85 -- All the EqnInfos should start with a ConPat
86 eqn_groups = equivClassesByUniq get_uniq eqns_info
87 get_uniq (EqnInfo _ _ (ConPatOut data_con _ _ _ _ : _) _) = getUnique data_con
89 -- Now make a case alternative out of each group
90 mappM (match_con vars) eqn_groups `thenDs` \ alts ->
92 returnDs (mkCoAlgCaseMatchResult var alts)
95 And here is the local function that does all the work. It is
96 more-or-less the @matchCon@/@matchClause@ functions on page~94 in
97 Wadler's chapter in SLPJ.
100 match_con vars (eqn1@(EqnInfo _ _ (ConPatOut data_con (PrefixCon arg_pats) _ ex_tvs ex_dicts : _) _)
102 = -- Make new vars for the con arguments; avoid new locals where possible
103 mappM selectMatchVarL arg_pats `thenDs` \ arg_vars ->
105 -- Now do the business to make the alt for _this_ ConPat ...
106 match (arg_vars ++ vars)
107 (map shift_con_pat (eqn1:other_eqns)) `thenDs` \ match_result ->
109 -- [See "notes on do_subst" below this function]
110 -- Make the ex_tvs and ex_dicts line up with those
111 -- in the first pattern. Remember, they are all guaranteed to be variables
113 match_result' | null ex_tvs = match_result
114 | null other_eqns = match_result
115 | otherwise = adjustMatchResult do_subst match_result
118 returnDs (data_con, ex_tvs ++ ex_dicts ++ arg_vars, match_result')
120 shift_con_pat :: EquationInfo -> EquationInfo
121 shift_con_pat (EqnInfo n ctx (ConPatOut _ (PrefixCon arg_pats) _ _ _ : pats) match_result)
122 = EqnInfo n ctx (map unLoc arg_pats ++ pats) match_result
124 other_pats = [p | EqnInfo _ _ (p:_) _ <- other_eqns]
126 var_prs = concat [ (ex_tvs' `zip` ex_tvs) ++
127 (ex_dicts' `zip` ex_dicts)
128 | ConPatOut _ _ _ ex_tvs' ex_dicts' <- other_pats ]
130 do_subst e = substExpr subst e
132 subst = foldl (\ s (v', v) -> bindSubst s v' v) in_scope var_prs
133 in_scope = mkSubst (mkInScopeSet (exprFreeVars e)) emptySubstEnv
134 -- We put all the free variables of e into the in-scope
135 -- set of the substitution, not because it is necessary,
136 -- but to suppress the warning in Subst.lookupInScope
137 -- Tiresome, but doing the substitution at all is rare.
140 Note on @shift_con_pats@ just above: does what the list comprehension in
141 @matchClause@ (SLPJ, p.~94) does, except things are trickier in real
142 life. Works for @ConPats@, and we want it to fail catastrophically
143 for anything else (which a list comprehension wouldn't).
144 Cf.~@shift_lit_pats@ in @MatchLits@.
147 Notes on do_subst stuff
148 ~~~~~~~~~~~~~~~~~~~~~~~
150 data T = forall a. Ord a => T a (a->Int)
152 f (T x f) True = ...expr1...
153 f (T y g) False = ...expr2..
155 When we put in the tyvars etc we get
157 f (T a (d::Ord a) (x::a) (f::a->Int)) True = ...expr1...
158 f (T b (e::Ord a) (y::a) (g::a->Int)) True = ...expr2...
160 After desugaring etc we'll get a single case:
164 T a (d::Ord a) (x::a) (f::a->Int)) ->
169 *** We have to substitute [a/b, d/e] in expr2! **
170 That is what do_subst is doing.
172 Originally I tried to use
173 (\b -> let e = d in expr2) a
174 to do this substitution. While this is "correct" in a way, it fails
175 Lint, because e::Ord b but d::Ord a.
177 So now I simply do the substitution properly using substExpr.