src/Pure/thm_name.ML
author wenzelm
Mon, 02 Dec 2024 11:36:53 +0100
changeset 81535 db073d1733ab
parent 80590 505f97165f52
permissions -rw-r--r--
clarified signature: uniform context;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
70574
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/thm_name.ML
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
     3
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
     4
Systematic naming of individual theorems, as selections from multi-facts.
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
     5
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
     6
  (NAME, 0): the single entry of a singleton fact NAME
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
     7
  (NAME, i): entry i of a non-singleton fact (1 <= i <= length)
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
     8
*)
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
     9
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    10
signature THM_NAME =
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    11
sig
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    12
  type T = string * int
70586
57df8a85317a clarified signature;
wenzelm
parents: 70575
diff changeset
    13
  val ord: T ord
80295
8a9588ffc133 more accurate Thm_Name.T for PThm / Thm.name_derivation / Thm.derivation_name;
wenzelm
parents: 80289
diff changeset
    14
  structure Set: SET
8a9588ffc133 more accurate Thm_Name.T for PThm / Thm.name_derivation / Thm.derivation_name;
wenzelm
parents: 80289
diff changeset
    15
  structure Table: TABLE
8a9588ffc133 more accurate Thm_Name.T for PThm / Thm.name_derivation / Thm.derivation_name;
wenzelm
parents: 80289
diff changeset
    16
  val empty: T
8a9588ffc133 more accurate Thm_Name.T for PThm / Thm.name_derivation / Thm.derivation_name;
wenzelm
parents: 80289
diff changeset
    17
  val is_empty: T -> bool
80330
e01aae620437 clarified signature: prefer internal Thm_Name.T over external Facts.ref;
wenzelm
parents: 80311
diff changeset
    18
  val make_list: string * 'a list -> (T * 'a) list
80297
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
    19
80289
40a6a6ac1669 tuned signature;
wenzelm
parents: 80286
diff changeset
    20
  type P = T * Position.T
40a6a6ac1669 tuned signature;
wenzelm
parents: 80286
diff changeset
    21
  val none: P
40a6a6ac1669 tuned signature;
wenzelm
parents: 80286
diff changeset
    22
  val list: string * Position.T -> 'a list -> (P * 'a) list
