Skip to content

Instantly share code, notes, and snippets.

@kyleheadley
kyleheadley / traitlang.rs
Created November 14, 2018 17:22
Example of typed functional programming in Rust's type-level language
trait Nat {}
struct Zero;
impl Nat for Zero {}
struct Succ<N>(N);
impl<N:Nat> Nat for Succ<N> {}
type One = Succ <Zero >;
trait AddOne : Nat { type Result:Nat; }
impl<N:Nat> AddOne for N { type Result = Succ<N>; }
@kyleheadley
kyleheadley / trati-dsl.rs
Created November 14, 2018 17:24
A lambda DSL embedded in Rust
#![allow(unused)]
// Meta values and functions
// -------------------------
// Bool values
struct False;
struct True;
// Option values
@kyleheadley
kyleheadley / hkt_tree.rs
Last active March 18, 2020 17:13
demo a struct that can be used with Rc or Arc pointer types
use std::ops::Deref;
use std::rc::Rc;
use std::sync::Arc;
trait PtrFunc<A> {type Ptr:Sized+Clone+Deref+From<A>;}
struct RcPtr;
impl<A> PtrFunc<A> for RcPtr {type Ptr = Rc<A>;}
struct ArcPtr;
impl<A> PtrFunc<A> for ArcPtr {type Ptr = Arc<A>;}
@kyleheadley
kyleheadley / split_comma.rs
Last active April 18, 2021 19:41
Higher-order rust macro that assists in parsing by macro
/// Higher-order macro fold function for pre-parsing comma separated lists
///
/// The commas on KEYWORD lines can be changed to parse lists with any
/// separator.
/// run a macro on a list of lists after splitting the input at commas
macro_rules! split_comma {
// no defaults
{$fun:ident <= $($item:tt)*} => {
split_comma![$fun () () () <= $($item)*]
@kyleheadley
kyleheadley / inherit.rs
Created January 4, 2018 03:21
A model for trait-based inheritance in Rust
//! Demo of static "inheritance"
//!
//! Use trait objects to get dynamic inheritance,
//! but casting to a subtype is not explored here
////////////////////////////////////////////////////////
// Define Base type, interface, and what is overloadable
////////////////////////////////////////////////////////
/// The main type that will be extended
@kyleheadley
kyleheadley / untyped_lambda.rs
Created August 2, 2017 22:19
Implement the untyped lambda calculus in the Rust type system.
#![allow(unused)]
//#![feature(optin_builtin_traits)]
// Booleans
struct True;
struct False;
trait BoolOr<B> {type BoolOr;}
impl<B> BoolOr<B> for True {type BoolOr=True;}
impl BoolOr<True> for False {type BoolOr=True;}
impl BoolOr<False> for False {type BoolOr=False;}