Metastrudel

A database where everything is an element.

Metastrudel stores a typed metagraph. Links, types, schemas, whole subgraphs and even your queries are stored elements with an identity, so you can point at any of them, type them and query them.

Design stage. The data model is specified in Lean 4 and the engine is planned in Rust. There is no release to install yet.

Two team contexts, each holding a person who owns an asset. A depends_on edge connects the two contexts, and both conform to the Team signature. Team : Signature conforms_to conforms_to team42 : Context owns ada : Person laptop : Asset owner item team7 : Context owns lin : Person server : Asset owner item depends_on
A metagraph. The green boxes are contexts: subgraphs that are elements themselves, so the depends_on edge connects two whole teams.

The idea

Most graph databases give identity to some things and not others. You can point at a node, but not at the fact that a person plays a role, or at a subgraph, or at the schema. Metastrudel removes that split.

One kind of thing

Everything stored is an element. An element is either an atom (pure identity) or a link from a subject to an object. Nodes, edges, attributes, types and schemas are all built from those two shapes.

Typing happens inside a context

A context groups elements and is the frame their types are judged in. The same person can be an Agent in one team and something else in another. Each context chooses to be open or closed by a schema.

Queries are data too

A query or rule is stored as a metagraph of its own. It has an id, a type and a history, and another query can search it.

What a metagraph adds

A graph connects nodes. A metagraph also lets an edge connect to another edge, to an attribute, or to a whole group of elements. Here is what that fixes compared with entity-relation-attribute models.

Usual limitIn Metastrudel
The link between a relation and its role player has no identity.A role is a link, and every link is an element you can reference.
You cannot say anything about an attribute value on a thing.An attribute is a link too, so it can carry links of its own, such as a source.
Entities, relations and attributes share no common root.One element structure with two shapes: atom and link.
A subgraph is not a value and queries are not scoped.A context is an element. Edges can connect contexts, and a query runs inside one.
Types and schema live outside the data.Types and signatures are elements in three layers: Data, Schema, Meta.
The schema is closed everywhere or nowhere.Each context is open or closed on its own, and signatures take type parameters.

Five terms

The whole model is built from these. Everything else, including nodes, edges, roles and attributes, is a bootstrap type defined with them.

Element
Anything stored with an identity. Every element has one type, one home context and a time.Elements are immutable. You assert one, and later retract it. There is no update.
Link
An element that connects a subject to an object, which is another element or a value. Its type is its predicate.Roles, attributes and memberships are all link types.
Type
An element that classifies elements in a lower layer.Each element has one declared type. Further types are derived inside a context by subtyping, bindings and rules.
Context
An element that groups other elements and frames how they are typed.Every element has exactly one home context, which records where it was asserted and never changes.
Signature
A context whose members are the types another context may use. It is the schema, stored as ordinary data one layer up.A context that conforms to a signature is closed. One that conforms to none is open.
MetaThe fixed prelude: Root, the base types, and eight primitive value types (Bool, Int, Dec, Str, Bytes, Time, Duration, Iri).
SchemaYour types, link types, signatures and parameters, plus the query vocabulary q:.
DataYour atoms and links, including stored queries.

Dependent types, kept decidable

Every typing question in Metastrudel has the form M ⊢ x : T: "inside context M, is x a T?" The answer depends on the context and on what that context binds. It can differ between contexts, and it is still checked on every write in bounded time.

Depends on the context

M ⊢ x : T

An element has one declared type, which is stored and global. Every other type is derived inside a context, through subtyping, bindings, refinements and rules. A query asks for derived types, so agent X means "an agent in this context".

Depends on a binding

Parameterised signatures

A signature can declare type parameters, much like generics. Each context that conforms to it binds them, so one signature describes a family of schemas. A binding is a link on the conforms_to link, so it is timed and can be retracted.

Depends on a value

Refinements

A type can narrow another type with a decidable predicate: a range, an enum, a length, or a regex that runs in linear time. A stronger predicate is a rule you write, and it runs at commit under a step budget, or offline.

One signature, two schemas

Bind the parameter per context

team says an owner must be an agent, but leaves open what an agent is. team42 binds it to people and fleet binds it to robots. The rule is written once and gives a different answer in each context.

signature team [
  param agent,
  type person, type robot, type asset,
  type owns (sub_of edge),
  role owner owns agent (card 1 1),
  role item  owns asset (card 1 inf)
].

conforms team42 team [agent := person].
conforms fleet  team [agent := robot].

