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
@rntz
rntz / alltogethernow.py
Created May 21, 2025 05:30
worst-case optimal seekable iterators with union & intersection in Python as coroutines
import bisect
from dataclasses import dataclass
class Bound:
def to_bound(self): return self
def __lt__(self, other):
return self <= other and self != other
def __le__(self, other):
if isinstance(self, Init) or isinstance(other, Done): return True
if isinstance(self, Done) or isinstance(other, Init): return False
@rntz
rntz / iters_alltogethernow_minimal.hs
Created May 20, 2025 05:06
seekable sorted worst-case optimal iterators in Haskell
-- A lower bound in a totally ordered key-space k; corresponds to some part of an
-- ordered sequence we can seek forward to.
data Bound k = Init | Atleast !k | Greater !k | Done deriving (Show, Eq)
instance Ord k => Ord (Bound k) where
Init <= _ = True
_ <= Done = True
_ <= Init = False
Done <= _ = False
Atleast x <= Atleast y = x <= y
Atleast x <= Greater y = x <= y
@alt-romes
alt-romes / UnsureCalculator.hs
Created April 24, 2025 23:38
Unsure Calculator in exactly 100 lines of Haskell
{- cabal:
build-depends: base, random, containers
-}
{-# LANGUAGE GADTs, ViewPatterns, GHC2021 #-}
import Text.Printf
import Control.Monad (liftM, ap)
import Data.Function (on)
import Data.List (sort, minimumBy)
import System.Random
import qualified Data.Map as M
open import Data.Nat
open import Data.Nat.Properties
open import Relation.Binary.PropositionalEquality
open import Data.Product
open import Data.Sum
open import Relation.Nullary
module MonoidNat3 where
data Expr : Set where
@Lysxia
Lysxia / CoDebruijn.agda
Created February 17, 2025 20:00
CoDebruijn scoped syntax of untyped lambda calculus
module CoDebruijn where
open import Data.Product using (_×_; _,_; ∃-syntax)
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl)
data Context : Set where
O : Context
1+_ : Context → Context
data Singleton : Context → Set where
@TOTBWF
TOTBWF / Thorin.ml
Last active January 30, 2025 01:40
A short and sweet implementation of NbE for Thorin.
(** {1 Syntax} *)
(** Expressions. *)
type expr =
| Var of cont * int
(** Local references.
Thorin is a "blockless" IR, so we can reference arguments of
other nodes by adding a data dependency edge, which implicitly
nests the two nodes. *)
@rntz
rntz / fix-eval-eager.ml
Last active December 7, 2024 20:58
Eager, lazy, & small-step "correct" fixed points
type var = string
type term = Var of var
| Lam of var * term
| App of term * term
| Fix of var * term (* only used for functions *)
| Lit of int
type value = Int of int
| Fun of (value -> value)
@rntz
rntz / mapgen.hs
Created November 13, 2024 03:04
generating maps
import System.Environment (getArgs)
import Control.Monad (guard, forM_)
import System.Random (RandomGen)
import qualified System.Random as Random
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
description
Inversion hell.
@AndrasKovacs
AndrasKovacs / TwoStageRegion.md
Last active June 10, 2025 20:19
Lightweight region memory management in a two-stage language