ohua-dev / ohua-core

Core Haskell library for the compiler
https://ohua-dev.github.io
Eclipse Public License 1.0
5 stars 0 forks source link

Get lambda lifting right #34

Open JustusAdam opened 5 years ago

JustusAdam commented 5 years ago

Lambda lifting is kind of a mess right now.

I just had to change it so that it doesn't recognize function references as literals, because for unitFn it is important that the reference stays as as a direct argument to the function, due to how code generation works in the backend. The same is actually true for nth, where the two indices have to remain literals. I'd prefer not having to teach the lambda lifting pass which functions need literal inputs and which don't. I don't yet know how this can be improved.

It could also mean that the current representation of the execution graph is insufficient or at least not well suited for nth and the like.

The workaround I am employing for the function references right now might not be available always, but for now its the easiest way.

I'm simply putting this issue out there so that we don't forget that it exists. For now this is low priority.

sertel commented 4 years ago

I think stating it to be a mess without even raising a concern, analyzing the problem properly and suggesting a solution is not really appropriate. You should keep those comments to yourself and rather remain constructive.

That being said, I would like to give the reasoning behind the current implementation:

We find all free variables but also all `lonely´ literals to construct the argument list of the lambda abstaction to be created. That way, no (stateful) function executes before something gets applied to the lambda. This makes sense because this is what a contextified execution is all about.

The problem seems to be now that certain ohua-internal functions exist that would fall into the category of having lonely literals, i.e., the arguments to the call are literals only. In your report you mention the following functions:

A solution now could certainly be to just filter out function literals in general. This would work for as long as we do not support stateful functions as first class citizens in the algo language. To support it, we could just always say that the selection should prefer other types of literals first and only take function literals if they are the only type of literal in the argument list.

Looking at the commit history, I can see that Function Literals were introduced into the code without a proper idea of what to do with them in case of lambda lifting: see commit 8a66be136dd67cb21f4fafa77d459b0234f2a95c I can see a TODO comment there and I believe this issue is all about this.