Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complex types & layouts #11

Open
4 of 6 tasks
timothee-haudebourg opened this issue Mar 25, 2022 · 0 comments
Open
4 of 6 tasks

Complex types & layouts #11

timothee-haudebourg opened this issue Mar 25, 2022 · 0 comments

Comments

@timothee-haudebourg
Copy link
Collaborator

timothee-haudebourg commented Mar 25, 2022

This is a tracking issue for the implementation of complex types and layouts. Here is the current state of implementation. Below are the specification for each feature:

  • Union/sum types & layouts
  • Intersection types & layouts
  • Literal types
  • Enumeration layouts
  • Restrictions
  • Predicate constraints

Priority goes to the implementation of literal types (without regular expression) and union types.

Union/sum types & layouts

A sum type (or union type) is defined as the union of a set of types. A value is an instance of this type if it is an instance of at least one type of the set. It is defined using the | operator.

type Sum = A | B | C

The layout of this type is an union layout, following a similar syntax (although it does not need to be explicitly defined):

layout SumLayout = ALayout | BLayout | CLayout

Such layout is translated into an enumeration in the target language. Here in Rust:

enum SumLayout {
  A(ALayout),
  B(BLayout),
  C(CLayout)
}

If possible, the definition can be inlined in the target language, here in TypeScript:

type SumLayout = ALayout | BLayout | CLayout

Intersection types & layouts

Similarly, an intersection type is the intersection of a set of types, defined with the & operator.

type Inter = A & B

The layout of this type is a structure defining fields that correspond a property of any of the field in the intersection. The structure must define fields for the required property of all the type in the intersection.
The implementation may be delayed for a future PR since it is not required now.

Literal types & layouts

Literal types define a subset of all literal values using either a string, or a regular expression (using at least POSIX Basic Regular Expressions for now). For instance, the following type defines
the property constant whose value must be equal to "foo".

type Foo {
	constant: "foo"
}

Regular expression types

Literal types are a particular case for regular expression types,
where each instance of the type matches the defining regular expression.

The following type defines the regexp property whose value must
match the /foo+/ regular expression recognizing every "fo...o" string with at least two o.

type Foo {
	regexp: /foo+/
}

Enumeration layouts

An enumeration layout is defined as list of variants declared between [ ].

layout Enum [A, B]

Such layout will be translated into an enumeration type in the target language.
Each variant can have a payload, defined as a structure layout (between { }). Variants must not be ambiguous: there must be a way of telling variants apart just by looking at the payload, and not at the name of the variant. If a variant has no payload, it must be assigned a singleton value using the =.

For instance, the following layout can be defined for the rdf:list type:

layout List for rdf:type [
  Nil = rdf:nil,
  Cons {
    rdf:first: Any,
    rdf:rest: List
  }
]

Restrictions

Intersection type can also be defined using restrictions defined
using the where keyword.
For instance the following type defines the subtype of wine located in the Bourgogne Region of France:

type Burgrundy = Wine where locatedIn: "bourgogne"

Predicates constraints

Type constraints using predicates, such as numerical constraints:

type Age = { x: xsd:integer, x >= 0 && x < 130 }`

Syntax is not fixed yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant