src/Pure/facts.ML
author wenzelm
Mon Feb 23 14:50:30 2015 +0100 (2015-02-23)
changeset 59564 fdc03c8daacc
parent 59058 a78612c67ec0
child 59883 12a89103cae6
permissions -rw-r--r--
Goal.prove_multi is superseded by the fully general Goal.prove_common;
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@55740
     9
  val the_single: string * Position.T -> 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@57887
    25
  val alias: Name_Space.naming -> binding -> string -> T -> T
wenzelm@33163
    26
  val is_concealed: T -> string -> bool
wenzelm@56003
    27
  val check: Context.generic -> T -> xstring * Position.T -> string
wenzelm@26664
    28
  val intern: T -> xstring -> string
wenzelm@42358
    29
  val extern: Proof.context -> T -> string -> xstring
wenzelm@56140
    30
  val markup_extern: Proof.context -> T -> string -> Markup.T * xstring
wenzelm@27738
    31
  val lookup: Context.generic -> T -> string -> (bool * thm list) option
wenzelm@57942
    32
  val retrieve: Context.generic -> T -> xstring * Position.T ->
wenzelm@57942
    33
    {name: string, static: bool, thms: thm list}
wenzelm@26692
    34
  val defined: T -> string -> bool
wenzelm@26692
    35
  val fold_static: (string * thm list -> 'a -> 'a) -> T -> 'a -> 'a
wenzelm@56158
    36
  val dest_static: bool -> T list -> T -> (string * thm list) list
wenzelm@26281
    37
  val props: T -> thm list
wenzelm@26281
    38
  val could_unify: T -> term -> thm list
wenzelm@26281
    39
  val merge: T * T -> T
wenzelm@49747
    40
  val add_static: Context.generic -> {strict: bool, index: bool} ->
wenzelm@49747
    41
    binding * thm list -> T -> string * T
wenzelm@47005
    42
  val add_dynamic: Context.generic -> binding * (Context.generic -> thm list) -> T -> string * T
wenzelm@26654
    43
  val del: string -> T -> T
wenzelm@26654
    44
  val hide: bool -> string -> T -> T
wenzelm@26281
    45
end;
wenzelm@26281
    46
wenzelm@26281
    47
structure Facts: FACTS =
wenzelm@26281
    48
struct
wenzelm@26281
    49
wenzelm@26336
    50
(** fact references **)
wenzelm@26336
    51
wenzelm@26336
    52
fun the_single _ [th] : thm = th
wenzelm@55740
    53
  | the_single (name, pos) ths =
wenzelm@55740
    54
      error ("Expected singleton fact " ^ quote name ^
wenzelm@55740
    55
        " (length " ^ string_of_int (length ths) ^ ")" ^ Position.here pos);
wenzelm@26336
    56
wenzelm@26336
    57
wenzelm@26336
    58
(* datatype interval *)
wenzelm@26336
    59
wenzelm@26336
    60
datatype interval =
wenzelm@26336
    61
  FromTo of int * int |
wenzelm@26336
    62
  From of int |
wenzelm@26336
    63
  Single of int;
wenzelm@26336
    64
wenzelm@26336
    65
fun string_of_interval (FromTo (i, j)) = string_of_int i ^ "-" ^ string_of_int j
wenzelm@26336
    66
  | string_of_interval (From i) = string_of_int i ^ "-"
wenzelm@26336
    67
  | string_of_interval (Single i) = string_of_int i;
wenzelm@26336
    68
wenzelm@26336
    69
fun interval n iv =
wenzelm@26336
    70
  let fun err () = raise Fail ("Bad interval specification " ^ string_of_interval iv) in
wenzelm@26336
    71
    (case iv of
wenzelm@26336
    72
      FromTo (i, j) => if i <= j then i upto j else err ()
wenzelm@26336
    73
    | From i => if i <= n then i upto n else err ()
wenzelm@26336
    74
    | Single i => [i])
wenzelm@26336
    75
  end;
wenzelm@26336
    76
wenzelm@26336
    77
wenzelm@26336
    78
(* datatype ref *)
wenzelm@26336
    79
wenzelm@26336
    80
datatype ref =
wenzelm@26361
    81
  Named of (string * Position.T) * interval list option |
wenzelm@26336
    82
  Fact of string;
wenzelm@26336
    83
wenzelm@26361
    84
fun named name = Named ((name, Position.none), NONE);
wenzelm@26361
    85
wenzelm@55740
    86
fun name_of_ref (Named ((name, _), _)) = name
wenzelm@55740
    87
  | name_of_ref (Fact _) = raise Fail "Illegal literal fact";
wenzelm@55740
    88
wenzelm@55740
    89
fun pos_of_ref (Named ((_, pos), _)) = pos
wenzelm@55740
    90
  | pos_of_ref (Fact _) = Position.none;
wenzelm@55740
    91
wenzelm@26361
    92
fun map_name_of_ref f (Named ((name, pos), is)) = Named ((f name, pos), is)
wenzelm@26336
    93
  | map_name_of_ref _ r = r;
