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