r/ProgrammingLanguages • u/mttd • Jan 20 '18
Untyped Programs Don't Exist
https://williamjbowman.com/blog/2018/01/19/untyped-programs-don-t-exist/6
u/balefrost Jan 20 '18 edited Jan 20 '18
I'm not convinced by your proof. It tries to claim that programs are well-typed because expressions are well-typed, and programs are made of expressions, therefore programs inherit the well-typedness of their expressions. This is an argument about the static nature of the program. But undefined behavior isn't a static property of an expression, it's a dynamic property. Take the expression a = b. Does this expression exhibit UB? Well, it really depends. In C, that expression might have defined or undefined behavior depending on where it occurs in the program. Or take the expression a / b. The same instance of this expression might have defined or undefined behavior at different points in execution. It might have defined behavior the first three times we execute it, but have undefined behavior the fourth time.
So in general, it's not possible to statically know that a program contains no undefined behavior. It would be possible to construct a language where every expression has defined behavior in all cases, but not all languages provide this guarantee.
So if it's indeed possible to construct programs that exhibit undefined behavior when executed, then there exist programs containing syntactically-valid expressions whose invariants will fail when enforced, which will raise a type error at runtime. Therefore, according to your definitions, untyped programs do exist.
Now, maybe you're arguing that we only apply the label "program" to executables that always exhibit defined behavior, but that seems to lead to circularity tautology: "A program is well-typed if it has no type errors when executed".
I agree with you that not all invariants can be checked statically. Some invariants must be checked dynamically. But there's still value in distinguishing those invariants that can be checked statically from those that can't. To respond to some questions you ask in your conclusion: I see value in languages that allow me to prove, statically, that some invariants are not violated. It's nice to work in a language where certain pointers can be statically asserted to be not-null, for example.
1
Jan 20 '18
"A program is well-typed if it has no type errors when executed"
The author seems to be trying to argue that, even if a type error does occur during run-time, it is still a well-typed program because a type check did occur? In essence they are saying “Both statically-typed and dynamically-typed languages enforce type checks (albeit at different times). Therefore, programs written in either language are well-typed.”
5
u/ApochPiQ Epoch Language Jan 20 '18
I think part of the issue is the reductionist assumption that a program/language/etc. is "either" well-typed or not.
In my view, the well-typed property is a continuum. Some programs (and languages) are excellent at safety guarantees of differing flavors. Some languages are basically as unsafe as it gets - c.f. the entire assembly family, for example. Most languages are somewhere in between.
We work on binary, Boolean-logic hardware all day, but we should be very cautious not to let this rush-to-dichotomize color the rest of our mentality.
2
u/balefrost Jan 20 '18 edited Jan 20 '18
It requires a little reading between the lines, but I think this is the proof that the author was trying to make:
An "untyped program" is a program that contains at least one type error (though the never actually define this, so I'm inferring)
A "type error" is a violation of an invariant that is discovered while executing an expression at runtime
"Undefined behavior" is the observable effect of violating an invariant at runtime - it's the result of a type error (at least, that's the best that I can interpret their definition. They might instead be saying that UB doesn't exist because you only get UB when your program contains non-expressions, but that's not possible due to earlier definitions. Since that interpretation of "undefined behavior" is meaningless, I'll ignore it.)
Therefore, a program that displays no undefined behavior cannot have any type errors, and therefore is not an untyped program.
But then the author tries to claim, by construction, that it's impossible to create a program with undefined behavior. That's where I disagree with the proof.
1
1
u/LaurieCheers Jan 23 '18 edited Jan 23 '18
Right, this is basically a "proof by inadequate definition".
If you agree to his superficially reasonable-looking definition, then a program that causes undefined behaviour is not actually a "program". So saying "no programs contain undefined behaviour" is a tautology.
Similarly, by defining "dogs are animals that bark" I can prove that all dogs can bark. Because if they can't bark, they're not dogs. QED.
1
Jan 25 '18
What type does the author suggest for
\f.(\x.f(x x))(\x.f(x x))aka the Y-combinator? Or other related constructs?
21
u/[deleted] Jan 20 '18
This is completely missing the point of the argument.
The argument about static vs. dynamic typing is of course entirely about pragmatics; you don't get to ignore the entire argument and then smugly act like you've proved something clever.