oxilean-std 0.1.2

OxiLean standard library
Documentation
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
//! Auto-generated module
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)

use oxilean_kernel::{
    BinderInfo, Declaration, Environment, Expr, InductiveEnv, InductiveType, IntroRule, Level, Name,
};

use super::functions::{
    sm_ext_inl_injective, sm_ext_inl_inr_disjoint, sm_ext_inr_injective, sm_ext_sigma_intro,
    sm_ext_sum_applicative_ap, sm_ext_sum_applicative_pure, sm_ext_sum_assoc_iso,
    sm_ext_sum_bifunctor_id, sm_ext_sum_case_analysis, sm_ext_sum_categorical_coproduct,
    sm_ext_sum_disjoint_union, sm_ext_sum_distributes_over_prod, sm_ext_sum_functor_compose,
    sm_ext_sum_functor_id, sm_ext_sum_lefts_count, sm_ext_sum_monad_assoc, sm_ext_sum_monad_bind,
    sm_ext_sum_monad_left_id, sm_ext_sum_monad_return, sm_ext_sum_monad_right_id,
    sm_ext_sum_partition_complete, sm_ext_sum_path_inl, sm_ext_sum_path_inr, sm_ext_sum_path_space,
    sm_ext_sum_swap_involution, sm_ext_sum_traversal_id, sm_ext_sum_traversal_naturality,
    sm_ext_sum_universal_property, sm_ext_sum_void_initial, sm_ext_tagged_union_semantics,
};
use super::types::{
    Coproduct, EitherPartitionSm, SumBifunctor, SumTraversal, SumUniversal, TaggedUnionSm,
};