wenzelm@26336
    94
wenzelm@49887
    95
fun string_of_selection NONE = ""
wenzelm@49887
    96
  | string_of_selection (SOME is) = enclose "(" ")" (commas (map string_of_interval is));
wenzelm@49887
    97
wenzelm@49887
    98
fun string_of_ref (Named ((name, _), sel)) = name ^ string_of_selection sel
wenzelm@49887
    99
  | string_of_ref (Fact _) = raise Fail "Illegal literal fact";
wenzelm@26336
   100
wenzelm@26336
   101
wenzelm@26336
   102
(* select *)
wenzelm@26336
   103
wenzelm@26336
   104
fun select (Fact _) ths = ths
wenzelm@26336
   105
  | select (Named (_, NONE)) ths = ths
wenzelm@26361
   106
  | select (Named ((name, pos), SOME ivs)) ths =
wenzelm@26336
   107
      let
wenzelm@26336
   108
        val n = length ths;
wenzelm@26361
   109
        fun err msg =
wenzelm@55740
   110
          error (msg ^ " for fact " ^ quote name ^ " (length " ^ string_of_int n ^ ")" ^
wenzelm@48992
   111
            Position.here pos);
wenzelm@26336
   112
        fun sel i =
wenzelm@26336
   113
          if i < 1 orelse i > n then err ("Bad subscript " ^ string_of_int i)
wenzelm@26336
   114
          else nth ths (i - 1);
wenzelm@26336
   115
        val is = maps (interval n) ivs handle Fail msg => err msg;
wenzelm@26336
   116
      in map sel is end;
wenzelm@26336
   117
wenzelm@26336
   118
wenzelm@26336
   119
(* selections *)
wenzelm@26336
   120
wenzelm@26361
   121
fun selections (name, [th]) = [(Named ((name, Position.none), NONE), th)]
wenzelm@26361
   122
  | selections (name, ths) = map2 (fn i => fn th =>
wenzelm@26361
   123
      (Named ((name, Position.none), SOME [Single i]), th)) (1 upto length ths) ths;
wenzelm@26336
   124
wenzelm@26336
   125
wenzelm@26336
   126
wenzelm@26336
   127
(** fact environment **)
wenzelm@26336
   128
wenzelm@26393
   129
(* datatypes *)
wenzelm@26393
   130
wenzelm@26393
   131
datatype fact = Static of thm list | Dynamic of Context.generic -> thm list;
wenzelm@26281
   132
wenzelm@26281
   133
datatype T = Facts of
wenzelm@33095
   134
 {facts: fact Name_Space.table,
wenzelm@26281
   135
  props: thm Net.net};
wenzelm@26281
   136
wenzelm@26281
   137
fun make_facts facts props = Facts {facts = facts, props = props};
wenzelm@33163
   138
wenzelm@33096
   139
val empty = make_facts (Name_Space.empty_table "fact") Net.empty;
wenzelm@26281
   140
wenzelm@26281
   141
wenzelm@26281
   142
(* named facts *)
wenzelm@26281
   143
wenzelm@26281
   144
fun facts_of (Facts {facts, ...}) = facts;
wenzelm@26281
   145
wenzelm@56025
   146
val space_of = Name_Space.space_of_table o facts_of;
wenzelm@26393
   147
wenzelm@57887
   148
fun alias naming binding name (Facts {facts, props}) =
wenzelm@57887
   149
  make_facts (Name_Space.alias_table naming binding name facts) props;
wenzelm@57887
   150
wenzelm@33163
   151
val is_concealed = Name_Space.is_concealed o space_of;
wenzelm@33163
   152
wenzelm@56003
   153
fun check context facts (xname, pos) =
wenzelm@56003
   154
  let
wenzelm@56003
   155
    val (name, fact) = Name_Space.check context (facts_of facts) (xname, pos);
wenzelm@56003
   156
    val _ =
wenzelm@56003
   157
      (case fact of
wenzelm@56003
   158
        Static _ => ()
wenzelm@56003
   159
      | Dynamic _ => Context_Position.report_generic context pos (Markup.dynamic_fact name));
wenzelm@56003
   160
  in name end;
wenzelm@56003
   161
wenzelm@33095
   162
val intern = Name_Space.intern o space_of;
wenzelm@42358
   163
fun extern ctxt = Name_Space.extern ctxt o space_of;
wenzelm@57887
   164
fun markup_extern ctxt = Name_Space.markup_extern ctxt o space_of;
wenzelm@26664
   165
wenzelm@56158
   166
wenzelm@56158
   167
(* retrieve *)
wenzelm@56158
   168
wenzelm@56025
   169
val defined = is_some oo (Name_Space.lookup_key o facts_of);
wenzelm@26692
   170
wenzelm@26393
   171
fun lookup context facts name =
wenzelm@56025
   172
  (case Name_Space.lookup_key (facts_of facts) name of
wenzelm@26393
   173
    NONE => NONE
wenzelm@56025
   174
  | SOME (_, Static ths) => SOME (true, ths)
wenzelm@56025
   175
  | SOME (_, Dynamic f) => SOME (false, f context));
