algebra/
wrapper.rs

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
//! Wrappers that attach an algebraic structure with a value type.

use std::ops::{Add, Neg, Sub, Mul, Div};
use std::fmt::{Display, Formatter, Error};

use ops::{Op, Inverse, Recip, Additive, Multiplicative};
use cmp::ApproxEq;
use ident::Identity;

use structure::MagmaApprox;
use structure::QuasigroupApprox;

/// Wrapper that allows to use operators on algebraic types.
#[derive(Clone, Copy, PartialOrd, PartialEq, Debug)]
pub struct Wrapper<M>(pub M);

impl<M: Display> Display for Wrapper<M> {
    fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
        self.0.fmt(fmt)
    }
}

/// Creates wrapper with identity value for a specific operator.
pub fn id<O: Op, M>(_: O) -> Wrapper<M>
where M: Identity<O>
{
    Wrapper(Identity::<O>::id())
}

impl<M> ApproxEq for Wrapper<M>
where M: ApproxEq
{
    type Eps = M::Eps;
    fn default_epsilon() -> Self::Eps {
        M::default_epsilon()
    }

    fn approx_eq_eps(&self, b: &Self, epsilon: &Self::Eps) -> bool {
        M::approx_eq_eps(&self.0, &b.0, epsilon)
    }
}

impl<M> Add<Wrapper<M>> for Wrapper<M>
where M: MagmaApprox<Additive>
{
    type Output = Self;
    fn add(self, lhs: Self) -> Self {
        Wrapper(self.0.approx(lhs.0))
    }
}

impl<M> Neg for Wrapper<M>
where M: QuasigroupApprox<Additive>
{
    type Output = Self;
    fn neg(mut self) -> Self {
        self.0 = self.0.inv();
        self
    }
}

impl<M> Sub<Wrapper<M>> for Wrapper<M>
where M: QuasigroupApprox<Additive>
{
    type Output = Self;
    fn sub(self, lhs: Self) -> Self {
        self + -lhs
    }
}

impl<M> Mul<Wrapper<M>> for Wrapper<M>
where M: MagmaApprox<Multiplicative>
{
    type Output = Self;
    fn mul(self, lhs: Self) -> Self {
        Wrapper(self.0.approx(lhs.0))
    }
}

impl<M> Recip for Wrapper<M>
where M: QuasigroupApprox<Multiplicative>
{
    type Result = Self;
    fn recip(self) -> Self {
        Wrapper(self.0.inv())
    }
}

impl<M> Div<Wrapper<M>> for Wrapper<M>
where M: QuasigroupApprox<Multiplicative>
{
    type Output = Self;
    fn div(self, lhs: Self) -> Self {
        self * lhs.inv()
    }
}