Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added sister/2 predicate in Prolog notes.

  • Loading branch information...
commit 8e97fc7c96213a0621ad0bf0145b5846d1cf395d 1 parent 6dc3dd2
@joshuaeckroth authored
View
2  learning.html
@@ -7,7 +7,7 @@
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"/>
<meta name="title" content="Learning"/>
<meta name="generator" content="Org-mode"/>
-<meta name="generated" content="2012-04-22 23:20:10 EDT"/>
+<meta name="generated" content="2012-04-23 07:19:44 EDT"/>
<meta name="author" content="Joshua Eckroth"/>
<meta name="description" content=""/>
<meta name="keywords" content=""/>
View
2  planning.html
@@ -7,7 +7,7 @@
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"/>
<meta name="title" content="Planning"/>
<meta name="generator" content="Org-mode"/>
-<meta name="generated" content="2012-04-22 23:20:09 EDT"/>
+<meta name="generated" content="2012-04-23 07:19:43 EDT"/>
<meta name="author" content="Joshua Eckroth"/>
<meta name="description" content=""/>
<meta name="keywords" content=""/>
View
91 prolog.html
@@ -7,7 +7,7 @@
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"/>
<meta name="title" content="Prolog"/>
<meta name="generator" content="Org-mode"/>
-<meta name="generated" content="2012-04-22 23:20:08 EDT"/>
+<meta name="generated" content="2012-04-23 07:19:43 EDT"/>
<meta name="author" content="Joshua Eckroth"/>
<meta name="description" content=""/>
<meta name="keywords" content=""/>
@@ -456,6 +456,81 @@ <h2 id="sec-2">Defining complex predicates (rules)</h2>
<p>
+Let&rsquo;s try defining the sister relationship:
+</p>
+
+
+
+<pre class="src src-prolog"><span style="color: #268bd2;">sister</span>(<span style="color: #268bd2;">X</span>, <span style="color: #268bd2;">Y</span>) :- parent(<span style="color: #268bd2;">Z</span>, <span style="color: #268bd2;">X</span>), parent(<span style="color: #268bd2;">Z</span>, <span style="color: #268bd2;">Y</span>), female(<span style="color: #268bd2;">X</span>), female(<span style="color: #268bd2;">Y</span>).
+</pre>
+
+
+<p>
+And some queries:
+</p>
+
+
+
+<pre class="src src-prolog">?- sister(ann, pat).
+true.
+
+?- sister(bob, pat).
+false.
+
+?- sister(<span style="color: #268bd2;">X</span>, <span style="color: #268bd2;">Y</span>).
+<span style="color: #268bd2;">X</span> = <span style="color: #268bd2;">Y</span>, <span style="color: #268bd2;">Y</span> = liz <span style="color: #859900;">;</span>
+<span style="color: #268bd2;">X</span> = <span style="color: #268bd2;">Y</span>, <span style="color: #268bd2;">Y</span> = ann <span style="color: #859900;">;</span>
+<span style="color: #268bd2;">X</span> = ann,
+<span style="color: #268bd2;">Y</span> = pat <span style="color: #859900;">;</span>
+<span style="color: #268bd2;">X</span> = pat,
+<span style="color: #268bd2;">Y</span> = ann <span style="color: #859900;">;</span>
+<span style="color: #268bd2;">X</span> = <span style="color: #268bd2;">Y</span>, <span style="color: #268bd2;">Y</span> = pat <span style="color: #859900;">;</span>
+false.
+</pre>
+
+
+<p>
+What does <code>X = Y, Y = liz</code> mean? It means <code>liz</code> is, apparently, her
+own sister:
+</p>
+
+
+
+<pre class="src src-prolog">?- sister(liz, liz).
+true.
+</pre>
+
+
+<p>
+Let&rsquo;s prevent that by adding another clause to the <code>sister</code> predicate
+(<code>\=</code> means &ldquo;not equal&rdquo;):
+</p>
+
+
+
+<pre class="src src-prolog"><span style="color: #268bd2;">sister</span>(<span style="color: #268bd2;">X</span>, <span style="color: #268bd2;">Y</span>) :- parent(<span style="color: #268bd2;">Z</span>, <span style="color: #268bd2;">X</span>), parent(<span style="color: #268bd2;">Z</span>, <span style="color: #268bd2;">Y</span>), female(<span style="color: #268bd2;">X</span>), female(<span style="color: #268bd2;">Y</span>), <span style="color: #268bd2;">X</span> \= <span style="color: #268bd2;">Y</span>.
+</pre>
+
+
+<p>
+Now, we get the correct behavior:
+</p>
+
+
+
+<pre class="src src-prolog">?- sister(liz, liz).
+false.
+
+?- sister(<span style="color: #268bd2;">X</span>, <span style="color: #268bd2;">Y</span>).
+<span style="color: #268bd2;">X</span> = ann,
+<span style="color: #268bd2;">Y</span> = pat <span style="color: #859900;">;</span>
+<span style="color: #268bd2;">X</span> = pat,
+<span style="color: #268bd2;">Y</span> = ann <span style="color: #859900;">;</span>
+false.
+</pre>
+
+
+<p>
These rules are essentially the following logical statements:
</p>
@@ -469,13 +544,17 @@ <h2 id="sec-2">Defining complex predicates (rules)</h2>
$$(\forall x)(\exists y)(\exists z)((Parent(x, y) \wedge Parent(y, z))
\rightarrow Grandparent(x))$$
+$$(\forall x)(\forall y)(\exists z)((Parent(z, x) \wedge Parent(z, y)
+\wedge Female(x) \wedge Female(y) \wedge x \neq y)
+\rightarrow Sister(x, y))$$
+
<p>
Notice how they all have the same form. Essentially, the variables in
-the rule (<code>X</code> in each of these cases) have a \(\forall\) in front
-(because the rule is true for all variables <code>X</code>, should certain
-conditions hold), and the variables inside the rule (<code>Y</code> and <code>Z</code>) have
-\(\exists\) in front because you only need to find one value for each
-variable to make the rule true for some <code>X</code>. Notice we have
+the rule (<code>X</code> or both <code>X</code> and <code>Y</code> in each of these cases) have a
+\(\forall\) in front (because the rule is true for all variables <code>X</code> or
+<code>X</code> and <code>Y</code>, should certain conditions hold), and the variables inside
+the rule have \(\exists\) in front because you only need to find one
+value for each variable to make the rule true. Notice we have
\(\rightarrow\) not \(\leftrightarrow\) because there may be more than one
way to satisfy a rule. For example,
</p>
View
2  reasoning-with-uncertainty.html
@@ -7,7 +7,7 @@
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"/>
<meta name="title" content="Reasoning with uncertainty"/>
<meta name="generator" content="Org-mode"/>
-<meta name="generated" content="2012-04-22 23:20:08 EDT"/>
+<meta name="generated" content="2012-04-23 07:19:42 EDT"/>
<meta name="author" content="Joshua Eckroth"/>
<meta name="description" content=""/>
<meta name="keywords" content=""/>
View
2  siteindex.html
@@ -7,7 +7,7 @@
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"/>
<meta name="title" content="Site index"/>
<meta name="generator" content="Org-mode"/>
-<meta name="generated" content="2012-04-22 23:20:07 EDT"/>
+<meta name="generated" content="2012-04-23 07:19:42 EDT"/>
<meta name="author" content="Joshua Eckroth"/>
<meta name="description" content=""/>
<meta name="keywords" content=""/>
View
2  theindex.html
@@ -7,7 +7,7 @@
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"/>
<meta name="title" content="theindex"/>
<meta name="generator" content="Org-mode"/>
-<meta name="generated" content="2012-04-22 23:20:29 EDT"/>
+<meta name="generated" content="2012-04-23 07:19:57 EDT"/>
<meta name="author" content="Joshua Eckroth"/>
<meta name="description" content=""/>
<meta name="keywords" content=""/>
Please sign in to comment.
Something went wrong with that request. Please try again.