un_algebra 0.9.0

Simple implementations of selected abstract algebraic structures--including groups, rings, and fields. Intended for self-study of abstract algebra concepts and not for production use.
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
//
// # Example: The _rational_ _numbers_ ℚ.
//
// The rational numbers (ℚ) form a _field_. Rust does not have a
// rational number type in the standard library so the rational number
// type used here is the `Ratio<T>` type from the very handy `num`
// crate.
//
use ::num::rational::*;
use un_algebra::tests::*;
use un_algebra::prelude::*;


//
// Integer "base" type for rational numerators and denominators. To keep
// this example simple-ish we limit rationals to `i128` components.
// Ideally, they should be unbounded integers, e.g. `num`'s `BigInt`
// type.
//
type Base = i128;


//
// We use a newtype wrapper around the `num` crate rational type to work
// within Rust's _trait_ _coherence_ rules.
//
#[derive(Copy, Clone, Debug, PartialEq)]
struct Rational(Ratio<Base>);


//
// Create a Rational instance from base integer components.
//
impl Rational {
  pub fn new(n: Base, d: Base) -> Self {
    Self(Ratio::new(n, d))
  }
}


//
// Rational numbers form an additive magma with (rational) addition as
// the operation.
//
impl AddMagma for Rational {

  fn add(&self, other: &Self) -> Self {
    Self(self.0 + other.0)
  }
}


//
// Rational numbers form an additive semigroup as (rational) addition is
// associative.
//
impl AddSemigroup for Rational {}


//
// Rational numbers form an additive monoid with (rational) zero as the
// additive identity.
//
impl AddMonoid for Rational {

  fn zero() -> Self {
    Self::new(0, 1)
  }
}


//
// Rational numbers form an additive group with (rational) negation as
// the group inverse.
//
impl AddGroup for Rational {

  fn negate(&self) -> Self {
    Self(-self.0)
  }
}


//
// Rational numbers form an additive commutative group as (rational)
// addition is commutative.
//
impl AddComGroup for Rational {}


//
// Rational numbers form a multiplicative magma with (rational)
// multiplication as the operation.
//
impl MulMagma for Rational {

  fn mul(&self, other: &Self) -> Self {
    Self(self.0 * other.0)
  }
}


//
// Rational numbers form a multiplicative semigroup as (rational)
// multiplication is associative.
//
impl MulSemigroup for Rational {}


// Non-zero rational numbers form a quasigroup with left and right
// division of non-zero (rational) values.
//
impl Quasigroup for Rational {

  fn is_divisor(&self) -> bool {
    *self != Self::new(0, 1)
  }


  fn ldiv(&self, other: &Self) -> Self {
    Self(other.0 / self.0)
  }


  fn rdiv(&self, other: &Self) -> Self {
    Self(self.0 / other.0)
  }
}


//
// Rational numbers form a multiplicative monoid with (rational) one as
// the multiplicative identity.
//
impl MulMonoid for Rational {

  fn one() -> Self {
    Self::new(1, 1)
  }
}


//
// Rational numbers form a multiplicative group with reciprocal of
// non-zero (rational) values as the group inverse.
//
impl MulGroup for Rational {

  fn is_invertible(&self) -> bool {
    *self != Self::new(0, 1)
  }


  fn invert(&self) -> Self {
    Self(self.0.recip())
  }
}


//
// Rational numbers form a multiplicative commutative group as
// (rational) multiplication is commutative.
//
impl MulComGroup for Rational {}


//
// Rational numbers form a ring.
//
impl Ring for Rational {}


//
// Rational numbers form a commutative ring.
//
impl ComRing for Rational {}


//
// Rational numbers (without zero) form a field with reciprocal of
// non-zero (rational) values as the field inverse.
//
impl Field for Rational {

  fn invert(&self) -> Self {
    Self(self.0.recip())
  }
}


//
// Generate `proptest` arbitrary (i.e. boxed strategy) Rational values
// from i32 components. Short function name to keep generator
// expressions manageable.
//
fn r32() -> impl Strategy<Value = Rational> {
  let ints = any::<(i32, i32)>(); // i32's to avoid overflow.

  ints.prop_map(|(n, d)| Rational::new(i128::from(n), i128::from(d)))
}


