From 363306bc8d210e3abe06fad31a6e33f7f7c5da98 Mon Sep 17 00:00:00 2001 From: Shinya Sato Date: Thu, 21 Jul 2022 14:51:27 +0900 Subject: v0.8.3-1 --- Changelog.md | 37 ++- Gentle_introduction_Inpla.md | 716 +++++++++++++++++++++++++++++++++++++++++++ How_to_write_programs.md | 612 ------------------------------------ README.md | 4 +- sample/hanoi.in | 49 +-- sample/nqueen.in | 141 +++++++++ src/inpla.y | 187 ++++++++--- 7 files changed, 1042 insertions(+), 704 deletions(-) create mode 100644 Gentle_introduction_Inpla.md delete mode 100644 How_to_write_programs.md create mode 100644 sample/nqueen.in diff --git a/Changelog.md b/Changelog.md index d19faa5..c796a01 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,23 +1,32 @@ # Change log -### v0.8.3 (released on 19 July 2022) -#### New Features +### v0.8.3-1 minor update (released on 21 July 2022) +#### Polished +* **Built-in agent** `Dup`: it can duplicate attributes also immediately. For instance, with respect to `Cons(int, xs)`, it performs as follows: + ``` + Dup(a1,a2) >< (int i):xs => a1~i:w1, a2~i:w2, Dup(w1,w2)~xs; + ``` + -* **A built-in agent** `Dup`: it duplicates any nets gradually with the following rules: -![Eraser](pic/dup.png) -``` ->>> Dup(a1,a2) ~ [1,2,3]; -(7 interactions, 0.00 sec) ->>> ifce; -a1 a2 -Connections: -a1 ->[1,2,3] -a2 ->[1,2,3] +### v0.8.3 (released on 19 July 2022) +#### New Features ->>> -``` +* **Built-in agent** `Dup`: it duplicates any nets gradually with the following rules: +![Eraser](pic/dup.png) + ``` + >>> Dup(a1,a2) ~ [1,2,3]; + (7 interactions, 0.00 sec) + >>> ifce; + a1 a2 + + Connections: + a1 ->[1,2,3] + a2 ->[1,2,3] + + >>> + ``` ### v0.8.2-2 minor update (released on 30 May 2022) diff --git a/Gentle_introduction_Inpla.md b/Gentle_introduction_Inpla.md new file mode 100644 index 0000000..b64adc5 --- /dev/null +++ b/Gentle_introduction_Inpla.md @@ -0,0 +1,716 @@ +# Gentle introduction to Inpla +#### Contents in this section +* [Nets: terms and connections](#nets-terms-and-connections) +* [Interaction rules: rewriting rules for nets](#interaction-rules-rewriting-rules-for-nets) + - [Example: Operations on unary natural numbers](#example-operations-on-unary-natural-numbers) +* [Built-in Agents](#built-in-agents) +* [Attributes (integers)](#attributes-integers) + - [Built-in anonymous agent for attributes](#built-in-anonymous-agent-for-attributes) + - [Arithmetic expressions on attributes](#arithmetic-expressions-on-attributes) + - [Interaction rules with expressions on attributes](#interaction-rules-with-expressions-on-attributes) + - [Built-in rules of attributes in the anonymous agents](#built-in-rules-of-attributes-in-the-anonymous-agents) + - [Interaction rules with conditions on attributes](#interaction-rules-with-conditions-on-attributes) +* [Commands](#commands) +* [Execution Options](#execution-options) +* [Advanced topics](#advanced-topics) + - [Reuse annotations](#reuse-annotations) + - [built-in agents more](#built-in-agents-more) + - [Weak reduction strategy](#weak-reduction-strategy) + + + +## Nets: terms and connections + +Inpla evaluates *nets*, which are built by *connections between terms*. First, we start learning about terms and connections. + +### Terms + +**Terms** are built on *names* and *agents* as follows: + +``` + ::= | + ::= + ::= + | ['(' ',' ... ',' ')'] +``` +* **Name**: It works as a buffer between terms. The `````` is defined as strings start with a small letter, e.g. ```x``` and ```y```. To show connected terms from names, type the names. For instance, type just `x` to show a term connected from the 'x': + + ``` + >>> x; + + >>> + ``` + + The above execution means that nothing has been connected from the `x` yet, of course. + We have **Linearity restriction** for names such that **each name must occur at most twice**. This is required in order to ensure one-to-one connections between terms. + +* **Agent**: It works as a constructor and de-constructor (defined functions). Generally agents have one *principal port* and *n*-fixed *auxiliary ports*. The fixed number of auxiliary ports is called *arity*, and it is determined according to each agent sort. In graphical representation an agent term `A(x1,...,xn)`, whose arity is *n*, is drawn as the following picture, where its auxiliary ports and the principal port (drawn as an arrow) correspond to the occurrences of the `x1`,...`xn`, and `A(x1,...,xn)`, respectively: + + ![agent](pic/agent.png) + + The `````` is defined as strings start with a capital letter, e.g. ```A``` and ```Succ```, and also `````` followed by a open curry bracket```(```. So, ```foo(x)``` is recognised as an agent. + + + +### Connections + +A **connection** is a relation between two terms, that means the two term are connected, and it is expressed with the symbol `~`. For instance, a connection between a name `x` and an agent `A` (whose arity is 0) is denoted as `x~A`. There is no order in the left-hand and the right-hand side terms, thus `x~A` and `A~x` are identified as the same one. + +* **Connections between a name and an agent** are evaluated as that the agent is connected from the name. For instance, `x~A` is evaluated as that the `A` is connected from the `x`. Here, as an example, type `x~A` with the termination symbol `;` as follows: + + ``` + >>> x~A; + (0 interactions, 0.00 sec) + >>> + ``` + + To show the connected terms from the name x, type just `x`: + ``` + >>> x; + A + >>> + ``` + + To dispose the name `x` and anything connected from the `x`, use `free` command: + ``` + >>> free x; + >>> x; + + >>> + ``` + + +* **Connections between names** are evaluated as that ports corresponding to these names are connected mutually in interaction nets frameworks. However, in Inpla, these are evaluated as that, for the connections, the left-hand side name connects to the right-hand side name, thus only one way. For instance, `x~y` is evaluated that the `x` connects to the `x` (not that the `y` connects to `x`): + + ``` + >>> x~y; + >>> x; + y + >>> y; + + >>> + ``` + +* **Connections between agents** are evaluated according to *interaction rules* explained later. + +Two connections that have the same name at the right or left hand sides, say `t~x` and `x~s`, are written as `t~s`. **We note** that the `x` is disposed after rewriting so that every connection can be kept one-to-one via names, cannot be one-to-many. +``` +>>> x~A, x~y; +(0 interactions, 0.00 sec) +>>> y; +A +>>> x; // x has been consumed by the re-connection of x~A and x~y. + +>>> +``` + +Just in case for other examples, let the `y` disposed: + +``` +>>> free y; +>>> y; + +>>> +``` + + + +## Interaction rules: rewriting rules for nets + +Connections between agents are rewritten according to **interaction rules**: + +``` + ::= '><' '=>' ';' + ::= + | '(' ',' ... ',' ')' +``` + with the **proviso** that: + + * each name that occur in the two `` must be **distinct** + * (**Linearity restriction**): every name in the `` must **occur twice**. + +Something complicated? No problem! Let's us learn how to define the rules with some example! + +### Example: Operations on unary natural numbers + +Unary natural numbers are built by Z and S. For instance, 0, 1, 2, 3 are expressed as Z, S(Z), S(S(Z)), S(S(S(Z))). Here, let's think about an increment operation "inc" such that: +``` + inc(n) = S(n). + ``` + +First, we will think about the relationship between constructors and deconstructors in this computation. +Here, constructors are agents `Z`, `S(x)`, and a destructor is `inc(r)` which has an argument for the output. The behaviour of `inc(r)` will be as follows: +* When `inc(r)` meets `Z`, the output `r` is connected to `S(Z)`. +* In the case of `S(x)`, the `r` is connected to `S(S(x))`. + + + This is written as the followint rules: +``` +inc(r) >< Z => r~S(Z); +inc(r) >< S(x) => r~S(S(x)); +``` + +Let's check the rule proviso. In the first rule, the name `r` occurs twice, so it satisfies the proviso. The second rule is also OK because the `r` and `x` are distinct and occur twice in the rule. + +For agents can work as constructors and de-constructors, it could be good to use strings start from a capital letter such as `Z`, `S` and `Tree`, and all small letters for de-constructors such as `inc`. + +Let's take the result of the increment operation for `S(S(Z))`: + +``` +>>> inc(r)~S(S(Z)); +(1 interactions, 0.01 sec) +>>> r; +S(S(S(Z)) +>>> +``` + +Good! We get `S(S(S(Z)))` as the result of incrementation of `S(S(Z))` . + +To show the result as a natural number, use `prnat` command: + +``` +>>> prnat result; +3 +>>> +``` + +Let's clean the result in case it could be used anywhere: + +``` +>>> free r; +>>> +``` + +* **Exercise**: Addition on unary natural numbers. + + It is defined recursively in term rewriting systems as follows: + + - add(x, Z) = x, + - add(x, S(y)) = add(S(x), y). + + This is written in interaction nets as follows: ![add1](pic/add1.png) + + In Inpla (and textual notation in interaction nets also), each agent is expressed as a term whose arguments correspond to its auxiliary ports. For instance, the `add` agent in the most left-hand side of the above figure, putting distinct names on auxiliary ports such as `ret` and `x`, is written as a term `add(ret,x)` by assembling these names anti-clockwise from the principal port (drawn as an arrow). + + Every computation is performed on connections between principal ports according to interaction rules. The rules shown in above figure are written textually as follows: + + ``` + add(ret, x) >< Z => ret~x; + add(ret, x) >< S(y) => add(ret, S(x))~y; + ``` + The following is an execution example: + ``` + >>> add(r,S(Z))~S(S(Z)); + (3 interactions, 0.00 sec) + >>> r; + S(S(S(Z))) + >>> prnat result; + 3 + >>> free r; + >>> + ``` + +* **Exercise**: Another version of the addition. + + There is another version as follows: + + - add(x,Z) = x, + - add(x, S(y)) = S(add(x,y)). + + These are written in interaction nets: ![add2](pic/add2.png) + + and written textually as follows: + + ``` + add(ret,x) >< Z => ret~x; + add(ret,x) >< S(y) => ret~S(cnt), add(cnt, x)~y; + ``` + In comparison with the previous example, calculation results can be sent partially as `S(cnt)` to other soon, whereas those in the previous one are stored until all of computation finish. So, **this version is suitable for parallel execution**. + + +### Abbreviation notation `<<`: +An abbreviation notation `<<` is defined as follows: +``` +a,b,...,z << Agent(aa,bb,...,yy,zz) == for == Agent(a,b,...,z,aa,bb,...,yy) ~ zz + ``` +For instance, `r << add(S(Z),S(S(Z)))` is rewritten internally as `add(r,S(Z))~S(S(Z))`. It is handy to denote ports that take computation results. + + +## Built-in Agents +Inpla has built-in agents: + +### Tuples + +* `Tuple0`, `Tuple2(x1,x2)`, `Tuple3(x1,x2,x2)`, `Tuple4(x1,x2,x3,x4)`, `Tuple5(x1,x2,x3,x4,x5)`, written as `()`, `(x1,x2)`, `(x1,x2,x3)`, `(x1,x2,x3,x4)`, `(x1,x2,x3,x4,x5)`, respectively. + +* There is no Tuple1. `(x)` is evaluated just as `x`. + + +### Lists + +* `Nil`, `Cons(x,xs)`, written as `[]` and `x:xs`, respectively. +* A nested `Cons` terminated with `Nil` are written as a list notation using brackets `[` and `]`. For instance, `x1 : x2: x3 : Nil` is written as `[x1,x2,x3]` . + +#### Built-in rules for tuples and lists + +There are built-in rules for the same built-in agents that each element is matched and connected such that: + +``` +(x1,x2)><(y1,y2) => x1~y1, x2~y2 // This is already defined as a built-in rule. +``` + +We also have a built-in agent `Append` to append two lists as shown in the following pseudo code: + +``` +Append(r, listB) ~ listA --> r ~ (listA ++ listB) // pseudo code +``` +The order `listB` and `listA` seems something strange, so a special abbreviation notation is prepared: +``` +r << Append(listA, listB) == for == Append(r, listB)~listA +``` + + +* Examples of the built-in rules: + + ``` + >>> (x1,x2)~(Z, S(Z)); + (1 interactions, 0.00 sec) + >>> x1 x2; + Z S(Z) + >>> free x1 x2; + >>> + ``` + + ``` + >>> [y1, y2, y3]~[Z, S(Z), S(S(Z))]; + (1 interactions, 0.00 sec) + >>> y1 y2 y3; + Z S(Z) S(S(Z)) + >>> free y1 y2 y3; + >>> + ``` + + ``` + >>> Append(r, [Z, S(Z)]) ~ [A,B,C]; + (4 interactions, 0.00 sec) + >>> r; + [A,B,C,Z,S(Z)] + >>> free r; + >>> + ``` + +* The abbreviation notation for the Append: + ``` + >>> r << Append([A,B,C], [Z, S(Z)]); + (4 interactions, 0.00 sec) + >>> r; + [A,B,C,Z,S(Z)] + >>> free r; + >>> + ``` + + + +### Eraser + +* Eraser agent `Eraser` disposes any nets gradually: + ![Eraser](pic/eraser.png) + + ``` + >>> Eraser ~ A(x1,x2); + (1 interactions, 0.00 sec) + >>> x1 x2 + Eraser Eraser + >>> free x1 x2; + >>> + ``` + + +### Duplicator +* `Dup` agent duplicates any agents: + ![Dup](pic/dup.png) + ``` + >>> Dup(a1,a2) ~ [Z, S(Z), S(S(Z))]; + (1 interactions, 0.00 sec) + >>> a1 a2 + [Z,S(Z),S(S(Z))] [Z,S(Z),S(S(Z))] + >>> free a1 a2; + >>> + ``` + + +## Attributes (integers) + +Agents are extended so that integers can be held on their ports. These integers are called *attributes*. + +* For instance, `A(100)` is an agent `A` that holds an attribute value `100`. + + ``` + >>> x~A(100); + (0 interactions, 0.01 sec) + >>> x; + A(100); + >>> free x; + >>> + ``` + + +### Built-in anonymous agent for attributes +We can use integers the same as agents, and these are recognised as attributes of a built-in *anonymous agent* in Inpla. For instance, we can write `x~100` where the `100` is an attribute value of the anonymous agent. + + + ``` + >>> x~100; // The `100' is regarded as an attribute of an anonymous agent + (0 interactions, 0.00 sec) + >>> x; + 100 + >>> free x; + >>> + ``` + + + +### Arithmetic expressions on attributes +It is possible to have attributes as calculation results of arithmetic operation using `where` statement after connections: + +``` + ::= + + | 'where' * ';' + + ::= '=' +``` + +The symbol of addition, subtraction, multiplication, division, modulo are `+`, `-`, `*`, `/` and `%`, respectively. Relational operations `<`, `<=`, `>` and `>=` return 1 for True, 0 for False. Logical operations Not `!` (`not`), And `&&` (`and`) and Or `||` (`or`) are also available where only `0` is regarded as False. + +* Example: an expression using `where`: + + ``` + >>> x~A(a) where a=3+5; + (0 interactions, 0.00 sec) + >>> x; + A(8) + >>> free x; + >>> + ``` + +* Arithmetic expressions can be also written in arguments directly: + + ``` + >>> x~A(3+5); + (0 interactions, 0.00 sec) + >>> x; + A(8) + >>> free x; + >>> + ``` + + +* Arithmetic expressions are also available for the anonymous agent: + + ``` + >>> x~(3+5); // this is also written without brackets as x~3+5; + (0 interactions, 0.00 sec) + >>> x; + 8 + >>> free x; + >>> + ``` + + +### Interaction rules with expressions on attributes +In interaction rules, attributes are recognised by using variables with a modifier `int` in the rule-agent parts , and we call these *attribute variables*. +**Attribute variables has no Linear restriction** because these do not affect keeping the one-to-one connection among agent ports. So, we can use the same attribute variable as many times as we want. + +* Example: Incrementor on an attribute: + + ``` + >>> inc(result) >< (int a) => result~(a+1); // The `a' is a variable for an attribute + >>> inc(r)~10; // This is also written as: r << inc(10) + (1 interactions, 0.00 sec) + >>> r; + 11 + >>> free r; + >>> + ``` + + +- Example: Duplicator of integer lists. + + ![dup_list](pic/dup_list.png) + + + This has been implemented as a built-in agent `Dup`, but let's take it as an example that the same attribute variable occurs twice. On the first line of the following execution example, twice occurrences of *i* in the right-hand side (here, `(i:xs1)` and `(i:xs2)`) is allowed in a rule. This is possible because the *i* is an attribute variable. + + ``` + >>> dup(a1,a2) >< (int i):xs => a1~(i:xs1), a2~(i:xs2), dup(xs1,xs2)~xs; + >>> dup(a1,a2) >< [] => a1~[], a2~[]; + >>> dup(a,b) ~ [1,2,3]; // This is also written as: a,b << dup([1,2,3]) + (4 interactions, 0.00 sec) + >>> a b; + [1,2,3] [1,2,3] + >>> free a b; + >>> + ``` + + + +**We should be careful for operations of two attributes on distinct agents**. For instance, we take the following rule of an `add` agent: + +``` +>>> add(result, int b) >< (int a) => result~(a+b); +``` + +Of course, it works as an addition operation on two attributes: + +``` +>>> add(r, 3) ~ 5; +(1 interactions, 0.00 sec) +>>> r; +8 +>>> free r; +>>> +``` + +However, it causes runtime error if the `add` rule is invoked before an attribute value has been stored on the second argument of the `add`. For instance, we take the following computation: + +``` +>>> add(r, b)~3, add(b, 10)~20; +``` + +If the `add(r, b)~3` is operated first, it causes the runtime error because the `b` has not been connected to any attribute, whereas it is OK if the `add(b, 10)~20` is operated first. To prevent this fragile situation, **we have to have extra rules to ensure that every port with the modifier `int` has been connected to an attribute**. For the rule of the `add`, the following is a **solution** to have the extra rule (the `addn` is introduced as the extra agent): + +``` +>>> add(result, b) >< (int a) => addn(result, a) ~ b; +>>> addn(result, int a) >< (int b) => r~(a+b); +``` +Do not worry about it. Some built-in rules for arithmetic computations are prepared as follows: + + +### Built-in rules of attributes in the anonymous agents + +There are built-in rules for arithmetic operations between two agents by using `Add`, `Sub`, `Mul`, `Div`, `Mod` agents as addition, subtraction, multiplication, division and modulo, respectively. Rules for the `Add` are defined so that it can join two attributes on distinct agents safely as follows, and the others are also defined the same way: + +``` +Add(result, y)><(int x) => _Add(result, x)~y; +_Add(result, int x)><(int y) => result~(x+y); +``` + +* Example: + + ``` + >>> Add(r,3)~5; // Add is already defined as built-in. + >>> r; + 8 + >>> Sub(r1, r)~2; // It is also written as an abbreviation form: r1<>> r1; + 6 + >>> + ``` + + + +### Interaction rules with conditions on attributes +Conditional rewritings on attributes are available. The following is a general form: + +``` + ::= + '><' + '|' '=>' + '|' '=>' + ... + '|' '_' '=>' ';' + + is an expression on attributes specified in the two . +``` + +The sequence of `` must be finished with the otherwise case `_`. + +* Example: The following shows rules to obtain a list that contains only even numbers: + + ``` + // Rules + evenList(result) >< [] => r~[]; + evenList(result) >< (int x):xs + | x%2==0 => result~(x:cnt), evenList(cnt)~xs + | _ => evenList(result)~xs; + ``` + + ``` + >>> evenList(r)~[1,3,7,5,3,4,9,10]; + >>> r; + [4,10] + >>> free r; + >>> + ``` + + + +- Example: Fibonacci number: + + ``` + fib(result) >< (int n) + | n == 0 => result~0 + | n == 1 => result~1 + | _ => fib(r1)~(n-1), fib(r2)~(n-2), Add(result, r2)~r1; + + // * We cannot write result~(r1+r2) for Add(result, r2)~r1 + // because r1, r2 may have not been connected to attributes in the anonymous agents, + // thus we have to leave the addition until both fib(r1)~(n-1), fib(r2)~(n-2) are finished. + // For this purpose we have to use the addition operation between agents defined as + // Add(result, y)><(int x) => _Add(result, x)~y; + // _Add(result, int x)><(int y) => result~(x+y); + // By using Add(result, r2)~r1, + // after r1,r2 have been connected to (int x), (int y), respectively, + // x+y is executed and the calculation result is connected to the result safely. + ``` + + ``` + >>> fib(r)~39; + >>> r; + 63245986 + >>> free r; + >>> + ``` + + + +## Commands + +Inpla has the following commands: +* `free` *name1* ... *name_n* `;` + The *name1* ... *name_n* and connected terms from these are disposed. To dispose all living names and connected terms, type `free ifce;`, where the `ifce` is an abbreviation of *interface* that is called for the set of names that live and occur once. +* *name1* ... *name_n* `;` + Put terms connected from the *name1* ... *name_n*. To put all terms connected from the interface, type `ifce;`. +* `prnat` *name*`;` + Put a term connected from the *name* as a natural number. +* `use` `"`filename`";` + Read the file whose name is "filename". +* `exit;` + Quit the system. + +Inpla has the following macro: +* `const` *NAME*`=` *i* `;` + The *NAME* is bound to the integer value *i* as immutable, and replaced with the value *i* in nets and interaction rules. + + + +## Execution Options + +* When invoking Inpla, you can specify the following options: + + ``` + $ ./inpla -h + Inpla version 0.8.0 + Usage: inpla [options] + + Options: + -f Set input file name (Defalut: STDIN) + -d = Bind to + -Xms Set initial heap size to 2^ (Defalut: 12 (=4096)) + -Xmt Set multiple heap increment to 2^ (Defalut: 3 (= 8)) + 0: the same size heap is inserted when it runs up. + 1: the twice (=2^1) size heap is inserted. + -Xes Set initial equation stack size (Default: 256) + -w Enable Weak Reduction strategy (Default: false) + -t Set the number of threads (Default: 8) + -h Print this help message + ``` + +**Note**: + +* The option `-w` is available for the single-thread version. +* The option ```-t``` is available for the multi-thread version that is compiled by ```make thread```. The default value is setting for the number of cores, so execution will be automatically scaled without specifying this. + + +## Advanced topics + +### Reuse annotations +In interaction rule definitions, we can specify how active pair agents are reused by putting annotations `(*L)` and `(*R)` before agents in the right-hand side net. This annotations promote in-place computing, and as a result performance can be improved well in parallel execution. + +* For instance, in the rule `gcd(ret) >< (int a, int b)`, we can reuse the `gcd` and `Tuple2` in nets as follows: + + ``` + gcd(ret) >< (int a, int b) + | b==0 => ret ~ a + | _ => (*L)gcd(ret) ~ (*R)(b, a%b); + ``` + + +### Built-in agents more +* **Merger agent** `Marger`: +it merges two lists into one. Merger agent has two principal ports that can take the two distinct lists. Interactions with the lists are performed as soon as one of the principal ports is connected one of the lists. So, the merged result is non-deterministically decided, especially in multi-threaded execution. + + ![merger](pic/merger.png) + + We overload `<<` in order to use the Merger agent naturally as follows: + + ``` + ret << Merger(alist, blist) + ``` + + The following is an execution example (the count of interactions is not supported yet): + ``` + >>> r << Merger([0,0,0,0,0,0,0,0,0,0], [1,1,1,1,1,1,1,1,1,1]); + (1 interactions by 4 threads, 0.10 sec) + >>> ifce; + r + + Connections: + r ->[0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0] + + >>> + ``` + +### Weak reduction strategy + +In this reduction strategy, only connections that have interface names (thus, live and occur once) are evaluated. This is taken for non-terminate computation such as fixed point combinator and process networks. + +* Example: We have a sample net in `sample/processnet1.in` that keep producing natural numbers from 1 and output these to the port `r`: + + ``` + // Rules + dup(a1,a2) >< (int i):xs => a1~(i:xs1), a2~(i:xs2), dup(xs1,xs2)~xs; + dup(a1,a2) >< [] => a1~[], a2~[]; + + inc(r) >< (int i):xs => r~(i+1):w, inc(w)~xs; + inc(r) >< [] => r~[]; + + // Nets + dup(r,w)~r1, inc(r1) ~ 0:w; + + // +-----+ +-----+ +---+ + // r ----| | r1 | | | | + // | dup |--->---| inc |---><---| 0 |----+ + // +---| | | | | | | + // | +-----+ +-----+ +---+ | + // | | + // +-----------------------------------------+ + // w + ``` + + This reduction strategy is available by invoking with `-w` option as follows: + + ``` + $ ./inpla -w + Inpla 0.5.0 (Weak Strategy) : Interaction nets as a programming language [28 Oct. 2021] + >>> use "sample/processnet1.in"; + (2 interactions, 0.00 sec) + >>> ifce; + r + + Connections: + r ->[1,... // this means a list of 1 and something. + + >>> a:b ~ r; + (2 interactions, 0.00 sec) + >>> ifce; + a b + + Connections: + a ->1 + b ->[2,... + + >>> + ``` + \ No newline at end of file diff --git a/How_to_write_programs.md b/How_to_write_programs.md deleted file mode 100644 index 3f0cd27..0000000 --- a/How_to_write_programs.md +++ /dev/null @@ -1,612 +0,0 @@ -# Gentle introduction to programming in Inpla -#### Contents in this section -* [Nets: terms and connections](#nets-terms-and-connections) -* [Interaction rules: rewriting rules for nets](#interaction-rules-rewriting-rules-for-nets) - - [Example: Operations on unary natural numbers](#example-operations-on-unary-natural-numbers) -* [Built-in Agents](#built-in-agents) -* [Attributes (integers)](#attributes-integers) - - [Built-in anonymous agent for attributes](#built-in-anonymous-agent-for-attributes) - - [Arithmetic expressions on attributes](#arithmetic-expressions-on-attributes) - - [Interaction rules with expressions on attributes](#interaction-rules-with-expressions-on-attributes) - - [Built-in rules of attributes in the anonymous agents](#built-in-rules-of-attributes-in-the-anonymous-agents) - - [Interaction rules with conditions on attributes](#interaction-rules-with-conditions-on-attributes) -* [Commands](#commands) -* [Execution Options](#execution-options) - - - - -## Nets: terms and connections - -Inpla evaluates *nets*, which are built by *connections between terms*. First, we start learning about terms and connections. - -### Terms - -**Terms** are built on *names* and *agents* as follows: - -``` - ::= | - ::= - ::= - | ['(' ',' ... ',' ')'] -``` -* **Name**: It works as a buffer between terms. The `````` is defined as strings start with a small letter, e.g. ```x``` and ```y```. To show connected terms from names, type the names. For instance, type just `x` to show a term connected from the 'x': - - ``` - >>> x; - - >>> - ``` - - The above execution means that nothing has been connected from the `x` yet, of course. - We have **Linearity restriction** for names such that **each name must occur at most twice**. This is required in order to ensure one-to-one connections between terms. - -* **Agent**: It works as a constructor and de-constructor (defined functions). Generally agents have one *principal port* and *n*-fixed *auxiliary ports*. The fixed number of auxiliary ports is called *arity*, and it is determined according to each agent sort. In graphical representation an agent term `A(x1,...,xn)`, whose arity is *n*, is drawn as the following picture, where its auxiliary ports and the principal port (drawn as an arrow) correspond to the occurrences of the `x1`,...`xn`, and `A(x1,...,xn)`, respectively: - - ![agent](pic/agent.png) - - The `````` is defined as strings start with a capital letter, e.g. ```A``` and ```Succ```, and also `````` followed by a open curry bracket```(```. So, ```foo(x)``` is recognised as an agent. - - - -### Connections - -A **connection** is a relation between two terms, that means the two term are connected, and it is expressed with the symbol `~`. For instance, a connection between a name `x` and an agent `A` (whose arity is 0) is denoted as `x~A`. There is no order in the left-hand and the right-hand side terms, thus `x~A` and `A~x` are identified as the same one. - -* **Connections between a name and an agent** are evaluated as that the agent is connected from the name. For instance, `x~A` is evaluated as that the `A` is connected from the `x`. Here, as an example, type `x~A` with the termination symbol `;` as follows: - - ``` - >>> x~A; - (0 interactions, 0.00 sec) - >>> - ``` - - To show the connected terms from the name x, type just `x`: - ``` - >>> x; - A - >>> - ``` - - To dispose the name `x` and anything connected from the `x`, use `free` command: - ``` - >>> free x; - >>> x; - - >>> - ``` - - -* **Connections between names** are evaluated as that ports corresponding to these names are connected mutually in interaction nets frameworks. However, in Inpla, these are evaluated as that, for the connections, the left-hand side name connects to the right-hand side name, thus only one way. For instance, `x~y` is evaluated that the `x` connects to the `x` (not that the `y` connects to `x`): - - ``` - >>> x~y; - >>> x; - y - >>> y; - - >>> - ``` - -* **Connections between agents** are evaluated according to *interaction rules* explained later. - -Two connections that have the same name at the right or left hand sides, say `t~x` and `x~s`, are written as `t~s`. **We note** that the `x` is disposed after rewriting so that every connection can be kept one-to-one via names, cannot be one-to-many. -``` ->>> x~A, x~y; -(0 interactions, 0.00 sec) ->>> y; -A ->>> x; // x has been consumed by the re-connection of x~A and x~y. - ->>> -``` - -Just in case for other examples, let the `y` disposed: - -``` ->>> free y; ->>> y; - ->>> -``` - - - -## Interaction rules: rewriting rules for nets - -Connections between agents are rewritten according to **interaction rules**: - -``` - ::= '><' '=>' ';' - ::= - | '(' ',' ... ',' ')' -``` - with the **proviso** that: - - * each name that occur in the two `` must be **distinct** - * (**Linearity restriction**): every name in the `` must **occur twice**. - -Something complicated? No problem! Let's us learn how to define the rules with some example! - -### Example: Operations on unary natural numbers - -Unary natural numbers are built by Z and S. For instance, 0, 1, 2, 3 are expressed as Z, S(Z), S(S(Z)), S(S(S(Z))). Here, let's think about an increment operation `inc` such that inc(n) = S(n). This is written as rules for Z and S(x) as follows: - -``` -inc(ret) >< Z => ret~S(Z); -inc(ret) >< S(x) => ret~S(S(x)); -``` - -In the first rule, the name `ret` occurs twice, so it satisfies the rule proviso. The second rule is also OK because the `ret` and `x` are distinct and occur twice in the rule. - -For agents can work as constructors and de-constructors, it could be good to use strings start from a capital letter such as `Z`, `S` and `Tree`, and all small letters for de-constructors such as `inc`. - -Let's take the result of the increment operation for `S(S(Z))`: - -``` ->>> inc(r)~S(S(Z)); // This is also written as: r << inc(S(S(Z))) -(1 interactions, 0.01 sec) ->>> r; -S(S(S(Z)) ->>> -``` - -Good! We get `S(S(S(Z)))` as the result of incrementation of `S(S(Z))` . - -To show the result as a natural number, use `prnat` command: - -``` ->>> prnat result; -3 ->>> -``` - -Let's clean the result in case it could be used anywhere: - -``` ->>> free r; ->>> -``` - -* **Exercise**: Addition on unary natural numbers. - - It is defined recursively in term rewriting systems as follows: - - - add(x, Z) = x, - - add(x, S(y)) = add(S(x), y). - - This is written in interaction nets as follows: ![add1](pic/add1.png) - - In Inpla (and textual notation in interaction nets also), each agent is expressed as a term whose arguments correspond to its auxiliary ports. For instance, the `add` agent in the most left-hand side of the above figure, putting distinct names on auxiliary ports such as `ret` and `x`, is written as a term `add(ret,x)` by assembling these names anti-clockwise from the principal port (drawn as an arrow). - - Every computation is performed on connections between principal ports according to interaction rules. The rules shown in above figure are written textually as follows: - - ``` - add(ret, x) >< Z => ret~x; - add(ret, x) >< S(y) => add(ret, S(x))~y; - ``` - The following is an execution example: - ``` - >>> add(r,S(Z))~S(S(Z)); - (3 interactions, 0.00 sec) - >>> r; - S(S(S(Z))) - >>> prnat result; - 3 - >>> free r; - >>> - ``` - -* **Exercise**: Another version of the addition. - - There is another version as follows: - - - add(x,Z) = x, - - add(x, S(y)) = S(add(x,y)). - - These are written in interaction nets: ![add2](pic/add2.png) - - and written textually as follows: - - ``` - add(ret,x) >< Z => ret~x; - add(ret,x) >< S(y) => ret~S(cnt), add(cnt, x)~y; - ``` - In comparison with the previous example, calculation results can be sent partially as `S(cnt)` to other soon, whereas those in the previous one are stored until all of computation finish. So, **this version is suitable for parallel execution**. - - -### Abbreviation notation `<<`: -An abbreviation notation `<<` is defined as follows: -``` -a,b,...,z << Agent(aa,bb,...,yy,zz) == for == Agent(a,b,...,z,aa,bb,...,yy) ~ zz - ``` -For instance, `r << add(S(Z),S(S(Z)))` is rewritten internally as `add(r,S(Z))~S(S(Z))`. It is handy to denote ports that take computation results. - - -## Built-in Agents -Inpla has built-in agents: - -### Tuples - -* `Tuple0`, `Tuple2(x1,x2)`, `Tuple3(x1,x2,x2)`, `Tuple4(x1,x2,x3,x4)`, `Tuple5(x1,x2,x3,x4,x5)`, written as `()`, `(x1,x2)`, `(x1,x2,x3)`, `(x1,x2,x3,x4)`, `(x1,x2,x3,x4,x5)`, respectively. - -* There is no Tuple1. `(x)` is evaluated just as `x`. - - -### Lists - -* `Nil`, `Cons(x,xs)`, written as `[]` and `x:xs`, respectively. -* A nested `Cons` terminated with `Nil` are written as a list notation using brackets `[` and `]`. For instance, `x1 : x2: x3 : Nil` is written as `[x1,x2,x3]` . - -#### Built-in rules for tuples and lists - -There are built-in rules for the same built-in agents that each element is matched and connected such that: - -``` -(x1,x2)><(y1,y2) => x1~y1, x2~y2 // This is already defined as a built-in rule. -``` - -We also have a built-in agent `Append` to append two lists as shown in the following pseudo code: - -``` -Append(r, listB) ~ listA --> r ~ (listA ++ listB) // pseudo code -``` -The order `listB` and `listA` seems something strange, so a special abbreviation notation is prepared: -``` -r << Append(listA, listB) == for == Append(r, listB)~listA -``` - - -* Examples of the built-in rules: - - ``` - >>> (x1,x2)~(Z, S(Z)); - (1 interactions, 0.00 sec) - >>> x1 x2; - Z S(Z) - >>> free x1 x2; - >>> - ``` - - ``` - >>> [y1, y2, y3]~[Z, S(Z), S(S(Z))]; - (1 interactions, 0.00 sec) - >>> y1 y2 y3; - Z S(Z) S(S(Z)) - >>> free y1 y2 y3; - >>> - ``` - - ``` - >>> Append(r, [Z, S(Z)]) ~ [A,B,C]; - (4 interactions, 0.00 sec) - >>> r; - [A,B,C,Z,S(Z)] - >>> free r; - >>> - ``` - -* The abbreviation notation for the Append: - ``` - >>> r << Append([A,B,C], [Z, S(Z)]); - (4 interactions, 0.00 sec) - >>> r; - [A,B,C,Z,S(Z)] - >>> free r; - >>> - ``` - - - -### Eraser - -* Eraser agent `Eraser` disposes any nets gradually: - ![Eraser](pic/eraser.png) - - ``` - >>> Eraser ~ A(x1,x2); - (1 interactions, 0.00 sec) - >>> x1 x2 - Eraser Eraser - >>> free x1 x2; - >>> - ``` - - -### Duplicator -* `Dup` agent duplicates any agents: - ![Dup](pic/dup.png) - ``` - >>> Dup(a1,a2) ~ [Z, S(Z), S(S(Z))]; - (1 interactions, 0.00 sec) - >>> a1 a2 - [Z,S(Z),S(S(Z))] [Z,S(Z),S(S(Z))] - >>> free a1 a2; - >>> - ``` - - -## Attributes (integers) - -Agents are extended so that integers can be held on their ports. These integers are called *attributes*. - -* For instance, `A(100)` is an agent `A` that holds an attribute value `100`. - - ``` - >>> x~A(100); - (0 interactions, 0.01 sec) - >>> x; - A(100); - >>> free x; - >>> - ``` - - -### Built-in anonymous agent for attributes -We can use integers the same as agents, and these are recognised as attributes of a built-in *anonymous agent* in Inpla. For instance, we can write `x~100` where the `100` is an attribute value of the anonymous agent. - - - ``` - >>> x~100; // The `100' is regarded as an attribute of an anonymous agent - (0 interactions, 0.00 sec) - >>> x; - 100 - >>> free x; - >>> - ``` - - - -### Arithmetic expressions on attributes -It is possible to have attributes as calculation results of arithmetic operation using `where` statement after connections: - -``` - ::= - - | 'where' * ';' - - ::= '=' -``` - -The symbol of addition, subtraction, multiplication, division, modulo are `+`, `-`, `*`, `/` and `%`, respectively. Relational operations `<`, `<=`, `>` and `>=` return 1 for True, 0 for False. Logical operations Not `!` (`not`), And `&&` (`and`) and Or `||` (`or`) are also available where only `0` is regarded as False. - -* Example: an expression using `where`: - - ``` - >>> x~A(a) where a=3+5; - (0 interactions, 0.00 sec) - >>> x; - A(8) - >>> free x; - >>> - ``` - -* Arithmetic expressions can be also written in arguments directly: - - ``` - >>> x~A(3+5); - (0 interactions, 0.00 sec) - >>> x; - A(8) - >>> free x; - >>> - ``` - - -* Arithmetic expressions are also available for the anonymous agent: - - ``` - >>> x~(3+5); // this is also written without brackets as x~3+5; - (0 interactions, 0.00 sec) - >>> x; - 8 - >>> free x; - >>> - ``` - - -### Interaction rules with expressions on attributes -In interaction rules, attributes are recognised by using variables with a modifier `int` in the rule-agent parts , and we call these *attribute variables*. -**Attribute variables has no Linear restriction** because these do not affect keeping the one-to-one connection among agent ports. So, we can use the same attribute variable as many times as we want. - -* Example: Incrementor on an attribute: - - ``` - >>> inc(result) >< (int a) => result~(a+1); // The `a' is a variable for an attribute - >>> inc(r)~10; // This is also written as: r << inc(10) - (1 interactions, 0.00 sec) - >>> r; - 11 - >>> free r; - >>> - ``` - - -- Example: Duplicator of integer lists. - - ![dup_list](pic/dup_list.png) - - - This has been implemented as a built-in agent `Dup`, but let's take it as an example that the same attribute variable occurs twice. On the first line of the following execution example, twice occurrences of *i* in the right-hand side (here, `(i:xs1)` and `(i:xs2)`) is allowed in a rule. This is possible because the *i* is an attribute variable. - - ``` - >>> dup(a1,a2) >< (int i):xs => a1~(i:xs1), a2~(i:xs2), dup(xs1,xs2)~xs; - >>> dup(a1,a2) >< [] => a1~[], a2~[]; - >>> dup(a,b) ~ [1,2,3]; // This is also written as: a,b << dup([1,2,3]) - (4 interactions, 0.00 sec) - >>> a b; - [1,2,3] [1,2,3] - >>> free a b; - >>> - ``` - - - -**We have to be careful for operations of two attributes on distinct agents**. For instance, we take the following rule of an `add` agent: - -``` ->>> add(result, int b) >< (int a) => result~(a+b); -``` - -Of course, it works as an addition operation on two attributes: - -``` ->>> add(r, 3) ~ 5; -(1 interactions, 0.00 sec) ->>> r; -8 ->>> free r; ->>> -``` - -However, it causes runtime error if the `add` rule is invoked before an attribute value has been stored on the second argument of the `add`. For instance, we take the following computation: - -``` ->>> add(r, b)~3, add(b, 10)~20; -``` - -If the `add(r, b)~3` is operated first, it causes the runtime error because the `b` has not been connected to any attribute, whereas it is OK if the `add(b, 10)~20` is operated first. To prevent this fragile situation, **we have to have extra rules to ensure that every port with the modifier `int` has been connected to an attribute**. For the rule of the `add`, the following is a **solution** to have the extra rule (the `addn` is introduced as the extra agent): - -``` ->>> add(result, b) >< (int a) => addn(result, a) ~ b; ->>> addn(result, int a) >< (int b) => r~(a+b); -``` -Do not worry about it. Some built-in rules are prepared as follows: - - -### Built-in rules of attributes in the anonymous agents - -There are built-in rules for arithmetic operations between two agents by using `Add`, `Sub`, `Mul`, `Div`, `Mod` agents as addition, subtraction, multiplication, division and modulo, respectively. Rules for the `Add` are defined so that it can join two attributes on distinct agents safely as follows, and the others are also defined the same way: - -``` -Add(result, y)><(int x) => _Add(result, x)~y; -_Add(result, int x)><(int y) => result~(x+y); -``` - -* Example: - - ``` - >>> Add(r,3)~5; // Add is already defined as built-in. - >>> r; - 8 - >>> Sub(r1, r)~2; // It is also written as an abbreviation form: r1<>> r1; - 6 - >>> - ``` - - - -### Interaction rules with conditions on attributes -In interaction rules, conditional rewritings on attributes are available. The following is a general form: - -``` - ::= - '><' - '|' '=>' - '|' '=>' - ... - '|' '_' '=>' ';' - - is an expression on attributes specified in the two . -``` - -The sequence of `` must be finished with the otherwise case `_`. - -* Example: The following shows rules to obtain a list that contains only even numbers: - - ``` - // Rules - evenList(result) >< [] => r~[]; - evenList(result) >< (int x):xs - | x%2==0 => result~(x:cnt), evenList(cnt)~xs - | _ => evenList(result)~xs; - ``` - - ``` - >>> evenList(r)~[1,3,7,5,3,4,9,10]; - >>> r; - [4,10] - >>> free r; - >>> - ``` - - - -- Example: Fibonacci number: - - ``` - fib(result) >< (int n) - | n == 0 => result~0 - | n == 1 => result~1 - | _ => fib(r1)~(n-1), fib(r2)~(n-2), Add(result, r2)~r1; - - // * We cannot write result~(r1+r2) for Add(result, r2)~r1 - // because r1, r2 may have not been connected to attributes in the anonymous agents, - // thus we have to leave the addition until both fib(r1)~(n-1), fib(r2)~(n-2) are finished. - // For this purpose we have to use the addition operation between agents defined as - // Add(result, y)><(int x) => _Add(result, x)~y; - // _Add(result, int x)><(int y) => result~(x+y); - // By using Add(result, r2)~r1, - // after r1,r2 have been connected to (int x), (int y), respectively, - // x+y is executed and the calculation result is connected to the result safely. - ``` - - ``` - >>> fib(r)~39; - >>> r; - 63245986 - >>> free r; - >>> - ``` - - - -## Commands - -Inpla has the following commands: -* `free` *name1* ... *name_n* `;` - The *name1* ... *name_n* and connected terms from these are disposed. To dispose all living names and connected terms, type `free ifce;`, where the `ifce` is an abbreviation of *interface* that is called for the set of names that live and occur once. -* *name1* ... *name_n* `;` - Put terms connected from the *name1* ... *name_n*. To put all terms connected from the interface, type `ifce;`. -* `prnat` *name*`;` - Put a term connected from the *name* as a natural number. -* `use` `"`filename`";` - Read the file whose name is "filename". -* `exit;` - Quit the system. - -Inpla has the following macro: -* `const` *NAME*`=` *i* `;` - The *NAME* is bound to the integer value *i* as immutable, and replaced with the value *i* in nets and interaction rules. - - - -## Execution Options - -* When invoking Inpla, you can specify the following options: - - ``` - $ ./inpla -h - Inpla version 0.8.0 - Usage: inpla [options] - - Options: - -f Set input file name (Defalut: STDIN) - -d = Bind to - -Xms Set initial heap size to 2^ (Defalut: 12 (=4096)) - -Xmt Set multiple heap increment to 2^ (Defalut: 3 (= 8)) - 0: the same size heap is inserted when it runs up. - 1: the twice (=2^1) size heap is inserted. - -Xes Set initial equation stack size (Default: 256) - -w Enable Weak Reduction strategy (Default: false) - -t Set the number of threads (Default: 8) - -h Print this help message - ``` - -**Note**: - -* The option `-w` is available for the single-thread version. -* The option ```-t``` is available for the multi-thread version that is compiled by ```make thread```. The default value is setting for the number of cores, so execution will be automatically scaled without specifying this. - - diff --git a/README.md b/README.md index 71610b6..7ecd2f3 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Inpla is a multi-threaded parallel interpreter of interaction nets. Once you write programs for sequential execution, it works also in multi-threaded parallel execution. Each thread is managed on each CPU-core with POSIX-thread library. -* The current version is 0.8.3, released on **19 July 2022**. (See [Changelog.md](Changelog.md) for details.) +* The current version is 0.8.3-1 (minor update), released on **21 July 2022**. (See [Changelog.md](Changelog.md) for details.) * The below graph shows speed-up ratio to threads numbers for programs in the following benchmark table. ![speedup-ratio](pic/benchmark_reuse_v0.8.2-2.png) @@ -262,7 +262,7 @@ Inpla is a multi-threaded parallel interpreter of interaction nets. Once you wri ## How to write programs in Inpla -[How_to_write_programs.md](How_to_write_programs.md) explains how to make programs in Inpla step by step. Please see it! +[Gentle_introduction_Inpla.md](Gentle_introduction_Inpla.md) explains how to make programs in Inpla step by step. Please look it over! ## Updates 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, b~A; -// Dup(a,b)> a~B, b~B; -// Dup(a,b)> 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; diff --git a/src/inpla.y b/src/inpla.y index 79e460e..bc9cfc2 100644 --- a/src/inpla.y +++ b/src/inpla.y @@ -22,8 +22,8 @@ // ---------------------------------------------- -#define VERSION "0.8.3" -#define BUILT_DATE "19 July 2022" +#define VERSION "0.8.3-1" +#define BUILT_DATE "21 July 2022" // ------------------------------------------------------------------ @@ -9103,32 +9103,89 @@ loop_agent_a1_a2_this_order: a1=a1p; goto loop; } + + // a2p0 などが fixint の場合は即座に複製させる case 1: { // Dup(p0,p1) >< A(a2p0) => p1~A(w2), p0~A(w1), - // Dup(w1,w2)~a2p0; + // Dup(w1,w2)~a2p0; + // Dup(p0,p1) >< A(int a2p0) => p1~A(a2p0), p0~A(a2p0); + + VALUE a2p0 = AGENT(a2)->port[0]; + int a2id = BASIC(a2)->id; // p1 VALUE new_a2 = make_Agent(vm, a2id); - VALUE w = make_Name(vm); - AGENT(new_a2)->port[0] = w; - PUSH(vm, AGENT(a1)->port[1], new_a2); - AGENT(a1)->port[1] = w; - - // p0 - w = make_Name(vm); - AGENT(a2)->port[0] = w; - PUSH(vm, AGENT(a1)->port[0], a2); - AGENT(a1)->port[0] = w; - a2 = a2p0; - goto loop; + if (IS_FIXNUM(a2p0)) { + AGENT(new_a2)->port[0] = a2p0; + PUSH(vm, AGENT(a1)->port[1], new_a2); + AGENT(a2)->port[0] = a2p0; + + VALUE a1p0 = AGENT(a1)->port[0]; + free_Agent(a1); + + //PUSH(vm, a1p0, a2); + a1 = a1p0; + goto loop; + + } else { + VALUE w = make_Name(vm); + AGENT(new_a2)->port[0] = w; + PUSH(vm, AGENT(a1)->port[1], new_a2); + AGENT(a1)->port[1] = w; // for (*L)dup + + w = make_Name(vm); + AGENT(a2)->port[0] = w; + PUSH(vm, AGENT(a1)->port[0], a2); + + AGENT(a1)->port[0] = w; // for (*L)dup + + a2 = a2p0; + goto loop; + } + } + break; + case 2: + { + // Dup(p0,p1) >< Cons(int i, a2p1) => + // p0~Cons(i,w), p1~(*R)Cons(i,ww), (*L)Dup(w,ww)~a2p1; + + VALUE a2p0 = AGENT(a2)->port[0]; + if (IS_FIXNUM(a2p0)) { + int a2id = BASIC(a2)->id; + VALUE new_a2 = make_Agent(vm, a2id); + + AGENT(new_a2)->port[0] = a2p0; + + VALUE w = make_Name(vm); + AGENT(new_a2)->port[1] = w; + + PUSH(vm, AGENT(a1)->port[0], new_a2); + AGENT(a1)->port[0] = w; + + VALUE a2p1 = AGENT(a2)->port[1]; + VALUE ww = make_Name(vm); + AGENT(a2)->port[1] = ww; + + PUSH(vm, AGENT(a1)->port[1], a2); + AGENT(a1)->port[1] = ww; + + a2 = a2p1; + goto loop; + } + + // otherwise: goto default + // So, DO NOT put break. + } + + default: { // Dup(p0,p1) >< A(a2p0, a2p1) => @@ -9146,56 +9203,98 @@ loop_agent_a1_a2_this_order: // Dup(w2,ww2)~a2p2, // Dup(w3,ww3)~a2p3; + + // Dup(p0,p1) >< A(a2p0, int a2p1, a2p2 a2p3) => + // p0~A(w0,a2p1,w2,w3), (*L)Dup(w0,ww0)~a2p0 + // p1~(*R)A(ww0,a2p1,ww2,ww3), + // Dup(w2,ww2)~a2p2, + // Dup(w3,ww3)~a2p3; + + // newA = mkAgent(A); - // for (i=0; ip[i] = w_i; } - // + // for (i=0; ip[i])) { + // newA->p[i] = w_i; + // } else { + // newA->p[i] = a2->p[i]; + // } + // } // for (i=1; ip[i], newWW_i) ~ a2[i] - // a2[i] = newWW_i; + // if (!IS_FIXNUM(a2->p[i])) { + // newDup( newA->p[i], newWW_i) ~ a2[i] + // a2[i] = newWW_i; + // } // } - // p0_preserve = p0; - // p1_preserve = p1; - // (*L)Dup(w0, newWW0) ~ a2[0] // this destroys the p0 and p1. + // if (!IS_FIXNUM(a2->p[0])) { + // p0_preserve = p0; + // p1_preserve = p1; + // (*L)Dup(w0, newWW0) ~ a2[0] // this destroys the p0 and p1. // - // p0_preserve ~ newA - // a2[0] = newWW0; - - // p1_preserve ~ a2; <-- This will be `goto loop`. - + // p0_preserve ~ newA + // a2[0] = newWW0; + // + // p1_preserve ~ a2; <-- This will be `goto loop'. + // } else { + // p0 ~ newA; + // p1_preserve = p1; + // free_Agent((*L)Dup); + // p1_preserve ~ a2; >-- This will be `goto loop'. + // } int a2id = BASIC(a2)->id; VALUE new_a2 = make_Agent(vm, a2id); for (int i=0; iport[i] = make_Name(vm); + VALUE a2pi = AGENT(a2)->port[i]; + if (!IS_FIXNUM(a2pi)) { + AGENT(new_a2)->port[i] = make_Name(vm); + } else { + AGENT(new_a2)->port[i] = a2pi; + } } for (int i=1; iport[0] = AGENT(new_a2)->port[i]; + VALUE a2pi = AGENT(a2)->port[i]; - VALUE new_ww = make_Name(vm); - AGENT(new_dup)->port[1] = new_ww; + if (!IS_FIXNUM(a2pi)) { + + VALUE new_dup = make_Agent(vm, ID_DUP); + AGENT(new_dup)->port[0] = AGENT(new_a2)->port[i]; - PUSH(vm, new_dup, AGENT(a2)->port[i]); + VALUE new_ww = make_Name(vm); + AGENT(new_dup)->port[1] = new_ww; + + PUSH(vm, new_dup, a2pi); - AGENT(a2)->port[i] = new_ww; + AGENT(a2)->port[i] = new_ww; + } } + + VALUE a2p0 = AGENT(a2)->port[0]; + if (!IS_FIXNUM(a2p0)) { - VALUE a1p0 = AGENT(a1)->port[0]; - VALUE a1p1 = AGENT(a1)->port[1]; + VALUE a1p0 = AGENT(a1)->port[0]; + VALUE a1p1 = AGENT(a1)->port[1]; - AGENT(a1)->port[0] = AGENT(new_a2)->port[0]; - VALUE new_ww = make_Name(vm); - AGENT(a1)->port[1] = new_ww; - PUSH(vm, a1, AGENT(a2)->port[0]); + AGENT(a1)->port[0] = AGENT(new_a2)->port[0]; + VALUE new_ww = make_Name(vm); + AGENT(a1)->port[1] = new_ww; + PUSH(vm, a1, a2p0); - PUSH(vm, a1p0, new_a2); + PUSH(vm, a1p0, new_a2); - AGENT(a2)->port[0] = new_ww; + AGENT(a2)->port[0] = new_ww; - a1 = a1p1; + a1 = a1p1; - goto loop; + goto loop; + } else { + PUSH(vm, AGENT(a1)->port[0], new_a2); + VALUE a1p1 = AGENT(a1)->port[1]; + free_Agent(a1); + + a1=a1p1; + goto loop; + } } -- cgit v1.2.3 From 1f51c41a32e190d483ff336c34eb63acda27cbb6 Mon Sep 17 00:00:00 2001 From: Shinya Sato Date: Thu, 21 Jul 2022 14:56:47 +0900 Subject: Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7ecd2f3..e2eebc1 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Inpla is a multi-threaded parallel interpreter of interaction nets. Once you wri - The above table contains execution time in second on average of ten times execution by using Linux PC (Core i7-9700 (8 threads, no Hyper-threading), 16GB memory). The fastest one is shown with bold style. Scripts for the comparison table are in the `comparison` directory. - - Inpla8 and Inpla8r mean 8 threads without/with [reuse-annotated](#Reuse-annotations) execution, respectively. + - Inpla8 and Inpla8r mean 8 threads without/with [reuse-annotated](./Gentle_introduction_Inpla.md#reuse-annotations) execution, respectively. - "ack(3,11)" is computation of Ackermann function. Execution time of Python is a blank due to stack size limitation error. -- cgit v1.2.3