ordofp_core 0.1.0

OrdoFP core provides developers with HList, Disiunctio, NominataUniversalis, Universalis, and functional type classes
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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
//! Persistent Stack implementation.
//!
//! A fully persistent LIFO (Last In, First Out) stack using a linked list
//! with reference counting for structural sharing.

use core::iter::FromIterator;

#[cfg(feature = "alloc")]
use alloc::{rc::Rc, vec::Vec};

/// Error type for Stack operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum StackError {
    /// Index out of bounds.
    IndexOutOfBounds,
}

/// A persistent (immutable) stack.
///
/// All operations preserve the original stack, returning new versions.
/// Uses structural sharing via `Rc` for efficiency.
///
/// # Time Complexity
///
/// | Operation | Time |
/// |-----------|------|
/// | `push`    | O(1) |
/// | `pop`     | O(1) |
/// | `peek`    | O(1) |
/// | `len`     | O(n) |
/// | `get`     | O(n) |
///
/// # Example
///
/// ```rust
/// use ordofp_core::pfds::Stack;
///
/// let s1 = Stack::new().push(1).push(2).push(3);
/// assert_eq!(s1.peek(), Some(&3));
///
/// let (top, s2) = s1.clone().pop().unwrap();
/// assert_eq!(top, 3);
/// assert_eq!(s2.peek(), Some(&2));
///
/// // s1 is still valid and unchanged
/// assert_eq!(s1.peek(), Some(&3));
/// ```
#[cfg(feature = "alloc")]
pub struct Stack<A> {
    head: Link<A>,
}

#[cfg(feature = "alloc")]
type Link<A> = Option<Rc<Node<A>>>;

#[cfg(feature = "alloc")]
struct Node<A> {
    elem: A,
    next: Link<A>,
}

#[cfg(feature = "alloc")]
impl<A> Default for Stack<A> {
    #[inline]
    fn default() -> Self {
        Stack { head: None }
    }
}

// Manual: O(1), no `A: Clone` bound (derive would add one).
#[cfg(feature = "alloc")]
impl<A> Clone for Stack<A> {
    #[inline]
    fn clone(&self) -> Self {
        Stack {
            head: self.head.clone(),
        }
    }
}

#[cfg(feature = "alloc")]
impl<A: core::fmt::Debug> core::fmt::Debug for Stack<A> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_list().entries(self.iter()).finish()
    }
}

#[cfg(feature = "alloc")]
impl<A> Drop for Stack<A> {
    fn drop(&mut self) {
        // Iteratively unlink uniquely-owned nodes. Default drop recursion
        // depth equals stack length — a ~30k-element stack aborts the process.
        let mut cur = self.head.take();
        while let Some(node) = cur {
            match Rc::try_unwrap(node) {
                Ok(mut n) => cur = n.next.take(),
                // Shared tail: another Stack still owns it; that owner's
                // Drop unlinks it iteratively in turn.
                Err(_) => break,
            }
        }
    }
}

#[cfg(feature = "alloc")]
impl<A: PartialEq> PartialEq for Stack<A> {
    fn eq(&self, other: &Self) -> bool {
        let (mut a, mut b) = (&self.head, &other.head);
        loop {
            match (a, b) {
                (None, None) => return true,
                (Some(x), Some(y)) => {
                    // Shared spine: equal by construction.
                    if Rc::ptr_eq(x, y) {
                        return true;
                    }
                    if x.elem != y.elem {
                        return false;
                    }
                    a = &x.next;
                    b = &y.next;
                }
                _ => return false,
            }
        }
    }
}

#[cfg(feature = "alloc")]
impl<A: Eq> Eq for Stack<A> {}

#[cfg(feature = "alloc")]
impl<A> Stack<A> {
    /// Create a new empty stack.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ordofp_core::pfds::Stack;
    ///
    /// let s: Stack<i32> = Stack::new();
    /// assert!(s.is_empty());
    /// ```
    #[inline]
    pub fn new() -> Self {
        Stack { head: None }
    }

