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