Page 53 - MSDN Magazine, June 2017
P. 53

Once such a lambda object has been created, the eval Active Event in P5 sequentially executes the root node’s children nodes, invoking these as Active Events, from top to bottom. The lambda object created in the previous example contains four nodes, two of which are root nodes. Hence, eval will execute the “if ” before it executes the “else.”
Therefore, a lambda object becomes the “DOM structure” Hyperlambda creates. If I wanted to, I could’ve used XML instead of Hyperlambda to describe this tree structure. It just so happens that Hyperlambda is the superior method for describing such tree structures. It often helps to think of Hyperlambda as HTML or XML, and lambda objects as the resulting DOM structure.
Hyperlambda has an extremely simple syntax. All nodes can have a name, a value and children nodes. Here are all its major control structures:
• A colon separates names and values of nodes.
• Two spaces opens up the children collection of nodes.
A node’s value can optionally have a type declaration, squeezed in between the name and its value. The type declaration is optional, though, and rarely used. String is the default type declaration
in Hyperlambda.
What Makes Hyperlambda Unique
A static language, such as C++ or C#, compiles its code down to some machine-executable thing up front, before it starts executing the result. A dynamic language, such as JavaScript, normally inter- prets the code on the fly, as it’s executing the code. Hyperlambda, in contrast, does neither. Instead, you declare an execution tree, which is parsed by P5, producing lambda objects that are then evaluated using the eval Active Event in P5. The lambda objects you create don’t even consider syntax, and you can use any file format capable of declaring relational tree structures to declare your lambda objects, which means that Hyperlambda ends up being no more of a programming language than HTML or XML are. However, because it’s still a Turing-complete execution envi- ronment, it allows you to do everything you can do with any other programming language. Arguably, I could’ve used HTML to declare my lambda objects, at which point HTML would’ve become a Turing-complete programming language.
Branching Your Tree
Every keyword in Hyperlambda is simply an Active Event. Invoking these Active Events often results in recursively invoking eval, according to some sort of condition. This makes Hyperlambda extremely extendible. Creating a new keyword literally means cre- ating five lines of code. Here’s an example in C#:
[ActiveEvent (Name = "what-is-the-meaning-of-life?")]
private static void foo (ApplicationContext context, ActiveEventArgs e) {
e.Args.Value = 42; }
In fact, both the if and else keywords are created as Active Events like this.
You don’t have to use C# to create Active Events or keywords. You can also create your own keywords in Hyperlambda:
create-event:what-is-the-meaning-of-life? return:int:42
The observant reader might realize at this point that although Hyperlambda is definitely a very high-level abstraction, at some philosophical level it’s also a very low-level abstraction—arguably far lower than any other existing programming language, because what you’re modifying isn’t code but the actual execution tree. This characteristic of Hyperlambda has huge benefits, the same way the DOM model has huge benefits for HTML and XML. First of all, it enables extremely flexible and self-modifiable code. This means that the separation between what is normally thought of as creating code and executing code becomes much thinner—allowing for code that does a little bit of both. Evaluating a lambda object with some input data might easily create new logic and additional branches in your execution tree. For example, if you don’t like a particular if/else block for some reason, you can dynamically remove it, even after you’ve started evaluating the lambda object containing it. In fact, your execution tree might look completely different, depending on the input data you supply to your lambda objects.
Among Hyperlambda’s other odd traits is the fact that it has no variables.
Just as you can use the DOM model to modify parts of your HTML page in JavaScript, you can use the “lambda object model” to modify parts of your lambda objects during execution. Literally, your execu- tion tree becomes a dynamic entity that can change during execution.
Transforming back and forth between the Hyperlambda file format, which is serializable and persistent in nature, and lambda objects, which are execution trees, is as easy as invoking an Active Event.
Finding Your Branch
Among Hyperlambda’s other odd traits is the fact that it has no variables. I don’t mean no variables in the F# sense, where every- thing is immutable. I literally mean Hyperlambda has no variables! At this point, of course, you’re skeptical. After all, being able to change the state of your execution tree and memory is key to cre- ating a Turing-complete execution environment. However, if you can change the state of your execution tree, you actually don’t need what we traditionally refer to as variables.
In Hyperlambda, everything can change. Changing anything in Hyperlambda simply implies referencing the nodes you wish to change and supplying a source for your change operation. So, the first thing that’s needed is a way to reference nodes in your tree, and this is done with lambda expressions, like so:
_data:Thomas if:x:/@_data?value
=:Thomas
say-hello:Yo boss else
say-hello:Yo stranger
This code shows one lambda expression, which refers to the _data node’s value and is supplied as an argument to the if Active Event invocation. This if invocation compares the result of the lambda expression to the static value “Thomas,” and if the value of “_data”
msdnmagazine.com
June 2017 49




































































   51   52   53   54   55