Skip to content

Instantly share code, notes, and snippets.

@Hirrolot
Hirrolot / CoC.ml
Last active April 5, 2024 15:34
How to implement dependent types in 80 lines of code
type term =
| Lam of (term -> term)
| Pi of term * (term -> term)
| Appl of term * term
| Ann of term * term
| FreeVar of int
| Star
| Box
let unfurl lvl f = f (FreeVar lvl)
@Hirrolot
Hirrolot / CoC.ml
Last active March 5, 2024 09:47
Barebones lambda cube in OCaml
(* The syntax of our calculus. Notice that types are represented in the same way
as terms, which is the essence of CoC. *)
type term =
| Var of string
| Appl of term * term
| Binder of binder * string * term * term
| Star
| Box
and binder = Lam | Pi
@xarvh
xarvh / elm-format-hack
Created June 1, 2021 12:15
A wrapper for elm-format to nicely indent chained `andThen`s (ie, pseudo do-notation)
#!/usr/bin/env node
// A work around to https://github.com/avh4/elm-format/issues/352
// * Use it in place of elm-format (only some flags are supported)
// * Run it from a directory containing node_modules/elm-format/bin/elm-format (but you can change it below)
function extractFirstRegexMatch(regex, elmCode) {
@pesterev
pesterev / camel-case-keys.ts
Last active October 7, 2021 14:36
Convert Object Type keys from snake_case to camelCase on Type-Level
type CamelCaseKeys<T extends unknown> = {
[K in keyof T as CamelCaseKey<K>]: T[K]
}
type CamelCaseKey<T> = T extends `${infer T0}_${infer T1}` ? `${T0}${CamelCaseKeyN<T1>}` : T
type CamelCaseKeyN<T> = T extends `${infer T0}_${infer T1}` ? `${Sentence<T0>}${CamelCaseKeyN<T1>}` : Sentence<T>
type Sentence<T> = T extends `${infer T0}${infer T1}` ? `${Uppercase<T0>}${T1}` : T
@pesterev
pesterev / unique-tuple.ts
Created December 3, 2020 16:03
Type-level Unique for tuples
type UniqueParser0<T, R extends unknown[], M> = Readonly<T> extends readonly [] ? T : UniqueParser1<T, R, M>
type UniqueParser1<T, R extends unknown[], M> = Readonly<T> extends readonly [infer _] ? T : UniqueParserN<T, R, M>
type UniqueParserN<T, R extends unknown[], M> = Readonly<T> extends readonly [infer A, ...infer B] ? A extends M ? UniqueParserTerm<B, R, M> : UniqueParserTerm<B, [...R, A], M | A> : never
type UniqueParserTerm<T, R extends unknown[], M> = Readonly<T> extends readonly [] ? R : UniqueParserN<T, R, M>
export type UniqueTuple<T> = UniqueParser0<T, [], never>
@AndrasKovacs
AndrasKovacs / FixNf.hs
Last active June 17, 2023 10:48
Minimal environment machine normalization with a fixpoint operator
{-# language Strict, BangPatterns #-}
data Tm = Var Int | App Tm Tm | Lam Tm | Fix Tm deriving (Show, Eq)
data Val = VVar Int | VApp Val Val | VLam [Val] Tm | VFix [Val] Tm
isCanonical :: Val -> Bool
isCanonical VLam{} = True
isCanonical _ = False
eval :: [Val] -> Tm -> Val
@AndrasKovacs
AndrasKovacs / TypeLambdas.hs
Last active September 23, 2019 14:48
Type lambdas and induction with GHC 8.4.2 and singletons
{-# language TemplateHaskell, ScopedTypeVariables, RankNTypes,
TypeFamilies, UndecidableInstances, DeriveFunctor, GADTs,
TypeOperators, TypeApplications, AllowAmbiguousTypes,
TypeInType, StandaloneDeriving #-}
import Data.Singletons.TH -- singletons 2.4.1
import Data.Kind
-- some standard stuff for later examples' sake
@AndyShiue
AndyShiue / CuTT.md
Last active April 28, 2024 23:35
Cubical type theory for dummies

I think I’ve figured out most parts of the cubical type theory papers; I’m going to take a shot to explain it informally in the format of Q&As. I prefer using syntax or terminologies that fit better rather than the more standard ones.

Q: What is cubical type theory?

A: It’s a type theory giving homotopy type theory its computational meaning.

Q: What is homotopy type theory then?

A: It’s traditional type theory (which refers to Martin-Löf type theory in this Q&A) augmented with higher inductive types and the univalence axiom.

@codedot
codedot / Makefile
Last active May 21, 2018 00:36
Bitcoin proof of work in pure lambda calculus
all:
node work2mlc.js getwork.json 381353fa >test.mlc
lambda -pem lib.mlc -f test.mlc
clean:
@camsaul
camsaul / hello_world.asm
Last active April 30, 2024 11:16
Hello World in NES (6502 / NESASM) Assembly. Display a single sprite
;;; -*- tab-width: 2 -*-
;;; These four lines go at the beginning of almost every code file. 16-byte iNES header
.inesprg 1 ; one bank of program code
.ineschr 1 ; one bank of picture data
.inesmap 0 ; we use mapper 0
.inesmir 1 ; Mirror setting always 1
;;; BANKING