/// Build axiom: Sigma type projection fst.
fn sm_ext_sigma_fst(env: &mut Environment) -> Result<(), String> {
    use oxilean_kernel::{BinderInfo as Bi, Declaration, Expr, Level, Name};
    let type1 = Expr::Sort(Level::succ(Level::zero()));
    let ty = Expr::Pi(
        Bi::Implicit,
        Name::str("A"),
        Box::new(type1.clone()),
        Box::new(Expr::Pi(
            Bi::Implicit,
            Name::str("B"),
            Box::new(Expr::Pi(
                Bi::Default,
                Name::str("_"),
                Box::new(Expr::BVar(0)),
                Box::new(type1.clone()),
            )),
            Box::new(Expr::Pi(
                Bi::Default,
                Name::str("s"),
                Box::new(Expr::App(
                    Box::new(Expr::App(
                        Box::new(Expr::Const(Name::str("Sigma"), vec![])),
                        Box::new(Expr::BVar(1)),
                    )),
                    Box::new(Expr::BVar(0)),
                )),
                Box::new(Expr::BVar(2)),
            )),
        )),
    );
    match env.add(Declaration::Axiom {
        name: Name::str("Sigma.fst"),
        univ_params: vec![],
        ty,
    }) {
        Ok(()) | Err(_) => Ok(()),
    }
}
/// Build axiom: Sigma type projection snd.
fn sm_ext_sigma_snd(env: &mut Environment) -> Result<(), String> {
    use oxilean_kernel::{Declaration, Expr, Name};
    let prop = Expr::Sort(oxilean_kernel::Level::zero());
    match env.add(Declaration::Axiom {
        name: Name::str("Sigma.snd"),
        univ_params: vec![],
        ty: prop,
    }) {
        Ok(()) | Err(_) => Ok(()),
    }
}
/// Build axiom: Sum elim beta inl (elim (inl a) f g = f a).
fn sm_ext_sum_elim_beta_inl(env: &mut Environment) -> Result<(), String> {
    use oxilean_kernel::{Declaration, Expr, Name};
    let prop = Expr::Sort(oxilean_kernel::Level::zero());
    match env.add(Declaration::Axiom {
        name: Name::str("Sum.elimBetaInl"),
        univ_params: vec![],
        ty: prop,
    }) {
        Ok(()) | Err(_) => Ok(()),
    }
}
/// Build axiom: Sum elim beta inr (elim (inr b) f g = g b).
fn sm_ext_sum_elim_beta_inr(env: &mut Environment) -> Result<(), String> {
    use oxilean_kernel::{Declaration, Expr, Name};
    let prop = Expr::Sort(oxilean_kernel::Level::zero());
    match env.add(Declaration::Axiom {
        name: Name::str("Sum.elimBetaInr"),
        univ_params: vec![],
        ty: prop,
    }) {
        Ok(()) | Err(_) => Ok(()),
    }
}
/// Build axiom: Sum map identity law (map id id = id).
fn sm_ext_sum_map_id_law(env: &mut Environment) -> Result<(), String> {
    use oxilean_kernel::{Declaration, Expr, Name};
    let prop = Expr::Sort(oxilean_kernel::Level::zero());
    match env.add(Declaration::Axiom {
        name: Name::str("Sum.mapIdLaw"),
        univ_params: vec![],
        ty: prop,
    }) {
        Ok(()) | Err(_) => Ok(()),
    }
}
/// Build axiom: Sum map composition law.
fn sm_ext_sum_map_compose_law(env: &mut Environment) -> Result<(), String> {
    use oxilean_kernel::{Declaration, Expr, Name};
    let prop = Expr::Sort(oxilean_kernel::Level::zero());
    match env.add(Declaration::Axiom {
        name: Name::str("Sum.mapComposeLaw"),
        univ_params: vec![],
        ty: prop,
    }) {
        Ok(()) | Err(_) => Ok(()),
    }
}
/// Build axiom: swap ∘ swap = id (involution).
fn sm_ext_swap_swap_id(env: &mut Environment) -> Result<(), String> {
    use oxilean_kernel::{Declaration, Expr, Name};
    let prop = Expr::Sort(oxilean_kernel::Level::zero());
    match env.add(Declaration::Axiom {
        name: Name::str("Sum.swapSwapId"),
        univ_params: vec![],
        ty: prop,
    }) {
        Ok(()) | Err(_) => Ok(()),
    }
}
/// Build axiom: Sum isLeft/isRight partition law.
fn sm_ext_sum_is_left_right_partition(env: &mut Environment) -> Result<(), String> {
    use oxilean_kernel::{Declaration, Expr, Name};
    let prop = Expr::Sort(oxilean_kernel::Level::zero());
    match env.add(Declaration::Axiom {
        name: Name::str("Sum.isLeftRightPartition"),
        univ_params: vec![],
        ty: prop,
    }) {
        Ok(()) | Err(_) => Ok(()),
    }
}
/// Build axiom: Sum sequence (applicative traverse over List).
fn sm_ext_sum_sequence_law(env: &mut Environment) -> Result<(), String> {
    use oxilean_kernel::{Declaration, Expr, Name};
    let prop = Expr::Sort(oxilean_kernel::Level::zero());
    match env.add(Declaration::Axiom {
        name: Name::str("Sum.sequenceLaw"),
        univ_params: vec![],
        ty: prop,
    }) {
        Ok(()) | Err(_) => Ok(()),
    }
}
/// Registration function: registers all extended Sum axioms.
pub fn register_sum_extended_axioms(env: &mut Environment) {
    let _ = sm_ext_sum_categorical_coproduct(env);
    let _ = sm_ext_sum_universal_property(env);
    let _ = sm_ext_inl_injective(env);
    let _ = sm_ext_inr_injective(env);
    let _ = sm_ext_inl_inr_disjoint(env);
    let _ = sm_ext_sum_functor_id(env);
    let _ = sm_ext_sum_functor_compose(env);
    let _ = sm_ext_sum_bifunctor_id(env);
    let _ = sm_ext_sum_monad_return(env);
    let _ = sm_ext_sum_monad_bind(env);
    let _ = sm_ext_sum_monad_left_id(env);
    let _ = sm_ext_sum_monad_right_id(env);
    let _ = sm_ext_sum_monad_assoc(env);
    let _ = sm_ext_sum_applicative_pure(env);
    let _ = sm_ext_sum_applicative_ap(env);
    let _ = sm_ext_sum_swap_involution(env);
    let _ = sm_ext_sum_assoc_iso(env);
    let _ = sm_ext_sum_void_initial(env);
    let _ = sm_ext_sum_distributes_over_prod(env);
    let _ = sm_ext_sum_disjoint_union(env);
    let _ = sm_ext_sum_case_analysis(env);
    let _ = sm_ext_tagged_union_semantics(env);
    let _ = sm_ext_sum_partition_complete(env);
    let _ = sm_ext_sum_lefts_count(env);
    let _ = sm_ext_sum_traversal_naturality(env);
    let _ = sm_ext_sum_traversal_id(env);
    let _ = sm_ext_sum_path_space(env);
    let _ = sm_ext_sum_path_inl(env);
    let _ = sm_ext_sum_path_inr(env);
    let _ = sm_ext_sigma_intro(env);
    let _ = sm_ext_sigma_fst(env);
    let _ = sm_ext_sigma_snd(env);
    let _ = sm_ext_sum_elim_beta_inl(env);
    let _ = sm_ext_sum_elim_beta_inr(env);
    let _ = sm_ext_sum_map_id_law(env);
    let _ = sm_ext_sum_map_compose_law(env);
    let _ = sm_ext_swap_swap_id(env);
    let _ = sm_ext_sum_is_left_right_partition(env);
    let _ = sm_ext_sum_sequence_law(env);
}
/// Fold a list of `Coproduct<E, A>` using a combining function for rights,
/// stopping at the first left.
pub fn fold_rights<A, E: Clone, B>(
    xs: Vec<Coproduct<E, A>>,
    init: B,
    f: impl Fn(B, A) -> B,
) -> Coproduct<E, B> {
    let mut acc = init;
    for x in xs {
        match x {
            Coproduct::Inl(e) => return Coproduct::Inl(e),
            Coproduct::Inr(a) => acc = f(acc, a),
        }
    }
    Coproduct::Inr(acc)
}
/// Zip two `Coproduct<E, A>` and `Coproduct<E, B>` — fails on first left.
pub fn zip_sum<E: Clone, A, B>(sa: Coproduct<E, A>, sb: Coproduct<E, B>) -> Coproduct<E, (A, B)> {
    match (sa, sb) {
        (Coproduct::Inr(a), Coproduct::Inr(b)) => Coproduct::Inr((a, b)),
        (Coproduct::Inl(e), _) => Coproduct::Inl(e),
        (_, Coproduct::Inl(e)) => Coproduct::Inl(e),
    }
}
/// Unzip a `Coproduct<E, (A, B)>` into `(Coproduct<E, A>, Coproduct<E, B>)`.
pub fn unzip_sum<E: Clone, A, B>(s: Coproduct<E, (A, B)>) -> (Coproduct<E, A>, Coproduct<E, B>) {
    match s {
        Coproduct::Inr((a, b)) => (Coproduct::Inr(a), Coproduct::Inr(b)),
        Coproduct::Inl(e) => (Coproduct::Inl(e.clone()), Coproduct::Inl(e)),
    }
}
/// Transpose `Option<Coproduct<E, A>>` to `Coproduct<E, Option<A>>`.
pub fn transpose_option_sum<E, A>(s: Option<Coproduct<E, A>>) -> Coproduct<E, Option<A>> {
    match s {
        None => Coproduct::Inr(None),
        Some(Coproduct::Inr(a)) => Coproduct::Inr(Some(a)),
        Some(Coproduct::Inl(e)) => Coproduct::Inl(e),
    }
}
/// Count elements of a slice that are `Inl`.
pub fn count_lefts<A, B>(xs: &[Coproduct<A, B>]) -> usize {
    xs.iter().filter(|c| c.is_left()).count()
}
/// Count elements of a slice that are `Inr`.
pub fn count_rights<A, B>(xs: &[Coproduct<A, B>]) -> usize {
    xs.iter().filter(|c| c.is_right()).count()
}
/// Build a `TaggedUnionSm` from a `Coproduct`.
pub fn tagged_from_coproduct<A: std::fmt::Debug, B: std::fmt::Debug>(
    c: &Coproduct<A, B>,
) -> TaggedUnionSm {
    match c {
        Coproduct::Inl(a) => TaggedUnionSm::new("left", format!("{:?}", a)),
        Coproduct::Inr(b) => TaggedUnionSm::new("right", format!("{:?}", b)),
    }
}
#[cfg(test)]
mod sum_extended_tests {
    use super::*;
    use std::fmt;
    #[test]
    fn test_sum_universal_new() {
        let u: SumUniversal<i32, &str, String> = SumUniversal::new("test universal");
        assert_eq!(u.description, "test universal");
    }
    #[test]
    fn test_sum_universal_mediate_left() {
        let u: SumUniversal<i32, &str, i32> = SumUniversal::new("med");
        let s = Coproduct::inl(42i32);
        let result = u.mediate(s, |n| n * 2, |s: &str| s.len() as i32);
        assert_eq!(result, 84);
    }
    #[test]
    fn test_sum_universal_mediate_right() {
        let u: SumUniversal<i32, &str, i32> = SumUniversal::new("med");
        let s = Coproduct::inr("hello");
        let result = u.mediate(s, |n| n * 2, |s: &str| s.len() as i32);
        assert_eq!(result, 5);
    }
    #[test]
    fn test_sum_bifunctor_new() {
        let bf: SumBifunctor<i32, &str, i64, String> = SumBifunctor::new("test_bf");
        assert_eq!(bf.name, "test_bf");
    }
    #[test]
    fn test_sum_bifunctor_apply_left() {
        let bf: SumBifunctor<i32, &str, i64, String> = SumBifunctor::new("bf");
        let s = Coproduct::inl(5i32);
        let result = bf.apply(s, |n| n as i64 * 10, |s: &str| s.to_uppercase());
        assert_eq!(result.into_left(), Some(50i64));
    }
    #[test]
    fn test_sum_bifunctor_apply_right() {
        let bf: SumBifunctor<i32, &str, i64, String> = SumBifunctor::new("bf");
        let s = Coproduct::inr("hello");
        let result = bf.apply(s, |n| n as i64, |s: &str| s.to_uppercase());
        assert_eq!(result.into_right(), Some("HELLO".to_string()));
    }
    #[test]
    fn test_tagged_union_sm_new() {
        let t = TaggedUnionSm::new("left", "42");
        assert_eq!(t.tag, "left");
        assert_eq!(t.payload, "42");
    }
    #[test]
    fn test_tagged_union_sm_render() {
        let t = TaggedUnionSm::new("right", "hello");
        assert_eq!(t.render(), "right(hello)");
    }
    #[test]
    fn test_tagged_union_sm_has_tag() {
        let t = TaggedUnionSm::new("left", "x");
        assert!(t.has_tag("left"));
        assert!(!t.has_tag("right"));
    }
    #[test]
    fn test_either_partition_sm_from_vec() {
        let xs: Vec<Coproduct<i32, &str>> =
            vec![Coproduct::inl(1), Coproduct::inr("a"), Coproduct::inl(2)];
        let p = EitherPartitionSm::from_vec(xs);
        assert_eq!(p.left_count(), 2);
        assert_eq!(p.right_count(), 1);
        assert_eq!(p.total(), 3);
    }
    #[test]
    fn test_either_partition_sm_all_right() {
        let xs: Vec<Coproduct<i32, &str>> = vec![Coproduct::inr("x")];
        let p = EitherPartitionSm::from_vec(xs);
        assert!(p.all_right());
        assert!(!p.all_left());
    }
    #[test]
    fn test_either_partition_sm_all_left() {
        let xs: Vec<Coproduct<i32, &str>> = vec![Coproduct::inl(1)];
        let p = EitherPartitionSm::from_vec(xs);
        assert!(p.all_left());
        assert!(!p.all_right());
    }
    #[test]
    fn test_sum_traversal_new() {
        let t: SumTraversal<i32, &str> = SumTraversal::new("test traversal");
        assert_eq!(t.description, "test traversal");
    }
    #[test]
    fn test_sum_traversal_option_some() {
        let t: SumTraversal<i32, &str> = SumTraversal::new("t");
        let s = Coproduct::inr("hello");
        let result = t.traverse_option(s, |s: &str| Some(s.len()));
        assert_eq!(result, Some(Coproduct::inr(5)));
    }
    #[test]
    fn test_sum_traversal_option_none() {
        let t: SumTraversal<i32, &str> = SumTraversal::new("t");
        let s = Coproduct::inr("hello");
        let result: Option<Coproduct<i32, usize>> = t.traverse_option(s, |_| None);
        assert_eq!(result, None);
    }
    #[test]
    fn test_sum_traversal_option_left() {
        let t: SumTraversal<i32, &str> = SumTraversal::new("t");
        let s: Coproduct<i32, &str> = Coproduct::inl(42);
        let result = t.traverse_option(s, |s: &str| Some(s.len()));
        assert_eq!(result, Some(Coproduct::inl(42)));
    }
    #[test]
    fn test_fold_rights_all_ok() {
        let xs: Vec<Coproduct<String, i32>> =
            vec![Coproduct::inr(1), Coproduct::inr(2), Coproduct::inr(3)];
        let result = fold_rights(xs, 0, |acc, n| acc + n);
        assert_eq!(result.into_right(), Some(6));
    }
    #[test]
    fn test_fold_rights_short_circuit() {
        let xs: Vec<Coproduct<String, i32>> = vec![
            Coproduct::inr(1),
            Coproduct::inl("err".to_string()),
            Coproduct::inr(3),
        ];
        let result = fold_rights(xs, 0, |acc, n| acc + n);
        assert!(result.is_left());
    }
    #[test]
    fn test_zip_sum_both_right() {
        let sa: Coproduct<String, i32> = Coproduct::inr(1);
        let sb: Coproduct<String, &str> = Coproduct::inr("a");
        let result = zip_sum(sa, sb);
        assert_eq!(result.into_right(), Some((1, "a")));
    }
    #[test]
    fn test_zip_sum_left_first() {
        let sa: Coproduct<String, i32> = Coproduct::inl("err".to_string());
        let sb: Coproduct<String, &str> = Coproduct::inr("a");
        let result = zip_sum(sa, sb);
        assert!(result.is_left());
    }
    #[test]
    fn test_unzip_sum_right() {
        let s: Coproduct<String, (i32, &str)> = Coproduct::inr((5, "hi"));
        let (sa, sb) = unzip_sum(s);
        assert_eq!(sa.into_right(), Some(5));
        assert_eq!(sb.into_right(), Some("hi"));
    }
    #[test]
    fn test_unzip_sum_left() {
        let s: Coproduct<String, (i32, &str)> = Coproduct::inl("err".to_string());
        let (sa, sb) = unzip_sum(s);
        assert!(sa.is_left());
        assert!(sb.is_left());
    }
    #[test]
    fn test_transpose_option_sum_none() {
        let s: Option<Coproduct<String, i32>> = None;
        let result = transpose_option_sum(s);
        assert_eq!(result.into_right(), Some(None));
    }
    #[test]
    fn test_transpose_option_sum_some_right() {
        let s: Option<Coproduct<String, i32>> = Some(Coproduct::inr(42));
        let result = transpose_option_sum(s);
        assert_eq!(result.into_right(), Some(Some(42)));
    }
    #[test]
    fn test_transpose_option_sum_some_left() {
        let s: Option<Coproduct<String, i32>> = Some(Coproduct::inl("err".to_string()));
        let result = transpose_option_sum(s);
        assert!(result.is_left());
    }
    #[test]
    fn test_count_lefts_rights() {
        let xs: Vec<Coproduct<i32, &str>> = vec![
            Coproduct::inl(1),
            Coproduct::inr("a"),
            Coproduct::inl(2),
            Coproduct::inr("b"),
        ];
        assert_eq!(count_lefts(&xs), 2);
        assert_eq!(count_rights(&xs), 2);
    }
    #[test]
    fn test_tagged_from_coproduct_left() {
        let c: Coproduct<i32, &str> = Coproduct::inl(42);
        let t = tagged_from_coproduct(&c);
        assert!(t.has_tag("left"));
        assert!(t.payload.contains("42"));
    }
    #[test]
    fn test_tagged_from_coproduct_right() {
        let c: Coproduct<i32, &str> = Coproduct::inr("hello");
        let t = tagged_from_coproduct(&c);
        assert!(t.has_tag("right"));
        assert!(t.payload.contains("hello"));
    }
    #[test]
    fn test_register_sum_extended_axioms_runs() {
        let mut env = Environment::new();
        register_sum_extended_axioms(&mut env);
    }
}