An implementation of (Untyped) Lambda Calculus in JavaScript.

Overview

Lambda Calculus

More restraint and more pure,
so functional and so reduced.

-- Anonymous Bauhaus Student

An implementation of (Untyped) Lambda Calculus in JavaScript.

  • Use S-expression as overall syntax.
  • Implement call-by-need lazy evaluation.
  • Allow recursive definitions.
  • A simple module system with only one API -- (import).
    • It can import module from local file or remote URL.
  • Two simple testing statements (assert-equal) and (assert-not-equal).
    • They can handle beta and eta equivalence.

Usage

Online playground

Visit the Lambda Playground.

Command line tool

Install it by the following command:

npm -g i @cicada-lang/lambda

The command line program is called lambda.

Run a module by file:

lambda docs/tests/nat-church.md

Run a module by URL:

lambda https://readonly.link/files/cicada-lang/lambda/-/docs/tests/nat-church.md

Examples

Please see docs/tests for more examples.

Boolean

[ PLAYGROUND ]

(define (true t f) t)
(define (false t f) f)

(define (if p t f) (p t f))

(define (and x y) (if x y false))
(define (or x y) (if x true y))
(define (not x) (if x false true))

(and true false)
(not (not (or true false)))

Natural Number by Church encoding

[ PLAYGROUND | WIKIPEDIA ]

(define zero (lambda (base step) base))
(define (add1 n) (lambda (base step) (step (n base step))))
(define (iter-Nat n base step) (n base step))

(define one (add1 zero))
(define two (add1 one))
(define three (add1 two))

(define (add m n) (iter-Nat m n add1))

(add two two)

Factorial

[ PLAYGROUND ]

(import "https://readonly.link/files/cicada-lang/lambda/-/docs/tests/nat-church.md"
  zero? add mul sub1
  zero one two three four)

(import "https://readonly.link/files/cicada-lang/lambda/-/docs/tests/boolean.md"
  true false if)

(define (factorial n)
  (if (zero? n)
    one
    (mul n (factorial (sub1 n)))))

(factorial zero)
(factorial one)
(factorial two)
(factorial three)

Factorial by fixpoint combinator

[ PLAYGROUND | WIKIPEDIA ]

(import "https://readonly.link/files/cicada-lang/lambda/-/docs/tests/nat-church.md"
  zero? add mul sub1
  zero one two three four)

(import "https://readonly.link/files/cicada-lang/lambda/-/docs/tests/boolean.md"
  true false if)

;; NOTE `x` is `f`'s fixpoint if `(f x) = x`
;;   In lambda calculus, we have function `fix`
;;   which can find fixpoint of any function.
;;      (f (fix f)) = (fix f)
;;   The following `fix` is one way of defining `fix`.

(define (fix f)
  ((lambda (x) (f (x x)))
   (lambda (x) (f (x x)))))

;; (claim factorial-wrap (-> (-> Nat Nat) (-> Nat Nat)))
;; (claim (fix factorial-wrap) (-> Nat Nat))
;; (claim fix (forall (A) (-> (-> A A) A)))

(define (factorial-wrap factorial)
  (lambda (n)
    (if (zero? n)
      one
      (mul n (factorial (sub1 n))))))

(define factorial (fix factorial-wrap))

(factorial zero)
(factorial one)
(factorial two)
(factorial three)
(factorial four)

Cons the Magnificent

[ PLAYGROUND ]

;; NOTE Temporarily save `car` and `cdr` to a lambda,
;;   apply this lambda to a function -- `f`,
;;   will apply `f` to the saved `car` and `cdr`
(define (cons car cdr) (lambda (f) (f car cdr)))
(define (car pair) (pair (lambda (car cdr) car)))
(define (cdr pair) (pair (lambda (car cdr) cdr)))

(import "https://readonly.link/files/cicada-lang/lambda/-/docs/tests/boolean.md"
  true false)

(define (null f) true)
(define (null? pair) (pair (lambda (car cdr) false)))

Development

npm install    // Install dependencies
npm run build  // Compile `src/` to `lib/`
npm run watch  // Watch the compilation
npm run test   // Run test

Contributions

Be polite, do not bring negative emotion to others.

License

You might also like...

Alexa Skill & Google Action code that works on AWS Lambda

Alexa Skill & Google Action code that works on AWS Lambda

Jovo v4 Sample: Alexa Skill & Google Action on AWS Lambda Website - Docs - Marketplace - Template This Jovo v4 sample app showcases the following feat

Nov 22, 2022

A Lambda-Powered Social Media Tracker

A Lambda-Powered Social Media Tracker

A Lambda-Powered Social Media Dashboard Angular: the frontend application (= ./app) Terraform: to create our infrastructure (= ./infra) Precondition

May 14, 2022

A monorepo that uses the AWS Cloud Development Kit to deploy and configure nanomdm on AWS lambda.

NanoMDM on AWS This repo builds and configures a nanomdm server to run on AWS lambda. It uses the Cloud Development Kit and tries to follow best pract

