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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
// Designed for `use reify::*`
pub use crate::{ast::Ast, name::*, runtime::eval::Value};

use crate::runtime::eval;

use num::bigint::BigInt;
use std::rc::Rc;

/// This is for parts of this compiler that need to be represented as object-level values.
/// Almost all of it, turns out!
///
/// Since this language is extensible, we need to connect the Rust code in the compiler
///  with the Unseemly code that actually gets evaluated.
/// This is where the magic happens.
///
/// Suppose that `T` is a two-argument generic type.
/// Generally, we plan on executing code in an environment in which
///  `T::<Irr,Irr>::name()` is bound to `T::<Irr,Irr>::ty()`.
///   (The type arguments do not affect `name` and `ty`; `()` is convention.)
/// Then, we can use `T::<SomeActualArg, OtherActualArg>::ty_invocation()` in that environment.
///
/// This is also where ICPs can happen, so make sure that ::ty() is consistent with ::reify().

pub trait Reifiable {
    /// The Unseemly type that corresponds to to the `Reifiable` type.
    /// This leaves abstract the type parameters of `Self`; invoke like `Self::<Irr,Irr>::ty()`.
    /// e.g. `∀ A. Pair<A int>`
    /// TODO: rename to `generic_ty`
    fn ty() -> Ast {
        // By default, this is an opaque primitive.
        crate::core_type_forms::get__primitive_type(Self::ty_name())
    }

    /// A name for that type, so that recursive types are okay.
    /// Ignore the type parameters of `Self`; invoke like `Self::<Irr,Irr>::ty_name()`.
    /// e.g. `WithInteger`
    fn ty_name() -> Name;

    /// How to refer to this type, given an environment in which
    ///  `ty_name()` is defined to be `ty()`.
    /// Parameters will be concrete.
    /// e.g. `WithInteger<Float>`
    /// (Types using this type will use this, rather than `ty`)
    /// Don't override this.
    fn ty_invocation() -> Ast {
        let name_ref = ast!((vr Self::ty_name()));
        match Self::concrete_arguments() {
            None => name_ref,
            Some(args) => ast!({ "Type" "type_apply" :
                "type_rator" => (, name_ref),
                "arg" => (,seq args)
            }),
        }
    }

    // Override this to set the type arguments for invocation.
    fn concrete_arguments() -> Option<Vec<Ast>> { None }

    /// The Unseemly value that corresponds to a value.
    fn reify(&self) -> Value;

    /// Get a value from an Unseemly value
    fn reflect(_: &Value) -> Self;
}

// Core values

macro_rules! basic_reifiability {
    ( $underlying_type:ty, $ty_name:tt, $value_name:ident ) => {
        impl Reifiable for $underlying_type {
            fn ty_name() -> Name { n($ty_name) }

            // TODO: can we remove these clones? are they even bad?
            // They seem redundant in the `Name` case, at least
            fn reify(&self) -> Value { Value::$value_name(self.clone()) }

            fn reflect(v: &Value) -> Self {
                extract!((v) Value::$value_name = (ref i) => i.clone())
            }
        }
    }
}

/// Irr: the irrelevant type (like `!`). Satisfies a bunch of traits; can't be created.
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub enum Irr {} // No values can be created.

impl std::fmt::Display for Irr {
    fn fmt(&self, _: &mut std::fmt::Formatter) -> std::fmt::Result { icp!() }
}

impl Default for Irr {
    fn default() -> Irr { icp!() }
}

impl Reifiable for Irr {
    fn ty_name() -> Name { icp!() }
    fn reify(&self) -> Value { icp!() }
    fn reflect(_: &Value) -> Self { icp!() }
}

impl crate::walk_mode::WalkElt for Irr {
    fn from_ast(_: &Ast) -> Self { icp!() }
    fn to_ast(&self) -> Ast { icp!() }
}

impl crate::walk_mode::WalkMode for Irr {
    fn name() -> &'static str { icp!() }
    type Elt = Irr;
    type Negated = NegIrr;
    type AsPositive = Irr;
    type AsNegative = NegIrr;
    type Err = Irr;
    type D = crate::walk_mode::Positive<Irr>;
    type ExtraInfo = Irr;

