Debian Codegraph @

lib/util.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 (* Mapping under pairs *)
12
13 let on_fst f (a,b) = (f a,b)
14 let on_snd f (a,b) = (a,f b)
15 let map_pair f (a,b) = (f a,f b)
16
17 (* Folding under pairs *)
18
19 let fold_fst f acc (a,b) = let acc, a = f acc a in acc, (a, b)
20 let fold_snd f acc (a,b) = let acc, b = f acc b in acc, (a, b)
21
22 (* Equality on pairs *)
23
24 let eq_pair eq1 eq2 (a,b) (a',b') = eq1 a a' && eq2 b b'
25
26 (* Mapping under triplets *)
27
28 let on_pi1 f (a,b,c) = (f a,b,c)
29 let on_pi2 f (a,b,c) = (a,f b,c)
30 let on_pi3 f (a,b,c) = (a,b,f c)
31
32 (* Projections from triplets *)
33
34 let pi1 (a,_,_) = a
35 let pi2 (_,a,_) = a
36 let pi3 (_,_,a) = a
37
38 (* Characters *)
39
40 let is_letter c = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
41 let is_digit c = (c >= '0' && c <= '9')
42 let is_ident_tail c =
43 is_letter c || is_digit c || c = '\'' || c = '_'
44 let is_blank = function
45 | ' ' | '\r' | '\t' | '\n' -> true
46 | _ -> false
47
48 module Empty =
49 struct
50 type t = { abort : 'a. 'a }
51 let abort (x : t) = x.abort
52 end
53
54 (* Strings *)
55
56 module String = CString
57
58 let subst_command_placeholder s t =
59 let buff = Buffer.create (String.length s + String.length t) in
60 let i = ref 0 in
61 while (!i < String.length s) do
62 if s.[!i] = '%' && !i+1 < String.length s && s.[!i+1] = 's'
63 then (Buffer.add_string buff t;incr i)
64 else Buffer.add_char buff s.[!i];
65 incr i
66 done;
67 Buffer.contents buff
68
69 (* Lists *)
70
71 module List = CList
72
73 let (@) = CList.append
74
75 (* Arrays *)
76
77 module Array = CArray
78
79 (* Sets *)
80
81 module Set = CSet
82
83 (* Maps *)
84
85 module Map = CMap
86
87 (* Matrices *)
88
89 let matrix_transpose mat =
90 List.fold_right (List.map2 (fun p c -> p::c)) mat
91 (if List.is_empty mat then [] else List.map (fun _ -> []) (List.hd mat))
92
93 (* Functions *)
94
95 let identity x = x
96
97 (** Left-to-right function composition:
98
99 [f1 %> f2] is [fun x -> f2 (f1 x)].
100
101 [f1 %> f2 %> f3] is [fun x -> f3 (f2 (f1 x))].
102
103 [f1 %> f2 %> f3 %> f4] is [fun x -> f4 (f3 (f2 (f1 x)))]
104
105 etc.
106 *)
107 let (%>) f g x = g (f x)
108
109 let const x _ = x
110
111 let iterate =
112 let rec iterate_f f n x =
113 if n <= 0 then x else iterate_f f (pred n) (f x)
114 in
115 iterate_f
116
117 let repeat n f x =
118 let rec loop i = if i <> 0 then (f x; loop (i - 1)) in loop n
119
120 let app_opt f x =
121 match f with
122 | Some f -> f x
123 | None -> x
124
125 (* Delayed computations *)
126
127 type 'a delayed = unit -> 'a
128
129 let delayed_force f = f ()
130
131 (* finalize - Credit X.Leroy, D.Remy. , adapted to Coq's exn handling *)
132 let with_finally finally y =
133 let acquire () = () in
134 let release () = finally y in
135 Memprof_coq.Masking.with_resource ~acquire () ~release
136
137 let try_finally f x finally y =
138 let open Memprof_coq.Resource_bind in
139 let& () = with_finally finally y in
140 f x
141
142 let with_flag_value r =
143 let acquire () = !r in
144 let release r_val = r := r_val in
145 Memprof_coq.Masking.with_resource ~acquire () ~release
146
147 let protect_ref ~scope r =
148 let scope _ = scope () in
149 with_flag_value ~scope r
150
151 let protect_state ~freeze ~unfreeze ~scope =
152 let acquire () = freeze () in
153 let release p_st = unfreeze p_st in
154 let scope _ = scope () in
155 Memprof_coq.Masking.with_resource ~acquire () ~release ~scope
156
157 let atomify f x =
158 let acquire () = f x in
159 let release _ = () in
160 let scope r = r in
161 Memprof_coq.Masking.with_resource ~acquire () ~release ~scope
162
163 (* Misc *)
164
165 type ('a, 'b) union = ('a, 'b) CSig.union = Inl of 'a | Inr of 'b
166 type 'a until = 'a CSig.until = Stop of 'a | Cont of 'a
167 type ('a, 'b) eq = ('a, 'b) CSig.eq = Refl : ('a, 'a) eq
168
169 let sym : type a b. (a, b) eq -> (b, a) eq = fun Refl -> Refl
170
171 module Union =
172 struct
173 let map f g = function
174 | Inl a -> Inl (f a)
175 | Inr b -> Inr (g b)
176
177 (** Lifting equality onto union types. *)
178 let equal f g x y = match x, y with
179 | Inl x, Inl y -> f x y
180 | Inr x, Inr y -> g x y
181 | _, _ -> false
182
183 let fold_left f g a = function
184 | Inl y -> f a y
185 | Inr y -> g a y
186 end
187
188 module Compare = struct
189 type list = [] | (::) : (('a -> 'a -> int) * 'a * 'a) * list -> list
190
191 let rec compare = function
192 | [] -> 0
193 | (cmp,x,y) :: rest ->
194 let c = cmp x y in
195 if c <> 0 then c
196 else compare rest
197
198 end
199
200 let map_union = Union.map
201
202 let open_utf8_file_in fname =
203 let is_bom s =
204 Int.equal (Char.code (Bytes.get s 0)) 0xEF &&
205 Int.equal (Char.code (Bytes.get s 1)) 0xBB &&
206 Int.equal (Char.code (Bytes.get s 2)) 0xBF
207 in
208 let in_chan = open_in fname in
209 let s = Bytes.make 3 ' ' in
210 if input in_chan s 0 3 < 3 || not (is_bom s) then seek_in in_chan 0;
211 in_chan
212
213 (** A trick which can typically be used to store on the fly the
214 computation of values in the "when" clause of a "match" then
215 retrieve the evaluated result in the r.h.s of the clause *)
216
217 let set_temporary_memory () =
218 let a = ref None in
219 (fun x -> assert (!a = None); a := Some x; x),
220 (fun () -> match !a with Some x -> x | None -> assert false)