Stream: helpdesk (published)

Topic: ✔ help with Unicode variables defining a Bit type with ...


view this post on Zulip Peter Goodall (Nov 16 2021 at 00:46):

I am trying to create a Bit type - I'm not interested in efficiency - I'm playing with a proof of bitstrings with XOR as an Abelian Group.

Why are \ttone and \ttzero invalid characters? I'm getting them from https://docs.julialang.org/en/v1/manual/unicode-input/

fails:

julia> @enum 𝔹 𝟶 𝟷
ERROR: syntax: invalid character "𝟶" near column 9
Stacktrace:
 [1] top-level scope
   @ none:1

julia> typeof(𝟷)
ERROR: syntax: invalid character "𝟷" near column 8
Stacktrace:
 [1] top-level scope
   @ none:1

works:

julia> @enum 𝔹 α β

view this post on Zulip Peter Goodall (Nov 16 2021 at 04:09):

I can't find any contradiction in Julia - Allowed Variable Names

\ttzero = U+1D7F6

view this post on Zulip Sukera (Nov 16 2021 at 08:08):

seems like a parsing error

view this post on Zulip Sebastian Pfitzner (Nov 16 2021 at 08:56):

number-like chars are usually not allowed to start an identifier

view this post on Zulip Sebastian Pfitzner (Nov 16 2021 at 08:56):

so a𝟶 parses fine

view this post on Zulip Sukera (Nov 16 2021 at 08:59):

right, \ttone is not a letter

julia> '𝟷'
'𝟷': Unicode U+1D7F7 (category Nd: Number, decimal digit)

view this post on Zulip Sukera (Nov 16 2021 at 09:00):

maybe a string macro for creating your "bitstrings" is better suited?

view this post on Zulip Peter Goodall (Nov 16 2021 at 18:34):

@Sebastian Pfitzner That looks like what the parser was complaining about, but the documentation says:

Allowed Variable Names
Variable names must begin with a letter (A-Z or a-z), underscore, or a subset of Unicode code points greater than 00A0

And then (IMHO) it mumbles a bit...

view this post on Zulip Sebastian Pfitzner (Nov 16 2021 at 18:34):

It does say "Subsequent characters may also include ! and digits (0-9 and other characters in categories Nd/No)", which kinda implies that those are not allowed at the start

view this post on Zulip Peter Goodall (Nov 16 2021 at 18:38):

My lack of unicode experience is showing... Unicode Categories are now decrypted for me.
List of Unicode Characters of Category “Decimal Number”

view this post on Zulip Peter Goodall (Nov 16 2021 at 18:42):

Sort of happy with this:

> @enum 𝔹 zero one

> (a::𝔹, b::𝔹) = a == b ? zero : one
 (generic function with 1 method)

> @show (zero  one, one  one)
(zero  one, one  one) = (one, zero)

view this post on Zulip Sukera (Nov 16 2021 at 18:54):

you're kind of punning the functions zero and one though

view this post on Zulip Sukera (Nov 16 2021 at 18:55):

julia is very unicode aware

view this post on Zulip Peter Goodall (Nov 16 2021 at 19:42):

@Sukera

you're kind of punning the functions zero and one though

I'm not sure what you mean by functions. I thought I was defining a data-type with two elements zero and one, which I am giving behavior by defining the function .

Cheers...

view this post on Zulip Sebastian Pfitzner (Nov 16 2021 at 19:47):

that will locally shadow the one/zero functions that Base exports

view this post on Zulip Sebastian Pfitzner (Nov 16 2021 at 19:47):

which isn't necessarily bad though

view this post on Zulip Peter Goodall (Nov 16 2021 at 19:48):

If do:

julia> typeof(zero)
Enum 𝔹:
zero = 0
one = 1

The enumeration, to my mind - coincidentally, assigns them integers corresponding to their 'emotional' value.
This works as well:

> @enum 𝔹 zero=3 one=7

> (a::𝔹, b::𝔹) = a == b ? zero : one

> @show (zero  one, one  one)
(zero  one, one  one) = (one, zero)

I could have given them boolean values - which I might like to do?

view this post on Zulip Peter Goodall (Nov 16 2021 at 19:52):

