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 / 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

What is a type” in the semantic domain called?

One frustration I’ve run into when making a type checker/elaborator for a programming language is the following gap in terminology:

  • term1 / value
  • type / ???

This most commonly pops up when using normalisation-by-evaluation as part of a type checker, for example in the elab-system-f-bidirectional or elab-dependent projects in my language garden:

Footnotes

  1. Or an “expression”

@brendanzab
brendanzab / bank-account.pol
Last active September 26, 2024 10:38
Bank account interface as a codata type in Polarity (see: https://polarity-lang.github.io)
-- UML Class diagram:
--
-- ┌───────────────────────────────────┐
-- │ BankAccount |
-- ├───────────────────────────────────┤
-- │ - owner : String |
-- │ - balance : Dollars = 0 |
-- ├───────────────────────────────────┤
-- │ + getOwner() : String |
-- │ + getBalance() : Dollars |
@brendanzab
brendanzab / type_check_statements.rs
Last active September 26, 2024 04:11
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 { // t ::=
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>;