src/Pure/library.scala
author wenzelm
Thu, 06 Jun 2024 22:13:10 +0200
changeset 80272 9f89b3c41460
parent 80270 1d4300506338
child 80274 cff00b3dddf5
permissions -rw-r--r--
clarified signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34136
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/library.scala
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
     3
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
     4
Basic library.
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
     5
*/
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
     6
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
     7
package isabelle
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
     8
38258
dd7dcb9b2637 added Library.thread_actor -- thread as actor;
wenzelm
parents: 38232
diff changeset
     9
63781
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
    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
wenzelm
parents: 58592
diff changeset
    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
39f4cce5a22c added somewhat generic zoom box;
wenzelm
parents: 36791
diff changeset
    14
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75382
diff changeset
    15
object Library {
63789
af28929ff219 support resource management;
wenzelm
parents: 63781
diff changeset
    16
  /* resource management */
af28929ff219 support resource management;
wenzelm
parents: 63781
diff changeset
    17
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75382
diff changeset
    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
af28929ff219 support resource management;
wenzelm
parents: 63781
diff changeset
    21
  }
af28929ff219 support resource management;
wenzelm
parents: 63781
diff changeset
    22
77515
6aae7486e94a more operations;
wenzelm
parents: 77372
diff changeset
    23
  def using_option[A <: AutoCloseable, B](opt: Option[A])(f: A => B): Option[B] =
6aae7486e94a more operations;
wenzelm
parents: 77372
diff changeset
    24
    opt.map(a => using(a)(f))
6aae7486e94a more operations;
wenzelm
parents: 77372
diff changeset
    25
78198
c268def0784b clarified signature: prefer explicit combinator;
wenzelm
parents: 77652
diff changeset
    26
  def using_optional[A <: AutoCloseable, B](opt: Option[A])(f: Option[A] => B): B = {
c268def0784b clarified signature: prefer explicit combinator;
wenzelm
parents: 77652
diff changeset
    27
    try { f(opt) }
c268def0784b clarified signature: prefer explicit combinator;
wenzelm
parents: 77652
diff changeset
    28
    finally {
c268def0784b clarified signature: prefer explicit combinator;
wenzelm
parents: 77652
diff changeset
    29
      opt match {
c268def0784b clarified signature: prefer explicit combinator;
wenzelm
parents: 77652
diff changeset
    30
        case Some(a) if a != null => a.close()
c268def0784b clarified signature: prefer explicit combinator;
wenzelm
parents: 77652
diff changeset
    31
        case _ =>
c268def0784b clarified signature: prefer explicit combinator;
wenzelm
parents: 77652
diff changeset
    32
      }
c268def0784b clarified signature: prefer explicit combinator;
wenzelm
parents: 77652
diff changeset
    33
    }
c268def0784b clarified signature: prefer explicit combinator;
wenzelm
parents: 77652
diff changeset
    34
  }
c268def0784b clarified signature: prefer explicit combinator;
wenzelm
parents: 77652
diff changeset
    35
63789
af28929ff219 support resource management;
wenzelm
parents: 63781
diff changeset
    36
57909
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    37
  /* integers */
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
  private val small_int = 10000
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75382
diff changeset
    40
  private lazy val small_int_table = {
57909
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    41
    val array = new Array[String](small_int)
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    42
    for (i <- 0 until small_int) array(i) = i.toString
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    43
    array
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
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75382
diff changeset
    46
  def is_small_int(s: String): Boolean = {
57909
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    47
    val len = s.length
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    48
    1 <= len && len <= 4 &&
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    49
    s.forall(c => '0' <= c && c <= '9') &&
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    50
    (len == 1 || s(0) != '0')
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    51
  }
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    52
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    53
  def signed_string_of_long(i: Long): String =
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    54
    if (0 <= i && i < small_int) small_int_table(i.toInt)
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    55
    else i.toString
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    56
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    57
  def signed_string_of_int(i: Int): String =
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    58
    if (0 <= i && i < small_int) small_int_table(i)
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    59
    else i.toString
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    60
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    61
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    62
  /* separated chunks */
36688
321d392ab12e added separate;
wenzelm
parents: 36685
diff changeset
    63
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75382
diff changeset
    64
  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
    65
    val result = new mutable.ListBuffer[A]
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    66
    var first = true
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    67
    for (x <- list) {
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    68
      if (first) {
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    69
        first = false
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    70
        result += x
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    71
      }
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    72
      else {
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    73
        result += s
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    74
        result += x
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    75
      }
36688
321d392ab12e added separate;
wenzelm
parents: 36685
diff changeset
    76
    }
51981
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    77
    result.toList
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    78
  }
