Skip to content

Commit

Permalink
update page
Browse files Browse the repository at this point in the history
  • Loading branch information
ranjitjhala committed Jun 10, 2017
1 parent c9526b0 commit 45fa21f
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 21 deletions.
8 changes: 7 additions & 1 deletion docs/lectures.html
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,16 @@ <h1>Lecture Notes</h1>
</tr>
<tr class="even">
<td align="center">6/2</td>
<td align="left">Logic Programming</td>
<td align="left">Prolog: Basics</td>
<td align="center"><a href="lectures/09-prolog.html">html</a> <a href="http://github.com/ucsd-cse130/web/blob/master/lectures/09-prolog.md">md</a> <a href="static/raw/prolog.txt">txt</a></td>
<td align="center">[.pl][pl-5-2]</td>
</tr>
<tr class="odd">
<td align="center">6/9</td>
<td align="left">Prolog: Programs</td>
<td align="center"><a href="lectures/10-puzzles.html">html</a> <a href="http://github.com/ucsd-cse130/web/blob/master/lectures/10-puzzles.md">md</a></td>
<td align="center">[.pl][pl-5-2]</td>
</tr>
</tbody>
</table>
<h2 id="interesting-piazza-questions">Interesting Piazza Questions</h2>
Expand Down
30 changes: 21 additions & 9 deletions docs/lectures/10-puzzles.html
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ <h1>Logic Programming II</h1>
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<h2 id="numeric-computations-add">Numeric Computations: <code>add</code></h2>
<div class="sourceCode"><pre class="sourceCode prolog"><code class="sourceCode prolog">add(<span class="dt">X</span><span class="kw">,</span><span class="dt">Y</span><span class="kw">,</span><span class="dt">Z</span>) <span class="kw">:-</span> <span class="dt">Z</span> <span class="dt">is</span> <span class="dt">X+Y</span><span class="kw">.</span></code></pre></div>
<div class="sourceCode"><pre class="sourceCode ocaml"><code class="sourceCode ocaml"><span class="kw">let</span> add x y = x + y</code></pre></div>
<div class="sourceCode"><pre class="sourceCode prolog"><code class="sourceCode prolog">addP(<span class="dt">X</span><span class="kw">,</span><span class="dt">Y</span><span class="kw">,</span><span class="dt">Out</span>) <span class="kw">:-</span> <span class="dt">Out</span> <span class="dt">is</span> <span class="dt">X+Y</span><span class="kw">.</span></code></pre></div>
<p>We “call” the function with a query:</p>
<div class="sourceCode"><pre class="sourceCode prolog"><code class="sourceCode prolog"><span class="fu">?-</span> add(<span class="dv">1</span><span class="kw">,</span><span class="dv">5</span><span class="kw">,</span><span class="dt">Z</span>)<span class="kw">.</span>
<span class="dt">Z</span><span class="kw">=</span><span class="dv">6</span><span class="kw">.</span>
Expand All @@ -242,9 +243,14 @@ <h2 id="numeric-computations-add">Numeric Computations: <code>add</code></h2>
<span class="dt">Z</span><span class="kw">=</span><span class="dv">30</span><span class="kw">.</span></code></pre></div>
<h2 id="numeric-computations-fibonacci">Numeric Computations: <code>fibonacci</code></h2>
<p>First attempt.</p>
<div class="sourceCode"><pre class="sourceCode prolog"><code class="sourceCode prolog">fib(<span class="dt">N</span><span class="kw">,</span> <span class="dv">0</span>) <span class="kw">:-</span> <span class="dv">1</span><span class="kw">.</span>
fib(<span class="dt">N</span><span class="kw">,</span> <span class="dv">1</span>) <span class="kw">:-</span> <span class="dv">1</span><span class="kw">.</span>
fib(<span class="dt">N</span><span class="kw">,</span> <span class="dt">R</span>) <span class="kw">:-</span> <span class="dt">N1</span> <span class="dt">is</span> <span class="dt">N</span><span class="dv">-1</span><span class="kw">,</span> <span class="dt">N2</span> <span class="dt">is</span> <span class="dt">N</span><span class="dv">-2</span><span class="kw">,</span> fib(<span class="dt">N1</span><span class="kw">,</span> <span class="dt">R1</span>)<span class="kw">,</span> fib(<span class="dt">N2</span><span class="kw">,</span><span class="dt">R2</span>)<span class="kw">,</span> <span class="dt">R</span> <span class="dt">is</span> <span class="dt">R1+R2</span><span class="kw">.</span></code></pre></div>
<p>let rec fib n = match n with | 0 -&gt; 1 | 1 -&gt; 1 | n -&gt; fib (n-1) + fib (n-2)</p>
<div class="sourceCode"><pre class="sourceCode prolog"><code class="sourceCode prolog">fibP(<span class="dv">0</span><span class="kw">,</span> <span class="dv">1</span>)<span class="kw">.</span>
fibP(<span class="dv">1</span><span class="kw">,</span> <span class="dv">1</span>)<span class="kw">.</span>
fibP(<span class="dt">N</span><span class="kw">,</span> <span class="dt">R</span>) <span class="kw">:-</span> <span class="dt">N1</span> <span class="dt">is</span> <span class="dt">N</span><span class="dv">-1</span>
<span class="kw">,</span> <span class="dt">N2</span> <span class="dt">is</span> <span class="dt">N</span><span class="dv">-2</span>
<span class="kw">,</span> fibP(<span class="dt">N1</span><span class="kw">,</span> <span class="dt">R1</span>)
<span class="kw">,</span> fibP(<span class="dt">N2</span><span class="kw">,</span><span class="dt">R2</span>)
<span class="kw">,</span> <span class="dt">R</span> <span class="dt">is</span> <span class="dt">R1+R2</span><span class="kw">.</span></code></pre></div>
<p>Lets “call” it with a query:</p>
<div class="sourceCode"><pre class="sourceCode prolog"><code class="sourceCode prolog"><span class="fu">?-</span> fib(<span class="dv">5</span><span class="kw">,</span> <span class="dt">R</span>)<span class="kw">.</span>
<span class="dt">R</span> <span class="kw">=</span> <span class="dv">8</span>
Expand Down Expand Up @@ -307,9 +313,9 @@ <h2 id="lists-head-and-tail">Lists: <code>head</code> and <code>tail</code></h2>
<ul>
<li>With an extra output parameter</li>
</ul>
<div class="sourceCode"><pre class="sourceCode prolog"><code class="sourceCode prolog">headOf([<span class="dt">H</span><span class="fu">|</span><span class="dt">_</span>]<span class="kw">,</span> <span class="dt">H</span>)<span class="kw">.</span>
<div class="sourceCode"><pre class="sourceCode prolog"><code class="sourceCode prolog">headP([<span class="dt">H</span><span class="fu">|</span><span class="dt">_</span>]<span class="kw">,</span> <span class="dt">H</span>)<span class="kw">.</span>