    fn get_walk_rule(_: &crate::form::Form) -> crate::ast_walk::WalkRule<Irr> { icp!() }
    fn automatically_extend_env() -> bool { icp!() }

    fn underspecified(_: Name) -> Irr { icp!() }
}

#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub enum NegIrr {}

impl Reifiable for NegIrr {
    fn ty_name() -> Name { icp!() }
    fn reify(&self) -> Value { icp!() }
    fn reflect(_: &Value) -> Self { icp!() }
}

impl crate::walk_mode::WalkMode for NegIrr {
    fn name() -> &'static str { icp!() }
    type Elt = Irr;
    type Negated = Irr;
    type AsPositive = Irr;
    type AsNegative = NegIrr;
    type Err = Irr;
    type D = crate::walk_mode::Negative<NegIrr>;
    type ExtraInfo = Irr;

    fn get_walk_rule(_: &crate::form::Form) -> crate::ast_walk::WalkRule<NegIrr> { icp!() }
    fn automatically_extend_env() -> bool { icp!() }

    fn underspecified(_: Name) -> Irr { icp!() }
}

impl crate::walk_mode::NegativeWalkMode for NegIrr {
    fn needs_pre_match() -> bool { panic!() }
}

basic_reifiability!(BigInt, "Int", Int);

impl Reifiable for bool {
    fn ty_name() -> Name { n("Bool") }

    fn reify(&self) -> Value { eval::Value::Enum(n(if *self { "True" } else { "False" }), vec![]) }

    fn reflect(v: &Value) -> Self {
        extract!((v) Value::Enum = (ref name, _) => name == &n("True"))
    }
}

// note: operations for these shouldn't have BigInt semantics!
impl Reifiable for usize {
    fn ty_name() -> Name { n("Rust_usize") }

    fn reify(&self) -> Value { Value::Int(BigInt::from(*self)) }

    fn reflect(v: &Value) -> Self {
        use num::ToPrimitive;
        extract!((v) Value::Int = (ref i) => i.to_usize().unwrap())
    }
}
impl Reifiable for i32 {
    fn ty_name() -> Name { n("Rust_i32") }

    fn reify(&self) -> Value { Value::Int(BigInt::from(*self)) }

    fn reflect(v: &Value) -> Self {
        use num::ToPrimitive;
        extract!((v) Value::Int = (ref i) => i.to_i32().unwrap())
    }
}
impl Reifiable for u8 {
    fn ty_name() -> Name { n("Rust_u8") }

    fn reify(&self) -> Value { Value::Int(BigInt::from(*self)) }

    fn reflect(v: &Value) -> Self {
        use num::ToPrimitive;
        extract!((v) Value::Int = (ref i) => i.to_u8().unwrap())
    }
}
impl Reifiable for () {
    fn ty_name() -> Name { n("Unit") }

    fn reify(&self) -> Value { Value::Int(BigInt::from(0)) }

    fn reflect(_: &Value) -> Self {}
}

impl<T0: Reifiable, T1: Reifiable> Reifiable for (T0, T1) {
    fn ty_name() -> Name { n("Tuple2") }

    fn concrete_arguments() -> Option<Vec<Ast>> {
        Some(vec![T0::ty_invocation(), T1::ty_invocation()])
    }

    fn reify(&self) -> Value {
        Value::Sequence(vec![Rc::new(self.0.reify()), Rc::new(self.1.reify())])
    }

    fn reflect(v: &Value) -> Self {
        extract!((v) Value::Sequence = (ref s) => (T0::reflect(&*s[0]), T1::reflect(&*s[1])))
    }
}

impl Reifiable for String {
    fn ty_name() -> Name { n("Rust_str") }

    fn reify(&self) -> Value { val!(ast (at self)) }

    fn reflect(v: &Value) -> Self {
        match v {
            eval::AbstractSyntax(at_ast) => at_ast.to_name().orig_sp(),
            _ => icp!(),
        }
    }
}

// This is right, right?
impl Reifiable for Value {
    fn ty_name() -> Name { n("any") }

    fn reify(&self) -> Value { self.clone() }

