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

前端 未结 5 1274
生来不讨喜
生来不讨喜 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:19

    Using ghc -e is pretty much equivalent to invoking ghci. I believe that GHC's runhaskell compiles the code to a temporary executable before running it, as opposed to interpreting it like ghc -e/ghci, but I'm not 100% certain.

    $ time echo 'Hello, world!'
    Hello, world!
    
    real    0m0.021s
    user    0m0.000s
    sys     0m0.000s
    $ time ghc -e 'putStrLn "Hello, world!"'
    Hello, world!
    
    real    0m0.401s
    user    0m0.031s
    sys     0m0.015s
    $ echo 'main = putStrLn "Hello, world!"' > hw.hs
    $ time runhaskell hw.hs
    Hello, world!
    
    real    0m0.335s
    user    0m0.015s
    sys     0m0.015s
    $ time ghc --make hw
    [1 of 1] Compiling Main             ( hw.hs, hw.o )
    Linking hw ...
    
    real    0m0.855s
    user    0m0.015s
    sys     0m0.015s
    $ time ./hw
    Hello, world!
    
    real    0m0.037s
    user    0m0.015s
    sys     0m0.000s
    

    How hard is it to simply compile all your "scripts" before running them?

    Edit

    Ah, providing binaries for multiple architectures is a pain indeed. I've gone down that road before, and it's not much fun...

    Sadly, I don't think it's possible to make any Haskell compiler's startup overhead any better. The language's declarative nature means that it's necessary to read the entire program first even before trying to typecheck anything, nevermind execution, and then you either suffer the cost of strictness analysis or unnecessary laziness and thunking.

    The popular 'scripting' languages (shell, Perl, Python, etc.) and the ML-based languages require only a single pass... well okay, ML requires a static typecheck pass and Perl has this amusing 5-pass scheme (with two of them running in reverse); either way, being procedural means that the compiler/interpreter has a lot easier of a job assembling the bits of the program together.

    In short, I don't think it's possible to get much better than this. I haven't tested to see if Hugs or GHCi has a faster startup, but any difference there is still faaar away from non-Haskell languages.

提交回复
热议问题