author | wenzelm |
Thu, 04 May 2017 12:15:50 +0200 | |
changeset 65712 | ddd6dfc28e80 |
parent 65606 | d2f83588080f |
child 65715 | e57e5935c6b4 |
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 |
|
34136 | 15 |
object Library |
16 |
{ |
|
63789 | 17 |
/* resource management */ |
18 |
||
19 |
def using[A <: { def close() }, B](x: A)(f: A => B): B = |
|
20 |
{ |
|
63926 | 21 |
import scala.language.reflectiveCalls |
22 |
||
63789 | 23 |
try { f(x) } |
24 |
finally { if (x != null) x.close() } |
|
25 |
} |
|
26 |
||
27 |
||
57909
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
28 |
/* integers */ |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
29 |
|
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
30 |
private val small_int = 10000 |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
31 |
private lazy val small_int_table = |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
32 |
{ |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
33 |
val array = new Array[String](small_int) |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
34 |
for (i <- 0 until small_int) array(i) = i.toString |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
35 |
array |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
36 |
} |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
37 |
|
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
38 |
def is_small_int(s: String): Boolean = |
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 |
val len = s.length |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
41 |
1 <= len && len <= 4 && |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
42 |
s.forall(c => '0' <= c && c <= '9') && |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
43 |
(len == 1 || s(0) != '0') |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
44 |
} |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
45 |
|
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
46 |
def signed_string_of_long(i: Long): String = |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
47 |
if (0 <= i && i < small_int) small_int_table(i.toInt) |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
48 |
else i.toString |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
49 |
|
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
50 |
def signed_string_of_int(i: Int): String = |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
51 |
if (0 <= i && i < small_int) small_int_table(i) |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
52 |
else i.toString |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
53 |
|
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
54 |
|
48996 | 55 |
/* separated chunks */ |
36688 | 56 |
|
57 |
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
|
58 |
{ |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
59 |
val result = new mutable.ListBuffer[A] |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
60 |
var first = true |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
61 |
for (x <- list) { |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
62 |
if (first) { |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
63 |
first = false |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
64 |
result += x |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
65 |
} |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
66 |
else { |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
67 |
result += s |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
68 |
result += x |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
69 |
} |
36688 | 70 |
} |
51981
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
71 |
result.toList |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
72 |
} |
36688 | 73 |
|
56600 | 74 |
def separated_chunks(sep: Char => Boolean, source: CharSequence): Iterator[CharSequence] = |
48996 | 75 |
new Iterator[CharSequence] { |
76 |
private val end = source.length |
|
77 |
private def next_chunk(i: Int): Option[(CharSequence, Int)] = |
|
78 |
{ |
|
79 |
if (i < end) { |
|
56600 | 80 |
var j = i; do j += 1 while (j < end && !sep(source.charAt(j))) |
48996 | 81 |
Some((source.subSequence(i + 1, j), j)) |
82 |
} |
|
83 |
else None |
|
43598 | 84 |
} |
48996 | 85 |
private var state: Option[(CharSequence, Int)] = if (end == 0) None else next_chunk(-1) |
86 |
||
87 |
def hasNext(): Boolean = state.isDefined |
|
88 |
def next(): CharSequence = |
|
89 |
state match { |
|
60215 | 90 |
case Some((s, i)) => state = next_chunk(i); s |
48996 | 91 |
case None => Iterator.empty.next() |
92 |
} |
|
43598 | 93 |
} |
94 |
||
48996 | 95 |
def space_explode(sep: Char, str: String): List[String] = |
56600 | 96 |
separated_chunks(_ == sep, str).map(_.toString).toList |
48996 | 97 |
|
98 |
||
99 |
/* lines */ |
|
100 |
||
64002 | 101 |
def terminate_lines(lines: TraversableOnce[String]): String = lines.mkString("", "\n", "\n") |
51983
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
102 |
|
48996 | 103 |
def cat_lines(lines: TraversableOnce[String]): String = lines.mkString("\n") |
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 = |
108 |
if (str == "") str |
|
109 |
else cat_lines(split_lines(str).map(s => prfx + s)) |
|
110 |
||
48996 | 111 |
def first_line(source: CharSequence): String = |
112 |
{ |
|
56600 | 113 |
val lines = separated_chunks(_ == '\n', source) |
48996 | 114 |
if (lines.hasNext) lines.next.toString |
115 |
else "" |
|
116 |
} |
|
117 |
||
50847 | 118 |
|
119 |
/* strings */ |
|
120 |
||
64355 | 121 |
def make_string(f: StringBuilder => Unit): String = |
122 |
{ |
|
123 |
val s = new StringBuilder |
|
124 |
f(s) |
|
125 |
s.toString |
|
126 |
} |
|
127 |
||
50847 | 128 |
def try_unprefix(prfx: String, s: String): Option[String] = |
129 |
if (s.startsWith(prfx)) Some(s.substring(prfx.length)) else None |
|
130 |
||
55033 | 131 |
def try_unsuffix(sffx: String, s: String): Option[String] = |
132 |
if (s.endsWith(sffx)) Some(s.substring(0, s.length - sffx.length)) else None |
|
133 |
||
65606 | 134 |
def perhaps_unprefix(prfx: String, s: String): String = try_unprefix(prfx, s) getOrElse s |
135 |
def perhaps_unsuffix(sffx: String, s: String): String = try_unsuffix(sffx, s) getOrElse s |
|
136 |
||
52444
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
51983
diff
changeset
|
137 |
def trim_line(s: String): String = |
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
51983
diff
changeset
|
138 |
if (s.endsWith("\r\n")) s.substring(0, s.length - 2) |
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
51983
diff
changeset
|
139 |
else if (s.endsWith("\r") || s.endsWith("\n")) s.substring(0, s.length - 1) |
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
51983
diff
changeset
|
140 |
else s |
2cfe6656d6d6
slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents:
51983
diff
changeset
|
141 |
|
64063 | 142 |
def trim_split_lines(s: String): List[String] = |
143 |
split_lines(trim_line(s)).map(trim_line(_)) |
|
144 |
||
64820
00488a8c042f
Line.Document consists of independently allocated strings;
wenzelm
parents:
64370
diff
changeset
|
145 |
def trim_substring(s: String): String = new String(s.toCharArray) |
00488a8c042f
Line.Document consists of independently allocated strings;
wenzelm
parents:
64370
diff
changeset
|
146 |
|
65712 | 147 |
def proper_string(s: String): Option[String] = |
148 |
if (s == "") None else Some(s) |
|
149 |
||
150 |
def proper_string_default(s: String, default: => String): String = |
|
151 |
if (s == "") default else s |
|
152 |
||
43598 | 153 |
|
48996 | 154 |
/* quote */ |
46196 | 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 |
|
170 |
class Reverse(text: CharSequence, start: Int, end: Int) extends CharSequence |
|
171 |
{ |
|
172 |
require(0 <= start && start <= end && end <= text.length) |
|
173 |
||
174 |
def this(text: CharSequence) = this(text, 0, text.length) |
|
175 |
||
176 |
def length: Int = end - start |
|
177 |
def charAt(i: Int): Char = text.charAt(end - i - 1) |
|
178 |
||
179 |
def subSequence(i: Int, j: Int): CharSequence = |
|
180 |
if (0 <= i && i <= j && j <= length) new Reverse(text, end - j, end - i) |
|
181 |
else throw new IndexOutOfBoundsException |
|
182 |
||
183 |
override def toString: String = |
|
184 |
{ |
|
185 |
val buf = new StringBuilder(length) |
|
186 |
for (i <- 0 until length) |
|
187 |
buf.append(charAt(i)) |
|
188 |
buf.toString |
|
189 |
} |
|
190 |
} |
|
191 |
||
51983
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
192 |
class Line_Termination(text: CharSequence) extends CharSequence |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
193 |
{ |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
194 |
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
|
195 |
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
|
196 |
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
|
197 |
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
|
198 |
else text.subSequence(i, j) |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
199 |
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
|
200 |
} |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
201 |
|
34141 | 202 |
|
59224 | 203 |
/* regular expressions */ |
204 |
||
205 |
def make_regex(s: String): Option[Regex] = |
|
206 |
try { Some(new Regex(s)) } catch { case ERROR(_) => None } |
|
207 |
||
64871 | 208 |
def is_regex_meta(c: Char): Boolean = """()[]{}\^$|?*+.<>-=!""".contains(c) |
209 |
||
210 |
def escape_regex(s: String): String = |
|
211 |
if (s.exists(is_regex_meta(_))) { |
|
212 |
(for (c <- s.iterator) |
|
213 |
yield { if (is_regex_meta(c)) "\\" + c.toString else c.toString }).mkString |
|
214 |
} |
|
215 |
else s |
|
216 |
||
59224 | 217 |
|
61883
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
wenzelm
parents:
60215
diff
changeset
|
218 |
/* lists */ |
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
wenzelm
parents:
60215
diff
changeset
|
219 |
|
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
wenzelm
parents:
60215
diff
changeset
|
220 |
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
|
221 |
(xs.takeWhile(pred), xs.dropWhile(pred)) |
56686 | 222 |
|
60215 | 223 |
def member[A, B](xs: List[A])(x: B): Boolean = xs.contains(x) |
56688 | 224 |
def insert[A](x: A)(xs: List[A]): List[A] = if (xs.contains(x)) xs else x :: xs |
225 |
def remove[A, B](x: B)(xs: List[A]): List[A] = if (member(xs)(x)) xs.filterNot(_ == x) else xs |
|
226 |
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
|
227 |
|
63867 | 228 |
def merge[A](xs: List[A], ys: List[A]): List[A] = |
229 |
if (xs.eq(ys)) xs |
|
230 |
else if (xs.isEmpty) ys |
|
231 |
else ys.foldRight(xs)(Library.insert(_)(_)) |
|
232 |
||
64207 | 233 |
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
|
234 |
{ |
133e3e84e6fb
some support for merge of Isabelle/jEdit shortcuts wrt. jEdit keymap;
wenzelm
parents:
62590
diff
changeset
|
235 |
val result = new mutable.ListBuffer[A] |
64207 | 236 |
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
|
237 |
result.toList |
133e3e84e6fb
some support for merge of Isabelle/jEdit shortcuts wrt. jEdit keymap;
wenzelm
parents:
62590
diff
changeset
|
238 |
} |
63781 | 239 |
|
64207 | 240 |
def duplicates[A](lst: List[A], eq: (A, A) => Boolean = (x: A, y: A) => x == y): List[A] = |
63781 | 241 |
{ |
242 |
val result = new mutable.ListBuffer[A] |
|
243 |
@tailrec def dups(rest: List[A]): Unit = |
|
244 |
rest match { |
|
245 |
case Nil => |
|
246 |
case x :: xs => |
|
64207 | 247 |
if (!result.exists(y => eq(x, y)) && xs.exists(y => eq(x, y))) result += x |
63781 | 248 |
dups(xs) |
249 |
} |
|
250 |
dups(lst) |
|
251 |
result.toList |
|
252 |
} |
|
34136 | 253 |
} |