2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[HsMatches]{Abstract syntax: matches and guarded right-hand-sides}
6 The @Match@, @GRHSsAndBinds@ and @GRHS@ datatypes.
9 #include "HsVersions.h"
11 module HsMatches where
15 import HsLoop ( HsExpr, nullBinds, HsBinds )
16 import Outputable ( ifPprShowAll )
19 import SrcLoc ( SrcLoc{-instances-} )
23 %************************************************************************
25 \subsection{@Match@, @GRHSsAndBinds@, and @GRHS@ datatypes}
27 %************************************************************************
29 @Match@es are sets of pattern bindings and right hand sides for
30 functions, patterns or case branches. For example, if a function @g@
36 then \tr{g} has two @Match@es: @(x,y) = y@ and @((x:ys),y) = y+1@.
38 It is always the case that each element of an @[Match]@ list has the
39 same number of @PatMatch@s inside it. This corresponds to saying that
40 a function defined by pattern matching must have the same number of
41 patterns in each equation.
44 data Match tyvar uvar id pat
46 (Match tyvar uvar id pat)
47 | GRHSMatch (GRHSsAndBinds tyvar uvar id pat)
50 Sets of guarded right hand sides (GRHSs). In:
55 a guarded right hand side is either
56 @(x==True = y)@, or @(otherwise = y*2)@.
58 For each match, there may be several guarded right hand
59 sides, as the definition of @f@ shows.
62 data GRHSsAndBinds tyvar uvar id pat
63 = GRHSsAndBindsIn [GRHS tyvar uvar id pat] -- at least one GRHS
64 (HsBinds tyvar uvar id pat)
66 | GRHSsAndBindsOut [GRHS tyvar uvar id pat] -- at least one GRHS
67 (HsBinds tyvar uvar id pat)
70 data GRHS tyvar uvar id pat
71 = GRHS (HsExpr tyvar uvar id pat) -- guard(ed)...
72 (HsExpr tyvar uvar id pat) -- ... right-hand side
75 | OtherwiseGRHS (HsExpr tyvar uvar id pat) -- guard-free
79 %************************************************************************
83 %************************************************************************
85 We know the list must have at least one @Match@ in it.
87 pprMatches :: (NamedThing id, Outputable id, Outputable pat,
88 Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar) =>
89 PprStyle -> (Bool, Pretty) -> [Match tyvar uvar id pat] -> Pretty
91 pprMatches sty print_info@(is_case, name) [match]
93 pprMatch sty is_case match
95 ppHang name 4 (pprMatch sty is_case match)
97 pprMatches sty print_info (match1 : rest)
98 = ppAbove (pprMatches sty print_info [match1])
99 (pprMatches sty print_info rest)
101 ---------------------------------------------
102 pprMatch :: (NamedThing id, Outputable id, Outputable pat,
103 Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar) =>
104 PprStyle -> Bool -> Match tyvar uvar id pat -> Pretty
106 pprMatch sty is_case first_match
107 = ppHang (ppSep (map (ppr sty) row_of_pats))
110 (row_of_pats, grhss_etc_stuff) = ppr_match sty is_case first_match
112 ppr_match sty is_case (PatMatch pat match)
113 = (pat:pats, grhss_stuff)
115 (pats, grhss_stuff) = ppr_match sty is_case match
117 ppr_match sty is_case (GRHSMatch grhss_n_binds)
118 = ([], pprGRHSsAndBinds sty is_case grhss_n_binds)
120 ----------------------------------------------------------
122 pprGRHSsAndBinds sty is_case (GRHSsAndBindsIn grhss binds)
123 = ppAbove (ppAboves (map (pprGRHS sty is_case) grhss))
124 (if (nullBinds binds)
126 else ppAboves [ ppStr "where", ppNest 4 (ppr sty binds) ])
128 pprGRHSsAndBinds sty is_case (GRHSsAndBindsOut grhss binds ty)
129 = ppAbove (ppAboves (map (pprGRHS sty is_case) grhss))
130 (if (nullBinds binds)
132 else ppAboves [ ifPprShowAll sty
133 (ppCat [ppStr "{- ty:", ppr sty ty, ppStr "-}"]),
134 ppStr "where", ppNest 4 (ppr sty binds) ])
136 ---------------------------------------------
137 pprGRHS :: (NamedThing id, Outputable id, Outputable pat,
138 Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
139 => PprStyle -> Bool -> GRHS tyvar uvar id pat -> Pretty
141 pprGRHS sty is_case (GRHS guard expr locn)
142 = ppHang (ppCat [ppChar '|', ppr sty guard, ppStr (if is_case then "->" else "=")])
145 pprGRHS sty is_case (OtherwiseGRHS expr locn)
146 = ppHang (ppStr (if is_case then "->" else "="))