Underscore in binary literals
Moritz Angermann
moritz at lichtzwerge.de
Tue Sep 26 13:43:30 UTC 2017
Hi,
I for one, would like to have this in more than just BinaryLiterals (which I find rather useful as well!)
I’d like to see `_` in any numeric literal being simply ignored, as I find it makes writing and reading
numbers in source code much easier.
let i = 1_000_000 :: Int
f = 1_123.456 :: Float
b = 0b1100_0011
And grouping (via underscore) might be very much domain specific. (One might want to denote magnitudes
or patterns.) I ended up writing a quite a bit of stupid boilerplate[1] to support readable binary
notation[2].
Cheers,
Moritz
—
[1]: https://github.com/angerman/data-bitcode/blob/c9818debd3dae774967c0507882b6b3bec7f0ee4/test/BitcodeSpec.hs#L22-L83
[2]: https://github.com/angerman/data-bitcode/blob/c9818debd3dae774967c0507882b6b3bec7f0ee4/test/BitcodeSpec.hs#L146-L150
> On Sep 26, 2017, at 9:19 PM, Takenobu Tani <takenobu.hs at gmail.com> wrote:
>
> Hi Rahul,
>
> Thanks for the explanation.
> Hmm, Is not there much need...
>
> Thank you code example.
> I will also look at lexer for my study [1].
>
> [1]: https://phabricator.haskell.org/D22
>
> Thank you :) ,
> Takenobu
>
>
> 2017-09-26 21:58 GMT+09:00 Rahul Muttineni <rahulmutt at gmail.com>:
> Implementation-wise, it's no so difficult to include - the lexer needs to be tweaked. But it seems like a specialised use-case that will only affect a minority of users is probably not worthwhile as an extension to the language/compiler.
>
> Maybe you can try using OverloadedStrings and implement this as a library?
>
> ```
> newtype Binary = Binary Integer
> deriving Num
>
> instance IsString Binary where
> fromString binaryLiteral = error "Code here to parse binary literal with underscores"
>
> binaryVal :: Binary
> binaryVal = "1101_1110_0101"
> ```
>
> Hope that helps,
> Rahul
>
> On Tue, Sep 26, 2017 at 8:40 AM, Takenobu Tani <takenobu.hs at gmail.com> wrote:
> Dear devs,
>
> GHC's BinaryLiterals extension is useful.
> (For example, x = 0b110111000101)
>
> Is it difficult to include underscore(_) in the format like Verilog-HDL[1] ?
> (For example, x = 0b1101_1100_0101)
>
> [1]: https://inst.eecs.berkeley.edu/~cs150/fa06/Labs/verilog-ieee.pdf#page=20
>
> Regards,
> Takenobu
>
>
> _______________________________________________
> ghc-devs mailing list
> ghc-devs at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
>
>
>
>
> --
> Rahul Muttineni
>
> _______________________________________________
> ghc-devs mailing list
> ghc-devs at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
—————————————————
Moritz Angermann
+49 170 54 33 0 74
moritz at lichtzwerge.de
lichtzwerge GmbH
Raiffeisenstr. 8
93185 Michelsneukirchen
Amtsgericht Regensburg HRB 14723
Geschäftsführung: Moritz Angermann, Ralf Sangl
USt-Id: DE291948767
Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte
Informationen. Wenn Sie nicht der richtige Adressat sind oder diese
E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den
Absender und vernichten Sie diese Mail.
Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail
ist nicht gestattet.
This e-mail may contain confidential and/or privileged information.
If you are not the intended recipient (or have received this e-mail in
error) please notify the sender immediately and destroy this e-mail.
Any unauthorized copying, disclosure or distribution of the material in
this e-mail is strictly forbidden.
More information about the ghc-devs
mailing list