src/Pure/facts.ML
author wenzelm
Wed Dec 12 23:36:07 2012 +0100 (2012-12-12 ago)
changeset 50499 f496b2b7bafb
parent 49887 1a173b2503c0
child 55740 11dd48f84441
permissions -rw-r--r--
rendering of selected dialog_result as active_result_color, depending on dynamic command status in output panel, but not static popups etc.;
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@49887
    15
  val string_of_selection: interval list option -> string
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@33095
    24
  val space_of: T -> Name_Space.T
wenzelm@33163
    25
  val is_concealed: T -> string -> bool
wenzelm@26664
    26
  val intern: T -> xstring -> string
wenzelm@42358
    27
  val extern: Proof.context -> T -> string -> xstring
wenzelm@27738
    28
  val lookup: Context.generic -> T -> string -> (bool * thm list) option
wenzelm@26692
    29
  val defined: T -> string -> bool
wenzelm@26692
    30
  val fold_static: (string * thm list -> 'a -> 'a) -> T -> 'a -> 'a
wenzelm@27175
    31
  val dest_static: T list -> T -> (string * thm list) list
wenzelm@42358
    32
  val extern_static: Proof.context -> T list -> T -> (xstring * thm list) list
wenzelm@26281
    33
  val props: T -> thm list
wenzelm@26281
    34
  val could_unify: T -> term -> thm list
wenzelm@26281
    35
  val merge: T * T -> T
wenzelm@49747
    36
  val add_static: Context.generic -> {strict: bool, index: bool} ->
wenzelm@49747
    37
    binding * thm list -> T -> string * T
wenzelm@47005
    38
  val add_dynamic: Context.generic -> binding * (Context.generic -> thm list) -> T -> string * T
wenzelm@26654
    39
  val del: string -> T -> T
wenzelm@26654
    40
  val hide: bool -> string -> T -> T
wenzelm@26281
    41
end;
wenzelm@26281
    42
wenzelm@26281
    43
structure Facts: FACTS =
wenzelm@26281
    44
struct
wenzelm@26281
    45
wenzelm@26336
    46
(** fact references **)
wenzelm@26336
    47
wenzelm@26336
    48
fun the_single _ [th] : thm = th
wenzelm@33163
    49
  | the_single name _ = error ("Expected singleton fact " ^ quote name);
wenzelm@26336
    50
wenzelm@26336
    51
wenzelm@26336
    52
(* datatype interval *)
wenzelm@26336
    53
wenzelm@26336
    54
datatype interval =
wenzelm@26336
    55
  FromTo of int * int |
wenzelm@26336
    56
  From of int |
wenzelm@26336
    57
  Single of int;
wenzelm@26336
    58
wenzelm@26336
    59
fun string_of_interval (FromTo (i, j)) = string_of_int i ^ "-" ^ string_of_int j
wenzelm@26336
    60
  | string_of_interval (From i) = string_of_int i ^ "-"
wenzelm@26336
    61
  | string_of_interval (Single i) = string_of_int i;
wenzelm@26336
    62
wenzelm@26336
    63
fun interval n iv =
wenzelm@26336
    64
  let fun err () = raise Fail ("Bad interval specification " ^ string_of_interval iv) in
wenzelm@26336
    65
    (case iv of
wenzelm@26336
    66
      FromTo (i, j) => if i <= j then i upto j else err ()
wenzelm@26336
    67
    | From i => if i <= n then i upto n else err ()
wenzelm@26336
    68
    | Single i => [i])
wenzelm@26336
    69
  end;
wenzelm@26336
    70
wenzelm@26336
    71
wenzelm@26336
    72
(* datatype ref *)
wenzelm@26336
    73
wenzelm@26336
    74
datatype ref =
wenzelm@26361
    75
  Named of (string * Position.T) * interval list option |
wenzelm@26336
    76
  Fact of string;
wenzelm@26336
    77
wenzelm@26361
    78
fun named name = Named ((name, Position.none), NONE);
wenzelm@26361
    79
wenzelm@26366
    80
fun name_pos_of_ref (Named (name_pos, _)) = name_pos
wenzelm@49887
    81
  | name_pos_of_ref (Fact _) = raise Fail "Illegal literal fact";
wenzelm@26366
    82
