This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
structure Plus = struct | |
datatype 'a t = T of 'a -> 'a -> 'a | |
end | |
structure Mul = struct | |
datatype 'a t = T of 'a -> 'a -> 'a | |
end | |
structure FromInt = struct |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* direct style *) | |
fun substractorial n = | |
if n < 0 | |
then 0 | |
else n - substractorial (n-1) | |
(* Continuation passing style *) | |
(* In a functional language, when every branch is a "tail call", it optimizes to a jumps *) | |
(* this could already be turned into a loop pretty easily, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const interface_method = (object, _interface)=>(method) => ( | |
object._interface[_interface].hasOwnProperty(method) | |
&& (typeof object._interface[_interface][method]) == "function" | |
) | |
const implInterface = (_interface, invariant, object, impl) => { | |
if (!object.hasOwnProperty("_interface")) { | |
object._interface = {}; | |
} | |
object._interface[_interface] = impl | |
if (!invariant(object)) {throw `FAILED TO IMPLEMENT \`${_interface}\``} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(unused)] | |
use std::{borrow::Borrow, convert::Infallible}; | |
trait Functor { | |
type F<T>; | |
fn fmap<A, B>(f: impl Fn(A) -> B, x: Self::F<A>) -> Self::F<B>; | |
} | |
trait Rec: Sized { | |
type Fix; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 *) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![no_implicit_prelude] | |
#![allow(redundant_semicolons)] | |
extern crate core; | |
extern crate alloc; | |
use | |
{ core:: | |
{ option::Option::{*, self} | |
, result::Result::{*, self} |
NewerOlder