News for hackers. Stuff that matters.
Re:NIR: Nim Intermediate Representation by jadbox on Saturday September 30, 2023 @05:59PM (#37720312)
I do really like Nim, being a sister to D lang with similar benefits. The language wars feels like it's getting hotter lately, and I'd live to see D/Nim make a comeback. If any language is going to see new traction, it's going to need to be a compelling alternative for Go/Rust/JS/Python/Java. I'm keeping an eye on Zig here too.
Re:NIR: Nim Intermediate Representation by elcritch on Saturday September 30, 2023 @06:41PM (#37720623)
I really enjoy writing code in Nim. The move to the new memory management system turns it into a true systems-language (comparable to C++ + SharedPtr's or Rust + ARC's everywhere). I've been working on a multi-threaded pure Nim GUI which runs 60fps like a charm and compiles down to a ~3MB binary with no deps outside the system UI libs!

The other part is that Nim's strongly typed and compiles to native code but allows you to do dependent typing like things that lets you be flexible on types:

    type
      X = object
      Y = object
      Z = object
      NotXY = not (X | Y)

    proc doSomething(_: NotXY) = echo "hi!"
Re:NIR: Nim Intermediate Representation by mathisfun123 on Saturday September 30, 2023 @06:47PM (#37720690)
What is `object` here? Effectively a ⊤?
Re:NIR: Nim Intermediate Representation by auxym on Saturday September 30, 2023 @06:52PM (#37720727)
It's effectively a struct with no members.

(Nim compiles to C, so in fact it compiles to exactly that).

Re:NIR: Nim Intermediate Representation by elcritch on Saturday September 30, 2023 @06:51PM (#37720714)
Nope, it's just an empty object, aka an empty C struct. Generics would be like:

    type MyObject[T] = object
        val: int
        other: T
Re:NIR: Nim Intermediate Representation by mathisfun123 on Saturday September 30, 2023 @06:53PM (#37720731)
I don't get it - if empty object doesn't act as a top how does what you wrote demonstrate flexibility?

Edit: oh X, Y, Z are singletons I guess? Still don't see the flexibility or dependent types?

Re:NIR: Nim Intermediate Representation by elcritch on Saturday September 30, 2023 @07:07PM (#37720832)
> I don't get it - if empty object doesn't act as a top how does what you wrote demonstrate flexibility?

To quote from Wikipedia:

    dependent types are used to encode logic's quantifiers like "for all" and "there exists". ... dependent types help reduce bugs by enabling the programmer to assign types that further restrain the set of possible implementations
In my example the objects could be any object types, combination of types, or concepts. It allows you to encode logic at the type system level. If you use generics with the example above you can do things roughly like:

    type
      State = enum Open, Closed
      SomeObject[T: State] = object
        val: int
      SomeObjectOpen = not SomeObject[state.Close]

    proc close(obj: SomeObjectOpen) = ...
You can do some simlar things in other languages, but it's hard and more limited and usually can't expressions.
Re:NIR: Nim Intermediate Representation by mathisfun123 on Saturday September 30, 2023 @07:15PM (#37720896)
> dependent types are used to encode logic's quantifiers like "for all" and "there exists"

Ya I don't think this is a good definition of dependent type - the prototypical example of a dependent type is a k-length vector. Obviously there's a mapping from what you wrote to this (in the same way there's almost always a mapping from a sequence of decision problems to an optimization problem) but it's still not a good formulation.

> In my example the objects could be any object types, combination of types, or concepts

Um yes this is literally what I was asking with respect to top ⊤.

> usually can't [contain] expressions

ya the negation is interesting - I wonder how it's implemented.

Re:NIR: Nim Intermediate Representation by elcritch on Saturday September 30, 2023 @07:50PM (#37721109)
> Ya I don't think this is a good definition of dependent type - the prototypical example of a dependent type is a k-length vector.

Yah that's why I qualified my statement as dependent type like setup as I don't know a good definition of dependent types which I really grok and I've only briefly dabbled in it. It'd be awesome if you could point out a good resource showing a clear example / simple proof.

Well I started working on statically typed vector concepts [1]. The compiler can't do `proc concat[N,M: static int](v1: Vector[N], v2: Vector[M}): Vector[N+M]` and it fails. It might be able to be implemented via a macro like `proc concat(v1: Vector[N], v2: Vector[M}): typeFromSumOf(N,M)`, but I haven't tried yet.

Also just using static ints would require specific values at some point to work I think. Whereas you'd really want "induction" of some sort. Maybe a SAT solver would be required at that point? There's DrNim [2] which does tie Z3 but it's sorta dormant.

> Um yes this is literally what I was asking with respect to top ⊤.

Ah, I didn't know what you meant by top T.

> ya the negation is interesting - I wonder how it's implemented.

Nim's VM runs from Nim AST, so probably running it as a VM expression.

1: https://github.com/elcritch/platonic 2: https://nim-lang.org/docs/drnim.html

Re:NIR: Nim Intermediate Representation by mathisfun123 on Saturday September 30, 2023 @07:57PM (#37721147)
> Nim's VM runs from Nim AST

What does this mean? There's a runtime VM or compile time VM?

Re:NIR: Nim Intermediate Representation by elcritch on Saturday September 30, 2023 @08:19PM (#37721271)
> What does this mean? There's a runtime VM or compile time VM?

Compile time VM. It's used to run macros / templates / concepts. You can also run most code at compile time in a `static` block except for stuff that needs C calls. You can also compile the VM into a program and use it as a runtime VM (see https://github.com/beef331/nimscripter) which I do in my GUI lib. NIR should enable the compile time VM to run faster too, and possibly use JIT'ed code.

Re:NIR: Nim Intermediate Representation by mathisfun123 on Saturday September 30, 2023 @09:38PM (#37721738)
ah so like clang's constexpr. got it. thanks
Re:NIR: Nim Intermediate Representation by elcritch on Saturday September 30, 2023 @08:45PM (#37721418)
P.S. I went through Wikipedia's definition a bit more. Not sure it's a full dependent types with the type pairs and stuff. Nim does let you do (constant) value to type conversion with expressions: https://play.nim-lang.org/#ix=4HRz or more oddly https://play.nim-lang.org/#ix=4HRA . That'd seem to satisfy the `Π` definition and lack of fixed codomain. Though I've never taken a set theory course so YMMV.
Re:NIR: Nim Intermediate Representation by foresto on Saturday September 30, 2023 @07:08PM (#37720837)
> I've been working on a multi-threaded pure Nim GUI

Any plans to develop this into something beyond a hobby project? I think a Qt rival in a language safer and more pleasant than C++ would be welcomed by more than a few people.

Re:NIR: Nim Intermediate Representation by elcritch on Saturday September 30, 2023 @08:58PM (#37721495)
That's good to hear! I've been considering it. Not sure if as a commercial product in it's own right - what to sell, maybe first class mobile app support?

At least I personally would like to use it to ship commercial cross-platform apps w/o needing electron or something huge like QT. Nim is much more pleasant than C++! You can actually embed the Nim compiler (~8MB) and run Nim as scripts too, so in theory you could remotely update your app as a nimscript.

P.S. drop me a line (emails on my github profile) if you're interested

Re:NIR: Nim Intermediate Representation by MaxBarraclough on Sunday October 01, 2023 @05:21AM (#37724212)
> something huge like QT

In defence of Qt, there's a lot of functionality in there, and they've made it modular.

To my knowledge Qt's library binaries have roughly the same file sizes as, say, their Gtk equivalents. I wouldn't expect a new competitor to be that different.

Re:NIR: Nim Intermediate Representation by pjmlp on Sunday October 01, 2023 @10:19AM (#37726171)
Delphi is still around as well,

https://www.embarcadero.com/products/delphi

Re:NIR: Nim Intermediate Representation by miki123211 on Saturday September 30, 2023 @07:29PM (#37720973)
Are you considering accessibility in your project at all?

What do you use for controls, native OS objects or something custom drawn? If the latter, you're in for a word of pain with screen reader compatibility, internationalization, right-to-left languages, handling of Asian input methods etc.

Re:NIR: Nim Intermediate Representation by elcritch on Saturday September 30, 2023 @09:26PM (#37721660)
Not currently but it's on my mind and I'm using utf8 for text with utf-32 for rendering. Good links for screen reader info are welcome!

> If the latter, you're in for a word of pain with screen reader compatibility, internationalization, right-to-left languages, handling of Asian input methods etc.

Custom drawn, and yep it's a pain! Actually emoji's seem the hardest (e.g. smiley face + skin tone).

The windowing library I'm building upon uses the OS'es input method editors (IME). For example MacOS'es functions for composing letters like `é` already works. Hopefully Asian input methods will work via the OS'es IME. Luckily I know a bit of (modern) Hebrew, so I'll can add basic RTL languages soon.

Re:NIR: Nim Intermediate Representation by ddlsmurf on Sunday October 01, 2023 @12:02AM (#37722410)
Why not use something like pango, libschrift, etc ? Text rendering is a hard problem, moreso one cross-platform...
Re:NIR: Nim Intermediate Representation by elcritch on Sunday October 01, 2023 @01:50AM (#37723037)
Is it hard! Font rendering is handled by Pixie and the guys who wrote it made a great NimConf video showing that https://www.youtube.com/watch?v=8acDfUIwLnk

Pixie can render svg and ttf fonts with unicode in addition to drawing the shapes used for boxes and lines (e.g. like pango+cairo). It's SIMD optimized, very fast, and pure Nim so cross platform.

The challenge I mentioned with emojis is text selection and cursor movement when dealing with multi-grapheme characters. :)

edit: see https://youtu.be/8acDfUIwLnk?si=1EAUwUNZqje88J86&t=382 where they discuss how hard Text is

Re:NIR: Nim Intermediate Representation by ddlsmurf on Sunday October 01, 2023 @10:13AM (#37726131)
I didn't know Pixie, thanks !
Re:NIR: Nim Intermediate Representation by codethief on Sunday October 01, 2023 @11:04AM (#37726589)
> I've been working on a multi-threaded pure Nim GUI which runs 60fps like a charm and compiles down to a ~3MB binary with no deps outside the system UI libs!

Like the sibling I'd be very much interested in this! Have you considered open-sourcing the GUI library part?

Re:NIR: Nim Intermediate Representation by winrid on Saturday September 30, 2023 @06:11PM (#37720416)
Just needs good IDE support. That's always the issue. Do not like VSCode or the slowness of the Nim plugin. I think every "goto definition" scans the whole code base instead of using an index, so it's slow. I'm faster working in Java, verbosity be damned.
Re:NIR: Nim Intermediate Representation by michaelsbradley on Saturday September 30, 2023 @06:20PM (#37720478)
You may fare better hooking VSCode to nimlangserver:

https://github.com/saem/vscode-nim#nim-lanugage-server-integ...

Re:NIR: Nim Intermediate Representation by winrid on Saturday September 30, 2023 @07:53PM (#37721125)
Thanks! Still don't want to use VSCode. I'd rather not use nim until there's a supported Clion plugin.
Re:NIR: Nim Intermediate Representation by memco on Saturday September 30, 2023 @09:58PM (#37721838)
There is a number plugin: https://plugins.jetbrains.com/plugin/15128-nim. I haven’t tried it so don’t know how well it works.
Re:NIR: Nim Intermediate Representation by winrid on Saturday September 30, 2023 @10:28PM (#37721986)
Yes, as I said, a supported plugin. The author has not updated it since Jan so its not compatible anymore, and it's in beta at best.
Re:NIR: Nim Intermediate Representation by izoow on Saturday September 30, 2023 @07:06PM (#37720821)
I’d love to see nim become more mainstream. I don’t have much experience with it, but I looked into it a few times and always found it super interesting.
Re:NIR: Nim Intermediate Representation by netbioserror on Saturday September 30, 2023 @07:12PM (#37720871)
Nim is incredibly pleasant for actually engineering software. In most scenarios it feels like compiled Python, and when you need it, you get really precise control. The preference for referentially-transparent procedures and stack-managed dynamic data is icing on the cake. You don't have to write just about any memory-semantic code in most business scenarios. Feels like a scripting language, runs 10-100x faster, and add the power of static types.
Re:NIR: Nim Intermediate Representation by ternaryoperator on Saturday September 30, 2023 @08:52PM (#37721465)
I've never heard of Nim being a sister to D. Maybe to Python, but not D.

Nim's home page acknowledges borrowing from Python, Ada, and Modula, as well as C/C++. I don't think D is part of the mix.

Re:NIR: Nim Intermediate Representation by winrid on Saturday September 30, 2023 @11:58PM (#37722390)
Nim is closer to D than Python IMO. The syntax is like Python, sure, but in terms of features it's very similar to D (statically typed, supports multiple GCs, fast compiler).
Re:NIR: Nim Intermediate Representation by klibertp on Sunday October 01, 2023 @04:41AM (#37723995)
As I understand, D is a pragmatic vision of how C++ could look like if it was not C++ (that is, not designed by committee, without decades of backward compat needed, without the need to support some exotic hardware, etc.) It aims to completely drop the "C with classes" heritage, bring OOP and FP together while integrating important concepts into the language (delegates instead of function pointers, strings instead of char arrays, slices instead of T[], etc.)

While Nim indeed comes from Wirthian heritage instead of K&R heritage, its goals are pretty similar to D. You could say that Nim is for (Object)Pascal what D is for C(++). I think that's what the GP alluded to - not that Nim borrows directly from D or the other way around.

Re:NIR: Nim Intermediate Representation by jadbox on Monday October 02, 2023 @02:28PM (#37742398)
For the internet archives, this was the correct interpretation. Apologies for the original ambiguity. Also, I believe the creators of Nim where active in D language forum conversations, and I had the impression the Nim authors where very well inspired from D's design.
Re:NIR: Nim Intermediate Representation by ksec on Sunday October 01, 2023 @12:43AM (#37722639)
> being a sister to D lang with similar benefits.

That is the fist time I hear this. Other than Python, I always felt Nim is more like Pascal and Ada.

Re:NIR: Nim Intermediate Representation by pjmlp on Sunday October 01, 2023 @07:18AM (#37724835)
I don't see a value in Zig until they have a sound story for UAF.

We should be getting back what Xerox PARC was already doing in systems programming with Mesa/Cedar, Interlisp-D and Smalltalk in terms of systems programming.

This is where I see a value in languages like D and Nim, Swift, or even Go, regardless of the opinion on much systems it can be used for.

Back in my day writig compilers, linkers and OS services was considered systems programming.

Re:NIR: Nim Intermediate Representation by jstimpfle on Sunday October 01, 2023 @08:50AM (#37725441)
What were they doing? From all I can tell they require to hold down the shift key a lot, other than that it's hard to find good information how they offered anything not offered by contemporary ecosystems. Or how they offered a better set of tradeoffs in some situations than we can have today.
Re:NIR: Nim Intermediate Representation by pjmlp on Sunday October 01, 2023 @09:39AM (#37725856)
Easy to find out when one cares, all the stuff is on Bitsavers and Computer History Museum sessions.

A small taste,

"Eric Bier demonstrates Cedar"

https://www.computerhistory.org/collections/catalog/10278104...

Full graphics workstation in 1980's, implementated in a systems programming language using reference counting, with a tracing GC as cycle collector, with an object model partially implemented years later as OLE/COM.

Re:NIR: Nim Intermediate Representation by jstimpfle on Sunday October 01, 2023 @10:04AM (#37726050)
Watching the video, thanks. But back to my question, what of those is missing today?
Re:NIR: Nim Intermediate Representation by pjmlp on Sunday October 01, 2023 @10:12AM (#37726124)
Systems programming in a memory safe language with automatic memory management, across the whole OS, with a type safe ABI supporting dynamic loading, as starting point.

Swift is the closest to it, and it is still not widely adopted across Apple ecosystem, although as per CppCon 2023's Apple talk they even have plans to push it into the kernel (long term).

Re:NIR: Nim Intermediate Representation by jstimpfle on Monday October 02, 2023 @06:07PM (#37745121)
"Across the whole OS" is nothing special, it just means there hasn't been enough interest by diverse enough users of the OS. This is a research system, which is different from an OS supporting a huge diversity of ecosystems through the past 3 decades or so. There are different forces at play.

I'm sure there are quite a few systems worked on by lone developers today that make for a nice and smooth demo. The hard part is getting adoption and making it ergonomic and performant for a variety of uses, not just demo stuff.

Re:NIR: Nim Intermediate Representation by jstimpfle on Sunday October 01, 2023 @08:55AM (#37725481)
A fundamental problem that I see today ist that most systems end up being either too rigid or too unstructured. We always want guardrails but then we need ways to remove them. What sounds good in theory or looks amazing on a demo often becomes impractical in a realistic development setting.

(There is a hint on something like that wrt. Mesa/Cedar on wikipedia. FWIW -- I've obviously never used it myself)

Re:NIR: Nim Intermediate Representation by ylluminate on Sunday October 01, 2023 @11:03PM (#37733482)
V is doing really well as well. Very exciting times.