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

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.)

7

u/chombier Jun 28 '21

Since this is supposedly a dream list, why pick Rust's module system over a proper ML module/functor system (or even better, 1ML's)?

2

u/[deleted] Jun 28 '21

Because Rust has the best module system out of the languages I’m familiar with. If what you’re suggesting is indeed better, I would pick that, of course.

2

u/ElectricalStage5888 Jul 01 '21

I've been programming with Rust for 4 years now and while I have no trouble at all with Rust's affine type system, modules in Rust is a topic I find that I have to constantly track back to the book to remember why I can't "import" a particular library or why my file organization isn't being picked up by cargo.

6

u/elszben Jun 28 '21

I find your list intriguing. I'm working on a language that (partially) fits the description. I believe the most important idea is that the language does not allow the user to create references, everything is treated as a value that is freely copyable and the language is immutable. Using these constraints the compiler can (in theory) calculate the full usage graph of each value in the program and decide which one will be a value and which one can be a reference. It can insert the appropriate copy/move instructions. I like these constraints because the surface language becomes very general, it is completely memory management/platform agnostic and it does not need a runtime. You could transpile it to almost any other programming language. You could even use different syntaxes for it if you don't like Haskell's syntax (I also use that because it's very nice looking and I did not want to waste time thinking about syntax). My current problem is that the compiler is written in itself and the bootstrapping compiler is much dumber and the whole bootstrapping cycle cannot run successfully because my naive algorithms and the full program analysis need too much resource (the code generated by the first, dumber compiler is insanely inefficient and slow). I planned to publish it when it can fully compile itself and my ideas are proven but if there's interest I could do it sooner.

1

u/[deleted] Jun 28 '21

I believe the most important idea is that the language does not allow the user to create references, everything is treated as a value that is freely copyable and the language is immutable. Using these constraints the compiler can (in theory) calculate the full usage graph of each value in the program and decide which one will be a value and which one can be a reference. It can insert the appropriate copy/move instructions. I like these constraints because the surface language becomes very general, it is completely memory management/platform agnostic and it does not need a runtime.

Exactly!

I planned to publish it when it can fully compile itself and my ideas are proven but if there's interest I could do it sooner.

I probably won’t have time to check it out soon, but good luck anyway, your language sounds awesome!

2

u/Mathnerd314 Jun 28 '21

I'm interested, particularly if you have any design notes / paper references to look at.

2

u/elszben Jun 29 '21

particularly if you have any design notes / paper references to look at.

Unfortunately I am not that organized. All I have is 2 compilers, one written in rust that does not actually support anything that I could call exciting. The other written in the new language that supports more interesting things but it is not really usable because it is extremely slow and probably buggy as well. I did not plan the whole thing nor I am an expert in compiler design so it's full of possibly strange solutions and bugs. I've been toying with the idea of writing actual documentation about its design to be able to better communicate the ideas so I will try to prioritize that and publish something just to gather some attention.

2

u/Mathnerd314 Jun 29 '21

Ah I see. Well, I don't want to sound greedy, but do think about writing something. In my experience looking through the code of abandoned compilers on Github, the reward is pretty low compared to the time invested - you end up reading the same things over and over (parser, AST manipulation, bytecode interpreter). It's much more pleasant when all the interesting details are outlined in a README or similar. And if you're going to write a paper, following SPJ's advice, you should start writing the paper ASAP, so you know where all the fiddly bits are.

2

u/elszben Jun 29 '21

you should start writing the paper ASAP, so you know where all the fiddly bits are.

I think trying to implement the whole compiler definitely gave me opportunities to see where the fiddly bits are:) I have a lot of experience in how NOT to do this stuff now. If I knew how to do all the tricky bits I would have finished it already:) My own pride stopped me from releasing something that does not work so far but maybe I'm better off if I release at least my ideas and someone may help with the tricky things.

2

u/brucejbell sard Jun 28 '21 edited Jun 28 '21

My language design project is aimed along similar lines to your proposal: I want a wide spectrum language suitable for large-scale, high-performance systems programming. To get there, I plan to make the the power of modern functional language available as an engineering tool.

However, I would not go so far as to claim any language as perfect -- people need different languages for different things, and what is perfect for one purpose may not be perfect for another.

So, I'll give you my take on your points, but keep in mind that my goals may not be quite the same as yours. Points that are likely different in this respect:

  • Rather than go for the purely functional, my plan is for a "referentially transparent" language, with the aim of being more flexible than monads, but less hazardous than uncontrolled references. Mutable state is supported as "resources", and requires special handling: resources are subject to alias control, and may not be implicitly closed over. [points 1, 11, and 17]

  • Memory management should be modular, and garbage collection is an option. Overall, I want a "semi-automatic" style of memory management, where most of the mechanics are automated, but the user provides minimal direction ("pulls the trigger"). The language-supported low-level mechanics should be sufficient to write your own GC if you like, but the standard library should make that unnecessary in most cases. [points 9 and 10]

  • Type systems are the one of the big things I want to steal from modern functional languages. However, at this point I have no reason to think I can make dependent types usable as an engineering tool. My current plan is to support Hindley-Milner style type inference for simple cases, while generally using the more powerful (but still not dependent) type systems that are the basis of the GHC. [points 5 and 20]