    fn reflect(v: &Value) -> Self { v.clone() }
}

// TODO: when returning traits works, just make functions `Reifiable`
// TOUNDERSTAND: 'x also allows things to be owned instead?!?
pub fn reify_1ary_function<A: Reifiable + 'static, R: Reifiable + 'static>(
    f: Rc<Box<(dyn Fn(A) -> R)>>,
) -> Value {
    Value::BuiltInFunction(eval::BIF(Rc::new(move |args: Vec<Value>| {
        ((*f)(A::reflect(&args[0]))).reify()
    })))
}

pub fn reflect_1ary_function<A: Reifiable + 'static, R: Reifiable + 'static>(
    f_v: Value,
) -> Rc<Box<(dyn Fn(A) -> R)>> {
    Rc::new(Box::new(move |a: A| {
        extract!((&f_v)
        Value::BuiltInFunction = (ref bif) => R::reflect(&(*bif.0)(vec![a.reify()]));
        Value::Function = (ref closure) => {
            R::reflect(&eval::eval(&closure.body,
                closure.env.clone().set(closure.params[0], a.reify())).unwrap())
        })
    }))
}

// I bet there's more of a need for reification than reflection for functions....
pub fn reify_2ary_function<
    A: Reifiable + 'static,
    B: Reifiable + 'static,
    R: Reifiable + 'static,
>(
    f: Rc<Box<(dyn Fn(A, B) -> R)>>,
) -> Value {
    Value::BuiltInFunction(eval::BIF(Rc::new(move |args: Vec<Value>| {
        ((*f)(A::reflect(&args[0]), B::reflect(&args[1]))).reify()
    })))
}

pub fn reflect_2ary_function<
    A: Reifiable + 'static,
    B: Reifiable + 'static,
    R: Reifiable + 'static,
>(
    f_v: Value,
) -> Rc<Box<(dyn Fn(A, B) -> R)>> {
    Rc::new(Box::new(move |a: A, b: B| {
        extract!((&f_v)
        Value::BuiltInFunction = (ref bif) =>
            R::reflect(&(*bif.0)(vec![a.reify(), b.reify()]));
        Value::Function = (ref closure) => {
            R::reflect(&eval::eval(&closure.body,
                closure.env.clone().set(closure.params[0], a.reify())
                                   .set(closure.params[1], b.reify())).unwrap())
        })
    }))
}

pub fn ty_of_1ary_function<A: Reifiable + 'static, R: Reifiable + 'static>() -> Ast {
    ast!("TODO: generate type")
}

macro_rules! reify_types {
    ( $($t:ty),* ) => {{
        let mut res = Assoc::new();
        $(
           res = res.set(<$t as Reifiable>::ty_name(), <$t as Reifiable>::ty());
        )*
        res
    }}
}

macro_rules! fake_reifiability {
    ( $underlying_type:ty ) => {
        impl Reifiable for $underlying_type {
            fn ty_name() -> Name { n(stringify!($underlying_type)) }
            fn reify(&self) -> Value { panic!() }
            fn reflect(_: &Value) -> Self { panic!() }
        }
    };
}

// impl<A: Reifiable, R: Reifiable> Reifiable for Box<Fn(A) -> R> {
//     fn ty() -> Ast { panic!("") }
//
//     fn reify(&self) -> Value { panic!("") }
//
//     fn reflect(v: &Value) -> Self { panic!("") }
// }

// We can't add derive() to existing types, but we can `impl` these ourselves directly

// This feels a little awkward, just dropping the `Rc`ness on the floor.
// But I think `Value` has enouch `Rc` inside that nothing can go wrong... right?
impl<T: Reifiable> Reifiable for Rc<T> {
    fn ty() -> Ast { T::ty() }

    fn ty_name() -> Name { T::ty_name() }

    fn concrete_arguments() -> Option<Vec<Ast>> { T::concrete_arguments() }

    fn reify(&self) -> Value { (**self).reify() }

    fn reflect(v: &Value) -> Self { Rc::new(T::reflect(v)) }
}

