• Stars
    star
    115
  • Rank 297,079 (Top 6 %)
  • Language
    JavaScript
  • Created almost 9 years ago
  • Updated almost 7 years ago

Reviews

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

Repository Details

An optimal function evaluator written in JavaScript.

Optlam.js

An optimal function evaluator written in JavaScript.

Note: this is deprecated, see a much cleaner version here.

About

Optlam.js is a simple, optimal (in Levy's sense) 位-calculus evaluator using interaction nets. It is, currently, as far as I know, the fastest implementation of functions in the world. It uses Lamping's Abstract Algorithm - that is, the so called (and problematic) "oracle" is avoided altogether. As such, it is only capable of computing 位-terms that are typeable on Elementary Affine Logic. This includes most functions that you'd use in practice, but isn't powerful enough to process, for example, an unhalting turing machine. Notice being optimal doesn't mean it is efficient - it is implemented in JavaScript, after all. Nether less, it is still asymptotically faster than most evaluators, being able to quickly normalize functions that even Haskell would take years. Improved implementations would be great, and there is a lot of potential to explore parallel (GPU/ASIC?) processing. The API is very simple, consisting of one function, reduce, which receives a bruijn-indexed, JSON-encoded 位 calculus term and returns its normal form. See this image for an overall idea of how the magic works.

Example

What is the result of:

(function (a){ return function(b){ return a; } })(1)(2);

Using node.js, you can find it is 1. Now, what is the result of:

expMod = (function (v0) { return (function (v1) { return (function (v2) { return (((((((function(n){return(function(f){return(function(a){ for (var i=0;i<n;++i)a=f(a);return(a)})})})(v2))((function (v3) { return (function (v4) { return (v3((function (v5) { return ((v4((function (v6) { return (function (v7) { return (v6(((v5(v6))(v7)))) }) })))(v5)) }))) }) })))((function (v3) { return (v3((function (v4) { return (function (v5) { return v5 }) }))) })))((function (v3) { return (((((function(n){return(function(f){return(function(a){ for (var i=0;i<n;++i)a=f(a);return(a)})})})(v1))(((function(n){return(function(f){return(function(a){ for (var i=0;i<n;++i)a=f(a);return(a)})})})(v0))))((((((function(n){return(function(f){return(function(a){ for (var i=0;i<n;++i)a=f(a);return(a)})})})(v2))((function (v4) { return (function (v5) { return (function (v6) { return (v4((function (v7) { return ((v5(v7))(v6)) }))) }) }) })))((function (v4) { return v4 })))((function (v4) { return (function (v5) { return (v5(v4)) }) })))))((((((function(n){return(function(f){return(function(a){ for (var i=0;i<n;++i)a=f(a);return(a)})})})(v2))((function (v4) { return (function (v5) { return v4 }) })))((function (v4) { return v4 })))((function (v4) { return v4 }))))) })))((function (v3) { return (v3+1) })))(0)) }) }) })

console.log(expMod(10)(10)(2));

That JavaScript program uses church-encoded natural numbers to compute 10^10%2 - that is, the exponential modulus. The result should be 0, but node.js takes too long to compute it because it doesn't implement functions optimally. Thus, if you really need that answer, you can encode your function on the lambda-calculus and use optlam.js to find it for you:

-- expMod on the lambda calculus
expMod = (位abc.(c(位de.(d(位f.(e(位gh.(g(fgh)))f))))(位d.(d(位ef.f)))(位d.(ba(c(位efg.(e(位h.(fhg))))(位e.e)(位ef.(fe)))(c(位ef.e)(位e.e)(位e.e))))))

-- The computation we want
main = expMod 10 10 2

This correctly outputs 0.

How do I use it?

The API right now is actually non-existent, but check the test.js file for the expMod example. You can run it with node.js:

node test.js

Outputs:

25
{ iterations: 2579187,
applications: 1289514,
used_memory: 5938470 }

Which is 100 ^ 100 % 31. In a few days I might update this with a proper parser/pretty-printer and command line tool.

Why don't other languages implement functions optimally?

As important as functions are for programming in general, no common language implements them optimally. A wide range of algorithms is used, but all are asymptotically suboptimal. Not even the so-called "functional", pure, lazy languages (i.e., Haskell) do it. The reason is most real-world programming rarely needs it. The difference can only be noticed in functions much more complex than what you'd write normally, and we already have very efficient algorithms for those simpler functions.

How does it work?

It uses Lamping's "Abstract Algorithm", as explained on the The Optimal Implementation of Functional Programming Languages book, by Andrea Asperti and Stefano Guerrini. It does not implement the so-called (and problematic) "Oracle" - that is, no croissants nor brackets are used - so it actually only works on a subset of 位-terms that are elementary-affine-logic typeable. In practice, I couldn't find any interesting 位-term that wasn't EAL-typeable, so I chose to avoid the oracle altogether. Also, instead of applying rules nondeterministically in parallel, a cursor runs through the graph sequentially, avoiding unreachable branches. I'm not sure this was proposed on literature.

What is this actually useful for?

Not much, right now. It is optimal, but not terribly efficient (it is written in JavaScript, after all). I don't know if there is something practical Optlam, as is, could do that couldn't be done faster with alternative known algorithms. But it is something new that enables some things that weren't possible before, and has a lot of potential that deserves be explored. For example, the algorithm can be effortlessly distributed through hundreds of processing cores, but JavaScript can't even spawn threads.

More Repositories

1

WebMonkeys

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

Interaction-Calculus

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

LJSON

JSON extended with pure functions.
JavaScript
500
star
4

Caramel

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

PureState

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

forall

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

abstract-algorithm

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

Cedille-Core

A minimal proof language.
JavaScript
193
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