src/Pure/thm_name.ML
author wenzelm
Mon, 10 Jun 2024 14:53:54 +0200
changeset 80330 e01aae620437
parent 80311 31df11a23d6e
child 80590 505f97165f52
permissions -rw-r--r--
clarified signature: prefer internal Thm_Name.T over external Facts.ref;
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
80299
a397fd0c451a more accurate output of Thm_Name.T wrt. facts name space;
wenzelm
parents: 80297
diff changeset
    26
  val print_prefix: Context.generic -> Name_Space.T -> T -> Markup.T * string
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
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
    29
  val short: T -> string
80311
31df11a23d6e clarified signature: more explicit operations;
wenzelm
parents: 80305
diff changeset
    30
  val encode: T XML.Encode.T
31df11a23d6e clarified signature: more explicit operations;
wenzelm
parents: 80305
diff changeset
    31
  val decode: T XML.Decode.T
70574
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    32
end;
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    33
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    34
structure Thm_Name: THM_NAME =
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    35
struct
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    36
80297
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
    37
(* type T *)
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
    38
70574
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    39
type T = string * int;
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    40
val ord = prod_ord string_ord int_ord;
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    41
80295
8a9588ffc133 more accurate Thm_Name.T for PThm / Thm.name_derivation / Thm.derivation_name;
wenzelm
parents: 80289
diff changeset
    42
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
    43
structure Table = Table(Set.Key);
80289
40a6a6ac1669 tuned signature;
wenzelm
parents: 80286
diff changeset
    44
80295
8a9588ffc133 more accurate Thm_Name.T for PThm / Thm.name_derivation / Thm.derivation_name;
wenzelm
parents: 80289
diff changeset
    45
val empty: T = ("", 0);
8a9588ffc133 more accurate Thm_Name.T for PThm / Thm.name_derivation / Thm.derivation_name;
wenzelm
parents: 80289
diff changeset
    46
fun is_empty ((a, _): T) = a = "";
80286
00d68f324056 tuned signature: just one ZThm is sufficient;
wenzelm
parents: 79377
diff changeset
    47
80330
e01aae620437 clarified signature: prefer internal Thm_Name.T over external Facts.ref;
wenzelm
parents: 80311
diff changeset
    48
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
    49
  | 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
    50
70574
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    51
80297
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
    52
(* type P *)
80295
8a9588ffc133 more accurate Thm_Name.T for PThm / Thm.name_derivation / Thm.derivation_name;
wenzelm
parents: 80289
diff changeset
    53
8a9588ffc133 more accurate Thm_Name.T for PThm / Thm.name_derivation / Thm.derivation_name;
wenzelm
parents: 80289
diff changeset
    54
type P = T * Position.T;
8a9588ffc133 more accurate Thm_Name.T for PThm / Thm.name_derivation / Thm.derivation_name;
wenzelm
parents: 80289
diff changeset
    55
8a9588ffc133 more accurate Thm_Name.T for PThm / Thm.name_derivation / Thm.derivation_name;
wenzelm
parents: 80289
diff changeset
    56
val none: P = (empty, Position.none);
70574
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    57
80296
wenzelm
parents: 80295
diff changeset
    58
fun list (name, pos) [x] = [(((name, 0), pos): P, x)]
wenzelm
parents: 80295
diff changeset
    59
  | list ("", pos) xs = map (fn thm => ((empty, pos), thm)) xs
wenzelm
parents: 80295
diff changeset
    60
  | list (name, pos) xs = map_index (fn (i, thm) => (((name, i + 1), pos), thm)) xs;
70574
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
    61
79377
wenzelm
parents: 79376
diff changeset
    62
fun expr name = burrow_fst (burrow (list name));
79372
d02c8adce4e6 clarified modules;
wenzelm
parents: 79371
diff changeset
    63
80297
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
    64
