<?xml version="1.0" encoding="UTF-8"  standalone="no"?>
<?xml-stylesheet type="text/xsl" href="http://www.w3.org/Math/XSL/mathml.xsl"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
 <title>SPARQL definitions in MathML and Notation3</title>
</head>
<body>
<h1>Formalizing SPARQL rules</h1>

<p>Noodling on using <a href="http://www.w3.org/Math/">MathML</a>
for the definitions in the <a href="rq23/">SPARQL</a> spec, and
deriving N3 from it.</p>

<dl>
<dt>RDF Term</dt>
<dd>
  <blockquote>The set of RDF Terms, RDF-T, is RDF-U union RDF-L union
  RDF-B</blockquote>

  <math xmlns="http://www.w3.org/1998/Math/MathML"> 
    <declare type="set">
      <ci> RDF_T </ci>
      <apply> 
	<union/> 
	<ci> RDF_U </ci> 
	<ci> RDF_L </ci> 
	<ci> RDF_B </ci> 
      </apply> 
    </declare>
  </math>
</dd>

<dt>Query Variable</dt>
<dd>
  <blockquote> Let V be the set of all query variables.  V and RDF-T are disjoint.</blockquote>
  <p><em>hmm... mathml can say "the intersection of V and RDF_T is empty" but not "V and RDF_T are disjoint." Maybe it's extensible... ah yes... <a href="http://www.w3.org/TR/MathML2/chapter4.html#contm.csymbol">csymbol</a>. Hmm... encodings aren't grounded in URI space.</em></p>

  <math xmlns="http://www.w3.org/1998/Math/MathML"> 
    <apply> 
      <csymbol definitionURL="http://www.w3.org/2002/07/owl#disjointFrom"
	       encoding="RDF">disjointFrom</csymbol>
      <ci> V </ci> 
      <ci> RDF_T </ci> 
    </apply> 
  </math>
</dd>

<dt>Triple Pattern</dt>
<dd>
<blockquote>
 The set of triple patterns  is
    (RDF-U union RDF-B union V) x (RDF-U union V) x (RDF-T union V)
</blockquote>
<p><em>sent comment: let's use the same terms everywhere@@</em>.
Term Pattern is RDF-T union V.
</p>

<p><em>@@hmm... Triple Pattern isn't needed is, it? We can skip
right to Graph Pattern, no?</em></p>

  <math xmlns="http://www.w3.org/1998/Math/MathML"> 
    <declare type="set">
      <ci> TermPattern </ci>
      <apply>
	<union />
	<ci> RDF_T </ci> 
	<ci> V </ci> 
      </apply>
    </declare>
  </math>

  <math xmlns="http://www.w3.org/1998/Math/MathML"> 
    <declare type="set">
      <ci> TriplePattern </ci>
    <apply>
      <cartesianproduct/>
      <ci> TermPattern </ci> 
      <ci> TermPattern </ci> 
      <ci> TermPattern </ci> 
    </apply>
    </declare>
  </math>
</dd>

<dt>Binding</dt>
<dd><em>skipping. obsoleted by <dfn>substitution</dfn></em>
</dd>

<dt>Substitution</dt>
<dd><blockquote>
A substitution S is a partial functional relation from variables to RDF terms or variables. We write S[v] for the RDF term that S pairs with the variable v and define S[v] to be v where there is no such pairing.
<p><em>@@more on: extending substitution of terms to triples and graphs. Oops! so codomain isn't right; we want onProperty/allValuesFrom</em></p>
<p><em>@@move this up:</em>
For substution S and Triple Pattern T, S(T) is the triple pattern forms by replacing any variable v in T with S[v].</p>

</blockquote>


<!-- 
  <math xmlns="http://www.w3.org/1998/Math/MathML"> 
      <apply>
	<forall/> 
	<bvar> 
	  <ci>S</ci> 
	</bvar>
	<apply>
	  <implies/> 
	  <apply><ci>Substitution</ci><ci>S</ci></apply>
	  <apply>
	    <and />
	    <apply>
	      <domain />
	      <ci>S</ci>
	      <ci>V</ci>
	    </apply>
	    <apply>
	      <codomain />
	      <ci>S</ci>
	      <ci>TermPattern</ci>
	    </apply>
	  </apply>
	</apply> 
      </apply>
  </math>
