TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Berp: Python3 - Haskell compiler; Produces self contained binaries

10 pointsby donsalmost 15 years ago

1 comment

johkraalmost 15 years ago
Very interesting - I see myself using it.<p>My test case:<p><pre><code> 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)) </code></pre> produces the following Haskell code (tail call optimization works!)<p><pre><code> module Main where import Berp.Base import qualified Prelude main = runStmt init init = do _s_sumTo &#60;- var "sumTo" def _s_sumTo 1 none (\ [_s_n] -&#62; do _s_sumTCO &#60;- var "sumTCO" def _s_sumTCO 2 none (\ [_s_num, _s_sum] -&#62; do ifThen (do _t_0 &#60;- read _s_num _t_0 == 0) (do _t_1 &#60;- read _s_sum ret _t_1) _t_2 &#60;- read _s_sumTCO _t_3 &#60;- read _s_num _t_4 &#60;- _t_3 - 1 _t_5 &#60;- read _s_sum _t_6 &#60;- read _s_num _t_7 &#60;- _t_5 + _t_6 tailCall _t_2 [_t_4, _t_7]) _t_8 &#60;- read _s_sumTCO _t_9 &#60;- read _s_n tailCall _t_8 [_t_9, 0]) _t_10 &#60;- read _s_print _t_11 &#60;- read _s_sumTo _t_12 &#60;- _t_11 @@ [10000000] _t_10 @@ [_t_12] </code></pre> 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)<p>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.<p>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.<p>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.