

Show HN: A Python with Hindley-Milner-like type annotations, compiling to C - alehander42
https://github.com/alehander42/hermetic

======
dochtman
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/](https://github.com/djc/runa/)

------
keenerd
Packaged: [https://aur4.archlinux.org/packages/hermetic-
git/](https://aur4.archlinux.org/packages/hermetic-git/)

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

~~~
dagw
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).

~~~
kevin_thibedeau
+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.

~~~
dagw
_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.

~~~
Fede_V
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.

------
Animats
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.

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

[http://mypy-lang.org/](http://mypy-lang.org/)

------
bkeroack
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/](https://github.com/prechelt/typecheck-decorator/)

~~~
alehander42
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](https://github.com/alehander42/py-matchete)

------
jjangsangy
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...

------
evacchi
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!

~~~
alehander42
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).

------
lorenzhs
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!

~~~
alehander42
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)

~~~
lorenzhs
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 ;)

~~~
alehander42
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.

------
agumonkey
Python is the new lisp.

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

~~~
agumonkey
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.

------
LukeHoersten
Really impressive work!

~~~
jaytaylor
And that generated C code is quite amusing to boot!

