graphlava

Chapter 7

Containers: named and nested graphs

The second metagraph power: a vertex that holds a subgraph and is still a vertex. In a statement graph that vertex is a named graph. This chapter covers membership, nesting, scope, and how to make an edge hold a subgraph.

A graph is a node

A named graph groups statements. In RDF the name is an IRI, and membership is the fourth column of a quad. In a statement graph, membership is one more statement:

(s1, :in, :session7)          # s1 is in the episode
(:session7, :in, :aliceMemory) # the episode is in Alice's memory

Because the graph's name is a term, the graph can be described like anything else: (:session7, :startedAt, "2026-11-20T09:00Z"). That is Gapanyuk's metavertex: it contains statements, and it is a vertex.

In graphlava a graph has its own ID kind (tag 0x5). Writing into a graph uses standard SPARQL:

PREFIX : <http://example.org/>
INSERT DATA {
  GRAPH :session7 {
    :alice :worksAt :acme {| :confidence 0.9 ; :source :chat12 |} .
  }
}

Membership that costs nothing

Most statements live in exactly one graph. graphlava's spec makes that case free. IDs are allocated in chunks of 220 consecutive IDs, and each chunk is leased to one graph and one kind of item. The home graph of a statement is a function of its ID: look up the chunk. No membership row is stored.

MembershipStored asCost per item
Home graph (one per item, fixed at allocation)the ID's chunk, via gchunk0 bytes
Extra graphone key in gmem (g, item) and one in gmem_rev (item, g)2 keys

So "all statements in :session7" is a set of ID ranges, not a scan. A statement learned in the session and also filed under a project belongs to both: home :session7, extra :pricingProject.

The membership triple (x, :in, g) is virtual, like the other virtual statements of chapter 6. It has no statement ID until something annotates it (ADR 0017). Annotate it, "Bob filed this under the project", and the spec allocates an ID and stores it as the gmem value.

One statement, many graphs

A fact keeps one ID however many graphs hold it (ADR 0024). Graph clauses then behave like SPARQL datasets, without a copy per graph:

WriteEffect (spec)
INSERT an existing triple into another graphAdds a membership of that graph. Same statement, same ID, same annotations.
DELETE a triple inside GRAPH gRemoves only the membership in g. The statement is deleted, with its cascade, when its last membership goes.
Move from g to hAdds h, removes g, in one transaction. The ID and home chunk stay; a moved tombstone in gmem hides the old home membership.
DROP GRAPH gRemoves every membership in g. Items still in another graph survive; the rest are deleted.

Two episodes can assert the same fact and each keep its own metadata. Per-episode provenance goes on the promoted membership, not on the fact:

# one fact, two memberships; the spec writes a membership as << X :in g >>
s1 = (:alice, :worksAt, :acme)
m7 = (s1, :in, :session7)        # promoted when annotated
m9 = (s1, :in, :session9)
(m7, :confidence, 0.9)           # session 7 was sure
(m9, :confidence, 0.6)           # session 9 less so

Uniqueness of (s, p, o) holds per store: the global tables, or one partitioned graph's own tables. A triple stored both in a partition and globally therefore has two IDs, and a query over both deduplicates it.

Nesting is a DAG

Graphs nest through (child, :in, parent). graphlava allows several parents and forbids cycles:

:agentMemory :aliceMemory :pricingProject :session7 s1
Each line joins a parent (above) to a child (below). :session7 has two parents, and :pricingProject sits directly under :agentMemory. The statement s1 is a direct member of :session7 only, and in the scope of all four graphs.
(:session7,      :in, :aliceMemory)
(:session7,      :in, :pricingProject)
(:aliceMemory,   :in, :agentMemory)
(:pricingProject,:in, :agentMemory)
Rule (spec)Effect
No cyclesAdding (a, :in, b) fails with GraphCycle when a = b or b is already under a. The check is one point read.
Depth limitDefault 64 levels, configurable; past it, GraphDepthExceeded.
Closure kept currentin_star(descendant, ancestor) holds the transitive closure as a set of pairs, updated in the writer transaction. There are no path counts (ADR 0029): removing a parent link recomputes the ancestors of the graphs below it, so one of two parent links can go without losing a still-reachable ancestor.

The :in above is the spec's shorthand. The IRI of the system's membership predicate is still an open question.

Scope: what a GRAPH clause sees

The scope of a graph is the graph and every graph nested under it. graphlava's GRAPH matches the scope by default (ADR 0007). A new keyword, GRAPH!, matches direct members only.

SyntaxMatchess1 found via
GRAPH :agentMemory { P }statements in the graph or anywhere under it, each onceyes
GRAPH! :agentMemory { P }direct members onlyno
GRAPH ?g { P }binds ?g to each direct graph of the match, never an ancestor?g = :session7
FROM :gthe default graph is the scope of :g
# everything the agent believes about employers, at any depth
SELECT ?who ?org WHERE { GRAPH :agentMemory { ?who :worksAt ?org } }

# which episode did each fact come from, and which memories hold that episode?
SELECT ?who ?ep ?mem WHERE {
  GRAPH ?ep { ?who :worksAt :acme }
  ?ep :in* ?mem .
}

Standard SPARQL means direct membership. A connection flag, graph_scope = direct, restores it, and the W3C test suites run with it.

How a scope is read

The planner picks one of three plans per scoped pattern, from per-graph statistics:

PlanWhenCost
range-driventhe scope is small: read its ID ranges, filter by the patternO(items in scope)
filter-driventhe pattern is selective and the scope large: scan the pattern, test each ID against the scope bitmapO(matches)
partitionedthe graph was declared partitioned and has its own tablesO(matches in g)

A query over one small episode costs in proportion to the episode, not the database. DROP GRAPH g removes every membership in g: items that are still in another graph survive, the others are deleted and cascade to their annotations. DROP RECURSIVE GRAPH g also drops descendants that have no other surviving parent.

Edges as containers

Combine both powers and an edge can hold a subgraph: the meeting e1 holds its minutes. graphlava's graph IDs and hyperedge IDs are different kinds, so a hyperedge or statement is not itself a graph name. The pattern is a graph linked to the edge:

# the minutes of meeting e1 are a graph; ?e is the meeting's hyperedge ID
INSERT { ?e :minutes :e1minutes . }
WHERE  { ?e a :Meeting ; :agent :alice ; :target :bob ; :topic :pricing }

INSERT DATA { GRAPH :e1minutes { :bob :proposed :discount10 . } }

# open the meeting
SELECT ?s ?p ?o WHERE {
  ?e a :Meeting ; :agent :alice ; :minutes ?m .
  GRAPH ?m { ?s ?p ?o }
}

The link (e1, :minutes, :e1minutes) has e1 as subject, so deleting the meeting deletes the link by cascade. The minutes graph itself stays until dropped. tiramemsu goes one step further: a graph can be named by a statement, GRAPH <urn:tiramemsu:stmt:1> { … }, so the container follows the edge through corrections and goes with a retraction. In graphlava, make the link part of the edge's life explicitly: drop the minutes graph in the same transaction that deletes the meeting.

In graphlava. Graphs are nodes with their own ID kind. Home membership is free, read from the ID's chunk; extra memberships cost two keys. Membership triples are virtual until annotated. One statement can be in several graphs; deleting inside a graph removes only that membership, and per-episode metadata goes on << X :in g >>. Nesting is a DAG with cycle and depth checks and a maintained closure. GRAPH :g sees the whole scope, GRAPH! :g only direct members, and GRAPH ?g binds direct graphs.

Takeaways

Contents