63805
|
1 |
/* Title: Pure/General/value.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Plain values, represented as string.
|
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle
|
|
8 |
|
|
9 |
|
75393
|
10 |
object Value {
|
|
11 |
object Boolean {
|
63805
|
12 |
def apply(x: scala.Boolean): java.lang.String = x.toString
|
|
13 |
def unapply(s: java.lang.String): Option[scala.Boolean] =
|
|
14 |
s match {
|
|
15 |
case "true" => Some(true)
|
|
16 |
case "false" => Some(false)
|
|
17 |
case _ => None
|
|
18 |
}
|
|
19 |
def parse(s: java.lang.String): scala.Boolean =
|
|
20 |
unapply(s) getOrElse error("Bad boolean: " + quote(s))
|
|
21 |
}
|
|
22 |
|
75393
|
23 |
object Nat {
|
69452
|
24 |
def unapply(s: java.lang.String): Option[scala.Int] =
|
|
25 |
s match {
|
|
26 |
case Int(n) if n >= 0 => Some(n)
|
|
27 |
case _ => None
|
|
28 |
}
|
|
29 |
def parse(s: java.lang.String): scala.Int =
|
|
30 |
unapply(s) getOrElse error("Bad natural number: " + quote(s))
|
|
31 |
}
|
|
32 |
|
75393
|
33 |
object Int {
|
63805
|
34 |
def apply(x: scala.Int): java.lang.String = Library.signed_string_of_int(x)
|
|
35 |
def unapply(s: java.lang.String): Option[scala.Int] =
|
|
36 |
try { Some(Integer.parseInt(s)) }
|
|
37 |
catch { case _: NumberFormatException => None }
|
|
38 |
def parse(s: java.lang.String): scala.Int =
|
|
39 |
unapply(s) getOrElse error("Bad integer: " + quote(s))
|
|
40 |
}
|
|
41 |
|
75393
|
42 |
object Long {
|
63805
|
43 |
def apply(x: scala.Long): java.lang.String = Library.signed_string_of_long(x)
|
|
44 |
def unapply(s: java.lang.String): Option[scala.Long] =
|
|
45 |
try { Some(java.lang.Long.parseLong(s)) }
|
|
46 |
catch { case _: NumberFormatException => None }
|
|
47 |
def parse(s: java.lang.String): scala.Long =
|
77111
|
48 |
unapply(s) getOrElse error("Bad long integer: " + quote(s))
|
63805
|
49 |
}
|
|
50 |
|
75393
|
51 |
object Double {
|
63805
|
52 |
def apply(x: scala.Double): java.lang.String = x.toString
|
|
53 |
def unapply(s: java.lang.String): Option[scala.Double] =
|
|
54 |
try { Some(java.lang.Double.parseDouble(s)) }
|
|
55 |
catch { case _: NumberFormatException => None }
|
|
56 |
def parse(s: java.lang.String): scala.Double =
|
|
57 |
unapply(s) getOrElse error("Bad real: " + quote(s))
|
|
58 |
}
|
68944
|
59 |
|
75393
|
60 |
object Seconds {
|
|
61 |
def apply(t: Time): java.lang.String = {
|
68946
|
62 |
val s = t.seconds
|
|
63 |
if (s.toInt.toDouble == s) s.toInt.toString else t.toString
|
|
64 |
}
|
71601
|
65 |
def unapply(s: java.lang.String): Option[Time] = Double.unapply(s).map(Time.seconds)
|
68944
|
66 |
def parse(s: java.lang.String): Time =
|
|
67 |
unapply(s) getOrElse error("Bad real (for seconds): " + quote(s))
|
|
68 |
}
|
63805
|
69 |
}
|