author | desharna |
Mon, 10 Oct 2022 13:42:14 +0200 | |
changeset 76255 | b3ff4f171eda |
parent 75859 | 7164f537370f |
child 76788 | ce44e714d573 |
permissions | -rw-r--r-- |
34136 | 1 |
/* Title: Pure/library.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Basic library. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
38258 | 9 |
|
63781 | 10 |
import scala.annotation.tailrec |
51981
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
11 |
import scala.collection.mutable |
59224 | 12 |
import scala.util.matching.Regex |
51981
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
13 |
|
37018 | 14 |
|
75393 | 15 |
object Library { |
63789 | 16 |
/* resource management */ |
17 |
||
75393 | 18 |
def using[A <: AutoCloseable, B](a: A)(f: A => B): B = { |
69393
ed0824ef337e
static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
wenzelm
parents:
68715
diff
changeset
|
19 |
try { f(a) } |
ed0824ef337e
static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
wenzelm
parents:
68715
diff
changeset
|
20 |
finally { if (a != null) a.close() } |
63789 | 21 |
} |
22 |
||
23 |
||
57909
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
24 |
/* integers */ |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
25 |
|
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
26 |
private val small_int = 10000 |
75393 | 27 |
private lazy val small_int_table = { |
57909
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
28 |
val array = new Array[String](small_int) |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
29 |
for (i <- 0 until small_int) array(i) = i.toString |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
30 |
array |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
31 |
} |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
32 |
|
75393 | 33 |
def is_small_int(s: String): Boolean = { |
57909
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
34 |
val len = s.length |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
35 |
1 <= len && len <= 4 && |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
36 |
s.forall(c => '0' <= c && c <= '9') && |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
37 |
(len == 1 || s(0) != '0') |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
38 |
} |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
39 |
|
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
40 |
def signed_string_of_long(i: Long): String = |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
41 |
if (0 <= i && i < small_int) small_int_table(i.toInt) |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
42 |
else i.toString |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
43 |
|
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
44 |
def signed_string_of_int(i: Int): String = |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
45 |
if (0 <= i && i < small_int) small_int_table(i) |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
46 |
else i.toString |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
47 |
|
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
48 |
|
48996 | 49 |
/* separated chunks */ |
36688 | 50 |
|
75393 | 51 |
def separate[A](s: A, list: List[A]): List[A] = { |
51981
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
52 |
val result = new mutable.ListBuffer[A] |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
53 |
var first = true |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
54 |
for (x <- list) { |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
55 |
if (first) { |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
56 |
first = false |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
57 |
result += x |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
58 |
} |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
59 |
else { |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
60 |
result += s |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
61 |
result += x |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
62 |
} |
36688 | 63 |
} |
51981
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
64 |
result.toList |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
65 |
} |
36688 | 66 |
|
56600 | 67 |
def separated_chunks(sep: Char => Boolean, source: CharSequence): Iterator[CharSequence] = |
48996 | 68 |
new Iterator[CharSequence] { |
69 |
private val end = source.length |
|
75393 | 70 |
private def next_chunk(i: Int): Option[(CharSequence, Int)] = { |
48996 | 71 |
if (i < end) { |
75382
81673c441ce3
tuned: eliminted do-while for the sake of scala3;
wenzelm
parents:
75295
diff
changeset
|
72 |
var j = i |
75709 | 73 |
while ({ |
75382
81673c441ce3
tuned: eliminted do-while for the sake of scala3;
wenzelm
parents:
75295
diff
changeset
|
74 |
j += 1 |
75709 | 75 |
j < end && !sep(source.charAt(j)) |
76 |
}) () |
|
48996 | 77 |
Some((source.subSequence(i + 1, j), j)) |
78 |
} |
|
79 |
else None |
|
43598 | 80 |
} |
48996 | 81 |
private var state: Option[(CharSequence, Int)] = if (end == 0) None else next_chunk(-1) |
82 |
||
73337 | 83 |
def hasNext: Boolean = state.isDefined |
48996 | 84 |
def next(): CharSequence = |
85 |
state match { |
|
60215 | 86 |
case Some((s, i)) => state = next_chunk(i); s |
48996 | 87 |
case None => Iterator.empty.next() |
88 |
} |
|
43598 | 89 |
} |
90 |
||
48996 | 91 |
def space_explode(sep: Char, str: String): List[String] = |
56600 | 92 |
separated_chunks(_ == sep, str).map(_.toString).toList |
48996 | 93 |
|
94 |
||
95 |
/* lines */ |
|
96 |
||
75859
7164f537370f
proper treatment of empty lines (amending 08f89f0e8a62);
wenzelm
parents:
75709
diff
changeset
|
97 |
def terminate_lines(lines: IterableOnce[String]): String = { |
7164f537370f
proper treatment of empty lines (amending 08f89f0e8a62);
wenzelm
parents:
75709
diff
changeset
|
98 |
val it = lines.iterator |
7164f537370f
proper treatment of empty lines (amending 08f89f0e8a62);
wenzelm
parents:
75709
diff
changeset
|
99 |
if (it.isEmpty) "" else it.mkString("", "\n", "\n") |
7164f537370f
proper treatment of empty lines (amending 08f89f0e8a62);
wenzelm
parents:
75709
diff
changeset
|
100 |
} |
51983
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
101 |
|
73362 | 102 |
def cat_lines(lines: IterableOnce[String]): String = |
103 |
lines.iterator.mkString("\n") |
|
48996 | 104 |
|
43670
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
105 |
def split_lines(str: String): List[String] = space_explode('\n', str) |
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
106 |
|
62590 | 107 |
def prefix_lines(prfx: String, str: String): String = |
73963 | 108 |
isabelle.setup.Library.prefix_lines(prfx, str) |
62590 | 109 |
|
73736 | 110 |
def indent_lines(n: Int, str: String): String = |
111 |
prefix_lines(Symbol.spaces(n), str) |
|
112 |
||
75393 | 113 |
def first_line(source: CharSequence): String = { |
56600 | 114 |
val lines = separated_chunks(_ == '\n', source) |
73344 | 115 |
if (lines.hasNext) lines.next().toString |
48996 | 116 |
else "" |
117 |
} |
|
118 |
||
73332 | 119 |
def trim_line(s: String): String = |
73963 | 120 |
isabelle.setup.Library.trim_line(s) |
73332 | 121 |
|
122 |
def trim_split_lines(s: String): List[String] = |
|
123 |
split_lines(trim_line(s)).map(trim_line) |
|
124 |
||
65932 | 125 |
def encode_lines(s: String): String = s.replace('\n', '\u000b') |
126 |
def decode_lines(s: String): String = s.replace('\u000b', '\n') |
|
127 |
||
50847 | 128 |
|
129 |
/* strings */ |
|
130 |
||
75393 | 131 |
def make_string(f: StringBuilder => Unit, capacity: Int = 16): String = { |
74794
c606fddc5b05
slightly faster XML output: avoid too much regrowing of StringBuilder;
wenzelm
parents:
73963
diff
changeset
|
132 |
val s = new StringBuilder(capacity) |
64355 | 133 |
f(s) |
134 |
s.toString |
|
135 |
} |
|
136 |
||
50847 | 137 |
def try_unprefix(prfx: String, s: String): Option[String] = |
138 |
if (s.startsWith(prfx)) Some(s.substring(prfx.length)) else None |
|
139 |
||
55033 | 140 |
def try_unsuffix(sffx: String, s: String): Option[String] = |
141 |
if (s.endsWith(sffx)) Some(s.substring(0, s.length - sffx.length)) else None |
|
142 |
||
65606 | 143 |
def perhaps_unprefix(prfx: String, s: String): String = try_unprefix(prfx, s) getOrElse s |
144 |
def perhaps_unsuffix(sffx: String, s: String): String = try_unsuffix(sffx, s) getOrElse s |
|
145 |
||
65903 | 146 |
def isolate_substring(s: String): String = new String(s.toCharArray) |
64820
00488a8c042f
Line.Document consists of independently allocated strings;
wenzelm
parents:
64370
diff
changeset
|
147 |
|
71864 | 148 |
def strip_ansi_color(s: String): String = |
73355 | 149 |
s.replaceAll("\u001b\\[\\d+m", "") |
71864 | 150 |
|
43598 | 151 |
|
48996 | 152 |
/* quote */ |
46196 | 153 |
|
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67436
diff
changeset
|
154 |
def single_quote(s: String): String = "'" + s + "'" |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67436
diff
changeset
|
155 |
|
43598 | 156 |
def quote(s: String): String = "\"" + s + "\"" |
56843
b2bfcd8cda80
support for path completion based on file-system content;
wenzelm
parents:
56730
diff
changeset
|
157 |
|
b2bfcd8cda80
support for path completion based on file-system content;
wenzelm
parents:
56730
diff
changeset
|
158 |
def try_unquote(s: String): Option[String] = |
b2bfcd8cda80
support for path completion based on file-system content;
wenzelm
parents:
56730
diff
changeset
|
159 |
if (s.startsWith("\"") && s.endsWith("\"")) Some(s.substring(1, s.length - 1)) |
b2bfcd8cda80
support for path completion based on file-system content;
wenzelm
parents:
56730
diff
changeset
|
160 |
else None |
b2bfcd8cda80
support for path completion based on file-system content;
wenzelm
parents:
56730
diff
changeset
|
161 |
|
58592 | 162 |
def perhaps_unquote(s: String): String = try_unquote(s) getOrElse s |
163 |
||
43598 | 164 |
def commas(ss: Iterable[String]): String = ss.iterator.mkString(", ") |
48362 | 165 |
def commas_quote(ss: Iterable[String]): String = ss.iterator.map(quote).mkString(", ") |
43598 | 166 |
|
36688 | 167 |
|
51983
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
168 |
/* CharSequence */ |
34141 | 169 |
|
75393 | 170 |
class Reverse(text: CharSequence, start: Int, end: Int) extends CharSequence { |
73120
c3589f2dff31
more informative errors: simplify diagnosis of spurious failures reported by users;
wenzelm
parents:
72214
diff
changeset
|
171 |
require(0 <= start && start <= end && end <= text.length, "bad reverse range") |
34141 | 172 |
|
173 |
def this(text: CharSequence) = this(text, 0, text.length) |
|
174 |
||
175 |
def length: Int = end - start |
|
176 |
def charAt(i: Int): Char = text.charAt(end - i - 1) |
|
177 |
||
178 |
def subSequence(i: Int, j: Int): CharSequence = |
|
179 |
if (0 <= i && i <= j && j <= length) new Reverse(text, end - j, end - i) |
|
180 |
else throw new IndexOutOfBoundsException |
|
181 |
||
75393 | 182 |
override def toString: String = { |
34141 | 183 |
val buf = new StringBuilder(length) |
184 |
for (i <- 0 until length) |
|
185 |
buf.append(charAt(i)) |
|
186 |
buf.toString |
|
187 |
} |
|
188 |
} |
|
189 |
||
75393 | 190 |
class Line_Termination(text: CharSequence) extends CharSequence { |
51983
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
191 |
def length: Int = text.length + 1 |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
192 |
def charAt(i: Int): Char = if (i == text.length) '\n' else text.charAt(i) |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
193 |
def subSequence(i: Int, j: Int): CharSequence = |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
194 |
if (j == text.length + 1) new Line_Termination(text.subSequence(i, j - 1)) |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
195 |
else text.subSequence(i, j) |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
196 |
override def toString: String = text.toString + "\n" |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
197 |
} |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
198 |
|
34141 | 199 |
|
59224 | 200 |
/* regular expressions */ |
201 |
||
202 |
def make_regex(s: String): Option[Regex] = |
|
203 |
try { Some(new Regex(s)) } catch { case ERROR(_) => None } |
|
204 |
||
64871 | 205 |
def is_regex_meta(c: Char): Boolean = """()[]{}\^$|?*+.<>-=!""".contains(c) |
206 |
||
207 |
def escape_regex(s: String): String = |
|
71601 | 208 |
if (s.exists(is_regex_meta)) { |
64871 | 209 |
(for (c <- s.iterator) |
210 |
yield { if (is_regex_meta(c)) "\\" + c.toString else c.toString }).mkString |
|
211 |
} |
|
212 |
else s |
|
213 |
||
59224 | 214 |
|
61883
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
wenzelm
parents:
60215
diff
changeset
|
215 |
/* lists */ |
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
wenzelm
parents:
60215
diff
changeset
|
216 |
|
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
wenzelm
parents:
60215
diff
changeset
|
217 |
def take_prefix[A](pred: A => Boolean, xs: List[A]): (List[A], List[A]) = |
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
wenzelm
parents:
60215
diff
changeset
|
218 |
(xs.takeWhile(pred), xs.dropWhile(pred)) |
56686 | 219 |
|
75393 | 220 |
def take_suffix[A](pred: A => Boolean, xs: List[A]): (List[A], List[A]) = { |
67434 | 221 |
val rev_xs = xs.reverse |
222 |
(rev_xs.dropWhile(pred).reverse, rev_xs.takeWhile(pred).reverse) |
|
223 |
} |
|
224 |
||
225 |
def trim[A](pred: A => Boolean, xs: List[A]): List[A] = |
|
226 |
take_suffix(pred, take_prefix(pred, xs)._2)._1 |
|
227 |
||
60215 | 228 |
def member[A, B](xs: List[A])(x: B): Boolean = xs.contains(x) |
56688 | 229 |
def insert[A](x: A)(xs: List[A]): List[A] = if (xs.contains(x)) xs else x :: xs |
230 |
def remove[A, B](x: B)(xs: List[A]): List[A] = if (member(xs)(x)) xs.filterNot(_ == x) else xs |
|
231 |
def update[A](x: A)(xs: List[A]): List[A] = x :: remove(x)(xs) |
|
63734
133e3e84e6fb
some support for merge of Isabelle/jEdit shortcuts wrt. jEdit keymap;
wenzelm
parents:
62590
diff
changeset
|
232 |
|
63867 | 233 |
def merge[A](xs: List[A], ys: List[A]): List[A] = |
234 |
if (xs.eq(ys)) xs |
|
235 |
else if (xs.isEmpty) ys |
|
236 |
else ys.foldRight(xs)(Library.insert(_)(_)) |
|
237 |
||
75393 | 238 |
def distinct[A](xs: List[A], eq: (A, A) => Boolean = (x: A, y: A) => x == y): List[A] = { |
63734
133e3e84e6fb
some support for merge of Isabelle/jEdit shortcuts wrt. jEdit keymap;
wenzelm
parents:
62590
diff
changeset
|
239 |
val result = new mutable.ListBuffer[A] |
64207 | 240 |
xs.foreach(x => if (!result.exists(y => eq(x, y))) result += x) |
63734
133e3e84e6fb
some support for merge of Isabelle/jEdit shortcuts wrt. jEdit keymap;
wenzelm
parents:
62590
diff
changeset
|
241 |
result.toList |
133e3e84e6fb
some support for merge of Isabelle/jEdit shortcuts wrt. jEdit keymap;
wenzelm
parents:
62590
diff
changeset
|
242 |
} |
63781 | 243 |
|
75393 | 244 |
def duplicates[A](lst: List[A], eq: (A, A) => Boolean = (x: A, y: A) => x == y): List[A] = { |
63781 | 245 |
val result = new mutable.ListBuffer[A] |
246 |
@tailrec def dups(rest: List[A]): Unit = |
|
247 |
rest match { |
|
248 |
case Nil => |
|
249 |
case x :: xs => |
|
64207 | 250 |
if (!result.exists(y => eq(x, y)) && xs.exists(y => eq(x, y))) result += x |
63781 | 251 |
dups(xs) |
252 |
} |
|
253 |
dups(lst) |
|
254 |
result.toList |
|
255 |
} |
|
65761 | 256 |
|
68715 | 257 |
def replicate[A](n: Int, a: A): List[A] = |
258 |
if (n < 0) throw new IllegalArgumentException |
|
259 |
else if (n == 0) Nil |
|
260 |
else { |
|
261 |
val res = new mutable.ListBuffer[A] |
|
262 |
(1 to n).foreach(_ => res += a) |
|
263 |
res.toList |
|
264 |
} |
|
265 |
||
73571 | 266 |
def the_single[A](xs: List[A]): A = |
267 |
xs match { |
|
268 |
case List(x) => x |
|
269 |
case _ => error("Single argument expected") |
|
270 |
} |
|
271 |
||
65761 | 272 |
|
273 |
/* proper values */ |
|
274 |
||
75295
38398766be6b
command-line arguments for "isabelle vscode", similar to "isabelle jedit";
wenzelm
parents:
74794
diff
changeset
|
275 |
def proper_bool(b: Boolean): Option[Boolean] = |
38398766be6b
command-line arguments for "isabelle vscode", similar to "isabelle jedit";
wenzelm
parents:
74794
diff
changeset
|
276 |
if (!b) None else Some(b) |
38398766be6b
command-line arguments for "isabelle vscode", similar to "isabelle jedit";
wenzelm
parents:
74794
diff
changeset
|
277 |
|
65761 | 278 |
def proper_string(s: String): Option[String] = |
279 |
if (s == null || s == "") None else Some(s) |
|
280 |
||
281 |
def proper_list[A](list: List[A]): Option[List[A]] = |
|
282 |
if (list == null || list.isEmpty) None else Some(list) |
|
72214 | 283 |
|
284 |
||
285 |
/* reflection */ |
|
286 |
||
75393 | 287 |
def is_subclass[A, B](a: Class[A], b: Class[B]): Boolean = { |
73339 | 288 |
import scala.language.existentials |
75393 | 289 |
@tailrec def subclass(c: Class[_]): Boolean = { |
290 |
c == b || { val d = c.getSuperclass; d != null && subclass(d) } |
|
72214 | 291 |
} |
292 |
subclass(a) |
|
293 |
} |
|
34136 | 294 |
} |