Live data from Hacker News

Zig: software should be perfect [video]

youtube.com

61–70 of 141 posts

Re: Zig: software should be perfect [video]

#61
post #59

Earlier quoted context omitted.

While I agree with the content of your post literally, I think we often underestimate the importance of software reliability and performance, and end up giving it less attention than it deserves. I understand the place for rapid prototyping etc., and that not every software application deals with life-and-death situations — but even those that aren’t, I think our industry suffers a bit here. For example, to this day…

In over 6 years working in this industry, in projects with anywhere from dozens of users to millions of users, not a single bug I have encountered was caused by a "hidden allocation" causing the process to go OOM. Not one instance. One of the two examples he cited in his introduction, the Android one, wasn't even caused by an unhandled OOM error. Android by design will kill unused processes if they're occupying memor…

The vast majority of bugs aren't are due to corner cases that the programmer didn't think about. Quickcheck and related techniques expose many more bugs than eliminating OOM bugs.

Re: Zig: software should be perfect [video]

#62
post #8

Earlier quoted context omitted.

Come on, a title like that isn’t meant to be taken too seriously. And you’re reaching a bit far. You can write a perfect function that adds two 32 bit integers. There is a subset of code that can be written perfectly. Especially if you don’t need Turing completeness to write it. Zig just tries it’s best to make it easy to write as much as possible of low level code in a perfect way.

> You can write a perfect function that adds two 32 bit integers. how ? the problem of adding two 32 bits integers is itself imperfect since you may at some point have big integers to sum, so any solution is inherently flawed, too

The problem can be perfect depending on how it's stated. If you spell out the contract right, then it can be perfect. Some examples:

Given two 32-bit signed integers, produce a 32-bit signed integer that is a sum of the inputs; if the sum doesn't fit into 32 bits, wrap modulo 32 bits.

Given two 32-bit signed integers, produce a 64-bit signed integer that is a sum of the inputs.

Given two 32-bit signed integers, produce either a 32-bit signed integer that is a sum of the inputs if it fits into 32 bits, or an error code indicating that it did not fit.

The problem with languages like C is that they let you be imprecise and get away with in. If you just run with "add two signed 32-bit integers", and implement it as "x + y" in C, it will compile, and it will run, but the result is undefined behavior if it overflows (note: it doesn't even mean that you get the wrong value - it can literally do anything at all). In Zig, if you write it as "x + y", it will panic on overflow, which is at least well-defined and fails fast; and the language provides you with tools to implement any of the three properly defined scenarios above in straightforward ways (i.e. you can add with wraparound, or you can add with an overflow check).

Re: Zig: software should be perfect [video]

#63
post #49
post #42

Earlier quoted context omitted.

There are still plenty of exceptions that are unchecked, i.e. subtypes of RuntimeException, which do not have to be declared on the callee. As well as people who think it's a good idea to throw instances of Exception and just tack a "throws Exception" on their methods.

Right — that's a failing of Java the language; not the concept of checked exceptions. At least one problem with Java's exception model that Zig does not have is that some exceptions are "unchecked."

Zig has system-exit, which is strictly less useful than unchecked exceptions, so Zig certainly has the same problem.

Re: Zig: software should be perfect [video]

#64
post #20

Re: performance claims, I wonder if the C sha256 implementation would have been more competitive with -march=native -mtune=native?

Why isn't `-march=native -mtune=native` enabled by-default for every piece of software compiled unless explicitly specified otherwise?

Because your cpu has a base ISA, plus extensions. The compiler doesn't know if you're going to only run this binary on your machine, or distribute it to others that may share your base ISA but possibly not your extensions, so it takes the conservative approach and doesn't use them unless signaled to do so via those compiler flags. Also, I think -march implies -mtune.

Re: Zig: software should be perfect [video]

#65
post #8

Earlier quoted context omitted.

Come on, a title like that isn’t meant to be taken too seriously. And you’re reaching a bit far. You can write a perfect function that adds two 32 bit integers. There is a subset of code that can be written perfectly. Especially if you don’t need Turing completeness to write it. Zig just tries it’s best to make it easy to write as much as possible of low level code in a perfect way.

> You can write a perfect function that adds two 32 bit integers. how ? the problem of adding two 32 bits integers is itself imperfect since you may at some point have big integers to sum, so any solution is inherently flawed, too

> the problem of adding two 32 bits integers is itself imperfect since you may at some point have big integers to sum, so any solution is inherently flawed, too

Smalltalk solves that problem by promoting the result to arbitrary precision arithmetic. It does the same for integer division. For example, 5 / 2 returns a Fraction, not an Integer.

Re: Zig: software should be perfect [video]

#66

I've wanted a language like this. Java's checked exceptions with some way to offload the bookkeeping to the compiler. What about other run-time exceptions, like divide by zero? Are they checked? What about Hoare's billion-dollar mistake (null pointer exceptions)? Does Zig have non-nullable references?

Zig has a bunch of runtime safety checking. It applies to divide by zero as well as integer overflow, using the wrong union field, and many more. The runtime safety checks are enabled in Debug and ReleaseSafe mode and disabled in ReleaseFast and ReleaseSmall mode. (Actually they are used as assertions to the optimizer so that it can rely on extra stuff being undefined behavior.)

Pointers can not be null. However you can have optional pointers which are guaranteed to use the 0x0 value as null and have the same size as normal pointers.

Re: Zig: software should be perfect [video]

#67

Earlier quoted context omitted.

Why isn't `-march=native -mtune=native` enabled by-default for every piece of software compiled unless explicitly specified otherwise?

Because your cpu has a base ISA, plus extensions. The compiler doesn't know if you're going to only run this binary on your machine, or distribute it to others that may share your base ISA but possibly not your extensions, so it takes the conservative approach and doesn't use them unless signaled to do so via those compiler flags. Also, I think -march implies -mtune.

In Zig the default target is native, which turns on all the applicable CPU features. If you want to target something other than the native machine you can use --target-os --target-arch parameters. I haven't exposed extra CPU options for cross compiling yet.

Re: Zig: software should be perfect [video]

#68
post #20

Re: performance claims, I wonder if the C sha256 implementation would have been more competitive with -march=native -mtune=native?

I think it would. I posted some observations on this the last time this video was discussed: https://news.ycombinator.com/item?id=17187140 My hypothesis is that Zig's compiler passes the equivalent of -march=native to the backend, which is why it should also be given to the C compiler to give a fair comparison (and a speedup of 30% or so).

I think you are probably right. I will do a follow-up post addressing the performance claims in this talk. I think I owe it to the community.

Re: Zig: software should be perfect [video]

#69

I've wanted a language like this. Java's checked exceptions with some way to offload the bookkeeping to the compiler. What about other run-time exceptions, like divide by zero? Are they checked? What about Hoare's billion-dollar mistake (null pointer exceptions)? Does Zig have non-nullable references?

[deleted]

Re: Zig: software should be perfect [video]

#70
Many very bright people in the major sects of ML and Scheme tried to achieve perfection, and they have concluded, many times, that perfection implies a mostly-functional strongly (and, perhaps, even statically typed but with optional annotations only, like it is in Haskell) language, possibly with uniform pattern-matching, annotated laziness, and high-order channels, and select and receive in the language itself.

Such a language could be visualized as strict-by-default Haskell (with type-classes, uniform pattern-matching, minimalist syntax - everything, except monads) plus ideas from Erlang, Go and Ocaml.

Perfection and imperativeness, it seems, does not match.

Post reply on HN