[commit: ghc] master: Implement -XNumDecimals (#7266) (a6be6f1)
git at git.haskell.org
git at git.haskell.org
Thu Aug 29 07:15:16 CEST 2013
Repository : ssh://git@git.haskell.org/ghc
On branch : master
Link : http://ghc.haskell.org/trac/ghc/changeset/a6be6f1bd30c1476718392a259cfccf082d0da4d/ghc
>---------------------------------------------------------------
commit a6be6f1bd30c1476718392a259cfccf082d0da4d
Author: Austin Seipp <aseipp at pobox.com>
Date: Wed Aug 28 17:14:06 2013 -0500
Implement -XNumDecimals (#7266)
Under -XNumDecimals, it's possible to specify an integer literal using
compact "floating point" syntax for any floating literal constant which
also happens to be an integer. This lets us write
1.2e6 :: Integer
instead of:
1200000 :: Integer
This also makes some amendments to the users guide.
Authored-by: Shachaf Ben-Kiki <shachaf at gmail.com>
Signed-off-by: Austin Seipp <aseipp at pobox.com>
>---------------------------------------------------------------
a6be6f1bd30c1476718392a259cfccf082d0da4d
compiler/main/DynFlags.hs | 2 ++
compiler/rename/RnPat.lhs | 19 ++++++++++++++++---
docs/users_guide/7.8.1-notes.xml | 15 +++++++++++++++
3 files changed, 33 insertions(+), 3 deletions(-)
diff --git a/compiler/main/DynFlags.hs b/compiler/main/DynFlags.hs
index 849d8a4..4d19519 100644
--- a/compiler/main/DynFlags.hs
+++ b/compiler/main/DynFlags.hs
@@ -500,6 +500,7 @@ data ExtensionFlag
| Opt_TypeFamilies
| Opt_OverloadedStrings
| Opt_OverloadedLists
+ | Opt_NumDecimals
| Opt_DisambiguateRecordFields
| Opt_RecordWildCards
| Opt_RecordPuns
@@ -2677,6 +2678,7 @@ xFlags = [
deprecatedForExtension "NamedFieldPuns" ),
( "DisambiguateRecordFields", Opt_DisambiguateRecordFields, nop ),
( "OverloadedStrings", Opt_OverloadedStrings, nop ),
+ ( "NumDecimals", Opt_NumDecimals, nop),
( "OverloadedLists", Opt_OverloadedLists, nop),
( "GADTs", Opt_GADTs, nop ),
( "GADTSyntax", Opt_GADTSyntax, nop ),
diff --git a/compiler/rename/RnPat.lhs b/compiler/rename/RnPat.lhs
index 205dde1..065fa62 100644
--- a/compiler/rename/RnPat.lhs
+++ b/compiler/rename/RnPat.lhs
@@ -60,9 +60,10 @@ import Outputable
import SrcLoc
import FastString
import Literal ( inCharRange )
-import Control.Monad ( when )
import TysWiredIn ( nilDataCon )
import DataCon ( dataConName )
+import Control.Monad ( when )
+import Data.Ratio
\end{code}
@@ -643,9 +644,21 @@ rnLit :: HsLit -> RnM ()
rnLit (HsChar c) = checkErr (inCharRange c) (bogusCharError c)
rnLit _ = return ()
+-- Turn a Fractional-looking literal which happens to be an integer into an
+-- Integer-looking literal.
+generalizeOverLitVal :: OverLitVal -> OverLitVal
+generalizeOverLitVal (HsFractional (FL {fl_value=val}))
+ | denominator val == 1 = HsIntegral (numerator val)
+generalizeOverLitVal lit = lit
+
rnOverLit :: HsOverLit t -> RnM (HsOverLit Name, FreeVars)
-rnOverLit lit@(OverLit {ol_val=val})
- = do { let std_name = hsOverLitName val
+rnOverLit origLit
+ = do { opt_NumDecimals <- xoptM Opt_NumDecimals
+ ; let { lit@(OverLit {ol_val=val})
+ | opt_NumDecimals = origLit {ol_val = generalizeOverLitVal (ol_val origLit)}
+ | otherwise = origLit
+ }
+ ; let std_name = hsOverLitName val
; (from_thing_name, fvs) <- lookupSyntaxName std_name
; let rebindable = case from_thing_name of
HsVar v -> v /= std_name
diff --git a/docs/users_guide/7.8.1-notes.xml b/docs/users_guide/7.8.1-notes.xml
index 6b5830e..e2a5785 100644
--- a/docs/users_guide/7.8.1-notes.xml
+++ b/docs/users_guide/7.8.1-notes.xml
@@ -165,6 +165,21 @@
</para>
</listitem>
</itemizedlist>
+
+ <itemizedlist>
+ <listitem>
+ <para>
+ There is a new extension,
+ <literal>NumDecimals</literal>, which allows you
+ to specify an integer using compact "floating
+ literal" syntax. This lets you say things like
+ <literal>1.2e6 :: Integer</literal> instead of
+ <literal>1200000</literal>
+
+ TODO FIXME: example?
+ </para>
+ </listitem>
+ </itemizedlist>
</sect3>
<sect3>
More information about the ghc-commits
mailing list