sebastiaanvisser / clay

A CSS preprocessor as embedded Haskell.
Other
357 stars 72 forks source link

Ambiguous type variable #241

Closed joshua-obritsch closed 2 years ago

joshua-obritsch commented 2 years ago

Calling the following code in the repl:

>>> import Clay
>>> background inherit

Results in the following error message:

<interactive>:2:1: error:
    • Ambiguous type variable ‘a0’ arising from a use of ‘background’
      prevents the constraint ‘(Background a0)’ from being solved.
      Probable fix: use a type annotation to specify what ‘a0’ should be.
      These potential instances exist:
        instance Background BackgroundAttachment
          -- Defined in ‘Clay.Background’
        instance Background BackgroundClip -- Defined in ‘Clay.Background’
        instance Background BackgroundImage -- Defined in ‘Clay.Background’
        ...plus 7 others
        (use -fprint-potential-instances to see them all)
    • In the expression: background inherit
      In an equation for ‘it’: it = background inherit

<interactive>:2:12: error:
    • Ambiguous type variable ‘a0’ arising from a use of ‘inherit’
      prevents the constraint ‘(Inherit a0)’ from being solved.
      Probable fix: use a type annotation to specify what ‘a0’ should be.
      These potential instances exist:
        instance Inherit BackgroundAttachment
          -- Defined in ‘Clay.Background’
        instance Inherit BackgroundClip -- Defined in ‘Clay.Background’
        instance Inherit BackgroundImage -- Defined in ‘Clay.Background’
        ...plus 7 others
        (use -fprint-potential-instances to see them all)
    • In the first argument of ‘background’, namely ‘inherit’
      In the expression: background inherit
      In an equation for ‘it’: it = background inherit

If I annotate the type as suggested like this: background (inherit :: Color), then I get the expected output.

I'm wondering if there is a language extension that would help Haskell infer a/any type or a way of defaulting so that these explicit type annotations would become unnecessary.

ddssff commented 2 years ago

Haskell generally avoids this type of disambiguation, which in my opinion is a good thing. Compilers need to stay out of the business of resolving ambiguity. I just specify the type as you have here.

turion commented 2 years ago

Yes. Although the error message is quite verbose you can interpret it as "The code could potentially do different things depending on what the type is." So giving it the exact type is the right way to go. I'll close, please reopen if you have further questions :)