Skip to content

Instantly share code, notes, and snippets.

@mbillingr
mbillingr / main.rs
Created December 13, 2024 22:28
Implementation of Syntactic Closures in Rust
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
pub fn main() {
let x = Value::Symbol(Symbol("x".into()));
let quote = Value::Symbol(Symbol("quote".into()));
let quotx = cons(quote.clone(), cons(x.clone(), nil()));
let lambda = Value::Symbol(Symbol("lambda".into()));
@mbillingr
mbillingr / main.rs
Created July 29, 2024 20:29
An Atom as in data-oriented programming. Unfortunately, it only works with static references.
use std::sync::atomic::{AtomicPtr, Ordering};
use std::time::Duration;
fn main() {
let counter = Box::leak(Box::new(Atom::new(0)));
for _ in 0..100 {
std::thread::spawn(|| {
counter.swap(inc);
});
@mbillingr
mbillingr / main.py
Created June 1, 2024 19:48
simple evaluator with macros
from __future__ import annotations
import dataclasses
from typing import Any, TypeAlias
Expr: TypeAlias = Any
@dataclasses.dataclass
class Context:
env: dict[str, Any] = dataclasses.field(default_factory=dict)
@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):