diff options
author | Austin Seipp <aseipp@pobox.com> | 2013-08-28 17:14:06 -0500 |
---|---|---|
committer | Austin Seipp <aseipp@pobox.com> | 2013-08-28 20:15:47 -0500 |
commit | a6be6f1bd30c1476718392a259cfccf082d0da4d (patch) | |
tree | fd63a4d6b879bc94b23bc1b2721365cf0a415899 | |
parent | 9e02b0260ef5c6571b3fc1482401fd9668e2e507 (diff) | |
download | haskell-a6be6f1bd30c1476718392a259cfccf082d0da4d.tar.gz |
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@gmail.com>
Signed-off-by: Austin Seipp <aseipp@pobox.com>
-rw-r--r-- | compiler/main/DynFlags.hs | 2 | ||||
-rw-r--r-- | compiler/rename/RnPat.lhs | 19 | ||||
-rw-r--r-- | 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 849d8a418c..4d19519845 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 205dde1969..065fa62f6c 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 6b5830e528..e2a5785d32 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> |