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

Variant type spreads #6316

Merged
merged 9 commits into from
Jul 7, 2023
Merged

Variant type spreads #6316

merged 9 commits into from
Jul 7, 2023

Conversation

zth
Copy link
Collaborator

@zth zth commented Jun 29, 2023

Feature

Allow spreading variant type definitions.

type aa = One | Two
type bb = | ...aa | Three | Four

// bb = | One | Two | Three | Four

Assumptions

  • Allow spreading anything that can be looked up at the point of type checking the variant that has the spreads. This means that we for example don't allow spreading recursive variant definitions. If you try and spread a variant that cannot be looked up, you'll get an error.
  • Any constructor shape is allowed - without payload, with tuple argument payload, with inline record payload.
  • Both the variant that is spread, and the variant it's spread into, must conform to the same runtime representation. This means that the @unboxed and @tag configuration needs to match between the variants. If they don't match, you'll get an error.
  • Don't allow using type parameters with spreads. Can potentially be solved in the future. If you spread variants with type parameters, you'll get an error.
  • Duplicate and/or overriding constructors are not allowed. If you spread a variant that has constructors already present, you'll get an error.
  • Spreading means "copy paste" - the constructors from type aa are taken and copied to bb.

Description of how this PR works:

Parsing

Each spread parses to a constructor called "...", with a single payload pointing to the variant the spread is for. So this:

type a = | One | ...b

Parses to this:

type a = | One | ...(b)

These "spread" constructors will then be handled in the type checker.

Handling the spreads in the type checker

To process the spread elements in the type checker, we introduce a temporary "dummy" constructor, holding each variant type we're spreading, in the parse tree before it enters the type checker. In addition to this, we also copy paste each constructor from the variant we're spreading into the parse tree of the variant being spread into.

These dummy constructors, e.g., ...(b), are maintained temporarily during the type checking process, to be later removed.

To illustrate:

type aa = One | Two
type bb = | ...aa | Three | Four

The parse AST when this hits the type checker will look like this:

type aa = One | Two
type bb = | ...(aa) | One | Two | Three | Four

Subsequently in the type checker, we iterate through every constructor and if it turns out to be a dummy constructor, we look up the variant that dummy constructor holds (e.g., ...(b), lookup variant b). We then replace the constructor with the types provided by the type checker in the dummy constructor.

After this process, we purge all dummy constructors, leaving behind a fully defined variant type with each inlined constructor, complete with its correct type and arguments, derived from each spread variant.

This approach allows us to leverage the type checker to perform most of the complex work.

Important Considerations:

  1. When introducing dummy constructors before type checking, it's important to remember that we should not include their respective arguments at this stage, even if these constructors are inherently associated with arguments. The rationale behind this approach is that the actual type checking of these arguments occurs at the definition site for the spread variant. By postponing the inclusion of arguments, we avoid unnecessary type checking during the dummy constructor phase. Later, when needed, we copy the genuine, type-checked constructor arguments from the spread variant definition into our construct. This ensures that the arguments are appropriately type-checked at their original definition site, rather than prematurely during the dummy constructor phase.
  2. Consistency in length for constructors, arguments, etc., is vital for compatibility. Therefore, we insert dummy elements as necessary to maintain this uniformity in length.

@zth zth requested a review from cristianoc July 2, 2023 20:07
@zth zth marked this pull request as ready for review July 5, 2023 13:44
@zth zth marked this pull request as draft July 7, 2023 06:57
@zth zth removed the request for review from cristianoc July 7, 2023 06:57
Copy link
Collaborator

@cristianoc cristianoc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discusses offline, there might be opportunities to analyse the implementation further and perhaps simplify it.
However, for an early feature, this has plenty of tests and there's little risk that this will affect other language features.

The implementation can be refined later when there's more time.

@cristianoc cristianoc marked this pull request as ready for review July 7, 2023 11:24
@cristianoc cristianoc merged commit df1bc3d into master Jul 7, 2023
@cristianoc cristianoc deleted the variant-spreads branch July 7, 2023 11:24
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

Successfully merging this pull request may close these issues.

2 participants