Ticket #1312 (closed bug: fixed)

Opened 6 years ago

Last modified 4 years ago

runghc doesn't respect -main-is

Reported by: simonmar Owned by: igloo
Priority: normal Milestone: 6.8.3
Component: Compiler Version: 6.6.1
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Difficulty: Easy (less than 1 hour)
Test Case: Blocked By:
Blocking: Related Tickets:

Description

runghc always invokes Main.main, it doesn't pay any attention to a -main-is flag if there is one. The right way to fix this is for runghc to invoke GHCi's :main command, but unfortunately then we'd need to pass multiple commands to ghc -e, because we also need to :set prog, for example. So probably ghc -e should split the input expression into lines and execute each line as a separate statement/command.

Change History

Changed 6 years ago by Isaac Dupree

I've seen things like

ghc -e 'let
           foo = bar
        in
          do
           baz foo
           putStrLn "ha ha"
' | some other shell command

... how about allowing multiple -e expr arguments, which are done in sequence? At present it appears that GHC just evaluates the last one of those when given multiple (e.g. try ghc -e 'putStrLn "1"' -e 'putStrLn "2"'), which doesn't make much sense.

Changed 6 years ago by Isaac Dupree

Other unix programs' example -e behavior (well, at least in the GNU versions):

#sed: run the two expressions in sequence (for each line)
echo 123 | sed -e 's/1/2/g' -e 's/2/4/g'
443
#grep: any -e argument has to match (for each line)
echo 123 | grep -e 12 -e 24
123

On the other hand, the same programs also split their expression arguments into lines and treat the separate lines as if they were separate arguments to separate -e's. Personally I'd rather not have this unix behavior :)

printf '5\n3\n12\n' | grep -e "$(printf '12\n3')"
3
12
#sed's syntax is _designed_ to have scripts where the lines are sequenced
#(unlike Haskell's expression syntax)
printf '5\n3\n12\n' | sed -e "$(printf 's/1/2/g\ns/2/4/g')"
5
3
44

Changed 6 years ago by simonmar

  • milestone changed from 6.8 branch to 6.8.3

Changed 5 years ago by igloo

:main also always runs main, regardless of any -main-is flag, currently. Also, it parses arguments itself, so we would have to build strings in such a way that it would parse them correctly. Also, ghc -e :main doesn't seem to work at the moment.

I propose changing :main's behaviour, adding :run and adding :set arg-style so that things work like this:

:main arg1 arg2        -- runs main, with args ["arg1", "arg2"]
:set -main-is foo
:main arg1 arg2        -- runs foo, with args ["arg1", "arg2"]
:run bar arg1 arg2     -- runs bar, with args ["arg1", "arg2"]
:set arg-style haskell -- opposite is :set arg-style shell
:main ["arg1", "arg2"] -- runs foo, with args ["arg1", "arg2"]

and changing ghc so that

ghc -e X -e Y

behaves more like

X
Y

in ghci. Any opinions?

Changed 5 years ago by simonmar

Strange, I thought the whole point of :main was that it ran whatever -main-is was set to. I must have been dreaming that.

I'm not sure we need set arg-style, rather I'd like to see the argument parsing (currently in Util.toArgs) be the simple:

  • double-quoted strings are parsed using read
  • everything else is split up at whitespace

It would be easy to construct the :main args: just unwords . map show.

Didn't this come up in another context recently?

Changed 5 years ago by igloo

  • owner set to igloo

Changed 5 years ago by igloo

  • status changed from new to closed
  • resolution set to fixed

OK, this is now done in 6.8 and HEAD, but you need to escape the argument to -main-is so runghc doesn't think it's the module to load:

runghs -main-is --ghc-arg=foo bar.hs

Perhaps we should have

+GHC ... -GHC

or something, but that's a matter for another ticket!

Changed 5 years ago by fons

According the runghc's help message:

runghc [-f GHC-PATH | --] [GHC-ARGS] [--] FILE ARG

Shouldn't runghc -- -main-is foo -- bar.hs suffice (without the --ghc-arg flag) ?

Changed 5 years ago by igloo

No: If we see -- then that forces the current set of arguments to end, but if we decide to end the arguments when we see foo then we don't look ahead to see if a -- is coming.

Changed 5 years ago by simonmar

  • architecture changed from Multiple to Unknown/Multiple

Changed 5 years ago by simonmar

  • os changed from Unknown to Unknown/Multiple

Changed 4 years ago by simonmar

  • difficulty changed from Easy (1 hr) to Easy (less than 1 hour)
Note: See TracTickets for help on using tickets.