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
//! # alga-derive
//!
//! Custom derive for `alga` traits.
//!
//! Supported traits:
//!
//! - `AbstractQuasigroup`
//! - `AbstractMonoid`
//! - `AbstractSemigroup`
//! - `AbstractGroup`
//! - `AbstractGroupAbelian`
//! - `AbstractRing`
//! - `AbstractRingCommutative`
//! - `AbstractField`
//!
//! ## Examples
//!
//! ~~~.ignore
//! extern crate alga;
//! #[macro_use]
//! extern crate alga_derive;
//!
//! use alga::general::Additive;
//!
//! #[derive(Alga)]
//! #[alga_traits(Group(Additive))]
//! struct Struct;
//! ~~~
//! This derive implements `AbstractGroup` marker trait with `Additive` operator and all
//! marker traits required by the algebraic groupness property
//! (`AbstractMonoid`, `AbstractSemigroup`, `AbstractLoop` and `AbstractQuasigroup`) for the target of the derive.
//!
//! Traits required by these marker traits (`Identity`, `PartialEq`, `TwoSidedInverse` and `AbstractMagma`) should be implemented manually.
//!
//! If `#[alga_quickcheck]` attribute is added for the target of the derive,
//! then `quickcheck` tests will be generated.
//! These tests will check that the algebraic properties of the derived trait are true for the type.
//! This attribute requires `quickcheck`s `Arbitrary` trait to be implemented for the target of the derive.
//!
//! ~~~.ignore
//! extern crate alga;
//! #[macro_use]
//! extern crate alga_derive;
//!
//! use alga::general::{Additive, AbstractGroup};
//!
//! #[derive(Alga)]
//! #[alga_traits(Group(Additive), Where = "T: AbstractGroup")]
//! #[alga_quickcheck(check(i32), check(i64))]
//! struct Struct<T>;
//! ~~~
//! When there is generic parameters on the target of the derive,
//! then all the concrete type parameters that the tests are generated for can be specified in
//! `alga_quickcheck` attribute by listing them in `check`s.
//!
//! If bounds are required for the `alga` traits to be implemented,
//! they can be listed by `Where = "A: Bound1. B: Bound2"`.

#![recursion_limit = "1024"]
extern crate edit_distance as ed;
extern crate proc_macro;
extern crate proc_macro2;
#[macro_use]
extern crate quote;
extern crate syn;

use proc_macro::TokenStream;
use proc_macro2::Span;
use syn::{Generics, Ident, Path};

use std::iter::once;

fn get_op_arity(tra1t: &str) -> usize {
    match tra1t {
        "Quasigroup" | "Monoid" | "Semigroup" | "Loop" | "Group" | "GroupAbelian" => 1,
        "Ring" | "RingCommutative" | "Field" => 2,
        _ => panic!(
            "Invalid Alga trait provided. Did you mean `{}`?",
            get_closest_trait(tra1t)
        ),
    }
}

fn get_closest_trait(tra1t: &str) -> &str {
    [
        "Quasigroup",
        "Monoid",
        "Semigroup",
        "Group",
        "GroupAbelian",
        "Ring",
        "RingCommutative",
        "Field",
    ].iter()
        .map(|t| (ed::edit_distance(t, tra1t), t))
        .min()
        .expect("Hardcoded array which we are iterating should never be empty. Programming error.")
        .1
}

fn get_dependencies(tra1t: &str, op: usize) -> Vec<String> {
    match tra1t {
        "Quasigroup" => vec![],
        "Monoid" => vec!["Quasigroup"],
        "Semigroup" => vec![],
        "Loop" => vec!["Semigroup"],
        "Group" => vec!["Monoid", "Quasigroup", "Loop", "Semigroup"],
        "GroupAbelian" => vec!["Group", "Monoid", "Quasigroup", "Loop", "Semigroup"],
        _ => match tra1t {
            "Ring" => if op == 0 {
                vec![
                    "GroupAbelian",
                    "Group",
                    "Monoid",
                    "Quasigroup",
                    "Loop",
                    "Semigroup",
                ]
            } else {
                vec!["Monoid", "Quasigroup", "Loop", "Semigroup"]
            },
            "RingCommutative" => if op == 0 {
                vec![
                    "Ring",
                    "GroupAbelian",
                    "Group",
                    "Monoid",
                    "Quasigroup",
                    "Loop",
                    "Semigroup",
                ]
            } else {
                vec!["Ring", "Monoid", "Quasigroup", "Loop", "Semigroup"]
            },
            "Field" => if op == 0 {
                vec![
                    "RingCommutative",
                    "Ring",
                    "GroupAbelian",
                    "Group",
                    "Monoid",
                    "Quasigroup",
                    "Loop",
                    "Semigroup",
                ]
            } else {
                vec![
                    "GroupAbelian",
                    "Group",
                    "Monoid",
                    "Quasigroup",
                    "Loop",
                    "Semigroup",
                ]
            },
            _ => panic!("Unknown Alga trait `{}`. Programming error.", tra1t),
        },
    }.into_iter()
        .map(String::from)
        .collect()
}

