src/Pure/pure_thy.ML
author wenzelm
Fri Oct 28 22:27:59 2005 +0200 (2005-10-28)
changeset 18031 b17e25a7d820
parent 17930 e7160d70be1f
child 18358 0a733e11021a
permissions -rw-r--r--
datatype thmref: added Fact;
renamed Goal constant to prop;
     1 (*  Title:      Pure/pure_thy.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Theorem storage.  The ProtoPure theory.
     6 *)
     7 
     8 signature BASIC_PURE_THY =
     9 sig
    10   datatype interval = FromTo of int * int | From of int | Single of int
    11   datatype thmref =
    12     Name of string |
    13     NameSelection of string * interval list |
    14     Fact of string
    15   val print_theorems: theory -> unit
    16   val print_theory: theory -> unit
    17   val get_thm: theory -> thmref -> thm
    18   val get_thms: theory -> thmref -> thm list
    19   val get_thmss: theory -> thmref list -> thm list
    20   val thm: xstring -> thm
    21   val thms: xstring -> thm list
    22   structure ProtoPure:
    23     sig
    24       val thy: theory
    25       val prop_def: thm
    26     end
    27 end;
    28 
    29 signature PURE_THY =
    30 sig
    31   include BASIC_PURE_THY
    32   val string_of_thmref: thmref -> string
    33   val print_theorems_diff: theory -> theory -> unit
    34   val get_thm_closure: theory -> thmref -> thm
    35   val get_thms_closure: theory -> thmref -> thm list
    36   val single_thm: string -> thm list -> thm
    37   val name_of_thmref: thmref -> string
    38   val map_name_of_thmref: (string -> string) -> thmref -> thmref
    39   val select_thm: thmref -> thm list -> thm list
    40   val selections: string * thm list -> (thmref * thm) list
    41   val theorems_of: theory -> thm list NameSpace.table
    42   val theorem_space: theory -> NameSpace.T
    43   val fact_index_of: theory -> FactIndex.T
    44   val valid_thms: theory -> thmref * thm list -> bool
    45   val thms_containing: theory -> FactIndex.spec -> (string * thm list) list
    46   val thms_containing_consts: theory -> string list -> (string * thm) list
    47   val thms_of: theory -> (string * thm) list
    48   val all_thms_of: theory -> (string * thm) list
    49   val hide_thms: bool -> string list -> theory -> theory
    50   val store_thm: (bstring * thm) * theory attribute list -> theory -> theory * thm
    51   val smart_store_thms: (bstring * thm list) -> thm list
    52   val smart_store_thms_open: (bstring * thm list) -> thm list
    53   val forall_elim_var: int -> thm -> thm
    54   val forall_elim_vars: int -> thm -> thm
    55   val add_thms: ((bstring * thm) * theory attribute list) list -> theory -> theory * thm list
    56   val add_thmss: ((bstring * thm list) * theory attribute list) list -> theory
    57     -> theory * thm list list
    58   val note_thmss: theory attribute -> ((bstring * theory attribute list) *
    59     (thmref * theory attribute list) list) list ->
    60     theory -> theory * (bstring * thm list) list
    61   val note_thmss_i: theory attribute -> ((bstring * theory attribute list) *
    62     (thm list * theory attribute list) list) list ->
    63     theory -> theory * (bstring * thm list) list
    64   val add_axioms: ((bstring * string) * theory attribute list) list ->
    65     theory -> theory * thm list
    66   val add_axioms_i: ((bstring * term) * theory attribute list) list ->
    67     theory -> theory * thm list
    68   val add_axiomss: ((bstring * string list) * theory attribute list) list ->
    69     theory -> theory * thm list list
    70   val add_axiomss_i: ((bstring * term list) * theory attribute list) list ->
    71     theory -> theory * thm list list
    72   val add_defs: bool -> ((bstring * string) * theory attribute list) list ->
    73     theory -> theory * thm list
    74   val add_defs_i: bool -> ((bstring * term) * theory attribute list) list ->
    75     theory -> theory * thm list
    76   val add_defss: bool -> ((bstring * string list) * theory attribute list) list ->
    77     theory -> theory * thm list list
    78   val add_defss_i: bool -> ((bstring * term list) * theory attribute list) list ->
    79     theory -> theory * thm list list
    80   val generic_setup: string -> theory -> theory
    81   val add_oracle: bstring * string * string -> theory -> theory
    82 end;
    83 
    84 structure PureThy: PURE_THY =
    85 struct
    86 
    87 
    88 (*** theorem database ***)
    89 
    90 (** dataype theorems **)
    91 
    92 fun pretty_theorems_diff thy prev_thms (space, thms) =
    93   let
    94     val prt_thm = Display.pretty_thm_sg thy;
    95     fun prt_thms (name, [th]) =
    96           Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt_thm th]
    97       | prt_thms (name, ths) = Pretty.big_list (name ^ ":") (map prt_thm ths);
    98 
    99     val diff_thmss = Symtab.fold (fn fact =>
   100       if not (Symtab.member eq_thms prev_thms fact) then cons fact else I) thms [];
   101     val thmss = diff_thmss |> map (apfst (NameSpace.extern space)) |> Library.sort_wrt #1;
   102   in Pretty.big_list "theorems:" (map prt_thms thmss) end;
   103 
   104 fun pretty_theorems thy = pretty_theorems_diff thy Symtab.empty;
   105 
   106 structure TheoremsData = TheoryDataFun
   107 (struct
   108   val name = "Pure/theorems";
   109   type T =
   110    {theorems: thm list NameSpace.table,
   111     index: FactIndex.T} ref;
   112 
   113   fun mk_empty _ =
   114     ref {theorems = NameSpace.empty_table, index = FactIndex.empty}: T;
   115 
   116   val empty = mk_empty ();
   117   fun copy (ref x) = ref x;
   118   val extend = mk_empty;
   119   fun merge _ = mk_empty;
   120   fun print thy (ref {theorems, index}) = Pretty.writeln (pretty_theorems thy theorems);
   121 end);
   122 
   123 val get_theorems_ref = TheoremsData.get;
   124 val get_theorems = ! o get_theorems_ref;
   125 val theorems_of = #theorems o get_theorems;
   126 val theorem_space = #1 o theorems_of;
   127 val fact_index_of = #index o get_theorems;
   128 
   129 
   130 (* print theory *)
   131 
   132 val print_theorems = TheoremsData.print;
   133 
   134 fun print_theorems_diff prev_thy thy =
   135   Pretty.writeln (pretty_theorems_diff thy
   136     (#2 (theorems_of prev_thy)) (#theorems (get_theorems thy)));
   137 
   138 fun print_theory thy =
   139   Display.pretty_full_theory thy @
   140     [pretty_theorems thy (#theorems (get_theorems thy))]
   141   |> Pretty.chunks |> Pretty.writeln;
   142 
   143 
   144 
   145 (** retrieve theorems **)
   146 
   147 fun the_thms _ (SOME thms) = thms
   148   | the_thms name NONE = error ("Unknown theorem(s) " ^ quote name);
   149 
   150 fun single_thm _ [thm] = thm
   151   | single_thm name _ = error ("Single theorem expected " ^ quote name);
   152 
   153 
   154 (* datatype interval *)
   155 
   156 datatype interval =
   157   FromTo of int * int |
   158   From of int |
   159   Single of int;
   160 
   161 fun interval _ (FromTo (i, j)) = i upto j
   162   | interval n (From i) = i upto n
   163   | interval _ (Single i) = [i];
   164 
   165 fun string_of_interval (FromTo (i, j)) = string_of_int i ^ "-" ^ string_of_int j
   166   | string_of_interval (From i) = string_of_int i ^ "-"
   167   | string_of_interval (Single i) = string_of_int i;
   168 
   169 
   170 (* datatype thmref *)
   171 
   172 datatype thmref =
   173   Name of string |
   174   NameSelection of string * interval list |
   175   Fact of string;
   176 
   177 fun name_of_thmref (Name name) = name
   178   | name_of_thmref (NameSelection (name, _)) = name
   179   | name_of_thmref (Fact _) = raise ERROR_MESSAGE "Illegal literal fact";
   180 
   181 fun map_name_of_thmref f (Name name) = Name (f name)
   182   | map_name_of_thmref f (NameSelection (name, is)) = NameSelection (f name, is)
   183   | map_name_of_thmref _ thmref = thmref;
   184 
   185 fun string_of_thmref (Name name) = name
   186   | string_of_thmref (NameSelection (name, is)) =
   187       name ^ enclose "(" ")" (commas (map string_of_interval is))
   188   | string_of_thmref (Fact _) = raise ERROR_MESSAGE "Illegal literal fact";
   189 
   190 
   191 (* select_thm *)
   192 
   193 fun select_thm (Name _) thms = thms
   194   | select_thm (Fact _) thms = thms
   195   | select_thm (NameSelection (name, is)) thms =
   196       let
   197         val n = length thms;
   198         fun select i =
   199           if i < 1 orelse i > n then
   200             error ("Bad subscript " ^ string_of_int i ^ " for " ^
   201               quote name ^ " (length " ^ string_of_int n ^ ")")
   202           else List.nth (thms, i - 1);
   203       in map select (List.concat (map (interval n) is)) end;
   204 
   205 
   206 (* selections *)
   207 
   208 fun selections (name, [thm]) = [(Name name, thm)]
   209   | selections (name, thms) = (1 upto length thms, thms) |> ListPair.map (fn (i, thm) =>
   210       (NameSelection (name, [Single i]), thm));
   211 
   212 
   213 (* get_thm(s)_closure -- statically scoped versions *)
   214 
   215 (*beware of proper order of evaluation!*)
   216 
   217 fun lookup_thms thy =
   218   let
   219     val thy_ref = Theory.self_ref thy;
   220     val (space, thms) = #theorems (get_theorems thy);
   221   in
   222     fn name =>
   223       Option.map (map (Thm.transfer (Theory.deref thy_ref)))     (*dynamic identity*)
   224       (Symtab.lookup thms (NameSpace.intern space name)) (*static content*)
   225   end;
   226 
   227 fun get_thms_closure thy =
   228   let val closures = map lookup_thms (thy :: Theory.ancestors_of thy) in
   229     fn thmref =>
   230       let val name = name_of_thmref thmref;
   231       in select_thm thmref (the_thms name (get_first (fn f => f name) closures)) end
   232   end;
   233 
   234 fun get_thm_closure thy =
   235   let val get = get_thms_closure thy
   236   in fn thmref => single_thm (name_of_thmref thmref) (get thmref) end;
   237 
   238 
   239 (* get_thms etc. *)
   240 
   241 fun get_thms theory thmref =
   242   let val name = name_of_thmref thmref in
   243     get_first (fn thy => lookup_thms thy name) (theory :: Theory.ancestors_of theory)
   244     |> the_thms name |> select_thm thmref |> map (Thm.transfer theory)
   245   end;
   246 
   247 fun get_thmss thy thmrefs = List.concat (map (get_thms thy) thmrefs);
   248 fun get_thm thy thmref = single_thm (name_of_thmref thmref) (get_thms thy thmref);
   249 
   250 fun thm name = get_thm (the_context ()) (Name name);
   251 fun thms name = get_thms (the_context ()) (Name name);
   252 
   253 
   254 (* thms_containing etc. *)
   255 
   256 fun valid_thms thy (thmref, ths) =
   257   (case try (transform_error (get_thms thy)) thmref of
   258     NONE => false
   259   | SOME ths' => Thm.eq_thms (ths, ths'));
   260 
   261 fun thms_containing theory spec =
   262   (theory :: Theory.ancestors_of theory)
   263   |> map (fn thy =>
   264       FactIndex.find (fact_index_of thy) spec
   265       |> List.filter (fn (name, ths) => valid_thms theory (Name name, ths))
   266       |> gen_distinct (eq_fst (op =)))
   267   |> List.concat;
   268 
   269 fun thms_containing_consts thy consts =
   270   thms_containing thy (consts, []) |> map #2 |> List.concat
   271   |> map (fn th => (Thm.name_of_thm th, th));
   272 
   273 
   274 (* thms_of etc. *)
   275 
   276 fun thms_of thy =
   277   let val thms = #2 (theorems_of thy)
   278   in map (fn th => (Thm.name_of_thm th, th)) (List.concat (map snd (Symtab.dest thms))) end;
   279 
   280 fun all_thms_of thy = List.concat (map thms_of (thy :: Theory.ancestors_of thy));
   281 
   282 
   283 
   284 (** store theorems **)                    (*DESTRUCTIVE*)
   285 
   286 (* hiding -- affects current theory node only *)
   287 
   288 fun hide_thms fully names thy =
   289   let
   290     val r as ref {theorems = (space, thms), index} = get_theorems_ref thy;
   291     val space' = fold (NameSpace.hide fully) names space;
   292   in r := {theorems = (space', thms), index = index}; thy end;
   293 
   294 
   295 (* naming *)
   296 
   297 fun gen_names j len name =
   298   map (fn i => name ^ "_" ^ string_of_int i) (j + 1 upto j + len);
   299 
   300 fun name_multi name xs = gen_names 0 (length xs) name ~~ xs;
   301 
   302 fun name_thm pre (name, thm) =
   303   if Thm.name_of_thm thm <> "" andalso pre then thm else Thm.name_thm (name, thm);
   304 
   305 fun name_thms pre name [x] = [name_thm pre (name, x)]
   306   | name_thms pre name xs = map (name_thm pre) (name_multi name xs);
   307 
   308 fun name_thmss name xs =
   309   (case filter_out (null o fst) xs of
   310     [([x], z)] => [([name_thm true (name, x)], z)]
   311   | _ => snd (foldl_map (fn (i, (ys, z)) =>
   312     (i + length ys, (map (name_thm true) (gen_names i (length ys) name ~~ ys), z))) (0, xs)));
   313 
   314 
   315 (* enter_thms *)
   316 
   317 fun warn_overwrite name = warning ("Replaced old copy of theorems " ^ quote name);
   318 fun warn_same name = warning ("Theorem database already contains a copy of " ^ quote name);
   319 
   320 fun enter_thms _ _ app_att ("", thms) thy = app_att (thy, thms)
   321   | enter_thms pre_name post_name app_att (bname, thms) thy =
   322       let
   323         val name = Sign.full_name thy bname;
   324         val (thy', thms') = apsnd (post_name name) (app_att (thy, pre_name name thms));
   325         val r as ref {theorems = (space, theorems), index} = get_theorems_ref thy';
   326         val space' = Sign.declare_name thy' name space;
   327         val theorems' = Symtab.update (name, thms') theorems;
   328         val index' = FactIndex.add_global (name, thms') index;
   329       in
   330         (case Symtab.lookup theorems name of
   331           NONE => ()
   332         | SOME thms'' =>
   333             if Thm.eq_thms (thms', thms'') then warn_same name
   334             else warn_overwrite name);
   335         r := {theorems = (space', theorems'), index = index'};
   336         (thy', thms')
   337       end;
   338 
   339 
   340 (* add_thms(s) *)
   341 
   342 fun add_thms_atts pre_name ((bname, thms), atts) =
   343   enter_thms pre_name (name_thms false)
   344     (Thm.applys_attributes o rpair atts) (bname, thms);
   345 
   346 fun gen_add_thmss pre_name args theory =
   347   foldl_map (fn (thy, arg) => add_thms_atts pre_name arg thy) (theory, args);
   348 
   349 fun gen_add_thms pre_name args =
   350   apsnd (map hd) o gen_add_thmss pre_name (map (apfst (apsnd single)) args);
   351 
   352 val add_thmss = gen_add_thmss (name_thms true);
   353 val add_thms = gen_add_thms (name_thms true);
   354 
   355 
   356 (* note_thmss(_i) *)
   357 
   358 local
   359 
   360 fun gen_note_thss get kind_att (thy, ((bname, more_atts), ths_atts)) =
   361   let
   362     fun app (x, (ths, atts)) = Thm.applys_attributes ((x, ths), atts);
   363     val (thy', thms) = thy |> enter_thms
   364       name_thmss (name_thms false) (apsnd List.concat o foldl_map app)
   365       (bname, map (fn (ths, atts) => (get thy ths, atts @ more_atts @ [kind_att])) ths_atts);
   366   in (thy', (bname, thms)) end;
   367 
   368 fun gen_note_thmss get kind_att args thy =
   369   foldl_map (gen_note_thss get kind_att) (thy, args);
   370 
   371 in
   372 
   373 val note_thmss = gen_note_thmss get_thms;
   374 val note_thmss_i = gen_note_thmss (K I);
   375 
   376 end;
   377 
   378 
   379 (* store_thm *)
   380 
   381 fun store_thm ((bname, thm), atts) thy =
   382   let val (thy', [th']) = add_thms_atts (name_thms true) ((bname, [thm]), atts) thy
   383   in (thy', th') end;
   384 
   385 
   386 (* smart_store_thms(_open) *)
   387 
   388 local
   389 
   390 fun smart_store _ (name, []) =
   391       error ("Cannot store empty list of theorems: " ^ quote name)
   392   | smart_store name_thm (name, [thm]) =
   393       #2 (enter_thms (name_thm true) (name_thm false) I (name, [thm]) (Thm.theory_of_thm thm))
   394   | smart_store name_thm (name, thms) =
   395       let
   396         fun merge (thy, th) = Theory.merge (thy, Thm.theory_of_thm th);
   397         val thy = Library.foldl merge (Thm.theory_of_thm (hd thms), tl thms);
   398       in #2 (enter_thms (name_thm true) (name_thm false) I (name, thms) thy) end;
   399 
   400 in
   401 
   402 val smart_store_thms = smart_store name_thms;
   403 val smart_store_thms_open = smart_store (K (K I));
   404 
   405 end;
   406 
   407 
   408 (* forall_elim_var(s) -- belongs to drule.ML *)
   409 
   410 fun forall_elim_vars_aux strip_vars i th =
   411   let
   412     val {thy, tpairs, prop, ...} = Thm.rep_thm th;
   413     val add_used = Term.fold_aterms
   414       (fn Var ((x, j), _) => if i = j then curry (op ins_string) x else I | _ => I);
   415     val used = fold (fn (t, u) => add_used t o add_used u) tpairs (add_used prop []);
   416     val vars = strip_vars prop;
   417     val cvars = (Term.variantlist (map #1 vars, used), vars)
   418       |> ListPair.map (fn (x, (_, T)) => Thm.cterm_of thy (Var ((x, i), T)));
   419   in fold Thm.forall_elim cvars th end;
   420 
   421 val forall_elim_vars = forall_elim_vars_aux Term.strip_all_vars;
   422 
   423 fun forall_elim_var i th = forall_elim_vars_aux
   424   (fn Const ("all", _) $ Abs (a, T, _) => [(a, T)]
   425   | _ => raise THM ("forall_elim_vars", i, [th])) i th;
   426 
   427 
   428 (* store axioms as theorems *)
   429 
   430 local
   431   fun get_ax thy (name, _) = Thm.get_axiom_i thy (Sign.full_name thy name);
   432   fun get_axs thy named_axs = map (forall_elim_vars 0 o get_ax thy) named_axs;
   433 
   434   fun add_single add (thy, ((name, ax), atts)) =
   435     let
   436       val named_ax = [(name, ax)];
   437       val thy' = add named_ax thy;
   438       val thm = hd (get_axs thy' named_ax);
   439     in apsnd hd (gen_add_thms (K I) [((name, thm), atts)] thy') end;
   440 
   441   fun add_multi add (thy, ((name, axs), atts)) =
   442     let
   443       val named_axs = name_multi name axs;
   444       val thy' = add named_axs thy;
   445       val thms = get_axs thy' named_axs;
   446     in apsnd hd (gen_add_thmss (K I) [((name, thms), atts)] thy') end;
   447 
   448   fun add_singles add args thy = foldl_map (add_single add) (thy, args);
   449   fun add_multis add args thy = foldl_map (add_multi add) (thy, args);
   450 in
   451   val add_axioms    = add_singles Theory.add_axioms;
   452   val add_axioms_i  = add_singles Theory.add_axioms_i;
   453   val add_axiomss   = add_multis Theory.add_axioms;
   454   val add_axiomss_i = add_multis Theory.add_axioms_i;
   455   val add_defs      = add_singles o Theory.add_defs;
   456   val add_defs_i    = add_singles o Theory.add_defs_i;
   457   val add_defss     = add_multis o Theory.add_defs;
   458   val add_defss_i   = add_multis o Theory.add_defs_i;
   459 end;
   460 
   461 
   462 
   463 (*** ML setup ***)
   464 
   465 (* generic_setup *)
   466 
   467 val generic_setup =
   468   Context.use_let "val setup: (theory -> theory) list" "Library.apply setup";
   469 
   470 
   471 (* add_oracle *)
   472 
   473 fun add_oracle (name, T, oracle) =
   474   let val txt =
   475     "local\n\
   476     \  type T = " ^ T ^ ";\n\
   477     \  val oracle: theory -> T -> term = " ^ oracle ^ ";\n\
   478     \  val name = " ^ quote name ^ ";\n\
   479     \  exception Arg of T;\n\
   480     \  val _ = Context.>> (Theory.add_oracle (name, fn (thy, Arg x) => oracle thy x));\n\
   481     \  val thy = Context.the_context ();\n\
   482     \  val invoke_" ^ name ^ " = Thm.invoke_oracle_i thy (Sign.full_name thy name);\n\
   483     \in\n\
   484     \  fun " ^ name ^ " thy x = invoke_" ^ name ^ " (thy, Arg x);\n\
   485     \end;\n";
   486   in Context.use_mltext_theory txt false end;
   487 
   488 
   489 
   490 (*** the ProtoPure theory ***)
   491 
   492 val aT = TFree ("'a", []);
   493 val A = Free ("A", propT);
   494 
   495 val proto_pure =
   496   Context.pre_pure_thy
   497   |> Compress.init_data
   498   |> Sign.init_data
   499   |> Theory.init_data
   500   |> Proofterm.init_data
   501   |> TheoremsData.init
   502   |> Theory.add_types
   503    [("fun", 2, NoSyn),
   504     ("prop", 0, NoSyn),
   505     ("itself", 1, NoSyn),
   506     ("dummy", 0, NoSyn)]
   507   |> Theory.add_nonterminals Syntax.pure_nonterms
   508   |> Theory.add_syntax Syntax.pure_syntax
   509   |> Theory.add_syntax Syntax.pure_appl_syntax
   510   |> Theory.add_modesyntax (Symbol.xsymbolsN, true) Syntax.pure_xsym_syntax
   511   |> Theory.add_syntax
   512    [("==>", "prop => prop => prop", Delimfix "op ==>"),
   513     (Term.dummy_patternN, "aprop", Delimfix "'_")]
   514   |> Theory.add_consts
   515    [("==", "'a => 'a => prop", InfixrName ("==", 2)),
   516     ("==>", "prop => prop => prop", Mixfix ("(_/ ==> _)", [2, 1], 1)),
   517     ("all", "('a => prop) => prop", Binder ("!!", 0, 0)),
   518     ("prop", "prop => prop", NoSyn),
   519     ("TYPE", "'a itself", NoSyn),
   520     (Term.dummy_patternN, "'a", Delimfix "'_")]
   521   |> Theory.add_finals_i false
   522     [Const ("==", [aT, aT] ---> propT),
   523      Const ("==>", [propT, propT] ---> propT),
   524      Const ("all", (aT --> propT) --> propT),
   525      Const ("TYPE", a_itselfT),
   526      Const (Term.dummy_patternN, aT)]
   527   |> Theory.add_modesyntax ("", false)
   528     (Syntax.pure_syntax_output @ Syntax.pure_appl_syntax)
   529   |> Theory.add_trfuns Syntax.pure_trfuns
   530   |> Theory.add_trfunsT Syntax.pure_trfunsT
   531   |> Sign.local_path
   532   |> (#1 oo (add_defs_i false o map Thm.no_attributes))
   533    [("prop_def", Logic.mk_equals (Logic.protect A, A))]
   534   |> (#1 o add_thmss [(("nothing", []), [])])
   535   |> Theory.add_axioms_i Proofterm.equality_axms
   536   |> Theory.end_theory;
   537 
   538 structure ProtoPure =
   539 struct
   540   val thy = proto_pure;
   541   val prop_def = get_axiom thy "prop_def";
   542 end;
   543 
   544 end;
   545 
   546 structure BasicPureThy: BASIC_PURE_THY = PureThy;
   547 open BasicPureThy;