October 30th, 2024

Sets, types and type checking

The blog post explains type theory and type-checking, highlighting their importance in programming languages for error detection, optimization, and understanding code, while introducing fundamental types and intersection types.

Read original articleLink Icon
Sets, types and type checking

The blog post discusses the fundamentals of type theory and type-checking, detailing their significance in programming languages. It begins by explaining the purpose of type-checking, which is to ensure that programs are sound by validating the types of expressions and preventing errors, such as multiplying a number by a string. The author emphasizes that types provide essential information for error detection, optimization, and aiding programmers in understanding and structuring code. Types categorize data based on properties, allowing for reasoning about data throughout a program's lifecycle. The post also distinguishes between types and sets, noting that types are constructed based on properties rather than predicates. It introduces fundamental types like "any" and "never," explaining their roles in type hierarchies. The author discusses binary conjugations of types, particularly intersection types, which combine properties from two types, and their practical applications in languages like TypeScript. The post serves as a foundational overview for readers interested in type theory, with the promise of more advanced topics in future posts.

- Type-checking ensures program soundness by validating expression types.

- Types provide crucial information for error detection, optimization, and code understanding.

- Types categorize data based on properties, facilitating reasoning about data.

- The post introduces fundamental types "any" and "never" and their significance.

- Intersection types combine properties from two types, useful in languages like TypeScript.

Link Icon 10 comments
By @haileys - about 1 month
> In Rust we have Option<T>, which is equivalent to T | null

No, not true!

As the author correctly states earlier in the post, unions are not an exclusive-or relation. Unions are often made between disjoint types, but not always.

This becomes important when T itself is nullable. Let's say T is `U | null`. `Option<Option<U>>` in Rust has one more inhabitant than `U | null | null` in TypeScript - `Some(None)`.

Union types can certainly be very useful, but they are tricky because they don't compose like sum types do. When writing `Option<T>` where T is a generic type, we can treat T as a totally opaque type - T could be anything and the code we write will still be correct. On the other hand, union types pierce this opacity. The meaning of `T | null` and the meaning of code you write with such a type does depend on what T is at runtime.

By @nikeee - about 1 month
An honorable mention is the string template literal type. It is between the string literal type (-unions); which allow a finite set of strings and the string type which, in theory, is an infinite set of strings. Template literal types can be infinite as well, but only represent a fraction. For example `foo${string}` represent all strings that start with "foo".

Similar to this, I proposed inequality types for TS. They allow constraining a number range. For example, it is possible to have the type "a number that is larger than 1". You can combine them using intersection types, forming intervals like "a number between 0 and 1". Because TS has type narrowing via control flow, these types automatically come forward if you do an "if (a<5)". The variable will have the type "(<5)" inside the if block.

You can find the proposal here [1]. Personally I think the effort for adding these isn't worth it. But maybe someone likes it or takes it further.

[1]: https://github.com/microsoft/TypeScript/issues/43505

By @o11c - about 1 month
`never` is better known as `bottom`. `noreturn` in some languages is the same thing

`any`, however, is not `top`, it is `break_the_type_system`. The top type in TS is `unknown`.

By @throwaway17_17 - about 1 month
I don’t think it is appropriate to say Rust has ‘union types’. Rust has sum types, implemented as Enums and (unsafe) Union types. There is a distinct difference between sum types and union types from a type theoretic perspective.
By @James_K - about 1 month
I think this post should have made a greater distinction between terms and types. The type 4 and the term 4 are separate entities with the same name. Otherwise string <=: string would imply that a function f : string => int could be called as f(string) on the type of strings (which is actually how they handle it Perl6/Raku). Overall it seemed very TS-centric and not much on type theory.
By @skybrian - about 1 month
> Like sets, types can be by description have an infinite number of distinct entries

I think they might have meant "entities" instead of "entries?"

The term "diagonal identity" seems to be non-standard as well?

By @disqard - about 1 month
Could someone here shed some light on "Why the intersection with any is always any"?

I didn't feel satisfied with the explanation in TFA.

By @Mathnerd314 - about 1 month
> Free variables and closures

Are these even types? I always mentally filed closures under "implementation detail of nested functions".

By @ingen0s - about 1 month
Finally, something useful to read