Programming Languages – What is a Type System?

inheritancelanguage-designprogramming-languagestype-systemsvirtual machine

Background

I am designing a language, as a side project. I have a working assembler, static analyser, and virtual machine for it. Since I can already compile and run non-trivial programs using the infrastructure I've built I thought about giving a presentation at my university.

During my talk I mentioned that the VM provides a type system, was asked "What is your type system for?". After answering I got laughed at by the person asking the question.

Thus, even though I am almost certainly going to lose reputation for asking this question, I turn to Programmers.

My understanding

As I understand them, type systems are used to provide additional layer of information about entities in a program, so that the runtime, or the compiler, or any other piece of machinery, knows what to do with the strings of bits it operates on.
They also help maintain contracts – the compiler (or code analyser, or runtime, or any other program) can verify that at any given point the program operates on values programmers expect it to operate on.

Types can also be used to provide information to those human programmers.
For example, I find this declaration:

function sqrt(double n) -> double;

more useful than this one

sqrt(n)

The former gives plenty of information: that the sqrt identifier is a function, takes a single double as input, and produces another double as output.
The latter tells you that it is probably a function taking a single parameter.

My answer

So, after being asked "What is your type system for?" I answered as follows:

The type system is dynamic (types are assigned to values, not to variables holding them), but strong without surprising coercion rules (you can't add string to integer as they represent incompatible types, but you can add integer to floating point number).

The type system is used by the VM to ensure that operands for instructions are valid; and can be used by programmers to ensure that parameters passed to their functions are valid (i.e. of correct type).
The type system supports subtyping and multiple inheritance (both features are available to programmers), and types are considered when dynamic dispatch of methods on objects is used – VM uses types to check by what function is a given message implemented for given type.

The follow-up question was "And how is type assigned to a value?". So I explained that all values are boxed, and have a pointer pointing to a type definition structure which provides information about name of the type, what messages it responds to, and what types it inherits from.

After that, I got laughed at, and my answer was dismissed with the comment "That is not a real typesystem.".

So – if what I described does not qualify as a "real typesystem", what would? Was that person right that what I provide cannot be considered a typesystem?

Best Answer

That all seems like a fine description of what type systems provide. And your implementation sounds like a reasonable enough one for what it's doing.

For some languages, you won't need the runtime information since your language doesn't do runtime dispatch (or you do single dispatch via vtables or another mechanism, so don't need the type information). For some languages, just having a symbol/placeholder is sufficient since you only care about type equality, not its name or inheritance.

Depending on your environment, the person may have wanted more formalism in your type system. They want to know what you can prove with it, not what programmers can do with it. This is pretty common in academia unfortunately. Though academics do such things because it's pretty easy to have flaws in your type system that allow things to escape correctness. It's possible they spotted one of these.

If you had further questions, Types and Programming Languages is the canonical book on the subject and can help you to learn some of the rigor needed by academics, as well as some of the terminology to help describe things.