Search code examples
haskellwarningsghci

Warnings on load from GHCi prompt


When using GHCi I'd like to know how can I use the -Wall option when (re)loading from the prompt.

For example in section 3.3 of Haskell Programming Tips The example shown with guards is as follows:

-- Bad implementation:
fac :: Integer -> Integer
fac n | n == 0 = 1
      | n /= 0 = n * fac (n-1)

-- Slightly improved implementation:
fac :: Integer -> Integer
fac n | n == 0    = 1
      | otherwise = n * fac (n-1)

It says "The first problem is that it's nearly impossible for the compiler to check whether guards like this are exhaustive, as the guard conditions may be arbitrarily complex (GHC will warn you if you use the -Wall option)."

I know I can type ghci -Wall some_file.hs from the command line but once in the prompt I'm unsure how to check for warnings if I wish to reload.

I can't seem to find the answer after trying to Google this!

Thanks in advance!


Solution

  • In ghci, enter

    :set -Wall
    

    and if you want to turn all warnings off, you can do

    :set -w
    

    (Note the lowercase w. Uppercase would be turn normal warnings on.)

    In the user guide it says we can use any ghc command line options at the command prompt as long as they're listed as dynamic, and we can see from the flag reference that all the warnings settings are dynamic.

    Here's an example session, using the "bad implementation" above:

    Prelude> :l temp.hs
    [1 of 1] Compiling Main             ( temp.hs, interpreted )
    Ok, modules loaded: Main.
    (0.11 secs, 6443184 bytes)
    
    *Main> :set -Wall
    
    *Main> :l temp.hs
    [1 of 1] Compiling Main             ( temp.hs, interpreted )
    
    temp.hs:3:1:
        Warning: Pattern match(es) are non-exhaustive
                 In an equation for `fac': Patterns not matched: _
    
    Ok, modules loaded: Main.
    (0.14 secs, 6442800 bytes)