    /// Check if the stack is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.head.is_none()
    }

    /// Push a value onto the stack.
    ///
    /// Returns a new stack with the value at the top.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ordofp_core::pfds::Stack;
    ///
    /// let s = Stack::new().push(1).push(2);
    /// assert_eq!(s.peek(), Some(&2));
    /// ```
    #[inline]
    pub fn push(mut self, value: A) -> Self {
        // `take`, never destructure: Stack has Drop (E0509).
        let next = self.head.take();
        Stack {
            head: Some(Rc::new(Node { elem: value, next })),
        }
    }

    /// Pop the top value from the stack.
    ///
    /// Returns `Some((value, new_stack))` if non-empty, `None` otherwise.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ordofp_core::pfds::Stack;
    ///
    /// let s = Stack::new().push(1).push(2);
    /// let (top, rest) = s.pop().unwrap();
    /// assert_eq!(top, 2);
    /// assert_eq!(rest.peek(), Some(&1));
    /// ```
    #[inline]
    pub fn pop(mut self) -> Option<(A, Self)>
    where
        A: Clone,
    {
        let node = self.head.take()?;
        match Rc::try_unwrap(node) {
            Ok(n) => Some((n.elem, Stack { head: n.next })),
            // Shared: clone the top element, share the tail.
            Err(rc) => Some((
                rc.elem.clone(),
                Stack {
                    head: rc.next.clone(),
                },
            )),
        }
    }

    /// Peek at the top value without removing it.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ordofp_core::pfds::Stack;
    ///
    /// let s = Stack::new().push(1).push(2);
    /// assert_eq!(s.peek(), Some(&2));
    /// assert_eq!(s.peek(), Some(&2)); // Still there
    /// ```
    #[inline]
    pub fn peek(&self) -> Option<&A> {
        self.head.as_deref().map(|n| &n.elem)
    }

    /// Get the tail of the stack (everything except the top).
    ///
    /// Returns the tail stack, if any (O(1) clone via structural sharing).
    #[inline]
    pub fn tail(&self) -> Option<Self> {
        self.head.as_ref().map(|node| Stack {
            head: node.next.clone(),
        })
    }

    /// Get the length of the stack.
    ///
    /// Note: This is O(n) as it traverses the entire stack.
    pub fn len(&self) -> usize {
        // Use iterative approach to avoid recursion limit
        let mut count = 0;
        let mut current = self.head.as_deref();
        while let Some(node) = current {
            count += 1;
            current = node.next.as_deref();
        }
        count
    }

    /// Get the element at the specified index.
    ///
    /// Index 0 is the top of the stack.
    #[inline]
    pub fn get(&self, index: usize) -> Option<&A> {
        // Use iterative approach to avoid recursion limit
        let mut current = self.head.as_deref();
        let mut remaining = index;
        while let Some(node) = current {
            if remaining == 0 {
                return Some(&node.elem);
            }
            remaining -= 1;
            current = node.next.as_deref();
        }
        None
    }

    /// Update the element at the specified index.
    ///
    /// Returns a new stack with the updated value. The nodes before `index`
    /// are copied; the tail after it is shared with the original stack.
    ///
    /// # Errors
    ///
    /// Returns `StackError::IndexOutOfBounds` if `index >= self.len()`.
    pub fn update(&self, index: usize, value: A) -> Result<Self, StackError>
    where
        A: Clone,
    {
        // Use iterative approach to avoid recursion limit: walk to `index`
        // collecting the prefix, replace the element, share the tail.
        // (No with_capacity(index): a wildly out-of-bounds index must
        // return Err, not attempt a huge allocation.)
        let mut prefix: Vec<&A> = Vec::new();
        let mut current = self.head.as_deref();
        loop {
            match current {
                None => return Err(StackError::IndexOutOfBounds),
                Some(node) => {
                    if prefix.len() == index {
                        let updated = Stack {
                            head: Some(Rc::new(Node {
                                elem: value,
                                next: node.next.clone(),
                            })),
                        };
                        return Ok(prefix
                            .into_iter()
                            .rev()
                            .fold(updated, |acc, x| acc.push(x.clone())));
                    }
                    prefix.push(&node.elem);
                    current = node.next.as_deref();
                }
            }
        }
    }

    /// Reverse the stack.
    ///
    /// Returns a new stack with elements in reverse order.
    pub fn reverse(&self) -> Self
    where
        A: Clone,
    {
        let mut result = Stack::new();
        let mut current = self.head.as_deref();
        while let Some(node) = current {
            result = result.push(node.elem.clone());
            current = node.next.as_deref();
        }
        result
    }

    /// Map a function over the stack.
    ///
    /// Returns a new stack with the function applied to each element.
    #[inline]
    pub fn map<B, F>(&self, f: F) -> Stack<B>
    where
        A: Clone,
        F: Fn(&A) -> B,
    {
        // Use iterative approach to avoid recursion limit
        let mut mapped: Vec<B> = Vec::with_capacity(self.len());
        let mut current = self.head.as_deref();
        while let Some(node) = current {
            mapped.push(f(&node.elem));
            current = node.next.as_deref();
        }
        // Build result in reverse
        mapped.into_iter().rev().fold(Stack::new(), Stack::push)
    }

    /// Fold the stack from left to right.
    #[inline]
    pub fn fold<B, F>(&self, init: B, f: F) -> B
    where
        F: Fn(B, &A) -> B,
    {
        // Use iterative approach to avoid recursion limit
        let mut acc = init;
        let mut current = self.head.as_deref();
        while let Some(node) = current {
            acc = f(acc, &node.elem);
            current = node.next.as_deref();
        }
        acc
    }

    /// Filter elements that satisfy the predicate.
    #[inline]
    pub fn filter<F>(&self, pred: F) -> Self
    where
        A: Clone,
        F: Fn(&A) -> bool,
    {
        // Use iterative approach to avoid recursion limit
        let mut filtered: Vec<&A> = Vec::with_capacity(self.len());
        let mut current = self.head.as_deref();
        while let Some(node) = current {
            if pred(&node.elem) {
                filtered.push(&node.elem);
            }
            current = node.next.as_deref();
        }
        // Build result in reverse
        filtered
            .into_iter()
            .rev()
            .fold(Stack::new(), |acc, x| acc.push(x.clone()))
    }

    /// Concatenate two stacks.
    ///
    /// Elements of `self` come before elements of `other`.
    #[inline]
    pub fn concat(&self, other: &Self) -> Self
    where
        A: Clone,
    {
        // Collect self's elements, then build on top of other
        let mut elements: Vec<&A> = Vec::with_capacity(self.len());
        let mut current = self.head.as_deref();
        while let Some(node) = current {
            elements.push(&node.elem);
            current = node.next.as_deref();
        }
        // Build result by pushing in reverse order onto other
        elements
            .into_iter()
            .rev()
            .fold(other.clone(), |acc, x| acc.push(x.clone()))
    }

    /// Convert to a Vec.
    #[inline]
    pub fn to_vec(&self) -> Vec<A>
    where
        A: Clone,
    {
        let mut result = Vec::with_capacity(self.len());
        let mut current = self.head.as_deref();
        while let Some(node) = current {
            result.push(node.elem.clone());
            current = node.next.as_deref();
        }
        result
    }
}

#[cfg(feature = "alloc")]
impl<A: Clone> From<Vec<A>> for Stack<A> {
    fn from(vec: Vec<A>) -> Self {
        vec.into_iter().rev().fold(Stack::new(), Stack::push)
    }
}

#[cfg(feature = "alloc")]
impl<A: Clone> FromIterator<A> for Stack<A> {
    fn from_iter<I: IntoIterator<Item = A>>(iter: I) -> Self {
        // Collect to vec first, then reverse to maintain order
        let vec: Vec<A> = iter.into_iter().collect();
        vec.into_iter().rev().fold(Stack::new(), Stack::push)
    }
}

/// Iterator over a Stack.
#[cfg(feature = "alloc")]
pub struct StackIter<'a, A> {
    current: Option<&'a Node<A>>,
}

#[cfg(feature = "alloc")]
impl<'a, A> Iterator for StackIter<'a, A> {
    type Item = &'a A;

    fn next(&mut self) -> Option<Self::Item> {
        let node = self.current.take()?;
        self.current = node.next.as_deref();
        Some(&node.elem)
    }
}

#[cfg(feature = "alloc")]
impl<'a, A> IntoIterator for &'a Stack<A> {
    type Item = &'a A;
    type IntoIter = StackIter<'a, A>;

    fn into_iter(self) -> Self::IntoIter {
        StackIter {
            current: self.head.as_deref(),
        }
    }
}

