News for hackers. Stuff that matters.
Re:Are unsound type systems wrong? by Tarean on Saturday April 07, 2018 @07:04AM (#16780250)
There are basically two important results for type systems, soundndess and completeness.

Soundness means that every program that typechecks is valid. Completeness means that every valid program can be typed.

So basically typescripts type system doesn't match the language semantics so type checked programs may crash at runtime.

Re:Are unsound type systems wrong? by tel on Saturday April 07, 2018 @11:43AM (#16781217)
While this is true, it's critical to note that "valid" here doesn't mean "valid as in it follows the intention of the programmer who wrote it" but instead "valid as in it either terminates in a well-defined error condition or completes to an identically-typed result" (preservation and progress).

The engineering constraint is to ensure that the design and logic of the types themselves enable preservation-and-progress-validity to enforce meaningful semantic constraints. A sound and complete type system can still say things that aren't practically meaningful.

Re:Are unsound type systems wrong? by davidgay on Saturday April 07, 2018 @10:12AM (#16780773)
Note that most common type systems "crash" type-checked programs at runtime: null-pointer check failures, array-bound check failures, and (even, Java) co-variant array write check failures. So the line is fairly fuzzy.
Re:Are unsound type systems wrong? by _asummers on Saturday April 07, 2018 @10:22AM (#16780810)
Or just silently fail! Knowing the history of the Collections API and generics in Java, I know WHY this is like this, but Map.get taking an Object has resulted in way more bugs in previous code bases than I'd like to think about. If you have a Map<S, T> Java will silently take an object that has NOTHING to do with S and return null because that key is obviously not in the map. You can of course wrap that call behind functions that DO check the proper input type, but until you've been burnt by it you might not think to do that.
Re:Are unsound type systems wrong? by brabel on Saturday April 07, 2018 @12:42PM (#16781482)
> If you have a Map<S, T> Java will silently take an object that has NOTHING to do with S and return null because that key is obviously not in the map

        Map map = new HashMap();
        map.put("one", 1);
        
        class Whatever {}

        Map<Whatever, Integer> whateverToIntMap = (Map<Whatever, Integer>) map;

        Integer i = whateverToIntMap.get("one");
        System.out.println("i = " + i);
Prints:

    i = 1
That's a consequence of generics type-erasure for backwards compatibility with non-generic code, so there _is_ a reason for `get` accepting any Object.
Re:Are unsound type systems wrong? by _asummers on Saturday April 07, 2018 @05:44PM (#16783173)
Like I said, I know the history and why it exists. I learned Java before 1.5 came out. That API existed long before generics did (I think 1.0 or earlier). If they had designed the language with generics in mind, they would have probably done it differently; hindsight is 20/20. Doesn't make it not a suboptimal API. I don't think there's a good way to fix it this late in the game without breaking old code, but such are the warts of backwards compatibility.
Re:Are unsound type systems wrong? by s_kilk on Saturday April 07, 2018 @07:53AM (#16780356)
I'm curious, given Godels incompleteness theory, isn't it impossible to have both soundness and completeness in the same language?
Re:Are unsound type systems wrong? by karatinversion on Saturday April 07, 2018 @08:06AM (#16780377)
Gödel's incompleteness is for proof systems for languages which contain enough arithmetic to express its own syntax. Lots of things don't go this far - first order predicate logic is sound and complete, for example (see Gödel's completeness theorem(!)).
Re:Are unsound type systems wrong? by pas on Saturday April 07, 2018 @08:56AM (#16780518)
Could you please expand on the soundness and completeness of first order logic? Isn't that false generally? Because you need a deductive/axiom system, so that you can ask the question of completeness/soundness. And ZFC is too strong, hence cannot prove its soundness and correctness at the same time. But you can formalize weaker systems in first order logic, that are provably complete and sound.
Re:Are unsound type systems wrong? by gpm on Saturday April 07, 2018 @11:28AM (#16781147)
(It's been a year and a half since I took a course on this, so I apologize for any mistakes)

You do need to define a deductive/axiom system before you can ask the question. We can use a few different standard deductive systems. We require that the axioms are sound. Then the system is complete. (I think one should also always be able to prove false if the axioms aren't sound... but I don't recall proving that)

ZFC isn't too strong. That sounds like a contradiction, but it isn't, because we don't have a rich enough vocabularly in first order logic to state the problematic statements that make it either inconsistent or incomplete in stronger logic systems. Every true statement you can state about ZFC in first order logic is provable.

Re:Are unsound type systems wrong? by enugu on Saturday April 07, 2018 @01:41PM (#16781738)
We can state a lot of things in first order logic about natural numbers or set theory which are not provable (in some given set of axioms like Peano axioms or ZFC, you can extend the axiom set but then one can generate a new unprovable statement if not a contradiction).

The issue here is first order logic is complete for statements that are true for all models of the axioms. As an example, imagine a infinite land which cant be completely described by any computable map(a programmable set of facts about the territory). The map will only tell you some true things about the territory.

But we can say this - if there is some statement that the map cant decide, then there are two different territories for both of which the map is accurate, and the statement is true for one territory and false for another. So the deductive system is complete description of true statements which hold for all territories for which the map applies.

But if we are interested in a single given territory, no computable system of facts suffices. For example deciding whether a diophantine equation has solutions in the standard set of Natural Numbers or a more familiar example for this site, whether a program halts. No computable deductive system(ie there is a program which generates all deductions) will suffice.

Re:Are unsound type systems wrong? by indexerror on Saturday April 07, 2018 @12:48PM (#16781509)
> It's been a year and a half since I took a course on this

Can you please recommend a MOOC/resource for learning more about this?

Re:Are unsound type systems wrong? by gpm on Saturday April 07, 2018 @02:07PM (#16781880)
My only real exposure to it is the course I took, csc438 at UofT taught by Steven Cook.

There are pretty complete notes for the course as well as assignments here [0], but not videos or planned lessons. You certainly could learn about it by reading them, but I don't know if it would be the most efficient way.

[0] http://www.cs.toronto.edu/~sacook/csc438h/

Re:Are unsound type systems wrong? by varjag on Saturday April 07, 2018 @09:13AM (#16780564)
Propositional calculus is sound and complete, first order predicate logic isn't.
Re:Are unsound type systems wrong? by jonnycomputer on Saturday April 07, 2018 @10:08AM (#16780758)
fol logic is sound and complete (and effective). second order logic is not.
Re:Are unsound type systems wrong? by mcguire on Saturday April 07, 2018 @01:55PM (#16781819)
Re:Are unsound type systems wrong? by snnw on Saturday April 07, 2018 @08:13AM (#16780394)
Godel's theorem only applies to proof systems that can encode basic arithmetic, which most type systems cannot.
Re:Are unsound type systems wrong? by vanderZwan on Saturday April 07, 2018 @10:01PM (#16784169)
Would a type system that can verify whether array access is always in-bound implicitly encode arithmetic somehow? My gut feeling says yes, since I don't know how one would prove bounds without arithmetic, but I don't have the mathematical knowledge to back this up.
Re:Are unsound type systems wrong? by lmm on Sunday April 08, 2018 @03:03AM (#16785006)
It would need a certain amount of arithmetic, but Presburger arithmetic (addition and constant multiplication, essentially) is complete and consistent and might be enough for all the array use we care about?
Re:Are unsound type systems wrong? by baq on Saturday April 07, 2018 @10:29AM (#16780841)
you can design a complete and sound type system in which any program can be checked in polynomial time.

https://en.wikipedia.org/wiki/Hindley%E2%80%93Milner_type_sy...

Re:Are unsound type systems wrong? by vilhelm_s on Saturday April 07, 2018 @03:09PM (#16782236)
Yes, although I would appeal to Rice's theorem rather than Gödel.
Re:Are unsound type systems wrong? by jackfoxy on Saturday April 07, 2018 @01:02PM (#16781579)
> Soundness means that every program that typechecks is valid. Completeness means that every valid program can be typed.

I like this definition. Can someone link to an authoritative article that elaborates on this statement?

Re:Are unsound type systems wrong? by eslaught on Saturday April 07, 2018 @01:08PM (#16781604)
This is a pretty standard definition (though sometimes the words get swapped). E.g. you see a very similar sentence in Wikipedia:

> Informally, a soundness theorem for a deductive system expresses that all provable sentences are true. Completeness states that all true sentences are provable.

https://en.wikipedia.org/wiki/Soundness#Relation_to_complete...

You could probably follow the references there to more authoritative sources.

Re:Are unsound type systems wrong? by Izkata on Sunday April 08, 2018 @12:17AM (#16784583)
Ehh, no. The definition on Wikipedia - at least the part you quoted, the rest is too jargony for me - is circular (and makes me think it's incorrect/missing something). The GP's sentence would require a third link, which missing means it isn't circular.

Programs that can be typed will not necessarily be typecheck-able successfully.

(I hope what I'm trying to get at makes sense and I'm not causing more confusion... what I have in mind is things like Java's ClassCastException - everything's typed, the typecheck passes, but it wasn't actually successful. Going by the Wikipedia quote, it would mean it wasn't actually possible for this exception to be thrown.)

Re:Are unsound type systems wrong? by systoll on Sunday April 08, 2018 @08:27AM (#16785827)
The two statements are equivalent, and not circular.

The type-checker 'proves' a statement of the form 'this program will never give values of one type to a statement expecting an incompatible type'.

The fact that a Java program can pass typechecking, and then throw a type error at run time, is proof that Java's type system is unsound.

https://dev.to/rosstate/java-is-unsound-the-industry-perspec...

Re:Are unsound type systems wrong? by Izkata on Sunday April 08, 2018 @12:03PM (#16786707)
In the Wikipedia definition, the first phrase uses "all provable sentences" and "are true"; the second phrase uses "all true sentences" and "are provable". "true" and "provable" are equivalent in the two phrase here.

In the other one, the first phrase uses "program that typechecks" and "is valid"; the second phrase uses "valid program" and "can be typed". "is valid" and "valid program" are equivalent, but "program that typechecks" and "can be typed" are not.

So the two completeness/soundness definitions taken as a whole are not equivalent. That's what I was trying to get at with my example - it's not that the exception throws, but that the exception even exists.

Re:Are unsound type systems wrong? by eslaught on Sunday April 08, 2018 @03:10PM (#16787714)
No, those are not equivalent at all.

"Valid" means, intuitively, that if I skip the compile-time type checks, and run the program with the types checked at runtime. If the type system is sound, and the compile-time type checks all pass, then it is impossible to hit a runtime type error. More formally, a program is valid if the execution within the operational semantics (basically, the semantics of running the program) do not lead to an error. This is distinct from type checking correctly.

Similarly, in formal logic "true" and "provable" are not equivalent. We'd like them to be, for sure, but the various incompleteness theorems ensure that we'll never be able to prove every possible true statement.

Re:Are unsound type systems wrong? by Izkata on Sunday April 08, 2018 @03:51PM (#16787964)
> Similarly, in formal logic "true" and "provable" are not equivalent.

This tells me you've totally misunderstood my comment. More simply, "true" and "true" are equivalent, and "provable" and "provable" are equivalent. But while "valid" and "valid" are equivalent in the other definition, "typed" and "typechecked" are not.

Re:Are unsound type systems wrong? by systoll on Monday April 09, 2018 @07:08AM (#16791978)
The distinction you're drawing is imprecision in the type-checking statement.

> "program that typechecks" and "can be typed" are not [equivelent]

Sure. The above statement be more rigorous if it said 'has consistent types assigned to it by the type checker' and 'will be assigned consistent types when run through the type checker'.

[And, yes, every runtime ClassCastError demonstrates Java's type-checker's unsoundness, and a 'Sound Java' would have no need for the concept]

Re:Are unsound type systems wrong? by a-saleh on Sunday April 08, 2018 @06:07AM (#16785448)
I think this might be, because the definition seems to stem from mathematical notion of logic. And logicians usually want to have their logics sound, because if you can prove false statements, what is the point of your logic, right?

So to distinguish between sound/unsound typesystem, I would look at it as if it were logic, where types are my theorems, and typechecking is attempt to find a proof.

Of course, type-system for a language has to have some compromises, Java explicitly tells you, that even typechcecked program still will have runtime exceptions (like ClassCastException). In haskell you can still fall into an infinite loop, even though all the types flow through the system as expected :-)

Re:Are unsound type systems wrong? by jackfoxy on Saturday April 07, 2018 @04:12PM (#16782634)
I was looking for something specific to type systems, but this is a good start.