src/Pure/General/value.ML
author wenzelm
Wed, 12 Mar 2025 11:39:00 +0100
changeset 82265 4b875a4c83b0
parent 82092 93195437fdee
permissions -rw-r--r--
update for release;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
63806
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/General/value.ML
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
     3
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
     4
Plain values, represented as string.
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
     5
*)
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
     6
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
     7
signature VALUE =
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
     8
sig
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
     9
  val parse_bool: string -> bool
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    10
  val print_bool: bool -> string
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    11
  val parse_nat: string -> int
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    12
  val parse_int: string -> int
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    13
  val print_int: int -> string
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    14
  val parse_real: string -> real
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    15
  val print_real: real -> string
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    16
end;
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    17
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    18
structure Value: VALUE =
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    19
struct
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    20
67000
1698e9ccef2d more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents: 63806
diff changeset
    21
(* bool *)
1698e9ccef2d more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents: 63806
diff changeset
    22
63806
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    23
fun parse_bool "true" = true
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    24
  | parse_bool "false" = false
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    25
  | parse_bool s = raise Fail ("Bad boolean " ^ quote s);
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    26
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    27
val print_bool = Bool.toString;
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    28
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    29
67000
1698e9ccef2d more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents: 63806
diff changeset
    30
(* nat and int *)
1698e9ccef2d more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents: 63806
diff changeset
    31
77775
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    32
local
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    33
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    34
val c0 = Char.ord #"0";
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    35
val c9 = Char.ord #"9";
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    36
val minus = Char.ord #"-";
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    37
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    38
in
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    39
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    40
fun parse_int str =
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    41
  let
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    42
    fun err () = raise Fail ("Bad integer " ^ quote str);
77847
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents: 77775
diff changeset
    43
    val char = Char.ord o String.nth str;
77775
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    44
    val n = size str;
67218
e62d72699666 more accurate parse_nat/parse_int: avoid corner cases of Int.fromString (e.g. "1.0");
wenzelm
parents: 67000
diff changeset
    45
77775
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    46
    fun digits i a =
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    47
      if i = n then a
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    48
      else
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    49
        let val c = char i
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    50
        in if c0 <= c andalso c <= c9 then digits (i + 1) (10 * a + (c - c0)) else err () end;
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    51
  in
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    52
    if n = 0 then err ()
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    53
    else if char 0 <> minus then digits 0 0
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    54
    else if n = 1 then err ()
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    55
    else ~ (digits 1 0)
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    56
  end;
63806
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    57
77775
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    58
fun parse_nat str =
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    59
  let
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    60
    fun err () = raise Fail ("Bad natural number " ^ quote str);
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    61
    val i = parse_int str handle Fail _ => err ();
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    62
  in if i >= 0 then i else err () end;
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    63
3cc55085d490 minor performance tuning;
wenzelm
parents: 67218
diff changeset
    64
end;
63806
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    65
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    66
val print_int = signed_string_of_int;
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    67
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    68
67000
1698e9ccef2d more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents: 63806
diff changeset
    69
(* real *)
1698e9ccef2d more portable print_time, notably for occasional negative (!) elapsed time of theory_timing;
wenzelm
parents: 63806
diff changeset
    70
63806
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    71
fun parse_real s =
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    72
  (case Real.fromString s of
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    73
    SOME x => x
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    74
  | NONE => raise Fail ("Bad real " ^ quote s));
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    75
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    76
fun print_real x =
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    77
  let val s = signed_string_of_real x in
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    78
    (case space_explode "." s of
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    79
      [a, b] => if forall_string (fn c => c = "0") b then a else s
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    80
    | _ => s)
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    81
  end;
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    82
c54a53ef1873 clarified modules;
wenzelm
parents:
diff changeset
    83
end;