36688
321d392ab12e added separate;
wenzelm
parents: 36685
diff changeset
    79
56600
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    80
  def separated_chunks(sep: Char => Boolean, source: CharSequence): Iterator[CharSequence] =
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    81
    new Iterator[CharSequence] {
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    82
      private val end = source.length
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75382
diff changeset
    83
      private def next_chunk(i: Int): Option[(CharSequence, Int)] = {
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    84
        if (i < end) {
75382
81673c441ce3 tuned: eliminted do-while for the sake of scala3;
wenzelm
parents: 75295
diff changeset
    85
          var j = i
75709
a068fb7346ef clarified while-loops;
wenzelm
parents: 75393
diff changeset
    86
          while ({
75382
81673c441ce3 tuned: eliminted do-while for the sake of scala3;
wenzelm
parents: 75295
diff changeset
    87
            j += 1
75709
a068fb7346ef clarified while-loops;
wenzelm
parents: 75393
diff changeset
    88
            j < end && !sep(source.charAt(j))
a068fb7346ef clarified while-loops;
wenzelm
parents: 75393
diff changeset
    89
          }) ()
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    90
          Some((source.subSequence(i + 1, j), j))
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    91
        }
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    92
        else None
43598
826ddd91ae2b basic operations on lists and strings;
wenzelm
parents: 43442
diff changeset
    93
      }
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    94
      private var state: Option[(CharSequence, Int)] = if (end == 0) None else next_chunk(-1)
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    95
73337
0af9e7e4476f tuned --- fewer warnings;
wenzelm
parents: 73333
diff changeset
    96
      def hasNext: Boolean = state.isDefined
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    97
      def next(): CharSequence =
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    98
        state match {
60215
5fb4990dfc73 misc tuning, based on warnings by IntelliJ IDEA;
wenzelm
parents: 59697
diff changeset
    99
          case Some((s, i)) => state = next_chunk(i); s
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
   100
          case None => Iterator.empty.next()
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
   101
        }
43598
826ddd91ae2b basic operations on lists and strings;
wenzelm
parents: 43442
diff changeset
   102
    }
826ddd91ae2b basic operations on lists and strings;
wenzelm
parents: 43442
diff changeset
   103
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
   104
  def space_explode(sep: Char, str: String): List[String] =
56600
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
   105
    separated_chunks(_ == sep, str).map(_.toString).toList
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
   106
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
   107
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
   108
  /* lines */
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
   109
77007
19a7046f90f9 clarified signature;
wenzelm
parents: 76788
diff changeset
   110
  def count_newlines(str: String): Int = str.count(_ == '\n')
19a7046f90f9 clarified signature;
wenzelm
parents: 76788
diff changeset
   111
75859
7164f537370f proper treatment of empty lines (amending 08f89f0e8a62);
wenzelm
parents: 75709
diff changeset
   112
  def terminate_lines(lines: IterableOnce[String]): String = {
7164f537370f proper treatment of empty lines (amending 08f89f0e8a62);
wenzelm
parents: 75709
diff changeset
   113
    val it = lines.iterator
7164f537370f proper treatment of empty lines (amending 08f89f0e8a62);
wenzelm
parents: 75709
diff changeset
   114
    if (it.isEmpty) "" else it.mkString("", "\n", "\n")
7164f537370f proper treatment of empty lines (amending 08f89f0e8a62);
wenzelm
parents: 75709
diff changeset
   115
  }
51983
32692ce4c61a more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents: 51981
diff changeset
   116
73362
dde25151c3c1 tuned --- fewer warnings;
wenzelm
parents: 73357
diff changeset
   117
  def cat_lines(lines: IterableOnce[String]): String =
dde25151c3c1 tuned --- fewer warnings;
wenzelm
parents: 73357
diff changeset
   118
    lines.iterator.mkString("\n")
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
   119