#[cfg(feature = "alloc")]
impl<A> Stack<A> {
    /// Returns an iterator over references to elements.
    #[inline]
    pub fn iter(&self) -> StackIter<'_, A> {
        StackIter {
            current: self.head.as_deref(),
        }
    }
}

#[cfg(feature = "serde")]
impl<A: serde::Serialize> serde::Serialize for Stack<A> {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        // Top-first element sequence; iterative, unlike the old derived
        // node-nesting which recursed per element.
        s.collect_seq(self.iter())
    }
}

#[cfg(all(feature = "serde", feature = "alloc"))]
impl<'de, A: serde::Deserialize<'de>> serde::Deserialize<'de> for Stack<A> {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let items = Vec::<A>::deserialize(d)?;
        // Rebuild through push so invariants hold by construction.
        Ok(items.into_iter().rev().fold(Stack::new(), Stack::push))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::vec;

    #[test]
    fn test_new_is_empty() {
        let s: Stack<i32> = Stack::new();
        assert!(s.is_empty());
        assert_eq!(s.len(), 0);
    }

    #[test]
    fn test_push_pop() {
        let s = Stack::new().push(1).push(2).push(3);
        assert_eq!(s.len(), 3);
        assert_eq!(s.peek(), Some(&3));

        let (top, rest) = s
            .pop()
            .expect("stack with 3 elements should pop successfully");
        assert_eq!(top, 3);
        assert_eq!(rest.peek(), Some(&2));
    }

    #[test]
    fn test_persistence() {
        let s1 = Stack::new().push(1).push(2);
        let s2 = s1.clone().push(3);

        // s1 unchanged
        assert_eq!(s1.peek(), Some(&2));
        assert_eq!(s1.len(), 2);

        // s2 has new element
        assert_eq!(s2.peek(), Some(&3));
        assert_eq!(s2.len(), 3);
    }

    #[test]
    fn test_get() {
        let s = Stack::new().push(1).push(2).push(3);
        assert_eq!(s.get(0), Some(&3));
        assert_eq!(s.get(1), Some(&2));
        assert_eq!(s.get(2), Some(&1));
        assert_eq!(s.get(3), None);
    }

    #[test]
    fn test_update() {
        let s1 = Stack::new().push(1).push(2).push(3);
        let s2 = s1
            .update(1, 5)
            .expect("index 1 is valid for a 3-element stack");

        assert_eq!(s1.get(1), Some(&2)); // unchanged
        assert_eq!(s2.get(1), Some(&5)); // updated
    }

    #[test]
    fn test_reverse() {
        let s = Stack::new().push(1).push(2).push(3);
        let r = s.reverse();

        assert_eq!(r.peek(), Some(&1));
        let (_, r) = r.pop().expect("reversed stack has at least one element");
        assert_eq!(r.peek(), Some(&2));
    }

    #[test]
    fn test_map() {
        let s = Stack::new().push(1).push(2).push(3);
        let doubled = s.map(|x| x * 2);

        assert_eq!(doubled.peek(), Some(&6));
        assert_eq!(doubled.get(1), Some(&4));
        assert_eq!(doubled.get(2), Some(&2));
    }

    #[test]
    fn test_fold() {
        let s = Stack::new().push(1).push(2).push(3);
        let sum = s.fold(0, |acc, x| acc + x);
        assert_eq!(sum, 6);
    }

    #[test]
    fn test_filter() {
        let s = Stack::new().push(1).push(2).push(3).push(4);
        let evens = s.filter(|x| x % 2 == 0);

        assert_eq!(evens.len(), 2);
        assert_eq!(evens.peek(), Some(&4));
    }

    #[test]
    fn test_concat() {
        let s1 = Stack::new().push(1).push(2);
        let s2 = Stack::new().push(3).push(4);
        let combined = s1.concat(&s2);

        assert_eq!(combined.len(), 4);
        assert_eq!(combined.to_vec(), vec![2, 1, 4, 3]);
    }

    #[test]
    fn test_from_vec() {
        let v = vec![1, 2, 3];
        let s = Stack::from(v);

        assert_eq!(s.peek(), Some(&1));
        assert_eq!(s.to_vec(), vec![1, 2, 3]);
    }

    #[test]
    fn test_iter() {
        let s = Stack::new().push(1).push(2).push(3);
        let items: Vec<_> = s.into_iter().copied().collect();
        assert_eq!(items, vec![3, 2, 1]);
    }

    #[test]
    fn test_from_iter() {
        let s: Stack<i32> = vec![1, 2, 3].into_iter().collect();
        assert_eq!(s.to_vec(), vec![1, 2, 3]);
    }

    #[test]
    fn test_update_empty_stack_returns_error() {
        let s: Stack<i32> = Stack::new();
        assert_eq!(s.update(0, 42), Err(StackError::IndexOutOfBounds));
    }

    #[test]
    fn test_update_out_of_bounds_returns_error() {
        let s = Stack::new().push(1).push(2);
        // Stack has indices 0 and 1; index 2 is out of bounds
        assert_eq!(s.update(2, 99), Err(StackError::IndexOutOfBounds));
    }

    /// `peek`, `pop`, and `tail` on an empty stack must all return `None`,
    /// and `filter` with a predicate that rejects every element must yield
    /// an empty stack.
    #[test]
    fn test_empty_stack_observer_edge_cases() {
        let empty: Stack<i32> = Stack::new();

        assert_eq!(empty.peek(), None, "peek on empty stack must return None");
        assert!(
            empty.clone().pop().is_none(),
            "pop on empty stack must return None"
        );
        assert!(
            empty.tail().is_none(),
            "tail on empty stack must return None"
        );

        let s = Stack::new().push(1).push(2).push(3);
        let none_pass = s.filter(|_| false);
        assert!(
            none_pass.is_empty(),
            "filter rejecting all elements must yield an empty stack"
        );
    }

    /// H6 regression (reproduced pre-fix as a process abort at ~30k).
    #[test]
    fn drop_deep_stack_no_overflow() {
        let mut s = Stack::new();
        for i in 0..100_000u32 {
            s = s.push(i);
        }
        drop(s);
    }

    /// H6 regression: eq on two independently built stacks (no shared Rc
    /// spine, so the ptr_eq shortcut cannot save us).
    #[test]
    fn eq_deep_stacks_no_overflow() {
        let build = || (0..100_000u32).fold(Stack::new(), super::Stack::push);
        assert_eq!(build(), build());
    }
}

#[cfg(all(test, feature = "serde"))]
mod serde_tests {
    use super::*;

    #[test]
    fn serde_roundtrip_is_sequence_shaped() {
        let s = Stack::new().push(1).push(2).push(3);
        let json = serde_json::to_string(&s).unwrap();
        assert_eq!(json, "[3,2,1]"); // top-first sequence, not nested nodes
        let back: Stack<i32> = serde_json::from_str(&json).unwrap();
        assert_eq!(s, back);
    }

    #[test]
    fn serde_deep_input_no_overflow() {
        let json = serde_json::to_string(&(0..100_000u32).collect::<Vec<_>>()).unwrap();
        let s: Stack<u32> = serde_json::from_str(&json).unwrap();
        assert_eq!(s.len(), 100_000);
    }
}