Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 459
Sarah Tattersall Avatar asked Nov 08 '12 23:11

Sarah Tattersall


People also ask

How do I load a Haskell file into ghci?

Open a command window and navigate to the directory where you want to keep your Haskell source files. Run Haskell by typing ghci or ghci MyFile. hs. (The "i" in "GHCi" stands for "interactive", as opposed to compiling and producing an executable file.)

How do I force quit ghci?

Quits GHCi. You can also quit by typing control-D at the prompt. Attempts to reload the current target set (see :load ) if any of the modules in the set, or any dependent module, has changed.

What does ghci mean in Haskell?

Introduction. GHCi is GHC's interactive environment, in which Haskell expressions can be interactively evaluated and programs can be interpreted.

How do I run ghci in terminal?

GHCi is the interactive interface to GHC. From the command line, enter "ghci" (or "ghci -W") followed by an optional filename to load. Note: We recommend using "ghci -W", which tells GHC to output useful warning messages in more situations. These warnings help to avoid common programming errors.


1 Answers

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)
like image 128
AndrewC Avatar answered Nov 02 '22 23:11

AndrewC