You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tinkerpop.apache.org by Marko Rodriguez <ok...@gmail.com> on 2016/06/08 14:50:35 UTC

[DISCUSS] Handling Lambdas in Gremlin Language Variants

Hello,

So in TINKERPOP-1278 there is Gremlin-Python. [https://issues.apache.org/jira/browse/TINKERPOP-1278 <https://issues.apache.org/jira/browse/TINKERPOP-1278>]. This is a full fledged version that is tested against both the ProcessComputerSuite and ProcessStandardSuite. 

How does Gremlin-Python work?

	Gremlin-Python -> Gremlin-Groovy (string) -> GremlinServer -> GroovyScriptEngine -> Python traversers

While Gremlin-Python is coded to produce a Gremlin-Groovy string, there is nothing that says we couldn’t just send the Gremlin-Python string over the wire to a Jython ScriptEngine as opposed to the Groovy ScriptEngine. What that means is that it would be possible do:

	Gremlin-Python (CPython) -> Gremlin-Jython (string) -> GremlinServer -> JythonScriptEngine -> Python traversers

With the latter model, we would need to have Gremlin-Jython in existence. If that existed, the Gremlin lambdas would be handled by Python lambdas as is — much like how Gremlin lambdas are handled by Groovy closures. Easy.

However, without Gremlin-Jython in existence, Gremlin-Python compiles to Gremlin-Groovy. That means that Python lambdas don’t have a representation in Gremlin-Groovy. Right now we simply throw an exception saying that lambda-steps are not supported, but what do people think of this "solution?

	g.V().out().map(S("{x -> x.value(’name’)}")

S would be like P and stand for “symbol.” This would compile to Groovy simply as:

	g.V().out().map({x -> x.value(’name’)})

If we didn’t have S and simply used “ “, then it would compile to:

	g.V().out().map("{x -> x.value(’name’)}")

…which is not a legal argument to MapStep.

In essence, S says “this is NOT quoted String, but a token/symbol to be inserted directly into the compilation.”

PROBLEMS:

	1. This binds the user’s traversal to the ScriptEngine that will ultimately evaluate it. (lame)
		- 99% of the time, it will most likely be Gremlin-Groovy, though, as more JVM variants come into existence…. ?
	2. Its a hack that mixes both Python and Groovy languages.

Its funny, as I was writing this email, I originally wanted to provide this hack, but now I think its a bad idea. I think we should simply wait for Gremlin-Jython to come into existence and then Python lambdas can be used as is:

	g.V().out().map(lambda x: x.values(‘name’))

But lambda is an object? How do you get the source?

>>> from dill.source import getsource
>>> y = lambda x: x.values('name')
>>> getsource(y)
"y = lambda x: x.values('name')\n"
>>>

That leads to a “final solution” (that would be hard to maintain and to write a valid compiler as lambdas can get nasty -- not just simple one liners as below).

>>> closure = getsource(y)
>>> "{" + closure[11:len(closure)-1].replace(":"," ->") + "}"
"{x -> x.values('name')}”
>>>

Nutty, eh!?

—

Given the nuttiness of the various “solutions,” perhaps we simply say, if the compilation language is not the host language, then lambdas are not supported.

Thoughts?,
Marko.

http://markorodriguez.com