Skip to content

Embedding SPARQL Queries

Andrew Matthews edited this page Oct 1, 2025 · 1 revision

While thinking about Graph Restructuring Comprehensions I touched on SPARQL queries a few times. A natural pattern started to arise within statements like these:

g: graph = <<{
  PREFIX foaf:   <http://xmlns.com/foaf/0.1/>
  PREFIX org:    <http://example.com/ns#>
  PREFIX x:    <http://example.com/x#>

  CONSTRUCT { ?p x:name ?name; x:age ?age }
  WHERE  { ?p org:uid ?name; foaf:age ?age }
}>> from store;

The syntax:

<<{some query}>> from store

There are a few options that might look nice:

  1. The plain old functional syntax, treating the store as just another object.
store.query("""
  some query
  """)

This is familiar, but has the SPARQL query as just a string, making strong type support harder since it would require the addition of string interpolation syntax.

  1. Erlang generator-style syntax
<<{some query}>> <:- store

The specific symbol doesn't matter particularly, <:- could just as easily be <- as in other languages. But the point could be to define a source for the query, and a means to define multiple such sources if need be:

g1: graph = . . . ;
g2: graph = . . . ;

g3: graph = <<{some query}>> <:- g1;
g4: graph = <<{some other query}>> <:- g2;
g5: graph = g3 + g4;

queries themselves shoiuld be first class citizens in the type system, perhaps:

rq1: query = <<{some query}>>;
rq2: query = <<{some other query}>>;

g1: graph = . . . ;
g2: graph = . . . ;
g3: graph = rq1 <- g1;
g4: graph = rq2 <- g2;
g5: graph = g3 + g4;

Which would certainly help to make graph construction and manipulation clean. Composition of graphs would allow the formation of a single source graph during query. At what cost, I'm not sure.

List comprehensions could follow the Erlang approach, with multiple generators defined in the comprehension, then we might allow something that forms a single source graph out of the generators defined in the comprehension, like so:

people: [Person] = [{ . . . }: Person from g4 + g5] ;
people2: [Person] = [{ . . . }: Person from g4 + g5 
  where g4 <- rq1 from g1, g5 <- rq2 from g2] ;

The emphasis here, is very much on the construction and manipulation of whole graphs. See Graph Restructuring Comprehensions for syntax that relates to getting from a source graph type to an object or list of objects.

To acheive this kind of syntax we need the following:

  1. Native data types for triple, and graph.
  2. Composition of graphs and triples.
  3. Native data types for query
  4. List Comprehensions mapping onto LINQ Select Statements
  5. from syntax for application of a query to a graph.
  6. <- (generator let decl ??) for declaring a comprehension generator
Clone this wiki locally