r/ProgrammingLanguages • • Jun 27 '21

Blog post A Dream of the Perfect Programming Language

telegra . ph/ A-Dream-of-the-Perfect-Programming-Language-06-26 (looks like Reddit’s global spam filters block Telegraph for some retarded reason)

Clickable but less good-looking version: https://github.com/natewind/articles/blob/master/2021-06-26-A-Dream-of-the-Perfect-Programming-Language.md

3 Upvotes

40 comments sorted by

View all comments

7

u/silicon-pizza Jun 28 '21

I'm skeptical of the goal: "a perfect programming language". There is no perfect language, there are only engineering tradeoffs, where making a language better for some use cases makes it worse for others.

One of these tradeoffs is between simplicity and ease of use, vs expressiveness, vs how effective the type system is at preventing bad things at run time by issuing compile time errors. You can't maximize all three. Pick two.

You want a perfect type system that guarantees all functions are total. There are no panics, because all potential problems are detected at compile time. You will pay a terrible cost for this. If you want the language to be as expressive as a general purpose language, and as efficient as Rust (you mention "zero cost abstractions"), then the type system will have a steep learning curve and be very difficult to use, worse than the Rust borrow checker.

For example, your language has efficient machine integers. How does the integer division operator work in your language? What happens when you divide by zero? That's a panic, so your type system has to prevent this at compile time. This probably means that when I write i / j, I need to prove to the type system that j is not zero. That's more complicated than division in a mainstream language. Alternatively, integer division could return Maybe Int, and I'll be required to perform a case analysis on the result and explicitly handle the error case. That simplifies the type system requirements for division at the cost of runtime overhead, and you said you want zero cost abstractions.

The Dhall programming language is total, and relatively simple. It achieves this at the cost of expressiveness. There is no division operator.

3

u/[deleted] Jun 29 '21

There is no perfect language, there are only engineering tradeoffs, where making a language better for some use cases makes it worse for others.

There are no fundamental reasons it should be this way, which is the whole idea behind my article.

One of these tradeoffs is between simplicity and ease of use, vs expressiveness, vs how effective the type system is at preventing bad things at run time by issuing compile time errors. You can't maximize all three. Pick two.

In a well-designed language, all three amplify each other. It happens in Rust and Haskell, for example.

then the type system will have a steep learning curve and be very difficult to use, worse than the Rust borrow checker.

First of all, let’s be clear that Rust‘s borrow checker is only hard compared to a garbage collector. In C/C++, you have to deal with lifetimes manually, and the compiler won’t save you. If it feels easier than Rust, you’re writing buggy C/C++.

Second, this logic applies to more than just memory management. If you want your program to behave correctly, you already have to worry about edge cases, illegal values etc. and make sure it’s all handled properly. The difference is, in a total language the compiler will make sure you haven’t missed anything. It’s only harder compared to writing bad code.

What happens when you divide by zero?

There’s no reason we can’t have both

(/) : Int -> NonZero -> Int

and

tryDiv : Int -> Int -> Maybe Int

Although, in this specific case, the responsibility should be on the caller. Returning Maybe makes sense when you have to do some work to find out an argument was invalid.

I need to prove to the type system that j is not zero. That's more complicated than division in a mainstream language.

You do realize you have to verify user input in any language, right? Just dividing by a user-entered number without checking it is shitty code no matter what. Always assume every part of the system you don’t control is stupid or malicious.

2

u/epicwisdom Jul 01 '21

Engineering trade-offs (particularly when it comes to software) are rules of thumb born from extremely limited practical experience. We aren't talking about the speed of light or the Heisenberg uncertainty principle here.

While I agree that accomplishing everything all at once is a bit of a fool's errand, that doesn't mean nobody should try. Sometimes good things come out of attempting what appears to be impossible, whether the original goal or simply new ideas.

1

u/[deleted] Jun 28 '21

The Dhall programming language is total, and relatively simple. It achieves this at the cost of expressiveness. There is no division operator.

A rather drastic measure. And futile, since the first thing someone will do is to write a divide function.

I'm skeptical of the goal: "a perfect programming language". There is no perfect language,

Maybe perfect for the OP; everyone can have their own idea of a perfect language.

(Doesn't quite work for me yet, as I still have two languages instead of one, neither of which is perfect either. The trade-off here is also between what I would like, and what I can implement and get working.)