- = let
- data_con_RDR = qual_orig_name data_con
- con_arity = dataConNumFields data_con
- bs_needed = take con_arity bs_RDRs
- con_pat = ConPatIn data_con_RDR (map VarPatIn bs_needed)
- nullary_con = isNullaryDataCon data_con
-
- show_con
- = let nm = occNameString (getOccName data_con)
- space_maybe = if nullary_con then _NIL_ else SLIT(" ")
- in
- HsApp (HsVar showString_RDR) (HsLit (HsString (nm _APPEND_ space_maybe)))
-
- show_thingies = show_con : (spacified real_show_thingies)
-
- real_show_thingies
- = [ HsApp (HsApp (HsVar showsPrec_RDR) (HsLit (HsInt 10))) (HsVar b)
- | b <- bs_needed ]
- in
- if nullary_con then -- skip the showParen junk...
- ASSERT(null bs_needed)
- ([a_Pat, con_pat], show_con)
- else
- ([a_Pat, con_pat],
- showParen_Expr (HsPar (genOpApp a_Expr ge_RDR (HsLit (HsInt 10))))
- (HsPar (nested_compose_Expr show_thingies)))
- where
- spacified [] = []
- spacified [x] = [x]
- spacified (x:xs) = (x : (HsVar showSpace_RDR) : spacified xs)
+ | nullary_con = -- skip the showParen junk...
+ ASSERT(null bs_needed)
+ ([wildPat, con_pat], show_con)
+ | otherwise =
+ ([a_Pat, con_pat],
+ showParen_Expr (HsPar (genOpApp a_Expr ge_RDR (HsLit (HsInt paren_prec_limit))))
+ (HsPar (nested_compose_Expr show_thingies)))
+ where
+ data_con_RDR = qual_orig_name data_con
+ con_arity = dataConSourceArity data_con
+ bs_needed = take con_arity bs_RDRs
+ con_pat = ConPatIn data_con_RDR (map VarPatIn bs_needed)
+ nullary_con = con_arity == 0
+ labels = dataConFieldLabels data_con
+ lab_fields = length labels
+
+ dc_nm = getName data_con
+ dc_occ_nm = getOccName data_con
+ dc_occ_nm_str = occNameUserString dc_occ_nm
+
+ is_infix = isDataSymOcc dc_occ_nm
+
+
+ show_con
+ | is_infix = mk_showString_app (' ':dc_occ_nm_str)
+ | otherwise = mk_showString_app (dc_occ_nm_str ++ space_ocurly_maybe)
+ where
+ space_ocurly_maybe
+ | nullary_con = ""
+ | lab_fields == 0 = " "
+ | otherwise = "{"
+
+
+ show_all con fs@(x:xs)
+ | is_infix = x:con:xs
+ | otherwise =
+ let
+ ccurly_maybe
+ | lab_fields > 0 = [mk_showString_app "}"]
+ | otherwise = []
+ in
+ con:fs ++ ccurly_maybe
+
+ show_thingies = show_all show_con real_show_thingies_with_labs
+
+ show_label l = mk_showString_app (the_name ++ "=")
+ where
+ occ_nm = getOccName (fieldLabelName l)
+ -- legal, but rare.
+ is_op = isSymOcc occ_nm
+ the_name
+ | is_op = '(':nm ++ ")"
+ | otherwise = nm
+
+ nm = occNameUserString occ_nm
+
+
+ mk_showString_app str = HsApp (HsVar showString_RDR)
+ (HsLit (mkHsString str))
+
+ prec_cons = getLRPrecs is_infix fixs_assoc dc_nm
+
+ real_show_thingies
+ | is_infix =
+ [ HsApp (HsApp (HsVar showsPrec_RDR) (HsLit (HsInt p))) (HsVar b)
+ | (p,b) <- zip prec_cons bs_needed ]
+ | otherwise =
+ [ HsApp (HsApp (HsVar showsPrec_RDR) (HsLit (HsInt 10))) (HsVar b)
+ | b <- bs_needed ]
+
+ real_show_thingies_with_labs
+ | lab_fields == 0 = intersperse (HsVar showSpace_RDR) real_show_thingies
+ | otherwise = --Assumption: no of fields == no of labelled fields
+ -- (and in same order)
+ concat $
+ intersperse ([mk_showString_app ","]) $ -- Using SLIT()s containing ,s spells trouble.
+ zipWithEqual "gen_Show_binds"
+ (\ a b -> [a,b])
+ (map show_label labels)
+ real_show_thingies
+
+ (con_left_assoc, con_right_assoc) = isLRAssoc fixs_assoc dc_nm
+
+ {-
+ c.f. Figure 16 and 17 in Haskell 1.1 report
+ -}
+ paren_prec_limit
+ | not is_infix = fromInt maxPrecedence + 1
+ | otherwise = getFixity fixs_assoc dc_nm + 1
+
+\end{code}
+
+\begin{code}
+getLRPrecs :: Bool -> Fixities -> Name -> [Integer]
+getLRPrecs is_infix fixs_assoc nm = [lp, rp]
+ where
+ {-
+ Figuring out the fixities of the arguments to a constructor,
+ cf. Figures 16-18 in Haskell 1.1 report.
+ -}
+ (con_left_assoc, con_right_assoc) = isLRAssoc fixs_assoc nm
+ paren_con_prec = getFixity fixs_assoc nm
+ maxPrec = fromInt maxPrecedence
+
+ lp
+ | not is_infix = maxPrec + 1
+ | con_left_assoc = paren_con_prec
+ | otherwise = paren_con_prec + 1
+
+ rp
+ | not is_infix = maxPrec + 1
+ | con_right_assoc = paren_con_prec
+ | otherwise = paren_con_prec + 1
+
+getFixity :: Fixities -> Name -> Integer
+getFixity fixs_assoc nm =
+ case lookupFixity fixs_assoc nm of
+ Fixity x _ -> fromInt x
+
+isLRAssoc :: Fixities -> Name -> (Bool, Bool)
+isLRAssoc fixs_assoc nm =
+ case lookupFixity fixs_assoc nm of
+ Fixity _ InfixN -> (False, False)
+ Fixity _ InfixR -> (False, True)
+ Fixity _ InfixL -> (True, False)
+
+lookupFixity :: Fixities -> Name -> Fixity
+lookupFixity fixs_assoc nm = assocDefault defaultFixity fixs_assoc nm
+
+isInfixOccName :: String -> Bool
+isInfixOccName str =
+ case str of
+ (':':_) -> True
+ _ -> False
+