Skip to content

Instantly share code, notes, and snippets.

@iamvery
Last active August 1, 2018 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamvery/b48be33dca30e946798805c76f754911 to your computer and use it in GitHub Desktop.
Save iamvery/b48be33dca30e946798805c76f754911 to your computer and use it in GitHub Desktop.
A little Jupyter notebook illustrating some basic lambda calculus with JavaScript :)
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# f(x)\n",
"\n",
"or\n",
"\n",
"# lambda calculus in 3 minutes, lol"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Lambda Calculus\n",
"\n",
"The idea is that we can describe all computation with functions of one argument."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With these fundamental building blocks, how can higher concepts be encoded?"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Booleans\n",
"\n",
"What are they? A binary choice!\n",
"\n",
"How might that be expressed as a function? Make a choice between two inputs."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"scrolled": true,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[Function: falz]"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"troo = x => y => x\n",
"falz = x => y => y"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'wat'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"// use the \"booleans\" to make a choice\n",
"troo('lol')('wat')\n",
"falz('lol')('wat')"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Boolean logic?"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[Function: falz]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"not = b => b(falz)(troo)\n",
"not(troo)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[Function: falz]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"and = b1 => b2 => b1(b2)(b1)\n",
"and(troo)(falz)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[Function: troo]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"or = b1 => b2 => b1(b1)(b2)\n",
"or(troo)(falz)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'lol'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iff = c => thn => els => c(thn)(els)\n",
"iff(troo)('lol')('wat')"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Numbers (well, natural numbers)\n",
"\n",
"What are they? Good for counting!\n",
"\n",
"How might that be expressed as a function? Counting function applications."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[Function: two]"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"zero = f => x => x\n",
"one = f => x => f(zero(f)(x)) // f(x)\n",
"two = f => x => f(one(f)(x)) // f(f(x))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"to_int = n => n(i => i+1)(0)\n",
"to_int(two)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"Each successor _can_ be implementated in terms of its predecessor."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"succ = n => f => x => f(n(f)(x))\n",
"three = succ(two)\n",
"four = succ(three)\n",
"to_int(four)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Arithmetic?"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"add = m => n => f => x => n(f)(m(f)(x))\n",
"to_int(add(two)(three))"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mul = m => n => f => x => n(m(f))(x) // simplifies to: m => n => f => n(m(f))\n",
"to_int(mul(two)(three))"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"8"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"exp = b => e => f => x => e(b)(f)(x) // simplifies to: b => e => e(b)\n",
"to_int(exp(two)(three))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"h/t Anjana Vakil! 🙌\n",
"- https://www.youtube.com/watch?v=7BsfMMYvGaU&t=2s\n",
"- https://gist.github.com/vakila/3d5cebaebf01c4c77b289b9a0388e3c8\n",
"\n",
"It me! ❤️\n",
"- twitter/iamvery\n",
"- slack/jay"
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"kernelspec": {
"display_name": "Javascript (Node.js)",
"language": "javascript",
"name": "javascript"
},
"language_info": {
"file_extension": ".js",
"mimetype": "application/javascript",
"name": "javascript",
"version": "9.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment