Source file bl.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
type 'a tree = Leaf of 'a | Node of 'a * 'a tree * 'a tree
type 'a spine = Nil | Tree of int * 'a tree * 'a spine
type 'a t = { front : 'a list; rear : 'a spine }
let empty () = { front = []; rear = Nil }
let cons x l = { l with front = x :: l.front }
let snoc spine x =
match spine with
| Tree (w1, t1, Tree (w2, t2, rest)) when w1 = w2 -> Tree ((2 * w1) + 1, Node (x, t2, t1), rest)
| s -> Tree (1, Leaf x, s)
let rcons x l = { l with rear = snoc l.rear x }
type 'a stack = Done | Emit of 'a * 'a stack | Visit of 'a tree * 'a stack
type 'a scan = 'a list * 'a stack
let rec push t k =
match t with
| Leaf x -> Emit (x, k)
| Node (x, l, r) -> push l (Visit (r, Emit (x, k)))
let normalize = function Visit (t, k) -> push t k | k -> k
let stack_of_spine s =
let rec aux k = function Nil -> k | Tree (_, t, s) -> aux (Visit (t, k)) s in
normalize (aux Done s)
let to_scan { front; rear } = (front, stack_of_spine rear)
let is_empty = function [], Done -> true | _ -> false
let next = function
| x :: xs, k -> (x, (xs, k))
| [], Emit (x, k) -> (x, ([], normalize k))
| [], (Done | Visit _) -> assert false
let to_list s =
let rec aux acc s =
if is_empty s then List.rev acc
else
let x, s = next s in
aux (x :: acc) s
in
aux [] s
let of_list l = (l, Done)
let length s =
let rec aux n s = if is_empty s then n else aux (n + 1) (snd (next s)) in
aux 0 s
let rec tree_exists test = function
| Leaf x -> test x
| Node (x, l, r) -> tree_exists test l || tree_exists test r || test x
let tree_to_list t = to_list ([], normalize (Visit (t, Done)))
let edit_list test rewrite l =
let rec aux acc = function
| [] -> assert false
| y :: ys -> if test y then List.rev_append acc (rewrite y ys) else aux (y :: acc) ys
in
aux [] l
let rec edit_tree test rewrite w t older =
match t with
| Leaf y -> List.fold_left snoc older (rewrite y [])
| Node (x, l, r) ->
let w = (w - 1) / 2 in
if tree_exists test l then
let s = edit_tree test rewrite w l older in
snoc (List.fold_left snoc s (tree_to_list r)) x
else if tree_exists test r then snoc (edit_tree test rewrite w r (Tree (w, l, older))) x
else List.fold_left snoc (Tree (w, r, Tree (w, l, older))) (rewrite x [])
let rec edit_spine test rewrite = function
| Nil -> None
| Tree (w, t, older) -> (
match edit_spine test rewrite older with
| Some older -> Some (List.fold_left snoc older (tree_to_list t))
| None -> if tree_exists test t then Some (edit_tree test rewrite w t older) else None)
let edit test rewrite l =
if List.exists test l.front then Some { l with front = edit_list test rewrite l.front }
else Option.map (fun rear -> { l with rear }) (edit_spine test rewrite l.rear)
let replace f x l = Option.value (edit f (fun _ ys -> x :: ys) l) ~default:l
let remove f l = Option.value (edit f (fun _ ys -> ys) l) ~default:l
let insert f x l =
let test y = f y > 0 in
if List.exists test l.front then { l with front = edit_list test (fun y ys -> x :: y :: ys) l.front }
else
let before_rear = match stack_of_spine l.rear with Emit (y, _) -> test y | Done | Visit _ -> false in
if before_rear then { l with front = l.front @ [ x ] }
else
match edit_spine test (fun y ys -> x :: y :: ys) l.rear with
| Some rear -> { l with rear }
| None -> rcons x l
let pp pp_elt fmt l =
let pp_sep fmt () = Format.fprintf fmt ";@ " in
Format.fprintf fmt "@[<hov 2>[%a]@]" (Format.pp_print_list ~pp_sep pp_elt) (to_list (to_scan l))
let show pp_elt l = Format.asprintf "%a" (pp pp_elt) l