mathics / Mathics

This repository is for archival. Please see https://github.com/Mathics3/mathics-core
https://mathics.org
Other
2.07k stars 206 forks source link

In the long term, we might need a different language #564

Open poke1024 opened 8 years ago

poke1024 commented 8 years ago

This obviously is a bit crazy, and I won't have time to even investigate this in detail, but still I want to post the idea to port Mathics to Swift (https://swift.org/) or Rust(https://www.rust-lang.org/) here for reference, since I strongly believe it would carry Mathics to a place where it's highly competitive in terms of speed (esp. compared to packages like R, GNU Octave, and so on) and Swift or Rust would be the right languages for this (as compared to C++11, Go, and other typed languages).

Even though Python allows very quick growth and development of Mathics at the current point, and it's the ideal language for feature extension, the better choice for the core engine (everything that doesn't interface with libs) would be Swift or Rust. This is crazy as it's a complete rewrite concerning also the tool chain.

On the other hand, Swift and Rust are both very Python-like, which means the code would look very similar; yet using a statically typed, compiled language would takes Mathic to a different league in terms of speed and memory consumption:

http://www.marcinkliks.pl/2015/02/22/swift-vs-others/

ghost commented 8 years ago

But there's PyPy :(

ghost commented 8 years ago

I know PyPy is slower, but... yeah

sn6uv commented 8 years ago

It's certainly a crazy idea but it's one I've had before. I'm not familiar enough with either Rust or Swift to comment on their suitability specifically.

Off the top of my head, a few approaches/techniques I've considered:

Bootstrapping

Rewrite the core of Mathics in something low-level and as much extension stuff as possible in Wolfram code.

Pros:

Cons:

We've been starting to do this already with the Autoload stuff but poor performance is limiting this at the moment.

Compilable Wolfram code

This is something that only occurred to me recently while working on #548 and builds on the 'Bootstrapping' approach above. Basically we find a subset of the Wolfram language compliable to LLVM IR and implement.

Pros:

Cons:

Embedding a Python intepreter

The idea is that we embed a python intepreter (like PyPy) within a compiled program (written something low-level) and re-use the existing Mathics codebase as much as possible.

Pros:

Cons:

Docs on embedding PyPy: http://doc.pypy.org/en/latest/embedding.html

Rewriting the core in RPython

RPython is a subset of the python language that implements PyPy. We could try porting the core to that subset and compiling it.

Pros:

Cons:

This is something I started to look into but didn't get very far. It certainly looked promising though.

Rewriting the core as a C-extension / Using Cython more

Pros:

Cons:

Rewriting the core in Lisp

Might as well embrace Greenspun's tenth rule. An implementation with TCO would fix some issues with the tree-walking evaluator.

wolfv commented 8 years ago

I think the speed of the things that are really slow and happen in SymPy will become much faster once symengine is feature complete and used as SymPy backend (https://github.com/symengine/symengine). Symengine is written in C++(11).

sn6uv commented 8 years ago

@wolfv absolutely, rewriting the mathics core in something lower-level than python might even allow us to call into symengine directly. The fact that sympy is currently implemented in python is a major hurdle for us moving to anything else right now.

sn6uv commented 7 years ago

I've got an experiment rewriting the Mathics core in C, see https://github.com/sn6uv/cmathics. I'm not convinced it's actually going to work out but I'm certainly getting a feel for how the core could be written in a statically typed language.

isuruf commented 7 years ago

@sn6uv, symengine's got all of the functionality in cmathics except for the pattern matching, but very less if you compared to SymPy. This should give a pretty good idea about what's implemented, https://github.com/symengine/symengine/blob/master/symengine/cwrapper.h SymEngine (C++) has bindings to C, Python, Julia and Ruby.

corywalker commented 7 years ago

I've been working on a Golang version of something very similar to this. My intent was not to create an alternative to Mathics. I chose not to look at the source code here because I've been enjoying the journey of understanding how everything works from the outside. There are some very fun algorithms embedded in the workings of the language. The result of my work is located here. I thought that I would update the developers here. I haven't bothered to optimize the replacement engine too much (or find concurrency in the evaluation sequence), so it has the potential to be faster. This has been one of my favorite programming projects, and I hope some others will find it useful.

ghost commented 7 years ago

By the way, there's Nim language - it's very powerful, but unpopular :( It can be compiled to C, C++, Objective C, and JS. So if you compile to C (it's by default), you can easily create bindings to something.

Also it's very performant, thanks for years of hard work of GCC and CLang developers

ghost commented 7 years ago

@TiberiumN is there a reason it's unpopular, if it's so great?

ghost commented 7 years ago

@PavelBraginskiy Because there's no a lot of sponsors.

poke1024 commented 7 years ago

I like Nim, but I'm not so sure about the stability of its toolchain and its future support at this stage. Another language that looks very promising for building compilers and interpreters is terralang (http://terralang.org/). It's great stuff.

For my part, I'm still trying to push the C++ experiment Angus started, to see where it goes. The compile times are horrible due my (excessive?) use of templates and the code is complex, and I wonder whether this is fixable. On the plus side, it can handle roughly 500 test cases now and it's fast. You can take a look at the current state of things at https://github.com/poke1024/cmathics (just look at the embedded test cases to see what works).