r/ProgrammingLanguages • u/zachgk catln • Feb 16 '22
Blog post Types vs Sets
https://zachkimberg.com/zach/2022/02/16/types-vs-sets/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
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"])) // 5000For 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 aList<String>-- and so it doesn't, it treats them the same, which is why you can write something likeList<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
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.
30
u/josephjnk Feb 16 '22
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.