80305
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    65
(* parse *)
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    66
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    67
local
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    68
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    69
fun is_bg c = c = #"(";
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    70
fun is_en c = c = #")";
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    71
fun is_digit c = #"0" <= c andalso c <= #"9";
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    72
fun get_digit c = Char.ord c - Char.ord #"0";
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    73
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    74
in
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    75
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    76
fun parse string =
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    77
  let
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    78
    val n = size string;
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    79
    fun char i = if 0 <= i andalso i < n then String.nth string i else #"\000";
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    80
    fun test pred i = pred (char i);
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    81
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    82
    fun scan i k m =
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    83
      let val c = char i in
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    84
        if is_digit c then scan (i - 1) (k * 10) (m + k * get_digit c)
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    85
        else if is_bg c then (String.substring (string, 0, i), m)
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    86
        else (string, 0)
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    87
      end;
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    88
  in
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    89
    if test is_en (n - 1) andalso test is_digit (n - 2)
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    90
    then scan (n - 2) 1 0
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    91
    else (string, 0)
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    92
  end;
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    93
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    94
end;
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    95
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    96
95b51df1382e more operationsd;
wenzelm
parents: 80299
diff changeset
    97
(* print *)
80297
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
    98
80299
a397fd0c451a more accurate output of Thm_Name.T wrt. facts name space;
wenzelm
parents: 80297
diff changeset
    99
fun print_prefix context space ((name, _): T) =
a397fd0c451a more accurate output of Thm_Name.T wrt. facts name space;
wenzelm
parents: 80297
diff changeset
   100
  if name = "" then (Markup.empty, "")
a397fd0c451a more accurate output of Thm_Name.T wrt. facts name space;
wenzelm
parents: 80297
diff changeset
   101
  else (Name_Space.markup space name, Name_Space.extern_generic context space name);
a397fd0c451a more accurate output of Thm_Name.T wrt. facts name space;
wenzelm
parents: 80297
diff changeset
   102
a397fd0c451a more accurate output of Thm_Name.T wrt. facts name space;
wenzelm
parents: 80297
diff changeset
   103
fun print_suffix (name, index) =
a397fd0c451a more accurate output of Thm_Name.T wrt. facts name space;
wenzelm
parents: 80297
diff changeset
   104
  if name = "" orelse index = 0 then ""
a397fd0c451a more accurate output of Thm_Name.T wrt. facts name space;
wenzelm
parents: 80297
diff changeset
   105
  else enclose "(" ")" (string_of_int index);
a397fd0c451a more accurate output of Thm_Name.T wrt. facts name space;
wenzelm
parents: 80297
diff changeset
   106
a397fd0c451a more accurate output of Thm_Name.T wrt. facts name space;
wenzelm
parents: 80297
diff changeset
   107
fun print (name, index) = name ^ print_suffix (name, index);
80297
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
   108
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
   109
fun short (name, index) =
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
   110
  if name = "" orelse index = 0 then name
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
   111
  else name ^ "_" ^ string_of_int index;
f38771b2df1c tuned structure;
wenzelm
parents: 80296
diff changeset
   112
80311
31df11a23d6e clarified signature: more explicit operations;
wenzelm
parents: 80305
diff changeset
   113
31df11a23d6e clarified signature: more explicit operations;
wenzelm
parents: 80305
diff changeset
   114
(* XML data representation *)
31df11a23d6e clarified signature: more explicit operations;
wenzelm
parents: 80305
diff changeset
   115
31df11a23d6e clarified signature: more explicit operations;
wenzelm
parents: 80305
diff changeset
   116
val encode = let open XML.Encode in pair string int end;
31df11a23d6e clarified signature: more explicit operations;
wenzelm
parents: 80305
diff changeset
   117
val decode = let open XML.Decode in pair string int end;
31df11a23d6e clarified signature: more explicit operations;
wenzelm
parents: 80305
diff changeset
   118
70574
503ca64329cc clarified modules;
wenzelm
parents:
diff changeset
   119
end;