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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//! Traits and operations for type-level booleans
//!
//! The operators on this type-level boolean representation are
//! implemented as associated types on the [`Boolean`] trait,
//! and don't require bounds other than `Boolean` to use them.
//!
//! # Example
//!
//! Returning different types depending on a type-level condition.
//!
//! ```rust
//! use nlist::{PeanoInt, Peano, peano};
//! use nlist::peano::IsLt;
//! use nlist::boolean::{self, Bool, Boolean, BoolWitG};
//! use nlist::typewit::{CallFn, TypeEq};
//!
//! assert_eq!(make(peano!(0)), "zero");
//! assert_eq!(make(peano!(1)), "one");
//! assert_eq!(make(peano!(2)), "two");
//! assert_eq!(make(peano!(3)), 3);
//! assert_eq!(make(peano!(4)), 4);
//!
//! // Function which returns different types depending on the value of `L`
//! //
//! // If L < 3, this returns a &'static str
//! // If L >= 3, this returns a usize
//! //
//! // The `-> CallFn<StrOrUsize, IsLt<L, Peano!(3)>>` return type calls the `StrOrUsize`
//! // type-level function with `IsLt<L, Peano!(3)>` as an argument.
//! //
//! // `IsLt<L, Peano!(3)>` effectively evaluates to `Bool<{L::USIZE < 3}>`
//! const fn make<L: PeanoInt>(_: L) -> CallFn<StrOrUsize, IsLt<L, Peano!(3)>> {
//! // Making a `BoolWitG<IsLt<L, Peano!(3)>>` with `Boolean::BOOL_WIT`
//! match IsLt::<L, Peano!(3)>::BOOL_WIT {
//! // lt_te is a proof that `IsLt<L, Peano!(3)> == Bool<true>`, i.e.: L < 3 == true
//! // lt_te: TypeEq<IsLt<L, Peano!(3)>, Bool<true>>
//! BoolWitG::True(lt_te) => {
//! // te is a proof that `CallFn<StrOrUsize, IsLt<L, Peano!(3)>> == &'static str`
//! let te: TypeEq<CallFn<StrOrUsize, IsLt<L, Peano!(3)>>, &'static str> =
//! lt_te.project::<StrOrUsize>();
//!
//! // using the type equality proof to coerce `&'static str` to the return type.
//! te.to_left(match L::USIZE {
//! 0 => "zero",
//! 1 => "one",
//! 2 => "two",
//! _ => panic!("unreachable"),
//! })
//! }
//!
//! // ge_te is a proof that `IsLt<L, Peano!(3)> == Bool<false>`, i.e.: L < 3 == false
//! // ge_te: TypeEq<IsLt<L, Peano!(3)>, Bool<false>>
//! BoolWitG::False(ge_te) => {
//! // te is a proof that `CallFn<StrOrUsize, IsLt<L, Peano!(3)>> == usize`
//! let te: TypeEq<CallFn<StrOrUsize, IsLt<L, Peano!(3)>>, usize> =
//! ge_te.project::<StrOrUsize>();
//!
//! // using the type equality proof to coerce `usize` to the return type.
//! te.to_left(L::USIZE)
//! }
//! }
//! }
//!
//! // StrOrUsize is a type-level function (`typewit::TypeFn` implementor),
//! // which takes a Boolean parameter.
//! //
//! // In pseudocode, this is what it does on the type level:
//! // fn StrOrUsize(B: Boolean) -> type {
//! // if B { &'static str } else { usize }
//! // }
//! type StrOrUsize = boolean::IfTrueAltFn<&'static str, usize>;
//!
//! ```
//!
use cratePeanoInt;
pub use ;
use ;
//////////////////////////////////////////////////////////////////////////////
/// [`typewit::TypeFn`] equivalents of boolean type aliases
pub use *;
//////////////////////////////////////////////////////////////////////////////
/// Type alias form of [`Boolean::IfTruePI`]
pub type IfTruePI<B, Then, Else> = IfTruePI;
/// Type alias form of [`Boolean::IfTrueB`]
pub type IfTrueB<B, Then, Else> = IfTrueB;
/// Type alias form of [`Boolean::IfTrue`]
pub type IfTrue<B, Then, Else> = IfTrue;
/// Type alias form of [`Boolean::Not`]
pub type Not<B> = Not;
/// Type alias form of [`Boolean::And`]
pub type And<L, R> = And;
/// Type alias form of [`Boolean::Or`]
pub type Or<L, R> = Or;
/// Type alias form of [`Boolean::Xor`]
pub type Xor<L, R> = Xor;
//////////////////////////////////////////////////////////////////////////////
/// Trait for bounding [type-level bools].
///
/// # Example
///
/// For an example that (indirectly) uses this trait,
/// you can look at the [module-level example](crate::boolean#example)
///
/// [type-level bools]: typewit::const_marker::Bool
/// Diverges when given a proof of `Bool<true> == Bool<false>`
/// (which is a contradiction, because they're different types).
pub const !