3 ms·
I'm not clear that I see the benefit of the techniques here? For one, it is not necessarily obvious that the "or (|)" is not a distributive property. It is, i
by taeric 2y ago
I'm not clear that I see the benefit of the techniques here? For one, it is not necessarily obvious that the "or (|)" is not a distributive property. It is, instead, a population description? As such, it seems like it leads to a very weird statements compared to what you would get in other uses.
For example, if instead of Role, it was IsAdmin, and IsAdmin = True | False, then your application here gets you the odd True = Name x Password. What does that even mean?
It also has an odd one of what if Role = Normal + Admin + SuperAdmin, but User is still Admin | Normal? What does that even mean?
- noelwelsh 2y agoOr is + (Writing or was a typo) User has a Name, Password, and a Role, which gives User = Name x Password x Role Role is Normal or Admin. Formally: Role = Normal + Admin Now substitute User = Name x Password x (Normal + Admin) Multiply out User = (Name x Password x Normal) + (Name x Password x Admin) I added an extra step where I named some of the components. If you understand how this can be represented in code as an algebraic data type, you will see these two different representations have different properties when you come to program with them. If you don't, I'm afraid this text box does not contain enough space. Chapter 2 of https://scalawithcats.com/ https://scalawithcats.com/ goes through this in much more detail.
- taeric 2y agoCool, was curious why you had + and "or". I was assuming both are the same as | in the linked article. (I am further assuming you are largely working on the "left hand side" per the article?) The rest of your math, I largely followed, but I can't see where `Normal = Name x Password` results. Nor do I see what it would communicate. This is why I dropped instead to Boolean = True | False. If I substitute that in your statements, I get `True = Name x Password`. And I don't see how that means anything. :(
- noelwelsh 2y agoName and Password are probably going to wrap the String type. Admin and Normal don't have data associated with them. They are just flags (or tags, depending on the terminology you are used to.) So when we write User = (Name x Password x Normal) + (Name x Password x Admin) we know that Normal and Admin distinguish two otherwise identical cases. So with a bit of substitution we can write User = Normal + Admin Normal = (Name x Password) Admin = (Name x Password) True + False is isomorphic to Normal + Admin, but if you aren't used to working in a language with algebraic data types you might not be used to thinking in this way. In Haskell you'd write something like data User = Normal Admin Password | Admin Name Password You could equally write data User = True Admin Password | False Name Password because True and False are just tags, distinct from their use in the boolean type. This would just really confusing to other programmers.
- taeric 2y agoI had a jest, at one point, where I would assume you are not modeling a password in your system. :D I'm assuming you'd instead model an AuthenticatedUser or some such. To that end, I'm fine with giving leeway to the modeling here. We are going for pedagogy. To that end, if this is a matter of going fast due to constraints of a message board, I am fine. My specific confusion was at the start where you had `Admin = Name x Password`. I'm not at all clear on what that would communicate to me in the types of any variables I would have in my code.
- noelwelsh 2y agoHave you used a language with algebraic data types? I assumed some familiarity, and also with the terminology sum and product types. Admin = Name x Password defines a type called Admin that has a Name and a Password. In OCaml I would write something like type Admin = Name * Password In Scala I'd write final case class Admin(name: Name, password: Password) The construction User = Normal + Admin Normal = (Name x Password) Admin = (Name x Password) in words is: A User is Normal or Admin A Normal (User) is a Name and a Password An Admin (User) is a Name and a Password In Scala enum User: case Normal(name: Name, password: Password) case Admin(name: Name, password: Password) In OCaml type User = Normal of Name * Password | Admin of Name * Password