author | wenzelm |
Sat, 04 Nov 2017 12:25:09 +0100 | |
changeset 67000 | 1698e9ccef2d |
parent 63806 | c54a53ef1873 |
child 67218 | e62d72699666 |
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 |
|
63806 | 34 |
fun parse_nat s = |
35 |
let val i = Int.fromString s in |
|
36 |
if is_none i orelse the i < 0 |
|
37 |
then raise Fail ("Bad natural number " ^ quote s) |
|
38 |
else the i |
|
39 |
end; |
|
40 |
||
41 |
fun parse_int s = |
|
42 |
let val i = Int.fromString s in |
|
43 |
if is_none i orelse String.isPrefix "~" s |
|
44 |
then raise Fail ("Bad integer " ^ quote s) |
|
45 |
else the i |
|
46 |
end; |
|
47 |
||
48 |
val print_int = signed_string_of_int; |
|
49 |
||
50 |
||
67000
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
51 |
(* real *) |
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
52 |
|
63806 | 53 |
fun parse_real s = |
54 |
(case Real.fromString s of |
|
55 |
SOME x => x |
|
56 |
| NONE => raise Fail ("Bad real " ^ quote s)); |
|
57 |
||
58 |
fun print_real x = |
|
59 |
let val s = signed_string_of_real x in |
|
60 |
(case space_explode "." s of |
|
61 |
[a, b] => if forall_string (fn c => c = "0") b then a else s |
|
62 |
| _ => s) |
|
63 |
end; |
|
64 |
||
67000
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
65 |
|
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
66 |
(* time *) |
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 |
fun parse_time s = |
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
69 |
(case Time.fromString s of |
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
70 |
SOME x => x |
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
71 |
| 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
|
72 |
|
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
73 |
fun print_time x = |
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
74 |
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
|
75 |
else Time.toString x; |
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents:
63806
diff
changeset
|
76 |
|
63806 | 77 |
end; |