this post was submitted on 11 Apr 2024
13 points (93.3% liked)
Programming Languages
1177 readers
1 users here now
Hello!
This is the current Lemmy equivalent of https://www.reddit.com/r/ProgrammingLanguages/.
The content and rules are the same here as they are over there. Taken directly from the /r/ProgrammingLanguages overview:
This community is dedicated to the theory, design and implementation of programming languages.
Be nice to each other. Flame wars and rants are not welcomed. Please also put some effort into your post.
This isn't the right place to ask questions such as "What language should I use for X", "what language should I learn", and "what's your favorite language". Such questions should be posted in /c/learn_programming or /c/programming.
This is the right place for posts like the following:
- "Check out this new language I've been working on!"
- "Here's a blog post on how I implemented static type checking into this compiler"
- "I want to write a compiler, where do I start?"
- "How does the Java compiler work? How does it handle forward declarations/imports/targeting multiple platforms/?"
- "How should I test my compiler? How are other compilers and interpreters like gcc, Java, and python tested?"
- "What are the pros/cons of ?"
- "Compare and contrast vs. "
- "Confused about the semantics of this language"
- "Proceedings from PLDI / OOPSLA / ICFP / "
See /r/ProgrammingLanguages for specific examples
Related online communities
- ProgLangDesign.net
- /r/ProgrammingLanguages Discord
- Lamdda the Ultimate
- Language Design Stack Exchange
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
That makes sense for
str | int
, but how is an enum a "sum type"?As for product types, in set theory a product of sets is a cartesian product. How is a
a product? What is it a product of? And why is the type itself a product, not
Dog x Cat
? Or isDog x Cat
indeed some kind of product that I'm not aware of but with another syntax?Anti Commercial-AI license
Well what is an enum except a chain of
X | Y | Z | ...
. An enum can be any of its variants, and therefore the set of its possible values are just all possibilities of its variants added together.Consider this enum:
The possible values for
A
are just one:A
. The possible values forB
areB( true )
andB( false )
. So the total possible values forFoo
are simply these sets combined:A
orB( true )
orB( false )
.As for product types, what it is the product is, is still the same: the sets of possible values. Consider the possible values for the product of
A
andB
. For every possible value ofA
, a value could be made by matching it with any possible value ofB
(so, multiplication). If there are 3 possible values ofA
, and two possible values ofB
, then the total number of possible combinations of these for the product type is 6.In your example,
Dog
is a product ofu8
, anotheru8
, andString
. If you decide to add a Boolean field to this type, accordingly the size of the set of options would double, because for every possibleDog
you currently have, two possibilities would be created, one with atrue
and one with afalse
.As for your last question, some languages might use
x
as a product type syntax, but because tuples and structs are inherently product types, most languages use those as Syntax. For example in Haskell the product type ofDog
andCat
would be written as(Dog, Cat)
.