author | wenzelm |
Sat, 13 Mar 2021 13:44:42 +0100 | |
changeset 73421 | a114ecd280ca |
parent 67218 | e62d72699666 |
child 77775 | 3cc55085d490 |
permissions | -rw-r--r-- |
63806 | 1 |
(* Title: Pure/General/value.ML |
2 |
Author: Makarius |
|
3 |
||
4 |
Plain values, represented as string. |
|
5 |
*) |
|
6 |
||
7 |
signature VALUE = |
|
8 |
sig |
|
9 |
val parse_bool: string -> bool |
|
10 |
val print_bool: bool -> string |
|
11 |
val parse_nat: string -> int |
|
12 |
val parse_int: string -> int |
|
13 |
val print_int: int -> string |
|
14 |
val parse_real: string -> real |
|
15 |
val print_real: real -> string |
|
67000
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
16 |
val parse_time: string -> Time.time |
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
17 |
val print_time: Time.time -> string |
63806 | 18 |
end; |
19 |
||
20 |
structure Value: VALUE = |
|
21 |
struct |
|
22 |
||
67000
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
23 |
(* bool *) |
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
24 |
|
63806 | 25 |
fun parse_bool "true" = true |
26 |
| parse_bool "false" = false |
|
27 |
| parse_bool s = raise Fail ("Bad boolean " ^ quote s); |
|
28 |
||
29 |
val print_bool = Bool.toString; |
|
30 |
||
31 |
||
67000
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
32 |
(* nat and int *) |
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
33 |
|
67218
e62d72699666
more accurate parse_nat/parse_int: avoid corner cases of Int.fromString (e.g. "1.0");
wenzelm
parents:
67000
diff
changeset
|
34 |
val zero = ord "0"; |
e62d72699666
more accurate parse_nat/parse_int: avoid corner cases of Int.fromString (e.g. "1.0");
wenzelm
parents:
67000
diff
changeset
|
35 |
val nine = ord "9"; |
e62d72699666
more accurate parse_nat/parse_int: avoid corner cases of Int.fromString (e.g. "1.0");
wenzelm
parents:
67000
diff
changeset
|
36 |
|
63806 | 37 |
fun parse_nat s = |
67218
e62d72699666
more accurate parse_nat/parse_int: avoid corner cases of Int.fromString (e.g. "1.0");
wenzelm
parents:
67000
diff
changeset
|
38 |
fold_string (fn c => fn n => |
e62d72699666
more accurate parse_nat/parse_int: avoid corner cases of Int.fromString (e.g. "1.0");
wenzelm
parents:
67000
diff
changeset
|
39 |
let val i = ord c in |
e62d72699666
more accurate parse_nat/parse_int: avoid corner cases of Int.fromString (e.g. "1.0");
wenzelm
parents:
67000
diff
changeset
|
40 |
if zero <= i andalso i <= nine then 10 * n + (i - zero) |
e62d72699666
more accurate parse_nat/parse_int: avoid corner cases of Int.fromString (e.g. "1.0");
wenzelm
parents:
67000
diff
changeset
|
41 |
else raise Fail ("Bad natural number " ^ quote s) |
e62d72699666
more accurate parse_nat/parse_int: avoid corner cases of Int.fromString (e.g. "1.0");
wenzelm
parents:
67000
diff
changeset
|
42 |
end) s 0; |
63806 | 43 |
|
44 |
fun parse_int s = |
|
67218
e62d72699666
more accurate parse_nat/parse_int: avoid corner cases of Int.fromString (e.g. "1.0");
wenzelm
parents:
67000
diff
changeset
|
45 |
(case try (unprefix "-") s of |
e62d72699666
more accurate parse_nat/parse_int: avoid corner cases of Int.fromString (e.g. "1.0");
wenzelm
parents:
67000
diff
changeset
|
46 |
NONE => parse_nat s |
e62d72699666
more accurate parse_nat/parse_int: avoid corner cases of Int.fromString (e.g. "1.0");
wenzelm
parents:
67000
diff
changeset
|
47 |
| SOME s' => ~ (parse_nat s')) |
e62d72699666
more accurate parse_nat/parse_int: avoid corner cases of Int.fromString (e.g. "1.0");
wenzelm
parents:
67000
diff
changeset
|
48 |
handle Fail _ => raise Fail ("Bad integer " ^ quote s); |
63806 | 49 |
|
50 |
val print_int = signed_string_of_int; |
|
51 |
||
52 |
||
67000
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
53 |
(* real *) |
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
54 |
|
63806 | 55 |
fun parse_real s = |
56 |
(case Real.fromString s of |
|
57 |
SOME x => x |
|
58 |
| NONE => raise Fail ("Bad real " ^ quote s)); |
|
59 |
||
60 |
fun print_real x = |
|
61 |
let val s = signed_string_of_real x in |
|
62 |
(case space_explode "." s of |
|
63 |
[a, b] => if forall_string (fn c => c = "0") b then a else s |
|
64 |
| _ => s) |
|
65 |
end; |
|
66 |
||
67000
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
67 |
|
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
68 |
(* time *) |
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
69 |
|
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
70 |
fun parse_time s = |
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
71 |
(case Time.fromString s of |
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
72 |
SOME x => x |
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
73 |
| NONE => raise Fail ("Bad time " ^ quote s)); |
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
74 |
|
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
75 |
fun print_time x = |
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
76 |
if x < Time.zeroTime then "-" ^ Time.toString (Time.zeroTime - x) |
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
77 |
else Time.toString x; |
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
78 |
|
63806 | 79 |
end; |