src/Pure/facts.ML
author wenzelm
Sun, 10 Aug 2014 19:44:20 +0200
changeset 57887 44354c99d754
parent 56158 c2c6d560e7b2
child 57942 e5bec882fdd0
permissions -rw-r--r--
support aliases within the facts space;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26281
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/facts.ML
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
     3
26307
27d3de85c266 replaced generic add by add_local/add_global;
wenzelm
parents: 26290
diff changeset
     4
Environment of named facts, optionally indexed by proposition.
26281
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
     5
*)
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
     6
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
     7
signature FACTS =
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
     8
sig
55740
11dd48f84441 more positions;
wenzelm
parents: 49887
diff changeset
     9
  val the_single: string * Position.T -> thm list -> thm
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    10
  datatype interval = FromTo of int * int | From of int | Single of int
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    11
  datatype ref =
26361
7946f459c6c8 Facts.Named: include position;
wenzelm
parents: 26336
diff changeset
    12
    Named of (string * Position.T) * interval list option |
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    13
    Fact of string
26361
7946f459c6c8 Facts.Named: include position;
wenzelm
parents: 26336
diff changeset
    14
  val named: string -> ref
49887
1a173b2503c0 tuned signature;
wenzelm
parents: 49747
diff changeset
    15
  val string_of_selection: interval list option -> string
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    16
  val string_of_ref: ref -> string
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    17
  val name_of_ref: ref -> string
26366
5c089f4219c2 added pos_of_ref;
wenzelm
parents: 26361
diff changeset
    18
  val pos_of_ref: ref -> Position.T
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    19
  val map_name_of_ref: (string -> string) -> ref -> ref
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    20
  val select: ref -> thm list -> thm list
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    21
  val selections: string * thm list -> (ref * thm) list
26281
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
    22
  type T
26393
42febbed5460 support dynamic facts;
wenzelm
parents: 26366
diff changeset
    23
  val empty: T
33095
bbd52d2f8696 renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
wenzelm
parents: 33093
diff changeset
    24
  val space_of: T -> Name_Space.T
57887
44354c99d754 support aliases within the facts space;
wenzelm
parents: 56158
diff changeset
    25
  val alias: Name_Space.naming -> binding -> string -> T -> T
33163
351fc13613f2 export is_concealed;
wenzelm
parents: 33096
diff changeset
    26
  val is_concealed: T -> string -> bool
56003
eccac152ffb4 check fact names with PIDE markup;
wenzelm
parents: 55740
diff changeset
    27
  val check: Context.generic -> T -> xstring * Position.T -> string
26664
167d879a89b0 renamed dest to dest_table, and extern to extern table;
wenzelm
parents: 26654
diff changeset
    28
  val intern: T -> xstring -> string
42358
b47d41d9f4b5 Name_Space: proper configuration options long_names, short_names, unique_names instead of former unsynchronized references;
wenzelm
parents: 35408
diff changeset
    29
  val extern: Proof.context -> T -> string -> xstring
56140
ed92ce2ac88e just one cumulative Proof_Context.facts, with uniform retrieval (including PIDE markup, completion etc.);
wenzelm
parents: 56137
diff changeset
    30
  val markup_extern: Proof.context -> T -> string -> Markup.T * xstring
27738
66596d7aa899 Facts.lookup: return static/dynamic status;
wenzelm
parents: 27175
diff changeset
    31
  val lookup: Context.generic -> T -> string -> (bool * thm list) option
56140
ed92ce2ac88e just one cumulative Proof_Context.facts, with uniform retrieval (including PIDE markup, completion etc.);
wenzelm
parents: 56137
diff changeset
    32
  val retrieve: Context.generic -> T -> xstring * Position.T -> string * thm list
26692
3f48d4f4229f removed unused space_of;
wenzelm
parents: 26664
diff changeset
    33
  val defined: T -> string -> bool
3f48d4f4229f removed unused space_of;
wenzelm
parents: 26664
diff changeset
    34
  val fold_static: (string * thm list -> 'a -> 'a) -> T -> 'a -> 'a
56158
c2c6d560e7b2 more explicit treatment of verbose mode, which includes concealed entries;
wenzelm
parents: 56140
diff changeset
    35
  val dest_static: bool -> T list -> T -> (string * thm list) list
26281
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
    36
  val props: T -> thm list
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
    37
  val could_unify: T -> term -> thm list
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
    38
  val merge: T * T -> T
