inpla/inpla
{ "createdAt": "2021-09-24T07:38:52Z", "defaultBranch": "main", "description": "Inpla: Interaction nets as a programming language (the current version)", "fullName": "inpla/inpla", "homepage": "", "language": "Yacc", "name": "inpla", "pushedAt": "2024-10-30T12:21:47Z", "stargazersCount": 316, "topics": [], "updatedAt": "2025-11-21T17:36:57Z", "url": "https://github.com/inpla/inpla"}Inpla: Interaction nets as a programming language
Section titled “Inpla: Interaction nets as a programming language”What is Inpla
Section titled “What is Inpla”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.13.0-2, released on 12 September 2024. (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.13.0.png)
- Comparison in execution time with other implementations: Haskell (GHC version 8.10.7), OCaml (ocamlopt, the native-code compiler, version 4.13.1), Standard ML of New Jersey v110.74 (interpreter mode) and Python 3.10.6 in execution time.
| Haskell | OCaml | SML | Python | Inpla8 | Inpla8r | |
|---|---|---|---|---|---|---|
| n-queens 12 | [0.23]!(comparison/Haskell/nqueen-12.hs) | [0.44]!(comparison/OCaml/nqueen12.ml) | [0.60]!(comparison/SML/nqueen-12.sml) | [3.79]!(comparison/Python/nqueen-12.py) | [0.55]!(comparison/Inpla/src/nqueen-12.in) | [0.44]!(comparison/Inpla/src/nqueen-12-reuse.in) |
| ack(3,11) | [2.37]!(comparison/Haskell/ack3-11.hs) | [0.57]!(comparison/OCaml/ack3_11.ml) | [0.42]!(comparison/SML/ack3-11.sml) | [-]!(comparison/Python/ack3-11.py) | [0.90]!(comparison/Inpla/src/ack-stream_3-11.in) | [0.72]!(comparison/Inpla/src/ack-stream_3-11-reuse.in) |
| fib 38 | [1.61]!(comparison/Haskell/fib-38.hs) | [0.15]!(comparison/OCaml/fib38.ml) | [0.27]!(comparison/SML/fib-38.sml) | [9.27]!(comparison/Python/fib-38.py) | [0.46]!(comparison/Inpla/src/fib-38.in) | [0.46]!(comparison/Inpla/src/fib-38-reuse.in) |
| bsort 20000 | [5.03]!(comparison/Haskell/bsort-20000.hs) | [6.47]!(comparison/OCaml/bsort20000.ml) | [2.39]!(comparison/SML/bsort-20000.sml) | [20.02]!(comparison/Python/bsort-20000.py) | [2.41]!(comparison/Inpla/src/bsort-20000.in) | [1.56]!(comparison/Inpla/src/bsort-20000-reuse.in) |
| isort 20000 | [2.15]!(comparison/Haskell/isort-20000.hs) | [1.48]!(comparison/OCaml/isort20000.ml) | [0.60]!(comparison/SML/isort-20000.sml) | [8.83]!(comparison/Python/isort-20000.py) | [0.33]!(comparison/Inpla/src/isort-20000.in) | [0.35]!(comparison/Inpla/src/isort-20000-reuse.in) |
| qsort 260000 | [0.36]!(comparison/Haskell/qsort-800000.hs) | [0.22]!(comparison/OCaml/qsort260000.ml) | [0.27]!(comparison/SML/qsort-260000.sml) | [10.33]!(comparison/Python/qsort-260000.py) | [0.15]!(comparison/Inpla/src/qsort-260000.in) | [0.11]!(comparison/Inpla/src/qsort-260000-reuse.in) |
| msort 260000 | [0.38]!(comparison/Haskell/msort-800000.hs) | [0.17]!(comparison/OCaml/msort260000.ml) | [0.29]!(comparison/SML/msort-260000.sml) | [11.09]!(comparison/Python/msort-260000.py) | [0.14]!(comparison/Inpla/src/msort-260000.in) | [0.13]!(comparison/Inpla/src/msort-260000-reuse.in) |
-
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
comparisondirectory. These are written to have the same algorithms as much as possible, though only the Ackermann function in Inpla is a little optimised to interaction nets computing so that it can take advantage of the parallelism well. -
Inpla8 and Inpla8r mean 8 threads without/with [reuse-annotated]!(./Gentle_introduction_Inpla.md#reuse-annotations) execution, respectively. Inpla8 is called with an enable switch to optimise tail calls, as it is off by default since v0.12.2. Inpla8r is not called with this option because this optimisation will not work if the last equation in a rule has the reuse annotations.
-
“n-queens 12” is computation of n-Queens at 12.
-
“ack(3,11)” is computation of Ackermann function. Execution time of Python is a blank due to stack size limitation error.
-
“fib 38” is computation to get the 38th Fibonacci number.
-
“bsort n”, “isort n”, “qsort n” and “msort n” are computation of bubble sort, insertion sort, quick sort and merge sort for random n-element lists, respectively, followed by a validation check process. In the graph quick sort and merge sort are for 800000 elements because the 260000 elements are too small for Inpla to show the performance in parallel. See the description of v0.8.2-1 in [Changelog.md]!(./Changelog.md) for details.
Contents
Section titled “Contents”- [Getting Started]!(#getting-started)
- [How to Execute]!(#how-to-execute)
- [Interactive mode (single-thread version)]!(#interactive-mode-single-thread-version)
- [Interactive mode (multi-thread version)]!(#interactive-mode-multi-thread-version)
- [Batch mode and sample files]!(#batch-mode-and-sample-files)
- [How to write programs in Inpla]!(#how-to-write-programs-in-inpla)
- [Updates]!(#updates)
- [Publications]!(#publications)
- [Related Works]!(#related-works)
- [License]!(#license)
Getting Started
Section titled “Getting Started”-
Requirement
- gcc (>= 4.0), flex, bison
-
Build
-
Single-thread version: Use
makecommand as follows (the symbol$means a shell prompt):$ make -
Multi-thread version: Use
makewiththreadoption:$ make threadTo get the single-thread version again, use
make clean; make.
-
How to Execute
Section titled “How to Execute”Interactive mode (single-thread version)
Section titled “Interactive mode (single-thread version)”-
Inpla starts in the interactive mode by typing the following command (where the symbol
$is a shell prompt):$ ./inplaInpla 0.10.0 : Interaction nets as a programming language [built: 17 September 2022]>>> -
The symbol
>>>is a prompt of Inpla. After the prompt you can write rules and nets. For instance, the following is a rule for incrementationincand a net to bind the increment result of10to a namer(where//is a comment):>>> inc(ret) >< (int i) => ret~(i+1); // a rule for inc >< (int i)>>> inc(r)~10; // a net(1 interactions, 0.16 sec)>>> r; // show a connected net from the r11>>> -
To quit this system, use
exitcommand:>>> exit;
Interactive mode (multi-thread version)
Section titled “Interactive mode (multi-thread version)”-
There is an execution option
-tthat specifies the number of threads in a thread pool. For instance, by invoking with-t 4Inpla populates 4 threads in the pool:$ ./inpla -t 4The default value is setting for the number of cores, so execution will be automatically scaled without specifying this. This option is useful to see the effect by number of threads.
Batch mode and sample files
Section titled “Batch mode and sample files”- Inpla has also the batch mode in which a file is evaluated. This is available when invoked with an execution option
-ffilename. There are sample files in thesamplefolder. Here we introduce some of ones:
Greatest common divisor
Section titled “Greatest common divisor”-
Sample file:
sample/gcd.in// Example program in Python// def gcd(a, b):// if b==0: return a// else: return gcd(b, a%b)// Rulesgcd(ret) >< (int a, int b)| b==0 => ret ~ a| _ => gcd(ret) ~ (b, a%b);// Netsgcd(r) ~ (14,21);r; // it should be 7-
Execution:
$ ./inpla -f sample/gcd.inInpla 0.10.0 : Interaction nets as a programming language [built: 17 September 2022](4 interactions, 0.00 sec)7$
-
Insertion sort
Section titled “Insertion sort”![insertion sort]!(pic/isort.png)
-
Sample file:
sample/isort.in// Rulesinsert(ret, int x) >< [] => ret~[x];insert(ret, int x) >< (int y):ys| x<=y => ret~(x:y:ys)| _ => ret~(y:cnt), insert(cnt, x)~ys;isort(ret) >< [] => ret~[];isort(ret) >< x:xs => insert(ret, x)~cnt, isort(cnt)~xs;// Netsisort(r)~[3,6,1,9,2];r;-
Execution:
$ ./inpla -f sample/isort.inInpla 0.10.0 : Interaction nets as a programming language [built: 17 September 2022](16 interactions, 0.00 sec)[1,2,3,6,9]$
-
Quick sort
Section titled “Quick sort”![quick sort]!(pic/qsort.png)
-
Sample file:
sample/qsort.in// Rulesqsort(ret) >< [] => ret~[];qsort(ret) >< (int x):xs =>ret << Append(left, x:right), part(smaller, larger, x)~xs,qsort(left)~smaller, qsort(right)~larger;// Note: `Append' is implemented as the following built-in agent:// Append(ret, b)~a --> ret ~ a++b// The ret << Append(left, x:right) is rewritten// by the built-in abbreviation into:// Append(ret, x:right)~left.part(smaller, larger, int x) >< [] => smaller~[], larger~[];part(smaller, larger, int x) >< (int y):ys| y<x => smaller~(y:cnt), part(cnt, larger, x)~ys| _ => larger~(y:cnt), part(smaller, cnt, x)~ys;// Netsqsort(r)~[3,6,1,9,2];r;These rules and nets are also written by using abbreviation:
-
Abbreviation notation version:
An abbreviation notation
<<is introduced in v.0.5.0. We can write also as follows by the<<:a,b,...,z << Agent(aa,bb,...,yy,zz) == for == Agent(a,b,...,z,aa,bb,...,yy) ~ zzFor instance,
r << Add(1,2)is rewritten internally asAdd(r,1)~2. It is handy to denote ports that take computation results. The following is another version by using the abbreviation:// Rulesqsort(ret) >< [] => ret~[];qsort(ret) >< (int x):xs =>ret << Append(left, x:right),smaller, larger << part(x,xs),left << qsort(smaller), right << qsort(larger);part(smaller, larger, int x) >< [] => smaller~[], larger~[];part(smaller, larger, int x) >< (int y):ys| y<x => smaller~(y:cnt),cnt, larger << part(x,ys)| _ => larger~(y:cnt),smaller, cnt << part(x,ys);// Netsr << qsort([3,6,1,9,2]);r; -
Execution:
$ ./inpla -f sample/qsort.inInpla 0.10.0 : Interaction nets as a programming language [built: 17 September 2022](22 interactions, 0.00 sec)[1,2,3,6,9]$
-
Other samples
Section titled “Other samples”-
Evaluation of a lambda term
245IIin YALE encoding, where2,4,5mean church numbers of lambda terms, andIis a lambda term $\lambda x.x$:$ ./inpla -f sample/245II.in -
Samples of linear systemT encoding (see our paper presented at FLOPS 2016).
$ ./inpla -f sample/linear-systemT.in
How to write programs in Inpla
Section titled “How to write programs in Inpla”[Gentle_introduction_Inpla.md]!(Gentle_introduction_Inpla.md) explains how to make programs in Inpla step by step. Please look it over!
Updates
Section titled “Updates”See [Changelog.md]!(Changelog.md) for details.
Publications
Section titled “Publications”- [1] Ian Mackie, Shinya Sato, In-place Graph Rewriting with Interaction Nets, TERMGRAPH 2016, EPTCS 225, pp.15-24, 2016.
- [2] Shinya Sato, Design and implementation of a low-level language for interaction nets, PhD Thesis, University of Sussex, September 2014.
- [3] Abubakar Hassan, Ian Mackie and Shinya Sato, An implementation model for interaction nets, Proceedings 8th International Workshop on Computing with Terms and Graphs, TERMGRAPH 2014, EPTCS 183, May 2015.
- [4] Ian Mackie and Shinya Sato, Parallel Evaluation of Interaction Nets: Case Studies and Experiments, Electronic Communications of the EASST, Volume 73: Graph Computation Models - Selected Revised Papers from GCM 2015, March 2016.
Related Work
Section titled “Related Work”- High-order Virtual Machine (HVM)
- HINet: Interaction Nets in Haskell
- A Rust-Based Interaction Combinator Runtime
- Language, interpreter and compiler for interaction nets
- Interaction Nets in OCaml
- ingpu - an experimental GPU-based interaction nets evaluator
My repository
Section titled “My repository”License
Section titled “License”Copyright (c) 2022 Shinya Sato Released under the MIT license http://opensource.org/licenses/mit-license.php