May 26, 2022

A quickstart AWS Lambda function code generator. Downloads a template function code file, test harness file, sample SAM deffiniation and appropriate file structure.

Welcome to function-stencil 👋 A quickstart AWS Lambda function code generator. Downloads a template function code file, test harness file, sample SAM

Jun 20, 2022

A simple example repo that demonstrates the dynamic ephemeral storage solution for AWS Lambda outlined in the corresponding Storyboard Dev Blog post.

AWS Lambda Dynamic Ephemeral Storage Example A simple example repo that demonstrates the dynamic ephemeral storage solution for AWS Lambda outlined in

Jun 14, 2022

Sample code for resizing Images with Lambda@Edge using the Custom Origin. You can deploy using AWS CDK.

Sample code for resizing Images with Lambda@Edge using the Custom Origin. You can deploy using AWS CDK.

Resizing Images with Lambda@Edge using the Custom Origin You can resize the images and convert the image format by query parameters. This Lambda@Edge

Dec 11, 2022

The JSON logger you always wanted for Lambda.

MikroLog The JSON logger you always wanted for Lambda. MikroLog is like serverless: There is still a logger ("server"), but you get to think a lot les

Nov 15, 2022

Wrapper for NextJS image handling, optimised for Lambda w/ ApiGw integration.

NextJS Lambda Utils This is a project allowing to deploy Next applications (standalone options turned on) to AWS Lambda without hassle. This is an alt

Dec 28, 2022

🚀 Using top-level await in AWS Lambda with TypeScript, esbuild and Serverless Framework

🚀 Using top-level await in AWS Lambda with TypeScript, esbuild and Serverless Framework

🚀 Top-level await in AWS Lamba with TypeScript Articles https://dev.to/oieduardorabelo/top-level-await-in-aws-lamba-with-typescript-1bf0 https://medi

Nov 23, 2022
Comments
  • [idea] graph-based implementation of lambda calculus

    [idea] graph-based implementation of lambda calculus

    Normal forms of Parigot numerals are exponential in size, but a reasonable term-graph implementation should be able to keep them linear via sharing.

    Implement lamping reduction by inet.

    After this, we can use lambda encoding for real.

    After using lambda encoding for real, maybe we can get better understanding of inductive datatype

    For example, parameters v.s. indexes.

    opened by xieyuheng 0
Owner
Cicada Language
Let's build a bridge between writing software and doing mathematics.
Cicada Language
MerLoc is a live AWS Lambda function development and debugging tool. MerLoc allows you to run AWS Lambda functions on your local while they are still part of a flow in the AWS cloud remote.

MerLoc MerLoc is a live AWS Lambda function development and debugging tool. MerLoc allows you to run AWS Lambda functions on your local while they are

Thundra 165 Dec 21, 2022
AWS Lambda & Serverless - Developer Guide with Hands-on Labs. Develop thousands line of aws lambda functions interact to aws serverless services with real-world hands-on labs

AWS Lambda & Serverless - Developer Guide with Hands-on Labs UDEMY COURSE WITH DISCOUNTED - Step by Step Development of this Repository -> https://www

awsrun 35 Dec 17, 2022
AWS Lambda and API Gateway, simplified for Javascript

alanajs AWS Lambda and API Gateway, simplified for JavaScript About alanajs Make setting up Lambda microservices easier than ever. alanajs is a free,

OSLabs Beta 64 Aug 1, 2022
Implementing hexagonal architecture on AWS Lambda with Node.js

Developing evolutionary architecture with AWS Lambda Context Agility enables you to evolve a workload quickly, adding new features, or introducing new

AWS Samples 95 Dec 20, 2022
🔐 Lambda Authorizer ready for integration with Serverless Framework and Auth0.

Getting started 1. Clone the repository (or generate a serverless project) sls create --name auth-service --template-url https://github.com/GustavoNor

Gustavo Noronha 2 Feb 10, 2022
A serverless AWS expense tracker API. AWS Lambda functions, API gateway, and Dynamodb are among the ingredients.

AWS-Serverless-API A serverless AWS expense tracker API. AWS Lambda functions API gateway Dynamodb Endpoints Create a new expense: Method: POST Body f

Ondiek Elijah Ochieng 1 Jul 16, 2022
Everynode allows you to run any version of Node.js in AWS Lambda, in any commercial AWS region

Run Any Node.js Version in AWS Lambda Everynode allows you to run any version of Node.js in AWS Lambda, in any commercial AWS region. We add support f

Fusebit 116 Dec 15, 2022
awsrun 189 Jan 3, 2023
It shows an effective way to correct bus arrival information using data analytics based on Amazon Serverless such as Kiness Data Stream, Kinesis Data Firehose, S3, and Lambda.

Amazon Serverless를 이용한 실시간 버스 정보 수집 및 저장 본 github repository는 버스 정보를 주기적으로 수집하여 분석할 수 있도록, Amazon Serverless인 Amazon Kinesis Data Stream, Kinesis Data

John Park 4 Nov 13, 2022