aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/collection/stack.lux
blob: 549450cbdcbf7aacfb19f209fa233807394b7727 (plain)
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
(.module:
  [library
   [lux #*
    [abstract
     [equivalence (#+ Equivalence)]
     [functor (#+ Functor)]]
    [data
     [collection
      ["//" list]]]
    [type
     abstract]]])

(abstract: .public (Stack a)
  {}

  (List a)

  (def: .public empty
    Stack
    (:abstraction (list)))

  (def: .public size
    (All [a] (-> (Stack a) Nat))
    (|>> :representation //.size))

  (def: .public empty?
    (All [a] (-> (Stack a) Bit))
    (|>> :representation //.empty?))

  (def: .public (value stack)
    (All [a] (-> (Stack a) (Maybe a)))
    (case (:representation stack)
      #.End
      #.None
      
      (#.Item value _)
      (#.Some value)))

  (def: .public (next stack)
    (All [a] (-> (Stack a) (Maybe [a (Stack a)])))
    (case (:representation stack)
      #.End
      #.None
      
      (#.Item top stack')
      (#.Some [top (:abstraction stack')])))

  (def: .public (top value stack)
    (All [a] (-> a (Stack a) (Stack a)))
    (:abstraction (#.Item value (:representation stack))))

  (implementation: .public (equivalence super)
    (All [a]
      (-> (Equivalence a)
          (Equivalence (Stack a))))

    (def: (= reference subject)
      (\ (//.equivalence super) = (:representation reference) (:representation subject))))

  (implementation: .public functor
    (Functor Stack)
    
    (def: (each f value)
      (|> value
          :representation
          (\ //.functor each f)
          :abstraction)))
  )