Is there a quick-starting Haskell interpreter suitable for scripting?

前端 未结 5 1276
生来不讨喜
生来不讨喜 2021-02-01 06:49

Does anyone know of a quick-starting Haskell interpreter that would be suitable for use in writing shell scripts? Running \'hello world\' using Hugs took 400ms on my old laptop

5条回答
  •  臣服心动
    2021-02-01 07:27

    What about having a ghci daemon and a feeder script that takes the script path and location, communicates with the already running ghci process to load and execute the script in the proper directory and pipes the output back to the feeder script for stdout?

    Unfortunately, I have no idea how to write something like this, but it seems like it could be really fast judging by the speed of :l in ghci. As it seems most of the cost in runhaskell is in starting up ghci, not parsing and running the script.

    Edit: After some playing around, I found the Hint package (a wrapper around the GHC API) to be of perfect use here. The following code will load the passed in module name (here assumed to be in the same directory) and will execute the main function. Now 'all' that's left is to make it a daemon, and have it accept scripts on a pipe or socket.

    import Language.Haskell.Interpreter
    import Control.Monad
    
    run = runInterpreter . test
    
    test :: String -> Interpreter ()
    test mname = 
      do
        loadModules [mname ++ ".hs"]
        setTopLevelModules [mname]
        res <- interpret "main" (as :: IO())
        liftIO res
    

    Edit2: As far as stdout/err/in go, using this specific GHC trick It looks like it would be possible to redirect the std's of the client program into the feeder program, then into some named pipes (perhaps) that the daemon is connected to at all times, and then have the daemon's stdout back to another named pipe that the feeder program is listening to. Pseudo-example:

    grep ... | feeder my_script.hs | xargs ...
                |   ^---------------- <
                V                      |
             named pipe -> daemon -> named pipe
    

    Here the feeder would be a small compiled harness program to just redirect the std's into and then back out of the daemon and give the name and location of the script to the daemon.

提交回复
热议问题