src/Pure/facts.ML
author wenzelm
Thu Sep 02 00:48:07 2010 +0200 (2010-09-02)
changeset 38980 af73cf0dc31f
parent 35408 b48ab741683b
child 42358 b47d41d9f4b5
permissions -rw-r--r--
turned show_question_marks into proper configuration option;
show_question_marks only affects regular type/term pretty printing, not raw Term.string_of_vname;
tuned;
wenzelm@26281
     1
(*  Title:      Pure/facts.ML
wenzelm@26281
     2
    Author:     Makarius
wenzelm@26281
     3
wenzelm@26307
     4
Environment of named facts, optionally indexed by proposition.
wenzelm@26281
     5
*)
wenzelm@26281
     6
wenzelm@26281
     7
signature FACTS =
wenzelm@26281
     8
sig
wenzelm@26336
     9
  val the_single: string -> thm list -> thm
wenzelm@26336
    10
  datatype interval = FromTo of int * int | From of int | Single of int
wenzelm@26336
    11
  datatype ref =
wenzelm@26361
    12
    Named of (string * Position.T) * interval list option |
wenzelm@26336
    13
    Fact of string
wenzelm@26361
    14
  val named: string -> ref
wenzelm@26336
    15
  val string_of_ref: ref -> string
wenzelm@26336
    16
  val name_of_ref: ref -> string
wenzelm@26366
    17
  val pos_of_ref: ref -> Position.T
wenzelm@26336
    18
  val map_name_of_ref: (string -> string) -> ref -> ref
wenzelm@26336
    19
  val select: ref -> thm list -> thm list
wenzelm@26336
    20
  val selections: string * thm list -> (ref * thm) list
wenzelm@26281
    21
  type T
wenzelm@26393
    22
  val empty: T
wenzelm@33095
    23
  val space_of: T -> Name_Space.T
wenzelm@33163
    24
  val is_concealed: T -> string -> bool
wenzelm@26664
    25
  val intern: T -> xstring -> string
wenzelm@26664
    26
  val extern: T -> string -> xstring
wenzelm@27738
    27
  val lookup: Context.generic -> T -> string -> (bool * thm list) option
wenzelm@26692
    28
  val defined: T -> string -> bool
wenzelm@26692
    29
  val fold_static: (string * thm list -> 'a -> 'a) -> T -> 'a -> 'a
wenzelm@27175
    30
  val dest_static: T list -> T -> (string * thm list) list
wenzelm@27175
    31
  val extern_static: T list -> T -> (xstring * thm list) list
wenzelm@26281
    32
  val props: T -> thm list
wenzelm@26281
    33
  val could_unify: T -> term -> thm list
wenzelm@26281
    34
  val merge: T * T -> T
wenzelm@33095
    35
  val add_global: Name_Space.naming -> binding * thm list -> T -> string * T
wenzelm@33095
    36
  val add_local: bool -> Name_Space.naming -> binding * thm list -> T -> string * T
wenzelm@33095
    37
  val add_dynamic: Name_Space.naming -> binding * (Context.generic -> thm list) -> T -> string * T
wenzelm@26654
    38
  val del: string -> T -> T
wenzelm@26654
    39
  val hide: bool -> string -> T -> T
wenzelm@26281
    40
end;
wenzelm@26281
    41
wenzelm@26281
    42
structure Facts: FACTS =
wenzelm@26281
    43
struct
wenzelm@26281
    44
wenzelm@26336
    45
(** fact references **)
wenzelm@26336
    46
wenzelm@26336
    47
fun the_single _ [th] : thm = th
wenzelm@33163
    48
  | the_single name _ = error ("Expected singleton fact " ^ quote name);
wenzelm@26336
    49
wenzelm@26336
    50
wenzelm@26336
    51
(* datatype interval *)
wenzelm@26336
    52
wenzelm@26336
    53
datatype interval =
wenzelm@26336
    54
  FromTo of int * int |
wenzelm@26336
    55
  From of int |
wenzelm@26336
    56
  Single of int;
wenzelm@26336
    57
wenzelm@26336
    58
fun string_of_interval (FromTo (i, j)) = string_of_int i ^ "-" ^ string_of_int j
wenzelm@26336
    59
  | string_of_interval (From i) = string_of_int i ^ "-"
wenzelm@26336
    60
  | string_of_interval (Single i) = string_of_int i;
wenzelm@26336
    61
wenzelm@26336
    62
fun interval n iv =
wenzelm@26336
    63
  let fun err () = raise Fail ("Bad interval specification " ^ string_of_interval iv) in
wenzelm@26336
    64
    (case iv of
wenzelm@26336
    65
      FromTo (i, j) => if i <= j then i upto j else err ()
wenzelm@26336
    66
    | From i => if i <= n then i upto n else err ()
wenzelm@26336
    67
    | Single i => [i])
wenzelm@26336
    68
  end;
wenzelm@26336
    69
wenzelm@26336
    70
wenzelm@26336
    71
(* datatype ref *)
wenzelm@26336
    72
wenzelm@26336
    73
datatype ref =
wenzelm@26361
    74
  Named of (string * Position.T) * interval list option |
wenzelm@26336
    75
  Fact of string;
wenzelm@26336
    76
wenzelm@26361
    77
fun named name = Named ((name, Position.none), NONE);
wenzelm@26361
    78
wenzelm@26366
    79
fun name_pos_of_ref (Named (name_pos, _)) = name_pos
wenzelm@26366
    80
  | name_pos_of_ref (Fact _) = error "Illegal literal fact";
wenzelm@26366
    81
wenzelm@26366
    82
val name_of_ref = #1 o name_pos_of_ref;
wenzelm@26366
    83
val pos_of_ref = #2 o name_pos_of_ref;
wenzelm@26336
    84
wenzelm@26361
    85
fun map_name_of_ref f (Named ((name, pos), is)) = Named ((f name, pos), is)
wenzelm@26336
    86
  | map_name_of_ref _ r = r;
wenzelm@26336
    87
wenzelm@26361
    88
fun string_of_ref (Named ((name, _), NONE)) = name
wenzelm@26361
    89
  | string_of_ref (Named ((name, _), SOME is)) =
wenzelm@26336
    90
      name ^ enclose "(" ")" (commas (map string_of_interval is))
wenzelm@26336
    91
  | string_of_ref (Fact _) = error "Illegal literal fact";
wenzelm@26336
    92
wenzelm@26336
    93
wenzelm@26336
    94
(* select *)
wenzelm@26336
    95
wenzelm@26336
    96
fun select (Fact _) ths = ths
wenzelm@26336
    97
  | select (Named (_, NONE)) ths = ths
wenzelm@26361
    98
  | select (Named ((name, pos), SOME ivs)) ths =
wenzelm@26336
    99
      let
wenzelm@26336
   100
        val n = length ths;
wenzelm@26361
   101
        fun err msg =
wenzelm@26361
   102
          error (msg ^ " for " ^ quote name ^ " (length " ^ string_of_int n ^ ")" ^
wenzelm@26361
   103
            Position.str_of pos);
wenzelm@26336
   104
        fun sel i =
wenzelm@26336
   105
          if i < 1 orelse i > n then err ("Bad subscript " ^ string_of_int i)
wenzelm@26336
   106
          else nth ths (i - 1);
wenzelm@26336
   107
        val is = maps (interval n) ivs handle Fail msg => err msg;
wenzelm@26336
   108
      in map sel is end;
wenzelm@26336
   109
wenzelm@26336
   110
wenzelm@26336
   111
(* selections *)
wenzelm@26336
   112
wenzelm@26361
   113
fun selections (name, [th]) = [(Named ((name, Position.none), NONE), th)]
wenzelm@26361
   114
  | selections (name, ths) = map2 (fn i => fn th =>
wenzelm@26361
   115
      (Named ((name, Position.none), SOME [Single i]), th)) (1 upto length ths) ths;
wenzelm@26336
   116
wenzelm@26336
   117
wenzelm@26336
   118
wenzelm@26336
   119
(** fact environment **)
wenzelm@26336
   120
wenzelm@26393
   121
(* datatypes *)
wenzelm@26393
   122
wenzelm@26393
   123
datatype fact = Static of thm list | Dynamic of Context.generic -> thm list;
wenzelm@26281
   124
wenzelm@26281
   125
datatype T = Facts of
wenzelm@33095
   126
 {facts: fact Name_Space.table,
wenzelm@26281
   127
  props: thm Net.net};
wenzelm@26281
   128
wenzelm@26281
   129
fun make_facts facts props = Facts {facts = facts, props = props};
wenzelm@33163
   130
wenzelm@33096
   131
val empty = make_facts (Name_Space.empty_table "fact") Net.empty;
wenzelm@26281
   132
wenzelm@26281
   133
wenzelm@26281
   134
(* named facts *)
wenzelm@26281
   135
wenzelm@26281
   136
fun facts_of (Facts {facts, ...}) = facts;
wenzelm@26281
   137
wenzelm@26281
   138
val space_of = #1 o facts_of;
wenzelm@26393
   139
val table_of = #2 o facts_of;
wenzelm@26393
   140
wenzelm@33163
   141
val is_concealed = Name_Space.is_concealed o space_of;
wenzelm@33163
   142
wenzelm@33095
   143
val intern = Name_Space.intern o space_of;
wenzelm@33095
   144
val extern = Name_Space.extern o space_of;
wenzelm@26664
   145
wenzelm@26692
   146
val defined = Symtab.defined o table_of;
wenzelm@26692
   147
wenzelm@26393
   148
fun lookup context facts name =
wenzelm@26393
   149
  (case Symtab.lookup (table_of facts) name of
wenzelm@26393
   150
    NONE => NONE
wenzelm@33093
   151
  | SOME (Static ths) => SOME (true, ths)
wenzelm@33093
   152
  | SOME (Dynamic f) => SOME (false, f context));
wenzelm@26393
   153
wenzelm@33093
   154
fun fold_static f =
wenzelm@33093
   155
  Symtab.fold (fn (name, Static ths) => f (name, ths) | _ => I) o table_of;
wenzelm@26393
   156
wenzelm@27175
   157
wenzelm@27175
   158
(* content difference *)
wenzelm@27175
   159
wenzelm@27175
   160
fun diff_table prev_facts facts =
wenzelm@27175
   161
  fold_static (fn (name, ths) =>
wenzelm@27175
   162
    if exists (fn prev => defined prev name) prev_facts then I
wenzelm@27175
   163
    else cons (name, ths)) facts [];
wenzelm@27175
   164
wenzelm@27175
   165
fun dest_static prev_facts facts =
wenzelm@27175
   166
  sort_wrt #1 (diff_table prev_facts facts);
wenzelm@27175
   167
wenzelm@27175
   168
fun extern_static prev_facts facts =
wenzelm@27175
   169
  sort_wrt #1 (diff_table prev_facts facts |> map (apfst (extern facts)));
wenzelm@26281
   170
wenzelm@26281
   171
wenzelm@26281
   172
(* indexed props *)
wenzelm@26281
   173
wenzelm@35408
   174
val prop_ord = Term_Ord.term_ord o pairself Thm.full_prop_of;
wenzelm@26281
   175
wenzelm@26281
   176
fun props (Facts {props, ...}) = sort_distinct prop_ord (Net.content props);
wenzelm@26281
   177
fun could_unify (Facts {props, ...}) = Net.unify_term props;
wenzelm@26281
   178
wenzelm@26281
   179
wenzelm@26393
   180
(* merge facts *)
wenzelm@26281
   181
wenzelm@26281
   182
fun merge (Facts {facts = facts1, props = props1}, Facts {facts = facts2, props = props2}) =
wenzelm@26281
   183
  let
wenzelm@33095
   184
    val facts' = Name_Space.merge_tables (facts1, facts2);
wenzelm@26281
   185
    val props' = Net.merge (is_equal o prop_ord) (props1, props2);
wenzelm@26281
   186
  in make_facts facts' props' end;
wenzelm@26281
   187
wenzelm@26393
   188
wenzelm@26654
   189
(* add static entries *)
wenzelm@26393
   190
wenzelm@33093
   191
local
wenzelm@33093
   192
wenzelm@33093
   193
fun add strict do_props naming (b, ths) (Facts {facts, props}) =
wenzelm@26281
   194
  let
wenzelm@33093
   195
    val (name, facts') =
wenzelm@33093
   196
      if Binding.is_empty b then ("", facts)
wenzelm@33095
   197
      else Name_Space.define strict naming (b, Static ths) facts;
wenzelm@26281
   198
    val props' =
wenzelm@33093
   199
      if do_props
wenzelm@33093
   200
      then fold (fn th => Net.insert_term (K false) (Thm.full_prop_of th, th)) ths props
wenzelm@26281
   201
      else props;
haftmann@28864
   202
  in (name, make_facts facts' props') end;
wenzelm@26281
   203
wenzelm@33093
   204
in
wenzelm@33093
   205
wenzelm@33093
   206
val add_global = add true false;
wenzelm@33093
   207
val add_local = add false;
wenzelm@33093
   208
wenzelm@33093
   209
end;
wenzelm@26307
   210
wenzelm@26393
   211
wenzelm@26654
   212
(* add dynamic entries *)
wenzelm@26393
   213
wenzelm@33093
   214
fun add_dynamic naming (b, f) (Facts {facts, props}) =
wenzelm@33095
   215
  let val (name, facts') = Name_Space.define true naming (b, Dynamic f) facts;
wenzelm@33093
   216
  in (name, make_facts facts' props) end;
wenzelm@26281
   217
wenzelm@26654
   218
wenzelm@26654
   219
(* remove entries *)
wenzelm@26654
   220
wenzelm@26654
   221
fun del name (Facts {facts = (space, tab), props}) =
wenzelm@26654
   222
  let
wenzelm@33163
   223
    val space' = Name_Space.hide true name space handle ERROR _ => space;
wenzelm@26654
   224
    val tab' = Symtab.delete_safe name tab;
wenzelm@26654
   225
  in make_facts (space', tab') props end;
wenzelm@26654
   226
wenzelm@26654
   227
fun hide fully name (Facts {facts = (space, tab), props}) =
wenzelm@33095
   228
  make_facts (Name_Space.hide fully name space, tab) props;
wenzelm@26654
   229
wenzelm@26281
   230
end;