49747
2cf86639b77e more explicit flags for facts table;
wenzelm
parents: 48992
diff changeset
    39
  val add_static: Context.generic -> {strict: bool, index: bool} ->
2cf86639b77e more explicit flags for facts table;
wenzelm
parents: 48992
diff changeset
    40
    binding * thm list -> T -> string * T
47005
421760a1efe7 maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents: 42375
diff changeset
    41
  val add_dynamic: Context.generic -> binding * (Context.generic -> thm list) -> T -> string * T
26654
1f711934f221 disallow duplicate entries (weak version for merge);
wenzelm
parents: 26393
diff changeset
    42
  val del: string -> T -> T
1f711934f221 disallow duplicate entries (weak version for merge);
wenzelm
parents: 26393
diff changeset
    43
  val hide: bool -> string -> T -> T
26281
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
    44
end;
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
    45
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
    46
structure Facts: FACTS =
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
    47
struct
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
    48
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    49
(** fact references **)
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    50
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    51
fun the_single _ [th] : thm = th
55740
11dd48f84441 more positions;
wenzelm
parents: 49887
diff changeset
    52
  | the_single (name, pos) ths =
11dd48f84441 more positions;
wenzelm
parents: 49887
diff changeset
    53
      error ("Expected singleton fact " ^ quote name ^
11dd48f84441 more positions;
wenzelm
parents: 49887
diff changeset
    54
        " (length " ^ string_of_int (length ths) ^ ")" ^ Position.here pos);
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    55
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    56
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    57
(* datatype interval *)
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    58
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    59
datatype interval =
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    60
  FromTo of int * int |
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    61
  From of int |
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    62
  Single of int;
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    63
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    64
fun string_of_interval (FromTo (i, j)) = string_of_int i ^ "-" ^ string_of_int j
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    65
  | string_of_interval (From i) = string_of_int i ^ "-"
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    66
  | string_of_interval (Single i) = string_of_int i;
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    67
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    68
fun interval n iv =
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    69
  let fun err () = raise Fail ("Bad interval specification " ^ string_of_interval iv) in
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    70
    (case iv of
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    71
      FromTo (i, j) => if i <= j then i upto j else err ()
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    72
    | From i => if i <= n then i upto n else err ()
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    73
    | Single i => [i])
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    74
  end;
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    75
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    76
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    77
(* datatype ref *)
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    78
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    79
datatype ref =
26361
7946f459c6c8 Facts.Named: include position;
wenzelm
parents: 26336
diff changeset
    80
  Named of (string * Position.T) * interval list option |
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    81
  Fact of string;
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    82
26361
7946f459c6c8 Facts.Named: include position;
wenzelm
parents: 26336
diff changeset
    83
fun named name = Named ((name, Position.none), NONE);
7946f459c6c8 Facts.Named: include position;
wenzelm
parents: 26336
diff changeset
    84
55740
11dd48f84441 more positions;
wenzelm
parents: 49887
diff changeset
    85
fun name_of_ref (Named ((name, _), _)) = name
11dd48f84441 more positions;
wenzelm
parents: 49887
diff changeset
    86
  | name_of_ref (Fact _) = raise Fail "Illegal literal fact";
11dd48f84441 more positions;
wenzelm
parents: 49887
diff changeset
    87
11dd48f84441 more positions;
wenzelm
parents: 49887
diff changeset
    88
fun pos_of_ref (Named ((_, pos), _)) = pos
11dd48f84441 more positions;
wenzelm
parents: 49887
diff changeset
    89
  | pos_of_ref (Fact _) = Position.none;
11dd48f84441 more positions;
wenzelm
parents: 49887
diff changeset
    90
26361
7946f459c6c8 Facts.Named: include position;
wenzelm
parents: 26336
diff changeset
    91
fun map_name_of_ref f (Named ((name, pos), is)) = Named ((f name, pos), is)
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    92
  | map_name_of_ref _ r = r;
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    93
49887
1a173b2503c0 tuned signature;
wenzelm
parents: 49747
diff changeset
    94
fun string_of_selection NONE = ""
1a173b2503c0 tuned signature;
wenzelm
parents: 49747
diff changeset
    95
  | string_of_selection (SOME is) = enclose "(" ")" (commas (map string_of_interval is));
1a173b2503c0 tuned signature;
wenzelm
parents: 49747
diff changeset
    96
1a173b2503c0 tuned signature;
wenzelm
parents: 49747
diff changeset
    97
