yeslogic / fathom

🚧 (Alpha stage software) A declarative data definition language for formally specifying binary data formats. 🚧
Apache License 2.0
258 stars 14 forks source link

Sum types? #411

Open Kmeakin opened 2 years ago

Kmeakin commented 2 years ago

Fathom currently has anonymous product types (records), but no (convenient) way of expressing sum types.

Potential solutions

Design considerations

Prior Art

eashanhatti commented 2 years ago

Another consideration is recursive types! If sum types are not anonymous then things become a lot easier, but if they're anonymous it gets tricky. In the latter case, maybe you could use isorecursive types and insert roll and unroll during elaboration?

brendanzab commented 2 years ago

At one stage I was thinking you could add an structural enum type:

enum { l₁, ..., lβ‚™ } : Type

       lβ‚˜ ∈ { l₁, ..., lβ‚™ }
────────────────────────────────
 enum lβ‚˜ : enum { l₁, ..., lβ‚™ } 

  e : enum { l₁, ..., lβ‚™ }      t₁ : t  ...  t₁ : tβ‚™
─────────────────────────────────────────────────────
  match e { enum l₁ => t₁, ..., enum lβ‚™ => tβ‚™ } : t

Which could be used for the tags of records. This is a bit like Martin LΓΆf’s finite sets (I think?). I was also thinking that there would be some corresponding format for enums as well. In this case we’d need some syntactic sugar or elaborator support to make tagged unions bearable if we went that route. I’d also imagine that it would require us to implement dependent pattern matching on records (which we don’t yet do). And yeah recursive types, and mutually recursive types could be… fun?

We already have top level items so maybe it would be easier to go with top level tagged union declarations like Haskell, OCaml, Rust? Save the experimentation for another time and place?

brendanzab commented 2 years ago

IIRC Mini-TT had some simple form of tagged union that might be interesting to look at.

brendanzab commented 2 years ago

I think one thing that might help in terms of design is thinking in terms of how this might help us with binary data descriptions. Eg. Looking through the OpenType data description, where would tagged/disjoint unions help us. And also make us think about how we might have corresponding formats that might produce these tagged unions.