src/Pure/facts.ML
author haftmann
Thu Nov 20 19:06:03 2008 +0100 (2008-11-20)
changeset 28864 d6fe93e3dcb9
parent 27738 66596d7aa899
child 28941 128459bd72d2
permissions -rw-r--r--
fact table now using name bindings
     1 (*  Title:      Pure/facts.ML
     2     ID:         $Id$
     3     Author:     Makarius
     4 
     5 Environment of named facts, optionally indexed by proposition.
     6 *)
     7 
     8 signature FACTS =
     9 sig
    10   val the_single: string -> thm list -> thm
    11   datatype interval = FromTo of int * int | From of int | Single of int
    12   datatype ref =
    13     Named of (string * Position.T) * interval list option |
    14     Fact of string
    15   val named: string -> ref
    16   val string_of_ref: ref -> string
    17   val name_of_ref: ref -> string
    18   val pos_of_ref: ref -> Position.T
    19   val map_name_of_ref: (string -> string) -> ref -> ref
    20   val select: ref -> thm list -> thm list
    21   val selections: string * thm list -> (ref * thm) list
    22   type T
    23   val empty: T
    24   val intern: T -> xstring -> string
    25   val extern: T -> string -> xstring
    26   val lookup: Context.generic -> T -> string -> (bool * thm list) option
    27   val defined: T -> string -> bool
    28   val fold_static: (string * thm list -> 'a -> 'a) -> T -> 'a -> 'a
    29   val dest_static: T list -> T -> (string * thm list) list
    30   val extern_static: T list -> T -> (xstring * thm list) list
    31   val props: T -> thm list
    32   val could_unify: T -> term -> thm list
    33   val merge: T * T -> T
    34   val add_global: NameSpace.naming -> Name.binding * thm list -> T -> string * T
    35   val add_local: bool -> NameSpace.naming -> Name.binding * thm list -> T -> string * T
    36   val add_dynamic: NameSpace.naming -> Name.binding * (Context.generic -> thm list) -> T -> string * T
    37   val del: string -> T -> T
    38   val hide: bool -> string -> T -> T
    39 end;
    40 
    41 structure Facts: FACTS =
    42 struct
    43 
    44 (** fact references **)
    45 
    46 fun the_single _ [th] : thm = th
    47   | the_single name _ = error ("Single theorem expected " ^ quote name);
    48 
    49 
    50 (* datatype interval *)
    51 
    52 datatype interval =
    53   FromTo of int * int |
    54   From of int |
    55   Single of int;
    56 
    57 fun string_of_interval (FromTo (i, j)) = string_of_int i ^ "-" ^ string_of_int j
    58   | string_of_interval (From i) = string_of_int i ^ "-"
    59   | string_of_interval (Single i) = string_of_int i;
    60 
    61 fun interval n iv =
    62   let fun err () = raise Fail ("Bad interval specification " ^ string_of_interval iv) in
    63     (case iv of
    64       FromTo (i, j) => if i <= j then i upto j else err ()
    65     | From i => if i <= n then i upto n else err ()
    66     | Single i => [i])
    67   end;
    68 
    69 
    70 (* datatype ref *)
    71 
    72 datatype ref =
    73   Named of (string * Position.T) * interval list option |
    74   Fact of string;
    75 
    76 fun named name = Named ((name, Position.none), NONE);
    77 
    78 fun name_pos_of_ref (Named (name_pos, _)) = name_pos
    79   | name_pos_of_ref (Fact _) = error "Illegal literal fact";
    80 
    81 val name_of_ref = #1 o name_pos_of_ref;
    82 val pos_of_ref = #2 o name_pos_of_ref;
    83 
    84 fun map_name_of_ref f (Named ((name, pos), is)) = Named ((f name, pos), is)
    85   | map_name_of_ref _ r = r;
    86 
    87 fun string_of_ref (Named ((name, _), NONE)) = name
    88   | string_of_ref (Named ((name, _), SOME is)) =
    89       name ^ enclose "(" ")" (commas (map string_of_interval is))
    90   | string_of_ref (Fact _) = error "Illegal literal fact";
    91 
    92 
    93 (* select *)
    94 
    95 fun select (Fact _) ths = ths
    96   | select (Named (_, NONE)) ths = ths
    97   | select (Named ((name, pos), SOME ivs)) ths =
    98       let
    99         val n = length ths;
   100         fun err msg =
   101           error (msg ^ " for " ^ quote name ^ " (length " ^ string_of_int n ^ ")" ^
   102             Position.str_of pos);
   103         fun sel i =
   104           if i < 1 orelse i > n then err ("Bad subscript " ^ string_of_int i)
   105           else nth ths (i - 1);
   106         val is = maps (interval n) ivs handle Fail msg => err msg;
   107       in map sel is end;
   108 
   109 
   110 (* selections *)
   111 
   112 fun selections (name, [th]) = [(Named ((name, Position.none), NONE), th)]
   113   | selections (name, ths) = map2 (fn i => fn th =>
   114       (Named ((name, Position.none), SOME [Single i]), th)) (1 upto length ths) ths;
   115 
   116 
   117 
   118 (** fact environment **)
   119 
   120 (* datatypes *)
   121 
   122 datatype fact = Static of thm list | Dynamic of Context.generic -> thm list;
   123 
   124 datatype T = Facts of
   125  {facts: (fact * serial) NameSpace.table,
   126   props: thm Net.net};
   127 
   128 fun make_facts facts props = Facts {facts = facts, props = props};
   129 val empty = make_facts NameSpace.empty_table Net.empty;
   130 
   131 
   132 (* named facts *)
   133 
   134 fun facts_of (Facts {facts, ...}) = facts;
   135 
   136 val space_of = #1 o facts_of;
   137 val table_of = #2 o facts_of;
   138 
   139 val intern = NameSpace.intern o space_of;
   140 val extern = NameSpace.extern o space_of;
   141 
   142 val defined = Symtab.defined o table_of;
   143 
   144 fun lookup context facts name =
   145   (case Symtab.lookup (table_of facts) name of
   146     NONE => NONE
   147   | SOME (Static ths, _) => SOME (true, ths)
   148   | SOME (Dynamic f, _) => SOME (false, f context));
   149 
   150 fun fold_static f = Symtab.fold (fn (name, (Static ths, _)) => f (name, ths) | _ => I) o table_of;
   151 
   152 
   153 (* content difference *)
   154 
   155 fun diff_table prev_facts facts =
   156   fold_static (fn (name, ths) =>
   157     if exists (fn prev => defined prev name) prev_facts then I
   158     else cons (name, ths)) facts [];
   159 
   160 fun dest_static prev_facts facts =
   161   sort_wrt #1 (diff_table prev_facts facts);
   162 
   163 fun extern_static prev_facts facts =
   164   sort_wrt #1 (diff_table prev_facts facts |> map (apfst (extern facts)));
   165 
   166 
   167 (* indexed props *)
   168 
   169 val prop_ord = Term.term_ord o pairself Thm.full_prop_of;
   170 
   171 fun props (Facts {props, ...}) = sort_distinct prop_ord (Net.content props);
   172 fun could_unify (Facts {props, ...}) = Net.unify_term props;
   173 
   174 
   175 (* merge facts *)
   176 
   177 fun err_dup_fact name = error ("Duplicate fact " ^ quote name);
   178 
   179 (* FIXME stamp identity! *)
   180 fun eq_entry ((Static ths1, _), (Static ths2, _)) = is_equal (list_ord Thm.thm_ord (ths1, ths2))
   181   | eq_entry ((_, id1: serial), (_, id2)) = id1 = id2;
   182 
   183 fun merge (Facts {facts = facts1, props = props1}, Facts {facts = facts2, props = props2}) =
   184   let
   185     val facts' = NameSpace.merge_tables eq_entry (facts1, facts2)
   186       handle Symtab.DUP dup => err_dup_fact dup;
   187     val props' = Net.merge (is_equal o prop_ord) (props1, props2);
   188   in make_facts facts' props' end;
   189 
   190 
   191 (* add static entries *)
   192 
   193 fun add permissive do_props naming (b, ths) (Facts {facts, props}) =
   194   let
   195     val (name, facts') = if Name.is_nothing b then ("", facts)
   196       else let
   197         val (space, tab) = facts;
   198         val (name, space') = NameSpace.declare_binding naming b space;
   199         val entry = (name, (Static ths, serial ()));
   200         val tab' = (if permissive then Symtab.update else Symtab.update_new) entry tab
   201           handle Symtab.DUP dup => err_dup_fact dup;
   202       in (name, (space', tab')) end;
   203     val props' =
   204       if do_props then fold (fn th => Net.insert_term (K false) (Thm.full_prop_of th, th)) ths props
   205       else props;
   206   in (name, make_facts facts' props') end;
   207 
   208 val add_global = add false false;
   209 val add_local = add true;
   210 
   211 
   212 (* add dynamic entries *)
   213 
   214 fun add_dynamic naming (b, f) (Facts {facts = (space, tab), props}) =
   215   let
   216     val (name, space') = NameSpace.declare_binding naming b space;
   217     val entry = (name, (Dynamic f, serial ()));
   218     val tab' = Symtab.update_new entry tab
   219       handle Symtab.DUP dup => err_dup_fact dup;
   220   in (name, make_facts (space', tab') props) end;
   221 
   222 
   223 (* remove entries *)
   224 
   225 fun del name (Facts {facts = (space, tab), props}) =
   226   let
   227     val space' = NameSpace.hide true name space handle ERROR _ => space;
   228     val tab' = Symtab.delete_safe name tab;
   229   in make_facts (space', tab') props end;
   230 
   231 fun hide fully name (Facts {facts = (space, tab), props}) =
   232   make_facts (NameSpace.hide fully name space, tab) props;
   233 
   234 end;