this post was submitted on 11 Aug 2024
15 points (100.0% liked)
Programming
17398 readers
94 users here now
Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!
Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.
Hope you enjoy the instance!
Rules
Rules
- Follow the programming.dev instance rules
- Keep content related to programming in some way
- If you're posting long videos try to add in some form of tldr for those who don't want to watch videos
Wormhole
Follow the wormhole through a path of communities !webdev@programming.dev
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
Your post only showed adding functionality over the algebra, not new types on which the algebra operates (or "sorts", as they are otherwise known). In other words, you can't easily extend
Expr
to support Boolean logic in addition to addition itself. For a concrete example, how could you represent ternary operators like in the expression2 + 2 == 4 ? 1 : 2
, such that it's well typed and will never result in an exception? With GADTs, this is very simple to do:lemmy seems to have lost my response to this, so I'll type it again and hope that it doesn't show up twice
There's three separate issues here:
The ability to express multi-sorted algebras
The ability to extend algebras with new sorts
The ability to extend a single sort of an algebra with new variants
For point 1, object algebras do support this even though I didn't show it in the post:
For point 2, you are correct that the original Java formulation of object algebras does not support data sort extensibility. I haven't tried to see if TS's more powerful generics change this or not.
For point 3, consider what happens if you want to add a new variant to the data type. In this case, add a
Mult
variant for multiplication. With GADTs this is not possible without modifying or duplicating the existing evaluation code, but I can do it with object algebras:This is the point of object algebras. ADTs and GADTs can't do this out of the box; this is the essence of the expression problem and is why more advanced techniques like final tagless encodings and datatypes a la carte exist.