#[cfg(test)]
proptest! {
  #![proptest_config(config::standard())]


  #[test]
  fn add_closure([p, q] in [r32(), r32()]) {
    prop_assert!(AddMagmaLaws::closure(&p, &q))
  }


  #[test]
  fn mul_closure([p, q] in [r32(), r32()]) {
    prop_assert!(MulMagmaLaws::closure(&p, &q))
  }


  #[test]
  fn mul_associative([p, q, r] in [r32(), r32(), r32()]) {
    prop_assert!(MulSemigroupLaws::associativity(&p, &q, &r))
  }


  #[test]
  fn left_ldiv([p, q] in [r32(), r32()]) {
    prop_assume!(p.is_divisor());

    prop_assert!(p.left_lcancellation(&q))
  }


  #[test]
  fn right_ldiv([p, q] in [r32(), r32()]) {
    prop_assume!(q.is_divisor());

    prop_assert!(p.right_lcancellation(&q))
  }


  #[test]
  fn left_rdiv([p, q] in [r32(), r32()]) {
    prop_assume!(q.is_divisor());

    prop_assert!(p.left_rcancellation(&q))
  }


  #[test]
  fn right_rdiv([p, q] in [r32(), r32()]) {
    prop_assume!(q.is_divisor());

    prop_assert!(p.right_rcancellation(&q))
  }


  #[test]
  fn add_associative([p, q, r] in [r32(), r32(), r32()]) {
    prop_assert!(AddSemigroupLaws::associativity(&p, &q, &r))
  }


  #[test]
  fn left_add_identity(q in r32()) {
    prop_assert!(AddMonoidLaws::left_identity(&q))
  }


  #[test]
  fn right_add_identity(q in r32()) {
    prop_assert!(AddMonoidLaws::right_identity(&q))
  }


  #[test]
  fn left_mul_identity(q in r32()) {
    prop_assert!(MulMonoidLaws::left_identity(&q))
  }


  #[test]
  fn right_mul_identity(q in r32()) {
    prop_assert!(MulMonoidLaws::right_identity(&q))
  }


  #[test]
  fn left_add_inverse(q in r32()) {
    prop_assert!(AddGroupLaws::left_inverse(&q))
  }


  #[test]
  fn right_add_inverse(q in r32()) {
    prop_assert!(AddGroupLaws::right_inverse(&q))
  }


  #[test]
  fn left_mul_inverse(q in r32()) {
    prop_assume!(MulGroup::is_invertible(&q));

    prop_assert!(MulGroupLaws::left_inverse(&q))
  }


  #[test]
  fn right_mul_inverse(q in r32()) {
    prop_assume!(MulGroup::is_invertible(&q));

    prop_assert!(MulGroupLaws::right_inverse(&q))
  }


  #[test]
  fn add_commute([p, q] in [r32(), r32()]) {
    prop_assert!(AddComGroupLaws::commutivity(&p, &q))
  }


  #[test]
  fn mul_commute([p, q] in [r32(), r32()]) {
    prop_assert!(MulComGroupLaws::commutivity(&p, &q))
  }


  #[test]
  fn left_distributivity([q, r, s] in [r32(), r32(), r32()]) {
    prop_assert!(RingLaws::left_distributivity(&q, &r, &s))
  }


  #[test]
  fn right_distributivity([q, r, s] in [r32(), r32(), r32()]) {
    prop_assert!(RingLaws::right_distributivity(&q, &r, &s))
  }


  #[test]
  fn left_absorption(q in r32()) {
    prop_assert!(RingLaws::left_absorption(&q))
  }


  #[test]
  fn right_absorption(q in r32()) {
    prop_assert!(RingLaws::right_absorption(&q))
  }


  #[test]
  fn left_negation((q, r) in (r32(), r32())) {
    prop_assert!(RingLaws::left_negation(&q, &r))
  }


  #[test]
  fn right_negation((q, r) in (r32(), r32())) {
    prop_assert!(RingLaws::right_negation(&q, &r))
  }


  #[test]
  fn commutivity((q, r) in (r32(), r32())) {
    prop_assert!(ComRingLaws::commutivity(&q, &r))
  }


  #[test]
  fn field_left_inverse(q in r32()) {
    prop_assume!(Field::is_invertible(&q));

    prop_assert!(FieldLaws::left_inverse(&q))
  }


  #[test]
  fn field_right_inverse(q in r32()) {
    prop_assume!(Field::is_invertible(&q));

    prop_assert!(FieldLaws::left_inverse(&q))
  }


  #[test]
  fn zero_cancellation([p, q] in [r32(), r32()]) {
    prop_assert!(FieldLaws::zero_cancellation(&p, &q))
  }


  #[test]
  fn add_cancellation([q, r, s] in [r32(), r32(), r32()]) {
    prop_assert!(FieldLaws::add_cancellation(&q, &r, &s))
  }


  #[test]
  fn mul_cancellation([q, r, s] in [r32(), r32(), r32()]) {
    prop_assert!(FieldLaws::mul_cancellation(&q, &r, &s))
  }
}


fn main() {
}