fun string_of_ref (Named ((name, _), sel)) = name ^ string_of_selection sel
1a173b2503c0 tuned signature;
wenzelm
parents: 49747
diff changeset
    98
  | string_of_ref (Fact _) = raise Fail "Illegal literal fact";
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
    99
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
   100
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
   101
(* select *)
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
   102
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
   103
fun select (Fact _) ths = ths
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
   104
  | select (Named (_, NONE)) ths = ths
26361
7946f459c6c8 Facts.Named: include position;
wenzelm
parents: 26336
diff changeset
   105
  | select (Named ((name, pos), SOME ivs)) ths =
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
   106
      let
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
   107
        val n = length ths;
26361
7946f459c6c8 Facts.Named: include position;
wenzelm
parents: 26336
diff changeset
   108
        fun err msg =
55740
11dd48f84441 more positions;
wenzelm
parents: 49887
diff changeset
   109
          error (msg ^ " for fact " ^ quote name ^ " (length " ^ string_of_int n ^ ")" ^
48992
0518bf89c777 renamed Position.str_of to Position.here;
wenzelm
parents: 47005
diff changeset
   110
            Position.here pos);
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
   111
        fun sel i =
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
   112
          if i < 1 orelse i > n then err ("Bad subscript " ^ string_of_int i)
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
   113
          else nth ths (i - 1);
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
   114
        val is = maps (interval n) ivs handle Fail msg => err msg;
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
   115
      in map sel is end;
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
   116
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
   117
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
   118
(* selections *)
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
   119
26361
7946f459c6c8 Facts.Named: include position;
wenzelm
parents: 26336
diff changeset
   120
fun selections (name, [th]) = [(Named ((name, Position.none), NONE), th)]
7946f459c6c8 Facts.Named: include position;
wenzelm
parents: 26336
diff changeset
   121
  | selections (name, ths) = map2 (fn i => fn th =>
7946f459c6c8 Facts.Named: include position;
wenzelm
parents: 26336
diff changeset
   122
      (Named ((name, Position.none), SOME [Single i]), th)) (1 upto length ths) ths;
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
   123
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
   124
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
   125
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
   126
(** fact environment **)
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26307
diff changeset
   127
26393
42febbed5460 support dynamic facts;
wenzelm
parents: 26366
diff changeset
   128
(* datatypes *)
42febbed5460 support dynamic facts;
wenzelm
parents: 26366
diff changeset
   129
42febbed5460 support dynamic facts;
wenzelm
parents: 26366
diff changeset
   130
datatype fact = Static of thm list | Dynamic of Context.generic -> thm list;
26281
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   131
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   132
datatype T = Facts of
33095
bbd52d2f8696 renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
wenzelm
parents: 33093
diff changeset
   133
 {facts: fact Name_Space.table,
26281
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   134
  props: thm Net.net};
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   135
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   136
fun make_facts facts props = Facts {facts = facts, props = props};
33163
351fc13613f2 export is_concealed;
wenzelm
parents: 33096
diff changeset
   137
33096
db3c18fd9708 maintain explicit name space kind;
wenzelm
parents: 33095
diff changeset
   138
val empty = make_facts (Name_Space.empty_table "fact") Net.empty;
26281
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   139
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   140
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   141
(* named facts *)
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   142
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   143
fun facts_of (Facts {facts, ...}) = facts;
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   144
56025
d74fed45fa8b abstract type Name_Space.table;
wenzelm
parents: 56004
diff changeset
   145
val space_of = Name_Space.space_of_table o facts_of;
26393
42febbed5460 support dynamic facts;
wenzelm
parents: 26366
diff changeset
   146
57887
44354c99d754 support aliases within the facts space;
wenzelm
parents: 56158
diff changeset
   147
fun alias naming binding name (Facts {facts, props}) =
44354c99d754 support aliases within the facts space;
wenzelm
parents: 56158
diff changeset
   148
  make_facts (Name_Space.alias_table naming binding name facts) props;
44354c99d754 support aliases within the facts space;
wenzelm
parents: 56158
diff changeset
   149
33163
351fc13613f2 export is_concealed;
wenzelm
parents: 33096
diff changeset
   150
val is_concealed = Name_Space.is_concealed o space_of;
351fc13613f2 export is_concealed;
wenzelm
parents: 33096
diff changeset
   151
56003
eccac152ffb4 check fact names with PIDE markup;
wenzelm
parents: 55740
diff changeset
   152
fun check context facts (xname, pos) =
eccac152ffb4 check fact names with PIDE markup;
wenzelm
parents: 55740
diff changeset
   153
  let
