2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1994
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
13 import AbsUniType ( UniType
14 IF_ATTACK_PRAGMAS(COMMA cmpUniType)
16 import HsBinds ( Binds, nullBinds )
17 import HsExpr ( Expr )
18 import HsPat ( ProtoNamePat(..), RenamedPat(..),
20 IF_ATTACK_PRAGMAS(COMMA typeOfPat)
23 import Unique ( Unique )
27 import ProtoName ( ProtoName(..) ) -- .. for pragmas only
28 import SrcLoc ( SrcLoc )
32 %************************************************************************
34 \subsection[AbsSyntax-Match]{@Match@}
36 %************************************************************************
38 Sets of pattern bindings and right hand sides for
39 functions, patterns or case branches. For example,
40 if a function @g@ is defined as:
45 then a single @Match@ would be either @(x,y) = y@ or
46 @((x:ys),y) = y+1@, and @[Match]@ would be
47 @[((x,y) = y), (((x:ys),y) = y+1)]@.
49 It is always the case that each element of an @[Match]@ list has the
50 same number of @PatMatch@s inside it. This corresponds to saying that
51 a function defined by pattern matching must have the same number of
52 patterns in each equation.
54 So, a single ``match'':
59 | GRHSMatch (GRHSsAndBinds bdee pat)
61 type ProtoNameMatch = Match ProtoName ProtoNamePat
62 type RenamedMatch = Match Name RenamedPat
63 type TypecheckedMatch = Match Id TypecheckedPat
66 Printing, of one and several @Matches@.
68 pprMatch :: (NamedThing bdee, Outputable bdee,
69 NamedThing pat, Outputable pat) =>
70 PprStyle -> Bool -> Match bdee pat -> Pretty
72 pprMatch sty is_case first_match
73 = ppHang (ppSep (map (ppr sty) row_of_pats))
76 (row_of_pats, grhss_etc_stuff) = ppr_match sty is_case first_match
78 ppr_match sty is_case (PatMatch pat match)
79 = (pat:pats, grhss_stuff)
81 (pats, grhss_stuff) = ppr_match sty is_case match
83 ppr_match sty is_case (GRHSMatch grhss_n_binds)
84 = ([], pprGRHSsAndBinds sty is_case grhss_n_binds)
87 We know the list must have at least one @Match@ in it.
89 pprMatches :: (NamedThing bdee, Outputable bdee,
90 NamedThing pat, Outputable pat) =>
91 PprStyle -> (Bool, Pretty) -> [Match bdee pat] -> Pretty
93 pprMatches sty print_info@(is_case, name) [match]
95 pprMatch sty is_case match
97 ppHang name 4 (pprMatch sty is_case match)
99 pprMatches sty print_info (match1 : rest)
100 = ppAbove (pprMatches sty print_info [match1])
101 (pprMatches sty print_info rest)
105 instance (NamedThing bdee, Outputable bdee,
106 NamedThing pat, Outputable pat) =>
107 Outputable (Match bdee pat) where
108 ppr sty b = panic "ppr: Match"
111 %************************************************************************
113 \subsection[AbsSyntax-GRHSsAndBinds]{Guarded RHSs plus their Binds}
115 %************************************************************************
117 Possibly \tr{NoGuardNoBinds{In,Out}}, etc.? ToDo
120 data GRHSsAndBinds bdee pat
121 = GRHSsAndBindsIn [GRHS bdee pat] -- at least one GRHS
124 | GRHSsAndBindsOut [GRHS bdee pat] -- at least one GRHS
128 type ProtoNameGRHSsAndBinds = GRHSsAndBinds ProtoName ProtoNamePat
129 type RenamedGRHSsAndBinds = GRHSsAndBinds Name RenamedPat
130 type TypecheckedGRHSsAndBinds = GRHSsAndBinds Id TypecheckedPat
134 pprGRHSsAndBinds sty is_case (GRHSsAndBindsIn grhss binds)
135 = ppAbove (ppAboves (map (pprGRHS sty is_case) grhss))
136 (if (nullBinds binds)
138 else ppAboves [ ppStr "where", ppNest 4 (ppr sty binds) ])
140 pprGRHSsAndBinds sty is_case (GRHSsAndBindsOut grhss binds ty)
141 = ppAbove (ppAboves (map (pprGRHS sty is_case) grhss))
142 (if (nullBinds binds)
144 else ppAboves [ ifPprShowAll sty
145 (ppCat [ppStr "{- ty:", ppr sty ty, ppStr "-}"]),
146 ppStr "where", ppNest 4 (ppr sty binds) ])
150 instance (NamedThing bdee, Outputable bdee,
151 NamedThing pat, Outputable pat) =>
152 Outputable (GRHSsAndBinds bdee pat) where
153 ppr sty b = panic "ppr:GRHSsAndBinds"
156 %************************************************************************
158 \subsection[AbsSyntax-GRHS]{A guarded right-hand-side}
160 %************************************************************************
162 Sets of guarded right hand sides. In
164 f (x,y) | x==True = y
167 a guarded right hand side is either
168 @(x==True = y)@, or @(otherwise = y*2)@.
170 For each match, there may be several guarded right hand
171 sides, as the definition of @f@ shows.
175 = GRHS (Expr bdee pat) -- guard(ed)...
176 (Expr bdee pat) -- ... right-hand side
179 | OtherwiseGRHS (Expr bdee pat) -- guard-free
185 type ProtoNameGRHS = GRHS ProtoName ProtoNamePat
186 type RenamedGRHS = GRHS Name RenamedPat
187 type TypecheckedGRHS = GRHS Id TypecheckedPat
191 pprGRHS :: (NamedThing bdee, Outputable bdee,
192 NamedThing pat, Outputable pat) =>
193 PprStyle -> Bool -> GRHS bdee pat -> Pretty
195 pprGRHS sty is_case (GRHS guard expr locn)
197 ifPprShowAll sty (ppr sty locn),
198 ppHang (ppCat [ppStr "|", ppr sty guard, ppStr (if is_case then "->" else "=")])
202 pprGRHS sty is_case (OtherwiseGRHS expr locn)
204 ifPprShowAll sty (ppr sty locn),
205 ppHang (ppStr (if is_case then "->" else "="))
211 instance (NamedThing bdee, Outputable bdee,
212 NamedThing pat, Outputable pat) =>
213 Outputable (GRHS bdee pat) where
214 ppr sty b = panic "ppr: GRHSs"