40a6a6ac1669 tuned signature;
wenzelm
parents: 80286
diff changeset
    23
  val expr: string * Position.T -> ('a list * 'b) list -> ((P * 'a) list * 'b) list
80297
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
    24
80305
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    25
  val parse: string -> T
81535
db073d1733ab clarified signature: uniform context;
wenzelm
parents: 80590
diff changeset
    26
  val print_prefix: Proof.context -> Name_Space.T -> T -> Markup.T * string
80299
a397fd0c451a more accurate output of Thm_Name.T wrt. facts name space;
wenzelm
parents: 80297
diff changeset
    27
  val print_suffix: T -> string
80297
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
    28
  val print: T -> string
80590
505f97165f52 clarified thm_header command_pos vs. thm_pos;
wenzelm
parents: 80330
diff changeset
    29
  val print_pos: P -> string
80297
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
    30
  val short: T -> string
80311
31df11a23d6e clarified signature: more explicit operations;
wenzelm
parents: 80305
diff changeset
    31
  val encode: T XML.Encode.T
31df11a23d6e clarified signature: more explicit operations;
wenzelm
parents: 80305
diff changeset
    32
  val decode: T XML.Decode.T
80590
505f97165f52 clarified thm_header command_pos vs. thm_pos;
wenzelm
parents: 80330
diff changeset
    33
  val encode_pos: P XML.Encode.T
505f97165f52 clarified thm_header command_pos vs. thm_pos;
wenzelm
parents: 80330
diff changeset
    34
  val decode_pos: P XML.Decode.T
70574
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    35
end;
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    36
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    37
structure Thm_Name: THM_NAME =
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    38
struct
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    39
80297
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
    40
(* type T *)
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
    41
70574
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    42
type T = string * int;
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    43
val ord = prod_ord string_ord int_ord;
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    44
80295
8a9588ffc133 more accurate Thm_Name.T for PThm / Thm.name_derivation / Thm.derivation_name;
wenzelm
parents: 80289
diff changeset
    45
structure Set = Set(type key = T val ord = ord);
8a9588ffc133 more accurate Thm_Name.T for PThm / Thm.name_derivation / Thm.derivation_name;
wenzelm
parents: 80289
diff changeset
    46
structure Table = Table(Set.Key);
80289
40a6a6ac1669 tuned signature;
wenzelm
parents: 80286
diff changeset
    47
80295
8a9588ffc133 more accurate Thm_Name.T for PThm / Thm.name_derivation / Thm.derivation_name;
wenzelm
parents: 80289
diff changeset
    48
val empty: T = ("", 0);
8a9588ffc133 more accurate Thm_Name.T for PThm / Thm.name_derivation / Thm.derivation_name;
wenzelm
parents: 80289
diff changeset
    49
fun is_empty ((a, _): T) = a = "";
80286
00d68f324056 tuned signature: just one ZThm is sufficient;
wenzelm
parents: 79377
diff changeset
    50
80330
e01aae620437 clarified signature: prefer internal Thm_Name.T over external Facts.ref;
wenzelm
parents: 80311
diff changeset
    51
fun make_list (a, [b]) = [((a, 0): T, b)]
e01aae620437 clarified signature: prefer internal Thm_Name.T over external Facts.ref;
wenzelm
parents: 80311
diff changeset
    52
  | make_list (a, bs) = map_index (fn (i, b) => ((a, i + 1), b)) bs;
e01aae620437 clarified signature: prefer internal Thm_Name.T over external Facts.ref;
wenzelm
parents: 80311
diff changeset
    53
70574
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    54
80297
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
    55
(* type P *)
80295
8a9588ffc133 more accurate Thm_Name.T for PThm / Thm.name_derivation / Thm.derivation_name;
wenzelm
parents: 80289
diff changeset
    56
8a9588ffc133 more accurate Thm_Name.T for PThm / Thm.name_derivation / Thm.derivation_name;
wenzelm
parents: 80289
diff changeset
    57
type P = T * Position.T;
8a9588ffc133 more accurate Thm_Name.T for PThm / Thm.name_derivation / Thm.derivation_name;
wenzelm
parents: 80289
diff changeset
    58
8a9588ffc133 more accurate Thm_Name.T for PThm / Thm.name_derivation / Thm.derivation_name;
wenzelm
parents: 80289
diff changeset
    59
val none: P = (empty, Position.none);
70574
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    60
80296
wenzelm
parents: 80295
diff changeset
    61
fun list (name, pos) [x] = [(((name, 0), pos): P, x)]
wenzelm
parents: 80295
diff changeset
    62
  | list ("", pos) xs = map (fn thm => ((empty, pos), thm)) xs
wenzelm
parents: 80295
diff changeset
    63
  | list (name, pos) xs = map_index (fn (i, thm) => (((name, i + 1), pos), thm)) xs;
70574
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    64
79377
wenzelm
parents: 79376
diff changeset
    65
fun expr name = burrow_fst (burrow (list name));
79372
d02c8adce4e6 clarified modules;
wenzelm
parents: 79371
diff changeset
    66
80297
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
    67
80305
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    68
(* parse *)
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    69
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    70
local
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    71
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    72
fun is_bg c = c = #"(";
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    73
fun is_en c = c = #")";
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    74
fun is_digit c = #"0" <= c andalso c <= #"9";
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    75
fun get_digit c = Char.ord c - Char.ord #"0";
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    76
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    77
in
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    78
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    79
fun parse string =
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    80
  let
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    81
    val n = size string;
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    82
    fun char i = if 0 <= i andalso i < n then String.nth string i else #"\000";
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    83
    fun test pred i = pred (char i);
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    84
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    85
    fun scan i k m =
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    86
      let val c = char i in
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    87
        if is_digit c then scan (i - 1) (k * 10) (m + k * get_digit c)
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    88
        else if is_bg c then (String.substring (string, 0, i), m)
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    89
        else (string, 0)
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    90
      end;
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    91
  in
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    92
    if test is_en (n - 1) andalso test is_digit (n - 2)
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    93
    then scan (n - 2) 1 0
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    94
    else (string, 0)
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    95
  end;
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    96
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    97
end;
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    98
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    99
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
   100
(* print *)
80297
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
   101
81535
db073d1733ab clarified signature: uniform context;
wenzelm
parents: 80590
diff changeset
   102
fun print_prefix ctxt space ((name, _): T) =
80299
a397fd0c451a more accurate output of Thm_Name.T wrt. facts name space;
wenzelm
parents: 80297
diff changeset
   103
  if name = "" then (Markup.empty, "")
81535
db073d1733ab clarified signature: uniform context;
wenzelm
parents: 80590
diff changeset
   104
  else (Name_Space.markup space name, Name_Space.extern ctxt space name);
80299
a397fd0c451a more accurate output of Thm_Name.T wrt. facts name space;
wenzelm
parents: 80297
diff changeset
   105
a397fd0c451a more accurate output of Thm_Name.T wrt. facts name space;
wenzelm
parents: 80297
diff changeset
   106
fun print_suffix (name, index) =
a397fd0c451a more accurate output of Thm_Name.T wrt. facts name space;
wenzelm
parents: 80297
diff changeset
   107
  if name = "" orelse index = 0 then ""
a397fd0c451a more accurate output of Thm_Name.T wrt. facts name space;
wenzelm
parents: 80297
diff changeset
   108
  else enclose "(" ")" (string_of_int index);
a397fd0c451a more accurate output of Thm_Name.T wrt. facts name space;
wenzelm
parents: 80297
diff changeset
   109
a397fd0c451a more accurate output of Thm_Name.T wrt. facts name space;
wenzelm
parents: 80297
diff changeset
   110
fun print (name, index) = name ^ print_suffix (name, index);
80297
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
   111
80590
505f97165f52 clarified thm_header command_pos vs. thm_pos;
wenzelm
parents: 80330
diff changeset
   112
fun print_pos (thm_name, pos) = print thm_name ^ Position.here pos;
505f97165f52 clarified thm_header command_pos vs. thm_pos;
wenzelm
parents: 80330
diff changeset
   113
80297
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
   114
fun short (name, index) =
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
   115
  if name = "" orelse index = 0 then name
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
   116
  else name ^ "_" ^ string_of_int index;
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
   117
80311
31df11a23d6e clarified signature: more explicit operations;
wenzelm
parents: 80305
diff changeset
   118
31df11a23d6e clarified signature: more explicit operations;
wenzelm
parents: 80305
diff changeset
   119
(* XML data representation *)
31df11a23d6e clarified signature: more explicit operations;
wenzelm
parents: 80305
diff changeset
   120
31df11a23d6e clarified signature: more explicit operations;
wenzelm
parents: 80305
diff changeset
   121
val encode = let open XML.Encode in pair string int end;
31df11a23d6e clarified signature: more explicit operations;
wenzelm
parents: 80305
diff changeset
   122
val decode = let open XML.Decode in pair string int end;
31df11a23d6e clarified signature: more explicit operations;
wenzelm
parents: 80305
diff changeset
   123
80590
505f97165f52 clarified thm_header command_pos vs. thm_pos;
wenzelm
parents: 80330
diff changeset
   124
val encode_pos = let open XML.Encode in pair encode (properties o Position.properties_of) end;
505f97165f52 clarified thm_header command_pos vs. thm_pos;
wenzelm
parents: 80330
diff changeset
   125
val decode_pos = let open XML.Decode in pair decode (Position.of_properties o properties) end;
505f97165f52 clarified thm_header command_pos vs. thm_pos;
wenzelm
parents: 80330
diff changeset
   126
70574
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
   127
end;