Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Show HN: A Python with Hindley-Milner-like type annotations, compiling to C (github.com/alehander42)
134 points by alehander42 on June 25, 2015 | hide | past | favorite | 27 comments


Those who like this might also like my language project, Runa:

A Python-like systems programming language. This means that the design borrows as much from Python as makes sense in the context of a statically-typed, compiled language, and tries to apply the Zen of Python to everything else. The most important design goals for Runa are developer ergonomics and performance. The compiler is written in Python and targets LLVM IR; there's no run-time. More information can be found on the project website.

https://github.com/djc/runa/



A typed and fast Python with concurrency is something I've wanted for the longest time. I really hope that this will take off.


Have you tried Nim? The syntax is Python-inspired rather than Python-like, but it seems to combine a lot of the nice features of dynamic languages with the rigour of a static language.

https://github.com/nim-lang/Nim/wiki/Nim-for-Python-Programm... http://nim-lang.org/

Haven't played with it myself, as I'm still waiting for a compiler to show up in Debian, but I plan to.


Have you looked at cython? It's (optionally) typed, fast (with only a little bit of effort extra effort over writing normal python), makes the GIL optional (for a reasonable subset of cases) and, most importantly, stable and ready to use today (so no waiting).


+1 for cython. The only criticism I could make is that the real speedups come when you can avoid using PyObjects which means the easiest way to employ it is in the inner loops of numeric code where it truly is phenomenal. Complex data structures have to be boiled into C-style structs to get any significant speedup with them although there is still a base level performance boost from having everything compiled into cpython API calls.


Complex data structures have to be boiled into C-style structs to get any significant speedup

I've never really played with it, but cython does have mappings between python builtin types and STL containers, as well as letting you create C++ classes using cython syntax that you can inherit from and access in python.


Yes, that's correct, but if you are using semi-complex objects, then you have to invoke the python interpreter and lose most of the advantage of going to Cython in the first place.

Even for things like ints, floats, etc - if you want to see a significant speed up, you have to convert them into c ints and floats.


Yes, Cython is exactly that, typed python.


There have been other typed Python variants. RPython, in which PyPy was written, is sort of one. Shed Skin tried to do it entirely via type inference, which is really hard across functions.

It's clear now that you want at least enough type inference in a language that LHS types of assignments and variable creations are inferred when possible. When you have that, as in Go, Rust, and even C++ to some extent, most of the declarations are in function definitions. There, you want them for documentation purposes anyway.


MyPy is a pretty good typed Python variant. I believe the "optional type annotations" PEP used their style.

http://mypy-lang.org/


> even C++ to some extent

C++ can also infer return types, the following is a valid C++14 function definition

    auto foo() {
        auto curry = [](auto x) {
            return [=](auto y) { return x + y; };
        };
        auto add42 = curry(42);
        return add42(58);
    }
which incidentally gets compiled to this:

    0000000000000000 <foo()>:
       0:	mov    $0x64,%eax
       5:	retq


The value-add here is that it compiles to C and therefore the type enforcement is done at compile time. If you are OK with interpreted Python and runtime checking you can use https://github.com/prechelt/typecheck-decorator/


That looks cool! It's pretty different though, it's more like a "design by contract" tool. I have a similar one, more Python2 style dsl (it supports also function overloading through a kind of pattern matching) https://github.com/alehander42/py-matchete


A Python 3 based library that compiles to C?!

> "yeah, you need a shower now, doncha"

I saw that there is a requirements.txt file, but it's blank. This project amazes me, knowing it exists gives me motivation to get better

That is all...


Correct me if I'm wrong but IIRC HM is not applicable with class-based inheritance. How do you (plan to) deal with this?

BTW I really like the idea of a typed, compilable Python, keep it up!


Well yeah, but I wanted to experiment with the language type system(it started from HM, but it can go anywhere).

Classes/records are not implemented yet, but I can imagine that we have two cases:

  <object_with_known_exact_class>.<method>
then we'll just convert that to a normal function invocation in c(because we'll be able to detect the exact ancestor which defines the method and the method)

  <an_instance_of_some_class>.<method> 
if we only know that an object is an instance of a class, we can wrap the invocation in an inline switch on the type field of the object.(instances would map to C structs).


Very cool idea, I like it. But I can't really get much to compile yet -- while loops do not work, nor does range(). Anyway, quite impressive!


Yep, it's still more like a prototype, so not everything is supported(but I'll add support for all basic constructs soon, stay tuned)


Nice. I wanted to find out how well it performs (with a simple summing task) but I gave up before I had managed to loop over a range. I'll check back in a week or two ;)


It does support `for ... in range(start, end)` and `while` now. You can check out two summing examples in the examples dir too. It typically gives ~ 6-7x speedup on my machine.


Python is the new lisp.


Now if we could just get an emacs written in Python =).


Well considering the amount of courses now in python, who knows what will be asked to and done by students. Maybe it will land before guile-emacs is stable.


FWIW, the geany editor has a GeanyPy plugin that enables you to write other plugins entirely in Python.


Really impressive work!


And that generated C code is quite amusing to boot!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: