Redirecting Haskell GHCi output to text file [duplicate] - haskell

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Outputting Haskell GHCi command results to a txt file
I am new to Haskell and I am trying to redirect test cases output results to a text file. The way it is set up now, is a AddAllTestCases.hs contains all the test cases I need to run in order to test a function I created. I run the test cases on GHCi by loading AddAllTestCases.hs and then simply typing main and hitting enter. That causes test case output results to print inside the GHCi perfectly.
Because there hundreds of test cases, I need to redirect output results to text file.
Attempt #1:
writeFile "myoutput.txt" $ show $ main
I get the following error:
No instance for (Show(IO())) arising from a use of show
Attempt #2 in CMD (trying to create an executable, then outputting executable results to text file):
ghc --make AddAllTests.hs -o testResults.exe
Which gives me the following error:
Warning: output was redirected with -o, but no output will be generated because there is no Min module
This is weird because when I am using GHCi (attempt #1) and I type in main it executes everything perfectly, which I would assume, implies that there is a main module?
I greatly appreciate any help with redirecting test case results to a text file.
Many thanks in advance!

You need a Main module (and a main action) to produce an executable. You can rename your module to Main, or you can specify the module to be considered Main on the command line,
ghc --make -main-is AddAllTests AddAllTests.hs -o testResults.exe
to produce an executable without a module named Main.
A method without compiling would be
ghc AddAllTests.hs -e "main" > testResults.txt
Another method would be to have a file in which you just list all test cases,
3 + 2 :: Rational
reverse "foobar"
:q
and run ghci with redirected in- and output
ghci < testCases > testResults.txt

Related

multiline contents of a IO handle in haskell display nothing

I have been experimenting with Haskell. I am trying to write a web crawler and I need to use external curl binary (due to some proxy settings, curl needs to have some special arguments which seem to be impossible/hard to set inside the haskell code, so i rather just pass it as a command line option. but that is another story...)
In the code at the bottom, if I change the marked line with curl instead of curl --help the output renders properly and gives:
"curl: try 'curl --help' or 'curl --manual' for more information
"
otherwise the string is empty - as the `curl --help' response is multiline.
I suspect that in haskell the buffer is cleared with every new line. (same goes for other simple shell commands like ls versus ls -l etc.)
How do I fix it?
The code:
import System.Process
import System.IO
main = do
let sp = (proc "curl --help"[]){std_out=CreatePipe} -- *** THIS LINE ***
(_,Just out_h,_,_)<- createProcess sp
out <-hGetContents out_h
print out
proc takes as a first argument the name of the executable, not a shell command. That, is when you use proc "foo bar" you are not referring to a foo executable, but to an executable named exactly foo bar, with the space in its file name.
This is a useful feature in practice, because sometimes you do have spaces in there (e.g. on Windows you might have c:\Program Files\Foo\Foo.exe). Using a shell command you would have to escape spaces in your command string. Worse, a few other characters need to be escaped as well, and it's cumbersome to check what exactly those are. proc sidesteps the issue by not using the shell at all but passing the string as it is to the OS.
For the executable arguments, proc takes a separate argument list. E.g.
proc "c:\\Program Files\\Foo\\Foo.exe" ["hello world!%$"]
Note that the arguments need no escaping as well.
If you want to pass arguments to curl you have to pass that it in the list:
sp = (proc "/usr/bin/curl" ["--help"]) {std_out=CreatePipe}
Then you will get the complete output in the entire string.

Calling Haskell script on mac?

I've installed the Haskell platform on my mac (OSX lion), and ghci is running great.
Now I've created a haskell-file, stored on my "desk." How can I call it from this directory?
Example:
Prelude> :load datei.hs
[1 of 1] Compiling Main ( datei.hs, interpreted )
datei.hs:1:7: parse error on input `\'
Failed, modules loaded: none.
datei.hs:
let fac n = if n == 0 then 1 else n * fac (n-1)
Why do I get this?
Use the OSX terminal to reach your desktop and invoke yourfile.hs using ghci:
cd ~/Desktop
ghci yourfile.hs
edit:
As stated in the comments, the error message you're seeing above is warning you that the character \ exists at an unexpected location in the source code.
Since that character does not exist in the line of code you posted, there must be more to datei.hs. We need to see the rest of your source code before we can help.
If you saved your program with TextEdit, it's very possible that you're seeing a '\' character because you're saving it as an RTF file (TextEdit's default). Hit Ctrl-shift-t to convert it into a plain text file.
If your already in ghci you can use ':cd /path/to/file' as well.
Here is a good thread discussing let.

How do you compile and run haskell on notepad++

How do you compile and run Haskell on notepad++
I installed the plugin NppExec and then I pressed F6
I saved my Haskell file to C:\Users\Sam\Desktop\haskell files\new 3.hs
So on the command after I press F6 I tried typing in a few different things:
C:\Users\Sam\Desktop\haskell files\new 3.hs`
ghc.exe new 3.hs
haskell new
but I got these responses:
C:\Users\Sam\Desktop\haskell files\new 3.hs
CreateProcess() failed with error code 2:
The system cannot find the file specified.
ghc.exe new 3.hs
Process started >>>
target `new' is not a module name or a source file
<<< Process finished.
haskell new 3
CreateProcess() failed with error code 2:
The system cannot find the file specified.
================ READY ================
What is the correct way of compiling and executing at haskell file on notepad++?
You need to set NppExec to work in the current directory, so In Plugins, NppExec, tick Follow $(CURRENT_DIRECTORY).
Use the command ghc new3.hs when you press F6 (no spaces in filenames).
If you're using Haskell with stack, I found a lovely way to run things quickly using NppExec. It's a simpler process than it looks and once you do it, you're good to go:
Suppose you have a file like this in a file called yourFileName.hs:
main :: IO ()
main = putStrLn "Hello world!"
Press F6 to begin NppExec. (See Note 1 below.)
Paste the below into the window.
cd "$(FULL_CURRENT_PATH)"
stack ghci
// This is a comment you can delete. Note 2 below.
(See Note 3 below.)
Upon pressing the OK button, the Notepad++ console will run the Haskell interpreter.
Now, press F6 again. A warning menu will pop up.
Type this into the menu: :cmd return $ unlines [":l yourFileName", ":main"] and press ENTER. The file will execute. Pressing F6+ENTER will load and run the file again. When you open Notepad++ next time, this will still be there. Whenever you want to work with a new file, you will have to change yourFileName of course.
Explanation: :cmd return " . . . " allows you to execute a string as multiple ghci commands, separated by \n. unlines takes a list of strings and joins them with \n. If you don't know about $, you'll learn it soon as it's part of basic Haskell.
If you don't have a main function in your file, then instead use :cmd return $ unlines [":l yourFileName"].
Note 1: For convenience, I used the Settings > ShortCutMapper > Plugin Commands to change Execute from F6 to F1.
Note 2: If you run multiple languages in this way (like maybe Lisp?), then you can replace the // This is a comment... line with // :cmd return $ unlines [":l yourFileName", ":main"] just so you have it for later when you switch back to Haskell.
Note 3: Instead of pasting cd "$(FULL_CURRENT_PATH)" stack ghci into the NppExec window, a much simpler way to do all this is to paste stack runghc "$(FULL_CURRENT_PATH)" and nothing else needs to be done. However I found the console takes a lot longer to load and run the file in that case, so the method above is what I use.

How do you get GHC to output compile errors to a file instead of standard output?

I'm trying to compile a haskell file that has a HUGE number of errors in it. I want to start debugging the first one but unfortunately there are so many that they go off the screen.
I want to pipe the error messages to a file so that I can read from the top, but normal methods don't seem to work.
I've tried:
ghc File.hs > errors.log
ghc File.hs >> errors.log
ghc File.hs | more
None of them work. Using > and >> only writes the first couple of lines to the file and then the rest to standard output. Using more, less, cat etc doesn't make any difference at all.
Is there a flag for GHC that will let me output to a file?
(I should probably let you know that I'm working on a Windows machine with Cygwin.)
Most programs write output to the standard output (file descriptor 1) and error messages to the standard error (file descriptor 2).
You can ask your shell to redirect the standard error to another location like this:
ghc File.hs > output.log 2> errors.log
or if you want them in the same file:
ghc File.hs > output.log 2>&1
See your shell's manpage section of redirections for full details. Note that the shells are picky about the order of the redirections.
You can also view the output directly, using the same redirect as sarnold's solution, but without the intermediate output file:
ghc File.hs 2>&1 | less
(same goes for more instead of less, etc.)

Haskell Compiling Problem using GHCi Windows

I have developed a haskell application which is tested with WinHugs interpreter working fine .. when i try to comiple the same application using WinGHCi it prompts a error
lexical error in string/character literal at character '\t'
I have used \t in IO Program to display text
Example :- putStr "\n \n \t \t Hello ! "
Any solutions ?
You may want to try this step-by-step guide:
Save your program in a file program.hs this file should contain a function main of the type IO () that is executed at the program's start.
Open a shell in the directory where this file is.
Type ghc -O3 --make program.hs to compile program.hs into an executable program.exe.
Try to run program.exe
If the error still occurs, please post some more code to aid debugging.

Resources