5039
|
1 |
(* Title: Pure/General/history.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Markus Wenzel, TU Muenchen
|
8806
|
4 |
License: GPL (GNU GENERAL PUBLIC LICENSE)
|
5039
|
5 |
|
6680
|
6 |
Histories of values, with undo and redo, and optional limit.
|
5039
|
7 |
*)
|
|
8 |
|
|
9 |
signature HISTORY =
|
|
10 |
sig
|
|
11 |
type 'a T
|
6680
|
12 |
val init: int option -> 'a -> 'a T
|
|
13 |
val is_initial: 'a T -> bool
|
5039
|
14 |
val current: 'a T -> 'a
|
7715
|
15 |
val clear: int -> 'a T -> 'a T
|
5039
|
16 |
val undo: 'a T -> 'a T
|
|
17 |
val redo: 'a T -> 'a T
|
6680
|
18 |
val apply_copy: ('a -> 'a) -> ('a -> 'a) -> 'a T -> 'a T
|
5039
|
19 |
val apply: ('a -> 'a) -> 'a T -> 'a T
|
6680
|
20 |
val map: ('a -> 'a) -> 'a T -> 'a T
|
5039
|
21 |
end;
|
|
22 |
|
|
23 |
structure History: HISTORY =
|
|
24 |
struct
|
|
25 |
|
|
26 |
|
|
27 |
(* datatype history *)
|
|
28 |
|
|
29 |
datatype 'a T =
|
6680
|
30 |
History of 'a * (int option * int * 'a list * 'a list);
|
5039
|
31 |
|
6680
|
32 |
fun init lim x = History (x, (lim, 0, [], []));
|
|
33 |
|
|
34 |
fun is_initial (History (_, (_, len, _, _))) = len = 0;
|
5039
|
35 |
|
6680
|
36 |
fun current (History (x, _)) = x;
|
|
37 |
|
7715
|
38 |
fun clear n (History (x, (lim, len, undo_list, redo_list))) =
|
|
39 |
History (x, (lim, Int.max (0, len - n), drop (n, undo_list), redo_list));
|
5039
|
40 |
|
6680
|
41 |
fun undo (History (_, (_, _, [], _))) = error "No further undo information"
|
|
42 |
| undo (History (x, (lim, len, u :: undo_list, redo_list))) =
|
|
43 |
History (u, (lim, len - 1, undo_list, x :: redo_list));
|
5039
|
44 |
|
6680
|
45 |
fun redo (History (_, (_, _, _, []))) = error "No further redo information"
|
|
46 |
| redo (History (x, (lim, len, undo_list, r :: redo_list))) =
|
|
47 |
History (r, (lim, len + 1, x :: undo_list, redo_list));
|
|
48 |
|
|
49 |
fun push None _ x xs = x :: xs
|
|
50 |
| push (Some 0) _ _ _ = []
|
|
51 |
| push (Some n) len x xs = if len < n then x :: xs else take (n, x :: xs);
|
5039
|
52 |
|
6680
|
53 |
fun apply_copy cp f (History (x, (lim, len, undo_list, _))) =
|
|
54 |
let val x' = cp x
|
|
55 |
in History (f x, (lim, len + 1, push lim len x' undo_list, [])) end;
|
5039
|
56 |
|
6680
|
57 |
fun apply f = apply_copy I f;
|
|
58 |
|
|
59 |
fun map f (History (x, hist)) = History (f x, hist);
|
5039
|
60 |
|
|
61 |
|
|
62 |
end;
|