17368
|
1 |
(* Title: Pure/General/stack.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Makarius
|
|
4 |
|
|
5 |
Non-empty stacks.
|
|
6 |
*)
|
|
7 |
|
|
8 |
signature STACK =
|
|
9 |
sig
|
17756
|
10 |
type 'a T (*= 'a * 'a list*)
|
17368
|
11 |
val level: 'a T -> int
|
|
12 |
val init: 'a -> 'a T
|
|
13 |
val top: 'a T -> 'a
|
21272
|
14 |
val map_top: ('a -> 'a) -> 'a T -> 'a T
|
|
15 |
val map_all: ('a -> 'a) -> 'a T -> 'a T
|
17368
|
16 |
val push: 'a T -> 'a T
|
|
17 |
val pop: 'a T -> 'a T (*exception Empty*)
|
|
18 |
end;
|
|
19 |
|
|
20 |
structure Stack: STACK =
|
|
21 |
struct
|
|
22 |
|
|
23 |
type 'a T = 'a * 'a list;
|
|
24 |
|
|
25 |
fun level (_, xs) = length xs;
|
|
26 |
|
|
27 |
fun init x = (x, []);
|
|
28 |
|
|
29 |
fun top (x, _) = x;
|
|
30 |
|
21272
|
31 |
fun map_top f (x, xs) = (f x, xs);
|
|
32 |
|
|
33 |
fun map_all f (x, xs) = (f x, map f xs);
|
17368
|
34 |
|
|
35 |
fun push (x, xs) = (x, x :: xs);
|
|
36 |
|
|
37 |
fun pop (_, x :: xs) = (x, xs)
|
|
38 |
| pop (_, []) = raise Empty;
|
|
39 |
|
|
40 |
end;
|