w3c / N3

W3C's Notation 3 (N3) Community Group
48 stars 18 forks source link

Language-integrated Semantic Templates #110

Open AdamSobieski opened 1 year ago

AdamSobieski commented 1 year ago

Hello. I would like to propose language-integrated semantic templates for N3. Ways of delivering semantic templates for users include: (1) the semantic modeling of templates, resembling OTTR and/or FnO, combined with existing N3 processing and rules, and (2) new language-integrated approaches, proposed here.

Language-integrated approaches could utilize the proposed URI query string mapping syntax, perhaps resembling:

@prefix list:  <http://www.w3.org/2000/10/swap/list#> .  
@prefix log:   <http://www.w3.org/2000/10/swap/log#> .  
@prefix ex:    <http://example.org/#> .  
@prefix graph: <http://www.w3.org/2000/10/swap/graph#> . 

### example template definition
ex:ng??(?x ?y) :graph  
{  
    ?x foaf:knows ?y .  
    ?y foaf:knows ?x .  
} .  

### example usage of template
{  
    ex:ng??(ex:alice ex:bob) :graph ?graph .  
    ?graph graph:member ?stmt .  
    ?stmt log:equalTo { ?s ?p ?o } .  
}  
=>  
{   
    ?s ?p ?o .      
} .  

The idea is that the template definition declares that any named graph URI matching the syntactic pattern maps to a provided graph with the variables bound to provided template arguments.

Any thoughts on this proposed N3 language feature? Thank you.


See also: https://github.com/w3c-cg/planning/discussions/27#discussioncomment-4388356

william-vw commented 1 year ago

I think this would be a nice extension of N3.

Currently, I think it's tricky to do this in N3. (I made an effort here).

We'd have to allow rules to "pull-in" universals from the outer scope (without skolemizing them), or alternatively, use free variables in the graph template. Perhaps we need a new builtin as well to bind the "template" variables, but I'm unsure.

Any thoughts from @josd and @doerthe ?

josd commented 1 year ago

If you group the template triples in a graph like done at http://ppr.cs.dal.ca:3002/n3/editor/s/Dpa3chEt it gives the expected result:

<http://n3-editor.org/out> (<http://n3-editor.org/Bob> <http://n3-editor.org/Alice>).
<http://n3-editor.org/Bob> a foaf:Person.
<http://n3-editor.org/Alice> a foaf:Person.
<http://n3-editor.org/Bob> foaf:knows <http://n3-editor.org/Alice>.
<http://n3-editor.org/Alice> foaf:knows <http://n3-editor.org/Bob>.
william-vw commented 1 year ago

Nice!

AdamSobieski commented 1 year ago

There are other syntactic possibilities to consider for defining and invoking language-integrated semantic templates.

Other syntactic possibilities for defining language-integrated semantic templates include, but are not limited to:

  1. ex:template??(?x ?y) owl:sameAs { ... } .
  2. ex:template??(?x ?y) = { ... } .
  3. @define ex:template??(?x ?y) { ... } .
  4. @define ex:template??(?x ?y) = { ... } .
  5. @let ex:template??(?x ?y) { ... } .
  6. @let ex:template??(?x ?y) = { ... } .
  7. ex:template??(?x ?y) special:dereferenceWith { ... } .

Option 7 suggests a potential relationship between dereferencing a URI and invoking a semantic template. In this possible approach, before accessing the Web or a cache to dereference a URI, systems would check to see if there were a dereferencing operator overload or other defined content or functionality for the URI or URI pattern. That is, the syntax for invoking a template and for obtaining a graph from a URI could be identical or similar.

@josd’s example indicates to me that semantic templates are doable with N3.

There could be created multiple ways of doing semantic templating with its set of interoperating features to be determined (e.g., dereferencing, @requires, @if/@then/@else, @union/@intersection, ...), some ways syntactically sugary and other ways more advanced and potentially customizable. If the group wants new syntax for language-integrated semantic templates, there are options to consider beyond the one used to introduce this topic and beyond those other alternatives listed above. I'm confident that we would find the best syntactic approaches with which to provide both new and experienced N3 language users with the convenient utilization of the broader set of interoperating features.

william-vw commented 1 year ago

@AdamSobieski At this point, I'd propose creating a syntactic and semantic extension of N3 for the purpose of these semantic templates (including ancilliary constructs such as if/then/else and requires). Afterwards, they could be added to the N3 core.

Just a thought: maybe you could setup a new git repo for grouping all materials regarding this extension. This will allow others to get an easy overview and contribute solutions & ideas themselves. I'm sure the N3 community will help out where needed; at least, I will do my best!

Regarding semantics, I would suggest starting with an N3 serialization of your ideas (using the current N3 grammar, as here) and attempt to implement them using N3 rules. This will make it clear where N3 may be lacking in support (e.g., lack of suitable builtins).

Regarding syntax, you can find the current grammar rules in the spec and the ANTLR4 file on github (the EBNF file is in the same folder).

AdamSobieski commented 1 year ago

Thank you @william-vw. I will explore N3's semantics and, alongside any interested others, put together a fuller extension proposal for the group.

It appears that there are three main subtopics to consider: template definition, invocation, and reflection.

  1. Template definition: how do language users define templates?
  2. Template invocation: how do language users invoke templates?
  3. Template reflection: will there be support for metaprogramming?