SELSimple Expression Language
GitHub

Using SEL

SEL is embedded: your application hands a rule and some data to the host library, and gets a value back. This page is that host API in all seven languages. The pages after it are complete programs, each written seven times:

Page What it shows
A REPL in thirty lines the whole API in the smallest useful program
Validation a form's rule set: compiled once, run per submission, errors told apart
Scripting with host functions the application registers functions; a script decides what happens
SQL conditions a rule as a WHERE clause — naive, and with the schema described
SQL pipelines whole queries in SQL, split with memory, or in memory — star, EAV, 3NF, flat and complex data
Your own functions, in SQL host functions with a SQL spelling: PostgreSQL SQL and PL/pgSQL functions, list arguments, strict mode

Every snippet on these pages is quoted from a file under examples/ that the test suite runs in all seven languages and whose output it compares byte for byte — the code cannot drift from what the page says, and the seven tabs cannot drift from each other.


Installing

The package is sel-lang everywhere. Python, PHP and JavaScript have no dependencies, so copying the directory works just as well as a package manager.

sh
pip install sel-lang                 # or copy python/sel/ into your project
python
from sel import compile, evaluate, Value, SelError      # Python 3.10+
from sel.sql import Sql, Binding                        # the SQL layer, when you need it

PACKAGING.md has the details of each registry.

Evaluate, or compile once and run many times

evaluate(source, context) parses and runs in one call. A form or a batch job compiles each rule once and keeps the program: a Program is immutable and reusable, and compiling is where syntax errors, unknown functions and wrong argument counts are caught — before any data is involved.

The snippets on this page come from examples/plain. The Rust and Go tabs use a few small helpers from it: at is "the host" as a position (Pos::default(), sel.Pos{}); Rust's val(s) is Value::text_owned(s.to_string()) and text(v) is v.as_text(at); Go's check(err) stops on an error a well-formed program never has, and eval(src) is sel.Eval(src, nil) with that check.

python
rule = compile('IF(QTY * PRICE > LIMIT, "over budget", "ok")')
for row in [{'QTY': '3', 'PRICE': '19.99'}, {'QTY': '1', 'PRICE': '5.00'}]:
    ctx = Value.from_native({**row, 'LIMIT': '50.00'})
    print(f"   QTY={row['QTY']} PRICE={row['PRICE']} =>", rule.run(ctx).as_text())

Money is text. Pass "19.99", never a float: a double has already lost the exactness SEL exists to keep, and the dynamic hosts refuse one rather than pretend otherwise.

Building a context

The context is a value whose children are the rule's variables. Each host builds it from its own maps and lists; C++, Rust and Go, which have no native map to convert from, build it child by child — Value::none() and set(), or sel.NewNone() and Set().

python
order = Value.from_native({
    'CUSTOMER': 'Zażółć',
    'ITEMS': [                                    # a list is a 1-based SEL list
        {'SKU': 'AB-1234', 'QTY': '3', 'PRICE': '19.99'},
        {'SKU': 'CD-5678', 'QTY': '1', 'PRICE': '5.01'},
    ],
})
print('   first SKU =>', compile('ITEMS[1]["SKU"]').run(order).as_text())
print('   total     =>', compile('SUM(ITEMS, _["QTY"] * _["PRICE"])').run(order).as_text())
print('   0.10+0.20 =>', evaluate('0.10 + 0.20').as_text())

Constructors

fromNative is the short way to a value. Each host also builds one piece by piece, and every constructor holds the same rules (spec §8):

  • Text and keys are checked. Invalid UTF-8, or an unpaired surrogate in a UTF-16 or code-point host, is E_UTF8: in a text, and in every key.
  • Bytes are bytes. A byte outside 0–255 is E_RANGE, and so is a boolean.
  • Numbers obey the digit caps, whether they come as a string, a native integer or the host's decimal form (E_RANGE). A decimal is canonicalised like a string, so 007 becomes 7 and a negative zero loses its sign.
  • Keys and values pair up. Counts that differ are E_BAD_ARG. A key given twice for a record keeps its first position and takes its last value, as RECORD does. A list's keys are distinct.
  • What you pass is copied. Changing your list, map, string or buffer afterwards does not change the value, and changing what toNative returned does not change the value it came from.
  • A malformed call is E_BAD_ARG. That covers a float, a non-integer where an integer goes, a decimal that is not one, and a native value with no conversion. It is a SelError in every host, never the host's own exception. In PHP, C++, Rust and Go, a call whose argument types do not match the declared parameters is rejected by the language before SEL sees it. Go reports these by panicking with the *sel.SelError rather than returning it (below).
