Shen-py

183 views
Skip to first unread message

Ramil Farkhshatov

unread,
Apr 29, 2013, 2:32:58 PM4/29/13
to qil...@googlegroups.com
Hello.

I've just released Python version of Shen[1] which is a showcase of my
KLVM translator. In current state Shen-py is huge and unbearably slow.
Also keep in mind that I haven't written Python before. But it seems to
work.

Running
-------
Go to directory where `shen.py` is and type

python -m shen

If you imported shen module from Python repl you can start Shen repl via

shen.repl()

Python integration
------------------
To define a Shen function from Python use shen.defun construct:

shen.defun("plus", 2, lambda: shen.reg[1] + shen.reg[2])

where first argument is the function's name, second is the number of
arguments, and third is a function that takes zero arguments. In that
function passed arguments are accessed via shen.reg array.

To load Python file from Shen use `shenpy.load` function.

About KLVM Translator
---------------------
It translates KL to a some VM language suitable for further translation
to target languages. It takes care of
- TCO,
- lambdas,
- closures,
- exception handling,
- partial application.

So it needs just GC, vectors, function pointers and eval (till I finish
KLVM evaluator) in target language. Also, it's relatively simple to add
green threads support.

1. https://github.com/gravicappa/shen-py

Mark Tarver

unread,
Apr 29, 2013, 4:23:22 PM4/29/13
to Qilang
The tile of Hero of the Soviet Union is defunct and you're already on
the 2011 committee so all I can say is - well done!

Mark

Ramil Farkhshatov

unread,
Apr 29, 2013, 5:11:40 PM4/29/13
to qil...@googlegroups.com
Mark Tarver <dr.mt...@gmail.com> wrote:

> The tile of Hero of the Soviet Union is defunct and you're already on
> the 2011 committee so all I can say is - well done!

Thanks!
> --
> You received this message because you are subscribed to the Google Groups "Qilang" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to qilang+un...@googlegroups.com.
> To post to this group, send email to qil...@googlegroups.com.
> Visit this group at http://groups.google.com/group/qilang?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>

Ramil Farkhshatov

unread,
Apr 30, 2013, 1:27:22 AM4/30/13
to qil...@googlegroups.com, greg.s...@gmail.com
Greg Spurrier <greg.s...@gmail.com> wrote:

> Nice! Congratulations!
>
> Your KLVM translator sounds interesting. Do you have more written about it
> somewhere? I'm curious, e.g., how it handles TCO. Do you use CPS conversion
> to handle tail calls in general or convert self tail calls to loops, or
> something else?

I'll release it after some code cleanup. Documentation will be added
sometime later.