43670
7f933761764b prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents: 43652
diff changeset
   120
  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
   121
62590
0c837beeb5e7 upgrade "isabelle build" to Isabelle/Scala;
wenzelm
parents: 62492
diff changeset
   122
  def prefix_lines(prfx: String, str: String): String =
73963
59b6f0462086 clarified modules;
wenzelm
parents: 73736
diff changeset
   123
    isabelle.setup.Library.prefix_lines(prfx, str)
62590
0c837beeb5e7 upgrade "isabelle build" to Isabelle/Scala;
wenzelm
parents: 62492
diff changeset
   124
73736
a8ff6e4ee661 tuned signature;
wenzelm
parents: 73573
diff changeset
   125
  def indent_lines(n: Int, str: String): String =
a8ff6e4ee661 tuned signature;
wenzelm
parents: 73573
diff changeset
   126
    prefix_lines(Symbol.spaces(n), str)
a8ff6e4ee661 tuned signature;
wenzelm
parents: 73573
diff changeset
   127
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75382
diff changeset
   128
  def first_line(source: CharSequence): String = {
56600
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
   129
    val lines = separated_chunks(_ == '\n', source)
73344
f5c147654661 tuned --- fewer warnings;
wenzelm
parents: 73339
diff changeset
   130
    if (lines.hasNext) lines.next().toString
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
   131
    else ""
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
   132
  }
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
   133
73332
wenzelm
parents: 73120
diff changeset
   134
  def trim_line(s: String): String =
73963
59b6f0462086 clarified modules;
wenzelm
parents: 73736
diff changeset
   135
    isabelle.setup.Library.trim_line(s)
73332
wenzelm
parents: 73120
diff changeset
   136
wenzelm
parents: 73120
diff changeset
   137
  def trim_split_lines(s: String): List[String] =
wenzelm
parents: 73120
diff changeset
   138
    split_lines(trim_line(s)).map(trim_line)
wenzelm
parents: 73120
diff changeset
   139
65932
db5e701b691a clarified modules;
wenzelm
parents: 65903
diff changeset
   140
  def encode_lines(s: String): String = s.replace('\n', '\u000b')
db5e701b691a clarified modules;
wenzelm
parents: 65903
diff changeset
   141
  def decode_lines(s: String): String = s.replace('\u000b', '\n')
db5e701b691a clarified modules;
wenzelm
parents: 65903
diff changeset
   142
50847
78c40f1cc9b3 tuned signature;
wenzelm
parents: 50845
diff changeset
   143
78c40f1cc9b3 tuned signature;
wenzelm
parents: 50845
diff changeset
   144
  /* strings */
78c40f1cc9b3 tuned signature;
wenzelm
parents: 50845
diff changeset
   145
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75382
diff changeset
   146
  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
   147
    val s = new StringBuilder(capacity)
64355
c6a1031cf925 support for XML as HTML;
wenzelm
parents: 64207
diff changeset
   148
    f(s)
c6a1031cf925 support for XML as HTML;
wenzelm
parents: 64207
diff changeset
   149
    s.toString
c6a1031cf925 support for XML as HTML;
wenzelm
parents: 64207
diff changeset
   150
  }
c6a1031cf925 support for XML as HTML;
wenzelm
parents: 64207
diff changeset
   151
50847
78c40f1cc9b3 tuned signature;
wenzelm
parents: 50845
diff changeset
   152
  def try_unprefix(prfx: String, s: String): Option[String] =
78c40f1cc9b3 tuned signature;
wenzelm
parents: 50845
diff changeset
   153
    if (s.startsWith(prfx)) Some(s.substring(prfx.length)) else None
78c40f1cc9b3 tuned signature;
wenzelm
parents: 50845
diff changeset
   154
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54548
diff changeset
   155
  def try_unsuffix(sffx: String, s: String): Option[String] =
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54548
diff changeset
   156
    if (s.endsWith(sffx)) Some(s.substring(0, s.length - sffx.length)) else None
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54548
diff changeset
   157
65606
wenzelm
parents: 64871
diff changeset
   158
  def perhaps_unprefix(prfx: String, s: String): String = try_unprefix(prfx, s) getOrElse s
wenzelm
parents: 64871
diff changeset
   159
  def perhaps_unsuffix(sffx: String, s: String): String = try_unsuffix(sffx, s) getOrElse s
wenzelm
parents: 64871
diff changeset
   160
65903
692e428803c8 clarified signature;
wenzelm
parents: 65761
diff changeset
   161
  def isolate_substring(s: String): String = new String(s.toCharArray)
64820
00488a8c042f Line.Document consists of independently allocated strings;
wenzelm
parents: 64370
diff changeset
   162
71864
bfc120aa737a clarified signature;
wenzelm
parents: 71601
diff changeset
   163
  def strip_ansi_color(s: String): String =
73355
ec52a1a6ed31 tuned --- fewer warnings;
wenzelm
parents: 73344
diff changeset
   164
    s.replaceAll("\u001b\\[\\d+m", "")
71864
bfc120aa737a clarified signature;
wenzelm
parents: 71601
diff changeset
   165
43598
826ddd91ae2b basic operations on lists and strings;
wenzelm
parents: 43442
diff changeset
   166
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
   167
  /* quote */
46196
805de058722b added cat_lines convenience;
wenzelm
parents: 45900
diff changeset
   168
67820
e30d6368c7c8 clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents: 67436
diff changeset
   169
  def single_quote(s: String): String = "'" + s + "'"
e30d6368c7c8 clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents: 67436
diff changeset
   170
43598
826ddd91ae2b basic operations on lists and strings;
wenzelm
parents: 43442
diff changeset
   171
  def quote(s: String): String = "\"" + s + "\""
56843
b2bfcd8cda80 support for path completion based on file-system content;
wenzelm
parents: 56730
diff changeset
   172
b2bfcd8cda80 support for path completion based on file-system content;
wenzelm
parents: 56730
diff changeset
   173
  def try_unquote(s: String): Option[String] =
b2bfcd8cda80 support for path completion based on file-system content;
wenzelm
parents: 56730
diff changeset
   174
    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
   175
    else None
b2bfcd8cda80 support for path completion based on file-system content;
wenzelm
parents: 56730
diff changeset
   176
58592
b0fff34d3247 completion for bibtex entries;
wenzelm
parents: 57909
diff changeset
   177
  def perhaps_unquote(s: String): String = try_unquote(s) getOrElse s
b0fff34d3247 completion for bibtex entries;
wenzelm
parents: 57909
diff changeset
   178
43598
826ddd91ae2b basic operations on lists and strings;
wenzelm
parents: 43442
diff changeset
   179
  def commas(ss: Iterable[String]): String = ss.iterator.mkString(", ")
48362
c3192ccb0ff4 proper commas_quote;
wenzelm
parents: 48345
diff changeset
   180
  def commas_quote(ss: Iterable[String]): String = ss.iterator.map(quote).mkString(", ")
43598
826ddd91ae2b basic operations on lists and strings;
wenzelm
parents: 43442
diff changeset
   181
36688
321d392ab12e added separate;
wenzelm
parents: 36685
diff changeset
   182
51983
32692ce4c61a more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents: 51981
diff changeset
   183
  /* CharSequence */
34141
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   184
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75382
diff changeset
   185
  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
   186
    require(0 <= start && start <= end && end <= text.length, "bad reverse range")
34141
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   187
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   188
    def this(text: CharSequence) = this(text, 0, text.length)
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   189
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   190
    def length: Int = end - start
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   191
    def charAt(i: Int): Char = text.charAt(end - i - 1)
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   192
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   193
    def subSequence(i: Int, j: Int): CharSequence =
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   194
      if (0 <= i && i <= j && j <= length) new Reverse(text, end - j, end - i)
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   195
      else throw new IndexOutOfBoundsException
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   196
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75382
diff changeset
   197
    override def toString: String = {
34141
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   198
      val buf = new StringBuilder(length)
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   199
      for (i <- 0 until length)
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   200
        buf.append(charAt(i))
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   201
      buf.toString
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   202
    }
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   203
  }
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   204
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75382
diff changeset
   205
  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
   206
    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
   207
    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
   208
    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
   209
      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
   210
      else text.subSequence(i, j)
32692ce4c61a more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents: 51981
diff changeset
   211
    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
   212
  }