Python JavaScript PHP C++ Common Lisp Rust Go
text, bytes, bool Value.text Value.bin Value.bool Value.text Value.bin Value.bool Value::text Value::bin Value::bool Value::text Value::bin Value::boolean make-text make-bin make-bool Value::text_owned Value::bin Value::bool sel.NewText sel.NewBin sel.NewBool
number from a string Value.num("1.50") Value.num('1.50') Value::num('1.50') Value::num("1.50") (make-num "1.50") Value::num(dec_parse("1.50", pos)?)? — (a number is its text: sel.NewText("1.50"))
number from the decimal form Value.num(Dec(neg, digits, scale)), Dec from sel.decimal Value.num({ neg, digits, scale }), digits a bigint Value::num(['neg' => …, 'digits' => '150', 'scale' => 2]) Value::num(const Dec&) (make-num (dec-make neg digits scale)) Value::num(Dec::from_small(neg, mantissa, scale))? sel.NewDecimal(sel.Decimal{Neg: neg, Digits: digits, Scale: scale}), Digits a *big.Int
native integer Value.int(n) Value.int(n), a safe integer or a bigint Value::int($n) Value::integer(n) (make-int n) Value::int(n), an i64 sel.NewInt(n), an int64
list Value.list(values) Value.list(values) Value::list($values) Value::list(values) (make-list-value values) Value::list(values) sel.NewList(values)
list with its own keys Value.list(values, keys) Value.fromEntries(entries, true) Value::list($values, $keys) — — — sel.NewListWithKeys(values, keys)
record from keys and values Value.record(keys, values), Value.shaped(keys, values) Value.shaped(keys, values) Value::record($keys, $values), Value::shaped(…) Value::record(keys, values) — — —
record from pairs Value.from_entries(pairs) Value.fromEntries(pairs) Value::fromEntries($pairs) — (from-native alist) — sel.NewRecordFromEntries(entries)
record from a prepared shape Value.shaped(shape, values) — Value::fromShape(RecordShape::intern($keys), $values) Value::shaped(shape, values) — — sel.NewShapedRecord(sel.InternRecordShape(keys), values)
many rows of one shape — — Value::fromNativeRows($rows) — — — —
one key v.set(k, x) v.set(k, x) $v->set($k, $x) v.set(k, x) (value-set v k x) v.set(k, x, pos)? v.Set(k, x)

Rust (Value::list_with_keys, record_from_entries, shaped_record) has more constructors than the table shows, but those are the builtins' own: they take what they are given, without the checks above, so the table leaves them out. Go's record constructors check their input (a key that is not UTF-8, a repeated key in a shape, a count that is not the shape's, a nil value), panicking with the *sel.SelError like the others.

The decimal form is digits × 10^-scale, negative when neg: digits is a non-negative whole number (a string of ASCII digits in PHP and C++) and scale a non-negative integer. Reading one back is v.as_decimal() in Python, v.asDecimal() in JS, $v->asDecimal() in PHP, v.as_decimal(pos) in C++ (read its digits with sel::dec_digits(d), whatever form the magnitude is held in; v.dec_val() is the cache read, null when the value holds no parsed decimal yet), (as-dec v) in Lisp, v.as_decimal(pos)? in Rust, and v.Decimal(pos) in Go, a sel.Decimal whose Digits is the caller's own copy. Go's sel.NewNum and Value.AsDecimal are deprecated: their decimal type cannot be named outside the module.

Variables flow back

The context is changed in place, so a rule can hand back more than its result:

python
ctx = Value.from_native({'QTY': '3', 'PRICE': '19.99'})
compile('NET = QTY * PRICE; VAT = ROUND(NET * 0.23, 2); GROSS = NET + VAT').run(ctx)
for name in ['NET', 'VAT', 'GROSS']:
    print(f'   {name:<5} =>', ctx.get(name).as_text())

A result may share values with the context. A bare variable, an index and TAKE hand back the context's own value in every host, and SPEC §3.4 lets a host skip the collectors' copies (MAP, FILTER, SORT_BY, LIST, …) in a program that has no assignment and no host function, because nothing in such a program can tell the difference; PHP, C++ and Python do. If the host changes a result through the API, or stores it back into a context that a later program assigns into, and the original values must stay as they were, clone() the result first.

Errors

Every failure is one exception type with a stable code and the position of the node that failed. Match on the code; the message is for people. E_ABORT is the rule speaking — everything else is the rule failing.

python
for src in ['3 + "A"', 'NOSUCH(1)', 'IF(1, "a", "b")', 'ABORT("no stock")']:
    try:
        evaluate(src)
        print(f'   {src:<17} => no error')
    except SelError as e:
        print(f'   {src:<17} => {e.code} at {e.line}:{e.col}')

Go and Rust: which calls return the error

In the five dynamic or exception-based hosts every failure is raised the same way. Go and Rust split it by call.

Go. sel.Compile, Program.Run, sel.Eval and the SQL layer's Translate, TryTranslate, TranslateStatement and ExecuteHybrid return an error, which is always a *sel.SelError (or the SQL layer's *sql.SqlError). Everything else reports a failure by panicking with one — the way an index out of range does:

  • the accessors of a value — AsText, AsBool, AsBytes, AsDecimal — when the value is not of that kind: a rule that answers TRUE where text was expected is a panic at v.AsText(pos), after Run returned no error;
  • constructors and Set given what SEL refuses (invalid UTF-8 is E_UTF8, NewListWithKeys with counts that differ is E_BAD_ARG);
  • sql.PlanHybrid with a dialect that does not exist, and the dialect extension calls sql.Define, DefineDialect and DefineBuilder;
  • RegisterFunction with a name or an arity that is not allowed — a programming error, so it panics with a plain string, at start-up.

Inside a host function that is the way to fail: sel.Fail(code, message, pos) panics with a *sel.SelError, the Args readers do the same, and Run turns it into its returned error. Outside one, a server that reads results it does not control recovers the accessor's panic, as the validation example does:

go
// asText is v.AsText for a result that might not be text. The accessors of a
// *sel.Value panic with a *sel.SelError rather than return one, so a rule that
// answered TRUE would otherwise take the request down with it.
func asText(v *sel.Value) (text string, err error) {
	defer func() {
		if r := recover(); r != nil {
			e, ok := r.(*sel.SelError)
			if !ok {
				panic(r)
			}
			err = e
		}
	}()
	return v.AsText(sel.Pos{}), nil
}

A compiled *sel.Program is safe to share between goroutines.

Rust. Every call that can fail returns Result<_, SelError>, accessors included (v.as_text(pos)?), so ? carries a SEL error up to the caller. The exceptions are the SQL layer's configuration calls: sql::define, define_dialect, define_builder, the Binding constructors and plan_hybrid panic with a SqlError on a bad argument. A Program is neither Send nor Sync, and run takes &mut self: compile one per thread (it is cheap), or keep it in a thread_local!. For the same reason a host function — which register_function requires to be Send + Sync + 'static — cannot capture a compiled program or a Value; examples/sql-functions keeps its programs in a thread_local!. A host function fails by returning Err(SelError::new(code, message, pos)).

What does a rule read?

dependencies() answers statically, without running the rule — which is what lets a form re-check only the rules a changed field can affect:

python
print('  ', ' '.join(
    compile('T = SUM(ITEMS, _["QTY"]); T > LIMIT AND CUSTOMER $!= ""').dependencies()))

The answer follows evaluation order (spec §8): a name is reported when some read of it can happen before the rule has definitely assigned it. So a rule that reads A and assigns it later reports A; one that assigns A first reports only what else it reads; and an assignment inside one branch of an IF, on the right of AND, OR or ??, or in an aggregate's body does not count as an assignment for a read after it. X += 1 and A[k] += x read their target. It is safe to over-report and never to under-report, which is what a form re-checking on change needs.

Host functions and threads

register_function / registerFunction may be called while other threads compile or run programs; the function table is synchronised in every host that has threads, and a program already compiled keeps the function it was compiled against. Reading an argument the call does not have (a function declared with one or two arguments asking for the fifth) is E_BAD_ARG, and a registration that is not callable, or a name that is not allowed, is refused when registering, in each host's own argument-error class.

Sharing a compiled program

A compiled program may be shared between threads in C++: a Program is immutable once compile() returns and may run on several threads at once, its first run included, provided every run gets a context of its own. A C++ Value is not thread-safe even for reading (its reference count is a plain integer), so a context and every value reachable from it belong to one thread at a time: build each thread's context on that thread, or clone() it there from a value no other thread is touching. The SQL layer's translation may run on several threads once the dialects it uses are registered. cpp/sel.hpp states the contract, and make -C cpp tsan holds it.

In Rust a Program is Send but not Sync, and run takes &mut self: move a compiled program to the thread that runs it, or give each thread its own — program.clone() before spawning, a program compiled per thread, or one in a thread_local!. A Value (and so a context or a result) is neither Send nor Sync; hand other threads its text (dump(), as_text()) instead. A host function must be Send + Sync + 'static, so it cannot capture either.

PHP memory

A PHP Value costs a few hundred bytes, so a rule that builds a collection near the language's cap (MAX_COLLECTION, one million elements) needs memory_limit of at least 1G on PHP 8.5, and roughly two to three times that on PHP 8.1. Text is kept as bytes and is not the problem: a 16-million-character text is about that many bytes. The default 128M is enough for everything below a few hundred thousand elements. The repository's own budget and PHP 8.1 lanes run with memory_limit=-1.

The API side by side

Python JavaScript PHP C++ Common Lisp Rust Go
compile compile(src) compile(src) Sel::compile($src) sel::compile(src) (sel:compile-source src) compile(src)? sel.Compile(src)
run p.run(ctx) p.run(ctx) $p->run($ctx) p.run(ctx) (sel:run p ctx) p.run(Some(ctx))? p.Run(ctx)
evaluate evaluate(src, ctx) evaluate(src, ctx) Sel::evaluate($src, $ctx) sel::evaluate(src, ctx) (sel:evaluate src ctx) evaluate(src, Some(ctx))? sel.Eval(src, ctx)
inputs p.dependencies() p.dependencies() $p->dependencies() p.dependencies() (sel:dependencies p) p.dependencies()? p.Dependencies()
context Value.from_native({...}) Value.fromNative({...}) Value::fromNative([...]) Value::none() + set (sel:from-native ...) Value::none() + set sel.NewNone() + Set
result .as_text() .as_bool() .dump() .asText() .asBool() .dump() ->asText() ->asBool() ->dump() .as_text() .as_bool() .dump() (sel:as-text v) (sel:as-bool v) (sel:value-dump v) .as_text(pos)? .as_bool(pos)? .dump()? .AsText(pos) .AsBool(pos) .Dump()
errors SelError .code .line .col SelError .code .line .col SelError ->code ->line ->col sel::SelError .code() .line() .col() sel:sel-error sel-error-code … SelError .code .pos.line .pos.col *sel.SelError .Code .Line() .Col()
own functions register_function registerFunction Sel::registerFunction sel::register_function sel:register-function register_function sel.RegisterFunction

In Go, the accessors in the result row and the constructors panic with a *sel.SelError rather than return it — see Go and Rust: which calls return the error.

The full contract is spec/SPEC.md §8, and tools/check-api.sh runs the same probes through all seven bindings to keep the answers identical.

View this page's Markdown on GitHub