% "an agent here": people in team42, robots in fleet
query agents_in M X :- M |- agent X.
Judgement or writeIn team42In fleet
ada : agent, where ada is a personholdsdoes not hold
r2 : agent, where r2 is a robotdoes not holdholds
Assert an owner link from an owns edge to adaacceptedrejected: range is agent
Assert a second owner on the same edgerejected: card 1 1rejected: card 1 1

Narrow a type by a value

Scalar user types are refinements of the eight primitives. The checker evaluates them on every write. A refinement on an atom type is evaluated only over the members of the context, never the whole database. The refinement syntax here is a sketch.

% a percentage is an Int between 0 and 100
type percent (refines int (range 0 100)).

% inside a signature: a share of an owns edge
attribute share owns percent (card 0 1).

What we left out

Metastrudel does not have full value-dependent types, where a type is computed from arbitrary stored values. Checking those is undecidable, so a single write could take unbounded time. Rules beyond the decidable fragment run on lean-elpi under a budget. If you write such a rule in LF, it produces a proof that the Lean kernel can check offline.

What dependent types give the database

In most databases the schema is one fence around all the data. In Metastrudel, typing is a judgement about elements in a context, and contexts are elements themselves. Typing becomes something the database can store, scope and query.

A schema per subgraph

Each context is open or closed on its own. Settled data sits in closed contexts that reject bad writes, and new data arrives in open ones. To close a context, you add one link: conforms_to.

Write a schema once, bind it many times

A parameterised signature is written once and bound per context. Rules belong to the signature that states them, so two signatures can share the link type owner with different cardinalities.

Many roles, one type

An element keeps one declared type, so the type index stays a single column. "Employee at Acme" is a type derived inside Acme, not a second type stored on the person.

Types about whole subgraphs

A context is an element, so "this context conforms to team" is an ordinary fact. Edges between contexts, and queries over those edges, can test it like any other fact.

Schema changes are data changes

Types, rules and bindings are timed links. You can ask what a context's schema was last month. Rebinding a parameter re-checks only the rules that name it.

Types as data, without paradox

Types are stored like data, but a Schema type classifies only Data, and the Meta layer is fixed. There is no Type : Type, so users cannot build a paradox.

Schema and queries

You write one language, the Metastrudel DSL. It uses Elpi (λProlog) syntax and reads like Datalog. The syntax below comes from the design documents and may still change.

Declare a signature

A signature lists the types a context may use. agent is a parameter: a type slot that each team fills in for itself. The cardinality rules belong to this signature only, so another signature can reuse owner with different rules.

signature team [
  param agent,
  type person, type asset,
  type owns (sub_of edge),
  role owner owns agent (card 1 1),
  role item  owns asset (card 1 inf),
  attribute name node str (card 1 1),
  member person (card 1 inf)
].

Close a context

team42 now has to satisfy team, with agent bound to person. Writes that break a rule are rejected with the rule's id. A context with no conforms line stays open and accepts anything.

conforms team42 team [agent := person].

Query inside a context

M |- G means: assume the facts of context M, then prove G. Here owns E asks for an element typed owns in that scope, and owner E X asks for a live owner link from E to X.

% who owns what, in one team
query owners_in M X A :-
  M |- ( owns E, owner E X, item E A,
         name X N, N != "" ).

Use contexts as values

A context is an element, so an edge can connect two of them. This finds pairs of teams where one depends on the other.

query linked_teams M1 M2 :-
  depends_on E, from E M1, to E M2,
  conforms M1 team, conforms M2 team.

Add your own typing rule

Derived types are rules. An active_agent in M is any agent of M whose status is active. Rules like this are opt-in and run at commit time under a budget.

of M X active_agent :-
  of M X agent, status X active.

Query the queries

Each pattern in a stored query is a q:Match atom whose slots link to live types. Finding every stored query that touches owns is an ordinary query, and renaming a type does not break the queries that use it.

% every stored pattern that mentions the type owns
query touches_owns A :-
  q:Match A, q:type A owns.
More of the language

Ask what is absent

not G means G has no proof from the context's members at the query's time. In an open context, an answer means "not known here" rather than "false". The planner can attach a note saying so, but it never rejects the query.

% people in team M who own nothing there
query idle M X :-
  M |- ( person X, not (owner E X) ).

Recurse over the graph

This rule is left-recursive. A top-down Prolog engine would loop on it, so the planner sends it to the native Datalog engine, which works bottom-up, one set at a time. You don't choose the engine: the planner routes each group of mutually recursive rules.