-->

</dd>

<dt>Triple Pattern Matching</dt>
<dd>
<blockquote>
Triple Pattern T matches RDF graph G with substitution S, if S(T) is a triple of G.
</blockquote>

    <math xmlns="http://www.w3.org/1998/Math/MathML"> 
      <apply>
	<forall/> 
	<bvar><ci>T</ci></bvar>
	<bvar><ci>G</ci></bvar>
	<bvar><ci>S</ci></bvar>
	<condition>
	  <apply>
	    <and/>
	    <apply><ci>TriplePattern</ci><ci>T</ci></apply>
	    <apply><ci>RDF_Graph</ci><ci>G</ci></apply>
	    <apply><ci>Substitution</ci><ci>S</ci></apply>
	  </apply>
	</condition>
	<apply>
	  <implies/> 
	  <apply>
	    <in />
	    <apply>
	      <ci>S</ci>
	      <ci>T</ci>
	    </apply>
	    <ci>G</ci>
	  </apply>
	  <apply>
	    <apply>
	      <ci>matches</ci>
	      <ci>S</ci>
	    </apply>
	    <ci>T</ci>
	    <ci>G</ci>
	  </apply>
	</apply>
      </apply> 
    </math>


</dd>

<dt>Graph Pattern (Partial Definition) -- Conjunction</dt>
<dd>
<blockquote>
A set of triple patterns is a graph pattern GP.
</blockquote>

    <math xmlns="http://www.w3.org/1998/Math/MathML"> 
      <apply>
	<forall />
	<bvar><ci>GP</ci></bvar>
	<apply>
	  <implies/>
	  <apply><subset/><ci>GP</ci><ci>TriplePattern</ci></apply>
	  <apply><ci>GraphPattern</ci><ci>GP</ci></apply>
	</apply>
      </apply>
    </math>
</dd>

<dt>Graph Pattern Matching</dt>
<dd>
<blockquote>
Graph Pattern GP matches RDF graph G with substitution S if S(GP) is a subgraph of G.
</blockquote>

    <math xmlns="http://www.w3.org/1998/Math/MathML"> 
      <apply>
	<forall/> 
	<bvar><ci>GP</ci></bvar>
	<bvar><ci>G</ci></bvar>
	<bvar><ci>S</ci></bvar>
	<condition>
	  <apply>
	    <and/>
	    <apply><ci>GraphPattern</ci><ci>GP</ci></apply>
	    <apply><ci>RDF_Graph</ci><ci>G</ci></apply>
	    <apply><ci>Substitution</ci><ci>S</ci></apply>
	  </apply>
	</condition>
	<apply>
	  <implies/> 
	  <apply>
	    <!-- @@http://www.w3.org/TR/rdf-mt/#defentail is a more standard anchor... -->
	    <csymbol
		definitionURL="http://www.w3.org/2000/10/swap/log#includes"
		encoding="RDF">entailsSimply</csymbol>
	    <ci>G</ci>
	    <apply>
	      <ci>S</ci>
	      <ci>GP</ci>
	    </apply>
	  </apply>
	  <apply>
	    <apply>
	      <ci>matches</ci>
	      <ci>S</ci>
	    </apply>
	    <ci>GP</ci>
	    <ci>G</ci>
	  </apply>
	</apply>
      </apply> 
    </math>

</dd>

<dt>Pattern Solution</dt>
<dd>
  <blockquote>
    A Pattern Solution of Graph Pattern GP on graph G is any substitution
    S such that GP matches G with S.
    
    <p>For a graph pattern GP formed as a set of triple patterns, S(G),
    has no variables and is a subgraph of G.</p>
  </blockquote>
  <math xmlns="http://www.w3.org/1998/Math/MathML"> 
    <apply>
      <forall/> 
      <bvar><ci>GP</ci></bvar>
      <bvar><ci>G</ci></bvar>
	<bvar><ci>S</ci></bvar>
	<condition>
	  <apply>
	    <and/>
	    <apply><ci>GraphPattern</ci><ci>GP</ci></apply>
	    <apply><ci>RDF_Graph</ci><ci>G</ci></apply>
	    <apply><ci>Substitution</ci><ci>S</ci></apply>
	  </apply>
	</condition>
	<apply>
	  <implies />
	  <apply>
	    <apply><ci>matches</ci><ci>S</ci></apply>
	    <ci>GP</ci>
	    <ci>G</ci>
	  </apply>
	  <apply>
	    <apply><ci>patternSolution</ci><ci>G</ci></apply>
	    <ci>GP</ci>
	    <ci>S</ci>
	  </apply>
	</apply>
    </apply>
  </math>
