summaryrefslogtreecommitdiff
path: root/reversible.rs
blob: 19e6f981cc662ab5f3d63cd1c2d7e963d2556166 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use std::marker::PhantomData;

pub trait Rev {
    type Input;
    type Output;

    fn forward(&self, i: Self::Input) -> Self::Output;
    fn reverse(&self, o: Self::Output) -> Self::Input;
}

#[derive(Clone)]
pub struct Addition<A> {
    amount: A
}
impl<A> Rev for Addition<A>
where
    A: std::ops::Add<Output = A> + std::ops::Sub<Output = A> + Copy
{
    type Input = A;
    type Output = A;

    fn forward(&self, a: A) -> A {
        a + self.amount
    }

    fn reverse(&self, b: A) -> A {
        b - self.amount
    }
}
pub fn plus<A>(amount: A) -> Addition<A> {
    Addition { amount }
}

// more blocks

#[derive(Clone)]
pub struct Negate<A>(PhantomData<A>);
impl<A: std::ops::Neg<Output = A>> Rev for Negate<A> {
    type Input = A;
    type Output = A;

    fn forward(&self, a: Self::Input) -> Self::Output {
        -a
    }

    fn reverse(&self, b: Self::Output) -> Self::Input {
        -b
    }
}
pub fn neg<A>() -> Negate<A> {
    Negate(PhantomData)
}

#[derive(Clone)]
pub struct Multiplication<A> {
    amount: A
}
impl<A> Rev for Multiplication<A>
where
    A: std::ops::Mul<Output = A> + std::ops::Div<Output = A> + Copy,
{
    type Input = A;
    type Output = A;

    fn forward(&self, a: A) -> A {
        a * self.amount
    }

    fn reverse(&self, b: A) -> A {
        b / self.amount
    }
}
pub fn times<A>(amount: A) -> Multiplication<A> {
    Multiplication { amount }
}

#[derive(Clone)]
pub struct DiscreteMap<A, B>(Vec<(A, B)>);
impl<A: PartialEq + Clone, B: PartialEq + Clone> Rev for DiscreteMap<A, B> {
    type Input = A;
    type Output = B;
    
    fn forward(&self, a: A) -> B {
        for i in self.0.iter() {
            if i.0 == a {
                return i.1.clone()
            }
        }
        panic!()
    }

    fn reverse(&self, b: B) -> A {
        for i in self.0.iter() {
            if i.1 == b {
                return i.0.clone()
            }
        }
        panic!()
    }
}
pub fn discrete<A, B>(vec: Vec<(A, B)>) -> DiscreteMap<A, B> {
    DiscreteMap(vec)
}

// combinators

#[derive(Clone)]
pub struct Pipe<X, Y>(X, Y);
impl<X, Y> Rev for Pipe<X, Y>
where
    X: Rev,
    Y: Rev<Input = X::Output>
{
    type Input = X::Input;
    type Output = Y::Output;

    fn forward(&self, a: Self::Input) -> Self::Output {
        self.1.forward(self.0.forward(a))
    }

    fn reverse(&self, c: Self::Output) -> Self::Input {
        self.0.reverse(self.1.reverse(c))
    }
}

#[derive(Clone)]
pub struct Inverse<X>(X);
impl<X: Rev> Rev for Inverse<X> {
    type Input = X::Output;
    type Output = X::Input;

    fn forward(&self, a: Self::Input) -> Self::Output {
        self.0.reverse(a)
    }

    fn reverse(&self, b: Self::Output) -> Self::Input {
        self.0.forward(b)
    }
}

pub trait RevExt: Rev + Sized {
    fn then<N: Rev<Input = Self::Output>>(self, other: N) -> Pipe<Self, N> {
        Pipe(self, other)
    }

    fn inverse(self) -> Inverse<Self> {
        Inverse(self)
    }
}
impl<T: Rev> RevExt for T {}

pub fn under<Op, Val>(op: Op, val: Val) -> impl Rev<Input = Op::Input, Output = Op::Input>
where
    Op: Rev + Clone,
    Val: Rev<Input = Op::Output, Output = Op::Output>
{
    op.clone().then(val).then(op.inverse())
}