• Stars
    star
    597
  • Rank 72,609 (Top 2 %)
  • Language
    Rust
  • License
    MIT License
  • Created almost 6 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A programming language and model of computation that matches the optimal 位-calculus reduction algorithm perfectly.

Interaction Calculus

The Interaction Calculus (IC) is a minimal programming language and model of computation obtained by "completing" the affine Lambda Calculus in a way that matches perfectly Lamping's optimal reduction algorithm. It can also be seen as a textual syntax for Symetric Interaction Combinators: both views are equivalent. As a model of computation, the IC has compeling characteristics:

  1. It features higher-order functions, just like the Lambda Calculus.

  2. It has a well-defined cost model, just like the Turing Machine.

  3. It is inherently concurrent, making it prone to massive parallelism.

  4. It is fully linear, making it garbage-collection free.

This repository contains a Rust reference implementation. Also check the Kind formalization here.

Usage

1. Install

git clone https://github.com/victortaelin/interaction_calculus
cd interaction_calculus
cargo install --path .

2. Create a 'main.ic' file

def id = 位x x
def c2 = 位f 位x (dup #b f0 f1 = f; (f0 (f1 x)))
(c2 id)

3. Run it

ic main.ic

See example.ic for a larger example.

Language

Interaction Calculus terms are defined by the following grammar:

term ::=
  | 位x term                   -- abstraction
  | (term term)               -- application
  | {term term}#N             -- superposition
  | dup #N {p q} = term; term -- duplication
  | x                         -- variable

Where variable have global scope (can occur outside binding lambdas).

The IC has 4 primitive reduction rules:

((位x f) a)
---------- lambda application
x <- a
f

({u v}#i a)
---------------- superposition application
dup #i x0 x1 = a
{(u x0) (v x1)}#i

dup #i p q = 位x f
body
----------------- lambda duplication
p <- 位x0 r
q <- 位x1 s
x <- {x0 x1}#i
dup #i r s = f
body

dup #i p q = {r s}#j
body
-------------------- superposition duplication
if #i == #j:
  a <- fst
  b <- snd
  cont
else:
  a <- {a0 a1}#j
  b <- {b0 b1}#j
  dup #i a0 a1 = fst;
  dup #i b0 b1 = snd;
  cont

Where, a <- b stands for a global, linear substitution of a by b. It can be performed in O(1) by a simple array write, which, in turn, makes all rewrite rules above O(1) too.

And that's all!

Why?

Consider the conventional Lambda Calculus, with pairs. It has two computational rules:

  • Lambda Application : (位x body) arg

  • Pair Projection : let {a b} = {fst snd} in cont

When compiling the Lambda Calculus to Interaction Combinators:

  • lams and apps can be represented as constructor nodes (纬)

  • pars and lets can be represented as duplicator nodes (未)

As such, lambda applications and pair projections are just annihilations:

      Lambda Application                 Pair Projection
                                                                   
      (位x body) arg                      let {a b} = {fst snd} in cont 
      ----------------                   -----------------------------
      x <- arg                           a <- fst                  
      body                               b <- snd                  
                                         cont                      
                                                                   
    ret  arg    ret  arg                  a   b       a    b       
     |   |       |    |                   |   |       |    |       
     |___|       |    |                   |___|       |    |       
 app  \ /         \  /                let  \#/         \  /        
       |    ==>    \/                       |    ==>    \/         
       |           /\                       |           /\         
 lam  /_\         /  \               pair  /#\         /  \        
     |   |       |    |                   |   |       |    |       
     |   |       |    |                   |   |       |    |       
     x  body     x   body                fst snd    fst   snd      
                                                                   
 "The application of a lambda        "The projection of a pair just 
 substitutes the lambda's var        substitutes the projected vars
 by the application's arg, and       by each element of the pair, and
 returns the lambda body."           returns the continuation."

But annihilations only happen when identical nodes interact. On interaction nets, it is possible for different nodes to interact, which triggers another rule, the commutation. That rule could be seen as handling the following expressions:

  • Lambda Projection : let {a b} = (位x body) in cont

  • Pair Application : ({fst snd} arg)

But how could we "project" a lambda or "apply" a pair? On the Lambda Calculus, these cases are undefined and stuck, and should be type errors. Yet, by interpreting the effects of the commutation rule on the interaction combinator point of view, we can propose a reasonable reduction for these lambda expressions:

   Lambda Application                         Pair Application
                                                                  
   let {a b} = (位x body) in cont             ({fst snd} arg)   
   ------------------------------             ---------------
   a <- (位x0 b0)                             let {x0 x1} = arg in
   b <- (位x1 b1)                             {(fst x0) (snd x1)}
   x <- {x0 x1}
   let {b0 b1} = body in
   cont                   
       
    ret  arg         ret  arg            ret  arg         ret  arg  
     |   |            |    |              |   |            |    |   
     |___|            |    |              |___|            |    |   
 let  \#/            /_\  /_\         app  \ /            /#\  /#\  
       |      ==>    |  \/  |               |      ==>    |  \/  |  
       |             |_ /\ _|               |             |_ /\ _|  
 lam  /_\            \#/  \#/        pair  /#\            \ /  \ /  
     |   |            |    |              |   |            |    |   
     |   |            |    |              |   |            |    |   
     x  body          x   body           var body         var  body 

 "The projection of a lambda         "The application of a pair is a pair
 substitutes the projected vars      of the first element and the second
 by a copies of the lambda that      element applied to projections of the
 return its projected body, with     application argument."
 the bound variable substituted
 by the new lambda vars paired."

This, in a way, completes the lambda calculus; i.e., previously "stuck" expressions now have a meaningful computation. That system, as written, is Turing complete, yet, it is very limited, since it isn't capable of cloning pairs, or cloning cloned lambdas. There is a simple way to greatly increase its expressivity, though: by decorating lets with labels, and upgrading the pair projection rule to:

let #i{a,b} = #j{fst,snd} in cont
---------------------------------
if #i == #j:
  a <- fst
  b <- snd
  cont
else:
  a <- #j{a0,a1}
  b <- #j{b0,b1} 
  let #i{a0,a1} = fst in
  let #i{b0,b1} = snd in
  cont

That is, it may correspond to either an Interaction Combinator annihilation or commutation, depending on the value of the labels #i and #j. This makes IC capable of cloning pairs, cloning cloned lambdas, computing nested loops, performing Church-encoded arithmetic up to exponentiation, expressing arbitrary recursive functions such as the Y-combinators and so on. In other words, with this simple extension, IC becomes extraordinarily powerful and expressive, giving us:

  1. A new model of computation that is similar to the lambda calculus, yet, can be reduced optimally.

  2. A general purpose, higher-order "core language" that is lighter and faster than the lambda calculus.

  3. A term-based view for interaction combinators, making it easier to reason about their graphs.

That said, keep in mind the IC is not equivalent to the Lambda Calculus. It is a different model. There are 位-terms that IC can't compute, and vice-versa. For example, the Lambda Calculus can perform self-exponentiation of church-nats as 位x (x x), which isn't possible on IC. Yet, on IC, we can have call/cc, direct O(1) queues, and fully linear HOAS, which aren't possible on the Lambda Calculus.

Finally, note that, in order to differentiate IC's "pairs" and "lets" from their 位-Calculus counterparts, which behave differently, we call them "sups" and "dups", respectivelly.

Examples

Lambda-application and superposition-projection (same as pair-projection).

位u 位v dup {a b} = {(位x x) (位y y)}; {(a u) (b v)}
------------------------------------------------ superposition-projection
位u 位v {((位x x) u) ((位y y) v)}
----------------------------- lambda-application
位u 位v {((位x x) u) v}
-------------------- lambda-application
位u 位v {u v}

Using lambda-projection to copy a function.

dup {a b} = 位x 位y 位z y; {a b}
----------------------------- lambda-projection
dup {a b} = 位y 位z y; {(位x0 a) (位x1 b)}
-------------------------------------- lambda-projection
dup {a b} = 位z {y0 y1}; {(位x0 位y0 a) (位x1 位y1 b)}
------------------------------------------------- lambda-projection
dup {a b} = {y0 y1}; {(位x0 位y0 位z0 a) (位x1 位y1 位z1 b)}
------------------------------------------------------ superposition-projection
{(位x0 位y0 位z0 y0) (位x1 位y1 位z1 y1)}

Demonstrating superposition-application (not part of Lambda Calculus)

{{(位x x) (位y y)} (位t t)}
------------------------ superposition-application
dup {a0 a1} = 位t t; {((位x x) a0) ((位y y) a1)}
--------------------------------------------- lambda-projection
dup {a0 a1} = {t0 t1}; {((位x x) (位t0 a0)) ((位y y) (位t1 a1))}
------------------------------------------------------------ superposition-projection
{((位x x) (位t0 t0)) ((位y y) (位t1 t1))}
------------------------------------- lambda-application
{((位x x) (位t0 t0)) (位t1 t1)}
---------------------------- lambda-application
{(位t0 t0) (位t1 t1)}

Example 3: 2 + 3.

This is equivalent to:

data Nat = S Nat | Z

add : Nat -> Nat -> Nat
add (S n) m = S (add n m)
add Z     m = m

main : Nat
main = add (S (S (S Z))) (S (S Z))

Full reduction.

Example 4: applying not 8 times to True.

Full reduction.

Here is a handwritten reduction of 2^(2^2).

High-order Virtual Machine (HVM)

The High-order Virtual Machine (HVM) is a high-performance practical implementation of the IC. Check it out!

More Repositories

1

WebMonkeys

Massively parallel GPU programming on JavaScript, simple and clean.
JavaScript
1,163
star
2

LJSON

JSON extended with pure functions.
JavaScript
500
star
3

Caramel

A modern syntax for the 位-calculus.
Haskell
405
star
4

PureState

The stupidest state management library that works.
JavaScript
309
star
5

forall

Expressive static types and invariant checks for JavaScript.
JavaScript
227
star
6

abstract-algorithm

Optimal evaluator of 位-calculus terms.
JavaScript
222
star
7

Cedille-Core

A minimal proof language.
JavaScript
193
star
8

optlam

An optimal function evaluator written in JavaScript.
JavaScript
115
star
9

calculus-of-constructions

Minimal, fast, robust implementation of the Calculus of Constructions on JavaScript.
JavaScript
94
star
10

Interaction-Type-Theory

Rust
88
star
11

Bitspeak

JavaScript
80
star
12

articles

Thoughts and stuff
JavaScript
65
star
13

ChatSH

Chat with GPT from the terminal, with the ability to execute shell scripts.
JavaScript
63
star
14

ESCoC

A nano "theorem prover".
JavaScript
61
star
15

absal-ex

Absal ex
53
star
16

eth-lib

Lightweight Ethereum libraries
JavaScript
52
star
17

UrnaCripto

Referendos criptograficamente incorrupt铆veis.
JavaScript
51
star
18

lrs

Linkable Ring Signatures on JavaScript and PureScript.
PureScript
46
star
19

swarm-js

JavaScript
45
star
20

lambda-calculus

A simple, clean and fast implementation of the 位-calculus on JavaScript.
JavaScript
44
star
21

heart

heart
JavaScript
41
star
22

AIEMU

Simple AI-based (Claude-3) game emulator
JavaScript
39
star
23

nano-ethereum-signer

Very small Ethereum signer and verifier
JavaScript
37
star
24

absal-rs

Rust
36
star
25

Moonad-web-legacy

A Peer-to-Peer Operating System
JavaScript
35
star
26

Formality

JavaScript
35
star
27

ultimate-calculus

TypeScript
35
star
28

HOC

C
32
star
29

interaction-calculus-of-constructions

A minimal proof checker.
TypeScript
31
star
30

nano-json-stream-parser

A complete, pure JavaScript, streamed JSON parser in less than 1kb.
JavaScript
29
star
31

servify

Microservices in the simplest way conceivable.
JavaScript
28
star
32

ab_challenge_eval

Evaluator for the A::B Prompting Challenge
JavaScript
24
star
33

kind-ai

23
star
34

optimul

Multiplication on optimal 位-calculus reducers
JavaScript
22
star
35

parallel_lambda_computer_tests

learning cuda
Cuda
18
star
36

Navim

Navigates files on the terminal with the minimal amount of keystrokes.
JavaScript
18
star
37

Elementary-Affine-Core-legacy

A simple, untyped, terminating functional language that is fully compatible with optimal reductions.
JavaScript
17
star
38

formality-agda-lib-legacy

Agda libraries relevant to Moonad
Agda
15
star
39

nano-ipfs-store

Lightweight library to store and get data to/from IPFS
JavaScript
14
star
40

Elementary-Affine-Type-Theory-legacy

Minimal, efficient proof language
JavaScript
14
star
41

Taelang

my personal lang
TypeScript
13
star
42

unknown_halting_status

Small programs with unknown halting status.
JavaScript
12
star
43

lsign

Quantum-proof, 768-bit signatures for 1-bit messages
JavaScript
11
star
44

AI-scripts

Some handy AI scripts
JavaScript
10
star
45

diagonalize

Searches through infinite branches
JavaScript
9
star
46

escoc-libs-legacy

Base Formality libraries
JavaScript
9
star
47

MiniPaper

shorten long papers with GPT-4
JavaScript
9
star
48

Elementary-Affine-Net-legacy

JavaScript
8
star
49

Formality-Net-legacy

C
7
star
50

run_solidity

Runs a Solidity file. That's all.
JavaScript
6
star
51

OpenLegends

An open-source MOBA in Rust
6
star
52

formality-document

Rust
6
star
53

coc-with-math-prims

JavaScript
5
star
54

Nasic-legacy

N-Ary Symmetric Interaction Combinators
JavaScript
5
star
55

idris-mergesort-benchmark

Benchmark of the new Idris JS backend
JavaScript
5
star
56

taemoba

4
star
57

LPU

JavaScript
4
star
58

Formality-Web-legacy

JavaScript
4
star
59

uwuchat

chat based messaging and rollback state computer
JavaScript
4
star
60

ethereum-offline-signer

Signs an Ethereum transaction from the command line.
JavaScript
4
star
61

LambdaIO-Formality-Talk

JavaScript
3
star
62

Vote

3
star
63

Formality-to-Nasic-legacy

Compiles a Formality term to a Nasic graph
JavaScript
3
star
64

Treeduce

C
3
star
65

clifun

3D clifford algebras visualization
JavaScript
3
star
66

Formality-IO-legacy

JavaScript
3
star
67

nano-persistent-memoizer

Caches a function permanently on browser and node.
JavaScript
3
star
68

nano-sha256

Use native Sha256 on both browser and Node.js
JavaScript
3
star
69

ReflexScreenWidget

A widget for Haskell-Reflex that renders a dynamic image to a Canvas in realtime.
Haskell
3
star
70

OSX

Vim script
3
star
71

EthFP

3
star
72

freeform-wpm-test

A simple WPM test that allows you to type anything you want.
HTML
2
star
73

ethereum-rpc

JavaScript
2
star
74

ethereum-publisher-dapp

JavaScript
2
star
75

symmetric-interaction-calculus-benchmarks

SIC benchmarks
JavaScript
2
star
76

NeoTaelin

2
star
77

shared-state-machine

JavaScript
2
star
78

Trabalho-IC-UFRJ-2

Python
2
star
79

eth-web-tools

Some web Ethereum tools that MyEtherWallet currently lacks
JavaScript
2
star
80

VictorTaelin

2
star
81

MiniPapers

Papers and books minified to fit given LLMs context lengths.
2
star
82

gpt

2
star
83

hvm2

Cuda
2
star
84

Formality-sugars-legacy

Default syntax sugars for Formality
JavaScript
2
star
85

talks

C
2
star
86

PongFromScratch

A simple tutorial on how to create a ping-pong game from scratch
1
star
87

kind-react-component

Renders a Kind app as a React component
1
star
88

fineorder

1
star
89

PotS

JavaScript
1
star
90

piramidex

JavaScript
1
star
91

LamBolt

The ultimate compile target for functional languages
1
star
92

sketch_bros

Super Sketch Bros!
JavaScript
1
star
93

StupidMinimalistHash

C
1
star
94

MaiaVictor.github.io

HTML
1
star
95

haelin

HTML
1
star
96

cuda_rewrite_tests

learning cuda
Cuda
1
star
97

luna-lang-old-abandoned-repo

Typed version of Moon-lang
JavaScript
1
star
98

Kind2

Kind refactor based on HVM
1
star
99

inferno-hello-world

Inferno Hello World with Hyperscript
JavaScript
1
star
100

posts

1
star