Skip to content

Commit

Permalink
setup ts project
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Nov 9, 2023
1 parent 347455a commit 7e48d9c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 193 deletions.
196 changes: 8 additions & 188 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Petri Net (JS)
# Petri Net JS

This is an implementation of [Petri Net](https://en.wikipedia.org/wiki/Petri_net).
This is an implementation of [Petri net](https://en.wikipedia.org/wiki/Petri_net).

## Usage

Expand All @@ -12,191 +12,17 @@ Install it by the following command:
npm install --global @cicada-lang/petri-net-js
```

The command-line program is called `inet-js`.
The command-line program is called `petri-net-js`.

```sh
inet-js repl # Open an interactive REPL
inet-js run [path] # Run an inet program
inet-js help [name] # Display help for a command
petri-net-js repl # Open an interactive REPL
petri-net-js run [path] # Run an inet program
petri-net-js help [name] # Display help for a command
```

## Examples

### Nat

[ [Goto The Playground](https://inet.run/playground/dHlwZSBOYXQKCm5vZGUgemVybygKICAtLS0tLS0KICB2YWx1ZSE6IE5hdAopCgpub2RlIGFkZDEoCiAgcHJldjogTmF0CiAgLS0tLS0tLS0tLQogIHZhbHVlITogTmF0CikKCm5vZGUgYWRkKAogIHRhcmdldCE6IE5hdCwKICBhZGRlbmQ6IE5hdAogIC0tLS0tLS0tCiAgcmVzdWx0OiBOYXQKKQoKcnVsZSBhZGQodGFyZ2V0ISwgYWRkZW5kLCByZXN1bHQpIHplcm8odmFsdWUhKSB7CiAgQGNvbm5lY3QoYWRkZW5kLCByZXN1bHQpCn0KCnJ1bGUgYWRkKHRhcmdldCEsIGFkZGVuZCwgcmVzdWx0KSBhZGQxKHByZXYsIHZhbHVlISkgewogIGFkZDEoYWRkKHByZXYsIGFkZGVuZCksIHJlc3VsdCkKfQoKZnVuY3Rpb24gb25lKCk6IE5hdCB7CiAgcmV0dXJuIGFkZDEoemVybygpKQp9CgpmdW5jdGlvbiB0d28oKTogTmF0IHsKICByZXR1cm4gYWRkKG9uZSgpLCBvbmUoKSkKfQoKZnVuY3Rpb24gdGhyZWUoKTogTmF0IHsKICByZXR1cm4gYWRkKHR3bygpLCBvbmUoKSkKfQoKZnVuY3Rpb24gZm91cigpOiBOYXQgewogIHJldHVybiBhZGQodHdvKCksIHR3bygpKQp9CgovLyBURVNUCgpldmFsIEBpbnNwZWN0KEBydW4oYWRkKHR3bygpLCB0d28oKSkpKQpldmFsIEBpbnNwZWN0KGFkZCh0d28oKSwgdHdvKCkpKQ) ]

```inet
type Nat
node zero(
------
value!: Nat
)
node add1(
prev: Nat
----------
value!: Nat
)
node add(
target!: Nat,
addend: Nat
--------
result: Nat
)
rule add(target!, addend, result) zero(value!) {
@connect(addend, result)
}
rule add(target!, addend, result) add1(prev, value!) {
add1(add(prev, addend), result)
}
function one(): Nat {
return add1(zero())
}
function two(): Nat {
return add(one(), one())
}
function three(): Nat {
return add(two(), one())
}
function four(): Nat {
return add(two(), two())
}
// TEST
eval @inspect(@run(add(two(), two())))
eval @inspect(add(two(), two()))
```

### List

[ [Goto The Playground](https://inet.run/playground/dHlwZSBMaXN0KEVsZW1lbnQ6IEBUeXBlKQoKbm9kZSBudWxsKAogIC0tLS0tLS0tCiAgdmFsdWUhOiBMaXN0KCdBKQopCgpub2RlIGNvbnMoCiAgaGVhZDogJ0EsCiAgdGFpbDogTGlzdCgnQSkKICAtLS0tLS0tLQogIHZhbHVlITogTGlzdCgnQSkKKQoKbm9kZSBhcHBlbmQoCiAgdGFyZ2V0ITogTGlzdCgnQSksCiAgcmVzdDogTGlzdCgnQSkKICAtLS0tLS0tLQogIHJlc3VsdDogTGlzdCgnQSkKKQoKcnVsZSBhcHBlbmQodGFyZ2V0ISwgcmVzdCwgcmVzdWx0KSBudWxsKHZhbHVlISkgewogIEBjb25uZWN0KHJlc3QsIHJlc3VsdCkKfQoKcnVsZSBhcHBlbmQodGFyZ2V0ISwgcmVzdCwgcmVzdWx0KSBjb25zKGhlYWQsIHRhaWwsIHZhbHVlISkgewogIGNvbnMoaGVhZCwgYXBwZW5kKHRhaWwsIHJlc3QpLCByZXN1bHQpCn0KCi8vIFRFU1QKCnR5cGUgVHJpdmlhbAoKbm9kZSBzb2xlKC0tIHZhbHVlITogVHJpdmlhbCkKCmZ1bmN0aW9uIHNpeFNvbGVzKCk6IExpc3QoVHJpdmlhbCkgewogIHJldHVybiBhcHBlbmQoCiAgICBjb25zKHNvbGUoKSwgY29ucyhzb2xlKCksIGNvbnMoc29sZSgpLCBudWxsKCkpKSksCiAgICBjb25zKHNvbGUoKSwgY29ucyhzb2xlKCksIGNvbnMoc29sZSgpLCBudWxsKCkpKSksCiAgKQp9CgpldmFsIEBpbnNwZWN0KEBydW4oc2l4U29sZXMoKSkpCmV2YWwgQGluc3BlY3Qoc2l4U29sZXMoKSk) ]

```inet
type List(Element: @Type)
node null(
--------
value!: List('A)
)
node cons(
head: 'A,
tail: List('A)
--------
value!: List('A)
)
node append(
target!: List('A),
rest: List('A)
--------
result: List('A)
)
rule append(target!, rest, result) null(value!) {
@connect(rest, result)
}
rule append(target!, rest, result) cons(head, tail, value!) {
cons(head, append(tail, rest), result)
}
// TEST
type Trivial
node sole(-- value!: Trivial)
function sixSoles(): List(Trivial) {
return append(
cons(sole(), cons(sole(), cons(sole(), null()))),
cons(sole(), cons(sole(), cons(sole(), null()))),
)
}
eval @inspect(@run(sixSoles()))
eval @inspect(sixSoles())
```

### DiffList

[ [Goto The Playground](https://inet.run/playground/aW1wb3J0IHsgTGlzdCB9IGZyb20gImh0dHBzOi8vY29kZS1vZi1pbmV0LWpzLmZpZGIuYXBwL3N0ZC9kYXRhdHlwZS9MaXN0LmkiCgovLyBDb25jYXRlbmF0aW9uIG9mIGxpc3RzIGlzIHBlcmZvcm1lZCBpbiBsaW5lYXIgdGltZQovLyB3aXRoIHJlc3BlY3QgdG8gaXRzIGZpcnN0IGFyZ3VtZW50LgovLyBDb25zdGFudCB0aW1lIGNvbmNhdGVuYXRpb24gaXMgcG9zc2libGUKLy8gd2l0aC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C5pIgoKdHlwZSBUcml2aWFsCgpub2RlIHNvbGUoLS0gdmFsdWUhOiBUcml2aWFsKQoKZnVuY3Rpb24gdHdvVHdvU29sZXMoKTogRGlmZkxpc3QoVHJpdmlhbCkgewogIGxldCBmcm9udCwgYmFjaywgdmFsdWUxID0gZGlmZigpCiAgQGNvbm5lY3QoZnJvbnQsIGNvbnMoc29sZSgpLCBjb25zKHNvbGUoKSwgYmFjaykpKQogIGxldCBmcm9udCwgYmFjaywgdmFsdWUyID0gZGlmZigpCiAgQGNvbm5lY3QoZnJvbnQsIGNvbnMoc29sZSgpLCBjb25zKHNvbGUoKSwgYmFjaykpKQogIHJldHVybiBkaWZmQXBwZW5kKHZhbHVlMSwgdmFsdWUyKQp9CgpldmFsIEBpbnNwZWN0KEBydW4odHdvVHdvU29sZXMoKSkpCmV2YWwgQGluc3BlY3QodHdvVHdvU29sZXMoKSk) ]

```inet
import { List } from "https://code-of-inet-js.fidb.app/std/datatype/List.i"
// Concatenation of lists is performed in linear time
// with respect to its first argument.
// Constant time concatenation is possible
// with difference-lists: the idea consists in
// plugging the front of the second argument
// at the back of the first one.
type DiffList(Element: @Type)
node diff(
front: List('A),
-------
back: List('A),
value!: DiffList('A),
)
node diffAppend(
target!: DiffList('A),
rest: DiffList('A)
--------
result: DiffList('A)
)
node diffOpen(
target!: DiffList('A),
newBack: List('A)
----------
oldBack: List('A)
)
rule diffAppend(target!, rest, result)
diff(front, back, value!) {
let newBack, value = diff(front)
@connect(value, result)
diffOpen(rest, newBack, back)
}
rule diffOpen(target!, newBack, oldBack)
diff(front, back, value!) {
@connect(back, newBack)
@connect(front, oldBack)
}
// TEST
import { cons } from "https://code-of-inet-js.fidb.app/std/datatype/List.i"
type Trivial
node sole(-- value!: Trivial)
function twoTwoSoles(): DiffList(Trivial) {
let front, back, value1 = diff()
@connect(front, cons(sole(), cons(sole(), back)))
let front, back, value2 = diff()
@connect(front, cons(sole(), cons(sole(), back)))
return diffAppend(value1, value2)
}
eval @inspect(@run(twoTwoSoles()))
eval @inspect(twoTwoSoles())
```
TODO

## Development

Expand All @@ -211,13 +37,7 @@ npm run test # Run test

**Papers**:

- [Interaction Nets, Yves Lafont, 1990 (the founding paper)](./docs/references/papers/1990-interaction-nets.pdf).
- [Interaction Combinators, Yves Lafont, 1997](./docs/references/papers/1997-interaction-combinators.pdf).

**Books**:

- [Models of Computation -- An Introduction to Computability Theory, Maribel Fernández, 2009](./docs/references/books/models-of-computation--maribel-fernández.pdf).
- Chapter 7. Interaction-Based Models of Computation.
TODO

## Community

Expand Down
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
setup ts project
publish project
docs syntax design
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "@cicada-lang/inet-js",
"version": "0.2.8",
"repository": "github:cicada-lang/inet-js",
"name": "@cicada-lang/petri-net-js",
"version": "0.1.0",
"repository": "github:cicada-lang/petri-net-js",
"main": "./lib/index.js",
"files": [
"lib"
],
"bin": {
"inet-js": "bin/inet-js"
"petri-net-js": "bin/petri-net-js"
},
"scripts": {
"build": "tsc",
Expand Down

0 comments on commit 7e48d9c

Please sign in to comment.