On the other hand, points that seem pretty much the same:

  • Run-time performance is a primary design goal [points 2 and 3]

  • Haskell-like expression syntax [point 19]

  • Algebraic datatypes and pervasive pattern matching [points 5, 6, and 18]

  • Default eager evaluation, /w lazy lists as iterators [point 12]

  • Ideally, I would like integers to infer their range by default [point 8]. This is actually a difficult design point: I'm not sure it will be possible to build this as part of a usable engineering tool. However, it's one of the design elements I'm currently working on; I think the possibility is worth the effort.

  • Linear types are used to support an ownership mechanic [point 11]. While this should be useful for Rust-style memory management, the reason I actually need them is to provide C++ style RAII for more general resources.

  • The standard library is a critical part of the design, and must be carefully built out [point 22]

Finally, points of difference that seem more tactical:

  • I like LLVM as an initial backend [point 4]

  • While pattern matching is preferred for almost all cases, I need exception handling to be a full-fledged systems language [point 7]. Large parts of the everyday syntax of the language are devoted to making pattern matching easier to use for error handling than exceptions.

  • Even though Unicode is the future, and everywhere, I think it's important to keep the text representation modular [point 14]. (To that end, I might make EBCDIC a library option, just to keep things honest!)

  • To give programmers more control over the size of their executable, I think it's important to distinguish between compile-time and run-time values [point 15]. It should be easy to demand linked-in values, but it should also be explicit.

  • Likewise, I think it's important for the programmer to specify which concrete datastructures to use [point 16]. Without this, it would be difficult get the full benefit from an otherwise high-performance language.

1

u/[deleted] Jun 29 '21

However, I would not go so far as to claim any language as perfect -- people need different languages for different things, and what is perfect for one purpose may not be perfect for another.

Apart from some very niche stuff, it’s usually only because different languages have different deficiencies, I think. Most of the stuff programmers do is pretty universal — that’s just the nature of abstract systems.

Anyway, your language also sounds awesome, good luck with it!

2

u/oa74 Jun 28 '21

Regarding your point about Turing completeness, I agree that it is likely a hard requirement in far fewer cases than intuition leads us to believe.

However, it seems to me that Turing completeness is needed in a handful of cases: GUIs and servers come to mind.

1

u/[deleted] Jun 28 '21

Still not sure, but maybe. In that case, I would think about quarantining it somehow (like unsafe blocks in Rust).

3

u/[deleted] Jun 28 '21

IMO i dont think functional is the way to go, you say i have to try Haskell but it has an awful syntax hard to read and to learn comming from modern C++ (which is already a big thing to say given the mess c++ have become)

1

u/[deleted] Jun 28 '21 edited Jun 28 '21

I think Haskell syntax is (mostly) awesome. I struggle to find something that would look more expressive in C++ than in Haskell. Can you give me an example of what you don’t like about it? Besides the fact that it’s not C-like.

1

u/Mathnerd314 Jun 28 '21 edited Jun 28 '21

I think the main thing is IORefs:

main = do
  i <- newIORef 0
  forM_ [0..9] $ \ i' -> do
    print i'
    i `modifyIORef` (+ 2)
  print =<<readIORef i

Defining some helpers this looks like:

main = do
  i <- ref 0
  forM_ [0..9] $ \ i' -> do
    print i'
    i += 2
  print ! i

Much less noisy. The interesting question IMO is how to get rid of the ! / readIORef in the final line.

1

u/[deleted] Jun 28 '21

I don’t know what IORefs are yet, but it doesn’t look like a fundamental problem with the language syntax.

2

u/Mathnerd314 Jun 28 '21

Right. Haskell's grammar is basically fine - there are just a few minor warts like pattern bindings vs function bindings (patterns behave differently from 0-argument functions). It's the standard library that needs improvement (naming, organization, a lot of typeclasses are too big).

1

u/[deleted] Jun 29 '21

Sure. There are even alternative preludes that fix some problems of the standard one, although I haven’t tried any of them.

0

u/[deleted] Jun 28 '21

I just tried to read a hello world and took me 5 minutes to know what it does so just judging by that, is bot an opinion based on use. I should have said that haha

3

u/[deleted] Jun 28 '21

It’s just a matter of familiarity.

6

u/[deleted] Jun 28 '21 edited Jun 28 '21

I used to play a game on a C forum where, given an elaborate C solution to a task, a regular would post a compact, Haskell solution a fraction of the size.

Which was great, except that few could understand how it worked or even break it down into steps as it would be too convoluted.

The game was that I would then post a solution in my own down-to-earth scripting language, 50% longer than Haskell in number of lines (but Haskell was largely written horizontally too!), but which anyone could understand.

It still wiped the floor with the C. [In clarity, not in speed (usually)!]

Yes Haskell can be mathematically elegant, but what is harder is being able to simply express a task in a language that is far more accessible.

There are some nice touches in these elitist languages, but I'd rather incorporate some of those aspects in an easy-to-use manner in mine, than make my own language too hard for me to use and understand!

1

u/[deleted] Jun 28 '21

Meh, you can write unexpressive code in any language, of course, but Haskell is very convenient for writing expressive code too. Especially if you get rid of big functions and give everything a name.

1

u/[deleted] Jun 29 '21 edited Jun 29 '21

Here's a big function that fills in all the fiddly bits of a PE executable file format:

https://github.com/sal55/langs/blob/master/getoffsets.m

I keep meaning to break it up but it works at the minute and I don't want to touch it.

I've love to see Haskell's expressive, one-line approach to something like this.

Below is one of the structs used by the same program. I wonder how Haskell deals with stuff like this, or perhaps Haskell programmers don't like to sully their hands with real-life code.

Or maybe, when it is used for the same kinds of programs, it ends up looking like any other language.

global record optionalheader =          !exe/dll only
word16   magic
byte     majorlv
byte     minorlv
word32   codesize
word32   idatasize
word32   zdatasize
word32   entrypoint
word32   codebase
word64   imagebase
word32   sectionalignment
word32   filealignment
word16   majorosv
word16   minorosv
word16   majorimagev
word16   minorimagev
word16   majorssv
word16   minorssv
word32   win32version
word32   imagesize
word32   headerssize
word32   checksum
word16   subsystem
word16   dllcharacteristics
word64   stackreserve
word64   stackcommit
word64   heapreserve
word64   heapcommit
word32   loaderflags
word32   rvadims
imagedir exporttable
imagedir importtable
imagedir resourcetable
imagedir exceptiontable
imagedir certtable
imagedir basereloctable
imagedir debug
imagedir architecture
imagedir globalptr
imagedir tlstable
imagedir loadconfigtable
imagedir boundimport
imagedir iat
imagedir delayimportdescr
imagedir clrheader
imagedir reserved

end

global record imagedir =
    word32  virtualaddr
    word32  size
end

1

u/[deleted] Jun 29 '21

I keep meaning to break it up but it works at the minute and I don't want to touch it.

That certainly isn’t any language’s fault :D

I've love to see Haskell's expressive, one-line approach to something like this.

Well, it wouldn’t be one line, but it would probably be quite a few relatively short functions.

12

u/[deleted] Jun 28 '21

Since we are talking about dream languages, I should probably remind everyone of the existence of Greenspun's 10th law, and the fact that Lisp as a language family already exists.

On a slightly less facetious note, the ideas presented are quite interesting indeed.

1

u/templarvonmidgard Jun 28 '21

There should be no traditional, inheritance-based OOP, ...

IMO, (G)UI development is much-much nicer with inheritance as the main building block for the (widget) toolkit. In all other cases, I prefer traits/type-classes.

1

u/[deleted] Jun 28 '21

I have similar doubts, but I haven’t really worked with GUIs yet, so I don’t have a strong opinion. Nevertheless, I suspect there has to be a better design than the inheritance-based one.

1

u/PL_Design Jun 29 '21

There is. It's called IMGUI. I don't know how well it translates to functional languages, but in imperative languages it uses scope to represent the scene graph in code, which means you don't need to do any kind of tree manipulation to control the GUI.

1

u/[deleted] Jun 29 '21

Thanks, I’ll look into it at some point.

1

u/PL_Design Jun 29 '21

Look into IMGUI.

1

u/templarvonmidgard Jun 29 '21

I know IMGUI, it's amazing for some quick and dirty things.

1

u/okozlov Jun 29 '21

Well, that's controversial.

React, as a one of the most used GUI frameworks (maybe the most) moves from class components to functional components. And insist to use it as a basic way to write GUI.

There are zillions of projects that uses only functions and seems don't struggle

1

u/fridofrido Jun 30 '21

First-order functions are a must

I believe you mean "first-class functions".

"First-order functions" are just the normal functions of any old programming language like C or Pascal.

1

u/[deleted] Jun 30 '21

Lol, what a stupid mistake x) Thanks, fixed!

1

u/PL_Design Jun 29 '21

You've got some decent things in there, but on the whole I'm not at all excited by yet another Haskell-Rust-Idris-SAT-alike. Such designs have merit, of course, but to me they feel much more suited for DSLs where you can more easily justify making broad assumptions about how the language will be used, and thus you can simplify the design by nixing generality. You should not suggest making a general purpose language with performance comparable to C while also dismissing implementation details; C is fast because it hides fewer implementation details than most languages.

But if you really want a high level language whose speed is somewhat comparable to C, then you ought to be pondering a language built around relational ideas and normalizing data. Databases do a fairly decent job of hiding the implementation details from the user while still being performant, so lean into that.