Ampersand icon indicating copy to clipboard operation
Ampersand copied to clipboard

Syntax for union-types

Open sjcjoosten opened this issue 4 years ago • 26 comments

Problem

Suppose we have a generic concept and two specific concepts, e.g.

contract :: Employee * Contract
CLASSIFY FixedTermContract ISA Contract
CLASSIFY PermanentContract ISA Contract

How do we build an interface for Contract?

The general problem is how to combine interfaces for specific concepts into an interface for a generic concept. Currently we have no operators to combine interfaces, so this calls for a language extension. This issue is to agree on some syntax for that.

Proposal

For example, an employee can be on a fixed-term and on a permanent contract, and the contract interface for such an employee may have two different looks. However, the concept that varies is Contract (which can be Permanent or Fixed-term):

contract :: Employee * Contract
CLASSIFY FixedTermContract ISA Contract
CLASSIFY PermanentContract ISA Contract
startdate :: Contract * Date
enddate :: FixedTermContract * Date

My proposal is to use the operator | for this:

INTERFACE EmployeeContract : I[Employee]
  BOX [ "Employee" : I
      , "Contract" : contract
       BOX [ "Start" : startdate
              , "??" : I[FixedTermContract] BOX [ "End" : enddate ]
                     | I[PermanentContract] BOX []
              ]
      ]

The semantics would be similar to having a , there, with two subtle differences:

  • A , does not allow you to use the same label twice
  • The | implies that only one of the two (or more) options can be used, never both. For displaying (read) this should mean that the first of the two is given preference.

For me personally, the need for | came up when working on Issue #1071 as I don't have a way to specify Haskell data-types with unions in them.

sjcjoosten avatar Apr 16 '20 21:04 sjcjoosten