Comments (3)
I think the VM idea is really good. One thing that may be worth exploring is using the C++ backend for this, which already has a mature community and FFI ecosystem. Another nice thing about that project is that the codegen is human readable, so you can debug pretty easily.
from purescript-python.
creating functions that take multiple arguments to get rid of currying when possible
This is basically impossible because we only get ASTs of curried functions from purescript corefn.
The erlang backend does arity optimizations:
https://github.com/purerl/purerl/blob/master/src/Language/PureScript/Erl/CodeGen.hs#L217
but it "cheats" by using the externs files:
https://github.com/purerl/purerl/blob/1cb8f0412da684dcc1720a0e46907ea04a6bfa55/app/Build.hs#L119
These aren't part of the official compiler API and can break compatibility at any time.
from purescript-python.
First, purescript compiler is already able to transform tail call into loops.
However, it does not work for deeply stacked do-notations.
I'm not sure if purescript-tailrec helps, but if you haven't checked it, please have a look.
As we can only access corefn
of purescript, we just cannot do much for this.
creating functions that take multiple arguments to get rid of currying when possible
This is basically impossible because we only get ASTs of curried functions from purescript corefn
.
In order to support functions with arbitrary stack depth, there're 2 approaches:
Transforming Generating Python Code via the Trampoline Method
Supporting tail call eliminations for generated Python code is very easy, however, the problem is, this extremely breaks our straightforward FFI.
We can simply translate following generated code
def f(x):
a = g(x)
return f(a)
to
def f(x):
a = compute_tail_call(g(x))
return tail_call(f, a)
However then writing Python FFI becomes painful as we have to explicitly annotating every function objects to be tail_call
functions:
@tailrec
def ffi_func(...):
...
And calling purescript functions from python becomes
from xxx import compute_tail_call
from Mod.pure import f
result =compute_tail_call(compute_tail_call(f(1))(2)) # equivalent to 'f 1 2' in purescript
Other than above annoying calling conventions, the performance got greatly decreased.
I feel like to reject this idea.
Stack-less Execution for Generated Code
I don't have a concrete design, but I think this is feasible and MAY avoid the performance pitfall when providing a stackless execution model.
In fact, so far we're not generating real Python code, instead, we generate something called PySExpr
, which is particularly designed for purescript-python as a program representation to support large-scale codegen. You can see some of my explanations about PySExpr at here.
This just suggests that even though we want to run code in CPython runtime, with very straightforward FFI interoperability, it's unnecessary to generate Python code directly.
In fact, directly generating Python code can be impossible in some cases, due to the size of generated code is not scalable. The parser of Python is also so weak that it crashes when the source code is large(e.g., > 5MB). If you'd know more, we could talk in this weekend.
Hence, it'll be okay for us to design a virtual machine, written in C extensions(or things like Cython), which could avoid the endless growth of stacks, instead, migrating it to the space complexity of a heap stack.
from purescript-python.
Related Issues (20)
- avoid generating pretty-printed SExpr
- use passing directory for CI build HOT 6
- CI build error
- register concerned ffi-repos and fix ci HOT 1
- Could have other/original licence HOT 4
- Implementing FFI for external packages HOT 7
- purescript-python up-to-date HOT 1
- Nix-based installer
- unicode string representation HOT 1
- A tracker for the membership of purescript-python organization
- Better debugging when lambdas are called HOT 4
- spago run does codegen HOT 2
- Troubleshoot: Execution Not Sync to Latest Code HOT 1
- spago passing arguments to 'pspy --run'
- Code is generated on "spago run" (expected that to happen on "spago build") HOT 2
- Documenting how to construct algebraic datatypes from python HOT 5
- How to make it support windows platform? HOT 2
- purescript/maybe segfaults at runtime HOT 9
- Trying to run a helloworld program, get "Backend "pspy" exited with error:2" HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from purescript-python.