Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Ravi – Lua 5.3 with optional static typing (ravilang.github.io)
48 points by johlo on Oct 17, 2015 | hide | past | favorite | 29 comments


If a type system can be easily escaped, does it really offer you much?

If it's still transformed to run in the runtime of a late-bound dynamic typed language where the types can't be used by the compiler to produce optimized code with early-bound type function/method dispatch, does it really offer you much?

I guess I don't really see optional static typing as giving the best of both worlds, as it cannot deliver on what a good static type system can give you.


Perl 6 has gradual typing that is used for optimizations at run-time. So, it's possible to use optional types for optimization (even if this particular implementation doesn't do it yet). And, it sounds like that's actually the purpose of this project from the description. It doesn't even seem like it's trying to add types as a method of avoiding errors.

And, the benefits of typed languages aren't necessarily all or nothing, and they aren't all performance-related. There are many kinds of error that can be revealed by adding type checks to a program, and just like adding more unit tests to an existing program can reveal existing bugs, adding type information could do the same (with arguably less tedium than writing basic unit tests just to confirm something is behaving correctly with a variety of input).

If I can choose to add types to my function rather than writing unit tests that test a bunch of different data inputs, I would definitely prefer to add types. (Now, in the case of some type systems, like Perl 6, you can go crazy with bounds and other things within the type system itself...so you can not only say a function receives an Int, it can also say it receives an Int in the range of -1...43, and nothing else is acceptable). Types are certainly more fun than fuzz testing everything.

None of those benefits require a program to be all-in on having typed data.


How does Perl's bound check work? If it's compile time then wouldn't you need some form of dependent types?


It gives you something. Compare Facebook Flow or Google Closure Compiler.

It gives you the flexibility of not having to prove types to the compiler sometimes, but the safety of proving it other times. And yet you can write both programs in a familiar language.

You could do early binding. Closure, for example, transforms non-polymorphic functions into static functions. But you are correct that performance is not the primary objective; it is verification.

I personally prefer static typing all the way. But it's possible to make a decent argument for optional static typing.


Julia is statically typed with the most general type being variant.

It will run as fast as native code in non dynamic scenarios.


I agree that a good static typing system gives you a lot more (especially regarding the "correctness" of programs when the type system can be escaped), but regarding your second point there's this quote from the linked page:

"Ravi extends Lua with static typing for greater performance under JIT compilation."

This project appears to be an optionally-typed superset of Lua WITH a modified runtime to support better typed JIT compilation.


Static types give me three things: Static analysis, safety and performance. With optional static types you get the analysis with some safety and maybe a little performance.

Safety is a time saver. Without static types you have to write a lot more code in the form of unit tests than amount of code it takes to put in some type annotations.

Static analysis is an even bigger time saver. Suppose I have to decipher someone else's code (or even my own). I can get a machine to definitively show me the call hierarchy of a function. That's not just all the call sites, but the complete call stack for every case. This immediately gives me a huge insight into the design (or lack of it) of the program. To do this with grep or a debugger potentially increases that time exponentially.


> If a type system can be easily escaped, does it really offer you much?

Yes, see http://julialang.org for a dynamic language with a rich type system.

In many cases, the compiler can indeed optimize and catch errors at compile-time using provided type information. And Julia's methods are a great improvement over most dynamically typed languages reliance on object orientation, IMO.

But I would be hesitant to call it "optional static typing," since, as you say, it cannot offer the guarantees of static typing.


No, it can actually offer the guarantees of static typing within statically typed functions.

    function match_books(bids::Array{Bid,1}, asks::Array{Ask,1})
      ...
    end
I have static guarantees on the type of bids, asks, and anything else inside `match_books`.

Outside, you might attempt to pass nonsense in and get a runtime error. But inside, I can rely on static guarantees and so can the compiler (this is why Julia is fast).


This is interesting, but the benchmarks suggest that LuaJIT usually does as good, if not better. So there doesn't seem to be much benefit to the static types, at least performance-wise.


