r/ProgrammingLanguages • u/Trave11er3427 • 4d ago
optimistic or pessimistic compiler?
for those that compose and craft compilers, do you find the results better and more satisfying for you if the compiler you work on is permissive/optimistic or strict/pessimistic?
permissive/optimistic here means that the starting point is the compiler assumes the line of code that it is parsing/evaluating is probably ok but needs to pass some checks before translating to the target backend? (this is “the compiler is your friend” approach)
strict/pessimistic here means that the starting point is the compiler assumes the line of code that it is parsing/evaluating has something wrong and its job is to discover that wrong thing, and it is disappointed when the line passes all checks before translating to the target backend? (this is “the compiler is the teacher we’ve all had at one point in our life where they assume you’re going to do something bad as soon as they’re not looking” - and they’re not wrong)
obviously there has to be some balance here, but i’m curious, what has been your experience with these two approaches?
3
u/Inconstant_Moo 🧿 Pipefish 3d ago edited 3d ago
How would you even write a pessimistic compiler? How would it even work except by reversing the results of an optimistic one?
By the time we've gotten through the lexer, I have pieces which are in themselves correct. `5` is valid code in itself, and so is `true`, and `"spaghetti"`. And we want the parser to be recursive, so it's going to be taking a bunch of things that are valid, like that, to produce larger structures which are valid (as far as the parser goes) by construction.
Well then we pass the AST on to the compiler, and all the leaf nodes are valid by construction, and any code that just emits them, and again, we want to work recursively, and we're starting with a bunch of things that are known to be correct.
Insofar as the optimism/pessimism distinction makes any sense, I think on this basis everything after the lexer has to be intrinsically optimistic because so many things are intrinsically valid. `"spaghetti"` makes sense to the lexer, it makes sense to the parser, it makes sense to the compiler, and it evaluates to `"spaghetti"`.
---
If you could get this to even mean anything, the problem for the user is that the only possible error message you could give at compile time would be "nothing went right". You could on the other hand give detailed reports on why compilation succeeded, if it did, but no-one wants that.
22
u/brucejbell sard 4d ago
Languages typically don't impose constraints on a whim. There's always something those constraints get you, and if you don't enforce the constraint, you can't get the thing.
You have set up your question as a strict/permissive dichotomy, but the way you phrase it makes me think the problem might be that you have been bruised by unfriendly error messages. Beware of anthropomorphising the compiler, there's nothing personal there because it's not a person.
Good compiler messages (i.e., consistently useful and reasonably friendly ones) are a major blessing, but they are hard to do well. Some implementations work harder at them than others. If you hate unfriendly error messages, you could work hard to produce good ones for your own project.
In my experience, crappy error messages are better than none, and when you're implementing a language it's easy to short them because you have so much else to do.
44
u/asdfa2342543 4d ago
Yes, how about instead of type coercion we do some type encouragement?
16
u/MadocComadrin 4d ago
I'm not 100% sure what the functional difference is unless you've got a use case similar to web pages where rendering something is better than failing (e.g. super permissive). Other than that, the compiler should ideally reject all text that isn't a valid program and accept all text that is, unless you're running into decidability/time/space issues in which case I'd rather sacrifice the latter than the former in many cases.
9
u/proudHaskeller 4d ago
What does that even mean? Is the "optimistic" compiler going to skip some of the checks because "eh it's probably fine"? Is the "strict" compiler going to tell you "I told you so" on every warning?
If you really must personify the compiler, then do it correctly. The compiler doesn't want your program to compile, and it doesn't want it to fail. It wants to correctly determine whether your code compiles, according to the language's spec and/or design. If it doesn't compile then it wants to tell you why informatively. If it does compile then it wants to compile it correctly. But it has no care in the world whether the code that you give it will compile or not.
3
u/Athas Futhark 3d ago
I don't think this binary distinction is really useful, but there is one thing related to this that I believe in:
In an optimising compiler, unless you have a concrete reason to make a change to the program, preserve as much as possible what the programmer wrote. For example, if two statements can semantically appear in whatever order in the output code, put them in the order specified by the programmer, unless an analysis concretely suggests one order is better than another. Perhaps the programmer knows something the compiler doesn't. Even if the compiler actually screwed up the ordering, and flipping them would be faster, then by maintaining the order in the input program, you at least provide a lever for the programmer to fix their mistake, without the compiler undoing it.
3
u/AdreKiseque 3d ago
I get the feeling you might not know enough about compilers to really understand what you're asking, but I don't know enough about compilers to say so.
2
u/pthierry 4d ago
What would your optimistic compiler would produce that the pessimistic compiler wouldn't?
2
u/ralphpotato 3d ago
You are kinda simplifying the process of executing source code into one compilation step, when in reality for any language there are multiple steps, and for any widely used language they are very explicit.
Parsing the source code is literally just the first step of many, though there are a few things I think are more aligned with your idea of optimistic compilation:
- javascript’s automatic semicolon insertion (ASI)
- treating separate sections of code as independent and being able to interpret/compile even if not all of them are valid
- treating these things as undefined behavior and shrugging it off until runtime
I’m not really sure if anyone nowadays thinks ASI is good and linters can enforce proper semicolon usage.
The sections of code things can come in the form of an interpreter like a shell giving an error for one block and executing the next. Or a compiler can still compile different translation units while not being able to complete the overall job (like linking) because one necessary part failed.
The last thing about undefined behavior, yeah there’s stuff that parses just fine but will crash the program or make decisions that you don’t expect if you compile C without warnings/errors. Again I’m not really sure this is that desirable behavior from a compiler, and people have been fighting C’s undefined behaviors for the past 50+ years even with stricter warnings and errors.
2
u/taysongoodmansen 2d ago
with the language I'm making right now, I originally started with a very optimistic view, but then as I've gone on I've shifted a lot towards pessimistic. I had to shift a lot because my language is built to be shared a lot between users, and so I needed to add built in safety and compatibility checks.
2
u/X4RC05 4d ago
I think it should be whichever increases developer velocity when it comes to shipping correct programs.
1
u/keyboard_toucher 3d ago
It's not possible for the starting point to be the belief that something is wrong. At the beginning of processing, nothing is known about the program. Therefore, no problems are known.
1
u/Kiore-NZ 3d ago
In the bad old days we had PL/I where the following is a legal statement
and other languages supporting other weird syntax. Compilers for them were hard to write.
If you are working on a modern language (excluding features I hate such as JavaScript's implied semicolons), the syntax is relatively easy to parse, usually a recursive descent parser maybe with a little look-ahead or a YACC (el al) generated one will do it for you, then semantics will need checking (e.g. you shouldn't directly assign "3.1459" to a float which is why we have atof type functions and the number and types of arguments should match) once you've got past those checks generating the code is pretty much grunt work.
I'd lean heavily into assuming the code is legal unless the parser or the semantic checks fail.
1
u/dinanomartin 3d ago
strict with helpful messages beats permissive with silent failures every time. the tone is the actual problem.
1
u/AdreKiseque 3d ago
Assuming you're asking about the technical implementation of the compiler to the same ends, I don't see how you'd make anything but an "optimistic" compiler. At the start the code already has to be "right", or else nothing can compile. The compiler then goes along parsing the code and if it runs into an issue it has to report an error. I don't see how you'd do it otherwise.
1
u/kwan_e tonal-lang 3d ago
Well, there are some checks that can be delayed, or ultimately made as a completely separate phase.
Take for example unused variable checks. I used to be of the opinion that that should be a compile error. But that gets in the way of when I want to write some small experiment, perhaps in an existing code base, and if it doesn't work, then I'll discard it.
Nothing more annoying than having the compile stop because of that.
Build systems and version control exists and are cheap, and used by all non-trivial projects where stuff like unused variables matter in distribution. Checks like unused variables should be made an error in a CI build. For local builds, having stuff like that run as a parallel part of the local build, and allows you to run the thing while the those check results come in.
1
u/GiveMe30Dollars 1d ago
I rather have a malformed or ill-defined program fail at compile-time than do something unexpected and unwelcome during runtime.
For the same reason, dynamically typed languages drive me insane, even if I do know that they have their usecases. It simply does not agree with the way I write code.
1
u/Rayy_23_ 7h ago
I think it helps to look step by step. One of the fists things the compiler does is parsing. The parser doesen't assume anything, it just checks whether the token stream belongs to the grammar of the language. Either the code is syntactically valid or it isn't.
Where the "optimistic vs pessimistic" actually shows up is in whats happens when the parser finds a error. A simple parser stops at the fist syntax error and reports it. A more "pessimistic" parser expects error and has some recover strategies, like skiping the token until finds a ; or }, or inserting a missing token.
For the programmer this is better, because there are fewer "compile, fix one error, recompile". For the compiler, it makes the parser more complex e harder to maintain and optimize, and it can cause cascading erros.
So, for me, being "pessimistic" is how you handle erros, and I like the ideia of making our lives easier haha (at least for languages that aren't focused on optimization, of course).
-1
u/Embarrassed-Crow9283 4d ago
Optimistic compilers, which is why I love dynamic typing even though others might not like it. I find that to get conservative static typing to a usable state with my code pattern, you would probably need a very aggressive compiler that might run for hours.
I want my code innocent until proven guilty. If you want your code untrusted until verified safe, that is your call, but I don't have a good experience with such compilers. It was mostly me fighting the compiler until it generates the code I want.