Skip to content

Instantly share code, notes, and snippets.

@DarinM223
DarinM223 / generic_ptr.rs
Last active December 20, 2025 19:48
Abstract over dereferenceable containers in Rust
use std::fmt::Debug;
use std::ops::Deref;
use std::ptr::NonNull;
trait Ptr {
type T<'a, U: 'a>: Deref<Target = U>;
}
struct BoxPtr;
impl Ptr for BoxPtr {
@DarinM223
DarinM223 / self_referential.rs
Last active December 11, 2025 16:47
Pin examples
extern crate pin_project_lite; // 0.2.16project;
use pin_project_lite::pin_project;
use std::marker::PhantomPinned;
use std::pin::{Pin, pin};
use std::ptr::NonNull;
pin_project! {
#[derive(Debug)]
pub struct A {
@DarinM223
DarinM223 / quadtree.py
Last active September 28, 2025 18:12
Numba experiments
"""
Quadtree in Numba:
Notes:
1. `if field:` or `if not field:` for an optional field
type doesn't work in numba, instead do `if field is not None:` or
`if field is None:`.
2. There is a bug with numba code generation with assignment
to deferred types. To work around that, add `set_field` methods
in the @jitclass (like `set_top_left_tree`) and use those inside a @njit
@DarinM223
DarinM223 / anf.sml
Last active August 14, 2025 06:21
ANF Conversion where all calls should be in tail position (fully CPS)
structure Lam =
struct
datatype bop = Add | Sub | Mul
val showBop = fn Add => "Add" | Sub => "Sub" | Mul => "Mul"
datatype exp = Lit of int | Bop of bop * exp * exp | Call of string * exp list
type var = string
val showVar = fn t0 => "\"" ^ t0 ^ "\""
@DarinM223
DarinM223 / adt.c
Last active July 21, 2025 16:10
Modeling various high level language features in C
#include <stdio.h>
enum bop {
BOP_PLUS,
BOP_MINUS,
BOP_TIMES,
};
typedef struct ast_exp ast_exp;
@DarinM223
DarinM223 / SoundEager.hs
Last active May 30, 2025 01:40
Type inference using Bluefin
{-# OPTIONS_GHC -Wno-orphans #-}
module SoundEager where
import Bluefin.Compound (Handle, mapHandle, useImpl, useImplIn)
import Bluefin.Eff (Eff, Effects, runPureEff, (:&), (:>))
import Bluefin.Internal (StateSource (StateSource))
import Bluefin.State (State, evalState, get, modify, put)
import Bluefin.StateSource (newState, withStateSource)
import Data.Functor ((<&>))
@DarinM223
DarinM223 / regalloc.py
Last active September 28, 2025 19:31
Linear scan register allocation in Python
from __future__ import annotations # type: ignore
from typing import * # type: ignore
from dataclasses import dataclass, field
@dataclass
class Physical:
register: int
@DarinM223
DarinM223 / build_sharp.sh
Last active September 23, 2023 20:44
Loads all the dependencies of a smi file and then runs the command
#!/bin/bash
cat > build.smi <<EOL
_require "basis.smi"
_require "smlnj-lib.smi"
EOL
cat > build.sml <<EOL
signature PACK_REAL = sig
type real
@DarinM223
DarinM223 / Eval.hs
Last active May 3, 2023 18:33
Example of an interpreter with recursive types using compdata
-- You need DeepSubsumption enabled in order for the example to compile.
{-# LANGUAGE DeepSubsumption #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE UndecidableInstances #-}
module Eval where
import Control.Monad.State.Strict (StateT, evalStateT, gets, liftIO, modify', void)
import Data.Comp.Multi
@DarinM223
DarinM223 / generic-mutrec.sml
Last active June 3, 2023 15:30
Example of type indexed values using mutual recursion with multiple type variables
infix & +` *`
datatype ('a, 'b) stmt =
Assign of 'a * ('a, 'b) expr
| If of ('a, 'b) expr * ('b, 'a) stmt list * ('a, 'b) stmt list
and ('a, 'b) expr =
Stmt of ('a, 'a) stmt
| Int of 'b
| Bop of ('b, 'a) expr * ('a, 'b) expr