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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//! `type_eval` provides data and evaluation types to the compiler, unburdened
//! by the limitations of execution code semantics.
//!
//!
//! ### Erase requirement of machine representation:
//!
//! ```rust
//! # use type_eval::{prelude::*, BoolExpr, NumExpr};
//! #[deprecated(note="use NumRet<DivExpr> instead")]
//! use core::ops::Div;
//! use core::cmp::Eq;
//! pub const fn safe_div_u32(numerator: u32, denominator: u32) -> Option<u32> {
//! if denominator == 0 {
//! None
//! } else {
//! Some(numerator / denominator)
//! }
//! }
//!
//! use type_eval::{NumRet, /* MemRep */};
//! fn main() {
//! // let safe_dived = safe_div(4u16, 2usize);
//! // TODO: memory representatiov will be released very shortly
//! // let safe_dived = NumRet<DivExp<U4, U2>>::U32;
//! }
//! ```
//! ### Enforce predicates/preconditions into the type-system:
//!
//!```
//! # use type_eval::{prelude::*, BoolExpr, NumExpr};
//!
//! // define functions that carry a proof of evaluation result
//! const fn _b0<E: NumExpr<Ret = U0>>() {}
//! const fn _b1<E: NumExpr<Ret = U1>>() {}
//! const fn _b2<E: NumExpr<Ret = U2>>() {}
//! const fn _b3<E: NumExpr<Ret = U3>>() {}
//! #[allow(non_upper_case_globals)]
//! fn add_sub() {
//! const _2_ADD_1__SUB_3: () = _b0::<SubExp<AddExp<U2, U1>, U3>>();
//! const _6_SUB__1_ADD_3: () = _b2::<SubExp<U6, AddExp<U1, U3>>>();
//! // const COMPILE_FAIL: () = _b2::<SubExp<U7, AddExp<U1, U3>>>();
//!
//! }
//!
//! fn shift_msb() {
//! const _MSB__2_SHL_1: () = _b2::<MSB<ShLExp<U2, U1>>>();
//! const _MSB__2_SHL_0: () = _b1::<MSB<ShLExp<U2, U0>>>();
//!
//! const _MSB_4__SUB__MSB_3: () = _b1::<SubExp<MSB<U4>, MSB<U3>>>();
//! const _MSB_4__ADD__MSB_3: () = _b3::<AddExp<MSB<U4>, MSB<U3>>>();
//! }
//!
//! fn ifs() {
//! // if (1/2) < 1 {0} else {1 / 0}
//! const _IF_T_U0_DIV0: () = _b0::<IF<LT<DivExp<U1, U2>, U1>, U0, DivExp<U1, U0>>>();
//! // div-by-zero is a compile fail
//! // const _COMPILE_FAIL: () = _b0::<DivExp<U1, U0>>();
//! }
//!
//! fn lt4() {
//! // define a function that carries a proof of evaluaton result < 6
//! const fn _lt6<E: NumExpr>()
//! where
//! LT<E::Ret, U6>: BoolExpr<Ret = True>,
//! {
//! }
//!
//! // 2 + 3 < 6
//! const _5_LT_6: () = _lt6::<AddExp<U2, U3>>();
//! // 2 * 3 !< 6
//! // const FAIL: () = _lt6::<MulExp<U2, U3>>();
//! }
//!```
//! let's observe the compilation failure:
//! ```compile_fail
//! # use type_eval::{prelude::*, BoolExpr, NumExpr};
//! # #[allow(non_upper_case_globals)]
//! fn lt4() {
//! // define a function that carries a proof of evaluaton result < 6
//! const fn _lt6<E: NumExpr>()
//! where
//! LT<E::Ret, U6>: BoolExpr<Ret = True>,
//! {}
//!
//! // 2 * 3 !< 6
//! const FAIL: () = _lt6::<MulExp<U2, U3>>();
//! }
//!```
//!
//! ## Propogate predicates/preconditions downstream via the type-system:
//!
//! Enforce co-veraiance of predicates when extending traits
//! In this example, a submatrix of XS/XY dimensions wants to be contained
//! within a "parent" matrix of X/Y dimension
//!
//! The top-left corner (let's call it XC/YCof the sub-matrix is constrained
//! so it's wholely contained within the parent matrix. i.e.
//!
//! `XC + XS < X && YC + YS < Y`
//!
//!```rust
//! use type_eval::{prelude::*, BoolExpr};
//! // A keyboard matrix trait.
//! trait KBMatrix
//! // TODO: impl non-zero
//! // where
//! // GTE<Self::Width, U1>: BoolExpr<Ret = True>,
//! // GTE<Self::Height, U1>: BoolExpr<Ret = True>,
//! {
//! type Width: NumberVal;
//! type Height: NumberVal;
//! }
//! // a convenience definition to get a parent-matrix width and height
//! type ParentWidth<S: SubMatrix> = <S::Parent as KBMatrix>::Width;
//! type ParentHeight<S: SubMatrix> = <S::Parent as KBMatrix>::Height;
//! // You can define a sub matrix such that the compiler will cause an error
//! // if it's not contained within the parent matrix
//! trait SubMatrix
//! where
//! GTE<Self::Width, U1>: BoolExpr<Ret = True>,
//! GTE<Self::Height, U1>: BoolExpr<Ret = True>,
//! // this is effectively `assert!(x + width <= parent.width)`, but in the type system
//! LTE<AddExp<Self::Width, Self::XLoc>, ParentWidth<Self>>: BoolExpr<Ret = True>,
//! // this is effectively `assert!(y + height <= parent.height)`, but in the type system
//! LTE<AddExp<Self::Height, Self::YLoc>, ParentHeight<Self>>: BoolExpr<Ret = True>,
//! {
//! type Parent: KBMatrix;
//! type Width: NumberVal;
//! type XLoc: NumberVal;
//! type Height: NumberVal;
//! type YLoc: NumberVal;
//! }
//! ```
/// Implementors of [`BoolExpr`]
/// Implementors of [`NumExpr`]
/// Constructors for numbers expressed at the type-level
/// Integers expressed as types, e.g.: [`prelude::U0`], [`prelude::U1`]
/// Inner implementation types. Generally not intended for end-use
/// An expression returning a [`prelude::NumberVal`]
/// <T as [NumExpr]>::Ret helper
pub type NumRet<T> = Ret;
/// An expression returning a [`prelude::BoolVal`]
/// <T as [BoolExpr]>::Ret helper
pub type BoolRet<T> = Ret;
/// An expression returning a [`prelude::BoolVal`]
/// <T as [BoolExpr]>::Ret helper
pub type OrdRet<T> = Ret;