32692ce4c61a more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents: 51981
diff changeset
   213
34141
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   214
59224
wenzelm
parents: 58592
diff changeset
   215
  /* regular expressions */
wenzelm
parents: 58592
diff changeset
   216
wenzelm
parents: 58592
diff changeset
   217
  def make_regex(s: String): Option[Regex] =
wenzelm
parents: 58592
diff changeset
   218
    try { Some(new Regex(s)) } catch { case ERROR(_) => None }
wenzelm
parents: 58592
diff changeset
   219
64871
wenzelm
parents: 64820
diff changeset
   220
  def is_regex_meta(c: Char): Boolean = """()[]{}\^$|?*+.<>-=!""".contains(c)
wenzelm
parents: 64820
diff changeset
   221
wenzelm
parents: 64820
diff changeset
   222
  def escape_regex(s: String): String =
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71379
diff changeset
   223
    if (s.exists(is_regex_meta)) {
64871
wenzelm
parents: 64820
diff changeset
   224
      (for (c <- s.iterator)
wenzelm
parents: 64820
diff changeset
   225
       yield { if (is_regex_meta(c)) "\\" + c.toString else c.toString }).mkString
wenzelm
parents: 64820
diff changeset
   226
    }
wenzelm
parents: 64820
diff changeset
   227
    else s
wenzelm
parents: 64820
diff changeset
   228
59224
wenzelm
parents: 58592
diff changeset
   229
61883
c0f34fe6aa61 clarified length of block with pre-existant forced breaks;
wenzelm
parents: 60215
diff changeset
   230
  /* lists */
c0f34fe6aa61 clarified length of block with pre-existant forced breaks;
wenzelm
parents: 60215
diff changeset
   231
c0f34fe6aa61 clarified length of block with pre-existant forced breaks;
wenzelm
parents: 60215
diff changeset
   232
  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
   233
    (xs.takeWhile(pred), xs.dropWhile(pred))
56686
2386d1a3ca8f canonical list operations, as in ML;
wenzelm
parents: 56672
diff changeset
   234
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75382
diff changeset
   235
  def take_suffix[A](pred: A => Boolean, xs: List[A]): (List[A], List[A]) = {
67434
a38c74b0886d more operations (as in ML);
wenzelm
parents: 67431
diff changeset
   236
    val rev_xs = xs.reverse
a38c74b0886d more operations (as in ML);
wenzelm
parents: 67431
diff changeset
   237
    (rev_xs.dropWhile(pred).reverse, rev_xs.takeWhile(pred).reverse)
a38c74b0886d more operations (as in ML);
wenzelm
parents: 67431
diff changeset
   238
  }
a38c74b0886d more operations (as in ML);
wenzelm
parents: 67431
diff changeset
   239
a38c74b0886d more operations (as in ML);
wenzelm
parents: 67431
diff changeset
   240
  def trim[A](pred: A => Boolean, xs: List[A]): List[A] =
a38c74b0886d more operations (as in ML);
wenzelm
parents: 67431
diff changeset
   241
    take_suffix(pred, take_prefix(pred, xs)._2)._1
a38c74b0886d more operations (as in ML);
wenzelm
parents: 67431
diff changeset
   242
60215
5fb4990dfc73 misc tuning, based on warnings by IntelliJ IDEA;
wenzelm
parents: 59697
diff changeset
   243
  def member[A, B](xs: List[A])(x: B): Boolean = xs.contains(x)
56688
f3932166a33d more canonical list operations;
wenzelm
parents: 56686
diff changeset
   244
  def insert[A](x: A)(xs: List[A]): List[A] = if (xs.contains(x)) xs else x :: xs
f3932166a33d more canonical list operations;
wenzelm
parents: 56686
diff changeset
   245
  def remove[A, B](x: B)(xs: List[A]): List[A] = if (member(xs)(x)) xs.filterNot(_ == x) else xs
f3932166a33d more canonical list operations;
wenzelm
parents: 56686
diff changeset
   246
  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
   247
63867
fb46c031c841 maintain abbrevs in canonical reverse order;
wenzelm
parents: 63789
diff changeset
   248
  def merge[A](xs: List[A], ys: List[A]): List[A] =
