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
@brendanzab
brendanzab / type_check_statements.rs
Last active September 2, 2024 13:32
Type checker for an extremely simple imperative programming language (Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=5d667459cabb1fc900473f85c541ee0a)
#![allow(dead_code)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum Type {
Unit, // ()
Int, // Int
Float, // Float
}
type Expr = Box<ExprNode>;
@brendanzab
brendanzab / open-interperters.ml
Last active August 25, 2024 02:28
Open interpreters using polymorphic variants and extensible variants.
(** This is a demonstration of implementing interpreters in an extensible way
that offers a partial solution to the expression problem. The idea is that
the language can be extended with more features after the fact, without
altering previous definitions. It also has the benefit of grouping the
related extensions to the syntax and semantic domains together with the
relevant evaluation rules in a per-feature way.
This approach used is similar to the one described by Matthias Blume in
{{:https://www.microsoft.com/en-us/research/video/records-sums-cases-and-exceptions-row-polymorphism-at-work/}
Records, sums, cases, and exceptions: Row-polymorphism at work}.
// WARNING: I have never programmed in gleam before and this is probably
// considered very unideomatic/cursed. You have been warned! :P
import gleam/io
import gleam/option.{type Option, None, Some}
type Iterator(a) {
Iterator(next: fn () -> Option(#(a, Iterator(a))))
}
@brendanzab
brendanzab / come-from.ml
Created July 28, 2024 09:58
Comefrom in OCaml, implemented with algebraic effects and handlers
(* Based on https://effekt-lang.org/examples/comefrom.html *)
module ComeFrom () : sig
val label : unit -> unit (* Label *)
val try_with : ?label:(unit -> unit (* E *)) -> ('a -> 'b (* Label *)) -> 'a -> 'b (* E *)
end = struct
type 'a Effect.t += Label : unit Effect.t
@brendanzab
brendanzab / univ.pol
Last active July 15, 2024 02:05
Coinductive universes in Polarity (see: https://polarity-lang.github.io/oopsla24/)
-- ------------------------------- --
-- Base types --
-- ------------------------------- --
data Top {
Unit,
}
data Bot { }
@brendanzab
brendanzab / streams.pol
Last active July 7, 2024 11:51
Playing with streams as codata in Polarity (see: https://polarity-lang.github.io/oopsla24/)
-- ------------------------------- --
-- Non-dependent functions --
-- ------------------------------- --
codata Fun(a b: Type) {
Fun(a, b).ap(a b: Type, x: a) : b,
}
-- ------------------------------- --
-- Top type --
@brendanzab
brendanzab / modules.rs
Created May 15, 2024 11:01
ML-style module thing in Rust (please forgive me)
use std::hash::Hash;
pub trait Key {
type T: Hash + Eq;
}
pub trait Map<K : Key> {
type T<V>;
fn new<V>() -> Self::T<V>;
@brendanzab
brendanzab / build_effects.ml
Last active July 7, 2024 08:09
An attempt at Build systems à la carte using algebraic effects
(* inspired by https://github.com/Tobi-3/build-systems-a-la-carte-with-effect-handlers/*)
type key = string
type value = int
type _ Effect.t +=
| Fetch : key -> value Effect.t
| NeedInput : key -> value Effect.t
| GetValue : key -> value option Effect.t
| SetValue : key * value -> unit Effect.t
(** Fractal binary trees
L-systems are a really wonderful model of plant growth, showing how a set of
simple grammar rules can result in incredibly life-like and complicated
organic forms.
They do however seem a little… stuck in the 80s? In L-systems the state of a
plant is modelled as a list of instructions, where the branching structure
emerges from an imperative interpretation of “push” and “pop” symbols. For
example an L-system that describes a fractal binary tree looks like this:
@brendanzab
brendanzab / stlc-infer.ml
Last active November 30, 2023 10:25
A demo of translating inference rules for the STLC into a type inference algorithm
(*
T ::=
| Bool
| T -> T
t ::=
| x
| \(x : T). t
| t t