playing with berp: python3 to haskell compiler in haskell

A

Alia Khouri

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top