Yeah, and the page itself further mentions additional benefits that LuaJIT has that Ravi doesn't, putting LuaJIT even further ahead. As an outsider to Lua, I assumed I was missing additional context (eg. limitations in applicability of LuaJIT maybe?) that necessitates the existence of Ravi.


One reason to use (potentially) Ravi over LuaJIT is that LJ has a 2GB memory limitation.


I often wonder if the whole idea of "typing" is the wrong decision in language design, if we shouldn't come up with something better. Dynamic languages are often trying to insert some form of typing. Static languages are often trying to inject tools for heterogenous collections. It's clear there are advantages to both approaches, yet a languages is usually definitely one or the other, which seems limiting. Not that I have a solution, but perhaps breaking programs into primitive types or lack thereof isn't the right approach to the abstraction.


Please check crystal [0]. It has syntax inspired from ruby, types are automatically infered, and it supports method overloading and metaprogramming using compile time macros.

Simple Crystal programs are valid Ruby scripts. I recently implemented a protobuf library in Crystal[1], and while the code for benchmark[2] is 100% valid code (no explicit typing), it runs 10 times faster due to Crystal beeing compiled instead of interpreted.

[0] http://crystal-lang.org/

[1] https://github.com/teodor-pripoae/protokol

[2] https://gist.github.com/teodor-pripoae/da785e3985a69555040e


There is another paper linked in another HN thread at the moment: https://news.ycombinator.com/item?id=10405143

It's worth a read for anyone interested in this topic. I'm not sure why all the downvotes, but it seems like typing in general is not a concept that has stabilized among languages, is the point I'm trying to make. That languages are often adding or loosening typing notions is an interesting indication to me that it is a bit fickle.

But as the article I linked here points out, a lot depends also on what your particular definition of "type" is.


> I often wonder if the whole idea of "typing" is the wrong decision in language design, if we shouldn't come up with something better.

Not sure what you mean by "better". A "type" is a proposition about the program. You can't get more general than that. Any static checking system will have an interpretation in terms of types, so it's hard to see what could possibly be better than a type system. There are just different type systems with different properties.


type-safety allows for the generating of far more efficient machine code. And you type your language everywhere, the difference is the time it happens and the consistency of the rules below handling type-conversions.

lua sets the type depending on the value assigned at run-time. There are a lot of non-dynamic lanugages who pretend to have dynamic types (var)but actually just let the compiler deduce at compile time the assigned valuetype


Well there is also a fair amount of supportive opinion on the contrary, that "better" is a system without types altogether, hence dynamic languages.


Dynamic languages have types, too. Even perl has its distinctions between lists and strings.


But most dynamic languages have types. PHP ,Ruby,Python and even JavaScript have types.


Typing becomes a tool for solving multiple problems and its uses become more vague under these terms.

In the most basic sense, types can offer the size in bytes of the data.

They can also be used to verify that certain data can be used in certain ways.

If you look at C++ you can use polymorphism if you want a uniform interface and generality elsewhere. You can use templates if you want uniform data size and interface.


Some static languages do have techniques for dealing with heterogenous lists. It is getting hard to find someone talking about a scala library these days without hearing them mention their use of hlist.


> yet a languages is usually definitely one or the other

Not necessarily true. C# has the dynamic keyword that bypasses the static typing and Java has the concept of Dynamic Proxies.


What are you referring to by "inject tools for heterogenous collections?


I take it to mean like a map that can take any type as key or value.


Lua is a great little language.

You gain the most from this, if you stay lua-native for the entire time- doing all calculations in the lua-vm, and are not depending to much on calls into the underlying c-programm (happy cache misses :( ).

Is there a optimization, that starting from the calls, creates dependancy trees, which allow to bundle away the calculation and calling of the call into seperate (pre-existing Tasks), in such a way, that the Result for simple-calls is in when the programm reaches the call?



Can projects like prosody (an xmpp server) runs on it ? Would it benefits from it ?


According to http://the-ravi-programming-language.readthedocs.org/en/late... it seems to have only type annotations for integers and numbers (which I assume to be floating-point numbers) and arrays thereof.

I guess an XMPP server will deal mostly with strings, so the benefits from type annotations will be rather small.




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

Search: