src/Pure/facts.ML
author wenzelm
Thu Mar 20 16:04:30 2008 +0100 (2008-03-20)
changeset 26361 7946f459c6c8
parent 26336 a0e2b706ce73
child 26366 5c089f4219c2
permissions -rw-r--r--
Facts.Named: include position;
wenzelm@26281
     1
(*  Title:      Pure/facts.ML
wenzelm@26281
     2
    ID:         $Id$
wenzelm@26281
     3
    Author:     Makarius
wenzelm@26281
     4
wenzelm@26307
     5
Environment of named facts, optionally indexed by proposition.
wenzelm@26281
     6
*)
wenzelm@26281
     7
wenzelm@26281
     8
signature FACTS =
wenzelm@26281
     9
sig
wenzelm@26336
    10
  val the_single: string -> thm list -> thm
wenzelm@26336
    11
  datatype interval = FromTo of int * int | From of int | Single of int
wenzelm@26336
    12
  datatype ref =
wenzelm@26361
    13
    Named of (string * Position.T) * interval list option |
wenzelm@26336
    14
    Fact of string
wenzelm@26361
    15
  val named: string -> ref
wenzelm@26336
    16
  val string_of_ref: ref -> string
wenzelm@26336
    17
  val name_of_ref: ref -> string
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@26281
    22
  val space_of: T -> NameSpace.T
wenzelm@26281
    23
  val lookup: T -> string -> thm list option
wenzelm@26281
    24
  val dest: T -> (string * thm list) list
wenzelm@26281
    25
  val extern: T -> (xstring * thm list) list
wenzelm@26281
    26
  val props: T -> thm list
wenzelm@26281
    27
  val could_unify: T -> term -> thm list
wenzelm@26281
    28
  val empty: T
wenzelm@26281
    29
  val merge: T * T -> T
wenzelm@26307
    30
  val add_global: NameSpace.naming -> string * thm list -> T -> T
wenzelm@26307
    31
  val add_local: bool -> NameSpace.naming -> string * thm list -> T -> T
wenzelm@26281
    32
  val del: string -> T -> T
wenzelm@26281
    33
end;
wenzelm@26281
    34
wenzelm@26281
    35
structure Facts: FACTS =
wenzelm@26281
    36
struct
wenzelm@26281
    37
wenzelm@26336
    38
(** fact references **)
wenzelm@26336
    39
wenzelm@26336
    40
fun the_single _ [th] : thm = th
wenzelm@26336
    41
  | the_single name _ = error ("Single theorem expected " ^ quote name);
wenzelm@26336
    42
wenzelm@26336
    43
wenzelm@26336
    44
(* datatype interval *)
wenzelm@26336
    45
wenzelm@26336
    46
datatype interval =
wenzelm@26336
    47
  FromTo of int * int |
wenzelm@26336
    48
  From of int |
wenzelm@26336
    49
  Single of int;
wenzelm@26336
    50
wenzelm@26336
    51
fun string_of_interval (FromTo (i, j)) = string_of_int i ^ "-" ^ string_of_int j
wenzelm@26336
    52
  | string_of_interval (From i) = string_of_int i ^ "-"
wenzelm@26336
    53
  | string_of_interval (Single i) = string_of_int i;
wenzelm@26336
    54
wenzelm@26336
    55
fun interval n iv =
wenzelm@26336
    56
  let fun err () = raise Fail ("Bad interval specification " ^ string_of_interval iv) in
wenzelm@26336
    57
    (case iv of
wenzelm@26336
    58
      FromTo (i, j) => if i <= j then i upto j else err ()
wenzelm@26336
    59
    | From i => if i <= n then i upto n else err ()
wenzelm@26336
    60
    | Single i => [i])
wenzelm@26336
    61
  end;
wenzelm@26336
    62
wenzelm@26336
    63
wenzelm@26336
    64
(* datatype ref *)
wenzelm@26336
    65
wenzelm@26336
    66
datatype ref =
wenzelm@26361
    67
  Named of (string * Position.T) * interval list option |
wenzelm@26336
    68
  Fact of string;
wenzelm@26336
    69
wenzelm@26361
    70
fun named name = Named ((name, Position.none), NONE);
wenzelm@26361
    71
wenzelm@26361
    72
fun name_of_ref (Named ((name, _), _)) = name
wenzelm@26336
    73
  | name_of_ref (Fact _) = error "Illegal literal fact";
wenzelm@26336
    74
wenzelm@26361
    75
fun map_name_of_ref f (Named ((name, pos), is)) = Named ((f name, pos), is)
wenzelm@26336
    76
  | map_name_of_ref _ r = r;
wenzelm@26336
    77
wenzelm@26361
    78
fun string_of_ref (Named ((name, _), NONE)) = name
wenzelm@26361
    79
  | string_of_ref (Named ((name, _), SOME is)) =
wenzelm@26336
    80
      name ^ enclose "(" ")" (commas (map string_of_interval is))
wenzelm@26336
    81
  | string_of_ref (Fact _) = error "Illegal literal fact";
wenzelm@26336
    82
wenzelm@26336
    83
wenzelm@26336
    84
(* select *)
wenzelm@26336
    85
wenzelm@26336
    86
fun select (Fact _) ths = ths
wenzelm@26336
    87
  | select (Named (_, NONE)) ths = ths
wenzelm@26361
    88
  | select (Named ((name, pos), SOME ivs)) ths =