fb46c031c841 maintain abbrevs in canonical reverse order;
wenzelm
parents: 63789
diff changeset
   249
    if (xs.eq(ys)) xs
fb46c031c841 maintain abbrevs in canonical reverse order;
wenzelm
parents: 63789
diff changeset
   250
    else if (xs.isEmpty) ys
fb46c031c841 maintain abbrevs in canonical reverse order;
wenzelm
parents: 63789
diff changeset
   251
    else ys.foldRight(xs)(Library.insert(_)(_))
fb46c031c841 maintain abbrevs in canonical reverse order;
wenzelm
parents: 63789
diff changeset
   252
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75382
diff changeset
   253
  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
   254
    val result = new mutable.ListBuffer[A]
64207
ad15c2f478b5 more general operations;
wenzelm
parents: 64063
diff changeset
   255
    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
   256
    result.toList
133e3e84e6fb some support for merge of Isabelle/jEdit shortcuts wrt. jEdit keymap;
wenzelm
parents: 62590
diff changeset
   257
  }
63781
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   258
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75382
diff changeset
   259
  def duplicates[A](lst: List[A], eq: (A, A) => Boolean = (x: A, y: A) => x == y): List[A] = {
63781
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   260
    val result = new mutable.ListBuffer[A]
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   261
    @tailrec def dups(rest: List[A]): Unit =
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   262
      rest match {
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   263
        case Nil =>
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   264
        case x :: xs =>
64207
ad15c2f478b5 more general operations;
wenzelm
parents: 64063
diff changeset
   265
          if (!result.exists(y => eq(x, y)) && xs.exists(y => eq(x, y))) result += x
63781
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   266
          dups(xs)
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   267
      }
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   268
    dups(lst)
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   269
    result.toList
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   270
  }
65761
ce909161d030 more operations;
wenzelm
parents: 65718
diff changeset
   271
68715
8197c2857267 more operations (as in ML);
wenzelm
parents: 67885
diff changeset
   272
  def replicate[A](n: Int, a: A): List[A] =
8197c2857267 more operations (as in ML);
wenzelm
parents: 67885
diff changeset
   273
    if (n < 0) throw new IllegalArgumentException
8197c2857267 more operations (as in ML);
wenzelm
parents: 67885
diff changeset
   274
    else if (n == 0) Nil
8197c2857267 more operations (as in ML);
wenzelm
parents: 67885
diff changeset
   275
    else {
8197c2857267 more operations (as in ML);
wenzelm
parents: 67885
diff changeset
   276
      val res = new mutable.ListBuffer[A]
8197c2857267 more operations (as in ML);
wenzelm
parents: 67885
diff changeset
   277
      (1 to n).foreach(_ => res += a)
8197c2857267 more operations (as in ML);
wenzelm
parents: 67885
diff changeset
   278
      res.toList
8197c2857267 more operations (as in ML);
wenzelm
parents: 67885
diff changeset
   279
    }
8197c2857267 more operations (as in ML);
wenzelm
parents: 67885
diff changeset
   280
78943
bc89bdc65f29 clarified signature: more operations;
wenzelm
parents: 78198
diff changeset
   281
  def the_single[A](xs: List[A], message: => String = "Single argument expected"): A =
73571
f86661e32bed clarified signature;
wenzelm
parents: 73362
diff changeset
   282
    xs match {
f86661e32bed clarified signature;
wenzelm
parents: 73362
diff changeset
   283
      case List(x) => x
78943
bc89bdc65f29 clarified signature: more operations;
wenzelm
parents: 78198
diff changeset
   284
      case _ => error(message)
73571
f86661e32bed clarified signature;
wenzelm
parents: 73362
diff changeset
   285
    }
f86661e32bed clarified signature;
wenzelm
parents: 73362
diff changeset
   286
65761
ce909161d030 more operations;
wenzelm
parents: 65718
diff changeset
   287
79834
45b81ff3c972 clarified modules;
wenzelm
parents: 79833
diff changeset
   288
  /* data update */
45b81ff3c972 clarified modules;
wenzelm
parents: 79833
diff changeset
   289
