This commit is contained in:
andreas 2022-09-06 19:14:00 +02:00
commit b14b3a4f29
Signed by: andreas
GPG Key ID: D97194E55873280A
4 changed files with 145 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "statemonad"
version = "0.1.0"

8
Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "statemonad"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

129
src/main.rs Normal file
View File

@ -0,0 +1,129 @@
mod state {
// State monad
pub struct WithState<'c, Value, State>(Box<dyn FnOnce(State) -> (Value, State) + 'c>);
impl <'c, Value, State> WithState<'c, Value, State> {
pub fn of(f: impl FnOnce(State) -> (Value, State) + 'c) -> WithState<'c, Value, State> {
WithState(Box::new(f))
}
pub fn run(self, s: State) -> (Value, State) {
self.0(s)
}
pub fn bind<'cn, NewValue>(self, f: impl FnOnce(Value) -> WithState<'cn, NewValue, State> + 'cn) -> WithState<'cn, NewValue, State>
where 'c: 'cn, State : 'cn, Value : 'cn {
WithState::<'cn, NewValue, State>::of(move |n| {
let (x,sp) = self.run(n);
f(x).run(sp)
})
}
pub fn zip<'cn, NewValue>(self, st: WithState<'cn, NewValue, State>) -> WithState<'cn, (Value,NewValue), State>
where 'c: 'cn, State: 'cn, Value: 'cn, NewValue: 'cn {
self.bind(|aa| st.map(|bb| (aa,bb)))
}
pub fn map<'cn, NewValue>(self, f: impl FnOnce(Value) -> NewValue + 'cn) -> WithState<'cn, NewValue, State>
where 'c: 'cn, State : 'cn, Value : 'cn {
WithState::<'cn, NewValue, State>::of(move |n| {
let (x, sp) = self.run(n);
( f(x), sp )
})
}
}
}
mod labelgenerators {
use super::state::WithState;
// Generate a next label
pub trait States {
type Item;
fn init() -> Self::Item;
fn next<'c, Value>(f: impl FnOnce(Self::Item) -> Value + 'c) -> WithState<'c, Value, Self::Item>;
}
pub struct IntCounter;
impl States for IntCounter
{
type Item = i32;
fn init() -> i32 { 0 }
fn next<'c, Value>(f: impl FnOnce(i32) -> Value + 'c) -> WithState<'c, Value, i32> {
WithState::of(move |n| { (f(n), n + 1) })
}
}
pub struct StrCat;
impl States for StrCat
{
type Item = String;
fn init() -> String { "a".to_string() }
fn next<'c, Value>(f: impl FnOnce(Self::Item) -> Value + 'c) -> WithState<'c, Value, Self::Item> {
WithState::of(move |n: String| { let s = n.clone(); (f(n), format!("{}a", s)) })
}
}
}
use labelgenerators::*;
use state::WithState;
// Tree
#[derive(Debug,Clone)]
enum Tree<A> {
Leaf(A),
Node(Box<Tree<A>>, Box<Tree<A>>)
}
impl <A> Tree<A> {
fn node(l: Tree<A>, r: Tree<A>) -> Tree<A> {
Tree::Node(Box::new(l), Box::new(r))
}
fn label<NodeLabler : States>(self) -> Tree<NodeLabler::Item>
where NodeLabler::Item : Clone {
self.mlabel::<NodeLabler>().run(NodeLabler::init()).0
}
// Monadically label a Tree<A> to Tree<L>
fn mlabel<'c, NodeLabler: States>(self) -> WithState<'c, Tree<NodeLabler::Item>, NodeLabler::Item>
where NodeLabler::Item : Clone + 'c {
match self {
Tree::Leaf(_) => NodeLabler::next(|n| Tree::Leaf(n)),
Tree::Node(l, r) => {
let (l,r) = (*l, *r);
l.mlabel::<NodeLabler>().zip(r.mlabel::<NodeLabler>())
.map(|(ll,rr)| Tree::node(ll,rr))
}
}
}
}
fn main() {
let init = Tree::node(
Tree::node(
Tree::Leaf('a'),
Tree::Leaf('b')),
Tree::node(
Tree::node(
Tree::Leaf('c'),
Tree::Leaf('d')),
Tree::Leaf('e'))
);
let mapped = init.clone().label::<IntCounter>();
dbg!(init);
dbg!(mapped);
}