playing with berp: python3 to haskell compiler in haskell

Alia Khouri alia_khouri at yahoo.com
Sun Oct 31 09:16:03 CET 2010


In case anyone finds this worthwhile: there is a pretty impressive
python3 to haskell compiler written in haskell called berp that looks
very interesting: http://github.com/bjpop/berp/wiki

I highly recommend reading through the berp implementation code for a
fascinating (but currently incomplete) attempt at a purely functional
implementation of python3. The current version doesn't do floats and
imports and other things but includes callcc continuations and tail-
call optimization, and it is likely these things will be added
relatively quickly as the code is extremely well-structured.

Even the generated code (minus the underscore) is surprisingly
readable:

<python>
class Person:
    def __init__(self, name):
        self.name = name

    def say_hello(self):
        print('hello, my name is '+self.name)

def main():
    p = Person('sam')
    p.say_hello()

main()
</python>

to

<haskell>
module Main where
import Berp.Base
import qualified Prelude
main = runStmt init
init
  = do _s_Person <- var "Person"
       _s_main <- var "main"
       klass "Person" _s_Person []
         (do _s___init__ <- var "__init__"
             _s_say_hello <- var "say_hello"
             def _s___init__ 2 none
               (\ [_s_self, _s_name] ->
                  do _t_0 <- read _s_name
                     _t_1 <- read _s_self
                     setattr _t_1 (621807930, "_s_name") _t_0)
             def _s_say_hello 1 none
               (\ [_s_self] ->
                  do _t_2 <- read _s_print
                     _t_3 <- read _s_self
                     _t_4 <- _t_3 . (621807930, "_s_name")
                     _t_5 <- string "hello, my name is " + _t_4
                     _t_2 @@ [_t_5])
             pure
               [((-277916325, "_s___init__"), _s___init__),
                ((175397596, "_s_say_hello"), _s_say_hello)])
       def _s_main 0 none
         (\ [] ->
            do _s_p <- var "p"
               _t_6 <- read _s_Person
               _t_7 <- _t_6 @@ [string "sam"]
               _s_p =: _t_7
               _t_8 <- read _s_p
               _t_9 <- _t_8 . (175397596, "_s_say_hello")
               _t_9 @@ [])
       _t_10 <- read _s_main
       _t_10 @@ []

</haskell>



Also see: others ways to interface to haskell code (from ctypes for
example, see my entry in http://wiki.python.org/moin/PythonVsHaskell)

AK



More information about the Python-list mailing list