Live data from Hacker News

Show HN: Numbat – A programming language with physical dimensions as types

numbat.dev

71–80 of 122 posts

Re: Show HN: Numbat – A programming language with physical dimensions as types

#71

It’s interesting that they support currency units. Wonder how the conversion works. Probably have to provide conversion factor as well?

It'll fetch current conversation rates from the internet

Yes. We use up-to-date currency exchange rates from the European Central Bank [1].

https://github.com/sharkdp/numbat/blob/786512175b99c195a7d5b...

Re: Show HN: Numbat – A programming language with physical dimensions as types

#72
post #49

the classic problem for such number systems is linear algebra https://yosefk.com/blog/can-your-static-type-system-handle-l... the issue is that each column and each row of a matrix can have different units. worse, gauss-jordan elimination chooses which rows to operate on dynamically. there is eventually a solution to this problem in c++ far down the comments thread i don't see anything in https://numbat.dev/doc/type-…

Interesting article... though to be honest, I'm not sure I buy the premise.

In the article, he states: "Let's call the matrix of all (xi 1) `X` and let's call the vector of all yi `Y`", and then states that the units of `X` are (m 1).

But if you instead say the units of `X` are just m, the "1" is in units "m", then the problem goes away, the whole matrix has the same units ("m"), and everything works fine.

I'll be honest in that I've never thought of a matrix having multiple units per column... and I can't think of any example where you aren't just putting the units in the wrong place. Let me know if you have a concrete example that might work.

Re: Show HN: Numbat – A programming language with physical dimensions as types

#73
post #72
post #49

the classic problem for such number systems is linear algebra https://yosefk.com/blog/can-your-static-type-system-handle-l... the issue is that each column and each row of a matrix can have different units. worse, gauss-jordan elimination chooses which rows to operate on dynamically. there is eventually a solution to this problem in c++ far down the comments thread i don't see anything in https://numbat.dev/doc/type-…

Interesting article... though to be honest, I'm not sure I buy the premise. In the article, he states: "Let's call the matrix of all (xi 1) `X` and let's call the vector of all yi `Y`", and then states that the units of `X` are (m 1). But if you instead say the units of `X` are just m, the "1" is in units "m", then the problem goes away, the whole matrix has the same units ("m"), and everything works fine. I'll be ho…

If you multiply by a transform where the first entry is 1, the second entry is d/dx, the third entry is d^2/dx^2, etc. You will get a different unit for each entry of the vector.

Also most of economics consists of linear systems with non homogeneous units. The first entry could be bushels of wheat, the next demand for steel, etc.

Re: Show HN: Numbat – A programming language with physical dimensions as types

#76
post #31

Super cool! I love when programming languages try to do more with types. Julia has a neat little library called Unitful.jl[1] which does almost exactly what Numbat does by taking advantage of Julia's extremely flexible type system. Extending it to cover all of Numbat's functionality could be trivially accomplished in a few lines. In fact, fun type magic like this is pretty much my primary motivation for using Julia i…

I looked at Julia and Unitful.jl quite a bit when designing Numbat. It looks great.

> Extending it to cover all of Numbat's functionality could be trivially accomplished in a few lines.

Look, I'm not claiming that Numbat is superior. But I think Numbat might have its own little niche. And even if not, it's always good to have alternative solutions available.

- Numbat is specifically designed to handle physical dimensions and units. It has a special syntax to give developers the best experience when dealing with units. You can just type "KiB" and have it be parsed as kibibytes. You can use the "->" operator to convert to other units. You can directly annotate functions with physical dimensions ("fn kinetic_energy(m: Mass, v: Velocity) -> Energy = …"). Other languages and their unit libraries might have the same functionality, but it has always been added as an afterthought.

- Numbat has a static type system, Julia's is dynamic.

- Numbat can be compiled to Web Assembly and you can run it in your browser. I'm not sure if that is possible with Julia?

- Numbat might eventually be able to infer (physical dimension) function parameter types with its Hindley-Milner-style type system [1]. The only language that I know of that can do this is F#. The author of F#'s unit system wrote his PhD thesis on the subject [2], and Numbat follows the original approach in the thesis quite closely.

[1] https://github.com/sharkdp/numbat/issues/29

[2] https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-391.pdf

Re: Show HN: Numbat – A programming language with physical dimensions as types

#77
post #25

Or you could just use Nim [0], where this sort of thing can be implemented in Nim's macro system. Then you have a regular programming language combined with CT safe units. :) It even pretty much looks identical to those Numbat snippets! import unchained let earth_mass = 5.972168e24.kg let solar_mass = 1.9885e30.kg let lunar_mass = 7.342e22.kg let distance_sun = 1.AU # astronomical unit let distance_moon = 384_400.km…

Thank you for the reference. I hadn't seen Unchained.

You missed the point about this example though. I wanted to show how Numbat can help prevent the exact error that you made in your program. 'G_Newton * earth_mass * solar_mass / distance_sun' is not a force. It's an energy. How would you write type annotations in this Nim example to help you find that same mistake?

Re: Show HN: Numbat – A programming language with physical dimensions as types

#78

Frink ( https://frinklang.org/ ) is an older language with similar design goals. Frink runs on the JVM and is also available on Android. I use it as a general purpose calculator on my smartphone. It's really nice that Numbat is written in Rust :) Will have to try it out.

There's also Insect for interactive use: https://insect.sh/

Insect is the predecessor project. Numbat is the replacement. It also has an interactive version at https://numbat.dev/

Re: Show HN: Numbat – A programming language with physical dimensions as types

#79

Frink ( https://frinklang.org/ ) is an older language with similar design goals. Frink runs on the JVM and is also available on Android. I use it as a general purpose calculator on my smartphone. It's really nice that Numbat is written in Rust :) Will have to try it out.

Frink is not open source, unfortunately.

Re: Show HN: Numbat – A programming language with physical dimensions as types

#80
Impressive!

One thing I find unintuitive is the implementation of variable length units (like months or years) as an averaged constant. Then again, perhaps that’s the point - and e.g. working with months without specifying a calendar is just unintuitive.

Post reply on HN