1 (* Title: Pure/General/balanced_tree.ML
3 Author: Lawrence C Paulson and Makarius
8 signature BALANCED_TREE =
10 val make: ('a * 'a -> 'a) -> 'a list -> 'a
11 val dest: ('a -> 'a * 'a) -> int -> 'a -> 'a list
12 val access: {left: 'a -> 'a, right: 'a -> 'a, init: 'a} -> int -> int -> 'a
13 val accesses: {left: 'a -> 'a, right: 'a -> 'a, init: 'a} -> int -> 'a list
16 structure BalancedTree: BALANCED_TREE =
19 fun make _ [] = raise Empty
23 val m = length xs div 2;
24 val (ps, qs) = chop m xs;
25 in f (make f ps, make f qs) end;
28 if n <= 0 then raise Empty
29 else if n = 1 then [x]
33 val (left, right) = f x;
34 in dest f m left @ dest f (n - m) right end;
36 (*construct something of the form f(...g(...(x)...)) for balanced access*)
37 fun access {left = f, right = g, init = x} len i =
41 let val m = n div 2 in
42 if i <= m then f (acc m i)
43 else g (acc (n - m) (i - m))
45 in if 1 <= i andalso i <= len then acc len i else raise Subscript end;
47 (*construct ALL such accesses; could try harder to share recursive calls!*)
48 fun accesses {left = f, right = g, init = x} len =
54 val accs_left = acc m;
56 if n - m = m then accs_left
58 in map f accs_left @ map g accs_right end;
59 in if 1 <= len then acc len else raise Subscript end;