author | wenzelm |
Sun, 27 Oct 2024 11:02:21 +0100 | |
changeset 81266 | 8300511f4c45 |
parent 58797 | 6d71f19a9fd6 |
permissions | -rw-r--r-- |
17368 | 1 |
(* Title: Pure/General/stack.ML |
2 |
Author: Makarius |
|
3 |
||
4 |
Non-empty stacks. |
|
5 |
*) |
|
6 |
||
7 |
signature STACK = |
|
8 |
sig |
|
58797 | 9 |
type 'a T |
10 |
val make: 'a -> 'a list -> 'a T |
|
11 |
val dest: 'a T -> 'a * 'a list |
|
17368 | 12 |
val level: 'a T -> int |
13 |
val init: 'a -> 'a T |
|
14 |
val top: 'a T -> 'a |
|
21272 | 15 |
val map_top: ('a -> 'a) -> 'a T -> 'a T |
16 |
val map_all: ('a -> 'a) -> 'a T -> 'a T |
|
17368 | 17 |
val push: 'a T -> 'a T |
47060
e2741ec9ae36
prefer explicitly qualified exception List.Empty;
wenzelm
parents:
29606
diff
changeset
|
18 |
val pop: 'a T -> 'a T (*exception List.Empty*) |
17368 | 19 |
end; |
20 |
||
21 |
structure Stack: STACK = |
|
22 |
struct |
|
23 |
||
58797 | 24 |
abstype 'a T = Stack of 'a * 'a list |
25 |
with |
|
17368 | 26 |
|
58797 | 27 |
fun make x xs = Stack (x, xs); |
28 |
fun dest (Stack (x, xs)) = (x, xs); |
|
17368 | 29 |
|
58797 | 30 |
fun level (Stack (_, xs)) = length xs; |
31 |
||
32 |
fun init x = Stack (x, []); |
|
17368 | 33 |
|
58797 | 34 |
fun top (Stack (x, _)) = x; |
21272 | 35 |
|
58797 | 36 |
fun map_top f (Stack (x, xs)) = Stack (f x, xs); |
17368 | 37 |
|
58797 | 38 |
fun map_all f (Stack (x, xs)) = Stack (f x, map f xs); |
17368 | 39 |
|
58797 | 40 |
fun push (Stack (x, xs)) = Stack (x, x :: xs); |
41 |
||
42 |
fun pop (Stack (_, x :: xs)) = Stack (x, xs) |
|
43 |
| pop (Stack (_, [])) = raise List.Empty; |
|
17368 | 44 |
|
45 |
end; |
|
58797 | 46 |
|
47 |
end; |