summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksander Sadikov <aleksander.sadikov@fri.uni-lj.si>2016-05-22 20:37:28 +0200
committerAleksander Sadikov <aleksander.sadikov@fri.uni-lj.si>2016-05-22 20:37:28 +0200
commitde7b441fe4ebd88fc19abbd6542cb8129f735ced (patch)
tree9c409cc281e5a095e468f20d9a33b1acad3ed5e7
parent52a65d75cd7adc7983d4e6c45709b7571497ca9a (diff)
Denotational semantics, part #1: tests and translations added.
-rw-r--r--prolog/problems/denotational_semantics/prog_8puzzle_2/common.py39
-rw-r--r--prolog/problems/denotational_semantics/prog_8puzzle_2/sl.py16
-rw-r--r--prolog/problems/denotational_semantics/prog_8puzzle_3/common.py39
-rw-r--r--prolog/problems/denotational_semantics/prog_8puzzle_3/sl.py35
-rw-r--r--prolog/problems/denotational_semantics/prog_listswap_2/common.py38
-rw-r--r--prolog/problems/denotational_semantics/prog_listswap_2/en.py6
-rw-r--r--prolog/problems/denotational_semantics/prog_listswap_2/sl.py18
-rw-r--r--prolog/problems/denotational_semantics/prog_listswap_3/common.py36
-rw-r--r--prolog/problems/denotational_semantics/prog_listswap_3/en.py2
-rw-r--r--prolog/problems/denotational_semantics/prog_listswap_3/sl.py37
10 files changed, 264 insertions, 2 deletions
diff --git a/prolog/problems/denotational_semantics/prog_8puzzle_2/common.py b/prolog/problems/denotational_semantics/prog_8puzzle_2/common.py
index 22429cc..7f78fec 100644
--- a/prolog/problems/denotational_semantics/prog_8puzzle_2/common.py
+++ b/prolog/problems/denotational_semantics/prog_8puzzle_2/common.py
@@ -1,3 +1,10 @@
+from operator import itemgetter
+import socket
+
+import prolog.engine
+import prolog.util
+from server.hints import Hint, HintPopup
+
id = 172
number = 30
visible = True
@@ -14,3 +21,35 @@ instr172 --> [right].
instr172 --> [up].
instr172 --> [down].
'''
+
+test_cases = [
+ ('findall(W, (W = [_], prog_8puzzle(W, [])), Words), Words == []',
+ [{}]),
+ ('setof(W, [A,B,C]^(W = [A,B,C], prog_8puzzle(W, [])), Words), \
+ Words == [[begin,down,end],[begin,left,end],[begin,right,end],[begin,up,end]]',
+ [{}]),
+ ('setof(W, [A,B,C,D]^(W = [A,B,C,D], prog_8puzzle(W, [])), Words), length(Words, 16)',
+ [{}]),
+ ('prog_8puzzle([begin,down,down,up,up,left,left,right,end], [])',
+ [{}]),
+]
+
+def test(code, aux_code):
+ n_correct = 0
+ engine_id = None
+ try:
+ engine_id, output = prolog.engine.create(code=code+aux_code, timeout=1.0)
+ if engine_id is not None and 'error' not in map(itemgetter(0), output):
+ # Engine successfully created, and no syntax error in program.
+ for query, answers in test_cases:
+ if prolog.engine.check_answers(engine_id, query=query, answers=answers, timeout=1.0):
+ n_correct += 1
+ except socket.timeout:
+ pass
+ finally:
+ if engine_id:
+ prolog.engine.destroy(engine_id)
+
+ hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}]
+ return n_correct, len(test_cases), hints
+
diff --git a/prolog/problems/denotational_semantics/prog_8puzzle_2/sl.py b/prolog/problems/denotational_semantics/prog_8puzzle_2/sl.py
new file mode 100644
index 0000000..0e5ad88
--- /dev/null
+++ b/prolog/problems/denotational_semantics/prog_8puzzle_2/sl.py
@@ -0,0 +1,16 @@
+name = 'prog_8puzzle/2'
+slug = 'jezik za reševanje igre Osem'
+
+description = '''\
+<p>
+Napiši DCG za jezik igre Osem (angl. 8-puzzle). Prvi simbol vsake besede je simbol <code>[begin]</code>, temu pa
+sledi poljubno zaporedje "ukazov" iz nabora {<code>left</code>, <code>right</code>, <code>up</code> <code>down</code>},
+na koncu pa je simbol <code>[end]</code>. Začetni nekončni simbol naj se imenuje <code>prog_8puzzle</code>.
+</p>
+
+<p>
+Primeri veljavnih besed: <code>[begin,left,down,right,end]</code>, <code>[begin,down,end]</code>.
+</p>
+'''
+
+hint = {}
diff --git a/prolog/problems/denotational_semantics/prog_8puzzle_3/common.py b/prolog/problems/denotational_semantics/prog_8puzzle_3/common.py
index 0e0d048..b88e544 100644
--- a/prolog/problems/denotational_semantics/prog_8puzzle_3/common.py
+++ b/prolog/problems/denotational_semantics/prog_8puzzle_3/common.py
@@ -1,3 +1,10 @@
+from operator import itemgetter
+import socket
+
+import prolog.engine
+import prolog.util
+from server.hints import Hint, HintPopup
+
id = 173
number = 40
visible = True
@@ -34,3 +41,35 @@ instr173((R0,C0) --> (R,C)) -->
;
C0>6, C=C0, R=R0)}.
'''
+
+test_cases = [
+ ('findall(W, (W = [_], prog_8puzzle(_, W, [])), Words), Words == []',
+ [{}]),
+ ('prog_8puzzle([1,2,3,4,5,6,7,8,0]-->Out, [begin,down,right,left,end], []), Out == [1,2,3,4,5,6,7,0,8]',
+ [{}]),
+ ('prog_8puzzle([1,2,3,4,5,6,7,8,0]-->Out, [begin,up,up,left,left,end], []), Out == [0,1,2,4,5,3,7,8,6]',
+ [{}]),
+ ('prog_8puzzle([1,2,3,4,5,6,7,8,0]-->Out, [begin,left,up,right,up,left,down,down,right,end], []), \
+ Out == [1,6,2,4,5,3,7,8,0]',
+ [{}]),
+]
+
+def test(code, aux_code):
+ n_correct = 0
+ engine_id = None
+ try:
+ engine_id, output = prolog.engine.create(code=code+aux_code, timeout=1.0)
+ if engine_id is not None and 'error' not in map(itemgetter(0), output):
+ # Engine successfully created, and no syntax error in program.
+ for query, answers in test_cases:
+ if prolog.engine.check_answers(engine_id, query=query, answers=answers, timeout=1.0):
+ n_correct += 1
+ except socket.timeout:
+ pass
+ finally:
+ if engine_id:
+ prolog.engine.destroy(engine_id)
+
+ hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}]
+ return n_correct, len(test_cases), hints
+
diff --git a/prolog/problems/denotational_semantics/prog_8puzzle_3/sl.py b/prolog/problems/denotational_semantics/prog_8puzzle_3/sl.py
new file mode 100644
index 0000000..63744f9
--- /dev/null
+++ b/prolog/problems/denotational_semantics/prog_8puzzle_3/sl.py
@@ -0,0 +1,35 @@
+name = 'prog_8puzzle/3'
+slug = 'jezik za reševanje igre Osem s pomenom'
+
+description = '''\
+<p>
+Napiši DCG za jezik igre Osem (angl. 8-puzzle). Sintaksa tega jezika naj bo enaka kot v prejšnji nalogi.
+Prvi simbol vsake besede je simbol <code>[begin]</code>, temu pa sledi poljubno zaporedje "ukazov" iz nabora
+{<code>left</code>, <code>right</code>, <code>up</code> <code>down</code>}, na koncu pa je simbol
+<code>[end]</code>. Začetni nekončni simbol naj se imenuje <code>prog_8puzzle</code>.
+</p>
+
+<p>
+Pomen besede (programa) v tem jeziku ima obliko <code>In-->Out</code> in pomeni preslikavo
+med vhodnim seznamom <code>In</code> in izhodnim seznamom <code>Out</code>. Stanje je seznam (permutiran)
+števil od 0 do 8, kjer 0 pomeni prazno polje, ostala števila pa pomenijo oznako ustrezne ploščice. Prva
+tri števila v seznamu predstavljajo prvo vrstico stanja igre Osem, naslednja tri srednjo vrstico in zadnja
+tri spodnjo vrstico. Pomen ukazov <code>left</code>, <code>right</code>, <code>up</code> in <code>down</code>
+je "premik" praznega polja v ustrezni smeri.
+</p>
+
+<pre>
+?- prog_8puzzle([0,1,2,3,4,5,6,7,8]-->Out, [begin,down,right,end], []).
+ Out = [3,1,2,4,0,5,6,7,8].
+</pre>
+
+<p>
+Pomožni predikati (že definirani):
+</p>
+<ul>
+<li><code>findblank(List,I)</code> vrne položaj (indeks z bazo 1) <code>I</code> elementa 0 v seznamu <code>List</code></li>
+<li><code>swap(List,I,J,NewList)</code> ustvari seznam <code>NewList</code> tako, da zamenja elementa na mestih <code>I</code> in <code>J</code> v seznamu <code>List</code></li>
+</ul>
+'''
+
+hint = {}
diff --git a/prolog/problems/denotational_semantics/prog_listswap_2/common.py b/prolog/problems/denotational_semantics/prog_listswap_2/common.py
index b3da01f..e4dd975 100644
--- a/prolog/problems/denotational_semantics/prog_listswap_2/common.py
+++ b/prolog/problems/denotational_semantics/prog_listswap_2/common.py
@@ -1,3 +1,10 @@
+from operator import itemgetter
+import socket
+
+import prolog.engine
+import prolog.util
+from server.hints import Hint, HintPopup
+
id = 175
number = 10
visible = True
@@ -25,3 +32,34 @@ instr --> [left].
instr --> [right].
instr --> [swap].
'''
+
+test_cases = [
+ ('findall(W, (W = [_], prog_listswap(W, [])), Words), Words == []',
+ [{}]),
+ ('setof(W, [A,B,C,D]^(W = [A,B,C,D], prog_listswap(W, [])), Words), length(Words, 9)',
+ [{}]),
+ ('prog_listswap([begin,right,right,swap,end], [])',
+ [{}]),
+ ('prog_listswap([begin,right,right,swap,left,swap,end], [])',
+ [{}]),
+]
+
+def test(code, aux_code):
+ n_correct = 0
+ engine_id = None
+ try:
+ engine_id, output = prolog.engine.create(code=code+aux_code, timeout=1.0)
+ if engine_id is not None and 'error' not in map(itemgetter(0), output):
+ # Engine successfully created, and no syntax error in program.
+ for query, answers in test_cases:
+ if prolog.engine.check_answers(engine_id, query=query, answers=answers, timeout=1.0):
+ n_correct += 1
+ except socket.timeout:
+ pass
+ finally:
+ if engine_id:
+ prolog.engine.destroy(engine_id)
+
+ hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}]
+ return n_correct, len(test_cases), hints
+
diff --git a/prolog/problems/denotational_semantics/prog_listswap_2/en.py b/prolog/problems/denotational_semantics/prog_listswap_2/en.py
index 89907bd..7db473b 100644
--- a/prolog/problems/denotational_semantics/prog_listswap_2/en.py
+++ b/prolog/problems/denotational_semantics/prog_listswap_2/en.py
@@ -6,12 +6,14 @@ description = '''\
Write a DCG for manipulating list elements. The first symbol in every word is
<code>[begin]</code>, followed by any sequence of "instruction" symbols from
the set {<code>left</code>, <code>right</code>, <code>swap</code>}, and finally
-<code>[end]</code>. The starting symbol should be named
+the symbol <code>[end]</code>. The starting symbol should be named
<code>prog_listswap</code>.
</p>
+<p>This exercise has already been solved for you. Check how it works in the console.</p>
+
<p>
-Example words: <code>[begin,right,swap,end]</code>, <code>[begin,right,left,end]</code>.
+Example words: <code>[begin,right,swap,end]</code>, <code>[begin,right,right,swap,left,swap,end]</code>.
</p>
'''
diff --git a/prolog/problems/denotational_semantics/prog_listswap_2/sl.py b/prolog/problems/denotational_semantics/prog_listswap_2/sl.py
new file mode 100644
index 0000000..0ed8e4d
--- /dev/null
+++ b/prolog/problems/denotational_semantics/prog_listswap_2/sl.py
@@ -0,0 +1,18 @@
+name = 'prog_listswap/2'
+slug = 'jezik za rokovanje s seznami'
+
+description = '''\
+<p>
+Napiši DCG za rokovanje s seznami. Prvi simbol vsake besede je <code>[begin]</code>, temu pa sledi
+poljubno zaporedje "ukazov" iz nabora {<code>left</code>, <code>right</code>, <code>swap</code>},
+na koncu pa je simbol <code>[end]</code>. Začetni nekončni simbol naj se imenuje <code>prog_listswap</code>.
+</p>
+
+<p>Ta naloga je že rešena. Preveri kako deluje v konzoli.</p>
+
+<p>
+Primeri veljavnih besed: <code>[begin,right,swap,end]</code>, <code>[begin,right,right,swap,left,swap,end]</code>.
+</p>
+'''
+
+hint = {}
diff --git a/prolog/problems/denotational_semantics/prog_listswap_3/common.py b/prolog/problems/denotational_semantics/prog_listswap_3/common.py
index 800717e..f8238c6 100644
--- a/prolog/problems/denotational_semantics/prog_listswap_3/common.py
+++ b/prolog/problems/denotational_semantics/prog_listswap_3/common.py
@@ -1,3 +1,10 @@
+from operator import itemgetter
+import socket
+
+import prolog.engine
+import prolog.util
+from server.hints import Hint, HintPopup
+
id = 174
number = 20
visible = True
@@ -48,3 +55,32 @@ instr((R0,C0)-->(R,C0)) -->
[swap],
{ swap(R0, C0, R) }.
'''
+
+test_cases = [
+ ('findall(W, (W = [_], prog_listswap(_, W, [])), Words), Words == []',
+ [{}]),
+ ('prog_listswap([1,2,3,4]-->Out, [begin,right,right,swap,end], []), Out == [1,3,2,4]',
+ [{}]),
+ ('prog_listswap([a,b,c,d,e]-->Out, [begin,right,right,swap,left,swap,end], []), Out == [c,a,b,d,e]',
+ [{}]),
+]
+
+def test(code, aux_code):
+ n_correct = 0
+ engine_id = None
+ try:
+ engine_id, output = prolog.engine.create(code=code+aux_code, timeout=1.0)
+ if engine_id is not None and 'error' not in map(itemgetter(0), output):
+ # Engine successfully created, and no syntax error in program.
+ for query, answers in test_cases:
+ if prolog.engine.check_answers(engine_id, query=query, answers=answers, timeout=1.0):
+ n_correct += 1
+ except socket.timeout:
+ pass
+ finally:
+ if engine_id:
+ prolog.engine.destroy(engine_id)
+
+ hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}]
+ return n_correct, len(test_cases), hints
+
diff --git a/prolog/problems/denotational_semantics/prog_listswap_3/en.py b/prolog/problems/denotational_semantics/prog_listswap_3/en.py
index e275335..d357e3e 100644
--- a/prolog/problems/denotational_semantics/prog_listswap_3/en.py
+++ b/prolog/problems/denotational_semantics/prog_listswap_3/en.py
@@ -33,6 +33,8 @@ Helper predicates (already defined):
swapping the <code>I</code>th element with its left neighbor in
<code>List</code></li>
</ul>
+
+<p>This exercise has already been solved for you. Check how it works in the console.</p>
'''
hint = {}
diff --git a/prolog/problems/denotational_semantics/prog_listswap_3/sl.py b/prolog/problems/denotational_semantics/prog_listswap_3/sl.py
new file mode 100644
index 0000000..4d009a7
--- /dev/null
+++ b/prolog/problems/denotational_semantics/prog_listswap_3/sl.py
@@ -0,0 +1,37 @@
+name = 'prog_listswap/3'
+slug = 'jezik za rokovanje s seznami s pomenom'
+
+description = '''\
+<p>
+Napiši DCG za rokovanje s seznami. Prvi simbol vsake besede je <code>[begin]</code>, temu pa sledi
+poljubno zaporedje "ukazov" iz nabora {<code>left</code>, <code>right</code>, <code>swap</code>},
+na koncu pa je simbol <code>[end]</code>. Začetni nekončni simbol naj se imenuje <code>prog_listswap</code>.
+</p>
+
+<p>
+Pomen besede (programa) v tem jeziku ima obliko <code>In-->Out</code> in pomeni preslikavo
+med vhodnim seznamom <code>In</code> in izhodnim seznamom <code>Out</code>. Notranje stanje
+gramatike, hranjeno v argumentih nekončnih simbolov, poleg trenutnega stanja seznama vodi tudi
+pozicijo kurzorja s katerim rokujemo s seznamom. Ukaza <code>left</code> in <code>right</code>
+kurzor premakneta za eno mesto v ustrezno smer, ukaz <code>swap</code> pa zamenja mesti elementov
+med katerima stoji kurzor. Če je kurzor na začetku seznama, ukaz <code>swap</code> ne uspe.
+</p>
+
+<pre>
+?- prog_listswap([1,2,3,4]-->Out, [begin,right,swap,end], []).
+ Out = [2,1,3,4].
+</pre>
+
+<p>
+Pomožni predikat (že definiran):
+</p>
+<ul>
+<li><code>swap(List,I,NewList)</code> ustvari nov seznam <code>NewList</code> tako, da
+zamenja mesti <code>I</code>-tega elementa in njegovega levega soseda v seznamu
+<code>List</code></li>
+</ul>
+
+<p>Ta naloga je že rešena. Preveri kako deluje v konzoli.</p>
+'''
+
+hint = {}