On a UNIX-like system with GHC installed, I can create an file with the following contents
#!/usr/bin/env runhaskell
main = putStrLn "Hello, I am a UNIX script file."
Make the file executable, and this executes the given Haskell code.
How do I do the equivalent in a Windows .bat file?
Preferably, no extra files are created, no environment variables are set, it also works if the batch file is accessed using a UNC path or a path containing spaces, the Haskell namespace is not polluted, and a reasonable behavior results if the Haskell code contains errors.
After much trial and error, this turns out to be possible. I have designed the following:
The only disadvantage of this mechanism is that error messages contain the name of a temp file created by (something called by)
runhaskell.Here is a full explanation of how this works:
set /p =-- < nuloutputs--(and two irrelevant spaces) not followed by a newline. This works like this:set /p ANSWER=Please enter answer:printsPlease enter answer:without a newline, waits for user input, and puts that in environment variableANSWER.< nulacts as if the user did not enter anything."%~f0"is the name of the current batch file.set /p =-- < nul & type "%~f0"outputs the current batch file, but with the first line commented out (when interpreted as Haskell code).runhaskell, which (undocumentedly?) interprets its stdin as non-literal (!) Haskell code.runhaskellis on the currentPATH.exitmakes sure that everything after the first line is not seen by the Windows batch file interpreter, andexit /bmakes sure that we only exit this script, not any surroundingcmd.exeshell.@makes sure that all this gibberish is notechoed when running this script.(I have not found a way to do the same for literate Haskell code; but I don’t have a need for that currently.)