invoking a Haskell script without a .hs extension

I have a Haskell script called "notify", without a .hs extension, which causes some problems. (I'm using ghc 6.8.3.) First attempt: runhaskell notify Without the .hs extension, ghc doesn't know it's a Haskell script, and so I get "Could not find module `notify'". Maybe runhaskell should automatically add "-x hs" to the ghc command? Second attempt: runhaskell -x hs notify This get me "Not in scope: `main'". runhaskell is invoking ghc with these arguments: -ignore-dot-ghci -x -e ':set prog "hs"' -e ':main ["notify"]' hs So it looks like runhaskell it treating "-x" as an argument to be relayed to ghc, "hs" as the name of the script, and "notify" as an argument to the script. I guess I need to use "--" to make it clear where the ghc arguments end and where the script and its arguments begin. Third attempt: runhaskell -- -x hs -- notify This gets me "Not in scope: `main'" again. runhaskell is invoking ghc with these arguments: -ignore-dot-ghci -x -e ':set prog "hs"' -e ':main ["--","notify"]' hs This looks like a bug in the "--" handling, unless I'm misinterpreting the usage message I get from running plain "runhaskell". Fourth attempt: ghc -ignore-dot-ghci -e ':set prog "notify"' \ -e ':main []' -x hs notify This works, but passing arguments becomes rather cumbersome. If there's a way to get runhaskell to pass "-x hs" in the right place, that would be much better. A somewhat related issue: I'd like to avoid hard-coding the path to runhaskell or ghc in the #! line. Instead, I'd like to use #!/bin/sh, and have the shell find runhaskell or ghc in $PATH. This means the first few lines of the script would have to be executed by sh, but ignored by ghc. Most scripting langauges have some way of doing this, although it's often by accident. The best way I've seen is in Guile, where "#!" starts a multi-line comment, and "!#" ends it. For Haskell, this is the best I could come up with: #!/bin/sh {- > /dev/null 2>&1 exec ghc -ignore-dot-ghci \ -e ":set prog \"$0\"" \ -e ':main []' -x hs "$0" -} But this depends on "{-" not existing as an executable command, or at least not doing anything harmful when invoked like this. I'd like to avoid depending on anything like that. Does anyone have any better ideas? paul

On Sun, Aug 17, 2008 at 8:45 PM, Paul Jarc
I have a Haskell script called "notify", without a .hs extension, which causes some problems. (I'm using ghc 6.8.3.)
First attempt: runhaskell notify Without the .hs extension, ghc doesn't know it's a Haskell script, and so I get "Could not find module `notify'". Maybe runhaskell should automatically add "-x hs" to the ghc command?
Second attempt: runhaskell -x hs notify This get me "Not in scope: `main'". runhaskell is invoking ghc with these arguments: -ignore-dot-ghci -x -e ':set prog "hs"' -e ':main ["notify"]' hs So it looks like runhaskell it treating "-x" as an argument to be relayed to ghc, "hs" as the name of the script, and "notify" as an argument to the script. I guess I need to use "--" to make it clear where the ghc arguments end and where the script and its arguments begin.
Third attempt: runhaskell -- -x hs -- notify This gets me "Not in scope: `main'" again. runhaskell is invoking ghc with these arguments: -ignore-dot-ghci -x -e ':set prog "hs"' -e ':main ["--","notify"]' hs This looks like a bug in the "--" handling, unless I'm misinterpreting the usage message I get from running plain "runhaskell".
Fourth attempt: ghc -ignore-dot-ghci -e ':set prog "notify"' \ -e ':main []' -x hs notify This works, but passing arguments becomes rather cumbersome. If there's a way to get runhaskell to pass "-x hs" in the right place, that would be much better.
A somewhat related issue: I'd like to avoid hard-coding the path to runhaskell or ghc in the #! line. Instead, I'd like to use #!/bin/sh, and have the shell find runhaskell or ghc in $PATH.
I'll let someone more knowledgeable address the other issues, but as for the argument to #!, I believe you could/should use "#!/usr/bin/env runhaskell".
This means the first few lines of the script would have to be executed by sh, but ignored by ghc. Most scripting langauges have some way of doing this, although it's often by accident. The best way I've seen is in Guile, where "#!" starts a multi-line comment, and "!#" ends it. For Haskell, this is the best I could come up with:
#!/bin/sh {- > /dev/null 2>&1 exec ghc -ignore-dot-ghci \ -e ":set prog \"$0\"" \ -e ':main []' -x hs "$0" -}
But this depends on "{-" not existing as an executable command, or at least not doing anything harmful when invoked like this. I'd like to avoid depending on anything like that. Does anyone have any better ideas?
paul _______________________________________________ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-users

"Philip Weaver"
I'll let someone more knowledgeable address the other issues, but as for the argument to #!, I believe you could/should use "#!/usr/bin/env runhaskell".
That would work if runhaskell automatically supplied -x hs (passing multiple arguments on the #! line doesn't work portably), and if no other arguments to runhaskell are needed, and if there's no need/want to do anything else in sh before re-executing with runhaskell. Using #!/bin/sh gives more flexibility, so I'd like to find a better way to do that. paul

Paul Jarc wrote:
I have a Haskell script called "notify", without a .hs extension, which causes some problems. (I'm using ghc 6.8.3.)
First attempt: runhaskell notify Without the .hs extension, ghc doesn't know it's a Haskell script, and so I get "Could not find module `notify'". Maybe runhaskell should automatically add "-x hs" to the ghc command?
The 6.9 branch version of runghc does that, actually. (runhaskell is probably a symlink to runghc.)
Second attempt: runhaskell -x hs notify
There's an undocumented runghc flag which I found by looking at the source code, --ghc-arg, that can be used for this. runghc -x --ghc-arg=hs notify should work. runghc's handling of "--" is a little odd; it does stop parsing ghc args when "--" is encountered, but it also stops at the first option that doesn't start with a hyphen, even if a "--" follows later. HTH, Bertram

Bertram Felgenhauer
Paul Jarc wrote:
Maybe runhaskell should automatically add "-x hs" to the ghc command?
The 6.9 branch version of runghc does that, actually.
Ah, that's good news.
There's an undocumented runghc flag which I found by looking at the source code, --ghc-arg, that can be used for this.
runghc -x --ghc-arg=hs notify
That works, thanks! paul

On 2008 Aug 17, at 23:45, Paul Jarc wrote:
A somewhat related issue: I'd like to avoid hard-coding the path to runhaskell or ghc in the #! line. Instead, I'd like to use #!/bin/sh, and have the shell find runhaskell or ghc in $PATH. This means the
#! /usr/bin/env runhaskell ... -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH
participants (4)
-
Bertram Felgenhauer
-
Brandon S. Allbery KF8NH
-
Philip Weaver
-
prj@po.cwru.edu