wenzelm@26366
    83
val name_of_ref = #1 o name_pos_of_ref;
wenzelm@26366
    84
val pos_of_ref = #2 o name_pos_of_ref;
wenzelm@26336
    85
wenzelm@26361
    86
fun map_name_of_ref f (Named ((name, pos), is)) = Named ((f name, pos), is)
wenzelm@26336
    87
  | map_name_of_ref _ r = r;
wenzelm@26336
    88
wenzelm@49887
    89
fun string_of_selection NONE = ""
wenzelm@49887
    90
  | string_of_selection (SOME is) = enclose "(" ")" (commas (map string_of_interval is));
wenzelm@49887
    91
wenzelm@49887
    92
fun string_of_ref (Named ((name, _), sel)) = name ^ string_of_selection sel
wenzelm@49887
    93
  | string_of_ref (Fact _) = raise Fail "Illegal literal fact";
wenzelm@26336
    94
wenzelm@26336
    95
wenzelm@26336
    96
(* select *)
wenzelm@26336
    97
wenzelm@26336
    98
fun select (Fact _) ths = ths
wenzelm@26336
    99
  | select (Named (_, NONE)) ths = ths
wenzelm@26361
   100
  | select (Named ((name, pos), SOME ivs)) ths =
wenzelm@26336
   101
      let
wenzelm@26336
   102
        val n = length ths;
wenzelm@26361
   103
        fun err msg =
wenzelm@26361
   104
          error (msg ^ " for " ^ quote name ^ " (length " ^ string_of_int n ^ ")" ^
wenzelm@48992
   105
            Position.here pos);
wenzelm@26336
   106
        fun sel i =
wenzelm@26336
   107
          if i < 1 orelse i > n then err ("Bad subscript " ^ string_of_int i)
wenzelm@26336
   108
          else nth ths (i - 1);
wenzelm@26336
   109
        val is = maps (interval n) ivs handle Fail msg => err msg;
wenzelm@26336
   110
      in map sel is end;
wenzelm@26336
   111
wenzelm@26336
   112
wenzelm@26336
   113
(* selections *)
wenzelm@26336
   114
wenzelm@26361
   115
fun selections (name, [th]) = [(Named ((name, Position.none), NONE), th)]
wenzelm@26361
   116
  | selections (name, ths) = map2 (fn i => fn th =>
wenzelm@26361
   117
      (Named ((name, Position.none), SOME [Single i]), th)) (1 upto length ths) ths;
wenzelm@26336
   118
wenzelm@26336
   119
wenzelm@26336
   120
wenzelm@26336
   121
(** fact environment **)
wenzelm@26336
   122
wenzelm@26393
   123
(* datatypes *)
wenzelm@26393
   124
wenzelm@26393
   125
datatype fact = Static of thm list | Dynamic of Context.generic -> thm list;
wenzelm@26281
   126
wenzelm@26281
   127
datatype T = Facts of
wenzelm@33095
   128
 {facts: fact Name_Space.table,
wenzelm@26281
   129
  props: thm Net.net};
wenzelm@26281
   130
wenzelm@26281
   131
fun make_facts facts props = Facts {facts = facts, props = props};
wenzelm@33163
   132
wenzelm@33096
   133
val empty = make_facts (Name_Space.empty_table "fact") Net.empty;
wenzelm@26281
   134
wenzelm@26281
   135
wenzelm@26281
   136
(* named facts *)
wenzelm@26281
   137
wenzelm@26281
   138
fun facts_of (Facts {facts, ...}) = facts;
wenzelm@26281
   139
wenzelm@26281
   140
val space_of = #1 o facts_of;
wenzelm@26393
   141
val table_of = #2 o facts_of;
wenzelm@26393
   142
wenzelm@33163
   143
val is_concealed = Name_Space.is_concealed o space_of;
wenzelm@33163
   144
wenzelm@33095
   145
val intern = Name_Space.intern o space_of;
wenzelm@42358
   146
fun extern ctxt = Name_Space.extern ctxt o space_of;
wenzelm@26664
   147
wenzelm@26692
   148
val defined = Symtab.defined o table_of;
wenzelm@26692
   149
wenzelm@26393
   150
