A tiny triple store
File: triple-store.lisp
An RDF-style triple store with a pattern-matching query engine in under thirty lines: variables, unification against the store, and relational joins across patterns.
The code
1 ;; sbcl --script triple-store.lisp (in-memory RDF triple store)
2
3 ;; A store is a list of (subject predicate object) triples.
4 ;; To keep several stores, rebind it: (let ((*store* nil)) ...)
5 (defvar *store* nil)
6
7 (defun add-triple (s p o)
8 (push (list s p o) *store*))
9
10 (defun var-p (x)
11 (and (symbolp x) (char= (char (symbol-name x) 0) #\?)))
12
13 (defun match-triple (pat triple env)
14 "ENV extended so that PAT matches TRIPLE, or :FAIL."
15 (loop for p in pat for x in triple
16 do (cond ((var-p p)
17 (let ((b (assoc p env)))
18 (cond ((null b) (push (cons p x) env))
19 ((not (equal (cdr b) x)) (return :fail)))))
20 ((not (equal p x)) (return :fail)))
21 finally (return env)))
22
23 (defun query-triples (patterns &optional (store *store*) (envs '(())))
24 (if (null patterns) envs
25 (query-triples
26 (cdr patterns) store
27 (loop for env in envs
28 append
29 (loop for tr in store
30 for next-env = (match-triple (car patterns) tr env)
31 unless (eq next-env :fail) collect next-env)))))
32
33 ;; Demo: Knowledge graph of thinkers, occupations, and cities
34 (dolist (tr '((socrates :is-a :philosopher)
35 (socrates :lives-in :athens)
36 (plato :is-a :philosopher)
37 (plato :lives-in :athens)
38 (descartes :is-a :philosopher)
39 (descartes :lives-in :paris)
40 (monet :is-a :painter)
41 (monet :lives-in :paris)
42 (voltaire :is-a :writer)
43 (voltaire :lives-in :paris)))
44 (apply #'add-triple tr))
45
46 (format t "Knowledge base triples count: ~a~%" (length *store*))
47
48 (format t "~%Query 1: Who is a philosopher?~%")
49 (dolist (b (query-triples '((?who :is-a :philosopher))))
50 (format t " who: ~a~%" (cdr (assoc '?who b))))
51
52 (format t "~%Query 2: Who lives in Paris AND is a philosopher? ~
53 (relational join)~%")
54 (dolist (b (query-triples '((?who :lives-in :paris)
55 (?who :is-a :philosopher))))
56 (format t " who: ~a~%" (cdr (assoc '?who b))))
57
58 (format t "~%Query 3: All people in Paris and their occupations:~%")
59 (dolist (b (query-triples '((?person :lives-in :paris)
60 (?person :is-a ?job))))
61 (format t " ~a is a ~a in Paris~%"
62 (cdr (assoc '?person b))
63 (cdr (assoc '?job b))))
How it works
*store*is a list of(subject predicate object)triples;add-triplepushes onto it. To keep several stores, rebind the special variable withlet(see “Using it” below).- A “variable” is any symbol whose name starts with
?(var-p), a convention borrowed from SPARQL. match-tripleunifies one pattern with one triple under a binding environment (an alist): a bound variable must match its binding, an unbound variable gets bound, and a literal must beequal. It returns the extended environment or the keyword:fail.query-triplesperforms the join: it passes a list of environments through the pattern list. For each pattern it tries every triple against every live environment and keeps the successes. Two patterns that mention the same variable therefore act as a relational join.- The environments need no copying.
pushnever modifies the alist it receives; it adds a new cons in front of it. Each extended environment shares its tail with the environment it came from, so a failed match cannot disturb other environments. Lists used this way give you a persistent data structure at no cost.
Running it
1 $ sbcl --script triple-store.lisp
Actual output:
1 Knowledge base triples count: 10
2
3 Query 1: Who is a philosopher?
4 who: DESCARTES
5 who: PLATO
6 who: SOCRATES
7
8 Query 2: Who lives in Paris AND is a philosopher? (relational join)
9 who: DESCARTES
10
11 Query 3: All people in Paris and their occupations:
12 VOLTAIRE is a WRITER in Paris
13 MONET is a PAINTER in Paris
14 DESCARTES is a PHILOSOPHER in Paris
Query 2 shows the join: the first pattern binds ?who, and the second pattern succeeds only where that same binding is a philosopher. Only Descartes passes. Results appear in reverse insertion order because add-triple uses push, which adds each new triple to the front of the list.
Using it
This is the core of a small Datalog. Add recursive rules or negation-as-failure and you have a little logic language; load it with results from the SPARQL chapter and you can cache and join remote knowledge-graph data locally. For a handful of triples in a script, it is often all the “database” you need.
Because *store* is a special variable, a let gives you a scratch store that disappears when the let exits and leaves the global store alone. This REPL session also shows a two-hop join, where ?y links the two patterns:
1 * (let ((*store* nil))
2 (add-triple 'a :knows 'b)
3 (add-triple 'b :knows 'c)
4 (query-triples '((?x :knows ?y) (?y :knows ?z))))
5 (((?Z . C) (?Y . B) (?X . A)))