src/Pure/facts.ML
author wenzelm
Tue Apr 15 16:12:11 2008 +0200 (2008-04-15)
changeset 26654 1f711934f221
parent 26393 42febbed5460
child 26664 167d879a89b0
permissions -rw-r--r--
disallow duplicate entries (weak version for merge);
added hide;
     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 space_of: T -> NameSpace.T
    25   val lookup: Context.generic -> T -> string -> thm list option
    26   val dest: T -> (string * thm list) list
    27   val extern: T -> (xstring * thm list) list
    28   val props: T -> thm list
    29   val could_unify: T -> term -> thm list
    30   val merge: T * T -> T
    31   val add_global: NameSpace.naming -> string * thm list -> T -> T
    32   val add_local: bool -> NameSpace.naming -> string * thm list -> T -> T
    33   val add_dynamic: NameSpace.naming -> string * (Context.generic -> thm list) -> T -> T
    34   val del: string -> T -> T
    35   val hide: bool -> string -> T -> T
    36 end;
    37 
    38 structure Facts: FACTS =
    39 struct
    40 
    41 (** fact references **)
    42 
    43 fun the_single _ [th] : thm = th
    44   | the_single name _ = error ("Single theorem expected " ^ quote name);
    45 
    46 
    47 (* datatype interval *)
    48 
    49 datatype interval =
    50   FromTo of int * int |
    51   From of int |
    52   Single of int;
    53 
    54 fun string_of_interval (FromTo (i, j)) = string_of_int i ^ "-" ^ string_of_int j
    55   | string_of_interval (From i) = string_of_int i ^ "-"
    56   | string_of_interval (Single i) = string_of_int i;
    57 
    58 fun interval n iv =
    59   let fun err () = raise Fail ("Bad interval specification " ^ string_of_interval iv) in
    60     (case iv of
    61       FromTo (i, j) => if i <= j then i upto j else err ()
    62     | From i => if i <= n then i upto n else err ()
    63     | Single i => [i])
    64   end;
    65 
    66 
    67 (* datatype ref *)
    68 
    69 datatype ref =
    70   Named of (string * Position.T) * interval list option |
    71   Fact of string;
    72 
    73 fun named name = Named ((name, Position.none), NONE);
    74 
    75 fun name_pos_of_ref (Named (name_pos, _)) = name_pos
    76   | name_pos_of_ref (Fact _) = error "Illegal literal fact";
    77 
    78 val name_of_ref = #1 o name_pos_of_ref;
    79 val pos_of_ref = #2 o name_pos_of_ref;
    80 
    81 fun map_name_of_ref f (Named ((name, pos), is)) = Named ((f name, pos), is)
    82   | map_name_of_ref _ r = r;
    83 
    84 fun string_of_ref (Named ((name, _), NONE)) = name
    85   | string_of_ref (Named ((name, _), SOME is)) =
    86       name ^ enclose "(" ")" (commas (map string_of_interval is))
    87   | string_of_ref (Fact _) = error "Illegal literal fact";
    88 
    89 
    90 (* select *)
    91 
    92 fun select (Fact _) ths = ths
    93   | select (Named (_, NONE)) ths = ths
    94   | select (Named ((name, pos), SOME ivs)) ths =
    95       let
    96         val n = length ths;
    97         fun err msg =
    98           error (msg ^ " for " ^ quote name ^ " (length " ^ string_of_int n ^ ")" ^
    99             Position.str_of pos);
   100         fun sel i =
   101           if i < 1 orelse i > n then err ("Bad subscript " ^ string_of_int i)
   102           else nth ths (i - 1);
   103         val is = maps (interval n) ivs handle Fail msg => err msg;
   104       in map sel is end;
   105 
   106 
   107 (* selections *)
   108 
   109 fun selections (name, [th]) = [(Named ((name, Position.none), NONE), th)]
   110   | selections (name, ths) = map2 (fn i => fn th =>
   111       (Named ((name, Position.none), SOME [Single i]), th)) (1 upto length ths) ths;
   112 
   113 
   114 
   115 (** fact environment **)
   116 
   117 (* datatypes *)
   118 
   119 datatype fact = Static of thm list | Dynamic of Context.generic -> thm list;
   120 
   121 datatype T = Facts of
   122  {facts: (fact * serial) NameSpace.table,
   123   props: thm Net.net};
   124 
   125 fun make_facts facts props = Facts {facts = facts, props = props};
   126 val empty = make_facts NameSpace.empty_table Net.empty;
   127 
   128 
   129 (* named facts *)
   130 
   131 fun facts_of (Facts {facts, ...}) = facts;
   132 
   133 val space_of = #1 o facts_of;
   134 val table_of = #2 o facts_of;
   135 
   136 fun lookup context facts name =
   137   (case Symtab.lookup (table_of facts) name of
   138     NONE => NONE
   139   | SOME (Static ths, _) => SOME ths
   140   | SOME (Dynamic f, _) => SOME (f context));
   141 
   142 fun dest facts =
   143   Symtab.fold (fn (name, (Static ths, _)) => cons (name, ths) | _ => I) (table_of facts) [];
   144 
   145 fun extern facts =
   146   dest facts
   147   |> map (apfst (NameSpace.extern (space_of facts)))
   148   |> sort_wrt #1;
   149 
   150 
   151 (* indexed props *)
   152 
   153 val prop_ord = Term.term_ord o pairself Thm.full_prop_of;
   154 
   155 fun props (Facts {props, ...}) = sort_distinct prop_ord (Net.content props);
   156 fun could_unify (Facts {props, ...}) = Net.unify_term props;
   157 
   158 
   159 (* merge facts *)
   160 
   161 fun err_dup_fact name = error ("Duplicate fact " ^ quote name);
   162 
   163 fun eq_entry ((Static ths1, _), (Static ths2, _)) = is_equal (list_ord Thm.thm_ord (ths1, ths2))
   164   | eq_entry ((_, id1: serial), (_, id2)) = id1 = id2;
   165 
   166 fun merge (Facts {facts = facts1, props = props1}, Facts {facts = facts2, props = props2}) =
   167   let
   168     val facts' = NameSpace.merge_tables eq_entry (facts1, facts2)
   169       handle Symtab.DUP dup => err_dup_fact dup;
   170     val props' = Net.merge (is_equal o prop_ord) (props1, props2);
   171   in make_facts facts' props' end;
   172 
   173 
   174 (* add static entries *)
   175 
   176 fun add permissive do_props naming (name, ths) (Facts {facts, props}) =
   177   let
   178     val facts' =
   179       if name = "" then facts
   180       else
   181         let
   182           val (space, tab) = facts;
   183           val space' = NameSpace.declare naming name space;
   184           val entry = (name, (Static ths, serial ()));
   185           val tab' = (if permissive then Symtab.update else Symtab.update_new) entry tab
   186             handle Symtab.DUP dup => err_dup_fact dup;
   187         in (space', tab') end;
   188     val props' =
   189       if do_props then fold (fn th => Net.insert_term (K false) (Thm.full_prop_of th, th)) ths props
   190       else props;
   191   in make_facts facts' props' end;
   192 
   193 val add_global = add false false;
   194 val add_local = add true;
   195 
   196 
   197 (* add dynamic entries *)
   198 
   199 fun add_dynamic naming (name, f) (Facts {facts = (space, tab), props}) =
   200   let
   201     val space' = NameSpace.declare naming name space;
   202     val entry = (name, (Dynamic f, serial ()));
   203     val tab' = Symtab.update_new entry tab
   204       handle Symtab.DUP dup => err_dup_fact dup;
   205   in make_facts (space', tab') props end;
   206 
   207 
   208 (* remove entries *)
   209 
   210 fun del name (Facts {facts = (space, tab), props}) =
   211   let
   212     val space' = NameSpace.hide true name space handle ERROR _ => space;
   213     val tab' = Symtab.delete_safe name tab;
   214   in make_facts (space', tab') props end;
   215 
   216 fun hide fully name (Facts {facts = (space, tab), props}) =
   217   make_facts (NameSpace.hide fully name space, tab) props;
   218 
   219 end;