Commit a6be6f1b authored by thoughtpolice's avatar thoughtpolice

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's avatarShachaf Ben-Kiki <shachaf@gmail.com>
Signed-off-by: thoughtpolice's avatarAustin Seipp <aseipp@pobox.com>
parent 9e02b026
......@@ -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 ),
......
......@@ -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
......
......@@ -161,6 +161,21 @@
allows you to declare a type class without any
parameters.
TODO FIXME: example?
</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>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment