author | blanchet |
Fri, 20 Dec 2013 20:36:38 +0100 | |
changeset 54838 | 16511f84913c |
parent 49063 | f93443defa6c |
child 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 |
|
28129 | 9 |
type 'a T = 'a * 'a list |
17368 | 10 |
val level: 'a T -> int |
11 |
val init: 'a -> 'a T |
|
12 |
val top: 'a T -> 'a |
|
21272 | 13 |
val map_top: ('a -> 'a) -> 'a T -> 'a T |
14 |
val map_all: ('a -> 'a) -> 'a T -> 'a T |
|
17368 | 15 |
val push: 'a T -> 'a T |
47060
e2741ec9ae36
prefer explicitly qualified exception List.Empty;
wenzelm
parents:
29606
diff
changeset
|
16 |
val pop: 'a T -> 'a T (*exception List.Empty*) |
17368 | 17 |
end; |
18 |
||
19 |
structure Stack: STACK = |
|
20 |
struct |
|
21 |
||
22 |
type 'a T = 'a * 'a list; |
|
23 |
||
24 |
fun level (_, xs) = length xs; |
|
25 |
||
26 |
fun init x = (x, []); |
|
27 |
||
28 |
fun top (x, _) = x; |
|
29 |
||
21272 | 30 |
fun map_top f (x, xs) = (f x, xs); |
31 |
||
32 |
fun map_all f (x, xs) = (f x, map f xs); |
|
17368 | 33 |
|
34 |
fun push (x, xs) = (x, x :: xs); |
|
35 |
||
36 |
fun pop (_, x :: xs) = (x, xs) |
|
47060
e2741ec9ae36
prefer explicitly qualified exception List.Empty;
wenzelm
parents:
29606
diff
changeset
|
37 |
| pop (_, []) = raise List.Empty; |
17368 | 38 |
|
39 |
end; |