-
Notifications
You must be signed in to change notification settings - Fork 0
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
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sheaf
changed the title
Tc macro conversions
Macro type-checking: support arithmetic conversions
Nov 22, 2024
sheaf
force-pushed
the
tc-macro-conversions
branch
2 times, most recently
from
December 5, 2024 09:15
7ea2449
to
0d74ee3
Compare
sheaf
force-pushed
the
tc-macro-conversions
branch
from
December 5, 2024 16:50
54d5786
to
20a2374
Compare
sheaf
force-pushed
the
tc-macro-conversions
branch
9 times, most recently
from
December 27, 2024 15:57
2692ed0
to
1f54608
Compare
sheaf
commented
Jan 8, 2025
sheaf
commented
Jan 8, 2025
sheaf
commented
Jan 8, 2025
sheaf
commented
Jan 8, 2025
sheaf
commented
Jan 8, 2025
sheaf
commented
Jan 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For platform-dependent instances, we should provide separate modules with the instances for all supported platforms. Possibly also a Host
platform that uses CPP to re-export the appropriate module.
sheaf
commented
Jan 8, 2025
sheaf
commented
Jan 8, 2025
sheaf
commented
Jan 8, 2025
sheaf
commented
Jan 8, 2025
sheaf
commented
Jan 8, 2025
sheaf
commented
Jan 8, 2025
sheaf
commented
Jan 8, 2025
sheaf
commented
Jan 8, 2025
sheaf
commented
Jan 8, 2025
sheaf
commented
Jan 8, 2025
sheaf
force-pushed
the
tc-macro-conversions
branch
13 times, most recently
from
January 13, 2025 15:43
65eb9e7
to
3485941
Compare
This commit generalises macro type-checking to handle C arithmetic conversion rules as per the C standard. What this means is that in a C expression 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 This is achieved as follows: - a new package, c-expr, with the following components: - c-expr-core, a public sublibrary which: - defines standard C types and C operators, - implements the C arithmetic conversion rules - defines a collection of type-classes that can be used for a DSL for C arithmetic expressions - uses Template Haskell to define a function that can, given a platform, implement all the instances of these typeclasses for types such as CInt, CUInt, CFloat, pointers, etc - c-expr, the main library, which exports **platform-dependent** modules such as `C.Expr.Posix32` or `C.Expr.Win64` with type-class instances for all the C arithmetic typeclasses - a test-suite, which uses hsbindgen-libclang to invoke Clang on test programs in order to check that the implementation of C arithmetic conversion rules in 'c-expr-core' are compatible with Clang - the C macro typechecker imports c-expr-core, which it uses to compute type family reduction (e.g. the 'SubRes' type family which computes the return type of the subtraction operator given its argument types) - hs-bindgen generates programs that will depend on c-expr, e.g. to turn a C macro into a Haskell function we will generate a module that imports the C arithmetic operator DSL provided by c-expr To do this, we also needed to significantly generalise the macro typechecker: - Make class constraints more general by allowing what GHC calls FlexibleContexts and FlexibleInstances, - Use a special monad for constraint 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.
This commit includes all the test changes subsequent to the previous commit. It was kept separate for the sake of rebasing.
sheaf
force-pushed
the
tc-macro-conversions
branch
from
January 13, 2025 16:04
cf0f847
to
305271b
Compare
Big chunk of work! Great to get this in. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR generalises macro type-checking to handle C arithmetic
conversion rules as per the C standard.
What this means is that in a C expression such as
the values
a
andb
are allowed to have different types, withimplicit conversions being inserted. For example:
a :: CFloat
,b :: CInt
=> convertb
toCFloat
before addinga :: Int
,b :: UInt
=> converta
toUInt
before addingThis is achieved as follows:
a new package,
c-expr
, with the following components:c-expr-core
, a public sublibrary which:a DSL for C arithmetic expressions
a platform, implement all the instances of these typeclasses
for types such as
CInt
,CUInt
,CFloat
, pointers, etcc-expr
, the main library, which exports platform-dependentmodules such as
C.Expr.Posix32
orC.Expr.Win64
withtype-class instances for all the C arithmetic typeclasses
a test-suite, which uses hsbindgen-libclang to invoke Clang on
test programs in order to check that the implementation
of C arithmetic conversion rules in
c-expr-core
arecompatible with Clang
the C macro typechecker imports
c-expr-core
, which it uses tocompute type family reduction (e.g. the
SubRes
type familywhich computes the return type of the subtraction operator given
its argument types)
hs-bindgen
generates programs that will depend onc-expr
, e.g.to turn a C macro into a Haskell function we will generate a module
that imports the C arithmetic operator DSL provided by
c-expr
To do this, we also needed to significantly generalise the macro
typechecker:
Make class constraints more general by allowing what GHC calls
FlexibleContexts and FlexibleInstances,
Use a special monad for constraint 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.