aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/collection/stack.lux
blob: 4994baf674b785ba26014119f8c856f0ed1fa9ae (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
(.using
 [library
  [lux (.except)
   [abstract
    [equivalence (.only Equivalence)]
    [functor (.only Functor)]]
   [data
    [collection
     ["//" list]]]
   [type
    [primitive (.except)]]]])

(primitive: .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)))
  )