-
Notifications
You must be signed in to change notification settings - Fork 8
/
natural.rs
186 lines (151 loc) · 4.93 KB
/
natural.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use std::num::NonZeroUsize;
use crate::token::variance::bound::{
Bounded, BoundedVariantRange, Boundedness, Unbounded, VariantRange,
};
use crate::token::variance::invariant::separation::{
BoundaryTerm, Finalize, SeparatedTerm, Termination,
};
use crate::token::variance::invariant::{BoundedVariance, Invariant, InvariantTerm, One, Zero};
use crate::token::variance::ops::{self, Conjunction, Disjunction, Product};
use crate::token::variance::{TokenVariance, Variance};
macro_rules! impl_natural_invariant_term {
($name:ident $(,)?) => {
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct $name(usize);
impl $name {
pub const ZERO: Self = $name(0);
pub const ONE: Self = $name(1);
pub const fn new(n: usize) -> Self {
$name(n)
}
pub fn map<F>(self, f: F) -> Self
where
F: FnOnce(usize) -> usize,
{
$name(f(self.0))
}
pub fn zip_map<F>(self, rhs: Self, f: F) -> Self
where
F: FnOnce(usize, usize) -> usize,
{
$name(f(self.0, rhs.0))
}
}
impl AsRef<usize> for $name {
fn as_ref(&self) -> &usize {
&self.0
}
}
impl BoundedVariance for $name {
type Bound = BoundedVariantRange;
}
impl Conjunction for $name {
type Output = Self;
fn conjunction(self, rhs: Self) -> Self::Output {
self.zip_map(rhs, ops::conjunction)
}
}
impl From<usize> for $name {
fn from(n: usize) -> $name {
$name(n)
}
}
impl From<$name> for usize {
fn from(size: $name) -> usize {
size.0
}
}
impl InvariantTerm for $name {
fn bound(lhs: Self, rhs: Self) -> Boundedness<Self::Bound> {
let [lower, upper] = crate::minmax(lhs, rhs);
BoundedVariantRange::try_from_lower_and_upper(lower.0, upper.0)
.map_or(Unbounded, Bounded)
}
fn into_lower_bound(self) -> Boundedness<Self::Bound> {
NonZeroUsize::new(self.0)
.map(BoundedVariantRange::Lower)
.map(From::from)
.unwrap_or(Unbounded)
}
}
impl One for $name {
fn one() -> Self {
$name(1)
}
}
impl PartialEq<usize> for $name {
fn eq(&self, rhs: &usize) -> bool {
self.0 == *rhs
}
}
impl Product<usize> for $name {
type Output = Self;
fn product(self, rhs: usize) -> Self::Output {
self.map(|lhs| ops::product(lhs, rhs))
}
}
impl Product<VariantRange> for $name {
type Output = TokenVariance<Self>;
fn product(self, rhs: VariantRange) -> Self::Output {
NonZeroUsize::new(self.0).map_or_else(TokenVariance::<Self>::zero, |lhs| {
Variance::Variant(rhs.map_bounded(|rhs| ops::product(rhs, lhs)))
})
}
}
impl Zero for $name {
fn zero() -> Self {
$name(0)
}
}
impl Conjunction<$name> for BoundedVariantRange {
type Output = Self;
fn conjunction(self, rhs: $name) -> Self::Output {
self.translation(rhs.0)
}
}
impl Disjunction<$name> for BoundedVariantRange {
type Output = VariantRange;
fn disjunction(self, rhs: $name) -> Self::Output {
self.union(rhs.0)
}
}
};
}
impl_natural_invariant_term!(Depth);
impl_natural_invariant_term!(Size);
impl Invariant for Depth {
type Term = DepthTerm;
fn finalize(term: Self::Term) -> TokenVariance<Self> {
term.finalize()
}
}
impl Invariant for Size {
type Term = TokenVariance<Self>;
fn finalize(term: Self::Term) -> TokenVariance<Self> {
term
}
}
pub type DepthTerm = BoundaryTerm<Depth>;
impl TokenVariance<Depth> {
pub fn is_exhaustive(&self) -> bool {
!self.has_upper_bound()
}
}
impl Finalize for SeparatedTerm<TokenVariance<Depth>> {
type Output = TokenVariance<Depth>;
fn finalize(self) -> Self::Output {
use Termination::{Closed, Open};
let SeparatedTerm(termination, term) = self;
match termination {
Open => ops::conjunction(term, One::one()),
Closed => term.map_invariant(|term| term.map(|term| term.saturating_sub(1))),
_ => term,
}
}
}
impl BoundaryTerm<Depth> {
pub fn is_exhaustive(&self) -> bool {
self.clone().finalize().is_exhaustive()
}
}