</dd>


<dt>Query Results</dt>
<dd>
  <blockquote>
    The Query Results, for a given graph pattern GP on G, is written R(GP,G), and is the set of all query solutions such that GP matches G.
    
    <p>R(GP, G) may be the empty set.</p>
  </blockquote>

  <p><em>@@hmm... I don't think we can write "the set of all S such
  that..." in N3 yet.</em></p>

  <math xmlns="http://www.w3.org/1998/Math/MathML"> 
    <apply>
      <csymbol
	  definitionURL="http://www.w3.org/2002/07/owl#FunctionalProperty"
	  encoding="RDF">FunctionalProperty</csymbol>
      <ci>R</ci>
    </apply>
  </math>

  <math xmlns="http://www.w3.org/1998/Math/MathML"> 
    <apply>
      <forall />
      <bvar><ci>GP</ci></bvar>
      <apply>
	<implies/>
	<apply><ci>GraphPattern</ci><ci>GP</ci></apply>
	<apply>
	  <csymbol
	      definitionURL="http://www.w3.org/2002/07/owl#FunctionalProperty"
	      encoding="RDF">FunctionalProperty</csymbol>
	  <apply><ci>R</ci><ci>GP</ci></apply>
	</apply>
      </apply>
    </apply>
  </math>

  <math xmlns="http://www.w3.org/1998/Math/MathML"> 
    <apply>
      <forall />
      <bvar><ci>GP</ci></bvar>
      <bvar><ci>G</ci></bvar>
      <bvar><ci>S</ci></bvar>
      <condition>
	<apply>
	  <and />
	    <apply><ci>GraphPattern</ci><ci>GP</ci></apply>
	    <apply><ci>RDF_Graph</ci><ci>G</ci></apply>
	    <apply><ci>Substitution</ci><ci>S</ci></apply>
	</apply>
      </condition>

      <apply>
	<implies /> <!-- @@iff -->
	<apply>
	  <in />
	  <ci>S</ci>
	  <apply>
	    <apply><ci>R</ci><ci>GP</ci></apply>
	    <ci>G</ci>
	  </apply>
	</apply>
	<apply>
	  <apply><ci>matches</ci><ci>S</ci></apply>
	  <ci>GP</ci>
	  <ci>G</ci>
	</apply>
      </apply>
    </apply>
  </math>
</dd>

<dt>Optional Matching</dt>
<dd>
<blockquote>
<p>Given graph pattern GP1, and graph pattern GP2, let GP= (GP1 union GP2).</p>

<p>The optional match of GP2 of graph G, given GP1, defines a pattern solution PS such that:</p>

<p>If GP matches G, then the solutions of GP is the patterns solutions of GP else the solutions are the pattern solutions of GP1 matching G.</p>
</blockquote>

    <math xmlns="http://www.w3.org/1998/Math/MathML"> 
      <apply>
	<forall/> 
	<bvar><ci>GP1</ci></bvar>
	<bvar><ci>GP2</ci></bvar>
	<bvar><ci>GP</ci></bvar>
	<bvar><ci>G</ci></bvar>
	<bvar><ci>PS</ci></bvar>
	<condition>
	  <apply>
	    <and/>
	    <apply><ci>GraphPattern</ci><ci>GP1</ci></apply>
	    <apply><ci>GraphPattern</ci><ci>GP2</ci></apply>
	    <apply><ci>RDF_Graph</ci><ci>G</ci></apply>
	    <apply><ci>Substitution</ci><ci>S</ci></apply>
	  </apply>
	</condition>
	@@
      </apply>
    </math>
</dd>

</dl>


<h2>Appendix: MathML as a semantic web rules language</h2>
<p>Let's take the classic syllogism, represent it in MathML, and then
convert it to N3 via XSLT (<a
href="mathmlRules.xsl">mathmlRules.xsl</a>), and then back using the
n3.n3@@link parser</p>

<ul>
 <li>
  <ul>
  <li>All men are mortal</li>
  <li>Socrates is a man</li>
  </ul>
 </li>
 <li>Socrates is mortal</li>
</ul>

<p>All men are mortal:</p>

    <math xmlns="http://www.w3.org/1998/Math/MathML"> 
      <apply>
	<forall/> 
	<bvar> 
	  <ci>x</ci> 
	</bvar>
	<apply>
	  <implies/> 
	  <apply><ci>Man</ci><ci>x</ci></apply>
	  <apply><ci>Mortal</ci><ci>x</ci></apply>
	</apply>
      </apply> 
    </math>

<p>Socrates is a man:</p>

    <math xmlns="http://www.w3.org/1998/Math/MathML"> 
	  <apply><ci>Man</ci><ci>s</ci></apply>
    </math>


<hr />

<p>another take at socrates, generated <em>from</em> N3:</p>

<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<forall/>
<bvar><ci id="who">who</ci></bvar>
<apply><and/>
<apply><ci>holds</ci>
<csymbol definitionURL="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" encoding="RDF"><ci>type</ci></csymbol>
<csymbol definitionURL="file:/home/connolly/w3ccvs/WWW/2000/10/swap/test/reason/socrates.n3#socrates" encoding="RDF"><ci>socrates</ci></csymbol>
<csymbol definitionURL="file:/home/connolly/w3ccvs/WWW/2000/10/swap/test/reason/socrates.n3#Man" encoding="RDF"><ci>Man</ci></csymbol>
</apply>
<apply><implies/>
<apply><ci>holds</ci>
<csymbol definitionURL="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" encoding="RDF"><ci>type</ci></csymbol>
<ci definitionURL="#who">who</ci>
<csymbol definitionURL="file:/home/connolly/w3ccvs/WWW/2000/10/swap/test/reason/socrates.n3#Man" encoding="RDF"><ci>Man</ci></csymbol>
</apply>
<apply><ci>holds</ci>
<csymbol definitionURL="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" encoding="RDF"><ci>type</ci></csymbol>
<ci definitionURL="#who">who</ci>
<csymbol definitionURL="file:/home/connolly/w3ccvs/WWW/2000/10/swap/test/reason/socrates.n3#Mortal" encoding="RDF"><ci>Mortal</ci></csymbol>
</apply>
</apply>
</apply>
</apply>
</math>

<hr />
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<forall/>
<bvar><ci id="s">s</ci></bvar>
<apply><and/>
<apply><ci>holds</ci>
<csymbol definitionURL="file:/home/connolly/w3ccvs/WWW/2000/10/swap/test/unify/reflexive.n3#talksTo" encoding="RDF"><mi>talksTo</mi></csymbol>
<csymbol definitionURL="file:/home/connolly/w3ccvs/WWW/2000/10/swap/test/unify/reflexive.n3#Steve" encoding="RDF"><mi>Steve</mi></csymbol>
<csymbol definitionURL="file:/home/connolly/w3ccvs/WWW/2000/10/swap/test/unify/reflexive.n3#Joe" encoding="RDF"><mi>Joe</mi></csymbol>
</apply>
<apply><implies/>
<apply><ci>holds</ci>
<csymbol definitionURL="file:/home/connolly/w3ccvs/WWW/2000/10/swap/test/unify/reflexive.n3#talksTo" encoding="RDF"><mi>talksTo</mi></csymbol>
<ci definitionURL="#s">s</ci>
<ci definitionURL="#s">s</ci>
</apply>
<apply><ci>holds</ci>
<csymbol definitionURL="file:/home/connolly/w3ccvs/WWW/2000/10/swap/test/unify/reflexive.n3#admits" encoding="RDF"><mi>admits</mi></csymbol>
<ci definitionURL="#s">s</ci>
<ms>I talk to myself</ms>
</apply>
</apply>
</apply>
</apply>
</math>

<hr />
<address>DanC@@ <br />
<small>$Id: mathml-rules.xml,v 1.25 2006/09/29 04:42:20 connolly Exp $</small>
</address>
</body>
</html>
