src/Pure/General/value.scala
author wenzelm
Sun, 21 Jul 2024 13:44:05 +0200
changeset 80604 67067490392d
parent 80548 d1662f1296db
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
63805
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/value.scala
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
     3
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
     4
Plain values, represented as string.
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
     5
*/
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
     6
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
     7
package isabelle
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
     8
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
     9
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 71601
diff changeset
    10
object Value {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 71601
diff changeset
    11
  object Boolean {
63805
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    12
    def apply(x: scala.Boolean): java.lang.String = x.toString
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    13
    def unapply(s: java.lang.String): Option[scala.Boolean] =
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    14
      s match {
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    15
        case "true" => Some(true)
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    16
        case "false" => Some(false)
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    17
        case _ => None
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    18
      }
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    19
    def parse(s: java.lang.String): scala.Boolean =
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    20
      unapply(s) getOrElse error("Bad boolean: " + quote(s))
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    21
  }
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    22
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 71601
diff changeset
    23
  object Nat {
80505
e3af424fdd1a more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents: 77111
diff changeset
    24
    def unapply(bs: Bytes): Option[scala.Int] =
80548
d1662f1296db tuned signature;
wenzelm
parents: 80505
diff changeset
    25
      if (bs.byte_iterator.forall(Symbol.is_ascii_digit)) unapply(bs.text)
80505
e3af424fdd1a more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents: 77111
diff changeset
    26
      else None
69452
704915cf59fa more uniform multi-language operations;
wenzelm
parents: 69450
diff changeset
    27
    def unapply(s: java.lang.String): Option[scala.Int] =
704915cf59fa more uniform multi-language operations;
wenzelm
parents: 69450
diff changeset
    28
      s match {
704915cf59fa more uniform multi-language operations;
wenzelm
parents: 69450
diff changeset
    29
        case Int(n) if n >= 0 => Some(n)
704915cf59fa more uniform multi-language operations;
wenzelm
parents: 69450
diff changeset
    30
        case _ => None
704915cf59fa more uniform multi-language operations;
wenzelm
parents: 69450
diff changeset
    31
      }
704915cf59fa more uniform multi-language operations;
wenzelm
parents: 69450
diff changeset
    32
    def parse(s: java.lang.String): scala.Int =
704915cf59fa more uniform multi-language operations;
wenzelm
parents: 69450
diff changeset
    33
      unapply(s) getOrElse error("Bad natural number: " + quote(s))
704915cf59fa more uniform multi-language operations;
wenzelm
parents: 69450
diff changeset
    34
  }
704915cf59fa more uniform multi-language operations;
wenzelm
parents: 69450
diff changeset
    35
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 71601
diff changeset
    36
  object Int {
63805
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    37
    def apply(x: scala.Int): java.lang.String = Library.signed_string_of_int(x)
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    38
    def unapply(s: java.lang.String): Option[scala.Int] =
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    39
      try { Some(Integer.parseInt(s)) }
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    40
      catch { case _: NumberFormatException => None }
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    41
    def parse(s: java.lang.String): scala.Int =
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    42
      unapply(s) getOrElse error("Bad integer: " + quote(s))
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    43
  }
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    44
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 71601
diff changeset
    45
  object Long {
63805
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    46
    def apply(x: scala.Long): java.lang.String = Library.signed_string_of_long(x)
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    47
    def unapply(s: java.lang.String): Option[scala.Long] =
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    48
      try { Some(java.lang.Long.parseLong(s)) }
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    49
      catch { case _: NumberFormatException => None }
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    50
    def parse(s: java.lang.String): scala.Long =
77111
aa359010d264 tuned message;
wenzelm
parents: 75393
diff changeset
    51
      unapply(s) getOrElse error("Bad long integer: " + quote(s))
63805
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    52
  }
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    53
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 71601
diff changeset
    54
  object Double {
63805
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    55
    def apply(x: scala.Double): java.lang.String = x.toString
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    56
    def unapply(s: java.lang.String): Option[scala.Double] =
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    57
      try { Some(java.lang.Double.parseDouble(s)) }
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    58
      catch { case _: NumberFormatException => None }
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    59
    def parse(s: java.lang.String): scala.Double =
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    60
      unapply(s) getOrElse error("Bad real: " + quote(s))
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    61
  }
68944
ce68b1488612 tuned signature;
wenzelm
parents: 63805
diff changeset
    62
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 71601
diff changeset
    63
  object Seconds {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 71601
diff changeset
    64
    def apply(t: Time): java.lang.String = {
68946
6dd1460f6920 more accurate output;
wenzelm
parents: 68944
diff changeset
    65
      val s = t.seconds
6dd1460f6920 more accurate output;
wenzelm
parents: 68944
diff changeset
    66
      if (s.toInt.toDouble == s) s.toInt.toString else t.toString
6dd1460f6920 more accurate output;
wenzelm
parents: 68944
diff changeset
    67
    }
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 69452
diff changeset
    68
    def unapply(s: java.lang.String): Option[Time] = Double.unapply(s).map(Time.seconds)
68944
ce68b1488612 tuned signature;
wenzelm
parents: 63805
diff changeset
    69
    def parse(s: java.lang.String): Time =
ce68b1488612 tuned signature;
wenzelm
parents: 63805
diff changeset
    70
      unapply(s) getOrElse error("Bad real (for seconds): " + quote(s))
ce68b1488612 tuned signature;
wenzelm
parents: 63805
diff changeset
    71
  }
63805
c272680df665 clarified modules;
wenzelm
parents:
diff changeset
    72
}