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

Rewrite rules for constraints #33

Open
amigalemming opened this issue Jan 24, 2023 · 2 comments
Open

Rewrite rules for constraints #33

amigalemming opened this issue Jan 24, 2023 · 2 comments

Comments

@amigalemming
Copy link

I would like to write a rule like:

type SimplifyUnary (n::Nat) = IsUnary (ToUnary n) ~ (()::Constraint)

where IsUnary is a type class.
Currently the rule seems to have no effect. If it had, would it work at all? I mean, GHC might be missing the IsUnary class method dictionary.

@gelisam
Copy link
Owner

gelisam commented Jan 24, 2023

Currently the rule seems to have no effect.

It has no effect in code like IsUnary (ToUnary 4) => Int because typelevel-rewrite-rules only rewrites the types which occur inside equality constraints: in (A a ~ B a, C a) => D a, it only touches A a and B a, not C a.

@gelisam
Copy link
Owner

gelisam commented Jan 24, 2023

If it had, would it work at all? I mean, GHC might be missing the IsUnary class method dictionary.

When typechecker plugins remove constraints, they do need to insert a dictionary to compensate. See e.g. my tutorial on implementing "magic typeclasses".

Because typelevel-rewrite-rules only touches a ~ b constraints, I always use a dictionary which basically does unsafeCoerce: https://github.com/gelisam/typelevel-rewrite-rules/blob/main/src/TypeLevel/Rewrite.hs#L179

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

2 participants