GithubHelp home page GithubHelp logo

cicada-lang / inet-js Goto Github PK

View Code? Open in Web Editor NEW
41.0 1.0 1.0 4.01 MB

This is an implementation of interaction nets. It introduces you to the bizarre world of graph-based computation and linear logic, using a familiar JavaScript-like syntax :)

Home Page: https://inet.run

License: GNU General Public License v3.0

JavaScript 0.07% Assembly 4.78% TypeScript 88.99% SWIG 6.16%
computation-model graph graph-processing inet interaction-nets interpreter language-design programming-language programming-language-concepts programming-language-design programming-language-development undirected-graph language

inet-js's Introduction

iNet JS

[ Website | iNet Cute ]

This is an implementation of interaction nets. It introduces you to the bizarre world of graph-based computation and linear logic, using a familiar JavaScript-like syntax :)

Usage

Command line tool

Install it by the following command:

npm install --global @cicada-lang/inet-js

The command-line program is called inet-js.

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

Examples

Nat

[ Goto The Playground ]

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 ]

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 ]

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())

Development

npm install     # Install dependencies
npm run build   # Compile `src/` to `lib/`
npm run test    # Run test

References

Papers:

Books:

Contributions

To make a contribution, fork this project and create a pull request.

Please read the STYLE-GUIDE.md before you change the code.

Remember to add yourself to AUTHORS. Your line belongs to you, you can write a little introduction to yourself but not too long.

License

GPLv3

inet-js's People

Contributors

xieyuheng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

gacogo

inet-js's Issues

Odd type-checking fail with generic function parameters

Reproduction:

// Setup, a generic type and a few helper nodes
type Gen(Element: @Type)
type Normal
node del(
  value!: Normal
  ------
)
node normal(
  ------
  value!: Normal,
)
node passGen(
  valueIn!: Gen('A)
  ------
  valueOut: Gen('A)
)

// As a node, typechecks perfectly:
node testNode(
  paramGen: Gen(Normal),
  paramNormal!: Normal
  ------
  out: Gen(Normal),
)
rule testNode(paramGen, paramNormal!, out) normal(value!) {
  let paramNormal = normal()

  del(paramNormal)
  let variable = passGen(paramGen)

  @connect(variable, out)
}

// As a function, fails to typecheck: (observe that the types and body are the same; the node just needs a few extra wrapper lines)
function testFunction(paramGen: Gen(Normal), paramNormal: Normal): Gen(Normal) {

  del(paramNormal) // <- I fail to unify types -- left: Normal | right: Normal Gen
  let variable = passGen(paramGen)

  return variable
}

Link to playground

Diamond imports don't seem to work correctly?

Description: Splitting a file into multiple files that import each other ends up breaking rule-s despite the project executing. It looks as if the different files are referring to the same types but to different node definitions, and hence the net never matches anything in order to start executing -- but it might be something else instead.

To reproduce: In the linked Gist, uncomment all the import lines and comment all the require lines. Despite everything parsing and executing, for some odd reason, rule-s won't match. I have not yet tried to debug the issue further, but I am going to guess that either the fact that I have "diamond" dependencies between files (A depends on B and C that both depend on D) manages to break the import system.

Issue found while developing https://gist.github.com/bojidar-bg/6c52d1f1ed3fb3a583aaff9a184687fe

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.