/// Takes the Unseemly type `T` to `Sequence<T>`
pub fn sequence_type__of(ty: &Ast) -> Ast {
    ast!({ "Type" "type_apply" :
        "type_rator" => (, crate::core_type_forms::get__primitive_type(n("Sequence"))),
        "arg" => [(, ty.clone()) ]})
}

/// Takes the Unseemly type `Sequence<T>` to `T`
pub fn un__sequence_type(ty: &Ast, loc: &Ast) -> Result<Ast, crate::ty::TypeError> {
    // This is a hack; `Sequence` is not a nonterminal!
    crate::core_type_forms::less_quoted_ty(ty, Some(n("Sequence")), loc)
}

impl<T: Reifiable> Reifiable for Vec<T> {
    fn ty_name() -> Name { n("Sequence") }

    fn concrete_arguments() -> Option<Vec<Ast>> { Some(vec![T::ty_invocation()]) }

    fn reify(&self) -> Value {
        Value::Sequence(self.iter().map(|elt| Rc::new(elt.reify())).collect())
    }

    fn reflect(v: &Value) -> Self {
        extract!((v) Value::Sequence = (ref s) =>
            s.iter().map(|elt| T::reflect(&elt)).collect()
        )
    }
}

impl<T: Reifiable> Reifiable for std::boxed::Box<T> {
    fn ty() -> Ast { T::ty() }

    fn ty_name() -> Name { T::ty_name() }

    fn concrete_arguments() -> Option<Vec<Ast>> { T::concrete_arguments() }

    fn reify(&self) -> Value { (**self).reify() }

    fn reflect(v: &Value) -> Self { std::boxed::Box::new(T::reflect(v)) }
}

// The roundtrip will de-alias the cell, sadly.
impl<T: Reifiable> Reifiable for std::cell::RefCell<T> {
    fn ty_name() -> Name { n("Rust_RefCell") }

    fn concrete_arguments() -> Option<Vec<Ast>> { Some(vec![T::ty_invocation()]) }

    fn reify(&self) -> Value { self.borrow().reify() }

    fn reflect(v: &Value) -> Self { std::cell::RefCell::<T>::new(T::reflect(v)) }
}

impl<T: Reifiable> Reifiable for std::marker::PhantomData<T> {
    fn ty_name() -> Name { n("PhantomData") }

    fn concrete_arguments() -> Option<Vec<Ast>> { Some(vec![T::ty_invocation()]) }

    fn reify(&self) -> Value { Value::Int(BigInt::from(0)) }

    fn reflect(_: &Value) -> Self { std::marker::PhantomData }
}

// Hey, I know how to generate the implementation for this...
Reifiable! {
    () pub enum Option<T> {
        None,
        Some(T)
    }
}
Reifiable! {
    () pub enum Result<T, E> {
        Ok(T),
        Err(E),
    }
}

// for testing

custom_derive! {
    #[derive(Debug, PartialEq, Eq, Reifiable, Clone)]
    struct BasicStruct {
        pub a: BigInt, // TODO: change to String to test heterogeneity
        b: BigInt
    }
}

custom_derive! {
    #[derive(Debug, PartialEq, Eq, Reifiable, Clone)]
    struct NestedStruct {
        x: BasicStruct
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
struct OldName<'t> {
    actual: Name,
    pd: std::marker::PhantomData<&'t u32>,
}
fn new_oldname<'t>(nm: Name) -> OldName<'t> { OldName { actual: nm, pd: std::marker::PhantomData } }

impl<'t> Reifiable for OldName<'t> {
    fn ty_name() -> Name { n("OldName") }

    fn reify(&self) -> Value { self.actual.reify() }

    fn reflect(v: &Value) -> Self { new_oldname(Name::reflect(v)) }
}

custom_derive! {
    #[derive(Debug, PartialEq, Eq, Reifiable(lifetime), Clone)]
    enum BasicLifetimeEnum<'t> {
        Only(OldName<'t>)
    }
}

custom_derive! {
    #[derive(Debug, PartialEq, Eq, Reifiable, Clone)]
    enum BasicEnum {
        Jefferson(BigInt, BigInt), // TODO: change the first one to String
        Burr(BigInt)
    }
}

custom_derive! {
    #[derive(Debug, PartialEq, Eq, Reifiable(lifetime), Clone)]
    struct ParameterizedLifetimeStruct<'t, T, S> {
        pub a: T, b: S, c: OldName<'t>
    }
}