fun lookup context facts name =
wenzelm@26393
   151
  (case Symtab.lookup (table_of facts) name of
wenzelm@26393
   152
    NONE => NONE
wenzelm@33093
   153
  | SOME (Static ths) => SOME (true, ths)
wenzelm@33093
   154
  | SOME (Dynamic f) => SOME (false, f context));
wenzelm@26393
   155
wenzelm@33093
   156
fun fold_static f =
wenzelm@33093
   157
  Symtab.fold (fn (name, Static ths) => f (name, ths) | _ => I) o table_of;
wenzelm@26393
   158
wenzelm@27175
   159
wenzelm@27175
   160
(* content difference *)
wenzelm@27175
   161
wenzelm@27175
   162
fun diff_table prev_facts facts =
wenzelm@27175
   163
  fold_static (fn (name, ths) =>
wenzelm@27175
   164
    if exists (fn prev => defined prev name) prev_facts then I
wenzelm@27175
   165
    else cons (name, ths)) facts [];
wenzelm@27175
   166
wenzelm@27175
   167
fun dest_static prev_facts facts =
wenzelm@27175
   168
  sort_wrt #1 (diff_table prev_facts facts);
wenzelm@27175
   169
wenzelm@42358
   170
fun extern_static ctxt prev_facts facts =
wenzelm@42358
   171
  sort_wrt #1 (diff_table prev_facts facts |> map (apfst (extern ctxt facts)));
wenzelm@26281
   172
wenzelm@26281
   173
wenzelm@26281
   174
(* indexed props *)
wenzelm@26281
   175
wenzelm@35408
   176
val prop_ord = Term_Ord.term_ord o pairself Thm.full_prop_of;
wenzelm@26281
   177
wenzelm@26281
   178
fun props (Facts {props, ...}) = sort_distinct prop_ord (Net.content props);
wenzelm@26281
   179
fun could_unify (Facts {props, ...}) = Net.unify_term props;
wenzelm@26281
   180
wenzelm@26281
   181
wenzelm@26393
   182
(* merge facts *)
wenzelm@26281
   183
wenzelm@26281
   184
fun merge (Facts {facts = facts1, props = props1}, Facts {facts = facts2, props = props2}) =
wenzelm@26281
   185
  let
wenzelm@33095
   186
    val facts' = Name_Space.merge_tables (facts1, facts2);
wenzelm@26281
   187
    val props' = Net.merge (is_equal o prop_ord) (props1, props2);
wenzelm@26281
   188
  in make_facts facts' props' end;
wenzelm@26281
   189
wenzelm@26393
   190
wenzelm@26654
   191
(* add static entries *)
wenzelm@26393
   192
wenzelm@49747
   193
fun add_static context {strict, index} (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@47005
   197
      else Name_Space.define context strict (b, Static ths) facts;
wenzelm@49747
   198
    val props' = props
wenzelm@49747
   199
      |> index ? fold (fn th => Net.insert_term (K false) (Thm.full_prop_of th, th)) ths;
haftmann@28864
   200
  in (name, make_facts facts' props') end;
wenzelm@26281
   201
wenzelm@26393
   202
wenzelm@26654
   203
(* add dynamic entries *)
wenzelm@26393
   204
wenzelm@47005
   205
fun add_dynamic context (b, f) (Facts {facts, props}) =
wenzelm@47005
   206
  let val (name, facts') = Name_Space.define context true (b, Dynamic f) facts;
wenzelm@33093
   207
  in (name, make_facts facts' props) end;
wenzelm@26281
   208
wenzelm@26654
   209
wenzelm@26654
   210
(* remove entries *)
wenzelm@26654
   211
wenzelm@26654
   212
fun del name (Facts {facts = (space, tab), props}) =
wenzelm@26654
   213
  let
wenzelm@33163
   214
    val space' = Name_Space.hide true name space handle ERROR _ => space;
wenzelm@26654
   215
    val tab' = Symtab.delete_safe name tab;
wenzelm@26654
   216
  in make_facts (space', tab') props end;
wenzelm@26654
   217
wenzelm@26654
   218
fun hide fully name (Facts {facts = (space, tab), props}) =
wenzelm@33095
   219
  make_facts (Name_Space.hide fully name space, tab) props;
wenzelm@26654
   220
wenzelm@26281
   221
end;