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
#![allow(unknown_lints)]
use std::marker::PhantomData;
use std::{fmt, hash};

pub trait TyFun<Param: ?Sized>: Sized {
  type Result: ?Sized;
}

impl<'x, Param: ?Sized, F: TyFun<Param>> TyFun<Param> for &'x F {
  type Result = F::Result;
}

impl<'x, Param: ?Sized, F: TyFun<Param>> TyFun<Param> for &'x mut F {
  type Result = F::Result;
}

impl<Param: ?Sized, F: TyFun<Param>> TyFun<Param> for Box<F> {
  type Result = F::Result;
}

pub trait LiFun<'param>: Sized {
  type Result: ?Sized;
}

impl<'x, 'param, F: LiFun<'param>> LiFun<'param> for &'x F {
  type Result = F::Result;
}

impl<'x, 'param, F: LiFun<'param>> LiFun<'param> for &'x mut F {
  type Result = F::Result;
}

impl<'param, F: LiFun<'param>> LiFun<'param> for Box<F> {
  type Result = F::Result;
}

pub struct LiToTy<'param> {
  phantom_fn: PhantomData<fn(&'param ()) -> &'param ()>,
}

impl<'param> fmt::Debug for LiToTy<'param> {
  fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result { fmtr.debug_struct("LiToTy").field("phantom_fn", &self.phantom_fn).finish() }
}

#[allow(expl_impl_clone_on_copy)]
impl<'param> Clone for LiToTy<'param> {
  fn clone(&self) -> Self { LiToTy::default() }
}

impl<'param> Copy for LiToTy<'param> {}

impl<'param> Default for LiToTy<'param> {
  fn default() -> Self { LiToTy { phantom_fn: PhantomData } }
}

impl<'param> hash::Hash for LiToTy<'param> {
  fn hash<H>(&self, state: &mut H)
    where H: hash::Hasher {
    self.phantom_fn.hash(state);
  }
}

#[derive(Debug,Clone,Copy,Default,Hash)]
pub struct LiFunToTyFun<F> {
  inner: F,
}

impl<'param, F: LiFun<'param>> TyFun<LiToTy<'param>> for LiFunToTyFun<F> {
  type Result = F::Result;
}

#[derive(Debug,Clone,Copy,Default,Hash)]
pub struct TyFunToLiFun<F> {
  inner: F,
}

impl<'param, F: TyFun<LiToTy<'param>>> LiFun<'param> for TyFunToLiFun<F> {
  type Result = F::Result;
}

/// Type-level function composition (traditional order).
#[derive(Debug,Clone,Copy,Default,Hash)]
pub struct Compose<TFa, TFb> {
  tf_a: TFa,
  tf_b: TFb,
}

/// Type-level function composition (intuitive order).
pub type AndThen<TFa, TFb> = Compose<TFb, TFa>;

pub fn ty_compose<TFa, TFb>(tf_a: TFa, tf_b: TFb) -> Compose<TFa, TFb> { Compose { tf_a: tf_a, tf_b: tf_b } }

pub fn ty_and_then<TFa, TFb>(tf_a: TFa, tf_b: TFb) -> AndThen<TFa, TFb> { Compose { tf_a: tf_b, tf_b: tf_a } }

impl<Param: ?Sized, TFa, TFb> TyFun<Param> for Compose<TFa, TFb>
  where TFb: TyFun<Param>, TFa: TyFun<TFb::Result> {
  type Result = TFa::Result;
}

impl<'param, TFa, TFb> LiFun<'param> for Compose<TFa, TFb>
  where TFa: LiFun<'param>, TFb: TyFun<TFa::Result> {
  type Result = TFb::Result;
}

/// Type-level I combinator.
#[derive(Debug,Clone,Copy,Default,Hash)]
pub struct Id;

pub fn ty_id() -> Id { Id }

impl<Param: ?Sized> TyFun<Param> for Id {
  type Result = Param;
}

/// Type-level K combinator.
pub struct Const<X: ?Sized> {
  phantom_fn: PhantomData<fn(X) -> X>,
}

impl<X: ?Sized> fmt::Debug for Const<X> {
  fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result { fmtr.debug_struct("Const").field("phantom_fn", &self.phantom_fn).finish() }
}

#[allow(expl_impl_clone_on_copy)]
impl<X: ?Sized> Clone for Const<X> {
  fn clone(&self) -> Self { Const::default() }
}

impl<X: ?Sized> Copy for Const<X> {}

impl<X: ?Sized> Default for Const<X> {
  fn default() -> Self { Const { phantom_fn: PhantomData } }
}

