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
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>
dee3310 to
2401bdf
Compare
miniBill
reviewed
Apr 6, 2026
Comment on lines
+314
to
+343
| , test "number constraint with concrete: number -> number -> Float" <| | ||
| -- One arg is concrete (Float), the other stays polymorphic. | ||
| -- In Elm this is: number -> Float -> Float (the number unifies with Float) | ||
| \_ -> | ||
| Elm.Declare.fn2 "addToFloat" | ||
| (Arg.var "a") | ||
| (Arg.var "b") | ||
| (\a b -> Elm.Op.plus a (Elm.Op.plus b (Elm.float 1.0))) | ||
| |> .declaration | ||
| |> Elm.Expect.declarationAs | ||
| """ | ||
| addToFloat : Float -> Float -> Float | ||
| addToFloat a b = | ||
| a + (b + 1) | ||
| """ | ||
| , test "number constraint with one concrete Float arg" <| | ||
| -- When one arg is used with a Float literal, both args | ||
| -- resolve to Float (the number constraint narrows to Float) | ||
| \_ -> | ||
| Elm.Declare.fn2 "addToFloat" | ||
| (Arg.var "a") | ||
| (Arg.var "b") | ||
| (\a b -> Elm.Op.plus a (Elm.Op.plus b (Elm.float 1.0))) | ||
| |> .declaration | ||
| |> Elm.Expect.declarationAs | ||
| """ | ||
| addToFloat : Float -> Float -> Float | ||
| addToFloat a b = | ||
| a + (b + 1) | ||
| """ |
Contributor
There was a problem hiding this comment.
This seems to be the same test twice? I'm guessing one of them was supposed to test (a + b) + 1?
Contributor
|
Would love to see a compappend test too |
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: