[project @ 2003-10-21 12:54:17 by simonpj]
[ghc-hetmet.git] / ghc / compiler / rename / RnTypes.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[RnSource]{Main pass of renamer}
5
6 \begin{code}
7 module RnTypes ( rnHsType, rnContext, 
8                  rnHsSigType, rnHsTypeFVs, rnHsSigTypeFVs, 
9                  rnPat, rnPats, rnPatsAndThen,  -- Here because it's not part 
10                  rnOverLit, litFVs,             -- of any mutual recursion      
11                  precParseErr, sectionPrecErr, dupFieldErr, patSigErr, checkTupSize
12   ) where
13
14 import CmdLineOpts      ( DynFlag(Opt_WarnUnusedMatches, Opt_GlasgowExts) )
15
16 import HsSyn
17 import RdrHsSyn ( RdrNameContext, RdrNameHsType, RdrNamePat,
18                   extractHsRhoRdrTyVars )
19 import RnHsSyn  ( RenamedContext, RenamedHsType, RenamedPat,
20                   extractHsTyNames, 
21                   parrTyCon_name, tupleTyCon_name, listTyCon_name, charTyCon_name )
22 import RnEnv    ( lookupOccRn, lookupBndrRn, lookupSyntaxName, lookupGlobalOccRn,
23                   bindTyVarsRn, lookupFixityRn, mapFvRn, newIPNameRn,
24                   bindPatSigTyVarsFV, bindLocalsFV, warnUnusedMatches )
25 import TcRnMonad
26 import RdrName  ( elemLocalRdrEnv )
27 import PrelNames( eqStringName, eqClassName, integralClassName, 
28                   negateName, minusName, lengthPName, indexPName, plusIntegerName, fromIntegerName,
29                   timesIntegerName, ratioDataConName, fromRationalName )
30 import Constants        ( mAX_TUPLE_SIZE )
31 import TysWiredIn       ( intTyCon )
32 import TysPrim          ( charPrimTyCon, addrPrimTyCon, intPrimTyCon, 
33                           floatPrimTyCon, doublePrimTyCon )
34 import Name     ( Name, NamedThing(..) )
35 import NameSet
36
37 import Literal          ( inIntRange, inCharRange )
38 import BasicTypes       ( compareFixity )
39 import ListSetOps       ( removeDups )
40 import Outputable
41
42 #include "HsVersions.h"
43 \end{code}
44
45 These type renamers are in a separate module, rather than in (say) RnSource,
46 to break several loop.
47
48 %*********************************************************
49 %*                                                      *
50 \subsection{Renaming types}
51 %*                                                      *
52 %*********************************************************
53
54 \begin{code}
55 rnHsTypeFVs :: SDoc -> RdrNameHsType -> RnM (RenamedHsType, FreeVars)
56 rnHsTypeFVs doc_str ty 
57   = rnHsType doc_str ty         `thenM` \ ty' ->
58     returnM (ty', extractHsTyNames ty')
59
60 rnHsSigTypeFVs :: SDoc -> RdrNameHsType -> RnM (RenamedHsType, FreeVars)
61 rnHsSigTypeFVs doc_str ty
62   = rnHsSigType doc_str ty      `thenM` \ ty' ->
63     returnM (ty', extractHsTyNames ty')
64
65 rnHsSigType :: SDoc -> RdrNameHsType -> RnM RenamedHsType
66         -- rnHsSigType is used for source-language type signatures,
67         -- which use *implicit* universal quantification.
68 rnHsSigType doc_str ty
69   = rnHsType (text "In the type signature for" <+> doc_str) ty
70 \end{code}
71
72 rnHsType is here because we call it from loadInstDecl, and I didn't
73 want a gratuitous knot.
74
75 \begin{code}
76 rnHsType :: SDoc -> RdrNameHsType -> RnM RenamedHsType
77
78 rnHsType doc (HsForAllTy Implicit _ ctxt ty)
79         -- Implicit quantifiction in source code (no kinds on tyvars)
80         -- Given the signature  C => T  we universally quantify 
81         -- over FV(T) \ {in-scope-tyvars} 
82   = getLocalRdrEnv              `thenM` \ name_env ->
83     let
84         mentioned = extractHsRhoRdrTyVars ctxt ty
85
86         -- Don't quantify over type variables that are in scope;
87         -- when GlasgowExts is off, there usually won't be any, except for
88         -- class signatures:
89         --      class C a where { op :: a -> a }
90         forall_tyvars = filter (not . (`elemLocalRdrEnv` name_env)) mentioned
91     in
92     rnForAll doc Implicit (map UserTyVar forall_tyvars) ctxt ty
93
94 rnHsType doc (HsForAllTy Explicit forall_tyvars ctxt tau)
95         -- Explicit quantification.
96         -- Check that the forall'd tyvars are actually 
97         -- mentioned in the type, and produce a warning if not
98   = let
99         mentioned          = extractHsRhoRdrTyVars ctxt tau
100         forall_tyvar_names = hsTyVarNames forall_tyvars
101
102         -- Explicitly quantified but not mentioned in ctxt or tau
103         warn_guys = filter (`notElem` mentioned) forall_tyvar_names
104     in
105     mappM_ (forAllWarn doc tau) warn_guys       `thenM_`
106     rnForAll doc Explicit forall_tyvars ctxt tau
107
108 rnHsType doc (HsTyVar tyvar)
109   = lookupOccRn tyvar           `thenM` \ tyvar' ->
110     returnM (HsTyVar tyvar')
111
112 rnHsType doc (HsOpTy ty1 op ty2)
113   = lookupOccRn op              `thenM` \ op' ->
114     rnHsType doc ty1            `thenM` \ ty1' ->
115     rnHsType doc ty2            `thenM` \ ty2' -> 
116     lookupTyFixityRn op'        `thenM` \ fix ->
117     mkHsOpTyRn op' fix ty1' ty2'
118
119 rnHsType doc (HsParTy ty)
120   = rnHsType doc ty             `thenM` \ ty' ->
121     returnM (HsParTy ty')
122
123 rnHsType doc (HsNumTy i)
124   | i == 1    = returnM (HsNumTy i)
125   | otherwise = addErr err_msg  `thenM_`  returnM (HsNumTy i)
126   where
127     err_msg = ptext SLIT("Only unit numeric type pattern is valid")
128                            
129
130 rnHsType doc (HsFunTy ty1 ty2)
131   = rnHsType doc ty1    `thenM` \ ty1' ->
132         -- Might find a for-all as the arg of a function type
133     rnHsType doc ty2    `thenM` \ ty2' ->
134         -- Or as the result.  This happens when reading Prelude.hi
135         -- when we find return :: forall m. Monad m -> forall a. a -> m a
136     returnM (HsFunTy ty1' ty2')
137
138 rnHsType doc (HsListTy ty)
139   = rnHsType doc ty                             `thenM` \ ty' ->
140     returnM (HsListTy ty')
141
142 rnHsType doc (HsKindSig ty k)
143   = rnHsType doc ty                             `thenM` \ ty' ->
144     returnM (HsKindSig ty' k)
145
146 rnHsType doc (HsPArrTy ty)
147   = rnHsType doc ty                             `thenM` \ ty' ->
148     returnM (HsPArrTy ty')
149
150 -- Unboxed tuples are allowed to have poly-typed arguments.  These
151 -- sometimes crop up as a result of CPR worker-wrappering dictionaries.
152 rnHsType doc (HsTupleTy tup_con tys)
153   = mappM (rnHsType doc) tys            `thenM` \ tys' ->
154     returnM (HsTupleTy tup_con tys')
155
156 rnHsType doc (HsAppTy ty1 ty2)
157   = rnHsType doc ty1            `thenM` \ ty1' ->
158     rnHsType doc ty2            `thenM` \ ty2' ->
159     returnM (HsAppTy ty1' ty2')
160
161 rnHsType doc (HsPredTy pred)
162   = rnPred doc pred     `thenM` \ pred' ->
163     returnM (HsPredTy pred')
164
165 rnHsTypes doc tys = mappM (rnHsType doc) tys
166 \end{code}
167
168
169 \begin{code}
170 rnForAll doc exp forall_tyvars ctxt ty
171   = bindTyVarsRn doc forall_tyvars      $ \ new_tyvars ->
172     rnContext doc ctxt                  `thenM` \ new_ctxt ->
173     rnHsType doc ty                     `thenM` \ new_ty ->
174     returnM (HsForAllTy exp new_tyvars new_ctxt new_ty)
175 \end{code}
176
177
178 %*********************************************************
179 %*                                                      *
180 \subsection{Fixities}
181 %*                                                      *
182 %*********************************************************
183
184 Infix types are read in a *right-associative* way, so that
185         a `op` b `op` c
186 is always read in as
187         a `op` (b `op` c)
188
189 mkHsOpTyRn rearranges where necessary.  The two arguments
190 have already been renamed and rearranged.  It's made rather tiresome
191 by the presence of ->
192
193 \begin{code}
194 lookupTyFixityRn n 
195   = doptM Opt_GlasgowExts                       `thenM` \ glaExts ->
196     warnIf (not glaExts) (infixTyConWarn n)     `thenM_`
197     lookupFixityRn n
198
199 -- Building (ty1 `op1` (ty21 `op2` ty22))
200 mkHsOpTyRn :: Name -> Fixity 
201            -> RenamedHsType -> RenamedHsType 
202            -> RnM RenamedHsType
203
204 mkHsOpTyRn op1 fix1 ty1 ty2@(HsOpTy ty21 op2 ty22)
205   = lookupTyFixityRn op2                `thenM` \ fix2 ->
206     let
207         (nofix_error, associate_right) = compareFixity fix1 fix2
208     in
209     if nofix_error then
210         addErr (precParseErr (quotes (ppr op1),fix1) 
211                                (quotes (ppr op2),fix2)) `thenM_`
212         returnM (HsOpTy ty1 op1 ty2)
213     else 
214     if not associate_right then
215         -- Rearrange to ((ty1 `op1` ty21) `op2` ty22)
216         mkHsOpTyRn op1 fix1 ty1 ty21            `thenM` \ new_ty ->
217         returnM (HsOpTy new_ty op2 ty22)
218     else
219     returnM (HsOpTy ty1 op1 ty2)
220
221 mkHsOpTyRn op fix ty1 ty2                       -- Default case, no rearrangment
222   = returnM (HsOpTy ty1 op ty2)
223 \end{code}
224
225 %*********************************************************
226 %*                                                      *
227 \subsection{Contexts and predicates}
228 %*                                                      *
229 %*********************************************************
230
231 \begin{code}
232 rnContext :: SDoc -> RdrNameContext -> RnM RenamedContext
233 rnContext doc ctxt = mappM (rnPred doc) ctxt
234
235 rnPred doc (HsClassP clas tys)
236   = lookupOccRn clas            `thenM` \ clas_name ->
237     rnHsTypes doc tys           `thenM` \ tys' ->
238     returnM (HsClassP clas_name tys')
239
240 rnPred doc (HsIParam n ty)
241   = newIPNameRn n               `thenM` \ name ->
242     rnHsType doc ty             `thenM` \ ty' ->
243     returnM (HsIParam name ty')
244 \end{code}
245
246
247 *********************************************************
248 *                                                       *
249 \subsection{Patterns}
250 *                                                       *
251 *********************************************************
252
253 \begin{code}
254 rnPatsAndThen :: HsMatchContext Name
255               -> Bool
256               -> [RdrNamePat] 
257               -> ([RenamedPat] -> RnM (a, FreeVars))
258               -> RnM (a, FreeVars)
259 -- Bring into scope all the binders and type variables
260 -- bound by the patterns; then rename the patterns; then
261 -- do the thing inside.
262 --
263 -- Note that we do a single bindLocalsRn for all the
264 -- matches together, so that we spot the repeated variable in
265 --      f x x = 1
266
267 rnPatsAndThen ctxt repUnused pats thing_inside
268   = bindPatSigTyVarsFV pat_sig_tys      $
269     bindLocalsFV doc_pat bndrs          $ \ new_bndrs ->
270     rnPats pats                         `thenM` \ (pats', pat_fvs) ->
271     thing_inside pats'                  `thenM` \ (res, res_fvs) ->
272
273     let
274         unused_binders = filter (not . (`elemNameSet` res_fvs)) new_bndrs
275     in
276     (if repUnused
277      then warnUnusedMatches unused_binders
278      else returnM ())                  `thenM_`
279     returnM (res, res_fvs `plusFV` pat_fvs)
280   where
281     pat_sig_tys = collectSigTysFromPats pats
282     bndrs       = collectPatsBinders    pats
283     doc_pat     = ptext SLIT("In") <+> pprMatchContext ctxt
284
285 rnPats :: [RdrNamePat] -> RnM ([RenamedPat], FreeVars)
286 rnPats ps = mapFvRn rnPat ps
287
288 rnPat :: RdrNamePat -> RnM (RenamedPat, FreeVars)
289
290 rnPat (WildPat _) = returnM (WildPat placeHolderType, emptyFVs)
291
292 rnPat (VarPat name)
293   = lookupBndrRn  name                  `thenM` \ vname ->
294     returnM (VarPat vname, emptyFVs)
295
296 rnPat (SigPatIn pat ty)
297   = doptM Opt_GlasgowExts `thenM` \ glaExts ->
298     
299     if glaExts
300     then rnPat pat              `thenM` \ (pat', fvs1) ->
301          rnHsTypeFVs doc ty     `thenM` \ (ty',  fvs2) ->
302          returnM (SigPatIn pat' ty', fvs1 `plusFV` fvs2)
303
304     else addErr (patSigErr ty)  `thenM_`
305          rnPat pat
306   where
307     doc = text "In a pattern type-signature"
308     
309 rnPat (LitPat s@(HsString _)) 
310   = returnM (LitPat s, unitFV eqStringName)
311
312 rnPat (LitPat lit) 
313   = litFVs lit          `thenM` \ fvs ->
314     returnM (LitPat lit, fvs) 
315
316 rnPat (NPatIn lit mb_neg) 
317   = rnOverLit lit                       `thenM` \ (lit', fvs1) ->
318     (case mb_neg of
319         Nothing -> returnM (Nothing, emptyFVs)
320         Just _  -> lookupSyntaxName negateName  `thenM` \ (neg, fvs) ->
321                    returnM (Just neg, fvs)
322     )                                   `thenM` \ (mb_neg', fvs2) ->
323     returnM (NPatIn lit' mb_neg', 
324               fvs1 `plusFV` fvs2 `addOneFV` eqClassName)        
325         -- Needed to find equality on pattern
326
327 rnPat (NPlusKPatIn name lit _)
328   = rnOverLit lit                       `thenM` \ (lit', fvs1) ->
329     lookupBndrRn name                   `thenM` \ name' ->
330     lookupSyntaxName minusName          `thenM` \ (minus, fvs2) ->
331     returnM (NPlusKPatIn name' lit' minus, 
332               fvs1 `plusFV` fvs2 `addOneFV` integralClassName)
333         -- The Report says that n+k patterns must be in Integral
334
335 rnPat (LazyPat pat)
336   = rnPat pat           `thenM` \ (pat', fvs) ->
337     returnM (LazyPat pat', fvs)
338
339 rnPat (AsPat name pat)
340   = rnPat pat           `thenM` \ (pat', fvs) ->
341     lookupBndrRn name   `thenM` \ vname ->
342     returnM (AsPat vname pat', fvs)
343
344 rnPat (ConPatIn con stuff) = rnConPat con stuff
345
346
347 rnPat (ParPat pat)
348   = rnPat pat           `thenM` \ (pat', fvs) ->
349     returnM (ParPat pat', fvs)
350
351 rnPat (ListPat pats _)
352   = rnPats pats                 `thenM` \ (patslist, fvs) ->
353     returnM (ListPat patslist placeHolderType, fvs `addOneFV` listTyCon_name)
354
355 rnPat (PArrPat pats _)
356   = rnPats pats                 `thenM` \ (patslist, fvs) ->
357     returnM (PArrPat patslist placeHolderType, 
358               fvs `plusFV` implicit_fvs `addOneFV` parrTyCon_name)
359   where
360     implicit_fvs = mkFVs [lengthPName, indexPName]
361
362 rnPat (TuplePat pats boxed)
363   = checkTupSize tup_size       `thenM_`
364     rnPats pats                 `thenM` \ (patslist, fvs) ->
365     returnM (TuplePat patslist boxed, fvs `addOneFV` tycon_name)
366   where
367     tup_size   = length pats
368     tycon_name = tupleTyCon_name boxed tup_size
369
370 rnPat (TypePat name) =
371     rnHsTypeFVs (text "In a type pattern") name `thenM` \ (name', fvs) ->
372     returnM (TypePat name', fvs)
373
374 ------------------------------
375 rnConPat con (PrefixCon pats)
376   = lookupOccRn con     `thenM` \ con' ->
377     rnPats pats         `thenM` \ (pats', fvs) ->
378     returnM (ConPatIn con' (PrefixCon pats'), fvs `addOneFV` con')
379
380 rnConPat con (RecCon rpats)
381   = lookupOccRn con     `thenM` \ con' ->
382     rnRpats rpats       `thenM` \ (rpats', fvs) ->
383     returnM (ConPatIn con' (RecCon rpats'), fvs `addOneFV` con')
384
385 rnConPat con (InfixCon pat1 pat2)
386   = lookupOccRn con                             `thenM` \ con' ->
387     rnPat pat1                                  `thenM` \ (pat1', fvs1) ->
388     rnPat pat2                                  `thenM` \ (pat2', fvs2) ->
389     lookupFixityRn con'                         `thenM` \ fixity ->
390     mkConOpPatRn con' fixity pat1' pat2'        `thenM` \ pat' ->
391     returnM (pat', fvs1 `plusFV` fvs2 `addOneFV` con')
392
393 ------------------------
394 rnRpats rpats
395   = mappM_ field_dup_err dup_fields     `thenM_`
396     mapFvRn rn_rpat rpats               `thenM` \ (rpats', fvs) ->
397     returnM (rpats', fvs)
398   where
399     (_, dup_fields) = removeDups compare [ f | (f,_) <- rpats ]
400
401     field_dup_err dups = addErr (dupFieldErr "pattern" dups)
402
403     rn_rpat (field, pat)
404       = lookupGlobalOccRn field `thenM` \ fieldname ->
405         rnPat pat               `thenM` \ (pat', fvs) ->
406         returnM ((fieldname, pat'), fvs `addOneFV` fieldname)
407 \end{code}
408
409 \begin{code}
410 mkConOpPatRn :: Name -> Fixity -> RenamedPat -> RenamedPat
411              -> RnM RenamedPat
412
413 mkConOpPatRn op2 fix2 p1@(ConPatIn op1 (InfixCon p11 p12)) p2
414   = lookupFixityRn op1          `thenM` \ fix1 ->
415     let
416         (nofix_error, associate_right) = compareFixity fix1 fix2
417     in
418     if nofix_error then
419         addErr (precParseErr (ppr_op op1,fix1) (ppr_op op2,fix2))       `thenM_`
420         returnM (ConPatIn op2 (InfixCon p1 p2))
421     else 
422     if associate_right then
423         mkConOpPatRn op2 fix2 p12 p2            `thenM` \ new_p ->
424         returnM (ConPatIn op1 (InfixCon p11 new_p))
425     else
426     returnM (ConPatIn op2 (InfixCon p1 p2))
427
428 mkConOpPatRn op fix p1 p2                       -- Default case, no rearrangment
429   = ASSERT( not_op_pat p2 )
430     returnM (ConPatIn op (InfixCon p1 p2))
431
432 not_op_pat (ConPatIn _ (InfixCon _ _)) = False
433 not_op_pat other                       = True
434 \end{code}
435
436
437 %************************************************************************
438 %*                                                                      *
439 \subsubsection{Literals}
440 %*                                                                      *
441 %************************************************************************
442
443 When literals occur we have to make sure
444 that the types and classes they involve
445 are made available.
446
447 \begin{code}
448 litFVs (HsChar c)
449    = checkErr (inCharRange c) (bogusCharError c) `thenM_`
450      returnM (unitFV charTyCon_name)
451
452 litFVs (HsCharPrim c)         = returnM (unitFV (getName charPrimTyCon))
453 litFVs (HsString s)           = returnM (mkFVs [listTyCon_name, charTyCon_name])
454 litFVs (HsStringPrim s)       = returnM (unitFV (getName addrPrimTyCon))
455 litFVs (HsInt i)              = returnM (unitFV (getName intTyCon))
456 litFVs (HsIntPrim i)          = returnM (unitFV (getName intPrimTyCon))
457 litFVs (HsFloatPrim f)        = returnM (unitFV (getName floatPrimTyCon))
458 litFVs (HsDoublePrim d)       = returnM (unitFV (getName doublePrimTyCon))
459 litFVs lit                    = pprPanic "RnExpr.litFVs" (ppr lit)      -- HsInteger and HsRat only appear 
460                                                                         -- in post-typechecker translations
461 bogusCharError c
462   = ptext SLIT("character literal out of range: '\\") <> int c <> char '\''
463
464 rnOverLit (HsIntegral i _)
465   = lookupSyntaxName fromIntegerName    `thenM` \ (from_integer_name, fvs) ->
466     if inIntRange i then
467         returnM (HsIntegral i from_integer_name, fvs)
468     else let
469         extra_fvs = mkFVs [plusIntegerName, timesIntegerName]
470         -- Big integer literals are built, using + and *, 
471         -- out of small integers (DsUtils.mkIntegerLit)
472         -- [NB: plusInteger, timesInteger aren't rebindable... 
473         --      they are used to construct the argument to fromInteger, 
474         --      which is the rebindable one.]
475     in
476     returnM (HsIntegral i from_integer_name, fvs `plusFV` extra_fvs)
477
478 rnOverLit (HsFractional i _)
479   = lookupSyntaxName fromRationalName           `thenM` \ (from_rat_name, fvs) ->
480     let
481         extra_fvs = mkFVs [ratioDataConName, plusIntegerName, timesIntegerName]
482         -- We have to make sure that the Ratio type is imported with
483         -- its constructor, because literals of type Ratio t are
484         -- built with that constructor.
485         -- The Rational type is needed too, but that will come in
486         -- as part of the type for fromRational.
487         -- The plus/times integer operations may be needed to construct the numerator
488         -- and denominator (see DsUtils.mkIntegerLit)
489     in
490     returnM (HsFractional i from_rat_name, fvs `plusFV` extra_fvs)
491 \end{code}
492
493
494
495 %*********************************************************
496 %*                                                      *
497 \subsection{Errors}
498 %*                                                      *
499 %*********************************************************
500
501 \begin{code}
502 checkTupSize :: Int -> RnM ()
503 checkTupSize tup_size
504   | tup_size <= mAX_TUPLE_SIZE 
505   = returnM ()
506   | otherwise                  
507   = addErr (sep [ptext SLIT("A") <+> int tup_size <> ptext SLIT("-tuple is too large for GHC"),
508                  nest 2 (parens (ptext SLIT("max size is") <+> int mAX_TUPLE_SIZE)),
509                  nest 2 (ptext SLIT("Workaround: use nested tuples or define a data type"))])
510
511 forAllWarn doc ty tyvar
512   = ifOptM Opt_WarnUnusedMatches        $
513     addWarn (sep [ptext SLIT("The universally quantified type variable") <+> quotes (ppr tyvar),
514                    nest 4 (ptext SLIT("does not appear in the type") <+> quotes (ppr ty))]
515                    $$
516                    doc
517                 )
518
519 precParseErr op1 op2 
520   = hang (ptext SLIT("precedence parsing error"))
521       4 (hsep [ptext SLIT("cannot mix"), ppr_opfix op1, ptext SLIT("and"), 
522                ppr_opfix op2,
523                ptext SLIT("in the same infix expression")])
524
525 sectionPrecErr op arg_op section
526  = vcat [ptext SLIT("The operator") <+> ppr_opfix op <+> ptext SLIT("of a section"),
527          nest 4 (ptext SLIT("must have lower precedence than the operand") <+> ppr_opfix arg_op),
528          nest 4 (ptext SLIT("in the section:") <+> quotes (ppr section))]
529
530 infixTyConWarn op
531   = ftext FSLIT("Accepting non-standard infix type constructor") <+> quotes (ppr op)
532
533 patSigErr ty
534   =  (ptext SLIT("Illegal signature in pattern:") <+> ppr ty)
535         $$ nest 4 (ptext SLIT("Use -fglasgow-exts to permit it"))
536
537 dupFieldErr str (dup:rest)
538   = hsep [ptext SLIT("duplicate field name"), 
539           quotes (ppr dup),
540           ptext SLIT("in record"), text str]
541
542 ppr_op op = quotes (ppr op)     -- Here, op can be a Name or a (Var n), where n is a Name
543 ppr_opfix (pp_op, fixity) = pp_op <+> brackets (ppr fixity)
544 \end{code}