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

Macro type-checking: support arithmetic conversions #299

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

sheaf
Copy link
Collaborator

@sheaf sheaf commented Nov 22, 2024

Builds on top of #296.

This PR generalises macro typechecking to take into account C
arithmetic conversion rules, e.g. in an operation such as

  a + b

the values a and b are allowed to have different types, with
implicit conversions being inserted. For example:

  • a :: CFloat, b :: CInt => convert b to CFloat before adding
  • a :: Int, b :: UInt => convert a to UInt before adding

The main challenge was to generalise the defaulting infrastructure to allow defaulting a multi-parameter class constraint such as
Add alpha[tau] beta[tau] with the equality alpha ~ beta.

This commit generalises macro type-checking, making class constraints
more general by allowing what GHC calls FlexibleContexts and
FlexibleInstances.

Key changes:

  - A special monad for solving, TcSolveM.
    This monad keeps track of a work list and a set of
    inert (= fully processed) constraints.

  - Solving a constraint using a top-level instance may now add
    additional constraints to the work list.

  - Instances are keyed using a TrieMap, similar to GHC's RoughMap,
    which avoids traversing all instances when doing instance matching.
The solver didn't handle equalities properly, as it didn't account for
the result of unification. Now, we try to unify, and if the unifier
makes no progress we make the constraint into an inert constraint.
@sheaf sheaf changed the title Tc macro conversions Macro type-checking: support arithmetic conversions Nov 22, 2024
This commit generalises macro typechecking to take into account C
arithmetic conversion rules, e.g. in an operation such as

  a + b

the values 'a' and 'b' are allowed to have different types, with
implicit conversions being inserted. For example:

  - a :: CFloat, b :: CInt => convert b to CFloat before adding
  - a :: Int, b :: UInt => convert a to UInt before adding
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.

1 participant