src/Pure/Thy/thy_read.ML
author clasohm
Thu, 22 Jun 1995 12:58:39 +0200
changeset 1154 bc295e3dc078
parent 1132 dfb29abcf3c2
child 1157 da78c293e8dc
permissions -rw-r--r--
changed call of store_thm_db so that it's result is not displayed by PolyML or SML
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Thy/thy_read.ML
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
     3
    Author:     Carsten Clasohm and Markus Wenzel and Sonia Mahjoub and
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
     4
                Tobias Nipkow and L C Paulson
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
     5
    Copyright   1994 TU Muenchen
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
     6
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
     7
(* FIXME !? *)
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
     8
Reading and writing the theory definition files.
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
     9
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
    10
(* FIXME !? *)
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    11
For theory XXX, the  input file is called XXX.thy
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    12
                the output file is called .XXX.thy.ML
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    13
                and it then tries to read XXX.ML
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    14
*)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    15
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
    16
datatype thy_info = ThyInfo of {path: string,
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    17
                                children: string list,
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
    18
                                thy_time: string option,
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
    19
                                ml_time: string option,
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
    20
                                theory: theory option,
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
    21
                                thms: thm Symtab.table};
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    22
412
216624270b80 restored old functor name;
wenzelm
parents: 397
diff changeset
    23
signature READTHY =
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    24
sig
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    25
  datatype basetype = Thy  of string
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    26
                    | File of string
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    27
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
    28
  val loaded_thys    : thy_info Symtab.table ref
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    29
  val loadpath       : string list ref
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    30
  val delete_tmpfiles: bool ref
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    31
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    32
  val use_thy        : string -> unit
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    33
  val update         : unit -> unit
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    34
  val time_use_thy   : string -> unit
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    35
  val unlink_thy     : string -> unit
586
201e115d8031 renamed base_on into mk_base and moved it to the beginning of the generated
clasohm
parents: 559
diff changeset
    36
  val mk_base        : basetype list -> string -> bool -> theory
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
    37
  val store_theory   : theory * string -> unit
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
    38
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
    39
  val theory_of_sign: Sign.sg -> theory
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
    40
  val theory_of_thm: thm -> theory
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
    41
  val store_thm: string * thm -> thm
758
c2b210bda710 added bind_thm
clasohm
parents: 746
diff changeset
    42
  val bind_thm: string * thm -> unit
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
    43
  val qed: string -> unit
758
c2b210bda710 added bind_thm
clasohm
parents: 746
diff changeset
    44
  val qed_thm: thm ref
746
6e815617d79f added qed_goal[w]
clasohm
parents: 715
diff changeset
    45
  val qed_goal: string -> theory -> string -> (thm list -> tactic list) -> unit
6e815617d79f added qed_goal[w]
clasohm
parents: 715
diff changeset
    46
  val qed_goalw: string -> theory->thm list->string->(thm list->tactic list)
6e815617d79f added qed_goal[w]
clasohm
parents: 715
diff changeset
    47
                 -> unit
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
    48
  val get_thm: theory -> string -> thm
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
    49
  val thms_of: theory -> (string * thm) list
778
9a03ed31ea2f added print_theory that prints stored thms;
wenzelm
parents: 774
diff changeset
    50
  val print_theory: theory -> unit
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    51
end;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    52
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    53
1132
dfb29abcf3c2 added theorem database which contains axioms and theorems indexed by the
clasohm
parents: 1098
diff changeset
    54
functor ReadthyFUN(structure ThySyn: THY_SYN and ThmDB: THMDB): READTHY =
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    55
struct
1132
dfb29abcf3c2 added theorem database which contains axioms and theorems indexed by the
clasohm
parents: 1098
diff changeset
    56
local open ThmDB in
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    57
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    58
datatype basetype = Thy  of string
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    59
                  | File of string;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    60
922
196ca0973a6d added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents: 871
diff changeset
    61
