Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overwrite units #293

Open
SollyBunny opened this issue Jan 18, 2024 · 11 comments
Open

Overwrite units #293

SollyBunny opened this issue Jan 18, 2024 · 11 comments
Labels

Comments

@SollyBunny
Copy link

I'm trying to make a module which makes numbat align with an exam board's constants
Constants can be overridden but not units, this restriction is quite annoying
This is probably like this so functions don't break, this can be fixed a number of ways:

  1. with an unsafe block (tell numbat "I totes know what I'm doing")
  2. only disallowing redefinition if the unit is used in a function (but then you'd have to keep track of where units are used)
  3. only allow redefinitions with the same dimension / units
  4. have units based of constants which can change (and have the units update when the constants do)
  5. allow redefinitions in modules (as those are normally written with more thought)
unit planck_length = sqrt(h_bar × G / c^3)
     ^^^^^^^^^^^^^ identifier is already in use

The fact you can't overwrite units also means that if you define a unit, you're stuck with it

@eminence
Copy link
Contributor

Some other solutions:

  • Run with --no-prelude and define your own custom prelude (this is probably my first recommendation)
  • Use a different name: unit planck_length_custom = sqrt(h_bar × G / c^3)

@SollyBunny
Copy link
Author

SollyBunny commented Jan 19, 2024

  • Run with --no-prelude and define your own custom prelude (this is probably my first recommendation)

I want to be able to swap easily without interacting with command line

  • Use a different name: unit planck_length_custom = sqrt(h_bar × G / c^3)

This isn't very seemless

Not to discredit these suggestions (specially the first), but if its feasable and easy enough to overwrite units, it would be a nice restriction to have lifted

@eminence
Copy link
Contributor

Sure, overwriting units is still a feature we should consider. But in the meantime, I wanted to share some possible workarounds

@masa300V
Copy link

I echo @SollyBunny .

I want to replace the default definition of electric-resistance "ohm" as its short-form "Ω" is nothing easy to type BUT I want to keep the rest of all these nice defaults. I don't want to copy entire defaults just for my single tweak.

@eminence
Copy link
Contributor

Normally you can get the ohm symbol by typing \ohm and then pressing tab. Unfortunately this produces a symbol (U+2126 OHM SIGN), but the ohm unit is currently defined using the Ω symbol (U+03A9 GREEK CAPITAL LETTER OMEGA). I'm opening a dedicated issue about this inconsistency. This doesn't invalidate your comment/request, but I did want to highlight this easy way to accessing hard-to-type symbols

@masa300V
Copy link

@eminence
Thanks. I remember I read this trick of "\ohm" and TAB in the manual somewhere...

Still, to me as an electronics engineer, I'm fine to use "ohm" as a short-form of resistance and that's 3-key-type (where "\ohm" + TAB is 5-key-type) :-P

Appreciate to implement "unit" overriding feature.

@sharkdp
Copy link
Owner

sharkdp commented Jan 27, 2024

I want to replace the default definition of electric-resistance "ohm" as its short-form "Ω" is nothing easy to type BUT I want to keep the rest of all these nice defaults.

Still, to me as an electronics engineer, I'm fine to use "ohm" as a short-form of resistance and that's 3-key-type

I don't quite understand. You can already use ohm. If you want something shorter, you can set unit o = ohm or whatever you prefer in ~/.config/numbat/init.nbt.

Overwriting units is only needed if you want to change the actual unit definition.

@sharkdp
Copy link
Owner

sharkdp commented Jan 27, 2024

I'm trying to make a module which makes numbat align with an exam board's constants

@SollyBunny I'm not questioning that it might be useful in some cases to overwrite units. But so far, I haven't seen a compelling use case. Can you go into a bit more detail what kind of changes you are thinking of?

unit planck_length = sqrt(h_bar × G / c^3)

planck_length is already defined as sqrt(ℏ G / c^3) 1. Or was that just a random example?

Footnotes

  1. https://github.com/sharkdp/numbat/blob/414d2db5757b0a31ba9e4ec6abf91fc7b4bbe0b7/numbat/modules/units/planck.nbt#L5-L7

@masa300V
Copy link

I want to replace the default definition of electric-resistance "ohm" as its short-form "Ω" is nothing easy to type BUT I want to keep the rest of all these nice defaults.

Still, to me as an electronics engineer, I'm fine to use "ohm" as a short-form of resistance and that's 3-key-type

I don't quite understand. You can already use ohm. If you want something shorter, you can set unit o = ohm or whatever you prefer in ~/.config/numbat/init.nbt.

Overwriting units is only needed if you want to change the actual unit definition.

I want to use milli-ohm or micro-ohm in a short form, "mohm"/"uohm", and this error is blocking me.
image

@SollyBunny
Copy link
Author

@SollyBunny I'm not questioning that it might be useful in some cases to overwrite units. But so far, I haven't seen a compelling use case. Can you go into a bit more detail what kind of changes you are thinking of?

unit planck_length = sqrt(h_bar × G / c^3)

planck_length is already defined as sqrt(ℏ G / c^3) 1. Or was that just a random example?

Yes, but at the time of parsing of the definition, it is set as stone when h_bar is 1.05457e-34 J/Hz, if h_bar got changed (to the value my exam board uses), planck_length doesnt change with it. this goes for all units

Someone pointed out I can just disable prelude, but that is not very seemless, I'd like you to be able to change it.

The unit could be evaluated every time it is used so if h_bar changes the unit changes dynamically with it, but I'd like it just to be possible to overwrite the units

@sharkdp
Copy link
Owner

sharkdp commented Jan 29, 2024

Thank you for the clarifications. I think those are reasonable requests, and I actually thought about this a few months ago here: #86. Maybe that @overwrite decorator is still the most reasonable solution for this?

only allow redefinitions with the same dimension / units

That might be required because Numbat is statically typed. Imagine sth like:

unit a: Length = 10 meters

fn foo() -> Length = 1 a + 2 meters  # Fine

@overwrite
unit a: Time = 10 seconds

foo() # oops

Implementing this proposal might actually not be too hard (I think we need to lift a restriction somewhere), but we should properly test it and think about potential pitfalls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants