src/Pure/facts.ML
author wenzelm
Tue Apr 15 16:12:11 2008 +0200 (2008-04-15)
changeset 26654 1f711934f221
parent 26393 42febbed5460
child 26664 167d879a89b0
permissions -rw-r--r--
disallow duplicate entries (weak version for merge);
added hide;
wenzelm@26281
     1
(*  Title:      Pure/facts.ML
wenzelm@26281
     2
    ID:         $Id$
wenzelm@26281
     3
    Author:     Makarius
wenzelm@26281
     4
wenzelm@26307
     5
Environment of named facts, optionally indexed by proposition.
wenzelm@26281
     6
*)
wenzelm@26281
     7
wenzelm@26281
     8
signature FACTS =
wenzelm@26281
     9
sig
wenzelm@26336
    10
  val the_single: string -> thm list -> thm
wenzelm@26336
    11
  datatype interval = FromTo of int * int | From of int | Single of int
wenzelm@26336
    12
  datatype ref =
wenzelm@26361
    13
    Named of (string * Position.T) * interval list option |
wenzelm@26336
    14
    Fact of string
wenzelm@26361
    15
  val named: string -> ref
wenzelm@26336
    16
  val string_of_ref: ref -> string
wenzelm@26336
    17
  val name_of_ref: ref -> string
wenzelm@26366
    18
  val pos_of_ref: ref -> Position.T
wenzelm@26336
    19
  val map_name_of_ref: (string -> string) -> ref -> ref
wenzelm@26336
    20
  val select: ref -> thm list -> thm list
wenzelm@26336
    21
  val selections: string * thm list -> (ref * thm) list
wenzelm@26281
    22
  type T
wenzelm@26393
    23
  val empty: T
wenzelm@26281
    24
  val space_of: T -> NameSpace.T
wenzelm@26393
    25
  val lookup: Context.generic -> T -> string -> thm list option
wenzelm@26281
    26
  val dest: T -> (string * thm list) list
wenzelm@26281
    27
  val extern: T -> (xstring * thm list) list
wenzelm@26281
    28
  val props: T -> thm list
wenzelm@26281
    29
  val could_unify: T -> term -> thm list
wenzelm@26281
    30
  val merge: T * T -> T
wenzelm@26307
    31
  val add_global: NameSpace.naming -> string * thm list -> T -> T
wenzelm@26307
    32
  val add_local: bool -> NameSpace.naming -> string * thm list -> T -> T
wenzelm@26393
    33
  val add_dynamic: NameSpace.naming -> string * (Context.generic -> thm list) -> T -> T
wenzelm@26654
    34
  val del: string -> T -> T
wenzelm@26654
    35
  val hide: bool -> string -> T -> T
wenzelm@26281
    36
end;
wenzelm@26281
    37
wenzelm@26281
    38
structure Facts: FACTS =
wenzelm@26281
    39
struct
wenzelm@26281
    40
wenzelm@26336
    41
(** fact references **)
wenzelm@26336
    42
wenzelm@26336
    43
fun the_single _ [th] : thm = th
wenzelm@26336
    44
  | the_single name _ = error ("Single theorem expected " ^ quote name);
wenzelm@26336
    45
wenzelm@26336
    46
wenzelm@26336
    47
(* datatype interval *)
wenzelm@26336
    48
wenzelm@26336
    49
datatype interval =
wenzelm@26336
    50
  FromTo of int * int |
wenzelm@26336
    51
  From of int |
wenzelm@26336
    52
  Single of int;
wenzelm@26336
    53
wenzelm@26336
    54
fun string_of_interval (FromTo (i, j)) = string_of_int i ^ "-" ^ string_of_int j
wenzelm@26336
    55
  | string_of_interval (From i) = string_of_int i ^ "-"
wenzelm@26336
    56
  | string_of_interval (Single i) = string_of_int i;
wenzelm@26336
    57
wenzelm@26336
    58
fun interval n iv =
wenzelm@26336
    59
  let fun err () = raise Fail ("Bad interval specification " ^ string_of_interval iv) in
wenzelm@26336
    60
    (case iv of
wenzelm@26336
    61
      FromTo (i, j) => if i <= j then i upto j else err ()
wenzelm@26336
    62
    | From i => if i <= n then i upto n else err ()
wenzelm@26336
    63
    | Single i => [i])
wenzelm@26336
    64
  end;
wenzelm@26336
    65
wenzelm@26336
    66
wenzelm@26336
    67
(* datatype ref *)
wenzelm@26336
    68
wenzelm@26336
    69
datatype ref =
wenzelm@26361
    70
  Named of (string * Position.T) * interval list option |
wenzelm@26336
    71
  Fact of string;
wenzelm@26336
    72
wenzelm@26361
    73
fun named name = Named ((name, Position.none), NONE);
wenzelm@26361
    74
wenzelm@26366
    75
fun name_pos_of_ref (Named (name_pos, _)) = name_pos
wenzelm@26366
    76
  | name_pos_of_ref (Fact _) = error "Illegal literal fact";
wenzelm@26366
    77
wenzelm@26366
    78
val name_of_ref = #1 o name_pos_of_ref;
wenzelm@26366
    79
val pos_of_ref = #2 o name_pos_of_ref;
wenzelm@26336
    80
wenzelm@26361
    81
fun map_name_of_ref f (Named ((name, pos), is)) = Named ((f name, pos), is)
wenzelm@26336
    82
  | map_name_of_ref _ r = r;
wenzelm@26336
    83
wenzelm@26361
    84
fun string_of_ref (Named ((name, _), NONE)) = name
wenzelm@26361
    85
  | string_of_ref (Named ((name, _), SOME is)) =
wenzelm@26336
    86
      name ^ enclose "(" ")" (commas (map string_of_interval is))
wenzelm@26336
    87
  | string_of_ref (Fact _) = error "Illegal literal fact";
wenzelm@26336
    88
wenzelm@26336
    89
wenzelm@26336
    90
(* select *)
wenzelm@26336
    91
wenzelm@26336
    92
fun select (Fact _) ths = ths
wenzelm@26336
    93
  | select (Named (_, NONE)) ths = ths
wenzelm@26361
    94
  | select (Named ((name, pos), SOME ivs)) ths =
wenzelm@26336
    95
      let
wenzelm@26336
    96
        val n = length ths;
wenzelm@26361
    97
        fun err msg =
wenzelm@26361
    98
          error (msg ^ " for " ^ quote name ^ " (length " ^ string_of_int n ^ ")" ^
wenzelm@26361
    99
            Position.str_of pos);
wenzelm@26336
   100
        fun sel i =
wenzelm@26336
   101
          if i < 1 orelse i > n then err ("Bad subscript " ^ string_of_int i)
wenzelm@26336
   102
          else nth ths (i - 1);
wenzelm@26336
   103
        val is = maps (interval n) ivs handle Fail msg => err msg;
wenzelm@26336
   104
      in map sel is end;
wenzelm@26336
   105
wenzelm@26336
   106
wenzelm@26336
   107
(* selections *)
wenzelm@26336
   108
wenzelm@26361
   109
fun selections (name, [th]) = [(Named ((name, Position.none), NONE), th)]
wenzelm@26361
   110
  | selections (name, ths) = map2 (fn i => fn th =>
wenzelm@26361
   111
      (Named ((name, Position.none), SOME [Single i]), th)) (1 upto length ths) ths;
wenzelm@26336
   112
wenzelm@26336
   113
wenzelm@26336
   114
wenzelm@26336
   115
(** fact environment **)
wenzelm@26336
   116
wenzelm@26393
   117
(* datatypes *)
wenzelm@26393
   118
wenzelm@26393
   119
datatype fact = Static of thm list | Dynamic of Context.generic -> thm list;
wenzelm@26281
   120
wenzelm@26281
   121
datatype T = Facts of
wenzelm@26393
   122
 {facts: (fact * serial) NameSpace.table,
wenzelm@26281
   123
  props: thm Net.net};
wenzelm@26281
   124
wenzelm@26281
   125
fun make_facts facts props = Facts {facts = facts, props = props};
wenzelm@26393
   126
val empty = make_facts NameSpace.empty_table Net.empty;
wenzelm@26281
   127
wenzelm@26281
   128
wenzelm@26281
   129
(* named facts *)
wenzelm@26281
   130
wenzelm@26281
   131
fun facts_of (Facts {facts, ...}) = facts;
wenzelm@26281
   132
wenzelm@26281
   133
val space_of = #1 o facts_of;
wenzelm@26393
   134
val table_of = #2 o facts_of;
wenzelm@26393
   135
wenzelm@26393
   136
fun lookup context facts name =
wenzelm@26393
   137
  (case Symtab.lookup (table_of facts) name of
wenzelm@26393
   138
    NONE => NONE
wenzelm@26393
   139
  | SOME (Static ths, _) => SOME ths
wenzelm@26393
   140
  | SOME (Dynamic f, _) => SOME (f context));
wenzelm@26393
   141
wenzelm@26393
   142
fun dest facts =
wenzelm@26393
   143
  Symtab.fold (fn (name, (Static ths, _)) => cons (name, ths) | _ => I) (table_of facts) [];
wenzelm@26393
   144
wenzelm@26393
   145
fun extern facts =
wenzelm@26393
   146
  dest facts
wenzelm@26393
   147
  |> map (apfst (NameSpace.extern (space_of facts)))
wenzelm@26393
   148
  |> sort_wrt #1;
wenzelm@26281
   149
wenzelm@26281
   150
wenzelm@26281
   151
(* indexed props *)
wenzelm@26281
   152
wenzelm@26281
   153
val prop_ord = Term.term_ord o pairself Thm.full_prop_of;
wenzelm@26281
   154
wenzelm@26281
   155
fun props (Facts {props, ...}) = sort_distinct prop_ord (Net.content props);
wenzelm@26281
   156
fun could_unify (Facts {props, ...}) = Net.unify_term props;
wenzelm@26281
   157
wenzelm@26281
   158
wenzelm@26393
   159
(* merge facts *)
wenzelm@26281
   160
wenzelm@26393
   161
fun err_dup_fact name = error ("Duplicate fact " ^ quote name);
wenzelm@26281
   162
wenzelm@26654
   163
fun eq_entry ((Static ths1, _), (Static ths2, _)) = is_equal (list_ord Thm.thm_ord (ths1, ths2))
wenzelm@26654
   164
  | eq_entry ((_, id1: serial), (_, id2)) = id1 = id2;
wenzelm@26654
   165
wenzelm@26281
   166
fun merge (Facts {facts = facts1, props = props1}, Facts {facts = facts2, props = props2}) =
wenzelm@26281
   167
  let
wenzelm@26654
   168
    val facts' = NameSpace.merge_tables eq_entry (facts1, facts2)
wenzelm@26654
   169
      handle Symtab.DUP dup => err_dup_fact dup;
wenzelm@26281
   170
    val props' = Net.merge (is_equal o prop_ord) (props1, props2);
wenzelm@26281
   171
  in make_facts facts' props' end;
wenzelm@26281
   172
wenzelm@26393
   173
wenzelm@26654
   174
(* add static entries *)
wenzelm@26393
   175
wenzelm@26307
   176
fun add permissive do_props naming (name, ths) (Facts {facts, props}) =
wenzelm@26281
   177
  let
wenzelm@26281
   178
    val facts' =
wenzelm@26281
   179
      if name = "" then facts
wenzelm@26281
   180
      else
wenzelm@26281
   181
        let
wenzelm@26281
   182
          val (space, tab) = facts;
wenzelm@26281
   183
          val space' = NameSpace.declare naming name space;
wenzelm@26393
   184
          val entry = (name, (Static ths, serial ()));
wenzelm@26393
   185
          val tab' = (if permissive then Symtab.update else Symtab.update_new) entry tab
wenzelm@26654
   186
            handle Symtab.DUP dup => err_dup_fact dup;
wenzelm@26281
   187
        in (space', tab') end;
wenzelm@26281
   188
    val props' =
wenzelm@26281
   189
      if do_props then fold (fn th => Net.insert_term (K false) (Thm.full_prop_of th, th)) ths props
wenzelm@26281
   190
      else props;
wenzelm@26281
   191
  in make_facts facts' props' end;
wenzelm@26281
   192
wenzelm@26307
   193
val add_global = add false false;
wenzelm@26307
   194
val add_local = add true;
wenzelm@26307
   195
wenzelm@26393
   196
wenzelm@26654
   197
(* add dynamic entries *)
wenzelm@26393
   198
wenzelm@26393
   199
fun add_dynamic naming (name, f) (Facts {facts = (space, tab), props}) =
wenzelm@26290
   200
  let
wenzelm@26393
   201
    val space' = NameSpace.declare naming name space;
wenzelm@26393
   202
    val entry = (name, (Dynamic f, serial ()));
wenzelm@26393
   203
    val tab' = Symtab.update_new entry tab
wenzelm@26393
   204
      handle Symtab.DUP dup => err_dup_fact dup;
wenzelm@26290
   205
  in make_facts (space', tab') props end;
wenzelm@26281
   206
wenzelm@26654
   207
wenzelm@26654
   208
(* remove entries *)
wenzelm@26654
   209
wenzelm@26654
   210
fun del name (Facts {facts = (space, tab), props}) =
wenzelm@26654
   211
  let
wenzelm@26654
   212
    val space' = NameSpace.hide true name space handle ERROR _ => space;
wenzelm@26654
   213
    val tab' = Symtab.delete_safe name tab;
wenzelm@26654
   214
  in make_facts (space', tab') props end;
wenzelm@26654
   215
wenzelm@26654
   216
fun hide fully name (Facts {facts = (space, tab), props}) =
wenzelm@26654
   217
  make_facts (NameSpace.hide fully name space, tab) props;
wenzelm@26654
   218
wenzelm@26281
   219
end;