Expressions

KVM (Karma Virtual Machine) programs can be represented as data. The model for such data is called expression and found under tag _expression.


Functions

Expressions are built by composing primitive functions. There are many functions available. Most functions are pure, some have side effects. These side effects correspond to data manipulation, i.e. persistent mutations to objects stored in a karma database.


Example

To illustrate, here's a simple karma progam:

Example:

Query:
e.all(e.tag(d.string("_tag")))
Result:
[ { "model": [ "FCyAReCEenjhJqsb", "CwdniKJtBtIlEYzI" ], "tag": "_tag" }, { "model": [ "FCyAReCEenjhJqsb", "GLEhvScBdSWYZBRW" ], "tag": "_expression" }, { "model": [ "FCyAReCEenjhJqsb", "FCyAReCEenjhJqsb" ], "tag": "_model" }, { "model": [ "FCyAReCEenjhJqsb", "ReUjWMdaHvGAJvzZ" ], "tag": "_role" }, { "model": [ "FCyAReCEenjhJqsb", "JPHNleKUAeTnXqlb" ], "tag": "_user" }, { "model": [ "FCyAReCEenjhJqsb", "sOolKDIdxgMuyQcg" ], "tag": "_migration" } ]
Notes:

yields a list of objects in the _tag model.

There are two functions involved in this example: tag and all. Let's take the example apart. The function tag takes a string and attempts to find a model tagged with that string. It returns a reference to said model. If no matching model is found, it returns an error.

Example:

Query:
e.tag(e.data(d.string("_tag")))
Result:
[ "fjxRgZyTyjEMruRg", "BhngXKDJZDJdExxN" ]
Notes:

yields a reference to the _user model

By running the expression above, we obtain a reference to the _user model.

The function all takes a reference to a model and lists the objects in its collection.

So passing tag("_user") to all will yield a list of all users (in no particular order).