3 ms·
> To make sum types work, you NEED to support embed relations inside relations You don't need to embed relations. You don't even need to embed tuples or other
by tome 5y ago
> To make sum types work, you NEED to support embed relations inside relations
You don't need to embed relations. You don't even need to embed tuples or other composite types, you can just model sum types using a pair of fields. The latter is what Opaleye and Rel8 (Postgres libraries for Haskell) do:
https://hackage.haskell.org/package/opaleye-0.7.1.0/docs/src/Opaleye.Internal.MaybeFields.html#MaybeFields https://hackage.haskell.org/package/opaleye-0.7.1.0/docs/src...
- alexvoda 5y agoYou linked to a file with pages of code. Care to summarize how they model sum types using a pair of fields?
- mamcx 5y agoCan be show how is done, with data instead of algorithms? (aka "...show me your tables and I won't usually need your flowcharts; they'll be obvious." ~ Fred Brooks") Haskell is hard to decipher for me...
- tome 5y agoTo reply to both my respondents, the link goes exactly to the source for Opaleye's version of the Maybe data type definition. Neatened up, it is data MaybeFields fields = MaybeFields { mfPresent :: Field SqlBool , mfFields :: fields } That is, an Opaleye Maybe (MaybeFields) is a pair. The first component is an SQL bool indicating whether it's a Just or a Nothing. The second component is the payload of the Just. If it's a Nothing the fields of the payload will be set to NULL.
- mamcx 5y agoOk, this is for a Maybe/Option, but can't see how can work for any algebraic type. I have this for mine: struct Ag { tag :String, //like "Option.None" data:Vec<Value> //The data inside, if any }
- tome 5y agoArbitrary sum types are just a generalisation of what I wrote. Opaleye doesn't have them (yet?) but rel8 does: https://hackage.haskell.org/package/rel8-1.1.0.0/docs/src/Rel8.Table.Either.html#EitherTable https://hackage.haskell.org/package/rel8-1.1.0.0/docs/src/Re... You have a bool which specifies whether it's left or right, and then two payloads, one for left and one for right.