// TODO: just write a macro that does a really faky custom_derive by calling `Reifiable!`
// around something and then putting down its definition.

#[test]
fn basic_reification() {
    assert_eq!(BigInt::from(1800).reify(), val!(i 1800));
}

#[test]
fn basic_reflection() {
    assert_eq!(BigInt::reflect(&val!(i 1800)), BigInt::from(1800));
}

#[test]
fn basic_r_and_r_roundtrip() {
    assert_eq!(BigInt::from(90), BigInt::reflect(&BigInt::from(90).reify()));

    let bsv = BasicStruct { a: BigInt::from(4), b: BigInt::from(5) };

    assert_eq!(bsv, BasicStruct::reflect(&bsv.reify()));

    let nsv = NestedStruct { x: bsv };

    assert_eq!(nsv, NestedStruct::reflect(&nsv.reify()));

    let bev0 = BasicEnum::Jefferson(BigInt::from(17), BigInt::from(1781));
    let bev1 = BasicEnum::Burr(BigInt::from(1781));

    assert_eq!(bev0, BasicEnum::reflect(&bev0.reify()));
    assert_eq!(bev1, BasicEnum::reflect(&bev1.reify()));

    // assert_eq!(None, Option::reflect(&None.reify()));
    assert_eq!(Some(BigInt::from(5)), Option::reflect(&Some(BigInt::from(5)).reify()));
    assert_eq!(Some(bev1.clone()), Option::reflect(&Some(bev1.clone()).reify()));

    assert_eq!(Rc::new(bev0.clone()), Rc::reflect(&Rc::new(bev0.clone()).reify()));

    assert_eq!(
        std::boxed::Box::new(bev0.clone()),
        std::boxed::Box::reflect(&std::boxed::Box::new(bev0.clone()).reify())
    );

    let bleo = BasicLifetimeEnum::Only(new_oldname(n("AlexanderHamilton")));

    assert_eq!(bleo, BasicLifetimeEnum::reflect(&bleo.reify()));

    let pls = ParameterizedLifetimeStruct::<BigInt, bool> {
        a: BigInt::from(10),
        b: false,
        c: new_oldname(n("DuelCommandments")),
    };

    assert_eq!(pls, ParameterizedLifetimeStruct::<BigInt, bool>::reflect(&pls.reify()));
}

#[test]
fn function_r_and_r_roundtrip() {
    let f = |a: BigInt| a + BigInt::from(1);

    let f2 = reflect_1ary_function::<BigInt, BigInt>(reify_1ary_function(Rc::new(Box::new(f))));

    assert_eq!((*f2)(BigInt::from(1776)), BigInt::from(1777));
}

struct T {}
fake_reifiability!(T);
struct S {}
fake_reifiability!(S);

#[test]
fn reified_types() {
    //"ParameterizedLifetimeStruct<Option<Rust_usize> integer>"
    assert_eq!(
        ParameterizedLifetimeStruct::<'static, Option<usize>, BigInt>::ty_invocation(),
        ast!({"Type" "type_apply" :
            "type_rator" => (vr "ParameterizedLifetimeStruct"),
            "arg" => [
                {"Type" "type_apply" :
                    "type_rator" => (vr "Option"),
                    "arg" => [ (vr "Rust_usize") ]
                },
                (vr "Int")]
        })
    );

    assert_eq!(
        ParameterizedLifetimeStruct::<'static, T, S>::ty(),
        ast!({"Type" "forall_type" :
                "param" => ["T", "S"],
                "body" => (import [* [forall "param"]] {"Type" "mu_type" :
                    "param" => [(import [prot "param"] (vr "ParameterizedLifetimeStruct"))],
                    "body" => (import [* [prot "param"]] {"Type" "struct" :
                        // TODO: why did the order of fields get reversed?
                        "component_name" => [@"c" "c", "b", "a"],
                        "component" => [@"c" (vr "OldName"), (vr "S"), (vr "T")]

                })
            })
        })
    )
}