2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[TcGenDeriv]{Generating derived instance declarations}
6 This module is nominally ``subordinate'' to @TcDeriv@, which is the
7 ``official'' interface to deriving-related things.
9 This is where we do all the grimy bindings' generation.
12 #include "HsVersions.h"
23 gen_tag_n_con_monobind,
25 con2tag_RDR, tag2con_RDR, maxtag_RDR,
31 IMPORT_1_3(List(partition))
33 import HsSyn ( HsBinds(..), MonoBinds(..), Match(..), GRHSsAndBinds(..),
34 GRHS(..), HsExpr(..), HsLit(..), InPat(..), Stmt(..), DoOrListComp(..),
35 SYN_IE(RecFlag), recursive,
36 ArithSeqInfo, Sig, HsType, FixityDecl, Fixity, Fake )
37 import RdrHsSyn ( RdrName(..), varQual, varUnqual, mkOpApp,
38 SYN_IE(RdrNameMonoBinds), SYN_IE(RdrNameHsExpr), SYN_IE(RdrNamePat)
40 import BasicTypes ( IfaceFlavour(..) )
41 import Id ( GenId, isNullaryDataCon, dataConTag,
42 dataConRawArgTys, fIRST_TAG,
43 isDataCon, SYN_IE(DataCon), SYN_IE(ConTag),
45 import Maybes ( maybeToBool )
46 import Name ( getOccString, getOccName, getSrcLoc, occNameString, modAndOcc, OccName, Name )
48 import PrimOp ( PrimOp(..) )
49 import PrelInfo -- Lots of RdrNames
50 import SrcLoc ( mkGeneratedSrcLoc, SrcLoc )
51 import TyCon ( TyCon, isNewTyCon, tyConDataCons, isEnumerationTyCon, maybeTyConSingleCon )
52 import Type ( eqTy, isPrimType, SYN_IE(Type) )
53 import TysPrim ( charPrimTy, intPrimTy, wordPrimTy, addrPrimTy,
54 floatPrimTy, doublePrimTy
56 import Util ( mapAccumL, zipEqual, zipWith3Equal, nOfThem, panic, assertPanic )
59 %************************************************************************
61 \subsection{Generating code, by derivable class}
63 %************************************************************************
65 %************************************************************************
67 \subsubsection{Generating @Eq@ instance declarations}
69 %************************************************************************
71 Here are the heuristics for the code we generate for @Eq@:
74 Let's assume we have a data type with some (possibly zero) nullary
75 data constructors and some ordinary, non-nullary ones (the rest,
76 also possibly zero of them). Here's an example, with both \tr{N}ullary
77 and \tr{O}rdinary data cons.
79 data Foo ... = N1 | N2 ... | Nn | O1 a b | O2 Int | O3 Double b b | ...
83 For the ordinary constructors (if any), we emit clauses to do The
87 (==) (O1 a1 b1) (O1 a2 b2) = a1 == a2 && b1 == b2
88 (==) (O2 a1) (O2 a2) = a1 == a2
89 (==) (O3 a1 b1 c1) (O3 a2 b2 c2) = a1 == a2 && b1 == b2 && c1 == c2
92 Note: if we're comparing unboxed things, e.g., if \tr{a1} and
93 \tr{a2} are \tr{Float#}s, then we have to generate
95 case (a1 `eqFloat#` a2) of
98 for that particular test.
101 If there are any nullary constructors, we emit a catch-all clause of
105 (==) a b = case (con2tag_Foo a) of { a# ->
106 case (con2tag_Foo b) of { b# ->
107 case (a# ==# b#) of {
112 If there aren't any nullary constructors, we emit a simpler
119 For the @(/=)@ method, we normally just use the default method.
121 If the type is an enumeration type, we could/may/should? generate
122 special code that calls @con2tag_Foo@, much like for @(==)@ shown
126 We thought about doing this: If we're also deriving @Ord@ for this
129 instance ... Eq (Foo ...) where
130 (==) a b = case (compare a b) of { _LT -> False; _EQ -> True ; _GT -> False}
131 (/=) a b = case (compare a b) of { _LT -> True ; _EQ -> False; _GT -> True }
133 However, that requires that \tr{Ord <whatever>} was put in the context
134 for the instance decl, which it probably wasn't, so the decls
135 produced don't get through the typechecker.
139 gen_Eq_binds :: TyCon -> RdrNameMonoBinds
143 tycon_loc = getSrcLoc tycon
144 (nullary_cons, nonnullary_cons)
145 | isNewTyCon tycon = ([], tyConDataCons tycon)
146 | otherwise = partition isNullaryDataCon (tyConDataCons tycon)
149 = if (null nullary_cons) then
150 case maybeTyConSingleCon tycon of
152 Nothing -> -- if cons don't match, then False
153 [([a_Pat, b_Pat], false_Expr)]
154 else -- calc. and compare the tags
156 untag_Expr tycon [(a_RDR,ah_RDR), (b_RDR,bh_RDR)]
157 (cmp_tags_Expr eqH_Int_RDR ah_RDR bh_RDR true_Expr false_Expr))]
159 mk_FunMonoBind tycon_loc eq_RDR ((map pats_etc nonnullary_cons) ++ rest)
161 mk_easy_FunMonoBind tycon_loc ne_RDR [a_Pat, b_Pat] [] (
162 HsApp (HsVar not_RDR) (HsPar (mk_easy_App eq_RDR [a_RDR, b_RDR])))
164 ------------------------------------------------------------------
167 con1_pat = ConPatIn data_con_RDR (map VarPatIn as_needed)
168 con2_pat = ConPatIn data_con_RDR (map VarPatIn bs_needed)
170 data_con_RDR = qual_orig_name data_con
171 con_arity = length tys_needed
172 as_needed = take con_arity as_RDRs
173 bs_needed = take con_arity bs_RDRs
174 tys_needed = dataConRawArgTys data_con
176 ([con1_pat, con2_pat], nested_eq_expr tys_needed as_needed bs_needed)
178 nested_eq_expr [] [] [] = true_Expr
179 nested_eq_expr tys as bs
180 = foldl1 and_Expr (zipWith3Equal "nested_eq" nested_eq tys as bs)
182 nested_eq ty a b = HsPar (eq_Expr ty (HsVar a) (HsVar b))
185 %************************************************************************
187 \subsubsection{Generating @Ord@ instance declarations}
189 %************************************************************************
191 For a derived @Ord@, we concentrate our attentions on @compare@
193 compare :: a -> a -> Ordering
194 data Ordering = LT | EQ | GT deriving ()
197 We will use the same example data type as above:
199 data Foo ... = N1 | N2 ... | Nn | O1 a b | O2 Int | O3 Double b b | ...
204 We do all the other @Ord@ methods with calls to @compare@:
206 instance ... (Ord <wurble> <wurble>) where
207 a < b = case (compare a b) of { LT -> True; EQ -> False; GT -> False }
208 a <= b = case (compare a b) of { LT -> True; EQ -> True; GT -> False }
209 a >= b = case (compare a b) of { LT -> False; EQ -> True; GT -> True }
210 a > b = case (compare a b) of { LT -> False; EQ -> False; GT -> True }
212 max a b = case (compare a b) of { LT -> b; EQ -> a; GT -> a }
213 min a b = case (compare a b) of { LT -> a; EQ -> b; GT -> b }
215 -- compare to come...
219 @compare@ always has two parts. First, we use the compared
220 data-constructors' tags to deal with the case of different
223 compare a b = case (con2tag_Foo a) of { a# ->
224 case (con2tag_Foo b) of { b# ->
225 case (a# ==# b#) of {
227 False -> case (a# <# b#) of
232 cmp_eq = ... to come ...
236 We are only left with the ``help'' function @cmp_eq@, to deal with
237 comparing data constructors with the same tag.
239 For the ordinary constructors (if any), we emit the sorta-obvious
240 compare-style stuff; for our example:
242 cmp_eq (O1 a1 b1) (O1 a2 b2)
243 = case (compare a1 a2) of { LT -> LT; EQ -> compare b1 b2; GT -> GT }
245 cmp_eq (O2 a1) (O2 a2)
248 cmp_eq (O3 a1 b1 c1) (O3 a2 b2 c2)
249 = case (compare a1 a2) of {
252 EQ -> case compare b1 b2 of {
260 Again, we must be careful about unboxed comparisons. For example,
261 if \tr{a1} and \tr{a2} were \tr{Int#}s in the 2nd example above, we'd need to
264 cmp_eq lt eq gt (O2 a1) (O2 a2)
266 -- or maybe the unfolded equivalent
270 For the remaining nullary constructors, we already know that the
278 gen_Ord_binds :: TyCon -> RdrNameMonoBinds
281 = defaulted `AndMonoBinds` compare
283 tycon_loc = getSrcLoc tycon
284 --------------------------------------------------------------------
285 compare = mk_easy_FunMonoBind tycon_loc compare_RDR
288 (if maybeToBool (maybeTyConSingleCon tycon) then
289 cmp_eq_Expr ltTag_Expr eqTag_Expr gtTag_Expr a_Expr b_Expr
291 untag_Expr tycon [(a_RDR, ah_RDR), (b_RDR, bh_RDR)]
292 (cmp_tags_Expr eqH_Int_RDR ah_RDR bh_RDR
293 -- True case; they are equal
294 -- If an enumeration type we are done; else
295 -- recursively compare their components
296 (if isEnumerationTyCon tycon then
299 cmp_eq_Expr ltTag_Expr eqTag_Expr gtTag_Expr a_Expr b_Expr
301 -- False case; they aren't equal
302 -- So we need to do a less-than comparison on the tags
303 (cmp_tags_Expr ltH_Int_RDR ah_RDR bh_RDR ltTag_Expr gtTag_Expr)))
305 (nullary_cons, nonnullary_cons)
306 | isNewTyCon tycon = ([], tyConDataCons tycon)
307 | otherwise = partition isNullaryDataCon (tyConDataCons tycon)
310 = mk_FunMonoBind tycon_loc cmp_eq_RDR (map pats_etc nonnullary_cons ++
311 [([WildPatIn, WildPatIn], default_rhs)])
314 = ([con1_pat, con2_pat],
315 nested_compare_expr tys_needed as_needed bs_needed)
317 con1_pat = ConPatIn data_con_RDR (map VarPatIn as_needed)
318 con2_pat = ConPatIn data_con_RDR (map VarPatIn bs_needed)
320 data_con_RDR = qual_orig_name data_con
321 con_arity = length tys_needed
322 as_needed = take con_arity as_RDRs
323 bs_needed = take con_arity bs_RDRs
324 tys_needed = dataConRawArgTys data_con
326 nested_compare_expr [ty] [a] [b]
327 = careful_compare_Case ty ltTag_Expr eqTag_Expr gtTag_Expr (HsVar a) (HsVar b)
329 nested_compare_expr (ty:tys) (a:as) (b:bs)
330 = let eq_expr = nested_compare_expr tys as bs
331 in careful_compare_Case ty ltTag_Expr eq_expr gtTag_Expr (HsVar a) (HsVar b)
333 default_rhs | null nullary_cons = impossible_Expr -- Keep desugarer from complaining about
334 -- inexhaustive patterns
335 | otherwise = eqTag_Expr -- Some nullary constructors;
336 -- Tags are equal, no args => return EQ
337 --------------------------------------------------------------------
339 defaulted = foldr1 AndMonoBinds [lt, le, ge, gt, max_, min_]
341 lt = mk_easy_FunMonoBind mkGeneratedSrcLoc lt_RDR [a_Pat, b_Pat] [] (
342 compare_Case true_Expr false_Expr false_Expr a_Expr b_Expr)
343 le = mk_easy_FunMonoBind mkGeneratedSrcLoc le_RDR [a_Pat, b_Pat] [] (
344 compare_Case true_Expr true_Expr false_Expr a_Expr b_Expr)
345 ge = mk_easy_FunMonoBind mkGeneratedSrcLoc ge_RDR [a_Pat, b_Pat] [] (
346 compare_Case false_Expr true_Expr true_Expr a_Expr b_Expr)
347 gt = mk_easy_FunMonoBind mkGeneratedSrcLoc gt_RDR [a_Pat, b_Pat] [] (
348 compare_Case false_Expr false_Expr true_Expr a_Expr b_Expr)
350 max_ = mk_easy_FunMonoBind mkGeneratedSrcLoc max_RDR [a_Pat, b_Pat] [] (
351 compare_Case b_Expr a_Expr a_Expr a_Expr b_Expr)
352 min_ = mk_easy_FunMonoBind mkGeneratedSrcLoc min_RDR [a_Pat, b_Pat] [] (
353 compare_Case a_Expr b_Expr b_Expr a_Expr b_Expr)
356 %************************************************************************
358 \subsubsection{Generating @Enum@ instance declarations}
360 %************************************************************************
362 @Enum@ can only be derived for enumeration types. For a type
364 data Foo ... = N1 | N2 | ... | Nn
367 we use both @con2tag_Foo@ and @tag2con_Foo@ functions, as well as a
368 @maxtag_Foo@ variable (all generated by @gen_tag_n_con_binds@).
371 instance ... Enum (Foo ...) where
372 toEnum i = tag2con_Foo i
374 enumFrom a = map tag2con_Foo [con2tag_Foo a .. maxtag_Foo]
378 = case con2tag_Foo a of
379 a# -> map tag2con_Foo (enumFromTo (I# a#) maxtag_Foo)
382 = map tag2con_Foo [con2tag_Foo a, con2tag_Foo b .. maxtag_Foo]
386 = case con2tag_Foo a of { a# ->
387 case con2tag_Foo b of { b# ->
388 map tag2con_Foo (enumFromThenTo (I# a#) (I# b#) maxtag_Foo)
392 For @enumFromTo@ and @enumFromThenTo@, we use the default methods.
395 gen_Enum_binds :: TyCon -> RdrNameMonoBinds
398 = to_enum `AndMonoBinds`
399 enum_from `AndMonoBinds`
400 enum_from_then `AndMonoBinds`
403 tycon_loc = getSrcLoc tycon
406 = mk_easy_FunMonoBind tycon_loc toEnum_RDR [a_Pat] [] $
407 mk_easy_App (tag2con_RDR tycon) [a_RDR]
410 = mk_easy_FunMonoBind tycon_loc enumFrom_RDR [a_Pat] [] $
411 untag_Expr tycon [(a_RDR, ah_RDR)] $
412 HsApp (mk_easy_App map_RDR [tag2con_RDR tycon]) $
413 HsPar (enum_from_to_Expr
414 (mk_easy_App mkInt_RDR [ah_RDR])
415 (HsVar (maxtag_RDR tycon)))
418 = mk_easy_FunMonoBind tycon_loc enumFromThen_RDR [a_Pat, b_Pat] [] $
419 untag_Expr tycon [(a_RDR, ah_RDR), (b_RDR, bh_RDR)] $
420 HsApp (mk_easy_App map_RDR [tag2con_RDR tycon]) $
421 HsPar (enum_from_then_to_Expr
422 (mk_easy_App mkInt_RDR [ah_RDR])
423 (mk_easy_App mkInt_RDR [bh_RDR])
424 (HsVar (maxtag_RDR tycon)))
427 = mk_easy_FunMonoBind tycon_loc fromEnum_RDR [a_Pat] [] $
428 untag_Expr tycon [(a_RDR, ah_RDR)] $
429 (mk_easy_App mkInt_RDR [ah_RDR])
432 %************************************************************************
434 \subsubsection{Generating @Eval@ instance declarations}
436 %************************************************************************
439 gen_Eval_binds tycon = EmptyMonoBinds
442 %************************************************************************
444 \subsubsection{Generating @Bounded@ instance declarations}
446 %************************************************************************
449 gen_Bounded_binds tycon
450 = if isEnumerationTyCon tycon then
451 min_bound_enum `AndMonoBinds` max_bound_enum
453 ASSERT(length data_cons == 1)
454 min_bound_1con `AndMonoBinds` max_bound_1con
456 data_cons = tyConDataCons tycon
457 tycon_loc = getSrcLoc tycon
459 ----- enum-flavored: ---------------------------
460 min_bound_enum = mk_easy_FunMonoBind tycon_loc minBound_RDR [] [] (HsVar data_con_1_RDR)
461 max_bound_enum = mk_easy_FunMonoBind tycon_loc maxBound_RDR [] [] (HsVar data_con_N_RDR)
463 data_con_1 = head data_cons
464 data_con_N = last data_cons
465 data_con_1_RDR = qual_orig_name data_con_1
466 data_con_N_RDR = qual_orig_name data_con_N
468 ----- single-constructor-flavored: -------------
469 arity = argFieldCount data_con_1
471 min_bound_1con = mk_easy_FunMonoBind tycon_loc minBound_RDR [] [] $
472 mk_easy_App data_con_1_RDR (nOfThem arity minBound_RDR)
473 max_bound_1con = mk_easy_FunMonoBind tycon_loc maxBound_RDR [] [] $
474 mk_easy_App data_con_1_RDR (nOfThem arity maxBound_RDR)
477 %************************************************************************
479 \subsubsection{Generating @Ix@ instance declarations}
481 %************************************************************************
483 Deriving @Ix@ is only possible for enumeration types and
484 single-constructor types. We deal with them in turn.
486 For an enumeration type, e.g.,
488 data Foo ... = N1 | N2 | ... | Nn
490 things go not too differently from @Enum@:
492 instance ... Ix (Foo ...) where
494 = map tag2con_Foo [con2tag_Foo a .. con2tag_Foo b]
498 = case (con2tag_Foo a) of { a# ->
499 case (con2tag_Foo b) of { b# ->
500 map tag2con_Foo (enumFromTo (I# a#) (I# b#))
505 then case (con2tag_Foo d -# con2tag_Foo a) of
507 else error "Ix.Foo.index: out of range"
511 p_tag = con2tag_Foo c
513 p_tag >= con2tag_Foo a && p_tag <= con2tag_Foo b
517 = case (con2tag_Foo a) of { a_tag ->
518 case (con2tag_Foo b) of { b_tag ->
519 case (con2tag_Foo c) of { c_tag ->
520 if (c_tag >=# a_tag) then
526 (modulo suitable case-ification to handle the unboxed tags)
528 For a single-constructor type (NB: this includes all tuples), e.g.,
530 data Foo ... = MkFoo a b Int Double c c
532 we follow the scheme given in Figure~19 of the Haskell~1.2 report
536 gen_Ix_binds :: TyCon -> RdrNameMonoBinds
539 = if isEnumerationTyCon tycon
543 tycon_str = getOccString tycon
544 tycon_loc = getSrcLoc tycon
546 --------------------------------------------------------------
547 enum_ixes = enum_range `AndMonoBinds`
548 enum_index `AndMonoBinds` enum_inRange
551 = mk_easy_FunMonoBind tycon_loc range_RDR [TuplePatIn [a_Pat, b_Pat]] [] $
552 untag_Expr tycon [(a_RDR, ah_RDR)] $
553 untag_Expr tycon [(b_RDR, bh_RDR)] $
554 HsApp (mk_easy_App map_RDR [tag2con_RDR tycon]) $
555 HsPar (enum_from_to_Expr
556 (mk_easy_App mkInt_RDR [ah_RDR])
557 (mk_easy_App mkInt_RDR [bh_RDR]))
560 = mk_easy_FunMonoBind tycon_loc index_RDR [AsPatIn c_RDR (TuplePatIn [a_Pat, b_Pat]), d_Pat] [] (
561 HsIf (HsPar (mk_easy_App inRange_RDR [c_RDR, d_RDR])) (
562 untag_Expr tycon [(a_RDR, ah_RDR)] (
563 untag_Expr tycon [(d_RDR, dh_RDR)] (
565 grhs = [OtherwiseGRHS (mk_easy_App mkInt_RDR [c_RDR]) tycon_loc]
568 (genOpApp (HsVar dh_RDR) minusH_RDR (HsVar ah_RDR))
569 [PatMatch (VarPatIn c_RDR)
570 (GRHSMatch (GRHSsAndBindsIn grhs EmptyBinds))]
574 HsApp (HsVar error_RDR) (HsLit (HsString (_PK_ ("Ix."++tycon_str++".index: out of range\n"))))
579 = mk_easy_FunMonoBind tycon_loc inRange_RDR [TuplePatIn [a_Pat, b_Pat], c_Pat] [] (
580 untag_Expr tycon [(a_RDR, ah_RDR)] (
581 untag_Expr tycon [(b_RDR, bh_RDR)] (
582 untag_Expr tycon [(c_RDR, ch_RDR)] (
583 HsIf (genOpApp (HsVar ch_RDR) geH_RDR (HsVar ah_RDR)) (
584 (genOpApp (HsVar ch_RDR) leH_RDR (HsVar bh_RDR))
589 --------------------------------------------------------------
590 single_con_ixes = single_con_range `AndMonoBinds`
591 single_con_index `AndMonoBinds` single_con_inRange
594 = case maybeTyConSingleCon tycon of -- just checking...
595 Nothing -> panic "get_Ix_binds"
596 Just dc -> if (any isPrimType (dataConRawArgTys dc)) then
597 error ("ERROR: Can't derive Ix for a single-constructor type with primitive argument types: "++tycon_str)
601 con_arity = argFieldCount data_con
602 data_con_RDR = qual_orig_name data_con
603 con_pat xs = ConPatIn data_con_RDR (map VarPatIn xs)
604 con_expr xs = mk_easy_App data_con_RDR xs
606 as_needed = take con_arity as_RDRs
607 bs_needed = take con_arity bs_RDRs
608 cs_needed = take con_arity cs_RDRs
610 --------------------------------------------------------------
612 = mk_easy_FunMonoBind tycon_loc range_RDR [TuplePatIn [con_pat as_needed, con_pat bs_needed]] [] $
613 HsDo ListComp stmts tycon_loc
615 stmts = zipWith3Equal "single_con_range" mk_qual as_needed bs_needed cs_needed
617 [ReturnStmt (con_expr cs_needed)]
619 mk_qual a b c = BindStmt (VarPatIn c)
620 (HsApp (HsVar range_RDR) (ExplicitTuple [HsVar a, HsVar b]))
625 = mk_easy_FunMonoBind tycon_loc index_RDR [TuplePatIn [con_pat as_needed, con_pat bs_needed], con_pat cs_needed] [range_size] (
626 foldl mk_index (HsLit (HsInt 0)) (zip3 as_needed bs_needed cs_needed))
628 mk_index multiply_by (l, u, i)
630 (HsApp (HsApp (HsVar index_RDR) (ExplicitTuple [HsVar l, HsVar u])) (HsVar i))
633 (HsApp (HsVar rangeSize_RDR) (ExplicitTuple [HsVar l, HsVar u]))
634 ) times_RDR multiply_by
638 = mk_easy_FunMonoBind tycon_loc rangeSize_RDR [TuplePatIn [a_Pat, b_Pat]] [] (
640 (HsApp (HsApp (HsVar index_RDR) (ExplicitTuple [a_Expr, b_Expr])) b_Expr)
641 ) plus_RDR (HsLit (HsInt 1)))
645 = mk_easy_FunMonoBind tycon_loc inRange_RDR
646 [TuplePatIn [con_pat as_needed, con_pat bs_needed], con_pat cs_needed]
648 foldl1 and_Expr (zipWith3Equal "single_con_inRange" in_range as_needed bs_needed cs_needed))
650 in_range a b c = HsApp (HsApp (HsVar inRange_RDR) (ExplicitTuple [HsVar a, HsVar b])) (HsVar c)
653 %************************************************************************
655 \subsubsection{Generating @Read@ instance declarations}
657 %************************************************************************
659 Ignoring all the infix-ery mumbo jumbo (ToDo)
662 gen_Read_binds :: TyCon -> RdrNameMonoBinds
665 = reads_prec `AndMonoBinds` read_list
667 tycon_loc = getSrcLoc tycon
668 -----------------------------------------------------------------------
669 read_list = mk_easy_FunMonoBind tycon_loc readList_RDR [] []
670 (HsApp (HsVar readList___RDR) (HsPar (HsApp (HsVar readsPrec_RDR) (HsLit (HsInt 0)))))
671 -----------------------------------------------------------------------
674 read_con_comprehensions
675 = map read_con (tyConDataCons tycon)
677 mk_easy_FunMonoBind tycon_loc readsPrec_RDR [a_Pat, b_Pat] [] (
678 foldr1 append_Expr read_con_comprehensions
681 read_con data_con -- note: "b" is the string being "read"
683 data_con_RDR = qual_orig_name data_con
684 data_con_str= occNameString (getOccName data_con)
685 con_arity = argFieldCount data_con
686 as_needed = take con_arity as_RDRs
687 bs_needed = take con_arity bs_RDRs
688 con_expr = mk_easy_App data_con_RDR as_needed
689 nullary_con = con_arity == 0
693 (TuplePatIn [LitPatIn (HsString data_con_str), d_Pat])
694 (HsApp (HsVar lex_RDR) c_Expr)
697 field_quals = snd (mapAccumL mk_qual d_Expr (zipEqual "as_needed" as_needed bs_needed))
698 mk_qual draw_from (con_field, str_left)
699 = (HsVar str_left, -- what to draw from down the line...
701 (TuplePatIn [VarPatIn con_field, VarPatIn str_left])
702 (HsApp (HsApp (HsVar readsPrec_RDR) (HsLit (HsInt 10))) draw_from)
706 result_expr = ExplicitTuple [con_expr, if null bs_needed
708 else HsVar (last bs_needed)]
710 stmts = (con_qual : field_quals) ++ [ReturnStmt result_expr]
714 = if nullary_con then -- must be False (parens are surely optional)
716 else -- parens depend on precedence...
717 HsPar (genOpApp a_Expr gt_RDR (HsLit (HsInt 9)))
720 readParen_Expr read_paren_arg $ HsPar $
721 HsLam (mk_easy_Match tycon_loc [c_Pat] [] $
722 HsDo ListComp stmts tycon_loc)
726 %************************************************************************
728 \subsubsection{Generating @Show@ instance declarations}
730 %************************************************************************
732 Ignoring all the infix-ery mumbo jumbo (ToDo)
735 gen_Show_binds :: TyCon -> RdrNameMonoBinds
738 = shows_prec `AndMonoBinds` show_list
740 tycon_loc = getSrcLoc tycon
741 -----------------------------------------------------------------------
742 show_list = mk_easy_FunMonoBind tycon_loc showList_RDR [] []
743 (HsApp (HsVar showList___RDR) (HsPar (HsApp (HsVar showsPrec_RDR) (HsLit (HsInt 0)))))
744 -----------------------------------------------------------------------
746 = mk_FunMonoBind tycon_loc showsPrec_RDR (map pats_etc (tyConDataCons tycon))
750 data_con_RDR = qual_orig_name data_con
751 con_arity = argFieldCount data_con
752 bs_needed = take con_arity bs_RDRs
753 con_pat = ConPatIn data_con_RDR (map VarPatIn bs_needed)
754 nullary_con = con_arity == 0
757 = let nm = occNameString (getOccName data_con)
758 space_maybe = if nullary_con then _NIL_ else SLIT(" ")
760 HsApp (HsVar showString_RDR) (HsLit (HsString (nm _APPEND_ space_maybe)))
762 show_thingies = show_con : (spacified real_show_thingies)
765 = [ HsApp (HsApp (HsVar showsPrec_RDR) (HsLit (HsInt 10))) (HsVar b)
768 if nullary_con then -- skip the showParen junk...
769 ASSERT(null bs_needed)
770 ([a_Pat, con_pat], show_con)
773 showParen_Expr (HsPar (genOpApp a_Expr ge_RDR (HsLit (HsInt 10))))
774 (HsPar (nested_compose_Expr show_thingies)))
778 spacified (x:xs) = (x : (HsVar showSpace_RDR) : spacified xs)
781 %************************************************************************
783 \subsection{Generating extra binds (@con2tag@ and @tag2con@)}
785 %************************************************************************
790 con2tag_Foo :: Foo ... -> Int#
791 tag2con_Foo :: Int -> Foo ... -- easier if Int, not Int#
792 maxtag_Foo :: Int -- ditto (NB: not unboxed)
795 The `tags' here start at zero, hence the @fIRST_TAG@ (currently one)
800 = GenCon2Tag | GenTag2Con | GenMaxTag
802 gen_tag_n_con_monobind
803 :: (RdrName, -- (proto)Name for the thing in question
804 TyCon, -- tycon in question
808 gen_tag_n_con_monobind (rdr_name, tycon, GenCon2Tag)
809 = mk_FunMonoBind (getSrcLoc tycon) rdr_name (map mk_stuff (tyConDataCons tycon))
811 mk_stuff :: DataCon -> ([RdrNamePat], RdrNameHsExpr)
814 = ASSERT(isDataCon var)
815 ([pat], HsLit (HsIntPrim (toInteger ((dataConTag var) - fIRST_TAG))))
817 pat = ConPatIn var_RDR (nOfThem (argFieldCount var) WildPatIn)
818 var_RDR = qual_orig_name var
820 gen_tag_n_con_monobind (rdr_name, tycon, GenTag2Con)
821 = mk_FunMonoBind (getSrcLoc tycon) rdr_name (map mk_stuff (tyConDataCons tycon) ++
822 [([WildPatIn], impossible_Expr)])
824 mk_stuff :: DataCon -> ([RdrNamePat], RdrNameHsExpr)
827 = ASSERT(isDataCon var)
828 ([lit_pat], HsVar var_RDR)
830 lit_pat = ConPatIn mkInt_RDR [LitPatIn (HsIntPrim (toInteger ((dataConTag var) - fIRST_TAG)))]
831 var_RDR = qual_orig_name var
833 gen_tag_n_con_monobind (rdr_name, tycon, GenMaxTag)
834 = mk_easy_FunMonoBind (getSrcLoc tycon)
835 rdr_name [] [] (HsApp (HsVar mkInt_RDR) (HsLit (HsIntPrim max_tag)))
837 max_tag = case (tyConDataCons tycon) of
838 data_cons -> toInteger ((length data_cons) - fIRST_TAG)
842 %************************************************************************
844 \subsection{Utility bits for generating bindings}
846 %************************************************************************
848 @mk_easy_FunMonoBind fun pats binds expr@ generates:
850 fun pat1 pat2 ... patN = expr where binds
853 @mk_FunMonoBind fun [([p1a, p1b, ...], e1), ...]@ is for
854 multi-clause definitions; it generates:
856 fun p1a p1b ... p1N = e1
857 fun p2a p2b ... p2N = e2
859 fun pMa pMb ... pMN = eM
863 mk_easy_FunMonoBind :: SrcLoc -> RdrName -> [RdrNamePat]
864 -> [RdrNameMonoBinds] -> RdrNameHsExpr
867 mk_easy_FunMonoBind loc fun pats binds expr
868 = FunMonoBind fun False{-not infix-} [mk_easy_Match loc pats binds expr] loc
870 mk_easy_Match loc pats binds expr
871 = mk_match loc pats expr (mkbind binds)
873 mkbind [] = EmptyBinds
874 mkbind bs = MonoBind (foldr1 AndMonoBinds bs) [] recursive
875 -- The renamer expects everything in its input to be a
876 -- "recursive" MonoBinds, and it is its job to sort things out
879 mk_FunMonoBind :: SrcLoc -> RdrName
880 -> [([RdrNamePat], RdrNameHsExpr)]
883 mk_FunMonoBind loc fun [] = panic "TcGenDeriv:mk_FunMonoBind"
884 mk_FunMonoBind loc fun pats_and_exprs
885 = FunMonoBind fun False{-not infix-}
886 [ mk_match loc p e EmptyBinds | (p,e) <-pats_and_exprs ]
889 mk_match loc pats expr binds
891 (GRHSMatch (GRHSsAndBindsIn [OtherwiseGRHS expr loc] binds))
894 paren p@(VarPatIn _) = p
895 paren other_p = ParPatIn other_p
899 mk_easy_App f xs = foldl HsApp (HsVar f) (map HsVar xs)
902 ToDo: Better SrcLocs.
905 compare_Case, cmp_eq_Expr ::
906 RdrNameHsExpr -> RdrNameHsExpr -> RdrNameHsExpr
907 -> RdrNameHsExpr -> RdrNameHsExpr
911 -> RdrNameHsExpr -> RdrNameHsExpr -> RdrNameHsExpr
912 -> RdrNameHsExpr -> RdrNameHsExpr
914 careful_compare_Case :: -- checks for primitive types...
916 -> RdrNameHsExpr -> RdrNameHsExpr -> RdrNameHsExpr
917 -> RdrNameHsExpr -> RdrNameHsExpr
920 compare_Case = compare_gen_Case compare_RDR
921 cmp_eq_Expr = compare_gen_Case cmp_eq_RDR
923 compare_gen_Case fun lt eq gt a b
924 = HsCase (HsPar (HsApp (HsApp (HsVar fun) a) b)) {-of-}
925 [PatMatch (ConPatIn ltTag_RDR [])
926 (GRHSMatch (GRHSsAndBindsIn [OtherwiseGRHS lt mkGeneratedSrcLoc] EmptyBinds)),
928 PatMatch (ConPatIn eqTag_RDR [])
929 (GRHSMatch (GRHSsAndBindsIn [OtherwiseGRHS eq mkGeneratedSrcLoc] EmptyBinds)),
931 PatMatch (ConPatIn gtTag_RDR [])
932 (GRHSMatch (GRHSsAndBindsIn [OtherwiseGRHS gt mkGeneratedSrcLoc] EmptyBinds))]
935 careful_compare_Case ty lt eq gt a b
936 = if not (isPrimType ty) then
937 compare_gen_Case compare_RDR lt eq gt a b
939 else -- we have to do something special for primitive things...
940 HsIf (genOpApp a relevant_eq_op b)
942 (HsIf (genOpApp a relevant_lt_op b) lt gt mkGeneratedSrcLoc)
945 relevant_eq_op = assoc_ty_id eq_op_tbl ty
946 relevant_lt_op = assoc_ty_id lt_op_tbl ty
949 = if null res then panic "assoc_ty"
952 res = [id | (ty',id) <- tyids, eqTy ty ty']
955 [(charPrimTy, eqH_Char_RDR)
956 ,(intPrimTy, eqH_Int_RDR)
957 ,(wordPrimTy, eqH_Word_RDR)
958 ,(addrPrimTy, eqH_Addr_RDR)
959 ,(floatPrimTy, eqH_Float_RDR)
960 ,(doublePrimTy, eqH_Double_RDR)
964 [(charPrimTy, ltH_Char_RDR)
965 ,(intPrimTy, ltH_Int_RDR)
966 ,(wordPrimTy, ltH_Word_RDR)
967 ,(addrPrimTy, ltH_Addr_RDR)
968 ,(floatPrimTy, ltH_Float_RDR)
969 ,(doublePrimTy, ltH_Double_RDR)
972 -----------------------------------------------------------------------
974 and_Expr, append_Expr :: RdrNameHsExpr -> RdrNameHsExpr -> RdrNameHsExpr
976 and_Expr a b = genOpApp a and_RDR b
977 append_Expr a b = genOpApp a append_RDR b
979 -----------------------------------------------------------------------
981 eq_Expr :: Type -> RdrNameHsExpr -> RdrNameHsExpr -> RdrNameHsExpr
983 = if not (isPrimType ty) then
985 else -- we have to do something special for primitive things...
986 genOpApp a relevant_eq_op b
988 relevant_eq_op = assoc_ty_id eq_op_tbl ty
992 argFieldCount :: Id -> Int -- Works on data and newtype constructors
993 argFieldCount con = length (dataConRawArgTys con)
997 untag_Expr :: TyCon -> [(RdrName, RdrName)] -> RdrNameHsExpr -> RdrNameHsExpr
998 untag_Expr tycon [] expr = expr
999 untag_Expr tycon ((untag_this, put_tag_here) : more) expr
1000 = HsCase (HsPar (HsApp (con2tag_Expr tycon) (HsVar untag_this))) {-of-}
1001 [PatMatch (VarPatIn put_tag_here)
1002 (GRHSMatch (GRHSsAndBindsIn grhs EmptyBinds))]
1005 grhs = [OtherwiseGRHS (untag_Expr tycon more expr) mkGeneratedSrcLoc]
1007 cmp_tags_Expr :: RdrName -- Comparison op
1008 -> RdrName -> RdrName -- Things to compare
1009 -> RdrNameHsExpr -- What to return if true
1010 -> RdrNameHsExpr -- What to return if false
1013 cmp_tags_Expr op a b true_case false_case
1014 = HsIf (genOpApp (HsVar a) op (HsVar b)) true_case false_case mkGeneratedSrcLoc
1017 :: RdrNameHsExpr -> RdrNameHsExpr
1019 enum_from_then_to_Expr
1020 :: RdrNameHsExpr -> RdrNameHsExpr -> RdrNameHsExpr
1023 enum_from_to_Expr f t2 = HsApp (HsApp (HsVar enumFromTo_RDR) f) t2
1024 enum_from_then_to_Expr f t t2 = HsApp (HsApp (HsApp (HsVar enumFromThenTo_RDR) f) t) t2
1026 showParen_Expr, readParen_Expr
1027 :: RdrNameHsExpr -> RdrNameHsExpr
1030 showParen_Expr e1 e2 = HsApp (HsApp (HsVar showParen_RDR) e1) e2
1031 readParen_Expr e1 e2 = HsApp (HsApp (HsVar readParen_RDR) e1) e2
1033 nested_compose_Expr :: [RdrNameHsExpr] -> RdrNameHsExpr
1035 nested_compose_Expr [e] = parenify e
1036 nested_compose_Expr (e:es)
1037 = HsApp (HsApp (HsVar compose_RDR) (parenify e)) (nested_compose_Expr es)
1039 -- impossible_Expr is used in case RHSs that should never happen.
1040 -- We generate these to keep the desugarer from complaining that they *might* happen!
1041 impossible_Expr = HsApp (HsVar error_RDR) (HsLit (HsString (_PK_ "Urk! in TcGenDeriv")))
1043 parenify e@(HsVar _) = e
1044 parenify e = HsPar e
1046 -- genOpApp wraps brackets round the operator application, so that the
1047 -- renamer won't subsequently try to re-associate it.
1048 -- For some reason the renamer doesn't reassociate it right, and I can't
1049 -- be bothered to find out why just now.
1051 genOpApp e1 op e2 = mkOpApp e1 op e2
1055 qual_orig_name n = case modAndOcc n of { (m,n) -> Qual m n HiFile }
1057 a_RDR = varUnqual SLIT("a")
1058 b_RDR = varUnqual SLIT("b")
1059 c_RDR = varUnqual SLIT("c")
1060 d_RDR = varUnqual SLIT("d")
1061 ah_RDR = varUnqual SLIT("a#")
1062 bh_RDR = varUnqual SLIT("b#")
1063 ch_RDR = varUnqual SLIT("c#")
1064 dh_RDR = varUnqual SLIT("d#")
1065 cmp_eq_RDR = varUnqual SLIT("cmp_eq")
1066 rangeSize_RDR = varUnqual SLIT("rangeSize")
1068 as_RDRs = [ varUnqual (_PK_ ("a"++show i)) | i <- [(1::Int) .. ] ]
1069 bs_RDRs = [ varUnqual (_PK_ ("b"++show i)) | i <- [(1::Int) .. ] ]
1070 cs_RDRs = [ varUnqual (_PK_ ("c"++show i)) | i <- [(1::Int) .. ] ]
1072 a_Expr = HsVar a_RDR
1073 b_Expr = HsVar b_RDR
1074 c_Expr = HsVar c_RDR
1075 d_Expr = HsVar d_RDR
1076 ltTag_Expr = HsVar ltTag_RDR
1077 eqTag_Expr = HsVar eqTag_RDR
1078 gtTag_Expr = HsVar gtTag_RDR
1079 false_Expr = HsVar false_RDR
1080 true_Expr = HsVar true_RDR
1082 con2tag_Expr tycon = HsVar (con2tag_RDR tycon)
1084 a_Pat = VarPatIn a_RDR
1085 b_Pat = VarPatIn b_RDR
1086 c_Pat = VarPatIn c_RDR
1087 d_Pat = VarPatIn d_RDR
1089 con2tag_RDR, tag2con_RDR, maxtag_RDR :: TyCon -> RdrName
1091 con2tag_RDR tycon = varUnqual (SLIT("con2tag_") _APPEND_ occNameString (getOccName tycon) _APPEND_ SLIT("#"))
1092 tag2con_RDR tycon = varUnqual (SLIT("tag2con_") _APPEND_ occNameString (getOccName tycon) _APPEND_ SLIT("#"))
1093 maxtag_RDR tycon = varUnqual (SLIT("maxtag_") _APPEND_ occNameString (getOccName tycon) _APPEND_ SLIT("#"))