2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
6 Pattern-matching constructors
9 {-# OPTIONS -fno-warn-incomplete-patterns #-}
10 -- The above warning supression flag is a temporary kludge.
11 -- While working on this module you are encouraged to remove it and fix
12 -- any warnings in the module. See
13 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
16 module MatchCon ( matchConFamily ) where
18 #include "HsVersions.h"
20 import {-# SOURCE #-} Match ( match )
31 import Util ( takeList )
38 We are confronted with the first column of patterns in a set of
39 equations, all beginning with constructors from one ``family'' (e.g.,
40 @[]@ and @:@ make up the @List@ ``family''). We want to generate the
41 alternatives for a @Case@ expression. There are several choices:
44 Generate an alternative for every constructor in the family, whether
45 they are used in this set of equations or not; this is what the Wadler
49 (a)~Simple. (b)~It may also be that large sparsely-used constructor
50 families are mainly handled by the code for literals.
52 (a)~Not practical for large sparsely-used constructor families, e.g.,
53 the ASCII character set. (b)~Have to look up a list of what
54 constructors make up the whole family.
58 Generate an alternative for each constructor used, then add a default
59 alternative in case some constructors in the family weren't used.
62 (a)~Alternatives aren't generated for unused constructors. (b)~The
63 STG is quite happy with defaults. (c)~No lookup in an environment needed.
65 (a)~A spurious default alternative may be generated.
69 ``Do it right:'' generate an alternative for each constructor used,
70 and add a default alternative if all constructors in the family
74 (a)~You will get cases with only one alternative (and no default),
75 which should be amenable to optimisation. Tuples are a common example.
77 (b)~Have to look up constructor families in TDE (as above).
81 We are implementing the ``do-it-right'' option for now. The arguments
82 to @matchConFamily@ are the same as to @match@; the extra @Int@
83 returned is the number of constructors in the family.
85 The function @matchConFamily@ is concerned with this
86 have-we-used-all-the-constructors? question; the local function
87 @match_cons_used@ does all the real work.
89 matchConFamily :: [Id]
93 -- Each group of eqns is for a single constructor
94 matchConFamily (var:vars) ty groups
95 = do { alts <- mapM (matchOneCon vars ty) groups
96 ; return (mkCoAlgCaseMatchResult var ty alts) }
101 -> DsM (DataCon, [TyVar], MatchResult)
102 matchOneCon vars ty (eqn1 : eqns) -- All eqns for a single constructor
103 = do { (wraps, eqns') <- mapAndUnzipM shift (eqn1:eqns)
104 ; arg_vars <- selectMatchVars (take (dataConSourceArity con1)
105 (eqn_pats (head eqns')))
106 -- Use the new argument patterns as a source of
107 -- suggestions for the new variables
108 ; match_result <- match (arg_vars ++ vars) ty eqns'
109 ; return (con1, tvs1 ++ dicts1 ++ arg_vars,
110 adjustMatchResult (foldr1 (.) wraps) match_result) }
112 ConPatOut { pat_con = L _ con1, pat_ty = pat_ty1,
113 pat_tvs = tvs1, pat_dicts = dicts1 } = firstPat eqn1
115 arg_tys = dataConInstOrigArgTys con1 inst_tys
116 inst_tys = tcTyConAppArgs pat_ty1 ++
117 mkTyVarTys (takeList (dataConExTyVars con1) tvs1)
118 -- Newtypes opaque, hence tcTyConAppArgs
119 -- dataConInstOrigArgTys takes the univ and existential tyvars
120 -- and returns the types of the *value* args, which is what we want
122 shift eqn@(EqnInfo { eqn_pats = ConPatOut{ pat_tvs = tvs, pat_dicts = ds,
123 pat_binds = bind, pat_args = args
125 = do { prs <- dsLHsBinds bind
126 ; return (wrapBinds (tvs `zip` tvs1)
127 . wrapBinds (ds `zip` dicts1)
128 . mkCoreLet (Rec prs),
129 eqn { eqn_pats = conArgPats con1 arg_tys args ++ pats }) }
131 conArgPats :: DataCon
132 -> [Type] -- Instantiated argument types
133 -- Used only to fill in the types of WildPats, which
134 -- are probably never looked at anyway
135 -> HsConDetails (LPat Id) (HsRecFields Id (LPat Id))
137 conArgPats _data_con _arg_tys (PrefixCon ps) = map unLoc ps
138 conArgPats _data_con _arg_tys (InfixCon p1 p2) = [unLoc p1, unLoc p2]
139 conArgPats data_con arg_tys (RecCon (HsRecFields rpats _))
141 = -- Special case for C {}, which can be used for
142 -- a constructor that isn't declared to have
147 = zipWith mk_pat (dataConFieldLabels data_con) arg_tys
149 -- mk_pat picks a WildPat of the appropriate type for absent fields,
150 -- and the specified pattern for present fields
152 = case [ pat | HsRecField sel_id pat _ <- rpats, idName (unLoc sel_id) == lbl ] of
153 (pat:pats) -> ASSERT( null pats ) unLoc pat
157 Note [Existentials in shift_con_pat]
158 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
160 data T = forall a. Ord a => T a (a->Int)
162 f (T x f) True = ...expr1...
163 f (T y g) False = ...expr2..
165 When we put in the tyvars etc we get
167 f (T a (d::Ord a) (x::a) (f::a->Int)) True = ...expr1...
168 f (T b (e::Ord b) (y::a) (g::a->Int)) True = ...expr2...
170 After desugaring etc we'll get a single case:
174 T a (d::Ord a) (x::a) (f::a->Int)) ->
179 *** We have to substitute [a/b, d/e] in expr2! **
181 False -> ....((/\b\(e:Ord b).expr2) a d)....
183 Originally I tried to use
184 (\b -> let e = d in expr2) a
185 to do this substitution. While this is "correct" in a way, it fails
186 Lint, because e::Ord b but d::Ord a.