blob: 3f720d9acd6ef39f9dc8648a6eb4cd14f1f92fdb (
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
69
|
(.module:
[library
[lux #*
[abstract
[equivalence (#+ Equivalence)]
[functor (#+ Functor)]]
[data
[collection
["//" list]]]
[type
abstract]]])
(abstract: #export (Stack a)
(List a)
{#.doc (doc "A first-in, last-out sequential data-structure.")}
(def: #export empty
Stack
(:abstraction (list)))
(def: #export size
(All [a] (-> (Stack a) Nat))
(|>> :representation //.size))
(def: #export empty?
(All [a] (-> (Stack a) Bit))
(|>> :representation //.empty?))
(def: #export (peek stack)
{#.doc (doc "Yields the top value in the stack, if any.")}
(All [a] (-> (Stack a) (Maybe a)))
(case (:representation stack)
#.End
#.None
(#.Item value _)
(#.Some value)))
(def: #export (pop stack)
(All [a] (-> (Stack a) (Maybe [a (Stack a)])))
(case (:representation stack)
#.End
#.None
(#.Item top stack')
(#.Some [top (:abstraction stack')])))
(def: #export (push value stack)
(All [a] (-> a (Stack a) (Stack a)))
(:abstraction (#.Item value (:representation stack))))
(implementation: #export (equivalence super)
(All [a]
(-> (Equivalence a)
(Equivalence (Stack a))))
(def: (= reference subject)
(\ (//.equivalence super) = (:representation reference) (:representation subject))))
(implementation: #export functor
(Functor Stack)
(def: (map f value)
(|> value
:representation
(\ //.functor map f)
:abstraction)))
)
|