eccac152ffb4 check fact names with PIDE markup;
wenzelm
parents: 55740
diff changeset
   154
    val (name, fact) = Name_Space.check context (facts_of facts) (xname, pos);
eccac152ffb4 check fact names with PIDE markup;
wenzelm
parents: 55740
diff changeset
   155
    val _ =
eccac152ffb4 check fact names with PIDE markup;
wenzelm
parents: 55740
diff changeset
   156
      (case fact of
eccac152ffb4 check fact names with PIDE markup;
wenzelm
parents: 55740
diff changeset
   157
        Static _ => ()
eccac152ffb4 check fact names with PIDE markup;
wenzelm
parents: 55740
diff changeset
   158
      | Dynamic _ => Context_Position.report_generic context pos (Markup.dynamic_fact name));
eccac152ffb4 check fact names with PIDE markup;
wenzelm
parents: 55740
diff changeset
   159
  in name end;
eccac152ffb4 check fact names with PIDE markup;
wenzelm
parents: 55740
diff changeset
   160
33095
bbd52d2f8696 renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
wenzelm
parents: 33093
diff changeset
   161
val intern = Name_Space.intern o space_of;
42358
b47d41d9f4b5 Name_Space: proper configuration options long_names, short_names, unique_names instead of former unsynchronized references;
wenzelm
parents: 35408
diff changeset
   162
fun extern ctxt = Name_Space.extern ctxt o space_of;
57887
44354c99d754 support aliases within the facts space;
wenzelm
parents: 56158
diff changeset
   163
fun markup_extern ctxt = Name_Space.markup_extern ctxt o space_of;
26664
167d879a89b0 renamed dest to dest_table, and extern to extern table;
wenzelm
parents: 26654
diff changeset
   164
56158
c2c6d560e7b2 more explicit treatment of verbose mode, which includes concealed entries;
wenzelm
parents: 56140
diff changeset
   165
c2c6d560e7b2 more explicit treatment of verbose mode, which includes concealed entries;
wenzelm
parents: 56140
diff changeset
   166
(* retrieve *)
c2c6d560e7b2 more explicit treatment of verbose mode, which includes concealed entries;
wenzelm
parents: 56140
diff changeset
   167
56025
d74fed45fa8b abstract type Name_Space.table;
wenzelm
parents: 56004
diff changeset
   168
val defined = is_some oo (Name_Space.lookup_key o facts_of);
26692
3f48d4f4229f removed unused space_of;
wenzelm
parents: 26664
diff changeset
   169
26393
42febbed5460 support dynamic facts;
wenzelm
parents: 26366
diff changeset
   170
fun lookup context facts name =
56025
d74fed45fa8b abstract type Name_Space.table;
wenzelm
parents: 56004
diff changeset
   171
  (case Name_Space.lookup_key (facts_of facts) name of
26393
42febbed5460 support dynamic facts;
wenzelm
parents: 26366
diff changeset
   172
    NONE => NONE
56025
d74fed45fa8b abstract type Name_Space.table;
wenzelm
parents: 56004
diff changeset
   173
  | SOME (_, Static ths) => SOME (true, ths)
d74fed45fa8b abstract type Name_Space.table;
wenzelm
parents: 56004
diff changeset
   174
  | SOME (_, Dynamic f) => SOME (false, f context));
26393
42febbed5460 support dynamic facts;
wenzelm
parents: 26366
diff changeset
   175
56140
ed92ce2ac88e just one cumulative Proof_Context.facts, with uniform retrieval (including PIDE markup, completion etc.);
wenzelm
parents: 56137
diff changeset
   176
fun retrieve context facts (xname, pos) =
ed92ce2ac88e just one cumulative Proof_Context.facts, with uniform retrieval (including PIDE markup, completion etc.);
wenzelm
parents: 56137
diff changeset
   177
  let
ed92ce2ac88e just one cumulative Proof_Context.facts, with uniform retrieval (including PIDE markup, completion etc.);
wenzelm
parents: 56137
diff changeset
   178
    val name = check context facts (xname, pos);
ed92ce2ac88e just one cumulative Proof_Context.facts, with uniform retrieval (including PIDE markup, completion etc.);
wenzelm
parents: 56137
diff changeset
   179
    val thms =