45b81ff3c972 clarified modules;
wenzelm
parents: 79833
diff changeset
   290
  object Update {
79869
ea335307d45e clarified signature: more explicit types;
wenzelm
parents: 79842
diff changeset
   291
    sealed abstract class Op[A]
ea335307d45e clarified signature: more explicit types;
wenzelm
parents: 79842
diff changeset
   292
    case class Delete[A](name: String) extends Op[A]
ea335307d45e clarified signature: more explicit types;
wenzelm
parents: 79842
diff changeset
   293
    case class Insert[A](item: A) extends Op[A]
ea335307d45e clarified signature: more explicit types;
wenzelm
parents: 79842
diff changeset
   294
80272
9f89b3c41460 clarified signature;
wenzelm
parents: 80270
diff changeset
   295
    def data[A <: Name.T](old_data: Name.Data[A], updates: List[Op[A]]): Name.Data[A] =
79870
510fe8c3d9b8 tuned signature: more operations;
wenzelm
parents: 79869
diff changeset
   296
      updates.foldLeft(old_data) {
510fe8c3d9b8 tuned signature: more operations;
wenzelm
parents: 79869
diff changeset
   297
        case (map, Delete(name)) => map - name
510fe8c3d9b8 tuned signature: more operations;
wenzelm
parents: 79869
diff changeset
   298
        case (map, Insert(item)) => map + (item.name -> item)
510fe8c3d9b8 tuned signature: more operations;
wenzelm
parents: 79869
diff changeset
   299
      }
510fe8c3d9b8 tuned signature: more operations;
wenzelm
parents: 79869
diff changeset
   300
79834
45b81ff3c972 clarified modules;
wenzelm
parents: 79833
diff changeset
   301
    val empty: Update = Update()
45b81ff3c972 clarified modules;
wenzelm
parents: 79833
diff changeset
   302
80272
9f89b3c41460 clarified signature;
wenzelm
parents: 80270
diff changeset
   303
    def make[A](a: Name.Data[A], b: Name.Data[A], kind: Int = 0): Update =
79842
ba306bc7d226 tuned signature;
wenzelm
parents: 79839
diff changeset
   304
      if (a eq b) empty
79834
45b81ff3c972 clarified modules;
wenzelm
parents: 79833
diff changeset
   305
      else {
79842
ba306bc7d226 tuned signature;
wenzelm
parents: 79839
diff changeset
   306
        val delete = List.from(for ((x, y) <- a.iterator if !b.get(x).contains(y)) yield x)
ba306bc7d226 tuned signature;
wenzelm
parents: 79839
diff changeset
   307
        val insert = List.from(for ((x, y) <- b.iterator if !a.get(x).contains(y)) yield x)
79839
f425bbc4b2eb record updates within database, based on serial;
wenzelm
parents: 79836
diff changeset
   308
        Update(delete = delete, insert = insert, kind = kind)
79834
45b81ff3c972 clarified modules;
wenzelm
parents: 79833
diff changeset
   309
      }
45b81ff3c972 clarified modules;
wenzelm
parents: 79833
diff changeset
   310
  }
45b81ff3c972 clarified modules;
wenzelm
parents: 79833
diff changeset
   311
45b81ff3c972 clarified modules;
wenzelm
parents: 79833
diff changeset
   312
  sealed case class Update(
45b81ff3c972 clarified modules;
wenzelm
parents: 79833
diff changeset
   313
    delete: List[String] = Nil,
79839
f425bbc4b2eb record updates within database, based on serial;
wenzelm
parents: 79836
diff changeset
   314
    insert: List[String] = Nil,
f425bbc4b2eb record updates within database, based on serial;
wenzelm
parents: 79836
diff changeset
   315
    kind: Int = 0
79834
45b81ff3c972 clarified modules;
wenzelm
parents: 79833
diff changeset
   316
  ) {
45b81ff3c972 clarified modules;
wenzelm
parents: 79833
diff changeset
   317
    def deletes: Boolean = delete.nonEmpty
45b81ff3c972 clarified modules;
wenzelm
parents: 79833
diff changeset
   318
    def inserts: Boolean = insert.nonEmpty
45b81ff3c972 clarified modules;
wenzelm
parents: 79833
diff changeset
   319
    def defined: Boolean = deletes || inserts
79836
c69ae2b8987e clarified signature: improved data integrity;
wenzelm
parents: 79834
diff changeset
   320
    lazy val domain: Set[String] = delete.toSet ++ insert
79834
45b81ff3c972 clarified modules;
wenzelm
parents: 79833
diff changeset
   321
  }
