4 ms·
A classic way to do this, is enumerations.
by ChrisMarshallNY 4y ago
A classic way to do this, is enumerations.
- the__alchemist 4y agoADTs is a concept I've had a tough time understanding; the approximate definition that made the most impact with is considering them as a specific type of enumerations: Ones where each variant holds a value. Is this correct?
- ChrisMarshallNY 4y agoThat’s the way Swift enums work. Here’s an interesting little exploration: https://littlegreenviper.com/miscellany/swiftwater/writing-a-robust-little-parser-in-swift-epilogue/#peek https://littlegreenviper.com/miscellany/swiftwater/writing-a...
- thesz 4y agoIt is the other way around: enumerations are special cases of algebraic data types with no values attached. In algebraic data types we have a sum (union) of a labeled cartesian products, and each product may have arbitrary number of values: data MaybeInt = NoInt | AnInt Int data MaybeItsPair = NoIntsPair | AnIntPair Int Int The first constructor in both data types is a labeled (No...) product with empty number of value to apply cartesian product to. The second constructor in both data types is a cartesian product: in first case an Int and in case a pair of Ints. Of course you can have more involved data type: data Expr = Void | Const Int | Var String | Bin BinOp Expr Expr | Un UnOp Expr A label, two single value constructors and much more involved operations as well.
- ghostpepper 4y agoCan you explain the term 'cartesian product' in this context? I'm familiar with the cross product from undergrad linear algebra but I haven't applied algebra to types before, and I don't understand what two sets would produce a single Int as their product. I haven't studied much theoretical computer science so I'd love to hear some good beginner resources on this stuff.
- thesz 4y agoCartesian product: https://en.wikipedia.org/wiki/Cartesian_product https://en.wikipedia.org/wiki/Cartesian_product The number of variants of the AnIntPair construction is the number of distinct elements in the Int type, squared. I used definition of algebraic data type in Haskell: http://wiki.haskell.org/Algebraic_data_type http://wiki.haskell.org/Algebraic_data_type
- justinpombrio 4y ago"Cartesian product" is a fancy word for "pair". Or more generally "tuple".
- KMag 4y agoThe nice thing about ADTs is that you can couple the state variables with the enum, so that the representable state is better tied to the enum value. (Yes, you can kind of do this in C with a discriminated union of a struct holding an enum and a union, but the type system doesn't stop you form grabbing a union member that doesn't match up with the current enum value.)
- ChrisMarshallNY 4y agoI use Swift. Swift enums are really nice[0]. [0] https://littlegreenviper.com/miscellany/swiftwater/enums-with-associated-values/ https://littlegreenviper.com/miscellany/swiftwater/enums-wit...
- jb1991 4y agoI like Swift but the examples on that page are quite over-engineered. Reads like someone was seduced by their own cleverness. There are much simpler ways to achieve what is being done in that example.
- ChrisMarshallNY 4y agoIf you read that page, you’d understand that it was supposed to be “over-engineered.” I assume that you did read it, and took the least charitable viewpoint. I didn’t post it for peer review. It reflects my enthusiasm, as I was learning the language, and is probably four years old. I'm a fairly decent chap, and have nothing against you. Not sure why it's so important to you to begin our relationship with an attack. I suspect we have far more in common, than we do, differences.
- jb1991 4y agoMy apologies, I didn’t realize you were posting a link to your own site! I would have phrased my comment differently, although probably that should not have made a difference and I should’ve phrased it differently anyway.
- peterfirefly 4y agoHence why they are called enums in Rust.
- jlouis 4y agoEnumerations are not ADTs.