val loaded_thys = ref (Symtab.make [("ProtoPure",
196ca0973a6d added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents: 871
diff changeset
    62
                                     ThyInfo {path = "",
196ca0973a6d added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents: 871
diff changeset
    63
                                       children = ["Pure", "CPure"],
196ca0973a6d added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents: 871
diff changeset
    64
                                       thy_time = Some "", ml_time = Some "",
196ca0973a6d added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents: 871
diff changeset
    65
                                       theory = Some proto_pure_thy,
196ca0973a6d added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents: 871
diff changeset
    66
                                       thms = Symtab.null}),
196ca0973a6d added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents: 871
diff changeset
    67
                                    ("Pure", ThyInfo {path = "", children = [],
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
    68
                                       thy_time = Some "", ml_time = Some "",
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
    69
                                       theory = Some pure_thy,
922
196ca0973a6d added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents: 871
diff changeset
    70
                                       thms = Symtab.null}),
196ca0973a6d added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents: 871
diff changeset
    71
                                    ("CPure", ThyInfo {path = "",
196ca0973a6d added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents: 871
diff changeset
    72
                                       children = [],
196ca0973a6d added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents: 871
diff changeset
    73
                                       thy_time = Some "", ml_time = Some "",
196ca0973a6d added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents: 871
diff changeset
    74
                                       theory = Some cpure_thy,
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
    75
                                       thms = Symtab.null})]);
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    76
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    77
val loadpath = ref ["."];           (*default search path for theory files *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    78
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    79
val delete_tmpfiles = ref true;         (*remove temporary files after use *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    80
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    81
(*Make name of the output ML file for a theory *)
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
    82
fun out_name tname = "." ^ tname ^ ".thy.ML";
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    83
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    84
(*Read a file specified by thy_file containing theory thy *)
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
    85
fun read_thy tname thy_file =
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
    86
  let
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    87
    val instream  = open_in thy_file;
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
    88
    val outstream = open_out (out_name tname);
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
    89
  in
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
    90
    output (outstream, ThySyn.parse tname (input (instream, 999999)));
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    91
    close_out outstream;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    92
    close_in instream
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    93
  end;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    94
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    95
fun file_exists file =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    96
  let val instream = open_in file in close_in instream; true end
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    97
    handle Io _ => false;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    98
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    99
(*Get thy_info for a loaded theory *)
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   100
fun get_thyinfo tname = Symtab.lookup (!loaded_thys, tname);
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   101
971
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   102
(*Check if a theory was completly loaded *)
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   103
fun already_loaded thy =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   104
  let val t = get_thyinfo thy
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   105
  in if is_none t then false
971
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   106
     else let val ThyInfo {thy_time, ml_time, ...} = the t
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   107
          in is_some thy_time andalso is_some ml_time end
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   108
  end;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   109
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   110
(*Check if a theory file has changed since its last use.
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   111
  Return a pair of boolean values for .thy and for .ML *)
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   112
fun thy_unchanged thy thy_file ml_file =
1098
487089cb173e fixed bug in thy_unchanged that occurred when the .thy file was changed
clasohm
parents: 971
diff changeset
   113
  case get_thyinfo thy of
487089cb173e fixed bug in thy_unchanged that occurred when the .thy file was changed
clasohm
parents: 971
diff changeset
   114
      Some (ThyInfo {thy_time, ml_time, ...}) =>
487089cb173e fixed bug in thy_unchanged that occurred when the .thy file was changed
clasohm
parents: 971
diff changeset
   115
       let val tn = is_none thy_time;
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   116
           val mn = is_none ml_time
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   117
       in if not tn andalso not mn then
1098
487089cb173e fixed bug in thy_unchanged that occurred when the .thy file was changed
clasohm
parents: 971
diff changeset
   118
            ((file_info thy_file = the thy_time),
487089cb173e fixed bug in thy_unchanged that occurred when the .thy file was changed
clasohm
parents: 971
diff changeset
   119
             (file_info ml_file = the ml_time))
487089cb173e fixed bug in thy_unchanged that occurred when the .thy file was changed
clasohm
parents: 971
diff changeset
   120
          else if not tn andalso mn then
487089cb173e fixed bug in thy_unchanged that occurred when the .thy file was changed
clasohm
parents: 971
diff changeset
   121
            (file_info thy_file = the thy_time, false)
487089cb173e fixed bug in thy_unchanged that occurred when the .thy file was changed
clasohm
parents: 971
diff changeset
   122
          else
487089cb173e fixed bug in thy_unchanged that occurred when the .thy file was changed
clasohm
parents: 971
diff changeset
   123
            (false, false)
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   124
       end
1098
487089cb173e fixed bug in thy_unchanged that occurred when the .thy file was changed
clasohm
parents: 971
diff changeset
   125
    | None => (false, false)
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   126
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   127
exception FILE_NOT_FOUND;   (*raised by find_file *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   128
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   129
(*Find a file using a list of paths if no absolute or relative path is
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   130
  specified.*)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   131
fun find_file "" name =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   132
      let fun find_it (curr :: paths) =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   133
                if file_exists (tack_on curr name) then
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   134
                    tack_on curr name
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   135
                else
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   136
                    find_it paths
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   137
           | find_it [] = ""
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   138
      in find_it (!loadpath) end
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   139
  | find_file path name =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   140
      if file_exists (tack_on path name) then tack_on path name
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   141
                                         else "";
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   142
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   143
(*Get absolute pathnames for a new or already loaded theory *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   144
fun get_filenames path name =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   145
  let fun make_absolute file =
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   146
        if file = "" then "" else
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   147
            if hd (explode file) = "/" then file else tack_on (pwd ()) file;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   148
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   149
      fun new_filename () =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   150
        let val found = find_file path (name ^ ".thy")
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   151
                        handle FILE_NOT_FOUND => "";
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   152
            val thy_file = make_absolute found;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   153
            val (thy_path, _) = split_filename thy_file;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   154
            val found = find_file path (name ^ ".ML");
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   155
            val ml_file = if thy_file = "" then make_absolute found
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   156
                          else if file_exists (tack_on thy_path (name ^ ".ML"))
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   157
                          then tack_on thy_path (name ^ ".ML")
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   158
                          else "";
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   159
            val searched_dirs = if path = "" then (!loadpath) else [path]
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   160
        in if thy_file = "" andalso ml_file = "" then
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   161
             error ("Could not find file \"" ^ name ^ ".thy\" or \""
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   162
                    ^ name ^ ".ML\" for theory \"" ^ name ^ "\"\n"
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   163
                    ^ "in the following directories: \"" ^
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   164
                    (space_implode "\", \"" searched_dirs) ^ "\"")
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   165
           else ();
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   166
           (thy_file, ml_file)
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   167
        end;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   168
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   169
      val tinfo = get_thyinfo name;
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   170
  in if is_some tinfo andalso path = "" then
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   171
       let val ThyInfo {path = abs_path, ...} = the tinfo;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   172
           val (thy_file, ml_file) = if abs_path = "" then new_filename ()
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   173
                                     else (find_file abs_path (name ^ ".thy"),
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   174
                                           find_file abs_path (name ^ ".ML"))
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   175
       in if thy_file = "" andalso ml_file = "" then
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   176
            (writeln ("Warning: File \"" ^ (tack_on path name)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   177
                      ^ ".thy\"\ncontaining theory \"" ^ name
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   178
                      ^ "\" no longer exists.");
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   179
             new_filename ()
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   180
            )
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   181
          else (thy_file, ml_file)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   182
       end
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   183
     else new_filename ()
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   184
  end;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   185
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   186
(*Remove theory from all child lists in loaded_thys *)
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   187
fun unlink_thy tname =
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   188
  let fun remove (ThyInfo {path, children, thy_time, ml_time, theory, thms}) =
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   189
        ThyInfo {path = path, children = children \ tname,
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   190
                 thy_time = thy_time, ml_time = ml_time,
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   191
                 theory = theory, thms = thms}
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   192
  in loaded_thys := Symtab.map remove (!loaded_thys) end;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   193
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   194
(*Remove a theory from loaded_thys *)
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   195
fun remove_thy tname =
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   196
  loaded_thys := Symtab.make (filter_out (fn (id, _) => id = tname)
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   197
                 (Symtab.dest (!loaded_thys)));
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   198
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   199
(*Change thy_time and ml_time for an existent item *)
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   200
fun set_info thy_time ml_time tname =
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   201
  let val ThyInfo {path, children, theory, thms, ...} =
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   202
        the (Symtab.lookup (!loaded_thys, tname));
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   203
  in loaded_thys := Symtab.update ((tname,
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   204
       ThyInfo {path = path, children = children,
971
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   205
                thy_time = thy_time, ml_time = ml_time,
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   206
                theory = theory, thms = thms}), !loaded_thys)
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   207
  end;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   208
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   209
(*Mark theory as changed since last read if it has been completly read *)
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   210
fun mark_outdated tname =
971
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   211
  let val t = get_thyinfo tname;
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   212
  in if is_none t then ()
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   213
     else let val ThyInfo {thy_time, ml_time, ...} = the t
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   214
          in set_info (if is_none thy_time then None else Some "")
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   215
                      (if is_none ml_time then None else Some "")
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   216
                      tname
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   217
          end
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   218
  end;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   219
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   220
(*Read .thy and .ML files that haven't been read yet or have changed since
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   221
  they were last read;
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   222
  loaded_thys is a thy_info list ref containing all theories that have
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   223
  completly been read by this and preceeding use_thy calls.
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   224
  If a theory changed since its last use its children are marked as changed *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   225
fun use_thy name =
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   226
    let val (path, tname) = split_filename name;
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   227
        val (thy_file, ml_file) = get_filenames path tname;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   228
        val (abs_path, _) = if thy_file = "" then split_filename ml_file
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   229
                            else split_filename thy_file;
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   230
        val (thy_uptodate, ml_uptodate) = thy_unchanged tname
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   231
                                                        thy_file ml_file;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   232
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   233
         (*Set absolute path for loaded theory *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   234
         fun set_path () =
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   235
           let val ThyInfo {children, thy_time, ml_time, theory, thms, ...} =
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   236
                 the (Symtab.lookup (!loaded_thys, tname));
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   237
           in loaded_thys := Symtab.update ((tname,
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   238
                               ThyInfo {path = abs_path, children = children,
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   239
                                        thy_time = thy_time, ml_time = ml_time,
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   240
                                        theory = theory, thms = thms}),
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   241
                               !loaded_thys)
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   242
           end;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   243
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   244
         (*Mark all direct descendants of a theory as changed *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   245
         fun mark_children thy =
971
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   246
           let val ThyInfo {children, ...} = the (get_thyinfo thy);
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   247
               val present = filter (is_some o get_thyinfo) children;
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   248
               val loaded = filter already_loaded present;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   249
           in if loaded <> [] then
971
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   250
                writeln ("The following children of theory " ^ (quote thy)
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   251
                         ^ " are now out-of-date: "
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   252
                         ^ (quote (space_implode "\",\"" loaded)))
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   253
              else ();
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   254
              seq mark_outdated present
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   255
           end;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   256
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   257
        (*Remove all theorems associated with a theory*)
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   258
        fun delete_thms () =
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   259
          let val tinfo = case Symtab.lookup (!loaded_thys, tname) of
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   260
             Some (ThyInfo {path, children, thy_time, ml_time, theory, ...}) =>
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   261
                 ThyInfo {path = path, children = children,
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   262
                          thy_time = thy_time, ml_time = ml_time,
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   263
                          theory = theory, thms = Symtab.null}
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   264
           | None => ThyInfo {path = "", children = [],
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   265
                              thy_time = None, ml_time = None,
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   266
                              theory = None, thms = Symtab.null};
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   267
         in loaded_thys := Symtab.update ((tname, tinfo), !loaded_thys) end;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   268
    in if thy_uptodate andalso ml_uptodate then ()
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   269
       else
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   270
       (
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   271
         delete_thms ();
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   272
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   273
         if thy_uptodate orelse thy_file = "" then ()
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   274
         else (writeln ("Reading \"" ^ name ^ ".thy\"");
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   275
               read_thy tname thy_file;
1098
487089cb173e fixed bug in thy_unchanged that occurred when the .thy file was changed
clasohm
parents: 971
diff changeset
   276
               use (out_name tname);
487089cb173e fixed bug in thy_unchanged that occurred when the .thy file was changed
clasohm
parents: 971
diff changeset
   277
487089cb173e fixed bug in thy_unchanged that occurred when the .thy file was changed
clasohm
parents: 971
diff changeset
   278
               if not (!delete_tmpfiles) then ()
487089cb173e fixed bug in thy_unchanged that occurred when the .thy file was changed
clasohm
parents: 971
diff changeset
   279
               else delete_file (out_name tname)
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   280
              );
971
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   281
         set_info (Some (file_info thy_file)) None tname;
783
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   282
                                       (*mark thy_file as successfully loaded*)
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   283
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   284
         if ml_file = "" then ()
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   285
         else (writeln ("Reading \"" ^ name ^ ".ML\"");
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   286
               use ml_file);
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   287
1154
bc295e3dc078 changed call of store_thm_db so that it's result is not displayed
clasohm
parents: 1132
diff changeset
   288
         use_string
bc295e3dc078 changed call of store_thm_db so that it's result is not displayed
clasohm
parents: 1132
diff changeset
   289
           ["val _ = (store_theory (" ^ tname ^ ".thy, " ^ quote tname ^ ");\
bc295e3dc078 changed call of store_thm_db so that it's result is not displayed
clasohm
parents: 1132
diff changeset
   290
            \map store_thm_db (axioms_of " ^ tname ^ ".thy));"];
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   291
                    (*Store theory again because it could have been redefined*)
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   292
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   293
         (*Now set the correct info*)
971
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   294
         set_info (Some (file_info thy_file)) (Some (file_info ml_file)) tname;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   295
         set_path ();
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   296
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   297
         (*Mark theories that have to be reloaded*)
1098
487089cb173e fixed bug in thy_unchanged that occurred when the .thy file was changed
clasohm
parents: 971
diff changeset
   298
         mark_children tname
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   299
        )
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   300
    end;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   301
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   302
fun time_use_thy tname = timeit(fn()=>
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   303
   (writeln("\n**** Starting Theory " ^ tname ^ " ****");
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   304
    use_thy tname;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   305
    writeln("\n**** Finished Theory " ^ tname ^ " ****"))
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   306
   );
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   307
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   308
(*Load all thy or ML files that have been changed and also
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   309
  all theories that depend on them *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   310
fun update () =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   311
  let (*List theories in the order they have to be loaded *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   312
      fun load_order [] result = result
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   313
        | load_order thys result =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   314
            let fun next_level (t :: ts) =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   315
                      let val thy = get_thyinfo t
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   316
                      in if is_some thy then
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   317
                             let val ThyInfo {children, ...} = the thy
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   318
                             in children union (next_level ts) end
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   319
                         else next_level ts
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   320
                      end
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   321
                  | next_level [] = [];
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   322
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   323
                val children = next_level thys;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   324
            in load_order children ((result \\ children) @ children) end;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   325
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   326
      fun reload_changed (t :: ts) =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   327
            let val thy = get_thyinfo t;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   328
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   329
                fun abspath () =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   330
                  if is_some thy then
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   331
                    let val ThyInfo {path, ...} = the thy in path end
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   332
                  else "";
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   333
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   334
                val (thy_file, ml_file) = get_filenames (abspath ()) t;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   335
                val (thy_uptodate, ml_uptodate) =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   336
                        thy_unchanged t thy_file ml_file;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   337
            in if thy_uptodate andalso ml_uptodate then ()
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   338
                                                   else use_thy t;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   339
               reload_changed ts
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   340
            end
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   341
        | reload_changed [] = ();
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   342
922
196ca0973a6d added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents: 871
diff changeset
   343
     (*Remove all theories that are no descendants of ProtoPure.
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   344
       If there are still children in the deleted theory's list
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   345
       schedule them for reloading *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   346
     fun collect_garbage not_garbage =
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   347
       let fun collect ((tname, ThyInfo {children, ...}) :: ts) =
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   348
                 if tname mem not_garbage then collect ts
922
196ca0973a6d added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents: 871
diff changeset
   349
                 else (writeln ("Theory \"" ^ tname ^
196ca0973a6d added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents: 871
diff changeset
   350
                       "\" is no longer linked with ProtoPure - removing it.");
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   351
                       remove_thy tname;
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   352
                       seq mark_outdated children)
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   353
             | collect [] = ()
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   354
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   355
       in collect (Symtab.dest (!loaded_thys)) end;
922
196ca0973a6d added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents: 871
diff changeset
   356
  in collect_garbage ("ProtoPure" :: (load_order ["ProtoPure"] []));
196ca0973a6d added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents: 871
diff changeset
   357
     reload_changed (load_order ["Pure", "CPure"] [])
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   358
  end;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   359
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   360
(*Merge theories to build a base for a new theory.
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   361
  Base members are only loaded if they are missing. *)
586
201e115d8031 renamed base_on into mk_base and moved it to the beginning of the generated
clasohm
parents: 559
diff changeset
   362
fun mk_base bases child mk_draft =
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   363
      let (*List all descendants of a theory list *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   364
          fun list_descendants (t :: ts) =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   365
                let val tinfo = get_thyinfo t
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   366
                in if is_some tinfo then
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   367
                     let val ThyInfo {children, ...} = the tinfo
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   368
                     in children union (list_descendants (ts union children))
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   369
                     end
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   370
                   else []
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   371
                end
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   372
            | list_descendants [] = [];
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   373
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   374
          (*Show the cycle that would be created by add_child *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   375
          fun show_cycle base =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   376
            let fun find_it result curr =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   377
                  let val tinfo = get_thyinfo curr
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   378
                  in if base = curr then
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   379
                       error ("Cyclic dependency of theories: "
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   380
                              ^ child ^ "->" ^ base ^ result)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   381
                     else if is_some tinfo then
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   382
                       let val ThyInfo {children, ...} = the tinfo
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   383
                       in seq (find_it ("->" ^ curr ^ result)) children
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   384
                       end
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   385
                     else ()
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   386
                  end
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   387
            in find_it "" child end;
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   388
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   389
          (*Check if a cycle would be created by add_child *)
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   390
          fun find_cycle base =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   391
            if base mem (list_descendants [child]) then show_cycle base
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   392
            else ();
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   393
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   394
          (*Add child to child list of base *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   395
          fun add_child base =
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   396
            let val tinfo =
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   397
                  case Symtab.lookup (!loaded_thys, base) of
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   398
                      Some (ThyInfo {path, children, thy_time, ml_time,
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   399
                                     theory, thms}) =>
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   400
                        ThyInfo {path = path, children = child ins children,
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   401
                                 thy_time = thy_time, ml_time = ml_time,
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   402
                                 theory = theory, thms = thms}
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   403
                    | None => ThyInfo {path = "", children = [child],
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   404
                                       thy_time = None, ml_time = None,
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   405
                                       theory = None, thms = Symtab.null};
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   406
            in loaded_thys := Symtab.update ((base, tinfo), !loaded_thys) end;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   407
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   408
          (*Load a base theory if not already done
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   409
            and no cycle would be created *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   410
          fun load base =
971
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   411
              let val thy_loaded = already_loaded base
426
767367131b47 replaced "foldl merge_theories" by "merge_thy_list" in base_on
clasohm
parents: 424
diff changeset
   412
                                           (*test this before child is added *)
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   413
              in
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   414
                if child = base then
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   415
                    error ("Cyclic dependency of theories: " ^ child
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   416
                           ^ "->" ^ child)
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   417
                else
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   418
                  (find_cycle base;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   419
                   add_child base;
971
f4815812665b fixed bug: parent theory wasn't loaded if .thy file was completly read before
clasohm
parents: 922
diff changeset
   420
                   if thy_loaded then ()
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   421
                   else (writeln ("Autoloading theory " ^ (quote base)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   422
                                  ^ " (used by " ^ (quote child) ^ ")");
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   423
                         use_thy base)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   424
                  )
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   425
              end;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   426
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   427
          (*Load all needed files and make a list of all real theories *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   428
          fun load_base (Thy b :: bs) =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   429
               (load b;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   430
                b :: (load_base bs))
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   431
            | load_base (File b :: bs) =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   432
               (load b;
922
196ca0973a6d added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents: 871
diff changeset
   433
                load_base bs)                    (*don't add it to mergelist *)
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   434
            | load_base [] = [];
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   435
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   436
          (*Get theory object for a loaded theory *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   437
          fun get_theory name =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   438
            let val ThyInfo {theory, ...} = the (get_thyinfo name)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   439
            in the theory end;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   440
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   441
          val mergelist = (unlink_thy child;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   442
                           load_base bases);
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   443
     in writeln ("Loading theory " ^ (quote child));
426
767367131b47 replaced "foldl merge_theories" by "merge_thy_list" in base_on
clasohm
parents: 424
diff changeset
   444
        merge_thy_list mk_draft (map get_theory mergelist) end;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   445
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   446
(*Change theory object for an existent item of loaded_thys
1132
dfb29abcf3c2 added theorem database which contains axioms and theorems indexed by the
clasohm
parents: 1098
diff changeset
   447
  or create a new item; also store axioms in Thm database*)
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   448
fun store_theory (thy, tname) =
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   449
  let val tinfo = case Symtab.lookup (!loaded_thys, tname) of
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   450
               Some (ThyInfo {path, children, thy_time, ml_time, thms, ...}) =>
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   451
                 ThyInfo {path = path, children = children,
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   452
                          thy_time = thy_time, ml_time = ml_time,
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   453
                          theory = Some thy, thms = thms}
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   454
             | None => ThyInfo {path = "", children = [],
922
196ca0973a6d added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents: 871
diff changeset
   455
                                thy_time = None, ml_time = None,
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   456
                                theory = Some thy, thms = Symtab.null};
1132
dfb29abcf3c2 added theorem database which contains axioms and theorems indexed by the
clasohm
parents: 1098
diff changeset
   457
  in loaded_thys := Symtab.update ((tname, tinfo), !loaded_thys)
dfb29abcf3c2 added theorem database which contains axioms and theorems indexed by the
clasohm
parents: 1098
diff changeset
   458
  end;
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   459
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   460
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   461
(** store and retrieve theorems **)
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   462
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   463
(*Guess to which theory a signature belongs and return it's thy_info*)
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   464
fun thyinfo_of_sign sg =
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   465
  let
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   466
    val ref xname = hd (#stamps (Sign.rep_sg sg));
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   467
    val opt_info = get_thyinfo xname;
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   468
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   469
    fun eq_sg (ThyInfo {theory = None, ...}) = false
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   470
      | eq_sg (ThyInfo {theory = Some thy, ...}) = Sign.eq_sg (sg,sign_of thy);
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   471
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   472
    val show_sg = Pretty.str_of o Sign.pretty_sg;
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   473
  in
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   474
    if is_some opt_info andalso eq_sg (the opt_info) then
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   475
      (xname, the opt_info)
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   476
    else
783
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   477
      (case Symtab.find_first (eq_sg o snd) (!loaded_thys) of
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   478
        Some name_info => name_info
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   479
      | None => error ("Theory " ^ show_sg sg ^ " not stored by loader"))
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   480
  end;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   481
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   482
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   483
(*Try to get the theory object corresponding to a given signature*)
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   484
fun theory_of_sign sg =
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   485
  (case thyinfo_of_sign sg of
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   486
    (_, ThyInfo {theory = Some thy, ...}) => thy
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   487
  | _ => sys_error "theory_of_sign");
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   488
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   489
(*Try to get the theory object corresponding to a given theorem*)
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   490
val theory_of_thm = theory_of_sign o #sign o rep_thm;
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   491
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   492
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   493
(* Store theorems *)
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   494
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   495
(*Store a theorem in the thy_info of its theory*)
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   496
fun store_thm (name, thm) =
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   497
  let
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   498
    val (thy_name, ThyInfo {path, children, thy_time, ml_time, theory, thms}) =
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   499
      thyinfo_of_sign (#sign (rep_thm thm));
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   500
    val thms' = Symtab.update_new ((name, thm), thms)
774
ea19f22ed23c added warning for already stored theorem to store_thm
clasohm
parents: 759
diff changeset
   501
      handle Symtab.DUPLICATE s => 
ea19f22ed23c added warning for already stored theorem to store_thm
clasohm
parents: 759
diff changeset
   502
        (if eq_thm (the (Symtab.lookup (thms, name)), thm) then 
ea19f22ed23c added warning for already stored theorem to store_thm
clasohm
parents: 759
diff changeset
   503
           (writeln ("Warning: Theorem database already contains copy of\
ea19f22ed23c added warning for already stored theorem to store_thm
clasohm
parents: 759
diff changeset
   504
                     \ theorem " ^ quote name);
ea19f22ed23c added warning for already stored theorem to store_thm
clasohm
parents: 759
diff changeset
   505
            thms)
ea19f22ed23c added warning for already stored theorem to store_thm
clasohm
parents: 759
diff changeset
   506
         else error ("Duplicate theorem name " ^ quote s));
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   507
  in
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   508
    loaded_thys := Symtab.update
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   509
     ((thy_name, ThyInfo {path = path, children = children,
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   510
       thy_time = thy_time, ml_time = ml_time, theory = theory, thms = thms'}),
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   511
      ! loaded_thys);
1132
dfb29abcf3c2 added theorem database which contains axioms and theorems indexed by the
clasohm
parents: 1098
diff changeset
   512
    store_thm_db (name, thm)
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   513
  end;
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   514
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   515
(*Store result of proof in loaded_thys and as ML value*)
758
c2b210bda710 added bind_thm
clasohm
parents: 746
diff changeset
   516
c2b210bda710 added bind_thm
clasohm
parents: 746
diff changeset
   517
val qed_thm = ref flexpair_def(*dummy*);
c2b210bda710 added bind_thm
clasohm
parents: 746
diff changeset
   518
c2b210bda710 added bind_thm
clasohm
parents: 746
diff changeset
   519
fun bind_thm (name, thm) =
c2b210bda710 added bind_thm
clasohm
parents: 746
diff changeset
   520
  (qed_thm := thm;
c2b210bda710 added bind_thm
clasohm
parents: 746
diff changeset
   521
   use_string ["val " ^ name ^ " = standard (store_thm (" ^ quote name ^
c2b210bda710 added bind_thm
clasohm
parents: 746
diff changeset
   522
               ", !qed_thm));"]);
c2b210bda710 added bind_thm
clasohm
parents: 746
diff changeset
   523
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   524
fun qed name =
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   525
  use_string ["val " ^ name ^ " = store_thm (" ^ quote name ^ ", result ());"];
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   526
746
6e815617d79f added qed_goal[w]
clasohm
parents: 715
diff changeset
   527
fun qed_goal name thy agoal tacsf =
758
c2b210bda710 added bind_thm
clasohm
parents: 746
diff changeset
   528
  (qed_thm := prove_goal thy agoal tacsf;
c2b210bda710 added bind_thm
clasohm
parents: 746
diff changeset
   529
   use_string ["val " ^ name ^ " = store_thm (" ^ quote name ^", !qed_thm);"]);
746
6e815617d79f added qed_goal[w]
clasohm
parents: 715
diff changeset
   530
6e815617d79f added qed_goal[w]
clasohm
parents: 715
diff changeset
   531
fun qed_goalw name thy rths agoal tacsf =
758
c2b210bda710 added bind_thm
clasohm
parents: 746
diff changeset
   532
  (qed_thm := prove_goalw thy rths agoal tacsf;
c2b210bda710 added bind_thm
clasohm
parents: 746
diff changeset
   533
   use_string ["val " ^ name ^ " = store_thm (" ^ quote name ^", !qed_thm);"]);
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   534
783
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   535
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   536
(* Retrieve theorems *)
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   537
783
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   538
(*Get all direct ancestors of a theory*)
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   539
fun get_parents child =
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   540
  let fun has_child (tname, ThyInfo {children, theory, ...}) = 
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   541
        if child mem children then Some tname else None;
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   542
  in mapfilter has_child (Symtab.dest (!loaded_thys)) end;
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   543
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   544
(*Get all theorems belonging to a given theory*)
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   545
fun thmtab_ofthy thy =
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   546
  let val (_, ThyInfo {thms, ...}) = thyinfo_of_sign (sign_of thy);
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   547
  in thms end;
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   548
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   549
fun thmtab_ofname name =
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   550
  let val ThyInfo {thms, ...} = the (get_thyinfo name);
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   551
  in thms end;
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   552
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   553
(*Get a stored theorem specified by theory and name*)
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   554
fun get_thm thy name =
783
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   555
  let fun get [] [] searched =
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   556
           raise THEORY ("get_thm: no theorem " ^ quote name, [thy])
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   557
        | get [] ng searched =
871
1c060d444a81 simplified get_thm a bit
clasohm
parents: 783
diff changeset
   558
            get (ng \\ searched) [] searched
783
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   559
        | get (t::ts) ng searched =
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   560
            (case Symtab.lookup (thmtab_ofname t, name) of
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   561
                 Some thm => thm
871
1c060d444a81 simplified get_thm a bit
clasohm
parents: 783
diff changeset
   562
               | None => get ts (ng union (get_parents t)) (t::searched));
783
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   563
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   564
      val (tname, _) = thyinfo_of_sign (sign_of thy);
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   565
  in get [tname] [] [] end;
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   566
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   567
(*Get stored theorems of a theory*)
783
08f1785a4384 changed get_thm to search all parent theories if the theorem is not found
clasohm
parents: 778
diff changeset
   568
val thms_of = Symtab.dest o thmtab_ofthy;
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   569
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   570
778
9a03ed31ea2f added print_theory that prints stored thms;
wenzelm
parents: 774
diff changeset
   571
(* print theory *)
9a03ed31ea2f added print_theory that prints stored thms;
wenzelm
parents: 774
diff changeset
   572
9a03ed31ea2f added print_theory that prints stored thms;
wenzelm
parents: 774
diff changeset
   573
fun print_thms thy =
9a03ed31ea2f added print_theory that prints stored thms;
wenzelm
parents: 774
diff changeset
   574
  let
9a03ed31ea2f added print_theory that prints stored thms;
wenzelm
parents: 774
diff changeset
   575
    val thms = thms_of thy;
9a03ed31ea2f added print_theory that prints stored thms;
wenzelm
parents: 774
diff changeset
   576
    fun prt_thm (s, th) = Pretty.block [Pretty.str (s ^ ":"), Pretty.brk 1,
9a03ed31ea2f added print_theory that prints stored thms;
wenzelm
parents: 774
diff changeset
   577
      Pretty.quote (pretty_thm th)];
9a03ed31ea2f added print_theory that prints stored thms;
wenzelm
parents: 774
diff changeset
   578
  in
9a03ed31ea2f added print_theory that prints stored thms;
wenzelm
parents: 774
diff changeset
   579
    Pretty.writeln (Pretty.big_list "stored theorems:" (map prt_thm thms))
9a03ed31ea2f added print_theory that prints stored thms;
wenzelm
parents: 774
diff changeset
   580
  end;
9a03ed31ea2f added print_theory that prints stored thms;
wenzelm
parents: 774
diff changeset
   581
9a03ed31ea2f added print_theory that prints stored thms;
wenzelm
parents: 774
diff changeset
   582
fun print_theory thy = (Drule.print_theory thy; print_thms thy);
9a03ed31ea2f added print_theory that prints stored thms;
wenzelm
parents: 774
diff changeset
   583
1132
dfb29abcf3c2 added theorem database which contains axioms and theorems indexed by the
clasohm
parents: 1098
diff changeset
   584
end
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   585
end;