45b81ff3c972 clarified modules;
wenzelm
parents: 79833
diff changeset
   322
45b81ff3c972 clarified modules;
wenzelm
parents: 79833
diff changeset
   323
65761
ce909161d030 more operations;
wenzelm
parents: 65718
diff changeset
   324
  /* proper values */
ce909161d030 more operations;
wenzelm
parents: 65718
diff changeset
   325
75295
38398766be6b command-line arguments for "isabelle vscode", similar to "isabelle jedit";
wenzelm
parents: 74794
diff changeset
   326
  def proper_bool(b: Boolean): Option[Boolean] =
38398766be6b command-line arguments for "isabelle vscode", similar to "isabelle jedit";
wenzelm
parents: 74794
diff changeset
   327
    if (!b) None else Some(b)
38398766be6b command-line arguments for "isabelle vscode", similar to "isabelle jedit";
wenzelm
parents: 74794
diff changeset
   328
65761
ce909161d030 more operations;
wenzelm
parents: 65718
diff changeset
   329
  def proper_string(s: String): Option[String] =
ce909161d030 more operations;
wenzelm
parents: 65718
diff changeset
   330
    if (s == null || s == "") None else Some(s)
ce909161d030 more operations;
wenzelm
parents: 65718
diff changeset
   331
ce909161d030 more operations;
wenzelm
parents: 65718
diff changeset
   332
  def proper_list[A](list: List[A]): Option[List[A]] =
ce909161d030 more operations;
wenzelm
parents: 65718
diff changeset
   333
    if (list == null || list.isEmpty) None else Some(list)
72214
5924c1da3c45 clarified modules;
wenzelm
parents: 71864
diff changeset
   334
77367
d27d1224c67f more operations;
wenzelm
parents: 77007
diff changeset
   335
  def if_proper[A](x: Iterable[A], body: => String): String =
d27d1224c67f more operations;
wenzelm
parents: 77007
diff changeset
   336
    if (x == null || x.isEmpty) "" else body
d27d1224c67f more operations;
wenzelm
parents: 77007
diff changeset
   337
77614
b619d80f61fa clarified signature;
wenzelm
parents: 77515
diff changeset
   338
  def if_proper(b: Boolean, body: => String): String =
b619d80f61fa clarified signature;
wenzelm
parents: 77515
diff changeset
   339
    if (!b) "" else body
b619d80f61fa clarified signature;
wenzelm
parents: 77515
diff changeset
   340
72214
5924c1da3c45 clarified modules;
wenzelm
parents: 71864
diff changeset
   341
5924c1da3c45 clarified modules;
wenzelm
parents: 71864
diff changeset
   342
  /* reflection */
5924c1da3c45 clarified modules;
wenzelm
parents: 71864
diff changeset
   343
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75382
diff changeset
   344
  def is_subclass[A, B](a: Class[A], b: Class[B]): Boolean = {
73339
9efdebe24c65 tuned --- fewer warnings;
wenzelm
parents: 73337
diff changeset
   345
    import scala.language.existentials
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75382
diff changeset
   346
    @tailrec def subclass(c: Class[_]): Boolean = {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75382
diff changeset
   347
      c == b || { val d = c.getSuperclass; d != null && subclass(d) }
72214
5924c1da3c45 clarified modules;
wenzelm
parents: 71864
diff changeset
   348
    }
5924c1da3c45 clarified modules;
wenzelm
parents: 71864
diff changeset
   349
    subclass(a)
5924c1da3c45 clarified modules;
wenzelm
parents: 71864
diff changeset
   350
  }
76788
ce44e714d573 clarified signature: more explicit types;
wenzelm
parents: 75859
diff changeset
   351
ce44e714d573 clarified signature: more explicit types;
wenzelm
parents: 75859
diff changeset
   352
  def as_subclass[C](c: Class[C])(x: AnyRef): Option[C] =
ce44e714d573 clarified signature: more explicit types;
wenzelm
parents: 75859
diff changeset
   353
    if (x == null || is_subclass(x.getClass, c)) Some(x.asInstanceOf[C]) else None
34136
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
   354
}