Skip to content

Instantly share code, notes, and snippets.

@ClarkeRemy
ClarkeRemy / recursion_schemes.sml
Last active August 16, 2024 12:58
Recursion Schemes SML extended
signature FUNCTOR = sig
type 'a F
val fmap : ('a -> 'b) -> 'a F -> 'b F (* F is a category theory functor *)
end
signature FUNCTOR_FIX = sig
include FUNCTOR
type fix (* The Fixpoint type *)
val prj : fix -> fix F (* Recursive *)
@ClarkeRemy
ClarkeRemy / main.rs
Created August 8, 2024 17:56
Recursion Schemes in Rust (naive)
trait Rec : Sized{
type F<T>;
fn fmap<A,B>(f : impl Fn(A)->B, x : Self::F<A>) -> Self::F<B>;
fn prj(t : Self)->Self::F<Self>;
fn inj(t : Self::F<Self>)->Self;
}
fn fold<R : Rec, A>(step : &impl Fn(R::F<A>)->A, x : R )->A {
@ClarkeRemy
ClarkeRemy / main.rs
Last active August 17, 2024 12:28
Flatten State Machine
fn flatten1(t: BinaryTree) -> Vec<i32> {
match t {
BinaryTree::Leaf(leaf) => Vec::from([leaf]), /* ret */
BinaryTree::Branch((l, r)) => {
let mut left /* ret */ = flatten1(*l); //<- rec remember!(r)
let right/* ret */ = flatten1(*r); //<- rec remember!(left)
left.extend(right);
@ClarkeRemy
ClarkeRemy / holes_1.rs
Created July 27, 2024 10:11
Rust with Holes
use std::marker::PhantomData;
pub fn main() {
let mut s = Splitter::new();
s.list[0] = 1;
s.id = 5;
println!("Splitter {{list : [{},{}], id : {} }}", s.list[0], s.list[1], s.id);
@ClarkeRemy
ClarkeRemy / recursion_schemes.sml
Created July 26, 2024 14:42
Recursion schemes based on Wang Murphy paper
(* https://www.cs.cmu.edu/~tom7/papers/wang-murphy-recursion.pdf *)
signature TYP = sig
type t
type 'a F
val Fmap : ('a -> 'b) -> 'a F -> 'b F
val inj : t F -> t
val prj : t -> t F
end
@ClarkeRemy
ClarkeRemy / sml_style.rs
Created July 23, 2024 03:40
based on Okazaki's book
#![no_implicit_prelude]
#![allow(redundant_semicolons)]
extern crate core;
extern crate alloc;
use
{ core::
{ option::Option::{*, self}
, result::Result::{*, self}
@ClarkeRemy
ClarkeRemy / rs.rs
Created July 17, 2024 13:42
Recursion schemes first steps
// struct FixSexpr(Sexpr<alloc::boxed::Box<FixSexpr>>);
enum SExpr<A=()> {
Ground(Arc<str>),
Var(i64),
App(App<A>)
}
struct App<A> { f : A, a : A}
trait Functor {
type F<T>;
@ClarkeRemy
ClarkeRemy / flatten.sml
Last active May 3, 2024 20:11
Continuation passing style flatten sml
datatype 'a Tree =
Leaf
| Branch of {value : 'a, left : 'a Tree, right : 'a Tree}
fun infix_flatten t = case t of
Leaf => []
| Branch {value, left, right} => let
val l = infix_flatten left
val r = infix_flatten right
in
@ClarkeRemy
ClarkeRemy / Pregnancy.txt
Last active April 22, 2024 15:23
I wrote C code for a friend
Table 2. Average age of mother at first birth: United States and each state, 2006
[By place of residence]
All races and
origins
Non-Hispanic
white
Non-Hispanic
black
@ClarkeRemy
ClarkeRemy / defunctionalization.rs
Created April 10, 2024 03:44
defunctionalization
use std::{process::Output, usize};
use crate::st::Process;
fn main() {
type T = Tree;
let tree = T::branch(
T::branch(T::Leaf, 1, T::Leaf),
2,
T::branch(T::branch(T::Leaf, 3, Tree::Leaf), 4, Tree::Leaf),
);