diff options
Diffstat (limited to 'sample/recursive_functions')
| -rw-r--r-- | sample/recursive_functions/ack_3_5.in | 32 | ||||
| -rw-r--r-- | sample/recursive_functions/fib-tail_9.in | 10 | ||||
| -rw-r--r-- | sample/recursive_functions/fib_9.in | 16 | ||||
| -rw-r--r-- | sample/recursive_functions/fib_9_abbr.in | 16 | ||||
| -rw-r--r-- | sample/recursive_functions/gcd.in | 16 | ||||
| -rw-r--r-- | sample/recursive_functions/gcd_another.in | 16 | ||||
| -rw-r--r-- | sample/recursive_functions/pow.in | 16 |
7 files changed, 122 insertions, 0 deletions
diff --git a/sample/recursive_functions/ack_3_5.in b/sample/recursive_functions/ack_3_5.in new file mode 100644 index 0000000..19f92fa --- /dev/null +++ b/sample/recursive_functions/ack_3_5.in @@ -0,0 +1,32 @@ +// Ackermann Function +// +// fun ack(0,n) = n+1 +// | ack(m,0) = ack(m-1,1) +// | ack(m,n) = ack(m-1,ack(m,n-1)); + +// The following rule is wrong because there is no gurantee that +// the 'cnt' is connected to an integer agent +// when ack(r)~(m-1,cnt) is evaluated. +// +// ack(r) >< (int m, int n) +// | m==0 => r~(n+1) +// | n==0 => ack(r) ~ (m-1,1) +// | _ => ack(cnt)~(m,n-1), ack(r)~(m-1,cnt); +// +// So, the `cnt' should be connected as an element of connections. + +ack(ret, int m) >< (int n) +| m==0 => ret~(n+1) +| n==0 => ack(ret, m-1)~1 +| _ => ack(cnt, m)~(n-1), ack(ret, m-1)~cnt; + +ackMain(ret)><(int m, int n) => ack(ret,m)~n; + + +// main +const M=3; +const N=5; +ackMain(ret)~(M,N); +ret; +free ret; + diff --git a/sample/recursive_functions/fib-tail_9.in b/sample/recursive_functions/fib-tail_9.in new file mode 100644 index 0000000..58f4cc5 --- /dev/null +++ b/sample/recursive_functions/fib-tail_9.in @@ -0,0 +1,10 @@ +// Fibonacci number +fibt(r, int a, int b) >< (int n) +| n == 0 => r~0 +| n == 1 => r~a +| _ => fibt(r, a+b, a) ~ (n-1); + +fibMain(r) >< (int n) => fibt(r, 1, 0) ~ n; + +fibMain(r)~9; +r; diff --git a/sample/recursive_functions/fib_9.in b/sample/recursive_functions/fib_9.in new file mode 100644 index 0000000..7c4af1f --- /dev/null +++ b/sample/recursive_functions/fib_9.in @@ -0,0 +1,16 @@ +// Fibonacci number + +// Rules +fib(ret) >< (int n) +| n == 0 => ret~0 +| n == 1 => ret~1 +| _ => fib(cnt1)~(n-1), fib(cnt2)~(n-2), Add(ret, cnt2)~cnt1; + + +// Note: Add is implemented as a built-in agent such that: +// Add(ret, int m) ~ (int n) --> ret~(m+n); + +// Nets +fib(ret)~9; +ret; + diff --git a/sample/recursive_functions/fib_9_abbr.in b/sample/recursive_functions/fib_9_abbr.in new file mode 100644 index 0000000..57a8c62 --- /dev/null +++ b/sample/recursive_functions/fib_9_abbr.in @@ -0,0 +1,16 @@ +// Fibonacci number + +// Rules +fib(ret) >< (int n) +| n == 0 => ret~0 +| n == 1 => ret~1 +| _ => cnt1 << fib(n-1), cnt2 << fib(n-2), ret << Add(cnt1,cnt2); + + +// Note: Add is implemented as a built-in agent such that: +// Add(ret, int m) ~ (int n) --> ret~(m+n); + +// Nets +ret << fib(9); +ret; + diff --git a/sample/recursive_functions/gcd.in b/sample/recursive_functions/gcd.in new file mode 100644 index 0000000..5a57d8d --- /dev/null +++ b/sample/recursive_functions/gcd.in @@ -0,0 +1,16 @@ +// Greatest common divisor +// +//def gcd(a: Int, b: Int): Int = +// if (b==0) a else gcd(b, a%b) + + +// Rule +gcd(ret, int a) >< (int b) +| b==0 => ret ~ a +| _ => gcd(ret, b) ~ a%b; + +// Net +gcd(r, 14) ~ 21; +r; // it should be 7 + + diff --git a/sample/recursive_functions/gcd_another.in b/sample/recursive_functions/gcd_another.in new file mode 100644 index 0000000..e616e28 --- /dev/null +++ b/sample/recursive_functions/gcd_another.in @@ -0,0 +1,16 @@ +// Greatest common divisor +// +//def gcd(a: Int, b: Int): Int = +// if (b==0) a else gcd(b, a%b) + + +// Rule +gcd(ret) >< (int a, int b) +| b==0 => ret ~ a +| _ => gcd(ret) ~ (b, a%b); + +// Net +gcd(r) ~ (14,21); +r; // it should be 7 + + diff --git a/sample/recursive_functions/pow.in b/sample/recursive_functions/pow.in new file mode 100644 index 0000000..a123b9f --- /dev/null +++ b/sample/recursive_functions/pow.in @@ -0,0 +1,16 @@ +pow2(r, int m) >< (int n) +| n == 0 => r~m +| _ => pow2(r, m*2)~(n-1); + +pow(r) >< (int n) +| n == 0 => r~1 +| _ => pow2(r, 1)~(n); + + +// Caclulate 2^10 +const NUM=10; +pow(r)~NUM; // it can also be written with abbreviation: r << pow(NUM) +r; + + + |
