aboutsummaryrefslogtreecommitdiff
path: root/sample
diff options
context:
space:
mode:
authorShinya Sato <shinnya.sato@gmail.com>2022-07-21 14:59:34 +0900
committerShinya Sato <shinnya.sato@gmail.com>2022-07-21 14:59:34 +0900
commit4753a3914675dd8a3dfb8233062f90b65520ba4e (patch)
tree3632b01a2caa682c45962fad114f7b7504ea2c1f /sample
parentc57cb1fd511d2c69bf332324c73608f309bb4772 (diff)
parent1f51c41a32e190d483ff336c34eb63acda27cbb6 (diff)
downloadinpla-4753a3914675dd8a3dfb8233062f90b65520ba4e.tar.gz
inpla-4753a3914675dd8a3dfb8233062f90b65520ba4e.zip
Merge branch 'develop'
Diffstat (limited to 'sample')
-rw-r--r--sample/hanoi.in49
-rw-r--r--sample/nqueen.in141
2 files changed, 158 insertions, 32 deletions
diff --git a/sample/hanoi.in b/sample/hanoi.in
index 665b2da..40cef50 100644
--- a/sample/hanoi.in
+++ b/sample/hanoi.in
@@ -1,42 +1,27 @@
// Tower of Hanoi Puzzle
// Rules
-hanoi(return, source, target, auxiliary) >< (int n)
-| n==0 => return~[], Eraser~source, Eraser~target, Eraser~auxiliary
-| _ => source ~ Dup(src1, Dup(src2, src3)),
- target ~ Dup(tgt1, Dup(tgt2, tgt3)),
- auxiliary ~ Dup(aux1, aux2),
- hanoi(ret1, src1, aux1, tgt1)~(n-1),
- hanoi(ret2, aux2, tgt2, src2)~(n-1),
- Append(return, (n,src3,tgt3):ret2)~ret1;
- // This Append is a bulit-in agent.
+hanoi(return, source, target, aux) >< (int n)
+| n==0 =>
+ // Erase the source, target and aux
+ Eraser~source, Eraser~target, Eraser~aux,
+ // result
+ return~[]
-// The Dup is defined as built-in:
-// Dup(a,b)><A => a~A, b~A;
-// Dup(a,b)><B => a~B, b~B;
-// Dup(a,b)><C => a~C, b~C;
+| _ =>
+ // Duplicate the source, target and aux
+ source ~ Dup(src1, Dup(src2, src3)),
+ target ~ Dup(tgt1, Dup(tgt2, tgt3)),
+ aux ~ Dup(aux1, aux2),
-// The `Eraser' is defined as a built-in agent for any agents as follows:
-//Eraser >< Alpha(a1, ..., a5) => Eraser~a1, ..., Eraser~a5;
+ // recursive applications
+ hanoi(ret1, src1, aux1, tgt1)~(n-1),
+ hanoi(ret2, aux2, tgt2, src2)~(n-1),
+
+ // result
+ return << Append(ret1, (n,src3,tgt3):ret2);
// Net
hanoi(result, A, C, B) ~ 3;
result; // should be [(1,A,C),(2,A,B),(1,C,B),(3,A,C),(1,B,A),(2,B,C),(1,A,C)]
-
-
-// These are also written using abbreviation as follows:
-// hanoi(return, source, target, auxiliary) >< (int n)
-// | n==0 => return~[],
-// << Eraser(source), << Eraser(target), << Eraser(auxiliary)
-// | _ => src1, Dup(src2, src3) << Dup(source),
-// tgt1, Dup(tgt2, tgt3) << Dup(target),
-// aux1, aux2 << Dup(auxiliary),
-// ret1 << hanoi(src1, aux1, tgt1, n-1),
-// ret2 << hanoi(aux2, tgt2, src2, n-1),
-// return << Append(ret1, (n,src3,tgt3):ret2);
-//
-// result << hanoi(A, C, B, 3);
-// result;
-
-
diff --git a/sample/nqueen.in b/sample/nqueen.in
new file mode 100644
index 0000000..03b2463
--- /dev/null
+++ b/sample/nqueen.in
@@ -0,0 +1,141 @@
+// N-Queen
+//
+// The original is written as a Lisp program in the following file:
+// https://www.istc.kobe-u.ac.jp/activity/mage/m27/27_Lisp.pdf
+
+
+/**
+ * member
+ * r, done << member(m, list)
+ * return
+ * r: 1 if m occurs in list
+ * done: the same to `list'
+ */
+// fun member x [] = false
+// | member x (y::ys) = if x=y then true else (member x ys);
+
+member(r, list, int x) >< [] =>
+ r~0, list~[];
+member(r, list, int x) >< (int y):ys
+| x==y =>
+ r~1, list~y:ys
+| _ =>
+ list~y:cnt, member(r, cnt, x)~ys;
+
+
+/**
+ * len_list (stream version)
+ * len, done << len_list(list)
+ * return
+ * len: length of `list'
+ * done: the same to `list'
+ */
+
+len_list(len, done) >< [] =>
+ len~0, done~[];
+len_list(len, done) >< x:xs =>
+ done~x:cnt,
+ len_list_(len, cnt, 1)~xs;
+
+len_list_(len, done, int ct) >< [] =>
+ len~ct, done~[];
+len_list_(len, done, int ct) >< x:xs =>
+ done~x:cnt,
+ len_list_(len, cnt, ct+1) ~ xs;
+
+/**
+ * len (non-stream version)
+ * ret << len(list)
+ * return
+ * ret: length of `list'
+ */
+
+len(ret) >< [] =>
+ ret~0;
+len(ret) >< x:xs =>
+ len_list(ret, Eraser)~x:xs;
+
+
+
+/**
+ * threat (stream version)
+ * check for (0,q) and (k, qpos[col=k]) where k=1...
+ * http://www.nct9.ne.jp/m_hiroi/func/haskell06.html
+ 0 1 2 -->
+ +-------------
+ 1| . . . . . .
+ 2| . . . -3. . 5 - 3 = 2
+ 3| . . -2. . . 5 - 2 = 3
+ 4| . -1. . . . 5 - 1 = 4
+ 5| Q . . . . . Q position is 5
+ 6| . +1. . . . 5 + 1 = 6
+ 7| . . +2. . . 5 + 2 = 7
+ 8| . . . +3. . 5 + 2 = 8
+ +-------------
+ * r, done << threat(k,m, board)
+ * return
+ * r: 1 if threat conditions are satisfied
+ * done: the same to `board'
+ */
+// fun threat k m [] = false
+// | threat k m (x::xs) =
+// if (k = x-m) orelse (k = x+m) then true
+// else (threat (k+1) m xs);
+
+threat(r, done, int k, int q)><[] =>
+ r~0, done~[];
+threat(r, done, int k, int q)><(int x):xs
+| (k == x-q) or (k == q-x) =>
+ r~1, done~(x:xs)
+| _ =>
+ done~x:cnt, threat(r, cnt, k+1, q)~xs;
+
+
+
+// fun queen1 b n 0 = []
+// | queen1 b n m =
+// if (member m b) orelse (threat 1 m b) then (queen1 b n (m-1))
+// else if (length b) = (n-1) then (m::b)::(queen1 b n (m-1))
+// else (queen1 (m::b) n n) @ (queen1 b n (m-1));
+// fun queen n = queen1 n [] n;
+
+queen(ret) >< (int n) => queen_(ret, [], n)~n;
+
+queen_(r, b, int n) >< (int m)
+| m==0 =>
+ r~[], Eraser~b
+| _ =>
+ mem_ret, done_b << member(m,b),
+ queen__chk_m_in_b(r, done_b, n, m)~mem_ret;
+
+queen__chk_m_in_b(r, b, int n, int m) >< (int mem_ret)
+| mem_ret == 1 =>
+ queen_(r, b, n)~(m-1)
+| _ =>
+ th_ret, done_b << threat(1, m, b),
+ queen__chk_threat(r, done_b, n, m)~th_ret;
+
+queen__chk_threat(r, b, int n, int m) >< (int threat_ret)
+| threat_ret == 1 =>
+ queen_(r, b, n)~(m-1)
+| _ =>
+ len_ret, done_b << len_list(b),
+ queen__chk_lastelem(r, done_b, n, m)~len_ret;
+
+queen__chk_lastelem(r, b, int n, int m) >< (int len_ret)
+| len_ret == n-1 =>
+ b1, b2 << Dup(b),
+ r~(m:b1):cnt, queen_(cnt, b2, n)~(m-1)
+| _ =>
+ b1, b2 << Dup(b),
+ r << Append(cnt1, cnt2),
+ queen_(cnt1, m:b1, n)~n, queen_(cnt2, b2, n)~(m-1);
+
+
+
+// ----------------------------------------------------------
+// main
+// ----------------------------------------------------------
+const N=8;
+queen(ret)~N, len(count)~ret;
+count;