impl<X: ?Sized> hash::Hash for Const<X> {
  fn hash<H>(&self, state: &mut H)
    where H: hash::Hasher {
    self.phantom_fn.hash(state);
  }
}

pub fn ty_const<X: ?Sized>() -> Const<X> { Const::default() }

impl<Param: ?Sized, X: ?Sized> TyFun<Param> for Const<X> {
  type Result = X;
}

impl<'param, X: ?Sized> LiFun<'param> for Const<X> {
  type Result = X;
}

#[derive(Debug,Clone,Copy,Default,Hash)]
pub struct ConstTyFun;

impl<X: ?Sized> TyFun<X> for ConstTyFun {
  type Result = Const<X>;
}

/// Type-level S combinator.
#[derive(Debug,Clone,Copy,Default,Hash)]
pub struct Ap<F, G> {
  inner_f: F,
  inner_g: G,
}

pub fn ty_ap<F, G>(f: F, g: G) -> Ap<F, G> { Ap { inner_f: f, inner_g: g } }

impl<Param: ?Sized, F, G> TyFun<Param> for Ap<F, G>
  where F: TyFun<Param>, G: TyFun<Param>, F::Result: TyFun<G::Result> {
  type Result = <F::Result as TyFun<G::Result>>::Result;
}

impl<'param, F, G> LiFun<'param> for Ap<F, G>
  where F: LiFun<'param>, G: LiFun<'param>, F::Result: TyFun<G::Result> {
  type Result = <F::Result as TyFun<G::Result>>::Result;
}

#[derive(Debug,Clone,Copy,Default,Hash)]
pub struct ApTyFun0;

impl<F> TyFun<F> for ApTyFun0 {
  type Result = ApTyFun1<F>;
}

#[derive(Debug,Clone,Copy,Default,Hash)]
pub struct ApTyFun1<F> {
  inner: F,
}

impl<F, G> TyFun<G> for ApTyFun1<F> {
  type Result = Ap<F, G>;
}

#[derive(Debug,Clone,Copy,Default,Hash)]
pub struct Flip<F> {
  inner: F,
}

impl<ParamB: ?Sized, F> TyFun<ParamB> for Flip<F> {
  type Result = Flipped<F, ParamB>;
}

pub struct Flipped<F, ParamB: ?Sized> {
  inner: F,
  phantom_fn: PhantomData<fn(ParamB) -> ParamB>,
}

impl<F: fmt::Debug, ParamB: ?Sized> fmt::Debug for Flipped<F, ParamB> {
  fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
    fmtr.debug_struct("Flipped").field("inner", &self.inner).field("phantom_fn", &self.phantom_fn).finish()
  }
}

#[allow(expl_impl_clone_on_copy)]
impl<F: Clone, ParamB: ?Sized> Clone for Flipped<F, ParamB> {
  #[allow(clone_on_copy)]
  fn clone(&self) -> Self {
    Flipped {
      inner: self.inner.clone(),
      phantom_fn: PhantomData,
    }
  }

  fn clone_from(&mut self, source: &Self) {
    self.inner.clone_from(&source.inner);
    self.phantom_fn.clone_from(&source.phantom_fn);
  }
}

impl<F: Copy, ParamB: ?Sized> Copy for Flipped<F, ParamB> {}

impl<F: Default, ParamB: ?Sized> Default for Flipped<F, ParamB> {
  fn default() -> Self {
    Flipped {
      inner: F::default(),
      phantom_fn: PhantomData,
    }
  }
}

impl<F: hash::Hash, ParamB: ?Sized> hash::Hash for Flipped<F, ParamB> {
  fn hash<H>(&self, state: &mut H)
    where H: hash::Hasher {
    self.inner.hash(state);
    self.phantom_fn.hash(state);
  }
}

impl<ParamA: ?Sized, ParamB: ?Sized, F: TyFun<ParamA>> TyFun<ParamA> for Flipped<F, ParamB>
  where F::Result: TyFun<ParamB> {
  type Result = <F::Result as TyFun<ParamB>>::Result;
}

pub fn ty_flip<F>(f: F) -> Flip<F> { Flip { inner: f } }

#[derive(Debug,Clone,Copy,Default,Hash)]
pub struct FlipTyFun;

impl<F> TyFun<F> for FlipTyFun {
  type Result = Flip<F>;
}

pub trait Pi<TF> {
  fn call<Param>(self, x: Param) -> TF::Result where TF: TyFun<Param>, TF::Result: Sized;
}