tailOf([<span class="dt">_</span><span class="fu">|</span><span class="dt">T</span>]<span class="kw">,</span> <span class="dt">T</span><span class="er">]</span>)<span class="kw">.</span></code></pre></div>
tailP([<span class="dt">_</span><span class="fu">|</span><span class="dt">T</span>]<span class="kw">,</span> <span class="dt">T</span><span class="er">]</span>)<span class="kw">.</span></code></pre></div>
<h2 id="lists-hasthreeormore">Lists: <code>hasThreeOrMore</code></h2>
<p>We want a predicate such that:</p>
<div class="sourceCode"><pre class="sourceCode prolog"><code class="sourceCode prolog"><span class="fu">?-</span> hasThreeOrMore([])<span class="kw">.</span>
Expand All @@ -334,16 +340,22 @@ <h2 id="lists-hasthreeormore">Lists: <code>hasThreeOrMore</code></h2>
<span class="fu">?-</span> hasThreeOrMore([<span class="dt">_</span>,<span class="dt">_</span>,<span class="dt">_</span>])<span class="kw">.</span> <span class="co">% E</span></code></pre></div>
<h2 id="lists-isin">Lists: <code>isIn</code></h2>
<p>Lets write a predicate to check if <code>X</code> is in some list <code>Xs</code>.</p>
<div class="sourceCode"><pre class="sourceCode prolog"><code class="sourceCode prolog">isIn(<span class="dt">X</span><span class="kw">,</span><span class="dt">Xs</span>) <span class="kw">:-</span> <span class="dt">TODO</span><span class="fu">-</span><span class="dt">IN</span><span class="fu">-</span><span class="dt">CLASS</span><span class="kw">.</span></code></pre></div>
<p>isIn(1, []) == FALSE isIn(1, [1]) == TRUE isIn(1, [2,1]) == TRUE isIn(1, [2,4]) == FALSE</p>
<div class="sourceCode"><pre class="sourceCode prolog"><code class="sourceCode prolog">isIn(<span class="dt">X</span><span class="kw">,</span>[<span class="dt">X</span><span class="fu">|</span><span class="dt">_</span>])<span class="kw">.</span>
isIn(<span class="dt">X</span><span class="kw">,</span>[<span class="dt">_</span><span class="fu">|</span><span class="dt">T</span>]) <span class="kw">:-</span> isIn(<span class="dt">X</span><span class="kw">,</span><span class="dt">T</span>)<span class="kw">.</span></code></pre></div>
<h2 id="lists-len">Lists: <code>len</code></h2>
<p>Lets write a “function” to add up the elements of a list</p>
<div class="sourceCode"><pre class="sourceCode prolog"><code class="sourceCode prolog">len(<span class="dt">Xs</span><span class="kw">,</span> <span class="dt">R</span>)<span class="kw">.</span></code></pre></div>
<h2 id="lists-sum">Lists: <code>sum</code></h2>
<p>Lets write a “function” to add up the elements of a list</p>
<div class="sourceCode"><pre class="sourceCode prolog"><code class="sourceCode prolog">sum(<span class="dt">Xs</span><span class="kw">,</span> <span class="dt">R</span>) <span class="kw">:-</span> <span class="dt">TODO</span><span class="fu">-</span><span class="dt">IN</span><span class="fu">-</span><span class="dt">CLASS</span><span class="kw">.</span></code></pre></div>
<p>let rec sum xs = match xs with | [] -&gt; 0 | (h::t) -&gt; h + sum t</p>
<div class="sourceCode"><pre class="sourceCode prolog"><code class="sourceCode prolog">sumP([]<span class="kw">,</span> <span class="dv">0</span>)<span class="kw">.</span>
sumP([<span class="dt">H</span><span class="fu">|</span><span class="dt">T</span>]<span class="kw">,</span> <span class="dt">R</span>) <span class="kw">:-</span> sumP(<span class="dt">T</span><span class="kw">,</span> <span class="dt">R1</span>)<span class="kw">,</span> <span class="dt">R</span> <span class="dt">is</span> <span class="dt">H</span> <span class="dt">+</span> <span class="dt">R1</span><span class="kw">.</span> </code></pre></div>
<h2 id="lists-append">Lists: <code>append</code></h2>
<p>let rec append xs ys = match xs with | [] -&gt; ys | (h::t) -&gt; h :: (append t ys)</p>
<p>Lets write a “function” to append <em>two</em> lists.</p>
<div class="sourceCode"><pre class="sourceCode prolog"><code class="sourceCode prolog">append(<span class="dt">Xs</span><span class="kw">,</span> <span class="dt">Yz</span><span class="kw">,</span> <span class="dt">R</span>) <span class="kw">:-</span> <span class="dt">TODO</span><span class="fu">-</span><span class="dt">IN</span><span class="fu">-</span><span class="dt">CLASS</span><span class="kw">.</span></code></pre></div>
<div class="sourceCode"><pre class="sourceCode prolog"><code class="sourceCode prolog">append([] <span class="kw">,</span> <span class="dt">Ys</span><span class="kw">,</span> <span class="dt">Ys</span>)<span class="kw">.</span>
append([<span class="dt">H</span><span class="fu">|</span><span class="dt">T</span>]<span class="kw">,</span> <span class="dt">Ys</span><span class="kw">,</span> [<span class="dt">H</span><span class="fu">|</span><span class="dt">R</span>]) <span class="kw">:-</span> append(<span class="dt">T</span><span class="kw">,</span> <span class="dt">Ys</span><span class="kw">,</span> <span class="dt">R</span>)<span class="kw">.</span></code></pre></div>
<p>Unlike elsewhere, the prolog append is a <strong>magical</strong>.</p>
<div class="sourceCode"><pre class="sourceCode prolog"><code class="sourceCode prolog"> <span class="fu">?-</span> append([<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>]<span class="kw">,</span> [<span class="dv">4</span>,<span class="dv">5</span>,<span class="dv">6</span>]<span class="kw">,</span> <span class="dt">R</span>)<span class="kw">.</span></code></pre></div>
<h2 id="lists-reverse">Lists: <code>reverse</code></h2>
Expand Down
22 changes: 22 additions & 0 deletions docs/static/raw/prolog.pl
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
append([] , Ys, Ys).
append([H|T], Ys, [H|R]) :- append(T, Ys, R).

