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