Skip to content

Instantly share code, notes, and snippets.

@mbillingr
mbillingr / types.dot
Last active March 27, 2024 16:23
A few example types
digraph {
subgraph cluster1 {
label="Any"
0 [ color=blue; label = "0: Var(0)" ]
1 [ color=red; label = "1: Value((VTop, Span(5)))" ]
}
subgraph cluster2 {
use std::cell::RefCell;
use std::collections::{HashMap, HashSet};
use std::rc::Rc;
use std::sync::atomic::{AtomicU64, Ordering};
fn main() {
use Term::*;
let id = Lam {
name: "x".into(),
body: Var { name: "x".into() }.into(),
@mbillingr
mbillingr / adt-demo.py
Created January 4, 2024 22:33
Python ADT Prototype
"""
Abstract Data Types in Python
=============================
(Python meta programming at its ugliest)
The implementation of `adt` is horrible. It could be improved by a few abstractions
for generating the different flavors of variant arguments.
I'm no longer sure if the use of exec is as bad as I initially thought. At least it's
relatively readable.
@mbillingr
mbillingr / hamt.py
Last active January 2, 2024 19:35
Prototype implementation of Ideal Hash Mapped Tries
from __future__ import annotations
import ctypes
import dataclasses
from typing import Any
LEAF_SIZE = 32
LEAF_MASK = LEAF_SIZE - 1
HASH_BITS = 64
@mbillingr
mbillingr / main.rs
Last active December 18, 2023 15:11
(WIP) Typing Haskell in Rust
// This is "Typying Haskell in Rust", based on "Typing Haskell in Haskell":
// https://web.cecs.pdx.edu/~mpj/thih/thih.pdf?_gl=1*1kpcq97*_ga*MTIwMTgwNTIxMS4xNzAyMzAzNTg2*_ga_G56YW5RFXN*MTcwMjMwMzU4NS4xLjAuMTcwMjMwMzU4NS4wLjAuMA..
use crate::Pred::IsIn;
use std::iter::once;
use std::rc::Rc;
type Result<T> = std::result::Result<T, String>;
macro_rules! list {
@mbillingr
mbillingr / main.rs
Last active November 23, 2023 12:45
Simple prototype for a Forth interpreter with function overloading
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
// What's missing:
// - branching (or more general, control flow) operations
// - records / structs or some form of named compound data type
type Symbol = &'static str;
type Block = Rc<[Op]>;
@mbillingr
mbillingr / main.py
Created September 26, 2023 11:45
simple evaluator with pattern matching
import ast
import dataclasses
from typing import Any, Optional, TypeAlias
Expr: TypeAlias = Any
class Context:
def __init__(self, env=None):
@mbillingr
mbillingr / tensor_autograd.rs
Last active August 16, 2023 13:48
Automatic differentiation on tensors prototype
use std::collections::HashMap;
use std::rc::Rc;
fn main() {
let x = &Tensor::new(vec![
vec![0.0, 0.0],
vec![1.0, 0.0],
vec![2.0, 0.0],
vec![0.0, 1.0],
vec![1.0, 1.0],
@mbillingr
mbillingr / main.rs
Created July 5, 2023 15:35
a basic evaluator in rust
use std::collections::HashMap;
use std::rc::Rc;
macro_rules! ident_list {
() => {
vec![]
};
($a:ident) => {
vec![stringify!(a)]
};
@mbillingr
mbillingr / mukanren.idr
Last active January 1, 2023 21:40
Implementation of µKanren in Idris
data Value = Null
| Var Nat
| Pair Value Value
| Num Int
data Substitution = Nil
| (::) (Nat, Value) Substitution
State : Type