Skip to content

Instantly share code, notes, and snippets.

View brendanzab's full-sized avatar
😵‍💫
writing elaborators

Brendan Zabarauskas brendanzab

😵‍💫
writing elaborators
View GitHub Profile
(** Interpreters based on the SECD machine from “A Rational Deconstruction of Landin's SECD Machine”
- Olivier Danvy. 2003. A Rational Deconstruction of Landin’s SECD Machine.
BRICS Report Series 33 (October 2003). https://doi.org/10.7146/brics.v10i33.21801
*)
module Source = struct
type ident = string
@brendanzab
brendanzab / eval_stlc.pol
Last active August 7, 2025 23:30
Evaluator for STLC in Polarity, using HOAS in values
use "../std/data/bool.pol"
use "../std/data/nat.pol"
data Ty {
FunTy(a b: Ty),
NatTy,
BoolTy,
}
data Ctx {
@brendanzab
brendanzab / set.pol
Last active October 16, 2025 11:31
Set interface as a codata type in Polarity (see: https://polarity-lang.github.io)
data Bool { T, F }
data Nat { Z, S(n: Nat) }
def (n1: Nat).eq(n2: Nat): Bool {
Z => n2.match {
Z => T,
S(_) => F,
},
S(n1) => n2.match {
@brendanzab
brendanzab / intuitionistic.elf
Last active March 17, 2025 11:49
Intuitionistic type theory in Twelf (try in the playground at: https://twelf.org/twelf-wasm/)
%% Intuitionistic type theory in Twelf by Brendan Zabarauskas
%%
%% Inspired by the examples found in “An Equational Logical Framework
%% for Type Theories” https://arxiv.org/abs/2106.01484
tp : type.
el : tp -> type.
%% Equality
module type Isorecursive = sig
type t
type 'a layer
val roll : t layer -> t
val unroll : t -> t layer
end
module Option = struct
type 'a shape = [
| `Some of 'a
| `None
]
module type S = sig
type 'a t
@brendanzab
brendanzab / ast_folds.ml
Created February 25, 2025 11:30
Folding over an AST using recursion schemes (a response to https://bsky.app/profile/bandukwala.me/post/3liwrjfotek2a).
module Expr = struct
module Layer = struct
type 'a t =
| Mul of 'a * 'a
| Add of 'a * 'a
| Int of int
let map (f : 'a -> 'b) (layer : 'a t) : 'b t =
@brendanzab
brendanzab / eval_stmts_cps.ml
Last active February 11, 2025 03:46
An evaluator for an imperative language with loop, break, and continue in continuation passing style.
(** An evaluator for an imperative language with loop, break, and continue in
continuation passing style. *)
type expr =
| Var of string
| Let of string * expr * expr
| Seq of expr * expr
| Unit
@brendanzab
brendanzab / lambda_nbe.ml
Last active February 4, 2025 03:17
Evaluator for a lambda calculus with de Bruijn indices, levels, and defunctionalised closures. For more, check out the language garden: https://github.com/brendanzab/language-garden/
type index = int
type level = int
type expr =
| Var of index
| Let of string * expr * expr
| Fun_lit of string * expr
| Fun_app of expr * expr
@brendanzab
brendanzab / set_objects.ml
Last active November 18, 2024 13:56
Set examples in OCaml based on William R. Cook’s “On Understanding Data Abstraction, Revisited”
(** Set examples in OCaml from {{:https://www.cs.utexas.edu/~wcook/Drafts/2009/essay.pdf}
“On Understanding Data Abstraction, Revisited”} by William R. Cook
*)
(** ADT-style set implementations.
See Section 2 of the paper.
*)
module Abstract_data_types = struct