Underscore in binary literals

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Underscore in binary literals

Takenobu Tani
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
Reply | Threaded
Open this post in threaded view
|

Re: Underscore in binary literals

Rahul Muttineni
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 <[hidden email]> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs




--
Rahul Muttineni

_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
Reply | Threaded
Open this post in threaded view
|

Re: Underscore in binary literals

Takenobu Tani
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 <[hidden email]>:
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 <[hidden email]> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs




--
Rahul Muttineni


_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
Reply | Threaded
Open this post in threaded view
|

Re: Underscore in binary literals

Moritz Angermann
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 <[hidden email]> 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 <[hidden email]>:
> 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 <[hidden email]> 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
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
>
>
>
>
> --
> Rahul Muttineni
>
> _______________________________________________
> ghc-devs mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs

—————————————————
Moritz Angermann
+49 170 54 33 0 74
[hidden email]

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.

_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
Reply | Threaded
Open this post in threaded view
|

Re: Underscore in binary literals

Ben Gamari-2
In reply to this post by Takenobu Tani
Takenobu Tani <[hidden email]> writes:

> 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].
>
If you do want to try pursuing a language change do feel free to submit
a proposal [1]. I would also like the ability to break up large
literals. However, why limit it to BinaryLiterals? I would like the same
syntax for deciaml and hexadecimal literals as well.

Cheers,

- Ben


[1] https://github.com/ghc-proposals/ghc-proposals

_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs

signature.asc (497 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Underscore in binary literals

Niklas Hambüchen
In reply to this post by Takenobu Tani
I'd find that quite useful for hex and binary.

It's useful for distinguishing e.g. 0xffffffff and 0xfffffff which when
confused accidentally and lead to big bugs.

Rust has exactly this feature for all numeric literals:

  https://rustbyexample.com/primitives/literals.html

On 26/09/17 14:40, Takenobu Tani wrote:
> 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)
_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
Reply | Threaded
Open this post in threaded view
|

Re: Underscore in binary literals

Merijn Verstraaten
In reply to this post by Ben Gamari-2
I, too, have wished for the ability to have a separator in large number literals.

So a strong +1 from me.

Cheers,
Merijn

> On 26 Sep 2017, at 15:43, Ben Gamari <[hidden email]> wrote:
>
> Takenobu Tani <[hidden email]> writes:
>
>> 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].
>>
> If you do want to try pursuing a language change do feel free to submit
> a proposal [1]. I would also like the ability to break up large
> literals. However, why limit it to BinaryLiterals? I would like the same
> syntax for deciaml and hexadecimal literals as well.
>
> Cheers,
>
> - Ben
>
>
> [1] https://github.com/ghc-proposals/ghc-proposals
> _______________________________________________
> ghc-devs mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs

_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs

signature.asc (891 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Underscore in binary literals

Takenobu Tani
Dear all,

Thank you very much for the response and kind explanation.
After studying, I will submit to ghc-proposals :)

Regards,
Takenobu



2017-09-26 22:56 GMT+09:00 Merijn Verstraaten <[hidden email]>:
I, too, have wished for the ability to have a separator in large number literals.

So a strong +1 from me.

Cheers,
Merijn

> On 26 Sep 2017, at 15:43, Ben Gamari <[hidden email]> wrote:
>
> Takenobu Tani <[hidden email]> writes:
>
>> 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].
>>
> If you do want to try pursuing a language change do feel free to submit
> a proposal [1]. I would also like the ability to break up large
> literals. However, why limit it to BinaryLiterals? I would like the same
> syntax for deciaml and hexadecimal literals as well.
>
> Cheers,
>
> - Ben
>
>
> [1] https://github.com/ghc-proposals/ghc-proposals
> _______________________________________________
> ghc-devs mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs




_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
Reply | Threaded
Open this post in threaded view
|

Re: Underscore in binary literals

Takenobu Tani
Dear devs,

> Thank you very much for the response and kind explanation.
> After studying, I will submit to ghc-proposals :)

I submitted a ghc-proposal #76 [1].
Please feedback:)

[1]: https://github.com/ghc-proposals/ghc-proposals/pull/76

Regards,
Takenobu


2017-09-26 23:05 GMT+09:00 Takenobu Tani <[hidden email]>:
Dear all,

Thank you very much for the response and kind explanation.
After studying, I will submit to ghc-proposals :)

Regards,
Takenobu



2017-09-26 22:56 GMT+09:00 Merijn Verstraaten <[hidden email]>:
I, too, have wished for the ability to have a separator in large number literals.

So a strong +1 from me.

Cheers,
Merijn

> On 26 Sep 2017, at 15:43, Ben Gamari <[hidden email]> wrote:
>
> Takenobu Tani <[hidden email]> writes:
>
>> 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].
>>
> If you do want to try pursuing a language change do feel free to submit
> a proposal [1]. I would also like the ability to break up large
> literals. However, why limit it to BinaryLiterals? I would like the same
> syntax for deciaml and hexadecimal literals as well.
>
> Cheers,
>
> - Ben
>
>
> [1] https://github.com/ghc-proposals/ghc-proposals
> _______________________________________________
> ghc-devs mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs





_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs