Type of system for programming language based on mathematical set theory

  • Thread starter Thread starter elcaro
  • Start date Start date
  • Tags Tags
    Set theory
Click For Summary
A type in programming languages defines the allowed values but often lacks the full set operations found in mathematical set theory. While some languages, like C++, allow for certain combinations of types, most do not support operations like intersection or difference directly on types. The discussion highlights that many programming languages treat values of the same base type as equal, which can lead to issues, such as confusing an error code with an integer. Some languages, like ADA, provide more nuanced type systems that can distinguish between different uses of similar base types. Ultimately, the necessity of such distinctions within a language's type system versus handling them at the application level remains a debated topic.
elcaro
Messages
129
Reaction score
30
TL;DR
Type systems in most programming languages are not very specific about the kind of values the type defines and miss the mathematical operations defined by mathematical set theory, like UNION, INTERSECTTION, MINUS. Is there a programming language which does have a type system that more or less conforms to mathematical set theory?
A type in a programming language more or less specifies what values are allowed for a given type. A type has some similarities with a set, but most programming languages lack the operations which a set has. For example, in C one can define a new type based on two other types as a union of those types with the union keyword, but there is no simple way to create a type based on the minus or interesction of two other types.
For languages like C++, one could define a class for such cases and define methods for defining such operations.
 
Technology news on Phys.org
elcaro said:
most programming languages lack the operations which a set has
This is not correct. Most programming languages have set theoretic operations or equivalents. But most programming languages apply those operations to objects or values, not types. You are talking about applying those operations to types, which is a very different thing. I'm not aware of any programming language that treats types as sets and allows the full range of set theoretic operations on them.
 
PeterDonis said:
This is not correct. Most programming languages have set theoretic operations or equivalents. But most programming languages apply those operations to objects or values, not types. You are talking about applying those operations to types, which is a very different thing. I'm not aware of any programming language that treats types as sets and allows the full range of set theoretic operations on them.
Agree, my point was it is not part of the type system itself, you can't combine defined types into other types (apart from arrays, pointers, struct and union), and type specifications are rather unspecific, they are al based on a small set of basic types (like for example int with specifics only about the bit size and no restrictions on the values that the type defines, or what the value means and if they are comparable with other types that can contain the same value) and programming languages treat values that are based on the same base type as equal, while that in many cases is not true.

For example an error code with a value of 4 is distinct from an int value of 4 in an aritmethic operation, as arithmetic operations as +, -, * and / are not valid operations for for example error codes. In reality, the type for the error code and that for an artihmetic value - even when represented as the same base type = should not be the same type as it would compare apples with oranges or allow operations which are not valid for that type.

Only the type system of some program languages allow to make such distinctions (for example ADA).

It is debatable of course if these kind of abstractions are necessary within the type system of the language, or should be handled by the program itself, as such features are rather specific for the application domain, as it would make the language rather domain specific.
 
elcaro said:
you can't combine defined types into other types
You can in languages that allow multiple inheritance, like Python.

elcaro said:
Only the type system of some program languages allow to make such distinctions
That's true, and it's probably a significant factor in people's choice of programming languages.

elcaro said:
such features are rather specific for the application domain
Yes. And, as you say, that's why such things are often handled at the application level, not the programming language level.
 
elcaro said:
programming languages treat values that are based on the same base type as equal
Not necessarily. Plenty of languages let you define, for example, a type that has the same storage as a 32-bit integer, but is not type compatible with a 32-bit integer, so that the types cannot be used interchangeably.
 
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 41 ·
2
Replies
41
Views
5K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 40 ·
2
Replies
40
Views
8K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 19 ·
Replies
19
Views
583
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K