src/Pure/fact_index.ML
author haftmann
Wed, 13 Jul 2005 11:30:37 +0200
changeset 16790 be2780f435e1
parent 16491 7310d0a36599
child 16874 3057990d20e0
permissions -rw-r--r--
(fix for an accidental commit)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13270
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/fact_index.ML
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
     4
16020
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
     5
Facts indexed by consts or frees.
13270
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
     6
*)
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
     7
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
     8
signature FACT_INDEX =
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
     9
sig
16020
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    10
  type spec
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    11
  val index_term: (string -> bool) -> term -> spec -> spec
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    12
  val index_thm: (string -> bool) -> thm -> spec -> spec
13270
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    13
  type T
16020
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    14
  val facts: T -> (string * thm list) list
13270
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    15
  val empty: T
16020
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    16
  val add: (string -> bool) -> (string * thm list) -> T -> T
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    17
  val find: T -> spec -> (string * thm list) list
13270
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    18
end;
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    19
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    20
structure FactIndex: FACT_INDEX =
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    21
struct
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    22
16020
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    23
type spec = string list * string list;
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    24
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    25
13270
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    26
(* indexing items *)
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    27
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    28
fun add_frees pred (Free (x, _), xs) = if pred x then x ins_string xs else xs
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    29
  | add_frees pred (t $ u, xs) = add_frees pred (t, add_frees pred (u, xs))
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    30
  | add_frees pred (Abs (_, _, t), xs) = add_frees pred (t, xs)
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    31
  | add_frees _ (_, xs) = xs;
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    32
16020
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    33
fun index_term pred t (cs, xs) =
13542
bb3e8a86d610 thms_containing: allow "_" in specification;
wenzelm
parents: 13283
diff changeset
    34
  (Term.add_term_consts (t, cs) \ Term.dummy_patternN, add_frees pred (t, xs));
13270
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    35
16020
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    36
fun index_thm pred th idx =
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    37
  let val {tpairs, hyps, prop, ...} = Thm.rep_thm th in
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    38
    idx
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    39
    |> index_term pred prop
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    40
    |> fold (index_term pred) hyps
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    41
    |> fold (index_term pred) (Thm.terms_of_tpairs tpairs)
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    42
  end;
13270
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    43
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    44
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    45
(* build index *)
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    46
13283
1051aa66cbf3 proper treatment of border cases;
wenzelm
parents: 13270
diff changeset
    47
datatype T = Index of {next: int, facts: (string * thm list) list,
13270
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    48
  consts: (int * (string * thm list)) list Symtab.table,
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    49
  frees: (int * (string * thm list)) list Symtab.table};
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    50
16020
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    51
fun facts (Index {facts, ...}) = facts;
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    52
13270
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    53
val empty =
13283
1051aa66cbf3 proper treatment of border cases;
wenzelm
parents: 13270
diff changeset
    54
  Index {next = 0, facts = [], consts = Symtab.empty, frees = Symtab.empty};
13270
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    55
16020
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    56
fun add pred (name, ths) (Index {next, facts, consts, frees}) =
13270
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    57
  let
16020
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    58
    fun upd a tab = Symtab.update_multi ((a, (next, (name, ths))), tab);
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    59
    val (cs, xs) = fold (index_thm pred) ths ([], []);
13283
1051aa66cbf3 proper treatment of border cases;
wenzelm
parents: 13270
diff changeset
    60
  in
1051aa66cbf3 proper treatment of border cases;
wenzelm
parents: 13270
diff changeset
    61
    Index {next = next + 1, facts = (name, ths) :: facts,
16020
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    62
      consts = fold upd cs consts, frees = fold upd xs frees}
13283
1051aa66cbf3 proper treatment of border cases;
wenzelm
parents: 13270
diff changeset
    63
  end;
13270
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    64
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    65
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    66
(* find facts *)
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    67
16491
7310d0a36599 OrdList.inter;
wenzelm
parents: 16020
diff changeset
    68
fun fact_ord ((i, _), (j, _)) = int_ord (j, i);
13270
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    69
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    70
fun intersects [xs] = xs
16491
7310d0a36599 OrdList.inter;
wenzelm
parents: 16020
diff changeset
    71
  | intersects xss =
7310d0a36599 OrdList.inter;
wenzelm
parents: 16020
diff changeset
    72
      if exists null xss then []
7310d0a36599 OrdList.inter;
wenzelm
parents: 16020
diff changeset
    73
      else fold (OrdList.inter fact_ord) (tl xss) (hd xss);
13270
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    74
16020
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    75
fun find idx ([], []) = facts idx
ace2c610b5be major tuning;
wenzelm
parents: 15570
diff changeset
    76
  | find (Index {consts, frees, ...}) (cs, xs) =
13283
1051aa66cbf3 proper treatment of border cases;
wenzelm
parents: 13270
diff changeset
    77
      (map (curry Symtab.lookup_multi consts) cs @
1051aa66cbf3 proper treatment of border cases;
wenzelm
parents: 13270
diff changeset
    78
        map (curry Symtab.lookup_multi frees) xs) |> intersects |> map #2;
13270
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    79
d7f35250cbad Facts indexed by consts or (some) frees.
wenzelm
parents:
diff changeset
    80
end;