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 -- XXX This define is a bit of a hack, and should be done more nicely
19 #define FAST_STRING_NOT_NEEDED 1
20 #include "HsVersions.h"
22 import {-# SOURCE #-} Match ( match )
32 import Util ( takeList )
39 We are confronted with the first column of patterns in a set of
40 equations, all beginning with constructors from one ``family'' (e.g.,
41 @[]@ and @:@ make up the @List@ ``family''). We want to generate the
42 alternatives for a @Case@ expression. There are several choices:
45 Generate an alternative for every constructor in the family, whether
46 they are used in this set of equations or not; this is what the Wadler
50 (a)~Simple. (b)~It may also be that large sparsely-used constructor
51 families are mainly handled by the code for literals.
53 (a)~Not practical for large sparsely-used constructor families, e.g.,
54 the ASCII character set. (b)~Have to look up a list of what
55 constructors make up the whole family.
59 Generate an alternative for each constructor used, then add a default
60 alternative in case some constructors in the family weren't used.
63 (a)~Alternatives aren't generated for unused constructors. (b)~The
64 STG is quite happy with defaults. (c)~No lookup in an environment needed.
66 (a)~A spurious default alternative may be generated.
70 ``Do it right:'' generate an alternative for each constructor used,
71 and add a default alternative if all constructors in the family
75 (a)~You will get cases with only one alternative (and no default),
76 which should be amenable to optimisation. Tuples are a common example.
78 (b)~Have to look up constructor families in TDE (as above).
82 We are implementing the ``do-it-right'' option for now. The arguments
83 to @matchConFamily@ are the same as to @match@; the extra @Int@
84 returned is the number of constructors in the family.
86 The function @matchConFamily@ is concerned with this
87 have-we-used-all-the-constructors? question; the local function
88 @match_cons_used@ does all the real work.
90 matchConFamily :: [Id]
94 -- Each group of eqns is for a single constructor
95 matchConFamily (var:vars) ty groups
96 = do { alts <- mapM (matchOneCon vars ty) groups
97 ; return (mkCoAlgCaseMatchResult var ty alts) }
102 -> DsM (DataCon, [TyVar], MatchResult)
103 matchOneCon vars ty (eqn1 : eqns) -- All eqns for a single constructor
104 = do { (wraps, eqns') <- mapAndUnzipM shift (eqn1:eqns)
105 ; arg_vars <- selectMatchVars (take (dataConSourceArity con1)
106 (eqn_pats (head eqns')))
107 -- Use the new arugment patterns as a source of
108 -- suggestions for the new variables
109 ; match_result <- match (arg_vars ++ vars) ty eqns'
110 ; return (con1, tvs1 ++ dicts1 ++ arg_vars,
111 adjustMatchResult (foldr1 (.) wraps) match_result) }
113 ConPatOut { pat_con = L _ con1, pat_ty = pat_ty1,
114 pat_tvs = tvs1, pat_dicts = dicts1 } = firstPat eqn1
116 arg_tys = dataConInstOrigArgTys con1 inst_tys
117 inst_tys = tcTyConAppArgs pat_ty1 ++
118 mkTyVarTys (takeList (dataConExTyVars con1) tvs1)
119 -- Newtypes opaque, hence tcTyConAppArgs
120 -- dataConInstOrigArgTys takes the univ and existential tyvars
121 -- and returns the types of the *value* args, which is what we want
123 shift eqn@(EqnInfo { eqn_pats = ConPatOut{ pat_tvs = tvs, pat_dicts = ds,
124 pat_binds = bind, pat_args = args
126 = do { prs <- dsLHsBinds bind
127 ; return (wrapBinds (tvs `zip` tvs1)
128 . wrapBinds (ds `zip` dicts1)
130 eqn { eqn_pats = conArgPats con1 arg_tys args ++ pats }) }
132 conArgPats :: DataCon
133 -> [Type] -- Instantiated argument types
134 -- Used only to fill in the types of WildPats, which
135 -- are probably never looked at anyway
136 -> HsConDetails (LPat Id) (HsRecFields Id (LPat Id))
138 conArgPats _data_con _arg_tys (PrefixCon ps) = map unLoc ps
139 conArgPats _data_con _arg_tys (InfixCon p1 p2) = [unLoc p1, unLoc p2]
140 conArgPats data_con arg_tys (RecCon (HsRecFields rpats _))
142 = -- Special case for C {}, which can be used for
143 -- a constructor that isn't declared to have
148 = zipWith mk_pat (dataConFieldLabels data_con) arg_tys
150 -- mk_pat picks a WildPat of the appropriate type for absent fields,
151 -- and the specified pattern for present fields
153 = case [ pat | HsRecField sel_id pat _ <- rpats, idName (unLoc sel_id) == lbl ] of
154 (pat:pats) -> ASSERT( null pats ) unLoc pat
158 Note [Existentials in shift_con_pat]
159 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
161 data T = forall a. Ord a => T a (a->Int)
163 f (T x f) True = ...expr1...
164 f (T y g) False = ...expr2..
166 When we put in the tyvars etc we get
168 f (T a (d::Ord a) (x::a) (f::a->Int)) True = ...expr1...
169 f (T b (e::Ord b) (y::a) (g::a->Int)) True = ...expr2...
171 After desugaring etc we'll get a single case:
175 T a (d::Ord a) (x::a) (f::a->Int)) ->
180 *** We have to substitute [a/b, d/e] in expr2! **
182 False -> ....((/\b\(e:Ord b).expr2) a d)....
184 Originally I tried to use
185 (\b -> let e = d in expr2) a
186 to do this substitution. While this is "correct" in a way, it fails
187 Lint, because e::Ord b but d::Ord a.