wenzelm@26336
    89
      let
wenzelm@26336
    90
        val n = length ths;
wenzelm@26361
    91
        fun err msg =
wenzelm@26361
    92
          error (msg ^ " for " ^ quote name ^ " (length " ^ string_of_int n ^ ")" ^
wenzelm@26361
    93
            Position.str_of pos);
wenzelm@26336
    94
        fun sel i =
wenzelm@26336
    95
          if i < 1 orelse i > n then err ("Bad subscript " ^ string_of_int i)
wenzelm@26336
    96
          else nth ths (i - 1);
wenzelm@26336
    97
        val is = maps (interval n) ivs handle Fail msg => err msg;
wenzelm@26336
    98
      in map sel is end;
wenzelm@26336
    99
wenzelm@26336
   100
wenzelm@26336
   101
(* selections *)
wenzelm@26336
   102
wenzelm@26361
   103
fun selections (name, [th]) = [(Named ((name, Position.none), NONE), th)]
wenzelm@26361
   104
  | selections (name, ths) = map2 (fn i => fn th =>
wenzelm@26361
   105
      (Named ((name, Position.none), SOME [Single i]), th)) (1 upto length ths) ths;
wenzelm@26336
   106
wenzelm@26336
   107
wenzelm@26336
   108
wenzelm@26336
   109
(** fact environment **)
wenzelm@26336
   110
wenzelm@26336
   111
(* datatype T *)
wenzelm@26281
   112
wenzelm@26281
   113
datatype T = Facts of
wenzelm@26281
   114
 {facts: thm list NameSpace.table,
wenzelm@26281
   115
  props: thm Net.net};
wenzelm@26281
   116
wenzelm@26281
   117
fun make_facts facts props = Facts {facts = facts, props = props};
wenzelm@26281
   118
wenzelm@26281
   119
wenzelm@26281
   120
(* named facts *)
wenzelm@26281
   121
wenzelm@26281
   122
fun facts_of (Facts {facts, ...}) = facts;
wenzelm@26281
   123
wenzelm@26281
   124
val space_of = #1 o facts_of;
wenzelm@26281
   125
val lookup = Symtab.lookup o #2 o facts_of;
wenzelm@26281
   126
val dest = NameSpace.dest_table o facts_of;
wenzelm@26281
   127
val extern = NameSpace.extern_table o facts_of;
wenzelm@26281
   128
wenzelm@26281
   129
wenzelm@26281
   130
(* indexed props *)
wenzelm@26281
   131
wenzelm@26281
   132
val prop_ord = Term.term_ord o pairself Thm.full_prop_of;
wenzelm@26281
   133
wenzelm@26281
   134
fun props (Facts {props, ...}) = sort_distinct prop_ord (Net.content props);
wenzelm@26281
   135
fun could_unify (Facts {props, ...}) = Net.unify_term props;
wenzelm@26281
   136
wenzelm@26281
   137
wenzelm@26281
   138
(* build facts *)
wenzelm@26281
   139
wenzelm@26281
   140
val empty = make_facts NameSpace.empty_table Net.empty;
wenzelm@26281
   141
wenzelm@26281
   142
fun merge (Facts {facts = facts1, props = props1}, Facts {facts = facts2, props = props2}) =
wenzelm@26281
   143
  let
wenzelm@26281
   144
    val facts' = NameSpace.merge_tables (K true) (facts1, facts2);
wenzelm@26281
   145
    val props' = Net.merge (is_equal o prop_ord) (props1, props2);
wenzelm@26281
   146
  in make_facts facts' props' end;
wenzelm@26281
   147
wenzelm@26307
   148
fun add permissive do_props naming (name, ths) (Facts {facts, props}) =
wenzelm@26281
   149
  let
wenzelm@26281
   150
    val facts' =
wenzelm@26281
   151
      if name = "" then facts
wenzelm@26281
   152
      else
wenzelm@26281
   153
        let
wenzelm@26281
   154
          val (space, tab) = facts;
wenzelm@26281
   155
          val space' = NameSpace.declare naming name space;
wenzelm@26307
   156
          val tab' = (if permissive then Symtab.update else Symtab.update_new) (name, ths) tab
wenzelm@26307
   157
            handle Symtab.DUP dup => (legacy_feature ("Duplicate fact " ^ quote dup ^
wenzelm@26307
   158
              Position.str_of (Position.thread_data ())); tab);
wenzelm@26281
   159
        in (space', tab') end;
wenzelm@26281
   160
    val props' =
wenzelm@26281
   161
      if do_props then fold (fn th => Net.insert_term (K false) (Thm.full_prop_of th, th)) ths props
wenzelm@26281
   162
      else props;
wenzelm@26281
   163
  in make_facts facts' props' end;
wenzelm@26281
   164
wenzelm@26307
   165
val add_global = add false false;
wenzelm@26307
   166
val add_local = add true;
wenzelm@26307
   167
wenzelm@26281
   168
fun del name (Facts {facts = (space, tab), props}) =
wenzelm@26290
   169
  let
wenzelm@26290
   170
    val space' = NameSpace.hide true name space handle ERROR _ => space;
wenzelm@26290
   171
    val tab' = Symtab.delete_safe name tab;
wenzelm@26290
   172
  in make_facts (space', tab') props end;
wenzelm@26281
   173
wenzelm@26281
   174
end;