wenzelm@26393
   176
wenzelm@56140
   177
fun retrieve context facts (xname, pos) =
wenzelm@56140
   178
  let
wenzelm@56140
   179
    val name = check context facts (xname, pos);
wenzelm@57942
   180
    val (static, thms) =
wenzelm@56140
   181
      (case lookup context facts name of
wenzelm@56140
   182
        SOME (static, thms) =>
wenzelm@56140
   183
          (if static then ()
wenzelm@56140
   184
           else Context_Position.report_generic context pos (Markup.dynamic_fact name);
wenzelm@57942
   185
           (static, thms))
wenzelm@56140
   186
      | NONE => error ("Unknown fact " ^ quote name ^ Position.here pos));
wenzelm@57942
   187
  in
wenzelm@57942
   188
   {name = name,
wenzelm@57942
   189
    static = static,
wenzelm@57942
   190
    thms = map (Thm.transfer (Context.theory_of context)) thms}
wenzelm@57942
   191
  end;
wenzelm@56140
   192
wenzelm@56158
   193
wenzelm@56158
   194
(* static content *)
wenzelm@56158
   195
wenzelm@33093
   196
fun fold_static f =
wenzelm@56025
   197
  Name_Space.fold_table (fn (name, Static ths) => f (name, ths) | _ => I) o facts_of;
wenzelm@26393
   198
wenzelm@56158
   199
fun dest_static verbose prev_facts facts =
wenzelm@27175
   200
  fold_static (fn (name, ths) =>
wenzelm@56158
   201
    if exists (fn prev => defined prev name) prev_facts orelse
wenzelm@56158
   202
      not verbose andalso is_concealed facts name then I
wenzelm@56158
   203
    else cons (name, ths)) facts []
wenzelm@56158
   204
  |> sort_wrt #1;
wenzelm@26281
   205
wenzelm@26281
   206
wenzelm@26281
   207
(* indexed props *)
wenzelm@26281
   208
wenzelm@59058
   209
val prop_ord = Term_Ord.term_ord o apply2 Thm.full_prop_of;
wenzelm@26281
   210
wenzelm@26281
   211
fun props (Facts {props, ...}) = sort_distinct prop_ord (Net.content props);
wenzelm@26281
   212
fun could_unify (Facts {props, ...}) = Net.unify_term props;
wenzelm@26281
   213
wenzelm@26281
   214
wenzelm@26393
   215
(* merge facts *)
wenzelm@26281
   216
wenzelm@26281
   217
fun merge (Facts {facts = facts1, props = props1}, Facts {facts = facts2, props = props2}) =
wenzelm@26281
   218
  let
wenzelm@33095
   219
    val facts' = Name_Space.merge_tables (facts1, facts2);
wenzelm@56137
   220
    val props' =
wenzelm@56137
   221
      if Net.is_empty props2 then props1
wenzelm@56137
   222
      else if Net.is_empty props1 then props2
wenzelm@56137
   223
      else Net.merge (is_equal o prop_ord) (props1, props2);  (*beware of non-canonical merge*)
wenzelm@26281
   224
  in make_facts facts' props' end;
wenzelm@26281
   225
wenzelm@26393
   226
wenzelm@26654
   227
(* add static entries *)
wenzelm@26393
   228
wenzelm@49747
   229
fun add_static context {strict, index} (b, ths) (Facts {facts, props}) =
wenzelm@26281
   230
  let
wenzelm@33093
   231
    val (name, facts') =
wenzelm@33093
   232
      if Binding.is_empty b then ("", facts)
wenzelm@47005
   233
      else Name_Space.define context strict (b, Static ths) facts;
wenzelm@49747
   234
    val props' = props
wenzelm@49747
   235
      |> index ? fold (fn th => Net.insert_term (K false) (Thm.full_prop_of th, th)) ths;
haftmann@28864
   236
  in (name, make_facts facts' props') end;
wenzelm@26281
   237
wenzelm@26393
   238
wenzelm@26654
   239
(* add dynamic entries *)
wenzelm@26393
   240
wenzelm@47005
   241
fun add_dynamic context (b, f) (Facts {facts, props}) =
wenzelm@47005
   242
  let val (name, facts') = Name_Space.define context true (b, Dynamic f) facts;
wenzelm@33093
   243
  in (name, make_facts facts' props) end;
wenzelm@26281
   244
wenzelm@26654
   245
wenzelm@26654
   246
(* remove entries *)
wenzelm@26654
   247
wenzelm@56025
   248
fun del name (Facts {facts, props}) =
wenzelm@56025
   249
  make_facts (Name_Space.del_table name facts) props;
wenzelm@26654
   250
wenzelm@56025
   251
fun hide fully name (Facts {facts, props}) =
wenzelm@56025
   252
  make_facts (Name_Space.hide_table fully name facts) props;
wenzelm@26654
   253
wenzelm@26281
   254
end;