aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 2de2e3a0ce932357717f6aa29d801d16166bb014 (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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#![recursion_limit="500"]

//! [source 1](https://aphyr.com/posts/342-typing-the-technical-interview)
//! [source 2](https://sdleffler.github.io/RustTypeSystemTuringComplete/)
use std::marker::PhantomData;

trait Typed {
    type Type;
}

struct ListType<T>(PhantomData<T>);

struct Nil<T>(PhantomData<T>);
impl<T> Typed for Nil<T> { type Type = ListType<T>; }
struct Cons<X, XS>(PhantomData<X>, PhantomData<XS>);
impl<T, X: Typed<Type=T>, XS: Typed<Type=ListType<T>>> Typed for Cons<X, XS> { type Type = ListType<T>; }

trait First<T>: Typed<Type=ListType<T>> {
    type X: Typed<Type=T>;
}

impl<T, X: Typed<Type=T>, XS: Typed<Type=ListType<T>>> First<T> for Cons<X, XS> {
    type X = X;
}

trait ListConcat<X> {
    type C: Typed<Type=ListType<X>>;
}

impl<T, X: Typed<Type=ListType<T>>> ListConcat<T> for (Nil<T>, X) {
    type C = X;
}

impl<T, A: Typed<Type=T>, As: Typed<Type=ListType<T>>, Bs: Typed<Type=ListType<T>>> ListConcat<T> for (Cons<A, As>, Bs)
    where (As, Bs): ListConcat<T> {
    type C = Cons<A, <(As, Bs) as ListConcat<T>>::C>;
}

trait ListConcatAll<T>: Typed<Type=ListType<ListType<T>>> {
    type L: Typed<Type=ListType<T>>;
}

impl<T> ListConcatAll<T> for Nil<ListType<T>> {
    type L = Nil<T>;
}

impl<T, Chunk: Typed<Type=ListType<T>>, Rest: Typed<Type=ListType<ListType<T>>>> ListConcatAll<T> for Cons<Chunk, Rest>
    where Rest: ListConcatAll<T>, (Chunk, <Rest as ListConcatAll<T>>::L): ListConcat<T> {
    type L = <(Chunk, <Rest as ListConcatAll<T>>::L) as ListConcat<T>>::C;
}

struct BoolType;

struct True;
impl Typed for True { type Type = BoolType; }
struct False;
impl Typed for False { type Type = BoolType; }

trait Not: Typed<Type=BoolType> {
    type B: Typed<Type=BoolType>;
}

impl Not for True {
    type B = False;
}

impl Not for False {
    type B = True;
}

trait Or {
    type B: Typed<Type=BoolType>;
}

impl Or for (True, True) {
    type B = True;
}

impl Or for (True, False) {
    type B = True;
}

impl Or for (False, True) {
    type B = True;
}

impl Or for (False, False) {
    type B = False;
}

trait AnyTrue: Typed<Type=ListType<BoolType>> {
    type T: Typed<Type=BoolType>;
}

impl AnyTrue for Nil<BoolType> {
    type T = False;
}

impl<More: Typed<Type=ListType<BoolType>>> AnyTrue for Cons<True, More> {
    type T = True;
}

impl<List> AnyTrue for Cons<False, List> where List: AnyTrue {
    type T = <List as AnyTrue>::T;
}

struct NatType;

struct N0;
impl Typed for N0 { type Type = NatType; }
struct N1;
impl Typed for N1 { type Type = NatType; }
struct N2;
impl Typed for N2 { type Type = NatType; }
struct N3;
impl Typed for N3 { type Type = NatType; }
struct N4;
impl Typed for N4 { type Type = NatType; }
struct N5;
impl Typed for N5 { type Type = NatType; }
struct N6;
impl Typed for N6 { type Type = NatType; }
struct N7;
impl Typed for N7 { type Type = NatType; }
struct N8;
impl Typed for N8 { type Type = NatType; }

trait Succ: Typed<Type=NatType> {
    type N: Typed<Type=NatType>;
}

impl Succ for N1 { type N = N0; }
impl Succ for N2 { type N = N1; }
impl Succ for N3 { type N = N2; }
impl Succ for N4 { type N = N3; }
impl Succ for N5 { type N = N4; }
impl Succ for N6 { type N = N5; }
impl Succ for N7 { type N = N6; }
impl Succ for N8 { type N = N7; }

trait PeanoEqual {
    type T: Typed<Type=BoolType>;
}

impl PeanoEqual for (N0, N0) {
    type T = True;
}

impl<A, N: Succ<N=A>> PeanoEqual for (N, N0) {
    type T = False;
}

impl<B, N: Succ<N=B>> PeanoEqual for (N0, N) {
    type T = False;
}

impl<A, B, SA: Succ<N=A>, SB: Succ<N=B>> PeanoEqual for (SA, SB) where (A, B): PeanoEqual {
    type T = <(A, B) as PeanoEqual>::T;
}

trait PeanoLT {
    type T: Typed<Type=BoolType>;
}

impl PeanoLT for (N0, N0) {
    type T = False;
}

impl<X, SX: Succ<N=X>> PeanoLT for (SX, N0) {
    type T = False;
}

impl<X, SX: Succ<N=X>> PeanoLT for (N0, SX) {
    type T = True;
}

impl<A, B, SA: Succ<N=A>, SB: Succ<N=B>> PeanoLT for (SA, SB) where (A, B): PeanoLT {
    type T = <(A, B) as PeanoLT>::T;
}

trait PeanoAbsDiff {
    type C: Typed<Type=NatType>;
}

impl PeanoAbsDiff for (N0, N0) {
    type C = N0;
}

impl<B, SB: Succ<N=B>> PeanoAbsDiff for (N0, SB) {
    type C = SB;
}

impl<A, SA: Succ<N=A>> PeanoAbsDiff for (SA, N0) {
    type C = SA;
}

impl<A, B, SA: Succ<N=A>, SB: Succ<N=B>> PeanoAbsDiff for (SA, SB) where (A, B): PeanoAbsDiff {
    type C = <(A, B) as PeanoAbsDiff>::C;
}

trait Range: Typed<Type=NatType> {
    type Xs: Typed<Type=ListType<NatType>>;
}

impl Range for N0 {
    type Xs = Nil<NatType>;
}

impl<N, SN: Succ<N=N>> Range for SN where N: Range {
    type Xs = Cons<N, N::Xs>;
}

fn accepts_true(_: True) {}
fn legal_compare(x: <(N1, N1) as PeanoEqual>::T) {
    accepts_true(x)
}

struct Fn1Type<X, R>(PhantomData<X>, PhantomData<R>);

trait Apply<T> {
    type R: Typed<Type=T>;
}

struct Conj1<List: Typed<Type=ListType<T>>, T>(PhantomData<List>);
impl<T, List: Typed<Type=ListType<T>>> Typed for Conj1<List, T> { type Type = Fn1Type<T, ListType<T>>; }

impl<T, List: Typed<Type=ListType<T>>, X: Typed<Type=T>> Apply<ListType<T>> for (Conj1<List, T>, X) {
    type R = Cons<X, List>;
}

trait Map<T> {
    type Ys: Typed<Type=ListType<T>>;
}

impl<T1, T2, F: Typed<Type=Fn1Type<T1, T2>>> Map<T2> for (F, Nil<T1>) {
    type Ys = Nil<T2>;
}

impl<T1, T2, F: Typed<Type=Fn1Type<T1, T2>>, X: Typed<Type=T1>, Xs: Typed<Type=ListType<T1>>> Map<T2> for (F, Cons<X, Xs>)
    where (F, X): Apply<T2>, (F, Xs): Map<T2> {
    type Ys = Cons<<(F, X) as Apply<T2>>::R, <(F, Xs) as Map<T2>>::Ys>;
}

trait MapCat<T> {
    type Zs: Typed<Type=ListType<T>>;
}

impl<T1, T2, F: Typed<Type=Fn1Type<T1, ListType<T2>>>> MapCat<T2> for (F, Nil<T1>) {
    type Zs = Nil<T2>;
}

impl<T1, T2, F: Typed<Type=Fn1Type<T1, ListType<T2>>>, Xs: Typed<Type=ListType<T1>>> MapCat<T2> for (F, Xs)
    where (F, Xs): Map<ListType<T2>>, <(F, Xs) as Map<ListType<T2>>>::Ys: ListConcatAll<T2> {
    type Zs = <<(F, Xs) as Map<ListType<T2>>>::Ys as ListConcatAll<T2>>::L;
}

trait AppendIf<T> {
    type Zs: Typed<Type=ListType<T>>;
}

impl<T, X: Typed<Type=T>, Ys: Typed<Type=ListType<T>>> AppendIf<T> for (True, X, Ys) {
    type Zs = Cons<X, Ys>;
}

impl<T, X: Typed<Type=T>, Ys: Typed<Type=ListType<T>>> AppendIf<T> for (False, X, Ys) {
    type Zs = Ys;
}

trait Filter<T> {
    type Ys: Typed<Type=ListType<T>>;
}

impl<T, F: Typed<Type=Fn1Type<T, BoolType>>> Filter<T> for (F, Nil<T>) {
    type Ys = Nil<T>;
}

impl<T, F: Typed<Type=Fn1Type<T, BoolType>>, X: Typed<Type=T>, Xs: Typed<Type=ListType<T>>> Filter<T> for (F, Cons<X, Xs>)
    where (F, X): Apply<BoolType>, (F, Xs): Filter<T>,
          (<(F, X) as Apply<BoolType>>::R, X, <(F, Xs) as Filter<T>>::Ys): AppendIf<T> {
    type Ys = <(<(F, X) as Apply<BoolType>>::R, X, <(F, Xs) as Filter<T>>::Ys) as AppendIf<T>>::Zs;
}

struct QueenType;

struct Queen<X: Typed<Type=NatType>, Y: Typed<Type=NatType>>(PhantomData<X>, PhantomData<Y>);
impl<X, Y> Typed for Queen<X, Y> { type Type = QueenType; }
struct Queen1<X: Typed<Type=NatType>>(PhantomData<X>);
impl<X> Typed for Queen1<X> { type Type = Fn1Type<NatType, QueenType>; }

impl<X: Typed<Type=NatType>, Y: Typed<Type=NatType>> Apply<QueenType> for (Queen1<X>, Y) {
    type R = Queen<X, Y>;
}

trait QueensInRow {
    type Queens: Typed<Type=ListType<QueenType>>;
}

impl<N: Typed<Type=NatType>, X: Typed<Type=NatType>> QueensInRow for (N, X)
    where N: Range, (Queen1<X>, <N as Range>::Xs): Map<QueenType> {
    type Queens = <(Queen1<X>, <N as Range>::Xs) as Map<QueenType>>::Ys;
}

trait Threatens {
    type T: Typed<Type=BoolType>;
}

impl<Ax: Typed<Type=NatType>, Ay: Typed<Type=NatType>, Bx: Typed<Type=NatType>, By: Typed<Type=NatType>> Threatens for (Queen<Ax, Ay>, Queen<Bx, By>)
    where (Ax, Bx): PeanoEqual + PeanoAbsDiff, (Ay, By): PeanoEqual + PeanoAbsDiff,
          (<(Ax, Bx) as PeanoEqual>::T, <(Ay, By) as PeanoEqual>::T): Or,
          (<(Ax, Bx) as PeanoAbsDiff>::C, <(Ay, By) as PeanoAbsDiff>::C): PeanoEqual,
          (<(<(Ax, Bx) as PeanoEqual>::T, <(Ay, By) as PeanoEqual>::T) as Or>::B,
           <(<(Ax, Bx) as PeanoAbsDiff>::C, <(Ay, By) as PeanoAbsDiff>::C) as PeanoEqual>::T,
          ): Or {
    type T = <(
        <(<(Ax, Bx) as PeanoEqual>::T, <(Ay, By) as PeanoEqual>::T) as Or>::B,
        <(<(Ax, Bx) as PeanoAbsDiff>::C, <(Ay, By) as PeanoAbsDiff>::C) as PeanoEqual>::T,
    ) as Or>::B;
}

struct Threatens1<A: Typed<Type=QueenType>>(PhantomData<A>);
impl<A> Typed for Threatens1<A> { type Type = Fn1Type<QueenType, BoolType>; }

impl<A: Typed<Type=QueenType>, B: Typed<Type=QueenType>> Apply<BoolType> for (Threatens1<A>, B)
    where (A, B): Threatens {
    type R = <(A, B) as Threatens>::T;
}

trait Safe {
    type T: Typed<Type=BoolType>;
}

impl<Config: Typed<Type=ListType<QueenType>>, Queen: Typed<Type=QueenType>> Safe for (Config, Queen)
    where (Threatens1<Queen>, Config): Map<BoolType>,
          <(Threatens1<Queen>, Config) as Map<BoolType>>::Ys: AnyTrue,
          <<(Threatens1<Queen>, Config) as Map<BoolType>>::Ys as AnyTrue>::T: Not {
    type T = <<<(Threatens1<Queen>, Config) as Map<BoolType>>::Ys as AnyTrue>::T as Not>::B;
}

struct Safe1<Config: Typed<Type=ListType<QueenType>>>(PhantomData<Config>);

impl<Config: Typed<Type=ListType<QueenType>>, Queen> Apply<BoolType> for (Safe1<Config>, Queen)
    where (Config, Queen): Safe {
    type R = <(Config, Queen) as Safe>::T;
}

trait AddQueen {
    type Cs: Typed<Type=ListType<QueenType>>;
}

impl<N: Typed<Type=NatType>, X: Typed<Type=NatType>, C: Typed<Type=ListType<QueenType>>> AddQueen for (N, X, C)
    where (N, X): QueensInRow,
          (Safe1<C>, <(N, X) as QueensInRow>::Queens): Filter<QueenType>,
          <(Conj1<C, QueenType>, (Safe1<C>, <(N, X) as QueensInRow>::Queens)) as Filter<QueenType>>::Ys: Map<QueenType> {
    type Cs = <<(Conj1<C, QueenType>, (Safe1<C>, <(N, X) as QueensInRow>::Queens)) as Filter<QueenType>>::Ys as Map<QueenType>>::Ys;
}

struct AddQueen2<N: Typed<Type=NatType>, X: Typed<Type=NatType>>(PhantomData<N>, PhantomData<X>);
impl<N: Typed<Type=NatType>, X: Typed<Type=NatType>> Typed for AddQueen2<N, X> { type Type = Fn1Type<ListType<QueenType>, ListType<QueenType>>; }

impl<N: Typed<Type=NatType>, X: Typed<Type=NatType>, C: Typed<Type=ListType<QueenType>>> Apply<ListType<QueenType>> for (AddQueen2<N, X>, C)
    where (N, X, C): AddQueen {
    type R = <(N, X, C) as AddQueen>::Cs;
}

trait AddQueenToAll {
    type CsPrime: Typed<Type=ListType<ListType<QueenType>>>;
}

impl<N: Typed<Type=NatType>, X: Typed<Type=NatType>, Cs: Typed<Type=ListType<ListType<QueenType>>>> AddQueenToAll for (N, X, Cs)
    where (AddQueen2<N, X>, Cs): MapCat<ListType<QueenType>> {
    type CsPrime = <(AddQueen2<N, X>, Cs) as MapCat<ListType<QueenType>>>::Zs;
}

trait AddQueensIf {
    type CsPrime: Typed<Type=ListType<ListType<QueenType>>>;
}

impl<N: Typed<Type=NatType>, Cs: Typed<Type=ListType<ListType<QueenType>>>> AddQueensIf for (N, N0, Cs) {
    type CsPrime = Cs;
}

impl<N: Typed<Type=NatType>, X: Typed<Type=NatType>, Cs: Typed<Type=ListType<ListType<QueenType>>>, SX: Succ<N=X>> AddQueensIf for (N, SX, Cs)
    where (N, SX, Cs): AddQueenToAll,
          (N, X, <(N, SX, Cs) as AddQueenToAll>::CsPrime): AddQueens {
    type CsPrime = <(N, X, <(N, SX, Cs) as AddQueenToAll>::CsPrime) as AddQueens>::CsPrime;
}

trait AddQueens {
    type CsPrime: Typed<Type=ListType<ListType<QueenType>>>;
}

impl<N: Typed<Type=NatType>, X: Typed<Type=NatType>, Cs: Typed<Type=ListType<ListType<QueenType>>>> AddQueens for (N, X, Cs)
    where (N, X, Cs): AddQueensIf {
    type CsPrime = <(N, X, Cs) as AddQueensIf>::CsPrime;
}

trait Solution {
    type C: Typed<Type=ListType<QueenType>>;
}

impl<N: Typed<Type=NatType>> Solution for N
    where (N, N, Cons<Nil<QueenType>, Nil<ListType<QueenType>>>): AddQueens, <(N, N, Cons<Nil<QueenType>, Nil<ListType<QueenType>>>) as AddQueens>::CsPrime: First<ListType<QueenType>> {
    type C = <<(N, N, Cons<Nil<QueenType>, Nil<ListType<QueenType>>>) as AddQueens>::CsPrime as First<ListType<QueenType>>>::X;
}

fn solve() -> <N6 as Solution>::C {
    todo!()
}

fn main() {
    // we should be able to read the type off the error here
    let _: () = solve();
}