Does Haskell's ghci support running a script and then quit, like python or perl? - haskell

Python or Perl supports:
python xxx.py
perl xxx.pl
powershell xxx.ps1
Execute the script and quit with an exit code. When I tried GHCi (as ghci xxx.hs) it seems to load the xxx.hs file and enter interactive mode, without quiting.
Does the GHC interpreter support such operations?
Does this "interpreting" require to have a main function like the GHC compiler does?

Use runghc to get the same behavior. You will indeed need a main :: IO () function still.

Related

How do I proc out with tilde expansion AND $PATH searching in Haskell?

I'm trying to run the elm-reactor project, which is written in Haskell. It fails because it's trying to proc out to the elm command like this:
createProcess (proc "elm" $ args fileName)
My elm executable is sitting in ~/.cabal/bin, which is in my PATH.
The System.Process.proc command searches the $PATH for its command argument, but it doesn't do tilde (~) expansion, so it doesn't find elm.
System.Process.shell has the opposite problem. It does tilde expansion, but it doesn't search the $PATH, apparently.
From the source of the System.Process command, it looks like most everything rests on a foreign ccall to "runInteractiveProcess", which I assume is doing whatever $PATH searching is being done. I don't know where the source for runInteractiveProcess would be, and my C is about 15 years worth of rusty.
I can work around this issue by
a) adding the fully-expanded cabal/bin path to my PATH or
b) symlinking an elm from the working directory to its location in cabal/bin.
However, I'd like to offer a suggested fix to the elm project, to save future adopters the trouble I've gone through. Is there a System.Process call that they should be making here that I haven't tried? Or is there a different method they should be using? I suppose at worst they could getEnv for the PATH and HOME, and implement their own file search using that before calling proc - but that breaks cross-platform compatibility. Any other suggestions?
Try using shell instead of proc, i.e.:
createProcess (shell "elm")
This should invoke elm via a shell, which hopefully will interpret tildes in $PATH as desired.
Update: Here is the experiment I performed to test what shell does...
Compile the following program (I called it run-foofoo):
import System.Process
main = do
(,,_,h) <- createProcess $ shell "foofoo"
ec <- waitForProcess h
print ec
Create a new directory ~/new-bin and place the following perl script there as the file foofoo:
#!/usr/bin/perl
print "Got here and PATH is $ENV{PATH}\n";
Run: chmod a+rx ~/new-bin/foofoo
Test with:
PATH="/bin:/usr/bin:/sbin" ./run-foofoo # should fail
PATH="$HOME/new-bin:/bin:/usr/bin:/sbin" ./run-foofoo # should succeed
PATH="~/new-bin:/bin:/usr/bin:/sbin" ./run-foofoo # ???
On my OSX system, the third test reports:
Got here and PATH is ~/new-bin:/bin:/usr/bin:/sbin
ExitSuccess

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.

Redirecting Haskell GHCi output to text file [duplicate]

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

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.

Executing a system command in Haskell

How could I execute a system command such as cp somefile somedestination in Haskell?
Something like an os.Exec.
The Haskell 98 standard provides:
System.system :: String
-> IO GHC.IO.Exception.ExitCode
which executes a command.
The new System.Process library is more useful though, allowing for portable input/output redirection and so forth.
import System.Process
main = callCommand "cp somefile somedestination"
just works, but you may prefer other functions from the System.Process module.
I'm not a haskell buff, but this may be what you're looking for
If you do this sort of thing a lot then it's worth having a look at http://hackage.haskell.org/package/HSH.

Resources