pub trait Sigma<TF>
  where TF: TyFun<Self::Param>, TF::Result: Sized {
  type Param;

  fn fst(self) -> Self::Param;

  fn snd(self) -> TF::Result;
}

pub trait Forall<TF> {
  fn instance<Param: ?Sized>(self) -> TF::Result where TF: TyFun<Param>, TF::Result: Sized;
}

pub trait Exists<TF>
  where TF: TyFun<Self::Param>, TF::Result: Sized {
  type Param: ?Sized;

  fn value(self) -> TF::Result;
}

impl<TF, X: Forall<TF>> Pi<TF> for X {
  fn call<Param>(self, _: Param) -> TF::Result
    where TF: TyFun<Param>, TF::Result: Sized {
    self.instance()
  }
}

impl<TF, X: Sigma<TF>> Exists<TF> for X
  where TF: TyFun<X::Param>, TF::Result: Sized {
  type Param = X::Param;

  fn value(self) -> TF::Result { self.snd() }
}

mod aux {
  pub trait TyListAux: Sized + ::std::fmt::Debug + Clone + Copy + Default + ::std::hash::Hash {}
}

pub trait TyList: aux::TyListAux {}

impl<L: aux::TyListAux> TyList for L {}

#[derive(Debug,Clone,Copy,Default,Hash)]
pub struct Nil;

impl aux::TyListAux for Nil {}

pub struct Cons<A: ?Sized, As: TyList> {
  phantom_fn: PhantomData<fn(A) -> A>,
  rest: As,
}

impl<A: ?Sized, As: TyList> fmt::Debug for Cons<A, As> {
  fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
    fmtr.debug_struct("Cons").field("phantom_fn", &self.phantom_fn).field("rest", &self.rest).finish()
  }
}

#[allow(expl_impl_clone_on_copy)]
impl<A: ?Sized, As: TyList> Clone for Cons<A, As> {
  #[allow(clone_on_copy)]
  fn clone(&self) -> Self {
    Cons {
      phantom_fn: PhantomData,
      rest: self.rest.clone(),
    }
  }

  fn clone_from(&mut self, source: &Self) {
    self.phantom_fn.clone_from(&source.phantom_fn);
    self.rest.clone_from(&source.rest);
  }
}

impl<A: ?Sized, As: TyList> Copy for Cons<A, As> {}

impl<A: ?Sized, As: TyList> Default for Cons<A, As> {
  fn default() -> Self {
    Cons {
      phantom_fn: PhantomData,
      rest: As::default(),
    }
  }
}

impl<A: ?Sized, As: TyList> hash::Hash for Cons<A, As> {
  fn hash<H>(&self, state: &mut H)
    where H: hash::Hasher {
    self.phantom_fn.hash(state);
    self.rest.hash(state);
  }
}

impl<A: ?Sized, As: TyList> aux::TyListAux for Cons<A, As> {}

// waiting on rust-lang/rust#34303 to allow lifetimes as well as types
#[macro_export]
macro_rules! ty_list {
  () => { $crate::lift::Nil };
  ($A:ty, $As:tt) => { $crate::lift::Cons<$A, ty_list!($As)> };
}

pub trait TysFun<Params: TyList> {
  type Result: ?Sized;
}

impl<R: ?Sized> TysFun<Nil> for R {
  type Result = R;
}

impl<Param: ?Sized, Params: TyList, F: TyFun<Param>> TysFun<Cons<Param, Params>> for F
  where F::Result: TysFun<Params> {
  type Result = <F::Result as TysFun<Params>>::Result;
}

#[derive(Debug,Clone,Copy,Default,Hash)]
pub struct Uncurry<F> {
  inner: F,
}

impl<Params: TyList, F: TysFun<Params>> TyFun<Params> for Uncurry<F> {
  type Result = F::Result;
}

pub type TyPair<A: ?Sized, B: ?Sized> = Cons<A, Cons<B, Nil>>;

pub type TyTriple<A: ?Sized, B: ?Sized, C: ?Sized> = Cons<A, Cons<B, Cons<C, Nil>>>;

#[cfg(test)]
mod test {
  use super::*;
  #[test]
  fn flip_test() {
    struct TestFun0;
    impl TyFun<str> for TestFun0 {
      type Result = TestFun1;
    }
    struct TestFun1;
    impl TyFun<i32> for TestFun1 {
      type Result = bool;
    }
    let x: <TestFun0 as TysFun<TyPair<str, i32>>>::Result = true;
    let y: <Flip<TestFun0> as TysFun<TyPair<i32, str>>>::Result = false;
    assert!(x && !y)
  }
}