Fix overly general typeclass in generated type annotations#133
Open
dillonkearns wants to merge 1 commit intomdgriffith:mainfrom
Open
Fix overly general typeclass in generated type annotations#133dillonkearns wants to merge 1 commit intomdgriffith:mainfrom
dillonkearns wants to merge 1 commit intomdgriffith:mainfrom
Conversation
dee3310 to
2401bdf
Compare
miniBill
reviewed
Apr 6, 2026
Contributor
|
Would love to see a compappend test too |
dillonkearns
added a commit
to dillonkearns/elm-codegen
that referenced
this pull request
Apr 10, 2026
The compappend typeclass is satisfied by String and List comparable (types that are both comparable and appendable). This test verifies that a user-provided annotation with a `compappend` variable is preserved in the generated type annotation. Requested in PR mdgriffith#133 comment. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
b2236db to
2401bdf
Compare
When a type variable stays polymorphic through an operator that requires a typeclass constraint (+ requires number, < requires comparable, ++ requires appendable), the constraint was lost during type variable rewriting, producing `a -> a -> a` instead of `number -> number -> number`. Root cause: `resolveVariables` replaces constrained names (like `number_0` or `comparable`) with arg variable names (like `arg_0`). Then `rewriteTypeVariables` renames `arg_0` to `a`, losing the constraint. Fix: `rewriteTypeVariablesPreservingConstraints` builds a mapping from resolved variable names back to their constraint names by walking the inference cache bidirectionally. If a constrained name maps to a generic (forward: `number_0 → arg_0`) or a generic maps to a constrained name (reverse: `arg_0 → comparable`), the constraint name is preserved during rewriting. Before: Elm.Op.plus a b → addBoth : a -> a -> a Elm.Op.lt a b → compareBoth : a -> a -> Bool Elm.Op.append a b → appendBoth : a -> a -> a After: Elm.Op.plus a b → addBoth : number -> number -> number Elm.Op.lt a b → compareBoth : comparable -> comparable -> Bool Elm.Op.append a b → appendBoth : appendable -> appendable -> appendable Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2401bdf to
3022b25
Compare
Contributor
Author
I tried adding a test, but since there isn't a way to use elm-codegen to generate a value of this type, it ends up not being a meaningful test case to just hardcode in the |
This file contains hidden or 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
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.
Functions using
+,<, or++were incorrectly resulting in type annotations with plain type variables instead of constrained ones, producing code that doesn't compile.Example
Before (broken)
The Elm compiler rejects this:
After (fixed)
This applies to all three typeclass constraints:
Elm.Op.plus a ba -> a -> anumber -> number -> numberElm.Op.lt a ba -> a -> Boolcomparable -> comparable -> BoolElm.Op.append a ba -> a -> aappendable -> appendable -> appendableWhen one operand is concrete, the constraint correctly narrows: