This handbook is written for a computer science student who wants to understand Eyeling as code and as a reasoning machine.
It’s meant to be read linearly, but each chapter stands on its own.
log:conclusionEyeling is a small Notation3 (N3) reasoner implemented in JavaScript. Its job is to take:
=> and <=),and compute consequences until nothing new follows.
If you’ve seen Datalog or Prolog, the shape will feel familiar. Eyeling blends both:
=> rules.<= rules and for built-in predicates.That last point is the heart of Eyeling’s design: forward rules are executed by proving their bodies using a backward engine. This lets forward rules depend on computations and “virtual predicates” without explicitly materializing everything as facts.
Eyeling deliberately keeps the implementation small and dependency-free:
eyeling.js)lib/* modules that read like a miniature compiler + logic engine.This handbook is a tour of that miniature system.
Let’s name the pieces:
(subject, predicate, object).{ body } => { head }.{ head } <= { body }.Eyeling runs like this:
FR_fR_br ∈ R_f:
r.body using:
r.headA good mental model is:
Forward chaining is “outer control”. Backward chaining is the “query engine” used inside each rule firing.
A sketch:
FORWARD LOOP (saturation)
for each forward rule r:
solutions = PROVE(r.body) <-- backward reasoning + builtins
for each s in solutions:
emit instantiate(r.head, s)
Because PROVE can call built-ins (math, string, list, crypto, dereferencing…), forward rules can compute fresh bindings as part of their condition.
If you want to follow the code in the same order Eyeling “thinks”, read:
lib/prelude.js — the AST (terms, triples, rules), namespaces, prefix handling.lib/lexer.js — N3/Turtle-ish tokenization.lib/parser.js — parsing tokens into triples, formulas, and rules.lib/rules.js — small rule “compiler passes” (blank lifting, constraint delaying).lib/engine.js — the core engine:
proveGoals)forwardChain)evalBuiltin)log:*In and includes tests)lib/trace.js, log:trace)time:* built-ins (lib/time.js)log:skolem) (lib/skolem.js)lib/deref.js — synchronous dereferencing for log:content / log:semantics.lib/printing.js — conversion back to N3 text.lib/cli.js + lib/entry.js — command-line wiring and bundle entry exports.index.js — the npm API wrapper (spawns the bundled CLI synchronously).This is almost literally a tiny compiler pipeline:
text → tokens → AST (facts + rules) → engine → derived facts → printer
lib/prelude.js)Eyeling uses a small AST. You can think of it as the “instruction set” for the rest of the reasoner.
A Term is one of:
Iri(value) — an absolute IRI stringLiteral(value) — stored as raw lexical form (e.g. "hi"@en, 12, "2020-01-01"^^<dt>)Var(name) — variable name without the leading ?Blank(label) — blank node label like _:b1ListTerm(elems) — a concrete N3 list (a b c)OpenListTerm(prefix, tailVar) — a “list with unknown tail”, used for list unification patternsGraphTerm(triples) — a quoted formula { ... } as a first-class termThat last one is special: N3 allows formulas as terms, so Eyeling must treat graphs as matchable data.
A triple is:
Triple(s, p, o) where each position is a Term.A rule is:
Rule(premiseTriples, conclusionTriples, isForward, isFuse, headBlankLabels)Two details matter later:
false acts as a hard failure. (More in Chapter 10.)headBlankLabels records which blank node labels occur explicitly in the head of a rule. Those blanks are treated as existentials and get skolemized per firing. (Chapter 9.)Eyeling interns IRIs and Literals by string value. Interning is a quiet performance trick with big consequences:
Terms are treated as immutable: once interned, the code assumes you won’t mutate .value.
PrefixEnv holds prefix mappings and a base IRI. It provides:
ex:foo → full IRI)ex:foo when possible)lib/lexer.js, lib/parser.js)Eyeling’s parser is intentionally pragmatic: it aims to accept “the stuff people actually write” in N3/Turtle, including common shorthand.
The lexer turns the input into tokens like:
{ } ( ) [ ] , ; .=>, <=, =, !, ^@prefix, @base, and also SPARQL-style PREFIX, BASE?x_:b1<...>rdf:type, :localtrue/false, ^^ datatypes, @en language tags# commentsParsing becomes dramatically simpler because tokenization already decided where strings end, where numbers are, and so on.
The parser supports:
; and ,[ :p :o; :q :r ]( ... ) as ListTerm{ ... } as GraphTermis ... of and inverse arrows! and ^ that may generate helper triples via fresh blanksA nice detail: the parser maintains a pendingTriples list used when certain syntactic forms expand into helper triples (for example, some path/property-list expansions). It ensures the “surface statement” still emits all required triples even if the subject itself was syntactic sugar.
=>, <=, and log idiomsAt the top level, the parser recognizes:
{ P } => { C } . as a forward rule{ H } <= { B } . as a backward ruleIt also normalizes top-level triples of the form:
{ P } log:implies { C } .{ H } log:impliedBy { B } .into the same internal Rule objects. That means you can write rules either as operators (=>, <=) or as explicit log: predicates.
true and false as rule endpointsEyeling treats two literals specially in rule positions:
true stands for the empty formula {} (an empty premise or head).false is used for inference fuses ({ ... } => false.).So these are valid patterns:
true => { :Program :loaded true }.
{ ?x :p :q } => false.
Internally:
true becomes “empty triple list”false becomes “no head triples” plus the isFuse flag if forward.lib/rules.js)Before rules hit the engine, Eyeling performs two lightweight transformations.
In N3 practice, blanks in rule premises behave like universally-quantified placeholders. Eyeling implements this by converting Blank(label) to Var(_bN) in the premise only.
So a premise like:
{ _:x :p ?y. } => { ... }.
acts like:
{ ?_b1 :p ?y. } => { ... }.
This avoids the “existential in the body” trap and matches how most rule authors expect N3 to behave.
Blanks in the conclusion are not lifted — they remain blanks and later become existentials (Chapter 9).
Some built-ins don’t generate bindings; they only test conditions:
math:greaterThan, math:lessThan, math:equalTo, …string:matches, string:contains, …log:notIncludes, log:forAllIn, log:outputString, …Eyeling treats these as “constraints” and moves them to the end of a forward rule premise. This is a Prolog-style heuristic:
Bind variables first; only then run pure checks.
It’s not logically necessary, but it improves the chance that constraints run with variables already grounded, reducing wasted search.
lib/engine.js)Once you enter engine.js, you enter the “physics layer.” Everything else depends on the correctness of:
Eyeling has ordinary structural equality (term-by-term) for most terms.
But quoted formulas (GraphTerm) demand something stronger. Two formulas should match even if their internal blank/variable names differ, as long as the structure is the same.
That’s alpha-equivalence:
{ _:x :p ?y. } should match { _:z :p ?w. }Eyeling implements alpha-equivalence by checking whether there exists a consistent renaming mapping between the two formulas’ variables/blanks that makes the triples match.
Eyeling makes a deliberate choice about groundness:
GraphTerm do not make the surrounding triple non-groundThis is encoded in functions like isGroundTermInGraph. It’s what makes it possible to assert and store triples that mention formulas with variables as data.
A substitution is a plain JS object:
{ X: Term, Y: Term, ... }
When applying substitutions, Eyeling follows chains:
X → Var(Y) and Y → Iri(...), applying to X yields the IRI.This matters because unification can bind variables to variables; it’s normal in logic programming, and you want applySubst to “chase the link” until it reaches a stable term.
Unification is implemented in unifyTerm / unifyTriple, with support for:
formula unification via graph unification:
There are two key traits of Eyeling’s graph unification:
Eyeling keeps literal values as raw strings, but it parses and normalizes where needed:
literalParts(lit) splits lexical form and datatype IRIrdf:JSON / <...rdf#JSON>)BigInt), and numeric metadata.This lets built-ins and fast-key indexing treat some different lexical spellings as the same value (for example, normalizing "abc" and "abc"^^xsd:string in the fast-key path).
Reasoning is mostly “join-like” operations: match a goal triple against known facts. Doing this naively is too slow, so Eyeling builds indexes on top of a plain array.
Facts live in an array facts: Triple[].
Eyeling attaches hidden (non-enumerable) index fields:
facts.__byPred: Map<predicateIRI, Triple[]>facts.__byPS: Map<predicateIRI, Map<subjectKey, Triple[]>>facts.__byPO: Map<predicateIRI, Map<objectKey, Triple[]>>facts.__keySet: Set<string> for a fast-path “S\tP\tO” key when all terms are IRI/Literal-likeThe “fast key” only exists when termFastKey succeeds for all three terms.
When proving a goal with IRI predicate, Eyeling computes candidate facts by:
This is a cheap selectivity heuristic. In type-heavy RDF, (p,o) is often extremely selective (e.g., rdf:type + a class IRI), so the PO index can be a major speed win.
A tempting optimization would be “treat two triples as duplicates modulo blank renaming.” Eyeling does not do this globally, because it would be unsound: different blank labels represent different existentials unless explicitly linked.
So:
proveGoals)Eyeling’s backward prover is an iterative depth-first search (DFS) that looks a lot like Prolog’s SLD resolution, but written explicitly with a stack to avoid JS recursion limits.
A proof state contains:
goals: remaining goal triplessubst: current substitutiondepth: current depth (used for compaction heuristics)visited: previously-seen goals (loop prevention)At each step:
Otherwise:
attempt to satisfy it in three ways:
Eyeling’s order is intentional: built-ins often bind variables cheaply; rules expand search trees.
A built-in is evaluated as:
deltas = evalBuiltin(goal0, {}, facts, backRules, ...)
for delta in deltas:
composed = composeSubst(currentSubst, delta)
So built-ins behave like relations that can generate zero, one, or many possible bindings.
This is important: a list generator might yield many deltas; a numeric test yields zero or one.
Eyeling prevents obvious infinite recursion by skipping a goal if it is already in the visited list. This is a pragmatic check; it doesn’t implement full tabling, but it avoids the most common “A depends on A” loops.
Backward rules are indexed in backRules.__byHeadPred. When proving a goal with IRI predicate p, Eyeling retrieves:
rules whose head predicate is p__wildHeadPred for rules whose head predicate is not an IRI (rare, but supported)For each candidate rule:
That “standardize apart” step is essential. Without it, reusing a rule multiple times would accidentally share variables across invocations, producing incorrect bindings.
Deep backward chains can create large substitutions. If you copy a growing object at every step, you can accidentally get O(depth²) behavior.
Eyeling avoids that with maybeCompactSubst:
if depth is high or substitution is large, it keeps only bindings relevant to:
This is semantics-preserving for the ongoing proof search, but dramatically improves performance on deep recursive proofs.
forwardChain)Forward chaining is Eyeling’s outer control loop. It is where facts get added and the closure grows.
Eyeling loops until no new facts are added. Inside that loop, it scans every forward rule and tries to fire it.
A simplified view:
repeat
changed = false
for each forward rule r:
sols = proveGoals(r.premise, facts, backRules)
for each solution s:
for each head triple h in r.conclusion:
inst = applySubst(h, s)
inst = skolemizeHeadBlanks(inst)
if inst is ground and new:
add inst to facts
changed = true
until not changed
There is a nice micro-compiler optimization in runFixpoint():
If a rule’s head is strictly ground (no vars, no blanks, no open lists, even inside formulas), and it contains no head blanks, then the head does not depend on which body solution you choose.
In that case:
This is a surprisingly effective optimization for “axiom-like” rules with constant heads.
Blank nodes in the rule head represent existentials: “there exists something such that…”
Eyeling handles this by replacing head blank labels with fresh blank labels of the form:
_:sk_0, _:sk_1, …But it does something subtle and important: it caches skolemization per (rule firing, head blank label), so that the same firing instance doesn’t keep generating new blanks across outer iterations.
The “firing instance” is keyed by a deterministic string derived from the instantiated body (“firingKey”). This stabilizes the closure and prevents “existential churn.”
Implementation: deterministic Skolem IDs live in lib/skolem.js; the per-firing cache and head-blank rewriting are implemented in lib/engine.js.
{ ... } => falseA rule whose conclusion is false is treated as a hard failure. During forward chaining:
This is Eyeling’s way to express constraints and detect inconsistencies.
Eyeling treats certain derived triples as new rules:
log:implies and log:impliedBy where subject/object are formulastrue as an empty formula {} on either sideSo these are “rule triples”:
{ ... } log:implies { ... }.
true log:implies { ... }.
{ ... } log:impliedBy true.
When such a triple is derived in a forward rule head:
Rule object and inserting it into the forward or backward rule list.This is meta-programming: your rules can generate new rules during reasoning.
log:conclusionSome log: built-ins talk about “what is included in the closure” or “collect all solutions.” These are tricky in a forward-chaining engine because the closure is evolving.
Eyeling addresses this with a disciplined two-phase strategy and an optional priority mechanism.
Forward chaining runs inside an outer loop that alternates:
Phase A: scoped built-ins are disabled (they “delay” by failing)
Eyeling saturates normally to a fixpoint
then Eyeling freezes a snapshot of the saturated facts
Phase B: scoped built-ins are enabled, but they query only the frozen snapshot
Eyeling runs saturation again (new facts can appear due to scoped queries)
This produces deterministic behavior for scoped operations: they observe a stable snapshot, not a moving target.
Eyeling introduces a scopedClosureLevel counter:
Some built-ins interpret a positive integer literal as a requested priority:
log:collectAllIn and log:forAllIn use the object position for prioritylog:includes and log:notIncludes use the subject position for priorityIf a rule requests priority N, Eyeling delays that builtin until scopedClosureLevel >= N.
In practice this allows rule authors to write “don’t run this scoped query until the closure is stable enough” and is what lets Eyeling iterate safely when rule-producing rules introduce new needs.
log:conclusion: local deductive closure of a formulalog:conclusion is handled in a particularly elegant way:
{ ... } (a GraphTerm),Eyeling computes the deductive closure inside that formula:
log:implies, log:impliedBy)forwardChain locally over those triplesWeakMap so the same formula doesn’t get recomputedNotably, log:impliedBy inside the formula is treated as forward implication too for closure computation (and also indexed as backward to help proving).
This makes formulas a little world you can reason about as data.
evalBuiltin)Built-ins are where Eyeling stops being “just a Datalog engine” and becomes a practical N3 tool.
A predicate is treated as builtin if:
it is an IRI in one of the builtin namespaces:
crypto:, math:, log:, string:, time:, list:rdf:first / rdf:rest (treated as list-like builtins)log:implies and log:impliedBy are treated as builtins.Super restricted mode exists to let you treat all other predicates as ordinary facts/rules without any built-in evaluation.
Every builtin returns a list of substitution deltas.
That means built-ins can be:
[{}] for success or [] for failure)List operations are a common source of generators; numeric comparisons are tests.
Below is a drop-in replacement for §11.3 “A tour of builtin families” that aims to be fully self-contained and to cover every builtin currently implemented in lib/engine.js (including the rdf:first / rdf:rest aliases).
Eyeling’s builtins are best thought of as foreign predicates: they look like ordinary N3 predicates in your rules, but when the engine tries to satisfy a goal whose predicate is a builtin, it does not search the fact store. Instead, it calls a piece of JavaScript that implements the predicate’s semantics.
That one sentence explains a lot of “why does it behave like that?”:
The N3 Builtins tradition often describes builtins using “schema” annotations like:
$s+ / $o+ — input must be bound (or at least not a variable in practice)$s- / $o- — output position (often a variable that will be bound)$s? / $o? — may be unbound$s.i — list element i inside the subject listEyeling is a little more pragmatic: it implements the spirit of these schemas, but it also has several “engineering” conventions that appear across many builtins:
?X) may be bound by a builtin if the builtin is written to do so.[] / _:) are frequently treated as “don’t care” placeholders. Many builtins accept a blank node in an output position and simply succeed without binding.With that, we can tour the builtin families as Eyeling actually implements them.
crypto: — digest functions (Node-only)These builtins hash a string and return a lowercase hex digest as a plain string literal.
crypto:sha, crypto:md5, crypto:sha256, crypto:sha512Shape:
$literal crypto:sha256 $digest
Semantics (Eyeling):
Important runtime note: Eyeling uses Node’s crypto module. If crypto is not available (e.g., in some browser builds), these builtins simply fail (return no solutions).
Example:
"hello" crypto:sha256 ?d.
# ?d becomes "2cf24dba5...<snip>...9824"
math: — numeric and numeric-like relationsEyeling’s math: builtins fall into three broad categories:
>, <, =, …).A key design choice: Eyeling parses numeric terms fairly strictly, but comparisons accept a wider “numeric-like” domain including durations and date/time values in some cases.
These builtins succeed or fail; they do not introduce new bindings.
math:greaterThan (>)math:lessThan (<)math:notGreaterThan (≤)math:notLessThan (≥)math:equalTo (=)math:notEqualTo (≠)Shapes:
$a math:greaterThan $b.
$a math:equalTo $b.
Eyeling also accepts an older cwm-ish variant where the subject is a 2-element list:
( $a $b ) math:greaterThan true. # (supported as a convenience)
Accepted term types (Eyeling):
xsd:integer, xsd:decimal, xsd:float, xsd:double, and integer-derived types).123, -4.5, 1.2e3) when they look numeric.xsd:duration literals (treated as seconds via a simplified model).xsd:date and xsd:dateTime literals (converted to epoch seconds for comparison).Edge cases:
NaN is treated as not equal to anything, including itself, for math:equalTo.Because these are pure tests, Eyeling treats them as constraint builtins and tends to push them to the end of forward-rule premises so they’re checked after other goals bind variables.
These are “function-like” relations where the subject is usually a list and the object is the result.
math:sumShape: ( $x1 $x2 ... ) math:sum $total
math:productShape: ( $x1 $x2 ... ) math:product $total
math:sum, but multiplies.math:differenceThis one is more interesting because Eyeling supports a couple of mixed “numeric-like” cases.
Shape: ( $a $b ) math:difference $c
Eyeling supports:
c = a - b.DateTime difference: (dateTime1 dateTime2) math:difference duration
xsd:duration in whole days (internally computed via seconds then formatted).DateTime minus duration: (dateTime duration) math:difference dateTime
If the types don’t fit any supported case, the builtin fails.
math:quotientShape: ( $a $b ) math:quotient $q
b != 0.a / b, picking a suitable numeric datatype for output.math:integerQuotientShape: ( $a $b ) math:integerQuotient $q
math:remainderShape: ( $a $b ) math:remainder $r
math:roundedShape: $x math:rounded $n
Math.round, i.e. halves go toward +∞ (-1.5 -> -1, 1.5 -> 2).math:exponentiationShape: ( $base $exp ) math:exponentiation $result
base ** exp.Reverse direction (limited): Eyeling can sometimes solve for the exponent if:
exp = log(result) / log(base).This is a pragmatic inversion, not a full algebra system.
Eyeling implements these as a shared pattern: if the subject is numeric, compute object; else if the object is numeric, compute subject via an inverse function; if both sides are unbound, succeed once (don’t enumerate).
math:absoluteValuemath:negationmath:degrees (and implicitly its inverse “radians” conversion)math:sin, math:cos, math:tanmath:asin, math:acos, math:atanmath:sinh, math:cosh, math:tanh (only if JS provides the functions)Example:
"0"^^xsd:double math:cos ?c. # forward
?x math:cos "1"^^xsd:double. # reverse (principal acos)
Inversion uses principal values (e.g., asin, acos, atan) and does not attempt to enumerate periodic families of solutions.
time: — dateTime inspection and “now”Eyeling’s time builtins work over xsd:dateTime lexical forms. They are deliberately simple: they extract components from the lexical form rather than implementing a full time zone database.
Implementation: these helpers live in lib/time.js and are called from lib/engine.js’s builtin evaluator.
time:yeartime:monthtime:daytime:hourtime:minutetime:secondShape:
$dt time:month $m
Semantics:
xsd:dateTime literal in a format Eyeling can parse.time:timeZoneShape:
$dt time:timeZone $tz
Returns the trailing zone designator:
"Z" for UTC, or"+02:00" / "-05:00"It yields a plain string literal (and also accepts typed xsd:string literals).
time:localTimeShape:
"" time:localTime ?now
Binds ?now to the current local time as an xsd:dateTime literal.
Two subtle but important engineering choices:
list: — list structure, iteration, and higher-order helpersEyeling has a real internal list term (ListTerm) that corresponds to N3’s (a b c) surface syntax.
rdf:first / rdf:rest) are materializedN3 and RDF can also express lists as linked blank nodes using rdf:first / rdf:rest and rdf:nil. Eyeling materializes such structures into internal list terms before reasoning so that list:* builtins can operate uniformly.
For convenience and compatibility, Eyeling treats:
rdf:first as an alias of list:firstrdf:rest as an alias of list:restlist:first (and rdf:first)Shape:
(a b c) list:first a
list:rest (and rdf:rest)Shape:
(a b c) list:rest (b c)
Eyeling supports both:
(a b c), and(a b ... ?T) internally.For open lists, “rest” preserves openness:
(a ... ?T) is ?T(a b ... ?T) is (b ... ?T)list:firstRestThis is a very useful “paired” view of a list.
Forward shape:
(a b c) list:firstRest (a (b c))
Backward shapes (construction):
(first restList), it can construct the list.rest is a variable, Eyeling constructs an open list term.This is the closest thing to Prolog’s [H|T] in Eyeling.
These builtins can yield multiple solutions.
list:memberShape:
(a b c) list:member ?x
Generates one solution per element, unifying the object with each member.
list:inShape:
?x list:in (a b c)
Same idea, but the list is in the object position and the subject is unified with each element.
list:iterateShape:
(a b c) list:iterate ?pair
Generates (index value) pairs with 0-based indices:
(0 a), (1 b), (2 c), …A nice ergonomic detail: the object may be a pattern such as:
(a b c) list:iterate ( ?i "b" ).
In that case Eyeling unifies ?i with 1 and checks the value part appropriately.
list:memberAtShape:
( (a b c) 1 ) list:memberAt b
The subject must be a 2-element list: (listTerm indexTerm).
Eyeling can use this relationally:
iterate, but with separate index/value logic).Indices are 0-based.
list:lengthShape:
(a b c) list:length 3
Returns the length as an integer token literal.
A small but intentional strictness: if the object is already ground, Eyeling does not accept “integer vs decimal equivalences” here; it wants the exact integer notion.
list:lastShape:
(a b c) list:last c
Returns the last element of a non-empty list.
list:reverseReversible in the sense that either side may be the list:
It does not enumerate arbitrary reversals; it’s a deterministic transform once one side is known.
list:removeShape:
( (a b a c) a ) list:remove (b c)
Removes all occurrences of an item from a list.
Important constraint: the item to remove must be ground (fully known) before the builtin will run.
list:notMember (constraint)Shape:
(a b c) list:notMember x
Succeeds iff the object cannot be unified with any element of the subject list. This is treated as a constraint builtin.
list:appendThis is list concatenation, but Eyeling implements it in a pleasantly relational way.
Forward shape:
( (a b) (c) (d e) ) list:append (a b c d e)
Subject is a list of lists; object is their concatenation.
Splitting (reverse-ish) mode: If the object is a concrete list, Eyeling tries all ways of splitting it into the given number of parts and unifying each part with the corresponding subject element. This can yield multiple solutions and is handy for logic programming patterns.
list:sortSorts a list into a deterministic order.
Like reverse, this is “reversible” only in the sense that if one side is a list, the other side can be unified with its sorted form.
list:map (higher-order)This is one of Eyeling’s most powerful list builtins because it calls back into the reasoner.
Shape:
( (x1 x2 x3) ex:pred ) list:map ?outList
Semantics:
(inputList predicateIri).inputList must be ground.For each element el in the input list, Eyeling proves the goal:
el predicateIri ?y.
using the full engine (facts, backward rules, and builtins).
?y values are collected in proof order and concatenated into the output list.This makes list:map a compact “query over a list” operator.
log: — unification, formulas, scoping, and meta-level controlThe log: family is where N3 stops being “RDF with rules” and becomes a meta-logic. Eyeling supports the core operators you need to treat formulas as terms, reason inside quoted graphs, and compute closures.
log:equalToShape:
$x log:equalTo $y
This is simply term unification: it succeeds if the two terms can be unified and returns any bindings that result.
log:notEqualTo (constraint)Succeeds iff the terms cannot be unified. No new bindings.
In Eyeling, a quoted formula { ... } is represented as a GraphTerm whose content is a list of triples (and, when parsed from documents, rule terms can also appear as log:implies / log:impliedBy triples inside formulas).
log:conjunctionShape:
( F1 F2 ... ) log:conjunction F
true is treated as the empty formula and is ignored in the merge.log:conclusionShape:
F log:conclusion C
Computes the deductive closure of the formula F using only the information inside F:
F as facts.{A} => {B} (represented internally as a log:implies triple between formulas) as a forward rule.{A} <= {B} as the corresponding forward direction for closure purposes.Eyeling caches log:conclusion results per formula object, so repeated calls with the same formula term are cheap.
These builtins reach outside the current fact set. They are synchronous by design.
log:contentShape:
<doc> log:content ?txt
xsd:string literal.file://).log:semanticsShape:
<doc> log:semantics ?formula
Dereferences and parses the remote/local resource as N3/Turtle-like syntax, returning a formula.
A nice detail: top-level rules in the parsed document are represented as data inside the returned formula using log:implies / log:impliedBy triples between formula terms. This means you can treat “a document plus its rules” as a single first-class formula object.
log:semanticsOrErrorLike log:semantics, but on failure it returns a string literal such as:
error(dereference_failed,...)error(parse_error,...)This is convenient in robust pipelines where you want logic that can react to failures.
log:parsedAsN3Shape:
" ...n3 text... " log:parsedAsN3 ?formula
Parses an in-memory string as N3 and returns the corresponding formula.
log:rawTypeReturns one of four IRIs:
log:Formula (quoted graph)log:Literalrdf:List (closed or open list terms)log:Other (IRIs, blank nodes, etc.)These two are classic N3 “bridge” operators between structured data and concrete RDF literal forms.
log:dtlitRelates a datatype literal to a pair (lex datatypeIri).
(stringLiteral datatypeIri).Language-tagged strings are normalized: they are treated as having datatype rdf:langString.
log:langlitRelates a language-tagged literal to a pair (lex langTag).
"hello"@en, subject can become ("hello" "en").("hello" "en"), object can become "hello"@en.log:implies and log:impliedByAs syntax, Eyeling parses {A} => {B} and {A} <= {B} into internal forward/backward rules.
As builtins, log:implies and log:impliedBy let you inspect the currently loaded rule set:
log:implies enumerates forward rules as (premiseFormula, conclusionFormula) pairs.log:impliedBy enumerates backward rules similarly.Each enumerated rule is standardized apart (fresh variable names) before unification so you can safely query over it.
log:includes and friendslog:includesShape:
Scope log:includes GoalFormula
This proves all triples in GoalFormula as goals, returning the substitutions that make them provable.
Eyeling has two modes:
Explicit scope graph: if Scope is a formula {...}
Priority-gated global scope: otherwise
N.N, the builtin “delays” by failing at that point in the search.This priority mechanism exists because Eyeling’s forward chaining runs in outer iterations with a “freeze snapshot then evaluate scoped builtins” phase. The goal is to make scoped meta-builtins stable and deterministic: they query a fixed snapshot rather than chasing a fact store that is being mutated mid-iteration.
Also supported:
true, meaning the empty formula, which is always included (subject to the priority gating above).log:notIncludes (constraint)Negation-as-failure version: it succeeds iff log:includes would yield no solutions (under the same scoping rules).
log:collectAllInShape:
( ValueTemplate WhereFormula OutList ) log:collectAllIn Scope
WhereFormula in the chosen scope.ValueTemplate and collects the instantiated terms into a list.OutList with that list.OutList is a blank node, Eyeling just checks satisfiable without binding/collecting.This is essentially a list-producing “findall”.
log:forAllIn (constraint)Shape:
( WhereFormula ThenFormula ) log:forAllIn Scope
For every solution of WhereFormula, ThenFormula must be provable under the bindings of that solution. If any witness fails, the builtin fails. No bindings are returned.
This is treated as a constraint builtin.
log:skolemShape:
$groundTerm log:skolem ?iri
Deterministically maps a ground term to a Skolem IRI in Eyeling’s well-known namespace. This is extremely useful when you want a repeatable identifier derived from structured content.
log:uriBidirectional conversion between IRIs and their string form:
<...> in Turtle/N3, and it rejects _:-style strings to avoid confusing blank nodes with IRIs.log:traceAlways succeeds once and prints a debug line to stderr:
<s> TRACE <o>
using the current prefix environment for pretty printing.
Implementation: this is implemented by lib/trace.js and called from lib/engine.js.
log:outputStringAs a goal, this builtin simply checks that the terms are sufficiently bound/usable and then succeeds. The actual “printing” behavior is handled by the CLI:
--strings / -r, the CLI collects all log:outputString triples from the saturated closure.This is treated as a constraint builtin (it shouldn’t drive search; it should merely validate that strings exist once other reasoning has produced them).
string: — string casting, tests, regexes, and JSON pointersEyeling implements string builtins with a deliberate interpretation of “domain is xsd:string”:
Any literal can be cast to a string:
string:concatenationShape:
( s1 s2 ... ) string:concatenation s
Casts each element to a string and concatenates.
string:formatShape:
( fmt a1 a2 ... ) string:format out
A tiny sprintf subset:
%s and %%.%d, %f, …) causes the builtin to fail.string:containsstring:containsIgnoringCasestring:startsWithstring:endsWithAll are pure tests: they succeed or fail.
string:equalIgnoringCasestring:notEqualIgnoringCasestring:greaterThanstring:lessThanstring:notGreaterThan (≤ in Unicode codepoint order)string:notLessThan (≥ in Unicode codepoint order)These compare JavaScript strings directly, i.e., Unicode code unit order (practically “lexicographic” for many uses, but not locale-aware collation).
Eyeling compiles patterns using JavaScript RegExp, with a small compatibility layer:
\p{L}) or code point escapes (\u{...}), Eyeling enables the /u flag.string:matches / string:notMatches (constraints)Shape:
data string:matches pattern
Tests whether pattern matches data.
string:replaceShape:
( data pattern replacement ) string:replace out
pattern as a global regex (/g).$1, $2, etc. work).string:scrapeShape:
( data pattern ) string:scrape out
Matches the regex once and returns the first capturing group (group 1). If there is no match or no group, it fails.
string:jsonPointerShape:
( jsonText pointer ) string:jsonPointer value
This builtin is intentionally “bridgey”: it lets you reach into JSON and get back an RDF/N3 term.
Rules:
jsonText must be an rdf:JSON literal (Eyeling is permissive and may accept a couple of equivalent datatype spellings).pointer is a string; Eyeling supports:
/a/b/0#/a/b (it is decoded first)(jsonText, pointer).Returned terms follow Eyeling’s jsonToTerm mapping:
null → "null" (a plain string literal)true / false token literal (untyped boolean token)rdf:JSON literal containing the object’s JSON textThis design keeps the builtin total and predictable even for nested structures.
log:outputString as a controlled side effectFrom a logic-programming point of view, printing is awkward: if you print during proof search, you risk producing output along branches that later backtrack, or producing the same line multiple times in different derivations. Eyeling avoids that whole class of problems by treating “output” as data.
The predicate log:outputString is the only officially supported “side-effect channel”, and even it is handled in two phases:
During reasoning (declarative phase):
log:outputString behaves like a constraint-style builtin: it succeeds when its arguments are well-formed and sufficiently bound (notably, when the object is a string literal that can be emitted). Importantly, it does not print anything at this time. If a rule derives a triple like:
:k log:outputString "Hello\n".
then that triple simply becomes part of the fact base like any other fact.
log:outputString facts and renders them deterministically. Concretely, the CLI collects all such triples, orders them in a stable way (using the subject as a key so output order is reproducible), and concatenates their string objects into the final emitted text.This separation is not just an aesthetic choice; it preserves the meaning of logic search:
log:outputString facts can be traced back to the rules that produced them.In short: Eyeling makes log:outputString safe by refusing to treat it as an immediate effect. It is a declarative output fact whose concrete rendering is a final, deterministic post-processing step.
lib/deref.js)Some N3 workflows treat IRIs as pointers to more knowledge. Eyeling supports this with:
log:content — fetch raw textlog:semantics — fetch and parse into a formulalog:semanticsOrError — produce either a formula or an error literalderef.js is deliberately synchronous so the engine can remain synchronous.
In Node, dereferencing can read:
file:// URIs) via fs.readFileSyncIn browser/worker, dereferencing uses synchronous XHR, subject to CORS, and only for HTTP(S).
Dereferencing is cached by IRI-without-fragment (fragments are stripped). There are separate caches for:
This is both a performance and a stability feature: repeated log:semantics calls in backward proofs won’t keep refetching.
Eyeling can optionally rewrite http://… to https://… before dereferencing (CLI --enforce-https, or API option). This is a pragmatic “make more things work in modern environments” knob.
Once reasoning is done (or as it happens in streaming mode), Eyeling converts derived facts back to N3.
lib/printing.js)Printing handles:
PrefixEnvrdf:type as aowl:sameAs as =The printer is intentionally simple; it prints what Eyeling can parse.
When enabled, Eyeling prints a compact comment block per derived triple:
It’s a “why this triple holds” explanation, not a globally exported proof graph.
The engine’s reasonStream API can accept an onDerived callback. Each time a new forward fact is derived, Eyeling can report it immediately.
This is especially useful in interactive demos (and is the basis of the playground streaming tab).
Eyeling exposes itself in three layers.
eyeling.js)The bundle contains the whole engine. The CLI path is the “canonical behavior”:
lib/entry.js: bundler-friendly exportslib/entry.js exports:
reasonStream, main, versionlex, Parser, forwardChain, etc.)index.js: the npm API wrapperThe npm reason(...) function does something intentionally simple and robust:
node eyeling.js ... input.n3)This ensures the API matches the CLI perfectly and keeps the public surface small.
One practical implication:
reasonStream from the bundle entry rather than the subprocess-based API.Consider:
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix : <http://example.org/socrates#>.
:Socrates a :Human.
:Human rdfs:subClassOf :Mortal.
{ ?S a ?A. ?A rdfs:subClassOf ?B } => { ?S a ?B }.
What Eyeling does:
Parsing yields two facts:
(:Socrates rdf:type :Human)(:Human rdfs:subClassOf :Mortal)
and one forward rule:?S a ?A, ?A rdfs:subClassOf ?B?S a ?BForward chaining scans the rule and calls proveGoals on the body.
Proving ?S a ?A matches the first fact, producing { S = :Socrates, A = :Human }.
With that substitution, the second goal becomes :Human rdfs:subClassOf ?B.
It matches the second fact, extending to { B = :Mortal }.
Eyeling instantiates the head ?S a ?B → :Socrates a :Mortal.
The triple is ground and not already present, so it is added and (optionally) printed.
That’s the whole engine in miniature: unify, compose substitutions, emit head triples.
Eyeling is small, which makes it pleasant to extend — but there are a few invariants worth respecting.
Most extensions belong in evalBuiltin:
Decide if your builtin is:
{ varName: Term }, not full substitutions.If your builtin needs a stable view of the closure, follow the scoped-builtin pattern:
facts.__scopedSnapshotfacts.__scopedClosureLevel and priority gatingIf you add a new Term subclass, you’ll likely need to touch:
termToN3)unifyTerm, termsEqual, fast keys)gcCollectVarsInTerm)If you extend parsing, preserve the Rule invariants:
headBlankLabels must reflect blanks occurring explicitly in the head before skolemizationEyeling’s codebase is compact because it chooses one powerful idea and leans into it:
Use backward proving as the “executor” for forward rule bodies.
That design makes built-ins and backward rules feel like a standard library of relations, while forward chaining still gives you the determinism and “materialized closure” feel of Datalog.
If you remember only one sentence from this handbook, make it this:
Eyeling is a forward-chaining engine whose rule bodies are solved by a Prolog-like backward prover with built-ins.
Everything else is engineering detail — interesting, careful, sometimes subtle — but always in service of that core shape.
This appendix is a compact, user-facing reference for running Eyeling and writing inputs that work well. For deeper explanations and implementation details, follow the chapter links in each section.
Eyeling is distributed as an npm package.
Run without installing:
npx eyeling --help
npx eyeling yourfile.n3
Or install globally:
npm i -g eyeling
eyeling yourfile.n3
See also: Chapter 14 — Entry points: CLI, bundle exports, and npm API.
By default, Eyeling prints newly derived forward facts (the heads of fired => rules), serialized as N3.
It does not reprint your input facts.
For proof/explanation output and output modes, see:
The authoritative list is always:
eyeling --help
Options:
-a, --ast Print parsed AST as JSON and exit.
-d, --deterministic-skolem Make log:skolem stable across reasoning runs.
-e, --enforce-https Rewrite http:// IRIs to https:// for log dereferencing builtins.
-h, --help Show this help and exit.
-p, --proof-comments Enable proof explanations.
-r, --strings Print log:outputString strings (ordered by key) instead of N3 output.
-s, --super-restricted Disable all builtins except => and <=.
-t, --stream Stream derived triples as soon as they are derived.
-v, --version Print version and exit.
See also:
Eyeling implements a practical N3 subset centered around facts and rules.
A fact is a triple ending in .:
:alice :knows :bob .
A forward rule:
{ ?x :p ?y } => { ?y :q ?x } .
A backward rule:
{ ?x :ancestor ?z } <= { ?x :parent ?z } .
Quoted graphs/formulas use { ... }. Inside a quoted formula, directive scope matters:
@prefix/@base and PREFIX/BASE directives may appear at top level or inside { ... }, and apply to the formula they occur in (formula-local scoping).For the formal grammar, see the N3 spec grammar:
See also:
Eyeling supports a built-in “standard library” across namespaces like log:, math:, string:, list:, time:, crypto:.
References:
eyeling-builtins.ttl (in this repo)If you are running untrusted inputs, consider --super-restricted to disable all builtins except implication.
log:skolemWhen forward rule heads contain blank nodes (existentials), Eyeling replaces them with generated Skolem IRIs so derived facts are ground.
See:
log:semanticslog:content, log:semantics, and related builtins dereference IRIs and parse retrieved content.
This is powerful, but it is also I/O.
See:
Safety tip:
--super-restricted if you want to ensure no dereferencing (and no other builtins) can run.If you depend on Eyeling as a library, the package exposes:
reason(...)), andSee:
If you want to go deeper into N3 itself and the logic/programming ideas behind Eyeling, these are good starting points:
N3 / Semantic Web specs and reports:
Logic & reasoning background (Wikipedia):