% transitive dependencies between teams
reaches A B :- depends_on E, from E A, to E B.
reaches A C :- reaches A B,
               depends_on E, from E B, to E C.

query upstream M U :- reaches M U, conforms U team.

Match the whole element

el L H T S O matches all five fields of a stored element: id, home, type, subject and object. Use it when you need a link's own id or the context it was asserted in. The home never changes, so it always records where a fact came from.

% each owner fact and the context it was asserted in
query owner_source E X H :-
  el L H owner E X.

State facts about facts

A name attribute is a link, and a link is an element, so another link can point at it. This query finds names together with the source that vouches for them.

% names that carry a source
query sourced_names X N S :-
  el L _ name X N, source L S.

Query the schema

A binding is a link whose subject is the conforms_to link itself. You can query bindings like any other data, for example to find every team where agents are people.

% contexts that bind agent to person
query person_teams M :-
  el C _ conforms_to M team,
  agent C person.

Reason hypothetically

F => G assumes F only while it proves G, and nothing is written. M |- G works the same way, with M's facts as the assumptions. A query like this runs on lean-elpi.

% would ada be an active agent in team42
% if its status were active?
query would_be_active :-
  status ada active =>
    of team42 ada active_agent.

Time comes from the read view

No query names a time. Any query can run as of now, as of a past transaction time ("what did the database know then?"), or at a bitemporal point ("what was true then, as known then?"). A read as of a past time gives the same result after every later commit.

One language, one stored form, two engines

You write

Metastrudel DSL

Elpi syntax with the Metastrudel vocabulary. Datalog is a subset of it, with no separate syntax.

Stored as

The q: metagraph

Programs, clauses, matches and calls are ordinary data. Ids are content hashes, so two queries that differ only in variable names are the same element.

Runs on

Scans, Datalog, lean-elpi

Matches map onto store scans. Each group of mutually recursive rules goes to the Datalog engine when it fits, and to lean-elpi otherwise.

Why another query language?

We tried to avoid it. Metastrudel needs five things from one language, and no existing query language provides all of them.

  1. Queries are stored in the same model as the data: as elements with ids, types and history, not as text.
  2. "Prove this inside context M" is a primitive operation, because every type and every query is judged in a context.
  3. Recursive rules run one set at a time over large graphs.
  4. Higher-order rules, with proofs, for typing rules the decidable checker cannot express.
  5. Patterns can reach links on links, and contexts used as values.
LanguageGood atWhat it lacks for Metastrudel
SQLMature planners, set-at-a-time evaluationData lives in tables, not a metagraph. There is no judgement scoped to a context, and a query is a string.
Cypher, GQLGraph pattern matchingA relationship cannot be the endpoint of another relationship, and a subgraph is not a value.
SPARQLNamed graphs, quoted triplesA named graph cannot be closed by a schema, and typing is not relative to it. You cannot define recursive rules, only property paths.
TypeQLTyped entities, relations and attributesThe links between a relation and its role players, and between an owner and its attribute, have no identity.
DatalogRecursion with clear semanticsNo hypothetical reasoning and no higher-order rules. Metastrudel keeps Datalog as a fragment of its language.
λProlog (Elpi)=> for hypotheses, pi for fresh names, higher-order rules, LF proofsNo storage model. Metastrudel adds one.
It is barely a new language

The syntax is Elpi's

The DSL is λProlog, a long-established logic language, plus a vocabulary for elements, links and contexts. Its => is the context judgement M ⊢ G, so the core of the type system needs no new syntax.

Datalog is a subset

Datalog rules are written in the same syntax and run on a native Datalog engine. Nothing has two spellings, and no engine brings syntax of its own.

One stored form

Everything compiles to the q: metagraph. SQL-style text dialects are not the goal, but SPARQL and Cypher subsets are planned as optional front-ends that compile into the same form.

Why lean-elpi

lean-elpi is an Elpi implementation written in Lean 4. The parser, the query codec, the interpreter and the kernel specification are therefore one codebase. The theorem that encoding then decoding a query gives it back is proved about the code that actually runs, not about a model of it.

What the design commits to

A specification you can check

The kernel model is written in Lean 4. The Rust implementation is tested against the Lean reference on the same inputs.

Two timelines

Every element has a valid time and a transaction time. You can ask what was true then, and what the database knew then.

Predictable writes

Write-time type checking stays in a decidable fragment with a known cost. Stronger rules are opt-in.