Skip to content

Instantly share code, notes, and snippets.

@ClarkeRemy
ClarkeRemy / multiplate.rs
Created April 28, 2025 04:22
Multiplate Rust
//! this library is largely "incorrect" in the sense that projectors should be exitentials and not part of type arguments
// https://hackage.haskell.org/package/multiplate-0.0.3/docs/src/Data-Generics-Multiplate.html
use std::{marker::PhantomData};
pub trait Funct {
type F<T>;
}
@ClarkeRemy
ClarkeRemy / rust_variance.rs
Last active April 24, 2025 07:27
rust lifetime variance (fail mains does note compile)
use std::marker::PhantomData;
// CONTRAVARIANT
struct Contra<'contra> (PhantomData<
fn(&'contra ()) -> ()
>);
// succeeds (contravariant "lengthening")
fn contra_1<'a>( contra : Contra<'a>) -> Contra<'static> {
contra
}
@ClarkeRemy
ClarkeRemy / main.ml
Last active April 3, 2025 10:33
Ocaml abstract list
type 'a list= List of { l : 'z . (unit -> 'z) -> ('a * 'a list -> 'z) -> 'z }
let nil : 'a list = List{ l = fun f -> fun _ -> f()}
let cons (v : 'a) (r : 'a list) : 'a list=
List{ l = fun _ -> fun f -> f(v,r) }
let ex : int list = cons 3 (cons 1 (cons 2 nil))
let length (l : 'a list) : int =
@ClarkeRemy
ClarkeRemy / bad_typeclass.prolog
Created April 1, 2025 14:36
Bad Typeclass Prolog
fAtoB_optionA_optionB(_, none, none).
fAtoB_optionA_optionB(F, option(A), option(B)) :- C =.. [F,A,B], C.
functor_map(option, fAtoB_optionA_optionB).
fmapAtoB_fA_fB(Func, F_A, F_B) :-
( nonvar(F_A) -> F_A =.. [F| _]
; nonvar(F_B) -> F_B =.. [F| _]
),
functor_map(F, Map),
@ClarkeRemy
ClarkeRemy / type_witness.rs
Created March 11, 2025 08:09
Type Witness Prototype
use std::marker::PhantomData;
/// We limit the size of a node to u16::MAX. If you want bigger, make a tree or use a Tensor
pub type Mem = u16;
pub type Offset = usize;
type InternalHandleRepr = u32;
struct InternalHandle<T>{handle : InternalHandleRepr, _boo : PhantomData<T>}
pub type ExtHandleRepr = u64;
@ClarkeRemy
ClarkeRemy / flatten_revisited.sml
Created February 10, 2025 11:47
Flatten Revisited
datatype 'a Tree =
Leaf
| Branch of {value : 'a, left : 'a Tree, right : 'a Tree}
fun flatten t = case t of
Leaf => []
| Branch {value, left, right} => let
@ClarkeRemy
ClarkeRemy / fix.rs
Created February 6, 2025 04:57
looking into fixpoint combinator in rust
// https://docs.rs/rust2fun/latest/src/rust2fun/combinator.rs.html#399-417
trait Rec<T, R> {
fn apply(&self, x: T) -> R;
}
impl<T, R, F> Rec<T, R> for F
where
F: Fn(&dyn Rec<T, R>, T) -> R,
@ClarkeRemy
ClarkeRemy / cats.rs
Created February 1, 2025 11:50
small revisiting Cats in Rust
use core::ops::ControlFlow;
use std::{marker::PhantomData, sync::Arc};
// HKT
#[allow(non_camel_case_types)]
pub type a_<'a, F, T> = <F as A_Funct>::F<'a, T>;
#[allow(non_camel_case_types)]
@ClarkeRemy
ClarkeRemy / TaglessFinal.sml
Created January 30, 2025 03:56
Tagless Final chat discord
datatype expr = PLUS of expr * expr
| NUM of int
fun eval_expr (NUM n) = n
| eval_expr (PLUS (l,r)) = eval_expr l + eval_expr r
fun pretty_expr (NUM n) = Int.toString n
| pretty_expr (PLUS (l,r)) = "( " ^ pretty_expr l ^ " + " ^ pretty_expr r ^ " )"
@ClarkeRemy
ClarkeRemy / grokking_the _sequent_calculus.sml
Last active March 25, 2025 15:47
Grokking the Sequent Calculus
fun y f x = f (y f) x
functor Fix(type 'a f )
= struct
type 'a f = 'a f
datatype fix = FIX of fix f
fun inj v = FIX v
fun prj (FIX v) = v
end