ed92ce2ac88e just one cumulative Proof_Context.facts, with uniform retrieval (including PIDE markup, completion etc.);
wenzelm
parents: 56137
diff changeset
   180
      (case lookup context facts name of
ed92ce2ac88e just one cumulative Proof_Context.facts, with uniform retrieval (including PIDE markup, completion etc.);
wenzelm
parents: 56137
diff changeset
   181
        SOME (static, thms) =>
ed92ce2ac88e just one cumulative Proof_Context.facts, with uniform retrieval (including PIDE markup, completion etc.);
wenzelm
parents: 56137
diff changeset
   182
          (if static then ()
ed92ce2ac88e just one cumulative Proof_Context.facts, with uniform retrieval (including PIDE markup, completion etc.);
wenzelm
parents: 56137
diff changeset
   183
           else Context_Position.report_generic context pos (Markup.dynamic_fact name);
ed92ce2ac88e just one cumulative Proof_Context.facts, with uniform retrieval (including PIDE markup, completion etc.);
wenzelm
parents: 56137
diff changeset
   184
           thms)
ed92ce2ac88e just one cumulative Proof_Context.facts, with uniform retrieval (including PIDE markup, completion etc.);
wenzelm
parents: 56137
diff changeset
   185
      | NONE => error ("Unknown fact " ^ quote name ^ Position.here pos));
ed92ce2ac88e just one cumulative Proof_Context.facts, with uniform retrieval (including PIDE markup, completion etc.);
wenzelm
parents: 56137
diff changeset
   186
  in (name, map (Thm.transfer (Context.theory_of context)) thms) end;
ed92ce2ac88e just one cumulative Proof_Context.facts, with uniform retrieval (including PIDE markup, completion etc.);
wenzelm
parents: 56137
diff changeset
   187
56158
c2c6d560e7b2 more explicit treatment of verbose mode, which includes concealed entries;
wenzelm
parents: 56140
diff changeset
   188
c2c6d560e7b2 more explicit treatment of verbose mode, which includes concealed entries;
wenzelm
parents: 56140
diff changeset
   189
(* static content *)
c2c6d560e7b2 more explicit treatment of verbose mode, which includes concealed entries;
wenzelm
parents: 56140
diff changeset
   190
33093
d010f61d3702 eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
wenzelm
parents: 29848
diff changeset
   191
fun fold_static f =
56025
d74fed45fa8b abstract type Name_Space.table;
wenzelm
parents: 56004
diff changeset
   192
  Name_Space.fold_table (fn (name, Static ths) => f (name, ths) | _ => I) o facts_of;
26393
42febbed5460 support dynamic facts;
wenzelm
parents: 26366
diff changeset
   193
56158
c2c6d560e7b2 more explicit treatment of verbose mode, which includes concealed entries;
wenzelm
parents: 56140
diff changeset
   194
fun dest_static verbose prev_facts facts =
27175
620295a57106 dest/export_static: content difference;
wenzelm
parents: 26692
diff changeset
   195
  fold_static (fn (name, ths) =>
56158
c2c6d560e7b2 more explicit treatment of verbose mode, which includes concealed entries;
wenzelm
parents: 56140
diff changeset
   196
    if exists (fn prev => defined prev name) prev_facts orelse
c2c6d560e7b2 more explicit treatment of verbose mode, which includes concealed entries;
wenzelm
parents: 56140
diff changeset
   197
      not verbose andalso is_concealed facts name then I
c2c6d560e7b2 more explicit treatment of verbose mode, which includes concealed entries;
wenzelm
parents: 56140
diff changeset
   198
    else cons (name, ths)) facts []
c2c6d560e7b2 more explicit treatment of verbose mode, which includes concealed entries;
wenzelm
parents: 56140
diff changeset
   199
  |> sort_wrt #1;
26281
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   200
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   201
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   202
(* indexed props *)
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   203
35408
b48ab741683b modernized structure Term_Ord;
wenzelm
parents: 33163
diff changeset
   204
val prop_ord = Term_Ord.term_ord o pairself Thm.full_prop_of;
26281
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   205
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   206
fun props (Facts {props, ...}) = sort_distinct prop_ord (Net.content props);
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   207
fun could_unify (Facts {props, ...}) = Net.unify_term props;
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   208
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   209
26393
42febbed5460 support dynamic facts;
wenzelm
parents: 26366
diff changeset
   210
(* merge facts *)
26281
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   211
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   212
fun merge (Facts {facts = facts1, props = props1}, Facts {facts = facts2, props = props2}) =
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   213
  let
33095
bbd52d2f8696 renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
wenzelm
parents: 33093
diff changeset
   214
    val facts' = Name_Space.merge_tables (facts1, facts2);
