Search code examples
haskellpattern-guards

Suggested ScopedTypeVariables In a pattern type-signature


I Started writing Haskell code . I tried to write a fibonacci function using Guards -

    fibo :: (Num z, Ord z) => z -> z
    fibo d
    | d <= 0 = 0
    | d == 1 = 1
    | otherwise = fibo (d-1) + fibo (d-2)

I got this error :-

Illegal type signature: ‘(Num z, Ord z) => z -> z fibo d’ Perhaps you intended to use ScopedTypeVariables In a pattern type-signature

However another function - replicate i have written in a similar way which compiled and worked fine . I can write fibonacci in another way , but I want to know what the error was


Solution

  • The indentation in your program is wrong and StackOverflow's weird treatment of tabs made the indentation in your question wrong in a different way.

    1. Your program should be indented like this:

      fibo :: (Num z, Ord z) => z -> z
      fibo d
        | d <= 0 = 0
        | ...
      

      The first two lines should start in the same column and the lines with guards should be more indented than those lines.

    2. The program as displayed in your question is wrong in a different way than the error you mentioned: the lines with guards must be more indented than the preceding lines. This happened because StackOverflow has a nonstandard treatment of tab characters. Don't use tabs.

    3. Your error is consistent with GHC viewing your program as indented like this:

      fibo :: (Num z, Ord z) => z -> z
        fibo d               -- wrong, must start in same column as previous line
        | d <= 0 = 0
        | ...
      

      We can reconstruct that your original program must have been

      <sp><sp><sp><sp>fibo :: (Num z, Ord z) => z -> z
      <tab>           fibo d
      <tab>           | d <= 0 = 0
      <tab>           | ...
      

      Don't use tabs.