| author | wenzelm | 
| Tue, 22 Oct 2024 12:45:38 +0200 | |
| changeset 81229 | e18600daa904 | 
| parent 77847 | 3bb6468d202e | 
| child 82092 | 93195437fdee | 
| 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: 
63806diff
changeset | 16 | val parse_time: string -> Time.time | 
| 
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
 wenzelm parents: 
63806diff
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: 
63806diff
changeset | 23 | (* bool *) | 
| 
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
 wenzelm parents: 
63806diff
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: 
63806diff
changeset | 32 | (* nat and int *) | 
| 
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
 wenzelm parents: 
63806diff
changeset | 33 | |
| 77775 | 34 | local | 
| 35 | ||
| 36 | val c0 = Char.ord #"0"; | |
| 37 | val c9 = Char.ord #"9"; | |
| 38 | val minus = Char.ord #"-"; | |
| 39 | ||
| 40 | in | |
| 41 | ||
| 42 | fun parse_int str = | |
| 43 | let | |
| 44 |     fun err () = raise Fail ("Bad integer " ^ quote str);
 | |
| 77847 
3bb6468d202e
more operations, following Isabelle/ML conventions;
 wenzelm parents: 
77775diff
changeset | 45 | val char = Char.ord o String.nth str; | 
| 77775 | 46 | val n = size str; | 
| 67218 
e62d72699666
more accurate parse_nat/parse_int: avoid corner cases of Int.fromString (e.g. "1.0");
 wenzelm parents: 
67000diff
changeset | 47 | |
| 77775 | 48 | fun digits i a = | 
| 49 | if i = n then a | |
| 50 | else | |
| 51 | let val c = char i | |
| 52 | in if c0 <= c andalso c <= c9 then digits (i + 1) (10 * a + (c - c0)) else err () end; | |
| 53 | in | |
| 54 | if n = 0 then err () | |
| 55 | else if char 0 <> minus then digits 0 0 | |
| 56 | else if n = 1 then err () | |
| 57 | else ~ (digits 1 0) | |
| 58 | end; | |
| 63806 | 59 | |
| 77775 | 60 | fun parse_nat str = | 
| 61 | let | |
| 62 |     fun err () = raise Fail ("Bad natural number " ^ quote str);
 | |
| 63 | val i = parse_int str handle Fail _ => err (); | |
| 64 | in if i >= 0 then i else err () end; | |
| 65 | ||
| 66 | end; | |
| 63806 | 67 | |
| 68 | val print_int = signed_string_of_int; | |
| 69 | ||
| 70 | ||
| 67000 
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
 wenzelm parents: 
63806diff
changeset | 71 | (* real *) | 
| 
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
 wenzelm parents: 
63806diff
changeset | 72 | |
| 63806 | 73 | fun parse_real s = | 
| 74 | (case Real.fromString s of | |
| 75 | SOME x => x | |
| 76 |   | NONE => raise Fail ("Bad real " ^ quote s));
 | |
| 77 | ||
| 78 | fun print_real x = | |
| 79 | let val s = signed_string_of_real x in | |
| 80 | (case space_explode "." s of | |
| 81 | [a, b] => if forall_string (fn c => c = "0") b then a else s | |
| 82 | | _ => s) | |
| 83 | end; | |
| 84 | ||
| 67000 
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
 wenzelm parents: 
63806diff
changeset | 85 | |
| 
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
 wenzelm parents: 
63806diff
changeset | 86 | (* time *) | 
| 
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
 wenzelm parents: 
63806diff
changeset | 87 | |
| 
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
 wenzelm parents: 
63806diff
changeset | 88 | fun parse_time s = | 
| 
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
 wenzelm parents: 
63806diff
changeset | 89 | (case Time.fromString s of | 
| 
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
 wenzelm parents: 
63806diff
changeset | 90 | SOME x => x | 
| 
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
 wenzelm parents: 
63806diff
changeset | 91 |   | NONE => raise Fail ("Bad time " ^ quote s));
 | 
| 
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
 wenzelm parents: 
63806diff
changeset | 92 | |
| 
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
 wenzelm parents: 
63806diff
changeset | 93 | fun print_time x = | 
| 
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
 wenzelm parents: 
63806diff
changeset | 94 | 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: 
63806diff
changeset | 95 | else Time.toString x; | 
| 
1698e9ccef2d
more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
 wenzelm parents: 
63806diff
changeset | 96 | |
| 63806 | 97 | end; |