oh boy...

view this post on Zulip Mason Protter (Nov 16 2021 at 19:53):

Not sure what to say other than to re-iterate what Sebastian said. one and zero are functions exported by Base

julia> zero(Float64)
0.0

julia> one(Complex{Rational{Int}})
1//1 + 0//1*im

When you do

@enum 𝔹 zero one

you're locally changing the meaning of those identifiers to be what your Enum defines

view this post on Zulip Mason Protter (Nov 16 2021 at 19:53):

Which is fine

view this post on Zulip Mason Protter (Nov 16 2021 at 19:53):

And you can still access Base.one and Base.zero by qualifying the names

view this post on Zulip Mason Protter (Nov 16 2021 at 19:54):

julia> @enum 𝔹 zero one

julia> zero(Float64)
ERROR: MethodError: objects of type 𝔹 are not callable
Stacktrace:
 [1] top-level scope
   @ REPL[5]:1

julia> Base.zero(Float64)
0.0

view this post on Zulip Peter Goodall (Nov 16 2021 at 19:55):

I made a joke - which I couldn't see :-))

view this post on Zulip Peter Goodall (Nov 16 2021 at 19:56):

Is there a way of creating a type in Julia, which has a set of elements, without using @enum?
Though now all you kind people have shown me what I was really doing, I think for the purposes of this exercise, I have something that works.

view this post on Zulip Mason Protter (Nov 16 2021 at 20:01):

You could use an inner constructor to reject invalid data

julia> struct 𝔹²
           x::Int
           y::Int
           𝔹²(x,y) = if (x,y)  Iterators.product((0,1), (0,1))
               new(x,y)
           else
               throw("Invalid booleans $((x,y))")
           end
       end

julia> 𝔹²(1, 0)
𝔹²(1, 0)

julia> 𝔹²(0, 0)
𝔹²(0, 0)

julia> 𝔹²(0, 2)
ERROR: "Invalid booleans (0, 2)"
Stacktrace:
 [1] 𝔹²(x::Int64, y::Int64)
   @ Main ./REPL[8]:7
 [2] top-level scope
   @ REPL[11]:1

view this post on Zulip Peter Goodall (Nov 16 2021 at 22:59):

I found this: Style recommendation for enum as type, which has discussion on the difference between enumerated types, and types that can be dispatched apon.

view this post on Zulip Notification Bot (Nov 17 2021 at 10:44):

Alexander has marked this topic as unresolved.

view this post on Zulip Notification Bot (Nov 17 2021 at 10:45):

Alexander has marked this topic as resolved.

view this post on Zulip Peter Goodall (Nov 17 2021 at 21:05):

from an out-of-the-box REPL:

julia> zero(true)
false
julia> zero(false)
false
julia> one(false)
true
julia> one(true)
true

Learning by Breaking

view this post on Zulip rocco sprmnt21 (Nov 17 2021 at 21:24):

Sebastian Pfitzner said:

It does say "Subsequent characters may also include ! and digits (0-9 and other characters in categories Nd/No)", which kinda implies that those are not allowed at the start

"Subsequen t characters may also include ! and digits (0-9 and other characters in categories Nd/No)", implies that characters other than a-z, _, A-Z,!, 0-9, "subset of Unicode code points greater than 00A0" and so on, are not allowed either at the beginning or as subsequent characters, in my opinion.
In any case, the definition seems to me not "clean" as it speaks as allowed characters of UN SUBSET unspecified of Unicode code points greater than 00A0
It is not clear whether all code points greater than 00A0 or only some (which?) of them are intended.

view this post on Zulip Sukera (Nov 17 2021 at 21:46):

yes, Bool is a Integer in julia

view this post on Zulip Mason Protter (Nov 18 2021 at 21:23):

which IMO is much more sensible than the reverse where a lot of languages treat integers as booleans

view this post on Zulip Sukera (Nov 18 2021 at 21:27):

yes, but only if you error when trying to index with a boolean

view this post on Zulip Sukera (Nov 18 2021 at 21:27):

else writing x > 4 instead of x >> 4 in an indexing expression by accident is horrible to debug


Last updated: Nov 22 2024 at 04:41 UTC