fn get_props(tra1t: &str) -> Vec<(Ident, Ident, usize)> {
    match tra1t {
        "Quasigroup" => vec![("prop_inv_is_latin_square", 2)],
        "Monoid" => vec![("prop_operating_identity_element_is_noop", 1)],
        "Semigroup" => vec![("prop_is_associative", 3)],
        "GroupAbelian" => vec![("prop_is_commutative", 2)],
        "Ring" => vec![("prop_mul_and_add_are_distributive", 3)],
        "RingCommutative" => vec![("prop_mul_is_commutative", 2)],
        _ => vec![],
    }.into_iter()
        .map(|(n, p)| {
            (
                Ident::new(&format!("Abstract{}", tra1t), Span::call_site()),
                Ident::new(&format!("{}_approx", n), Span::call_site()),
                p,
            )
        })
        .collect()
}

fn path_to_ident(p: &Path) -> &Ident {
    p.get_ident()
        .unwrap_or_else(|| panic!("Unable to determine trait from path: `{}`.", quote!(#p).to_string()))
}

/// Implementation of the custom derive
#[proc_macro_derive(Alga, attributes(alga_traits, alga_quickcheck))]
pub fn derive_alga(input: TokenStream) -> TokenStream {
    use syn::{parse_macro_input, DeriveInput, NestedMeta, Meta, Lit, TypeParam, GenericParam};

    let item = parse_macro_input!(input as DeriveInput);
    let name = &item.ident;
    let (i, t, w) = item.generics.split_for_impl();
    let (impl_generics, ty_generics) = (once(&i).cycle(), once(&t).cycle());

    let iter = item.attrs
        .iter()
        .filter_map(|a| {
            if let Ok(Meta::List(ml)) = a.parse_meta() {
                if let Some(ident) = ml.path.get_ident() {
                    Some((ident.clone(), ml.nested))
                } else {
                    None
                }
            } else {
                None
            }
        })
        .filter(|(i, _)| *i == "alga_traits")
        .flat_map(|(_, v)| v)
        .map(|t| match t {
            NestedMeta::Meta(ref m) => match m {
                Meta::List(ml) => (path_to_ident(&ml.path).clone(), ml.nested.iter().cloned().collect()),
                Meta::NameValue(mnv) => {
                    if mnv.path.is_ident("Where") {
                        (path_to_ident(&mnv.path).clone(), vec![NestedMeta::Lit(mnv.lit.clone())])
                    } else {
                        panic!("Where clause should be defined with `Where = \"TypeParameter: Trait\"`.");
                    }
                }
                Meta::Path(ref p) => {
                    let i = path_to_ident(p);
                    let oper = match get_op_arity(&format!("{}", i)) {
                        1 => "Operator",
                        2 => "Operator1, Operator2",
                        n => unreachable!("Trait `{}` with unknown arity {} encountered.", name, n),
                    };
                    panic!(
                        "Operator has to be provided via #[alga_traits({}({}))]",
                        i, oper
                    );
                }
            },
            _ => {
                panic!("Derived alga trait has to be provided via #[alga_traits(Trait(Operators))]")
            }
        });

    let mut traits: Vec<(_, _, Option<_>)> = vec![];
    let mut valid_clause_place = false;
    let mut first = true;
    for (name, value) in iter {
        if name == "Where" {
            if valid_clause_place {
                let len = traits.len();
                if let NestedMeta::Lit(Lit::Str(ref clause)) = value[0] {
                    let mut clause = syn::parse_str::<syn::WhereClause>(&format!("where {}", clause.value()))
                        .expect("Where clauses bound was invalid.");
                    if let Some(w) = w {
                        clause.predicates.extend(w.predicates.clone());
                    }
                    traits[len - 1].2 = Some(clause);
                } else {
                    panic!("Where clause should be a string literal.");
                }
                valid_clause_place = false;
            } else {
                if first {
                    panic!("There is where clause before any traits to apply it to.");
                } else {
                    panic!("There is multiple where clauses next to each other.");
                }
            }
        } else {
            first = false;
            valid_clause_place = true;
            let value: Vec<_> = value
                .iter()
                .map(|v| {
                    if let NestedMeta::Meta(Meta::Path(ref path)) = *v {
                        path_to_ident(path).clone()
                    } else {
                        panic!(
                            "Operator has to be provided via #[alga_traits({}({}))].",
                            name,
                            value
                                .iter()
                                .map(|v| match *v {
                                    NestedMeta::Meta(ref m) => path_to_ident(m.path()).to_string(),
                                    NestedMeta::Lit(Lit::Str(ref i)) => i.value(),
                                    _ => "Operator".to_string(),
                                })
                                .collect::<Vec<_>>()
                                .join(", ")
                        );
                    }
                })
                .collect();
            traits.push((name, value, None));
        }
    }

    let (tra1t, op, where_clause, checks) = traits
        .into_iter()
        .flat_map(|(name, value, clause)| {
            let name = name.to_string();
            let arity = get_op_arity(&name);
            let value = value.clone();
            if value.len() != arity {
                match arity {
                    1 => {
                        let message = format!("One operator is required for `{}` trait.", name);
                        match value.len() {
                            0 => panic!("{} None was provided.", message),
                            _ => panic!("{} Too many were provided.", message),
                        }
                    }
                    2 => {
                        let message = format!("Two operators are required for `{}` trait.", name);
                        match value.len() {
                            0 => panic!("{} None was provided.", message),
                            1 => panic!("{} Only one was provided.", message),
                            _ => panic!("{} Too many were provided.", message),
                        }
                    }
                    n => unreachable!("Trait `{}` with unknown arity {} encountered.", name, n),
                }
            }
            let create_tuple = |n: &str, i: usize| {
                let mul = if i == 1 { value.first().cloned() } else { None };
                let value = if get_op_arity(n) == 1 {
                    vec![value[i].clone()]
                } else {
                    value.clone()
                };
                (
                    Ident::new(&format!("Abstract{}", n), Span::call_site()),
                    value.clone(),
                    clause.clone(),
                    (value, mul, get_props(n)),
                )
            };
            let create_tuple = &create_tuple;
            let iter = once(name.clone())
                .chain(get_dependencies(&name, 0))
                .map(|n| create_tuple(&n, 0));
            if arity == 1 {
                iter.collect::<Vec<_>>()
            } else {
                iter.chain(
                    get_dependencies(&name, 1)
                        .into_iter()
                        .map(|n| create_tuple(&n, 1)),
                ).collect()
            }
        })
        .unzip4();
    assert!(!tra1t.is_empty(),
    "Atleast one trait is required to be implemented.\n         Trait can be specified with `#[alga_traits(Trait(Operators))]` attribute.");

    let dummy_const = Ident::new(&format!("_ALGA_DERIVE_{}", name), Span::call_site());
    let type_name = once(&name).cycle();
    let mut tks = quote!(
        #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
        const #dummy_const: () = {
            extern crate alga as _alga;
            #(
                #[automatically_derived]
                impl #impl_generics _alga::general::#tra1t<#(#op,)*> for #type_name #ty_generics #where_clause {}
            )*
        };
    );

    if let Some((_, checked_generics)) = item.attrs
        .iter()
        .filter_map(|a| match a.parse_meta() {
            Ok(Meta::Path(name)) => Some((path_to_ident(&name).clone(), None)),
            Ok(Meta::List(list)) => Some((path_to_ident(&list.path).clone(), Some(list.nested))),
            _ => None,
        })
        .filter(|&(ref n, _)| *n == "alga_quickcheck")
        .next()
    {
        let checked_generics = checked_generics
            .map(|checks| {
                let err = "To specify which concrete types are used for generic parameters `#[alga_quickcheck(check(Type1, Type2))]` form should be used.";
                checks
                    .iter()
                    .map(|ty_params| {
                        if let NestedMeta::Meta(Meta::List(ref list)) = *ty_params {
                            if list.path.is_ident("check") {
                                list.nested.iter()
                                    .map(|ty| {
                                        if let NestedMeta::Meta(Meta::Path(ref path)) = *ty {
                                            path_to_ident(path).clone()
                                        } else {
                                            panic!("Concrete types has to be provided via #[alga_quickcheck(check({}))].", list.nested.iter().map(|v| match *v {
                                NestedMeta::Meta(ref m) => path_to_ident(m.path()).to_string(),
                                NestedMeta::Lit(Lit::Str(ref i)) => i.value(),
                                _ => "Type".to_string(),
                            }).collect::<Vec<_>>().join(", "));
                                        }
                                    })
                                    .collect::<Vec<_>>()
                            } else {
                                panic!(err);
                            }
                        } else {
                            panic!(err);
                        }
                    })
                    .collect()
            })
            .unwrap_or(vec![]);

        for (ops, add, check) in checks {
            let ops = &ops;
            for (tra1t, check, nparams) in check {
                let mut add_test = |check_generics: &[Ident]| {
                    let params: &Vec<_> = &(0..nparams).map(|_| name).collect();
                    let nparams: &Vec<_> = &(0..nparams)
                        .map(|n| Ident::new(&format!("v{}", n), Span::call_site()))
                        .collect();
                    let show_ops: String = ops.iter().map(|n| format!("_{}", n)).collect();
                    let mut name_gens = check_generics
                        .iter()
                        .map(|g| g.to_string())
                        .collect::<Vec<_>>()
                        .join("_");
                    if !name_gens.is_empty() {
                        name_gens = format!("_{}", name_gens);
                    }
                    let test_name = Ident::new(&format!(
                        "{}_for_{}{}_as_{}{}",
                        check, name, name_gens, tra1t, show_ops
                    ), Span::call_site());
                    let check_generics = Generics {
                        params: check_generics.iter().cloned().map(|ident| GenericParam::Type(TypeParam::from(ident))).collect(),
                        ..Default::default()
                    };
                    let generics1 = once(&check_generics).cycle();
                    let generics2 = once(&check_generics).cycle();
                    let nonzero = if let Some(ref add) = add {
                        let add = once(add).cycle();
                        quote!(
                            {
                                let &(#(ref #nparams,)*) = &args;
                                #(
                                    if #nparams == &_alga::general::Identity::<#add>::identity() {
                                        return _quickcheck::TestResult::discard();
                                    }
                                )*
                            }
                        )
                    } else {
                        quote!()
                    };
                    let parsed = quote!(
                        #[test]
                        #[allow(non_snake_case)]
                        fn #test_name() {
                            extern crate quickcheck as _quickcheck;
                            extern crate alga as _alga;
                            fn prop(args: (#(#params #generics1,)*)) -> _quickcheck::TestResult {
                                #nonzero
                                _quickcheck::TestResult::from_bool(_alga::general::#tra1t::<#(#ops),*>::#check(args))
                            }
                            _quickcheck::quickcheck(prop as fn((#(#params #generics2,)*)) -> _quickcheck::TestResult);
                        }
                    );
                    tks.extend(parsed);
                };
                if checked_generics.is_empty() {
                    add_test(&vec![][..]);
                } else {
                    for check_generics in &checked_generics {
                        add_test(check_generics);
                    }
                }
            }
        }
    }

    tks.into()
}

trait Unzip4<A, B, C, D> {
    fn unzip4(self) -> (Vec<A>, Vec<B>, Vec<C>, Vec<D>);
}

impl<A, B, C, D, I> Unzip4<A, B, C, D> for I
where
    I: Iterator<Item = (A, B, C, D)>,
{
    fn unzip4(self) -> (Vec<A>, Vec<B>, Vec<C>, Vec<D>) {
        let hint = self.size_hint().1.unwrap_or(Vec::<A>::new().capacity());
        let (mut va, mut vb, mut vc, mut vd) = (
            Vec::with_capacity(hint),
            Vec::with_capacity(hint),
            Vec::with_capacity(hint),
            Vec::with_capacity(hint),
        );
        for (a, b, c, d) in self {
            va.push(a);
            vb.push(b);
            vc.push(c);
            vd.push(d);
        }
        (va, vb, vc, vd)
    }
}