isIn(X,[X|_]).
isIn(X,[_|T]) :- isIn(X,T).

headP([H|_],H).

tailP([_|T],T).

fibP(0, 1).
fibP(1, 1).
fibP(N, R) :- 1 < N
, N1 is N-1
, N2 is N-2
, fibP(N1, R1)
, fibP(N2,R2)
, R is R1+R2.


% term1 is term2

addP(X,Y,Out) :- Out is X + Y.

% fact
isMexican(pulpo).
Expand Down
9 changes: 8 additions & 1 deletion lectures.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ headerImg: books.jpg
| 5/24 | contd. | | |
| 5/26 | Polymorphism | [pdf][lec8] [pdf-small][lec8s] | [.ml][ml-5-26]|
| 5/31 | contd. | | [.ml][ml-5-31]|
| 6/2 | Logic Programming | [html][lec9] [md][md9] [txt][txt9] | [.pl][pl-5-2] |
| 6/2 | Prolog: Basics | [html][lec9] [md][md9] [txt][txt9] | [.pl][pl-5-2] |
| 6/9 | Prolog: Programs | [html][lec10] [md][md10] | [.pl][pl-5-2] |

## Interesting Piazza Questions

Expand Down Expand Up @@ -64,6 +65,12 @@ headerImg: books.jpg
[lec9]: lectures/09-prolog.html
[md9]: http://github.com/ucsd-cse130/web/blob/master/lectures/09-prolog.md
[txt9]: static/raw/prolog.txt
[lec10]: lectures/10-puzzles.html
[md10]: http://github.com/ucsd-cse130/web/blob/master/lectures/10-puzzles.md





