2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1997-1998
5 % Author: Juan J. Quintela <quintela@krilin.dc.fi.udc.es>
8 {-# OPTIONS -fno-warn-incomplete-patterns #-}
9 -- The above warning supression flag is a temporary kludge.
10 -- While working on this module you are encouraged to remove it and fix
11 -- any warnings in the module. See
12 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
15 module Check ( check , ExhaustivePat ) where
17 #include "HsVersions.h"
30 import Unify( dataConCannotMatch )
38 This module performs checks about if one list of equations are:
43 To discover that we go through the list of equations in a tree-like fashion.
45 If you like theory, a similar algorithm is described in:
47 {\em Two Techniques for Compiling Lazy Pattern Matching},
49 INRIA Rocquencourt (RR-2385, 1994)
51 The algorithm is based on the first technique, but there are some differences:
53 \item We don't generate code
54 \item We have constructors and literals (not only literals as in the
56 \item We don't use directions, we must select the columns from
59 (By the way the second technique is really similar to the one used in
60 @Match.lhs@ to generate code)
62 This function takes the equations of a pattern and returns:
64 \item The patterns that are not recognized
65 \item The equations that are not overlapped
67 It simplify the patterns and then call @check'@ (the same semantics), and it
68 needs to reconstruct the patterns again ....
70 The problem appear with things like:
75 We want to put the two patterns with the same syntax, (prefix form) and
76 then all the constructors are equal:
78 f (: x (: y [])) = ....
81 (more about that in @simplify_eqns@)
83 We would prefer to have a @WarningPat@ of type @String@, but Strings and the
84 Pretty Printer are not friends.
86 We use @InPat@ in @WarningPat@ instead of @OutPat@
87 because we need to print the
88 warning messages in the same way they are introduced, i.e. if the user
93 He don't want a warning message written:
95 f (: x (: y [])) ........
97 Then we need to use InPats.
99 Juan Quintela 5 JUL 1998\\
100 User-friendliness and compiler writers are no friends.
104 type WarningPat = InPat Name
105 type ExhaustivePat = ([WarningPat], [(Name, [HsLit])])
107 type EqnSet = UniqSet EqnNo
110 check :: [EquationInfo] -> ([ExhaustivePat], [EquationInfo])
111 -- Second result is the shadowed equations
112 -- if there are view patterns, just give up - don't know what the function is
113 check qs | has_view_pattern = ([],[])
114 | otherwise = (untidy_warns, shadowed_eqns)
116 eqnInfo_has_view_pattern (EqnInfo ps _) = any (hasViewPat . noLoc) ps
117 has_view_pattern = any eqnInfo_has_view_pattern qs
118 (warns, used_nos) = check' ([1..] `zip` map simplify_eqn qs)
119 untidy_warns = map untidy_exhaustive warns
120 shadowed_eqns = [eqn | (eqn,i) <- qs `zip` [1..],
121 not (i `elementOfUniqSet` used_nos)]
123 untidy_exhaustive :: ExhaustivePat -> ExhaustivePat
124 untidy_exhaustive ([pat], messages) =
125 ([untidy_no_pars pat], map untidy_message messages)
126 untidy_exhaustive (pats, messages) =
127 (map untidy_pars pats, map untidy_message messages)
129 untidy_message :: (Name, [HsLit]) -> (Name, [HsLit])
130 untidy_message (string, lits) = (string, map untidy_lit lits)
133 The function @untidy@ does the reverse work of the @simplify_pat@ funcion.
139 untidy_no_pars :: WarningPat -> WarningPat
140 untidy_no_pars p = untidy False p
142 untidy_pars :: WarningPat -> WarningPat
143 untidy_pars p = untidy True p
145 untidy :: NeedPars -> WarningPat -> WarningPat
146 untidy b (L loc p) = L loc (untidy' b p)
148 untidy' _ p@(WildPat _) = p
149 untidy' _ p@(VarPat _) = p
150 untidy' _ (LitPat lit) = LitPat (untidy_lit lit)
151 untidy' _ p@(ConPatIn _ (PrefixCon [])) = p
152 untidy' b (ConPatIn name ps) = pars b (L loc (ConPatIn name (untidy_con ps)))
153 untidy' _ (ListPat pats ty) = ListPat (map untidy_no_pars pats) ty
154 untidy' _ (TuplePat pats box ty) = TuplePat (map untidy_no_pars pats) box ty
155 untidy' _ (PArrPat _ _) = panic "Check.untidy: Shouldn't get a parallel array here!"
156 untidy' _ (SigPatIn _ _) = panic "Check.untidy: SigPat"
158 untidy_con :: HsConPatDetails Name -> HsConPatDetails Name
159 untidy_con (PrefixCon pats) = PrefixCon (map untidy_pars pats)
160 untidy_con (InfixCon p1 p2) = InfixCon (untidy_pars p1) (untidy_pars p2)
161 untidy_con (RecCon (HsRecFields flds dd))
162 = RecCon (HsRecFields [ fld { hsRecFieldArg = untidy_pars (hsRecFieldArg fld) }
165 pars :: NeedPars -> WarningPat -> Pat Name
166 pars True p = ParPat p
169 untidy_lit :: HsLit -> HsLit
170 untidy_lit (HsCharPrim c) = HsChar c
174 This equation is the same that check, the only difference is that the
175 boring work is done, that work needs to be done only once, this is
176 the reason top have two functions, check is the external interface,
177 @check'@ is called recursively.
179 There are several cases:
182 \item There are no equations: Everything is OK.
183 \item There are only one equation, that can fail, and all the patterns are
184 variables. Then that equation is used and the same equation is
186 \item All the patterns are variables, and the match can fail, there are
187 more equations then the results is the result of the rest of equations
188 and this equation is used also.
190 \item The general case, if all the patterns are variables (here the match
191 can't fail) then the result is that this equation is used and this
192 equation doesn't generate non-exhaustive cases.
194 \item In the general case, there can exist literals ,constructors or only
195 vars in the first column, we actuate in consequence.
202 check' :: [(EqnNo, EquationInfo)]
203 -> ([ExhaustivePat], -- Pattern scheme that might not be matched at all
204 EqnSet) -- Eqns that are used (others are overlapped)
206 check' [] = ([([],[])],emptyUniqSet)
208 check' ((n, EqnInfo { eqn_pats = ps, eqn_rhs = MatchResult can_fail _ }) : rs)
209 | first_eqn_all_vars && case can_fail of { CantFail -> True; CanFail -> False }
210 = ([], unitUniqSet n) -- One eqn, which can't fail
212 | first_eqn_all_vars && null rs -- One eqn, but it can fail
213 = ([(takeList ps (repeat nlWildPat),[])], unitUniqSet n)
215 | first_eqn_all_vars -- Several eqns, first can fail
216 = (pats, addOneToUniqSet indexs n)
218 first_eqn_all_vars = all_vars ps
219 (pats,indexs) = check' rs
222 | literals = split_by_literals qs
223 | constructors = split_by_constructor qs
224 | only_vars = first_column_only_vars qs
225 -- pprPanic "Check.check': Not implemented :-(" (ppr first_pats)
227 -- Note: RecPats will have been simplified to ConPats
229 first_pats = ASSERT2( okGroup qs, pprGroup qs ) map firstPatN qs
230 constructors = any is_con first_pats
231 literals = any is_lit first_pats
232 only_vars = all is_var first_pats
235 Here begins the code to deal with literals, we need to split the matrix
236 in different matrix beginning by each literal and a last matrix with the
240 split_by_literals :: [(EqnNo, EquationInfo)] -> ([ExhaustivePat], EqnSet)
241 split_by_literals qs = process_literals used_lits qs
243 used_lits = get_used_lits qs
246 @process_explicit_literals@ is a function that process each literal that appears
247 in the column of the matrix.
250 process_explicit_literals :: [HsLit] -> [(EqnNo, EquationInfo)] -> ([ExhaustivePat],EqnSet)
251 process_explicit_literals lits qs = (concat pats, unionManyUniqSets indexs)
253 pats_indexs = map (\x -> construct_literal_matrix x qs) lits
254 (pats,indexs) = unzip pats_indexs
258 @process_literals@ calls @process_explicit_literals@ to deal with the literals
259 that appears in the matrix and deal also with the rest of the cases. It
260 must be one Variable to be complete.
264 process_literals :: [HsLit] -> [(EqnNo, EquationInfo)] -> ([ExhaustivePat],EqnSet)
265 process_literals used_lits qs
266 | null default_eqns = ASSERT( not (null qs) ) ([make_row_vars used_lits (head qs)] ++ pats,indexs)
267 | otherwise = (pats_default,indexs_default)
269 (pats,indexs) = process_explicit_literals used_lits qs
270 default_eqns = ASSERT2( okGroup qs, pprGroup qs )
271 [remove_var q | q <- qs, is_var (firstPatN q)]
272 (pats',indexs') = check' default_eqns
273 pats_default = [(nlWildPat:ps,constraints) | (ps,constraints) <- (pats')] ++ pats
274 indexs_default = unionUniqSets indexs' indexs
277 Here we have selected the literal and we will select all the equations that
278 begins for that literal and create a new matrix.
281 construct_literal_matrix :: HsLit -> [(EqnNo, EquationInfo)] -> ([ExhaustivePat],EqnSet)
282 construct_literal_matrix lit qs =
283 (map (\ (xs,ys) -> (new_lit:xs,ys)) pats,indexs)
285 (pats,indexs) = (check' (remove_first_column_lit lit qs))
286 new_lit = nlLitPat lit
288 remove_first_column_lit :: HsLit
289 -> [(EqnNo, EquationInfo)]
290 -> [(EqnNo, EquationInfo)]
291 remove_first_column_lit lit qs
292 = ASSERT2( okGroup qs, pprGroup qs )
293 [(n, shift_pat eqn) | q@(n,eqn) <- qs, is_var_lit lit (firstPatN q)]
295 shift_pat eqn@(EqnInfo { eqn_pats = _:ps}) = eqn { eqn_pats = ps }
296 shift_pat _ = panic "Check.shift_var: no patterns"
299 This function splits the equations @qs@ in groups that deal with the
303 split_by_constructor :: [(EqnNo, EquationInfo)] -> ([ExhaustivePat], EqnSet)
304 split_by_constructor qs
305 | notNull unused_cons = need_default_case used_cons unused_cons qs
306 | otherwise = no_need_default_case used_cons qs
308 used_cons = get_used_cons qs
309 unused_cons = get_unused_cons used_cons
312 The first column of the patterns matrix only have vars, then there is
316 first_column_only_vars :: [(EqnNo, EquationInfo)] -> ([ExhaustivePat],EqnSet)
317 first_column_only_vars qs = (map (\ (xs,ys) -> (nlWildPat:xs,ys)) pats,indexs)
319 (pats, indexs) = check' (map remove_var qs)
322 This equation takes a matrix of patterns and split the equations by
323 constructor, using all the constructors that appears in the first column
324 of the pattern matching.
326 We can need a default clause or not ...., it depends if we used all the
327 constructors or not explicitly. The reasoning is similar to @process_literals@,
328 the difference is that here the default case is not always needed.
331 no_need_default_case :: [Pat Id] -> [(EqnNo, EquationInfo)] -> ([ExhaustivePat],EqnSet)
332 no_need_default_case cons qs = (concat pats, unionManyUniqSets indexs)
334 pats_indexs = map (\x -> construct_matrix x qs) cons
335 (pats,indexs) = unzip pats_indexs
337 need_default_case :: [Pat Id] -> [DataCon] -> [(EqnNo, EquationInfo)] -> ([ExhaustivePat],EqnSet)
338 need_default_case used_cons unused_cons qs
339 | null default_eqns = (pats_default_no_eqns,indexs)
340 | otherwise = (pats_default,indexs_default)
342 (pats,indexs) = no_need_default_case used_cons qs
343 default_eqns = ASSERT2( okGroup qs, pprGroup qs )
344 [remove_var q | q <- qs, is_var (firstPatN q)]
345 (pats',indexs') = check' default_eqns
346 pats_default = [(make_whole_con c:ps,constraints) |
347 c <- unused_cons, (ps,constraints) <- pats'] ++ pats
348 new_wilds = ASSERT( not (null qs) ) make_row_vars_for_constructor (head qs)
349 pats_default_no_eqns = [(make_whole_con c:new_wilds,[]) | c <- unused_cons] ++ pats
350 indexs_default = unionUniqSets indexs' indexs
352 construct_matrix :: Pat Id -> [(EqnNo, EquationInfo)] -> ([ExhaustivePat],EqnSet)
353 construct_matrix con qs =
354 (map (make_con con) pats,indexs)
356 (pats,indexs) = (check' (remove_first_column con qs))
359 Here remove first column is more difficult that with literals due to the fact
360 that constructors can have arguments.
362 For instance, the matrix
374 remove_first_column :: Pat Id -- Constructor
375 -> [(EqnNo, EquationInfo)]
376 -> [(EqnNo, EquationInfo)]
377 remove_first_column (ConPatOut{ pat_con = L _ con, pat_args = PrefixCon con_pats }) qs
378 = ASSERT2( okGroup qs, pprGroup qs )
379 [(n, shift_var eqn) | q@(n, eqn) <- qs, is_var_con con (firstPatN q)]
381 new_wilds = [WildPat (hsLPatType arg_pat) | arg_pat <- con_pats]
382 shift_var eqn@(EqnInfo { eqn_pats = ConPatOut{ pat_args = PrefixCon ps' } : ps})
383 = eqn { eqn_pats = map unLoc ps' ++ ps }
384 shift_var eqn@(EqnInfo { eqn_pats = WildPat _ : ps })
385 = eqn { eqn_pats = new_wilds ++ ps }
386 shift_var _ = panic "Check.Shift_var:No done"
388 make_row_vars :: [HsLit] -> (EqnNo, EquationInfo) -> ExhaustivePat
389 make_row_vars used_lits (_, EqnInfo { eqn_pats = pats})
390 = (nlVarPat new_var:takeList (tail pats) (repeat nlWildPat),[(new_var,used_lits)])
395 hash_x = mkInternalName unboundKey {- doesn't matter much -}
396 (mkVarOccFS (fsLit "#x"))
399 make_row_vars_for_constructor :: (EqnNo, EquationInfo) -> [WarningPat]
400 make_row_vars_for_constructor (_, EqnInfo { eqn_pats = pats})
401 = takeList (tail pats) (repeat nlWildPat)
403 compare_cons :: Pat Id -> Pat Id -> Bool
404 compare_cons (ConPatOut{ pat_con = L _ id1 }) (ConPatOut { pat_con = L _ id2 }) = id1 == id2
406 remove_dups :: [Pat Id] -> [Pat Id]
408 remove_dups (x:xs) | or (map (\y -> compare_cons x y) xs) = remove_dups xs
409 | otherwise = x : remove_dups xs
411 get_used_cons :: [(EqnNo, EquationInfo)] -> [Pat Id]
412 get_used_cons qs = remove_dups [pat | q <- qs, let pat = firstPatN q,
415 isConPatOut :: Pat Id -> Bool
416 isConPatOut (ConPatOut {}) = True
417 isConPatOut _ = False
419 remove_dups' :: [HsLit] -> [HsLit]
421 remove_dups' (x:xs) | x `elem` xs = remove_dups' xs
422 | otherwise = x : remove_dups' xs
425 get_used_lits :: [(EqnNo, EquationInfo)] -> [HsLit]
426 get_used_lits qs = remove_dups' all_literals
428 all_literals = get_used_lits' qs
430 get_used_lits' :: [(EqnNo, EquationInfo)] -> [HsLit]
431 get_used_lits' [] = []
432 get_used_lits' (q:qs)
433 | Just lit <- get_lit (firstPatN q) = lit : get_used_lits' qs
434 | otherwise = get_used_lits qs
436 get_lit :: Pat id -> Maybe HsLit
437 -- Get a representative HsLit to stand for the OverLit
438 -- It doesn't matter which one, because they will only be compared
439 -- with other HsLits gotten in the same way
440 get_lit (LitPat lit) = Just lit
441 get_lit (NPat (OverLit { ol_val = HsIntegral i}) mb _) = Just (HsIntPrim (mb_neg mb i))
442 get_lit (NPat (OverLit { ol_val = HsFractional f }) mb _) = Just (HsFloatPrim (mb_neg mb f))
443 get_lit (NPat (OverLit { ol_val = HsIsString s }) _ _) = Just (HsStringPrim s)
446 mb_neg :: Num a => Maybe b -> a -> a
448 mb_neg (Just _) v = -v
450 get_unused_cons :: [Pat Id] -> [DataCon]
451 get_unused_cons used_cons = ASSERT( not (null used_cons) ) unused_cons
453 used_set :: UniqSet DataCon
454 used_set = mkUniqSet [d | ConPatOut{ pat_con = L _ d} <- used_cons]
455 (ConPatOut { pat_ty = ty }) = head used_cons
456 Just (ty_con, inst_tys) = splitTyConApp_maybe ty
457 unused_cons = filterOut is_used (tyConDataCons ty_con)
458 is_used con = con `elementOfUniqSet` used_set
459 || dataConCannotMatch inst_tys con
461 all_vars :: [Pat Id] -> Bool
463 all_vars (WildPat _:ps) = all_vars ps
466 remove_var :: (EqnNo, EquationInfo) -> (EqnNo, EquationInfo)
467 remove_var (n, eqn@(EqnInfo { eqn_pats = WildPat _ : ps})) = (n, eqn { eqn_pats = ps })
468 remove_var _ = panic "Check.remove_var: equation does not begin with a variable"
470 -----------------------
471 eqnPats :: (EqnNo, EquationInfo) -> [Pat Id]
472 eqnPats (_, eqn) = eqn_pats eqn
474 okGroup :: [(EqnNo, EquationInfo)] -> Bool
475 -- True if all equations have at least one pattern, and
476 -- all have the same number of patterns
478 okGroup (e:es) = n_pats > 0 && and [length (eqnPats e) == n_pats | e <- es]
480 n_pats = length (eqnPats e)
483 pprGroup :: [(EqnNo, EquationInfo)] -> SDoc
484 pprEqnInfo :: (EqnNo, EquationInfo) -> SDoc
485 pprGroup es = vcat (map pprEqnInfo es)
486 pprEqnInfo e = ppr (eqnPats e)
489 firstPatN :: (EqnNo, EquationInfo) -> Pat Id
490 firstPatN (_, eqn) = firstPat eqn
492 is_con :: Pat Id -> Bool
493 is_con (ConPatOut {}) = True
496 is_lit :: Pat Id -> Bool
497 is_lit (LitPat _) = True
498 is_lit (NPat _ _ _) = True
501 is_var :: Pat Id -> Bool
502 is_var (WildPat _) = True
505 is_var_con :: DataCon -> Pat Id -> Bool
506 is_var_con _ (WildPat _) = True
507 is_var_con con (ConPatOut{ pat_con = L _ id }) | id == con = True
508 is_var_con _ _ = False
510 is_var_lit :: HsLit -> Pat Id -> Bool
511 is_var_lit _ (WildPat _) = True
513 | Just lit' <- get_lit pat = lit == lit'
517 The difference beteewn @make_con@ and @make_whole_con@ is that
518 @make_wole_con@ creates a new constructor with all their arguments, and
519 @make_con@ takes a list of argumntes, creates the contructor getting their
520 arguments from the list. See where \fbox{\ ???\ } are used for details.
522 We need to reconstruct the patterns (make the constructors infix and
523 similar) at the same time that we create the constructors.
525 You can tell tuple constructors using
529 You can see if one constructor is infix with this clearer code :-))))))))))
531 Lex.isLexConSym (Name.occNameString (Name.getOccName con))
534 Rather clumsy but it works. (Simon Peyton Jones)
537 We don't mind the @nilDataCon@ because it doesn't change the way to
538 print the messsage, we are searching only for things like: @[1,2,3]@,
541 In @reconstruct_pat@ we want to ``undo'' the work
542 that we have done in @simplify_pat@.
545 @((,) x y)@ & returns to be & @(x, y)@
546 \\ @((:) x xs)@ & returns to be & @(x:xs)@
547 \\ @(x:(...:[])@ & returns to be & @[x,...]@
550 The difficult case is the third one becouse we need to follow all the
551 contructors until the @[]@ to know that we need to use the second case,
552 not the second. \fbox{\ ???\ }
555 isInfixCon :: DataCon -> Bool
556 isInfixCon con = isDataSymOcc (getOccName con)
558 is_nil :: Pat Name -> Bool
559 is_nil (ConPatIn con (PrefixCon [])) = unLoc con == getName nilDataCon
562 is_list :: Pat Name -> Bool
563 is_list (ListPat _ _) = True
566 return_list :: DataCon -> Pat Name -> Bool
567 return_list id q = id == consDataCon && (is_nil q || is_list q)
569 make_list :: LPat Name -> Pat Name -> Pat Name
570 make_list p q | is_nil q = ListPat [p] placeHolderType
571 make_list p (ListPat ps ty) = ListPat (p:ps) ty
572 make_list _ _ = panic "Check.make_list: Invalid argument"
574 make_con :: Pat Id -> ExhaustivePat -> ExhaustivePat
575 make_con (ConPatOut{ pat_con = L _ id }) (lp:lq:ps, constraints)
576 | return_list id q = (noLoc (make_list lp q) : ps, constraints)
577 | isInfixCon id = (nlInfixConPat (getName id) lp lq : ps, constraints)
580 make_con (ConPatOut{ pat_con = L _ id, pat_args = PrefixCon pats, pat_ty = ty }) (ps, constraints)
581 | isTupleTyCon tc = (noLoc (TuplePat pats_con (tupleTyConBoxity tc) ty) : rest_pats, constraints)
582 | isPArrFakeCon id = (noLoc (PArrPat pats_con placeHolderType) : rest_pats, constraints)
583 | otherwise = (nlConPat name pats_con : rest_pats, constraints)
586 (pats_con, rest_pats) = splitAtList pats ps
589 -- reconstruct parallel array pattern
591 -- * don't check for the type only; we need to make sure that we are really
592 -- dealing with one of the fake constructors and not with the real
595 make_whole_con :: DataCon -> WarningPat
596 make_whole_con con | isInfixCon con = nlInfixConPat name nlWildPat nlWildPat
597 | otherwise = nlConPat name pats
600 pats = [nlWildPat | _ <- dataConOrigArgTys con]
603 This equation makes the same thing as @tidy@ in @Match.lhs@, the
604 difference is that here we can do all the tidy in one place and in the
605 @Match@ tidy it must be done one column each time due to bookkeeping
610 simplify_eqn :: EquationInfo -> EquationInfo
611 simplify_eqn eqn = eqn { eqn_pats = map simplify_pat (eqn_pats eqn),
612 eqn_rhs = simplify_rhs (eqn_rhs eqn) }
614 -- Horrible hack. The simplify_pat stuff converts NPlusK pats to WildPats
615 -- which of course loses the info that they can fail to match. So we
616 -- stick in a CanFail as if it were a guard.
617 -- The Right Thing to do is for the whole system to treat NPlusK pats properly
618 simplify_rhs (MatchResult can_fail body)
619 | any has_nplusk_pat (eqn_pats eqn) = MatchResult CanFail body
620 | otherwise = MatchResult can_fail body
622 has_nplusk_lpat :: LPat Id -> Bool
623 has_nplusk_lpat (L _ p) = has_nplusk_pat p
625 has_nplusk_pat :: Pat Id -> Bool
626 has_nplusk_pat (NPlusKPat _ _ _ _) = True
627 has_nplusk_pat (ParPat p) = has_nplusk_lpat p
628 has_nplusk_pat (AsPat _ p) = has_nplusk_lpat p
629 has_nplusk_pat (ViewPat _ p _) = has_nplusk_lpat p
630 has_nplusk_pat (SigPatOut p _ ) = has_nplusk_lpat p
631 has_nplusk_pat (ListPat ps _) = any has_nplusk_lpat ps
632 has_nplusk_pat (TuplePat ps _ _) = any has_nplusk_lpat ps
633 has_nplusk_pat (PArrPat ps _) = any has_nplusk_lpat ps
634 has_nplusk_pat (LazyPat _) = False -- Why?
635 has_nplusk_pat (BangPat p) = has_nplusk_lpat p -- I think
636 has_nplusk_pat (ConPatOut { pat_args = ps }) = any has_nplusk_lpat (hsConPatArgs ps)
637 has_nplusk_pat _ = False -- VarPat, VarPatOut, WildPat, LitPat, NPat, TypePat
639 simplify_lpat :: LPat Id -> LPat Id
640 simplify_lpat p = fmap simplify_pat p
642 simplify_pat :: Pat Id -> Pat Id
643 simplify_pat pat@(WildPat _) = pat
644 simplify_pat (VarPat id) = WildPat (idType id)
645 simplify_pat (VarPatOut id _) = WildPat (idType id) -- Ignore the bindings
646 simplify_pat (ParPat p) = unLoc (simplify_lpat p)
647 simplify_pat (LazyPat p) = WildPat (hsLPatType p) -- For overlap and exhaustiveness checking
648 -- purposes, a ~pat is like a wildcard
649 simplify_pat (BangPat p) = unLoc (simplify_lpat p)
650 simplify_pat (AsPat _ p) = unLoc (simplify_lpat p)
652 simplify_pat (ViewPat expr p ty) = ViewPat expr (simplify_lpat p) ty
654 simplify_pat (SigPatOut p _) = unLoc (simplify_lpat p) -- I'm not sure this is right
656 simplify_pat pat@(ConPatOut { pat_con = L _ id, pat_args = ps })
657 = pat { pat_args = simplify_con id ps }
659 simplify_pat (ListPat ps ty) =
660 unLoc $ foldr (\ x y -> mkPrefixConPat consDataCon [x,y] list_ty)
662 (map simplify_lpat ps)
663 where list_ty = mkListTy ty
665 -- introduce fake parallel array constructors to be able to handle parallel
666 -- arrays with the existing machinery for constructor pattern
668 simplify_pat (PArrPat ps ty)
669 = unLoc $ mkPrefixConPat (parrFakeCon (length ps))
670 (map simplify_lpat ps)
673 simplify_pat (TuplePat ps boxity ty)
674 = unLoc $ mkPrefixConPat (tupleCon boxity arity)
675 (map simplify_lpat ps) ty
679 -- unpack string patterns fully, so we can see when they overlap with
680 -- each other, or even explicit lists of Chars.
681 simplify_pat (LitPat (HsString s)) =
682 unLoc $ foldr (\c pat -> mkPrefixConPat consDataCon [mk_char_lit c, pat] stringTy)
683 (mkPrefixConPat nilDataCon [] stringTy) (unpackFS s)
685 mk_char_lit c = mkPrefixConPat charDataCon [nlLitPat (HsCharPrim c)] charTy
687 simplify_pat (LitPat lit) = tidyLitPat lit
688 simplify_pat (NPat lit mb_neg eq) = tidyNPat lit mb_neg eq
690 simplify_pat (NPlusKPat id _ _ _)
691 = WildPat (idType (unLoc id))
693 simplify_pat (CoPat _ pat _) = simplify_pat pat
696 simplify_con :: DataCon -> HsConPatDetails Id -> HsConPatDetails Id
697 simplify_con _ (PrefixCon ps) = PrefixCon (map simplify_lpat ps)
698 simplify_con _ (InfixCon p1 p2) = PrefixCon [simplify_lpat p1, simplify_lpat p2]
699 simplify_con con (RecCon (HsRecFields fs _))
700 | null fs = PrefixCon [nlWildPat | _ <- dataConOrigArgTys con]
701 -- Special case for null patterns; maybe not a record at all
702 | otherwise = PrefixCon (map (simplify_lpat.snd) all_pats)
704 -- pad out all the missing fields with WildPats.
705 field_pats = map (\ f -> (f, nlWildPat)) (dataConFieldLabels con)
706 all_pats = foldr (\(HsRecField id p _) acc -> insertNm (getName (unLoc id)) p acc)
709 insertNm nm p [] = [(nm,p)]
710 insertNm nm p (x@(n,_):xs)
711 | nm == n = (nm,p):xs
712 | otherwise = x : insertNm nm p xs