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(..), isEmptyLHsBinds )
14 import DsBinds ( dsHsNestedBinds )
15 import DataCon ( isVanillaDataCon, dataConTyVars, dataConOrigArgTys )
16 import TcType ( tcTyConAppArgs )
17 import Type ( substTys, zipTopTvSubst, mkTyVarTys )
24 import ListSetOps ( equivClassesByUniq )
25 import SrcLoc ( unLoc, Located(..) )
26 import Unique ( Uniquable(..) )
30 We are confronted with the first column of patterns in a set of
31 equations, all beginning with constructors from one ``family'' (e.g.,
32 @[]@ and @:@ make up the @List@ ``family''). We want to generate the
33 alternatives for a @Case@ expression. There are several choices:
36 Generate an alternative for every constructor in the family, whether
37 they are used in this set of equations or not; this is what the Wadler
41 (a)~Simple. (b)~It may also be that large sparsely-used constructor
42 families are mainly handled by the code for literals.
44 (a)~Not practical for large sparsely-used constructor families, e.g.,
45 the ASCII character set. (b)~Have to look up a list of what
46 constructors make up the whole family.
50 Generate an alternative for each constructor used, then add a default
51 alternative in case some constructors in the family weren't used.
54 (a)~Alternatives aren't generated for unused constructors. (b)~The
55 STG is quite happy with defaults. (c)~No lookup in an environment needed.
57 (a)~A spurious default alternative may be generated.
61 ``Do it right:'' generate an alternative for each constructor used,
62 and add a default alternative if all constructors in the family
66 (a)~You will get cases with only one alternative (and no default),
67 which should be amenable to optimisation. Tuples are a common example.
69 (b)~Have to look up constructor families in TDE (as above).
73 We are implementing the ``do-it-right'' option for now. The arguments
74 to @matchConFamily@ are the same as to @match@; the extra @Int@
75 returned is the number of constructors in the family.
77 The function @matchConFamily@ is concerned with this
78 have-we-used-all-the-constructors? question; the local function
79 @match_cons_used@ does all the real work.
81 matchConFamily :: [Id]
85 matchConFamily (var:vars) ty eqns_info
87 -- Sort into equivalence classes by the unique on the constructor
88 -- All the EqnInfos should start with a ConPat
89 eqn_groups = equivClassesByUniq get_uniq eqns_info
90 get_uniq (EqnInfo { eqn_pats = ConPatOut (L _ data_con) _ _ _ _ _ : _}) = getUnique data_con
92 -- Now make a case alternative out of each group
93 mappM (match_con vars ty) eqn_groups `thenDs` \ alts ->
94 returnDs (mkCoAlgCaseMatchResult var ty alts)
97 And here is the local function that does all the work. It is
98 more-or-less the @matchCon@/@matchClause@ functions on page~94 in
99 Wadler's chapter in SLPJ. The function @shift_con_pats@ does what the
100 list comprehension in @matchClause@ (SLPJ, p.~94) does, except things
101 are trickier in real life. Works for @ConPats@, and we want it to
102 fail catastrophically for anything else (which a list comprehension
103 wouldn't). Cf.~@shift_lit_pats@ in @MatchLits@.
106 match_con vars ty eqns
107 = do { -- Make new vars for the con arguments; avoid new locals where possible
108 arg_vars <- selectMatchVars (map unLoc arg_pats1) arg_tys
110 ; match_result <- match (arg_vars ++ vars) ty (shiftEqns eqns)
112 ; binds <- mapM ds_binds [ bind | ConPatOut _ _ _ bind _ _ <- pats,
113 not (isEmptyLHsBinds bind) ]
115 ; let match_result' = bindInMatchResult (line_up other_pats) $
116 mkCoLetsMatchResult binds match_result
118 ; return (data_con, tvs1 ++ dicts1 ++ arg_vars, match_result') }
120 pats@(pat1 : other_pats) = map firstPat eqns
121 ConPatOut (L _ data_con) tvs1 dicts1 _ (PrefixCon arg_pats1) pat_ty = pat1
123 ds_binds bind = do { prs <- dsHsNestedBinds bind; return (Rec prs) }
126 | null tvs1 && null dicts1 = [] -- Common case
127 | otherwise = [ pr | ConPatOut _ ts ds _ _ _ <- pats,
128 pr <- (ts `zip` tvs1) ++ (ds `zip` dicts1)]
130 -- Get the arg types, which we use to type the new vars
131 -- to match on, from the "outside"; the types of pats1 may
132 -- be more refined, and hence won't do
133 arg_tys = substTys (zipTopTvSubst (dataConTyVars data_con) inst_tys)
134 (dataConOrigArgTys data_con)
135 inst_tys | isVanillaDataCon data_con = tcTyConAppArgs pat_ty -- Newtypes opaque!
136 | otherwise = mkTyVarTys tvs1
139 Note [Existentials in shift_con_pat]
140 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
142 data T = forall a. Ord a => T a (a->Int)
144 f (T x f) True = ...expr1...
145 f (T y g) False = ...expr2..
147 When we put in the tyvars etc we get
149 f (T a (d::Ord a) (x::a) (f::a->Int)) True = ...expr1...
150 f (T b (e::Ord b) (y::a) (g::a->Int)) True = ...expr2...
152 After desugaring etc we'll get a single case:
156 T a (d::Ord a) (x::a) (f::a->Int)) ->
161 *** We have to substitute [a/b, d/e] in expr2! **
163 False -> ....((/\b\(e:Ord b).expr2) a d)....
165 Originally I tried to use
166 (\b -> let e = d in expr2) a
167 to do this substitution. While this is "correct" in a way, it fails
168 Lint, because e::Ord b but d::Ord a.