27 ms·
Berp: Python3 - Haskell compiler; Produces self contained binaries
- johkra 16y agoVery interesting - I see myself using it. My test case: def sumTo(n): def sumTCO(num, sum): if num == 0: return sum return sumTCO(num-1, sum + num) return sumTCO(n, 0) print(sumTo(10000000)) produces the following Haskell code (tail call optimization works!) module Main where import Berp.Base import qualified Prelude main = runStmt init init = do _s_sumTo <- var "sumTo" def _s_sumTo 1 none (\ [_s_n] -> do _s_sumTCO <- var "sumTCO" def _s_sumTCO 2 none (\ [_s_num, _s_sum] -> do ifThen (do _t_0 <- read _s_num _t_0 == 0) (do _t_1 <- read _s_sum ret _t_1) _t_2 <- read _s_sumTCO _t_3 <- read _s_num _t_4 <- _t_3 - 1 _t_5 <- read _s_sum _t_6 <- read _s_num _t_7 <- _t_5 + _t_6 tailCall _t_2 [_t_4, _t_7]) _t_8 <- read _s_sumTCO _t_9 <- read _s_n tailCall _t_8 [_t_9, 0]) _t_10 <- read _s_print _t_11 <- read _s_sumTo _t_12 <- _t_11 @@ [10000000] _t_10 @@ [_t_12] Which in turn produces a ~830KB binary after stripping (1.5MB before) which is linked only to gmp and the usual linux binaries, i.e. essentially static. Memory usage is also pretty low. (0.2% with 1GB RAM) Thats's the good side. The bad side: It's slow. Runtime is about 10s whereas the same code in C returns instantly and measures less than 4KB. The ugly: It's slower than Python 2.6 which "only" takes ~6 seconds for the iterative version using range. If converted to a while loop with manual incrementing (berp doesn't support range yet), Python takes ~8 seconds while berp still takes ~10s. I understand that it's only version 0.0x, but I'm a pretty disappointed by the speed for numerical operations - I hoped (Python compiled to) Haskell would be within an order of magniture to C. TCO on the other hand is very cool - and there's no stack limit, which is great for some algorithms easier expressed recursively.