2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
5 \section[HsLit]{Abstract syntax: source-language literals}
8 {-# LANGUAGE DeriveDataTypeable #-}
12 #include "HsVersions.h"
14 import {-# SOURCE #-} HsExpr( SyntaxExpr, pprExpr )
15 import HsTypes (PostTcType)
24 %************************************************************************
26 \subsection[HsLit]{Literals}
28 %************************************************************************
33 = HsChar Char -- Character
34 | HsCharPrim Char -- Unboxed character
35 | HsString FastString -- String
36 | HsStringPrim FastString -- Packed string
37 | HsInt Integer -- Genuinely an Int; arises from TcGenDeriv,
38 -- and from TRANSLATION
39 | HsIntPrim Integer -- Unboxed Int
40 | HsWordPrim Integer -- Unboxed Word
41 | HsInteger Integer Type -- Genuinely an integer; arises only from TRANSLATION
42 -- (overloaded literals are done with HsOverLit)
43 | HsRat Rational Type -- Genuinely a rational; arises only from TRANSLATION
44 -- (overloaded literals are done with HsOverLit)
45 | HsFloatPrim Rational -- Unboxed Float
46 | HsDoublePrim Rational -- Unboxed Double
47 deriving (Data, Typeable)
49 instance Eq HsLit where
50 (HsChar x1) == (HsChar x2) = x1==x2
51 (HsCharPrim x1) == (HsCharPrim x2) = x1==x2
52 (HsString x1) == (HsString x2) = x1==x2
53 (HsStringPrim x1) == (HsStringPrim x2) = x1==x2
54 (HsInt x1) == (HsInt x2) = x1==x2
55 (HsIntPrim x1) == (HsIntPrim x2) = x1==x2
56 (HsWordPrim x1) == (HsWordPrim x2) = x1==x2
57 (HsInteger x1 _) == (HsInteger x2 _) = x1==x2
58 (HsRat x1 _) == (HsRat x2 _) = x1==x2
59 (HsFloatPrim x1) == (HsFloatPrim x2) = x1==x2
60 (HsDoublePrim x1) == (HsDoublePrim x2) = x1==x2
63 data HsOverLit id -- An overloaded literal
66 ol_rebindable :: Bool, -- Note [ol_rebindable]
67 ol_witness :: SyntaxExpr id, -- Note [Overloaded literal witnesses]
68 ol_type :: PostTcType }
69 deriving (Data, Typeable)
72 = HsIntegral !Integer -- Integer-looking literals;
73 | HsFractional !Rational -- Frac-looking literals
74 | HsIsString !FastString -- String-looking literals
75 deriving (Data, Typeable)
77 overLitType :: HsOverLit a -> Type
83 The ol_rebindable field is True if this literal is actually
84 using rebindable syntax. Specifically:
86 False iff ol_witness is the standard one
87 True iff ol_witness is non-standard
89 Equivalently it's True if
90 a) RebindableSyntax is on
91 b) the witness for fromInteger/fromRational/fromString
92 that happens to be in scope isn't the standard one
94 Note [Overloaded literal witnesses]
95 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
96 *Before* type checking, the SyntaxExpr in an HsOverLit is the
97 name of the coercion function, 'fromInteger' or 'fromRational'.
98 *After* type checking, it is a witness for the literal, such as
99 (fromInteger 3) or lit_78
100 This witness should replace the literal.
102 This dual role is unusual, because we're replacing 'fromInteger' with
103 a call to fromInteger. Reason: it allows commoning up of the fromInteger
104 calls, which wouldn't be possible if the desguarar made the application.
106 The PostTcType in each branch records the type the overload literal is
110 -- Comparison operations are needed when grouping literals
111 -- for compiling pattern-matching (module MatchLit)
112 instance Eq (HsOverLit id) where
113 (OverLit {ol_val = val1}) == (OverLit {ol_val=val2}) = val1 == val2
115 instance Eq OverLitVal where
116 (HsIntegral i1) == (HsIntegral i2) = i1 == i2
117 (HsFractional f1) == (HsFractional f2) = f1 == f2
118 (HsIsString s1) == (HsIsString s2) = s1 == s2
121 instance Ord (HsOverLit id) where
122 compare (OverLit {ol_val=val1}) (OverLit {ol_val=val2}) = val1 `compare` val2
124 instance Ord OverLitVal where
125 compare (HsIntegral i1) (HsIntegral i2) = i1 `compare` i2
126 compare (HsIntegral _) (HsFractional _) = LT
127 compare (HsIntegral _) (HsIsString _) = LT
128 compare (HsFractional f1) (HsFractional f2) = f1 `compare` f2
129 compare (HsFractional _) (HsIntegral _) = GT
130 compare (HsFractional _) (HsIsString _) = LT
131 compare (HsIsString s1) (HsIsString s2) = s1 `compare` s2
132 compare (HsIsString _) (HsIntegral _) = GT
133 compare (HsIsString _) (HsFractional _) = GT
137 instance Outputable HsLit where
138 -- Use "show" because it puts in appropriate escapes
139 ppr (HsChar c) = pprHsChar c
140 ppr (HsCharPrim c) = pprHsChar c <> char '#'
141 ppr (HsString s) = pprHsString s
142 ppr (HsStringPrim s) = pprHsString s <> char '#'
143 ppr (HsInt i) = integer i
144 ppr (HsInteger i _) = integer i
145 ppr (HsRat f _) = rational f
146 ppr (HsFloatPrim f) = rational f <> char '#'
147 ppr (HsDoublePrim d) = rational d <> text "##"
148 ppr (HsIntPrim i) = integer i <> char '#'
149 ppr (HsWordPrim w) = integer w <> text "##"
151 -- in debug mode, print the expression that it's resolved to, too
152 instance OutputableBndr id => Outputable (HsOverLit id) where
153 ppr (OverLit {ol_val=val, ol_witness=witness})
154 = ppr val <+> (ifPprDebug (parens (pprExpr witness)))
156 instance Outputable OverLitVal where
157 ppr (HsIntegral i) = integer i
158 ppr (HsFractional f) = rational f
159 ppr (HsIsString s) = pprHsString s