On Sun, Jun 21, 2026 at 01:05:58PM -0700, Evan Laforge wrote:
I should have tried this before sending! It turns out NoMonomorphismRestriction is not really a usable workaround. First it seems to trigger a bug:
Derive/JScore/T.hs:76:11: error: [GHC-39999] • Ambiguous type variable ‘a0’ GHC Bug #20076 <https://gitlab.haskell.org/ghc/ghc/-/issues/20076> Assuming you have a partial type signature, you can avoid this error by either adding an extra-constraints wildcard (like `(..., _) => ...`, with the underscore at the end of the constraint), or by avoiding the use of a simplifiable constraint in your partial type signature. prevents the constraint ‘(Bounded a0)’ from being solved. Probable fix: use a type annotation to specify what ‘a0’ should be. Potentially matching instances: instance Bounded Ordering -- Defined in ‘GHC.Enum’ instance Bounded Instrument -- Defined at Derive/JScore/T.hs:132:30 ...plus 23 others ...plus four instances involving out-of-scope types (use -fprint-potential-instances to see them all)
This is in a context where all the types are fully specified, and no partial type signatures:
``` data PitchClass = P1 | P2 | P3 | P4 | P5 | P6 | P7 deriving (Eq, Ord, Show, Bounded, Enum) data Pitch = Pitch { pitch_octave :: Int, pitch_pc :: PitchClass } deriving (Eq, Ord, Show)
toEnumBounded :: forall a. (Bounded a, Enum a) => Int -> (Int, a) toEnumBounded n = (i, toEnum r) where (i, r) = n `divMod` (fromEnum (maxBound :: a) + 1)
add_pc_abs :: Int -> Pitch -> Pitch add_pc_abs steps (Pitch octave pc) = Pitch (octave + oct) pc2 where (oct, pc2) = toEnumBounded $ fromEnum pc + steps ```
`toEnumBounded` is considered ambiguous even though `pc2` should be clear. `pc2 :: PitchClass` works around.
With just the quoted above code fragment and "NoMonomorphismRestriction", testing a range of compiler versions I only see the reported "GHC Bug #20076" with GHC 9.4 and 9.6, but the code compiles successfully only with a now somewhat dated GHC 10.0 snapshot I built back in April. Are you using any additional relevant compiler options? -- Viktor. 🇺🇦 Слава Україні! GHCi, version 8.10.7: https://www.haskell.org/ghc/ :? for help [1 of 1] Compiling Main ( /tmp/foo.hs, interpreted ) /tmp/foo.hs:15:11: error: • Ambiguous type variable ‘b0’ prevents the constraint ‘(Bounded b0)’ from being solved. • When checking that the inferred type oct :: forall b. (Bounded b, Enum b) => Int is as general as its inferred signature oct :: Int In an equation for ‘add_pc_abs’: add_pc_abs steps (Pitch octave pc) = Pitch (octave + oct) pc2 where (oct, pc2) = toEnumBounded $ fromEnum pc + steps | 15 | where (oct, pc2) = toEnumBounded $ fromEnum pc + steps | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Failed, no modules loaded. ---- GHCi, version 9.2.8: https://www.haskell.org/ghc/ :? for help [1 of 1] Compiling Main ( /tmp/foo.hs, interpreted ) /tmp/foo.hs:15:11: error: • Ambiguous type variable ‘a0’ arising from GHC Bug #20076 prevents the constraint ‘(Bounded a0)’ from being solved. Probable fix: use a type annotation to specify what ‘a0’ should be. These potential instances exist: instance Bounded Ordering -- Defined in ‘GHC.Enum’ instance Bounded PitchClass -- Defined at /tmp/foo.hs:5:30 instance Bounded () -- Defined in ‘GHC.Enum’ ...plus 19 others ...plus three instances involving out-of-scope types (use -fprint-potential-instances to see them all) • When checking that the inferred type oct :: forall {a}. (Bounded a, Enum a) => Int is as general as its inferred signature oct :: Int In an equation for ‘add_pc_abs’: add_pc_abs steps (Pitch octave pc) = Pitch (octave + oct) pc2 where (oct, pc2) = toEnumBounded $ fromEnum pc + steps | 15 | where (oct, pc2) = toEnumBounded $ fromEnum pc + steps | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Failed, no modules loaded. ---- GHCi, version 9.4.8: https://www.haskell.org/ghc/ :? for help [1 of 2] Compiling Main ( /tmp/foo.hs, interpreted ) /tmp/foo.hs:15:11: error: • Ambiguous type variable ‘a0’ GHC Bug #20076 <https://gitlab.haskell.org/ghc/ghc/-/issues/20076> Assuming you have a partial type signature, you can avoid this error by either adding an extra-constraints wildcard (like `(..., _) => ...`, with the underscore at the end of the constraint), or by avoiding the use of a simplifiable constraint in your partial type signature. prevents the constraint ‘(Bounded a0)’ from being solved. Probable fix: use a type annotation to specify what ‘a0’ should be. Potentially matching instances: instance Bounded Ordering -- Defined in ‘GHC.Enum’ instance Bounded PitchClass -- Defined at /tmp/foo.hs:5:30 ...plus 20 others ...plus three instances involving out-of-scope types (use -fprint-potential-instances to see them all) • In an equation for ‘add_pc_abs’: add_pc_abs steps (Pitch octave pc) = Pitch (octave + oct) pc2 where (oct, pc2) = toEnumBounded $ fromEnum pc + steps | 15 | where (oct, pc2) = toEnumBounded $ fromEnum pc + steps | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Failed, no modules loaded. ---- GHCi, version 9.6.4: https://www.haskell.org/ghc/ :? for help [1 of 2] Compiling Main ( /tmp/foo.hs, interpreted ) /tmp/foo.hs:15:11: error: [GHC-39999] • Ambiguous type variable ‘a0’ GHC Bug #20076 <https://gitlab.haskell.org/ghc/ghc/-/issues/20076> Assuming you have a partial type signature, you can avoid this error by either adding an extra-constraints wildcard (like `(..., _) => ...`, with the underscore at the end of the constraint), or by avoiding the use of a simplifiable constraint in your partial type signature. prevents the constraint ‘(Bounded a0)’ from being solved. Probable fix: use a type annotation to specify what ‘a0’ should be. Potentially matching instances: instance Bounded Ordering -- Defined in ‘GHC.Enum’ instance Bounded PitchClass -- Defined at /tmp/foo.hs:5:30 ...plus 20 others ...plus three instances involving out-of-scope types (use -fprint-potential-instances to see them all) • In an equation for ‘add_pc_abs’: add_pc_abs steps (Pitch octave pc) = Pitch (octave + oct) pc2 where (oct, pc2) = toEnumBounded $ fromEnum pc + steps | 15 | where (oct, pc2) = toEnumBounded $ fromEnum pc + steps | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Failed, no modules loaded. ---- GHCi, version 9.8.3: https://www.haskell.org/ghc/ :? for help [1 of 2] Compiling Main ( /tmp/foo.hs, interpreted ) /tmp/foo.hs:15:11: error: [GHC-39999] • Ambiguous type variable ‘a0’ arising when matching required constraints in a group involving ‘oct’ prevents the constraint ‘(Bounded a0)’ from being solved. Probable fix: use a type annotation to specify what ‘a0’ should be. Potentially matching instances: instance Bounded Ordering -- Defined in ‘GHC.Enum’ instance Bounded PitchClass -- Defined at /tmp/foo.hs:5:30 ...plus 20 others ...plus three instances involving out-of-scope types (use -fprint-potential-instances to see them all) • In an equation for ‘add_pc_abs’: add_pc_abs steps (Pitch octave pc) = Pitch (octave + oct) pc2 where (oct, pc2) = toEnumBounded $ fromEnum pc + steps | 15 | where (oct, pc2) = toEnumBounded $ fromEnum pc + steps | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Failed, no modules loaded. ---- GHCi, version 9.10.3: https://www.haskell.org/ghc/ :? for help [1 of 2] Compiling Main ( /tmp/foo.hs, interpreted ) /tmp/foo.hs:15:11: error: [GHC-39999] • Ambiguous type variable ‘a0’ arising when matching required constraints in a group involving ‘oct’ prevents the constraint ‘(Bounded a0)’ from being solved. Probable fix: use a type annotation to specify what ‘a0’ should be. Potentially matching instances: instance Bounded Ordering -- Defined in ‘GHC.Internal.Enum’ instance Bounded PitchClass -- Defined at /tmp/foo.hs:5:30 ...plus 20 others ...plus three instances involving out-of-scope types (use -fprint-potential-instances to see them all) • In an equation for ‘add_pc_abs’: add_pc_abs steps (Pitch octave pc) = Pitch (octave + oct) pc2 where (oct, pc2) = toEnumBounded $ fromEnum pc + steps | 15 | where (oct, pc2) = toEnumBounded $ fromEnum pc + steps | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Failed, unloaded all modules. ---- GHCi, version 9.12.4: https://www.haskell.org/ghc/ :? for help [1 of 2] Compiling Main ( /tmp/foo.hs, interpreted ) /tmp/foo.hs:15:11: error: [GHC-39999] • Ambiguous type variable ‘a0’ arising when matching required constraints in a group involving ‘oct’ prevents the constraint ‘(Bounded a0)’ from being solved. Probable fix: use a type annotation to specify what ‘a0’ should be. Potentially matching instances: instance Bounded Ordering -- Defined in ‘GHC.Internal.Enum’ instance Bounded PitchClass -- Defined at /tmp/foo.hs:5:30 ...plus 20 others ...plus three instances involving out-of-scope types (use -fprint-potential-instances to see them all) • In an equation for ‘add_pc_abs’: add_pc_abs steps (Pitch octave pc) = Pitch (octave + oct) pc2 where (oct, pc2) = toEnumBounded $ fromEnum pc + steps | 15 | where (oct, pc2) = toEnumBounded $ fromEnum pc + steps | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Failed, unloaded all modules. ---- GHCi, version 9.14.1: https://www.haskell.org/ghc/ :? for help [1 of 2] Compiling Main ( /tmp/foo.hs, interpreted )[main] /tmp/foo.hs:15:11: error: [GHC-39999] • Ambiguous type variable ‘a0’ arising from matching required constraints in a binding group involving ‘oct’ prevents the constraint ‘(Bounded a0)’ from being solved. Probable fix: use a type annotation to specify what ‘a0’ should be. Potentially matching instances: instance Bounded Ordering -- Defined in ‘GHC.Internal.Enum’ instance Bounded PitchClass -- Defined at /tmp/foo.hs:5:30 ...plus 20 others ...plus three instances involving out-of-scope types (use -fprint-potential-instances to see them all) • In an equation for ‘add_pc_abs’: add_pc_abs steps (Pitch octave pc) = Pitch (octave + oct) pc2 where (oct, pc2) = toEnumBounded $ fromEnum pc + steps | 15 | where (oct, pc2) = toEnumBounded $ fromEnum pc + steps | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Failed, unloaded all modules. ---- GHCi, version 10.0.0.20260427: https://www.haskell.org/ghc/ :? for help Using default language edition: GHC2024 [1 of 2] Compiling Main ( /tmp/foo.hs, interpreted )[main] Ok, one module loaded.