[lc-4-7]: http://github.com/ucsd-cse130/web/blob/master/static/raw/code-4-7.lc
[lc-4-10]: http://github.com/ucsd-cse130/web/blob/master/static/raw/code-4-10.lc
Expand Down
50 changes: 40 additions & 10 deletions lectures/10-puzzles.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ headerImg: sea.jpg

## Numeric Computations: `add`

~~~~~{.ocaml}
let add x y = x + y
~~~~~

~~~~~{.prolog}
add(X,Y,Z) :- Z is X+Y.
addP(X,Y,Out) :- Out is X+Y.
~~~~~

We "call" the function with a query:
Expand All @@ -23,12 +27,22 @@ Z=30.

First attempt.

let rec fib n = match n with
| 0 -> 1
| 1 -> 1
| n -> fib (n-1) + fib (n-2)

~~~~~{.prolog}
fib(N, 0) :- 1.
fib(N, 1) :- 1.
fib(N, R) :- N1 is N-1, N2 is N-2, fib(N1, R1), fib(N2,R2), R is R1+R2.
fibP(0, 1).
fibP(1, 1).
fibP(N, R) :- N1 is N-1
, N2 is N-2
, fibP(N1, R1)
, fibP(N2,R2)
, R is R1+R2.
~~~~~


Lets "call" it with a query:

~~~~~{.prolog}
Expand Down Expand Up @@ -133,9 +147,9 @@ We have to write them as predicates
- With an extra output parameter

~~~~~{.prolog}
headOf([H|_], H).
headP([H|_], H).
tailOf([_|T], T]).
tailP([_|T], T]).
~~~~~


Expand All @@ -160,7 +174,6 @@ true
true.
~~~~~


Which of these is an implementation of such a predicate?

~~~~~{.prolog}
Expand All @@ -175,8 +188,14 @@ Which of these is an implementation of such a predicate?
Lets write a predicate to check if `X` is in some list `Xs`.
isIn(1, []) == FALSE
isIn(1, [1]) == TRUE
isIn(1, [2,1]) == TRUE
isIn(1, [2,4]) == FALSE
~~~~~{.prolog}
isIn(X,Xs) :- TODO-IN-CLASS.
isIn(X,[X|_]).
isIn(X,[_|T]) :- isIn(X,T).
~~~~~

## Lists: `len`
Expand All @@ -191,16 +210,27 @@ len(Xs, R).

Lets write a "function" to add up the elements of a list

let rec sum xs = match xs with
| [] -> 0
| (h::t) -> h + sum t

~~~~~{.prolog}
sum(Xs, R) :- TODO-IN-CLASS.
sumP([], 0).
sumP([H|T], R) :- sumP(T, R1), R is H + R1.
~~~~~

## Lists: `append`

let rec append xs ys = match xs with
| [] -> ys
| (h::t) -> h :: (append t ys)


Lets write a "function" to append *two* lists.

~~~~~{.prolog}
append(Xs, Yz, R) :- TODO-IN-CLASS.
append([] , Ys, Ys).
append([H|T], Ys, [H|R]) :- append(T, Ys, R).
~~~~~

Unlike elsewhere, the prolog append is a **magical**.
Expand Down
22 changes: 22 additions & 0 deletions static/raw/prolog.pl
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
append([] , Ys, Ys).
append([H|T], Ys, [H|R]) :- append(T, Ys, R).

isIn(X,[X|_]).
isIn(X,[_|T]) :- isIn(X,T).

headP([H|_],H).

tailP([_|T],T).

fibP(0, 1).
fibP(1, 1).
fibP(N, R) :- 1 < N
, N1 is N-1
, N2 is N-2
, fibP(N1, R1)
, fibP(N2,R2)
, R is R1+R2.


% term1 is term2

addP(X,Y,Out) :- Out is X + Y.

% fact
isMexican(pulpo).
Expand Down

0 comments on commit 45fa21f

Please sign in to comment.