56137
af71fb1cb31f minor tuning -- NB: props are usually empty for global facts;
wenzelm
parents: 56025
diff changeset
   215
    val props' =
af71fb1cb31f minor tuning -- NB: props are usually empty for global facts;
wenzelm
parents: 56025
diff changeset
   216
      if Net.is_empty props2 then props1
af71fb1cb31f minor tuning -- NB: props are usually empty for global facts;
wenzelm
parents: 56025
diff changeset
   217
      else if Net.is_empty props1 then props2
af71fb1cb31f minor tuning -- NB: props are usually empty for global facts;
wenzelm
parents: 56025
diff changeset
   218
      else Net.merge (is_equal o prop_ord) (props1, props2);  (*beware of non-canonical merge*)
26281
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   219
  in make_facts facts' props' end;
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   220
26393
42febbed5460 support dynamic facts;
wenzelm
parents: 26366
diff changeset
   221
26654
1f711934f221 disallow duplicate entries (weak version for merge);
wenzelm
parents: 26393
diff changeset
   222
(* add static entries *)
26393
42febbed5460 support dynamic facts;
wenzelm
parents: 26366
diff changeset
   223
49747
2cf86639b77e more explicit flags for facts table;
wenzelm
parents: 48992
diff changeset
   224
fun add_static context {strict, index} (b, ths) (Facts {facts, props}) =
26281
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   225
  let
33093
d010f61d3702 eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
wenzelm
parents: 29848
diff changeset
   226
    val (name, facts') =
d010f61d3702 eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
wenzelm
parents: 29848
diff changeset
   227
      if Binding.is_empty b then ("", facts)
47005
421760a1efe7 maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents: 42375
diff changeset
   228
      else Name_Space.define context strict (b, Static ths) facts;
49747
2cf86639b77e more explicit flags for facts table;
wenzelm
parents: 48992
diff changeset
   229
    val props' = props
2cf86639b77e more explicit flags for facts table;
wenzelm
parents: 48992
diff changeset
   230
      |> index ? fold (fn th => Net.insert_term (K false) (Thm.full_prop_of th, th)) ths;
28864
d6fe93e3dcb9 fact table now using name bindings
haftmann
parents: 27738
diff changeset
   231
  in (name, make_facts facts' props') end;
26281
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   232
26393
42febbed5460 support dynamic facts;
wenzelm
parents: 26366
diff changeset
   233
26654
1f711934f221 disallow duplicate entries (weak version for merge);
wenzelm
parents: 26393
diff changeset
   234
(* add dynamic entries *)
26393
42febbed5460 support dynamic facts;
wenzelm
parents: 26366
diff changeset
   235
47005
421760a1efe7 maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents: 42375
diff changeset
   236
fun add_dynamic context (b, f) (Facts {facts, props}) =
421760a1efe7 maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents: 42375
diff changeset
   237
  let val (name, facts') = Name_Space.define context true (b, Dynamic f) facts;
33093
d010f61d3702 eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
wenzelm
parents: 29848
diff changeset
   238
  in (name, make_facts facts' props) end;
26281
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   239
26654
1f711934f221 disallow duplicate entries (weak version for merge);
wenzelm
parents: 26393
diff changeset
   240
1f711934f221 disallow duplicate entries (weak version for merge);
wenzelm
parents: 26393
diff changeset
   241
(* remove entries *)
1f711934f221 disallow duplicate entries (weak version for merge);
wenzelm
parents: 26393
diff changeset
   242
56025
d74fed45fa8b abstract type Name_Space.table;
wenzelm
parents: 56004
diff changeset
   243
fun del name (Facts {facts, props}) =
d74fed45fa8b abstract type Name_Space.table;
wenzelm
parents: 56004
diff changeset
   244
  make_facts (Name_Space.del_table name facts) props;
26654
1f711934f221 disallow duplicate entries (weak version for merge);
wenzelm
parents: 26393
diff changeset
   245
56025
d74fed45fa8b abstract type Name_Space.table;
wenzelm
parents: 56004
diff changeset
   246
fun hide fully name (Facts {facts, props}) =
d74fed45fa8b abstract type Name_Space.table;
wenzelm
parents: 56004
diff changeset
   247
  make_facts (Name_Space.hide_table fully name facts) props;
26654
1f711934f221 disallow duplicate entries (weak version for merge);
wenzelm
parents: 26393
diff changeset
   248
26281
328fd1c551ef Environment of named facts (admits overriding). Optional indexing by proposition.
wenzelm
parents:
diff changeset
   249
end;