Modifying Emacs Inferior Haskell processes to enable CPP processing - haskell

If we look at the source of The random package we have a file Random.hs. Because of CPP extensions one has to invoke ghci via the following command :
ghci -cpp Random.hs
Alternatively one can do :
ghci -cpp
and then from within ghci :
Prelude GOA> :load Random
[1 of 1] Compiling System.Random ( Random.hs, interpreted )
Ok, modules loaded: System.Random.
If I use Emacs Inferior Haskell mode (Emacs/Inferior Haskell processes) and I have
the source :
module Main where
import System.Random
gen = (random (mkStdGen 0)) :: (Bool,StdGen)
mymult :: Int -> Int
mymult x = 2 * x
main = do
print $ mymult 5
then upon typing the emacs command :
C-c C-l
which is inferior-haskell-load-file, ghci is opened in a subwindow in emacs. However if from within this window I type load Random.hs then I get the error message :
*Main GOA> :load Random.hs
Random.hs:1:2: lexical error at character 'i'
Failed, modules loaded: none.
How can I load Random.hs taking into account cpp extensions? Or alternatively how do I modify haskell-mode/inf-haskell.el such that ghci is invoked with the -cpp option upon typing C-c C-l, so that the command :load Random.hs can be executed without error?

The most reliable way is certainly to request CPP, along with other extensions, in the file header:
{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Trustworthy #-}
#endif
-----------------------------------------------------------------------------
-- |
-- Module : System.Random
-- Copyright : (c) The University of Glasgow 2001
-- License : BSD-style (see the file LICENSE in the 'random' repository)
The random package only does this in the .cabal file.
The easiest way might be to simply turn CCP on permanently in all ghci sessions, by adding
:set -XCPP
to your ~/.ghci file.

Related

How to run GHCi in command line like a regular shell command

Is there any way I can run GHCi on command line like a regular command in shell?
For example: :browse in GHCi - list all the function for specific module.
but I want to run it on shell, e.g.: ghci --browse "MyModule"
which lists all the functions for the module
I know hoogle can run it on shell, e.g: hoogle Monad
The easiest way is to pipe in the commands via standard input. In Bash this can be done nicely with a “here-string”:
$ ghci <<< ':t reverse'
GHCi, version 8.2.1: http://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from /home/sagemuej/.ghci
Loaded GHCi configuration from /home/sagemuej/.ghc/ghci.conf
Prelude> reverse :: [a] -> [a]
Prelude> Leaving GHCi.
Use verbosity 0 to avoid all the greeting stuff:
$ ghci -v0 <<< ':t reverse'
reverse :: [a] -> [a]

signal EOF of stdin when in mac OS X terminal with ctrl+D doesn't work

I am on a mac OS X EL Captain and try to figure out how to signal EOF , however ctrl + D doesn't work. I have confirmed with the terminal using stty all that eof = ^D.
With the following code in Haskell
Module Input where
import Data.List
import Data.Char
import System.IO (isEOF)
main =
interact (concat . sort . lines)
The expression will never give me any stdout - I have tried ctrl+d , but nothing happens. Why is this?
I run this program in GHCI
(Note: Question about running the program in GHCI.)
According to this TRAC issue from 9 years ago it is not considered a bug.
You can hSetBuffer stdin LineBuffering and Control-D will be recognized, but GHCI will also raise a "handle is closed" error when it comes back to the REPL prompt and the session will terminate:
$ ghci ./control-d.hs
GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Input ( control-d.hs, interpreted )
Ok, modules loaded: Input.
*Input> import System.IO
*Input System.IO> hSetBuffering stdin LineBuffering
*Input System.IO> :main
jhskdfjhdf
jhskdfjhdf<stdin>: hGetBuffering: illegal operation (handle is closed)
$

Non-exhaustive pattern matching in Haskell

I am learning Haskell and wanted to write a simple program which just repeats each letter in a string twice.
I came up with this:
repl :: String -> String
repl " " = " "
repl (x:xs) = x:x:repl xs
When compiling, I didnt get any warning but a run-time error occurred when I did repl "abcd":
"abcd*** Exception: repl.hs:(2,1)-(3,23): Non-exhaustive patterns in function repl
Why did the compiler never report this and why is it ignored in Haskell when there are many languages like OCaml which clearly report this at compile time?
The pattern match warning is turned off by default. You can turn it on with the -fwarn-incomplete-patterns or as part of a larger bundle of warnings with -W and -Wall.
You can do this from ghci:
Prelude> :set -W
You can also pass the flag to ghc when you compile or include it as a pragma on top of your module:
{-# OPTIONS_GHC -fwarn-incomplete-patterns #-}
For your specific program, it should give the following warning:
/home/tjelvis/Documents/so/incomplete-patterns.hs:2:1: Warning:
Pattern match(es) are non-exhaustive
In an equation for ‘repl’: Patterns not matched: []

latexpdf-esque functionality in haskell?

My haskell program produces .tex output using my own module.
I can't see my way around having to run 'pdflatex' everytime I want to see a result. I can hardly believe this cannot be done in a more direct manner;
Is there a way to have my code compile the .tex-file?
Is the System.Process package what you are looking for? You can use the function
system :: String -> IO ExitCode
to perform system calls.
$ touch tempfile.txt
$ ghci
GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help
>> import System.Process
>> system "ls"
tempfile.txt
ExitSuccess
Not particularly idiomatic, but it gets the job done.

Haskell prelude, How to :load automatically after :edit?

In Prelude,
> :load foo.hs
> -- before edit
> :edit foo.hs
... Edit and save file
> -- But before edit
> :load foo.hs
> -- Then after edit
How can I load automatically after edit and save it?
Or is this bad thinking?
Upgrade to GHC 7.4.2, where reload automatic is after edit (Trac #5343).
BTW: you can reload current file with :r.

Resources