Debian Codegraph @

ide/rocqide/document.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 exception Empty
12
13 let invalid_arg s = raise (Invalid_argument ("Document."^s))
14
15 type 'a sentence = { mutable state_id : Stateid.t option; data : 'a }
16
17 type id = Stateid.t
18
19 class type ['a] signals =
20 object
21 method popped : callback:('a -> ('a list * 'a list) option -> unit) -> unit
22 method pushed : callback:('a -> ('a list * 'a list) option -> unit) -> unit
23 end
24
25 class ['a] signal () =
26 object
27 val mutable attached : ('a -> unit) list = []
28 method call (x : 'a) =
29 let iter f = try f x with _ -> () in
30 List.iter iter attached
31 method connect f = attached <- f :: attached
32 end
33
34 type 'a document = {
35 mutable stack : 'a sentence list;
36 mutable context : ('a sentence list * 'a sentence list) option;
37 pushed_sig : ('a * ('a list * 'a list) option) signal;
38 popped_sig : ('a * ('a list * 'a list) option) signal;
39 mutable last_errors : string list
40 }
41
42 let connect d : 'a signals =
43 object
44 method pushed ~callback = d.pushed_sig#connect (fun (x, ctx) -> callback x ctx)
45 method popped ~callback = d.popped_sig#connect (fun (x, ctx) -> callback x ctx)
46 end
47
48 let create () = {
49 stack = [];
50 context = None;
51 pushed_sig = new signal ();
52 popped_sig = new signal ();
53 last_errors = []
54 }
55
56 let repr_context s = match s.context with
57 | None -> None
58 | Some (cl, cr) ->
59 let map s = s.data in
60 Some (List.map map cl, List.map map cr)
61
62 (* Invariant, only the tip is a allowed to have state_id = None *)
63 let invariant l = l = [] || (List.hd l).state_id <> None
64
65 let tip = function
66 | { stack = [] } -> raise Empty
67 | { stack = { state_id = Some id }::_ } -> id
68 | { stack = { state_id = None }::_ } -> invalid_arg "tip"
69
70 let tip_data = function
71 | { stack = [] } -> raise Empty
72 | { stack = { data }::_ } -> data
73
74 let push d x =
75 assert(invariant d.stack);
76 d.stack <- { data = x; state_id = None } :: d.stack;
77 d.pushed_sig#call (x, repr_context d)
78
79 let pop = function
80 | { stack = [] } -> raise Empty
81 | { stack = { data }::xs } as s ->
82 s.stack <- xs; s.popped_sig#call (data, repr_context s); data
83
84 let set_errors d msgs = d.last_errors <- msgs
85 let get_errors d = d.last_errors
86
87 let focus d ~cond_top:c_start ~cond_bot:c_stop =
88 assert(invariant d.stack);
89 if d.context <> None then invalid_arg "focus";
90 let rec aux (a,s,b) grab = function
91 | [] -> invalid_arg "focus"
92 | { state_id = Some id; data } as x :: xs when not grab ->
93 if c_start id data then aux (a,s,b) true (x::xs)
94 else aux (x::a,s,b) grab xs
95 | { state_id = Some id; data } as x :: xs ->
96 if c_stop id data then List.rev a, List.rev (x::s), xs
97 else aux (a,x::s,b) grab xs
98 | _ -> assert false in
99 let a, s, b = aux ([],[],[]) false d.stack in
100 d.stack <- s;
101 d.context <- Some (a, b)
102
103 let unfocus = function
104 | { context = None } -> invalid_arg "unfocus"
105 | { context = Some (a,b); stack } as d ->
106 assert(invariant stack);
107 d.context <- None;
108 d.stack <- a @ stack @ b
109
110 let focused { context } = context <> None
111
112 let to_lists = function
113 | { context = None; stack = s } -> [],s,[]
114 | { context = Some (a,b); stack = s } -> a,s,b
115
116 let flat f b = fun x -> f b x.state_id x.data
117
118 let find d f =
119 let a, s, b = to_lists d in
120 (
121 try List.find (flat f false) a with Not_found ->
122 try List.find (flat f true) s with Not_found ->
123 List.find (flat f false) b
124 ).data
125
126 let find_map d f =
127 let a, s, b = to_lists d in
128 try CList.find_map_exn (flat f false) a with Not_found ->
129 try CList.find_map_exn (flat f true) s with Not_found ->
130 CList.find_map_exn (flat f false) b
131
132 let is_empty = function
133 | { stack = []; context = None } -> true
134 | _ -> false
135
136 let context d =
137 let top, _, bot = to_lists d in
138 let pair _ x y = match x with
139 | None -> assert false
140 | Some x -> x, y
141 in
142 List.map (flat pair true) top, List.map (flat pair true) bot
143
144 let stateid_opt_equal = Option.equal Stateid.equal
145
146 let is_in_focus d id =
147 let _, focused, _ = to_lists d in
148 List.exists (fun { state_id } -> stateid_opt_equal state_id (Some id)) focused
149
150 let print d f =
151 let top, mid, bot = to_lists d in
152 let open Pp in
153 v 0
154 (List.fold_right (fun i acc -> acc ++ cut() ++ flat f false i) top
155 (List.fold_right (fun i acc -> acc ++ cut() ++ flat f true i) mid
156 (List.fold_right (fun i acc -> acc ++ cut() ++ flat f false i) bot (mt()))))
157
158 let assign_tip_id d id =
159 match d with
160 | { stack = { state_id = None } as top :: _ } -> top.state_id <- Some id
161 | _ -> invalid_arg "assign_tip_id"
162
163 let cut_at d id =
164 let aux (n, zone) { state_id; data } =
165 if stateid_opt_equal state_id (Some id) then CSig.Stop (n, zone)
166 else CSig.Cont (n + 1, data :: zone) in
167 let n, zone = CList.fold_left_until aux (0, []) d.stack in
168 for _i = 1 to n do ignore(pop d) done;
169 List.rev zone
170
171 let find_id d f =
172 let top, focus, bot = to_lists d in
173 let pred = function
174 | { state_id = Some id; data } when f id data -> Some id
175 | _ -> None in
176 try CList.find_map_exn pred top, true with Not_found ->
177 try CList.find_map_exn pred focus, false with Not_found ->
178 CList.find_map_exn pred bot, true
179
180 let before_tip d =
181 let _, focused, rest = to_lists d in
182 match focused with
183 | _:: { state_id = Some id } :: _ -> id, false
184 | _:: { state_id = None } :: _ -> assert false
185 | [] -> raise Not_found
186 | [_] ->
187 match rest with
188 | { state_id = Some id } :: _ -> id, true
189 | { state_id = None } :: _ -> assert false
190 | [] -> raise Not_found
191
192 let fold_all d a f =
193 let top, focused, bot = to_lists d in
194 let a = List.fold_left (fun a -> flat (f a) false) a top in
195 let a = List.fold_left (fun a -> flat (f a) true) a focused in
196 let a = List.fold_left (fun a -> flat (f a) false) a bot in
197 a