The idea is taken from Marc Feeley's GVM which is used in Gambit-C
(http://dynamo.iro.umontreal.ca/wiki/index.php/Main_Page).

Mark Tarver

unread,
Apr 30, 2013, 1:40:36 AM4/30/13
to Qilang


On 29 Apr, 19:32, Ramil Farkhshatov <ra...@gmx.co.uk> wrote:
> Hello.
>
> I've just released Python version of Shen[1] which is a showcase of my
> KLVM translator. In current state Shen-py is huge and unbearably slow.

Python had the rep of being 10X slower than CL; if this is still true
I'm not surprised that Shen-py is slow.

I did once compile Qi programs to Python - I found the whitespace
convention a bit of a pain.

Mark

Ramil Farkhshatov

unread,
Apr 30, 2013, 2:11:16 AM4/30/13
to qil...@googlegroups.com
Mark Tarver <dr.mt...@gmail.com> wrote:

> On 29 Apr, 19:32, Ramil Farkhshatov <ra...@gmx.co.uk> wrote:
> > Hello.
> >
> > I've just released Python version of Shen[1] which is a showcase of my
> > KLVM translator. In current state Shen-py is huge and unbearably slow.
>
> Python had the rep of being 10X slower than CL; if this is still true
> I'm not surprised that Shen-py is slow.

In my tests Python3.2 almost twice as slow as Python2.7.

> I did once compile Qi programs to Python - I found the whitespace
> convention a bit of a pain.

There is a loads of pain in Python. Including parser that fails on
parsing lists with mere ~30 levels of depth.

Konrad Hinsen

unread,
Apr 30, 2013, 5:56:31 AM4/30/13
to qil...@googlegroups.com
--On 30 avril 2013 10:11:16 +0400 Ramil Farkhshatov <ra...@gmx.co.uk> wrote:

>> Python had the rep of being 10X slower than CL; if this is still true
>> I'm not surprised that Shen-py is slow.
>
> In my tests Python3.2 almost twice as slow as Python2.7.

That's rather surprising. Do you have an idea why?

>> I did once compile Qi programs to Python - I found the whitespace
>> convention a bit of a pain.
>
> There is a loads of pain in Python. Including parser that fails on
> parsing lists with mere ~30 levels of depth.

Do I understand correctly that you generate Python source code? You could
generate a Python AST directly, circumventing the Python parser and the
whitespace problem. There are libraries to re-create equivalent source code
from an AST, so you don't lose much in this transition.

A nice example is hy, a Lisp syntax for Python:

http://github.com/hylang/hy

Hy compiles to a Python AST, which is then compiled to Python bytecode
using the standard Python "compile" function.

Konrad.

Ramil Farkhshatov

unread,
Apr 30, 2013, 7:19:44 AM4/30/13
to qil...@googlegroups.com
Konrad Hinsen <google...@khinsen.fastmail.net> wrote:

> --On 30 avril 2013 10:11:16 +0400 Ramil Farkhshatov <ra...@gmx.co.uk> wrote:
>
> >> Python had the rep of being 10X slower than CL; if this is still true
> >> I'm not surprised that Shen-py is slow.
> >
> > In my tests Python3.2 almost twice as slow as Python2.7.
>
> That's rather surprising. Do you have an idea why?

No, I haven't.

> >> I did once compile Qi programs to Python - I found the whitespace
> >> convention a bit of a pain.
> >
> > There is a loads of pain in Python. Including parser that fails on
> > parsing lists with mere ~30 levels of depth.
>
> Do I understand correctly that you generate Python source code? You could
> generate a Python AST directly, circumventing the Python parser and the
> whitespace problem. There are libraries to re-create equivalent source code
> from an AST, so you don't lose much in this transition.

I thought about translating KLVM directly to bytecode but decided to
defer it. My primary task was to test KLVM translation.

Mark Tarver

unread,
Apr 30, 2013, 7:21:53 AM4/30/13
to Qilang
Generating Python source code has the plus that in the event of the
Ring appearing, Ramil will be able to generate stand alone Python
programs.

Mark

On Apr 30, 10:56 am, Konrad Hinsen <googlegro...@khinsen.fastmail.net>
wrote:

Ramil Farkhshatov

unread,
Apr 30, 2013, 3:58:08 PM4/30/13
to qil...@googlegroups.com
Source code for py-kl[1] and klvm[2] are released.

KLVM is missing documentation. I am planning to fix this in the nearest
future.

1. https://github.com/gravicappa/py-kl
2. https://github.com/gravicappa/klvm

Konrad Hinsen

unread,
May 1, 2013, 1:38:57 AM5/1/13
to qil...@googlegroups.com
Mark Tarver writes:

> Generating Python source code has the plus that in the event of the
> Ring appearing, Ramil will be able to generate stand alone Python
> programs.

You can still generate stand-alone programs if you compile to AST or
directly to bytecode, because you can generate .pyc files which Python
imports even if there is no corresponding source code file. What you
lose is the possibility to read and edit the Python version of the
code.

If the main goal is interoperability with Python code, there is
another interesting approach: compile to LLVM or C from which you
generate Python extension modules. The LLVM or C code would use the
Python C API to work with Python objects. There are enormous
opportunities there to get better performance than Python, while
maintaining full interoperability. The main downside is having either
LLVM or a C compiler as a dependency if either interactive development
or run-time eval is required.

Konrad

Ramil Farkhshatov

unread,
May 1, 2013, 12:44:26 PM5/1/13
to submissio...@gmx.com, qil...@googlegroups.com
Jacob <submissio...@gmx.com> wrote:

> Great job!

Thanks.

> Have you tried doing anything with pypy to compare speeds (forgive me
> if my statement is ignorant)

I've run one benchmark (see genbench script in shen-py repository, also
bench.py is attached). Pypy has very strange results with it on my
system.

I tried to run shen.py with pypy but it seems that my netbook doesn't
have enough RAM.
bench.py

Konrad Hinsen

unread,
May 1, 2013, 3:25:10 PM5/1/13
to qil...@googlegroups.com
Ramil Farkhshatov writes:

> Running
> -------
> Go to directory where `shen.py` is and type
>
> python -m shen
>
> If you imported shen module from Python repl you can start Shen repl via
>
> shen.repl()

I wrote a script run_shen.py containing the two lines

import shen
shen.repl()

This is enormously faster (startup time only of course) than "python
-m shen" because shen.py gets compiled only once, at the first import.

I haven't yet played much with this, but it looks very interesting. Thanks!

Konrad.

Ramil Farkhshatov

unread,
May 14, 2013, 1:28:49 AM5/14/13
to qil...@googlegroups.com, artella...@googlemail.com
Artella Coding <artella...@googlemail.com> wrote:

> Hi, I tried the commands jpd suggested in
> https://groups.google.com/forum/#!msg/qilang/w1JpNPfTc08/KX87ZFS6AFcJ but
> they did not work in this new port. They also did not work in the
> ChibiScheme port and the Ruby port, but they did work in Joel's java port.
> Thanks.

Works for me after I replaced `shen-` with `shen.`:

Shen 2010, copyright (C) 2010 Mark Tarver
released under the Shen license
www.shenlanguage.org, version 10
running under python, implementation: all
port 0.0.1 ported by Ramil Farkhshatov


(0-) (shen.variant? (shen.typecheck print B) [A --> A])
true

(1-) (element? print (value shen.*signedfuncs*))
true
Reply all
Reply to author
Forward
0 new messages