36
votes

Are there any languages that target the LLVM that:

  • Are statically typed
  • Use type inference
  • Are functional (i.e. lambda expressions, closures, list primitives, list comprehensions, etc.)
  • Have first class object-oriented features (inheritance, polymorphism, mixins, etc.)
  • Have a sophisticated type system (generics, covariance and contravariance, etc.)

Scala is all of these, but only targets the JVM. F# (and to some extent C#) is most if not all of these, but only targets .NET. What similar language targets the LLVM?

3
You're asking for a lot from such a small platform. I'd be really surprised if you found something that matches all your criteria. Why do you need to use LLVM specifically?Sasha Chedygov
@musicfreak: There are plenty of languages offering all or most of the above features, which compile down to assembly.sepp2k
Languages don't target any particular processor architecture be it a physical CPU or a virtual machine written in software. Implementations of languages target particular architectures. For example Sun's implementation of Java targets Sun's JVM. There's nothing in principle to stop somebody porting Java natively to x86 or ARM or even Microsoft's CLR.JeremyP
@JeremyP: GNU GCJ compiles Java to native code, IKVM.NET compiles Java to CIL. q.e.d. :-)Jörg W Mittag
First class objects AND first class functions are a rare bread to be found together on any platform.Eli

3 Answers

43
votes

There's a Haskell (GHC) backend targeting the LLVM.

You could also try using F# through Mono-LLVM.

Also, the VMKit project is implementing both the JVM and the .NET CLI on top of LLVM; it's still in its early stages but once it matures you could use it with F#, or any JVM-targeting functional languages (Scala, Clojure, etc.)

14
votes

I'm not sure how far these have progressed, but they may be worth adding to the list:

Scala for LLVM - https://github.com/greedy/scala/
Timber for LLVM - https://bitbucket.org/capitrane/timber-llvm
Mono for LLVM - http://www.mono-project.com/Mono_LLVM

-3
votes

Yes... clang. C++ has everything on your list except for list comprehensions. It is also the flagship LLVM language.

"Are statically typed"

Yup

"Use type inference"

// local type inference
auto var = 10;

// type inference on parameters to generic functions
template <typename T>
void my_function(T arg) {
    ...
}
my_function(1) // infers that T = int

// correctly handles more complicated cases where type is partially specified.
template <typename T>
void my_function(std::vector<T> arg) {
    ...
}
std::vector<int> my_vec = {1, 2, 3, 4};
my_function(my_vec) // infers that T = int

"Are functional (i.e. lambda expressions, closures, list primitives, list comprehensions, etc.)"

Lambdas in c++ look like [capture_spec](arglist...) { body }. You can either capture closed over variables by reference (similar to lisp) like so: [&]. Alternatively you can capture by value like so: [=].

int local = 10;
auto my_closure = [&]() { return local;};
my_closure(); // returns 10.

In C++ map, zip, and reduce are called std::transform and std::accumulate.

std::vector<int> vec = {1, 2, 3, 4};
int sum = std::accumulate(vec.begin(), vec.end(), [](int x, int y) { return x + y; });

You can also rig up list comprehensions using a macro and and a wrapper around std::transform if you really want...

"Have first class object-oriented features (inheritance, polymorphism, mixins, etc.)"

Of course. C++ allows virtual dispatch + multiple inheritance + implementation inheritance. Note: mixins are just implementation inheritance. You only need a special "mixin" mechanism if your language prohibits multiple inheritance.

"Have a sophisticated type system (generics, covariance and contravariance, etc.)"

C++ templates are the most powerful generics system in any language as far as I know.