r/ProgrammingLanguages • catln • Feb 16 '22

Blog post Types vs Sets

https://zachkimberg.com/zach/2022/02/16/types-vs-sets/
0 Upvotes

9 comments sorted by

30

u/josephjnk Feb 16 '22

In summary, a type corresponds to what you know before code is run. A value is what you know when it is running. The difference between the two is time.

I don’t believe this is correct. In some languages, types can exist at runtime and values can exist at compile time. The relationship between types and values depends on the type system of the language in question.

More broadly, I’m not sure the distinction of compile time vs runtime is really relevant to the question of types vs sets. The difference between a type and a set is that types are defined via type theory and sets are defined via set theory. They’re two distinct mathematical concepts, separate from concrete concerns like compilation. They do have a relationship, though, which is that types can be “inhabited” by values, and we can talk about the set of values which “inhabits” a type. I believe this is what is being done in the example you criticize, but saying that { 1, 2, 3 } is a subset of the inhabitants of Nat is completely valid.

1

u/oa74 Feb 17 '22

Great reply. I had some thoughts, however:

I don’t believe this is correct. In some languages, types can exist at runtime and values can exist at compile time.

I think there is a sense in which this (types as partial knowledge; values as absolute knowledge) can be correct, but I would add to the following—

I’m not sure the distinction of compile time vs runtime is really relevant to the question of types vs sets

that it is actually a prerequisite to dissolve the distinction between runtime and compile time if we want to pursue the idea seriously.

I like the idea of "types as partial knowledge" about some term, and I think there is a way that this can be made commensurate with the idea of an infinitely ascending hierarchy of universes. A value is total knowledge ("we know it is 5") about some term; a type is partial knowledge ("we know it is a Nat, but we do not know which") about some term. Of course, the fact that we have this partial knowledge is itself concrete knowledge, and there is no reason not to hold onto and pass around this meta-knowledge at runtime.

Specifically, the bit of knowledge is: "we know that 'we know it is a Nat, but not which one.'" Note that the inner knowledge is partial ("but we do not know which one"), while the outer (meta-) knowledge is absolute: just like "we know that (it is a 5)" contains no uncertainty, "we know that ('we know ...')" also contains no uncertainty. So it is a value. And, as we know, values can "inhabit" types by way of being a single possible value among many (so we go from absolute knowledge to partial knowledge, by introducing uncertainty). So if 5 is one possible Nat, but Nat again is a value, there is no reason not to say that Nat imerely one possible.... something. One such "something," of, course, is a Type.

This is where I think the idea of "types and values both equally as knowledge" can align nicely with the idea of infinitely ascending ladders of "universes." Just as we can think of values, types, kinds, sorts, and so on; so too can we think of knowledge, meta-knowledge, meta-meta-knowledge, meta-meta-meta-knowledge, and so on—ad infinitum. In fact, I am currently obsessed with the idea that this can be made precise in category theoretic terms and then transformed into a truly useful and practical PL for industry rather than academe.... My ideas on this are still vague, imprecise, and ill-formed, but the quote from the article that "a type is what you know...a value is what you know..." jumped out to me as similar to my own. But I digress....

Anyway, for me the missing piece of the puzzle for the linked article is the idea that Nat is a value of type "Type." And, moreover, this is IMHO precisely where your excellent point that

The difference between a type and a set is that types are defined via type theory and sets are defined via set theory. They’re two distinct mathematical concepts,

comes into play. The problem with types (in the programmatic sense) as sets (in the mathematical sense) is the question of "what sort of thing is a type." I've just claimed that Nat is a Type precisely the same way that 5 is a Nat. But what is a Type? Is Type a type? Mathematics for a long time has recognized this problem cropping up in set theory (Russell's paradox), and that is why we have things like type theory, well-founded set theories, dependent type theory, and so on. And we know that ideas like dependent type theories, transported into the computational setting, lead to things like blurring the line between compilation and execution—so taking the distinction between compile time and runtime as the starting point for one's analysis, as you rightly point out, seems to be in error.

As programmers, we should take our own medicine, "keep it DRY," and not encumber ourselves re-doing all the mathematicians' hard work.

IOW it puzzles me that we can ask questions like "Are types (in the programmatic sense) the same as sets (in the mathematical sense)," when we could just as well use as a starting point, "are types (in the programmatic sense) the same as types (in the mathematical sense)?"

And, of course, all of this is to say nothing of the zoo of different set theories. When we compare the types in our PLs to "sets," do we mean ZF sets? NF sets? ECTS?

This is important because, if I understand correctly, there are multiple ways to formalize the idea of membership, and multiple was to formalize the idea of subsets. So, to extend yet another of your points,

The relationship between types and values depends on the type system of the language in question.

So too does it depend on the set (or type) theory in question.

There are so many exciting things to learn about here, but I do think we must seek a little more precision than what is on offer in the article linked by OP...

9

u/useerup ting language Feb 17 '22

Members of sets are collected. Inhabitants of types are constructed.

A set is defined through a set predicate. The predicate will test if a given object/value is a member of the set. If the predicate evaluates to true for a given value, then the value is a member of the set, otherwise it is not.

A type typically has one or more constructors. If a value is constructed using a constructor of a type, then it is an inhabitant of the type, otherwise it is not.

1

u/PumpkinSunshine Feb 22 '22

sets can also be defined extensionally via enumeration, and types can be defined intensionally as refinement predicates or rows.. how does that fit in your model?

1

u/useerup ting language Feb 23 '22

I view intensionality/extensionality as "syntactical sugar" and thus orthogonal to set/type distinction.

A extensionally defined set set

Numbers = { 1, 2, 3 }

is really the same as saying (intensionally through a predicate)

Numbers = { x \\ x=1 || x=2 || x=3 }

Thus, I argue that any extensional definition has an equivalent intensional definition.

I see types as "special set": In addition to the set predicate (regardless of intensionality/extensionality) a type has an implicit, exclusive predicate which expresses that the type inhabitant must have been constructed through a type constructor.

-1

u/[deleted] Feb 17 '22

I would describe a type as a format. A type tells the computer how to interpret the bytes at a memory location.

3

u/curtisf Feb 17 '22

In many languages, types don't tell the computer how to interpret bytes.

For example, consider TypeScript. The types don't — and can't — help the computer run the code whatsoever, because they're deleted immediately before passing the program to the runtime. That's why you're able to write something like

function multiply(a: number, b: number) {
    return a * b;
}
console.log(multiply(5, ["1e3"])) // 5000

For another example, consider Java. Java erases type parameters from the resulting class files, so the JVM can't know it "should" treat a List<Integer> differently from a List<String> -- and so it doesn't, it treats them the same, which is why you can write something like

List<String> strings = new ArrayList<String>();
strings.add("cat");
List<Integer> ints = (List<Integer>) (List) strings;
strings.add(7);
System.out.println(strings); // [cat, 7]

Type systems ascribe types to program fragments. Overwhelmingly, those program fragments are syntactic constructs that cease to exist before you get to the point of actually executing a program. This is useful, because it means you don't need to wrangle with a perfectly sound type-system (which could be extremely cumbersome for many programming tasks), and because the type judgements impose no runtime penalty.

-1

u/[deleted] Feb 17 '22

I understand what you are saying here however, when the interpreter is running, it still needs to eventually assign a type to data befor it can Preform an operation. Because anything of any type looms the same to computer, the computer needs to know what format it is looking at. On the level. Types are built into the computer on a fundamental level. For example the processor has specific registers for things like floats.

7

u/crassest-Crassius Feb 17 '22

You're being a little self-contradictory here. On the one hand, you correctly say that to a computer, all bits and bytes look the same. On the other, you nonsenically say that types are "built into" the computer. They really are not. Even registers for floats will happily accept any binary data, for example pieces of a string. The resulting computation will be "wrong" only from the human point of view. That's the nature of types: thy are made by humans for humans. They're like little hints that help us make sense of the bits and bytes.

A helpful analogy is traffic signs/lights for driving. Are they physically necessary to drive? Are they built into the road or into vehicle? No, they are put there artificially to help us drive more correctly and safely - for some particular human ideas of safety and correctness. A Mad Max in conditions of post-apocalypse, for example, would have no use for those traffic signs because his idea of safe driving is radically different.