src/Pure/Thy/thy_read.ML
author clasohm
Fri, 18 Nov 1994 13:08:10 +0100
changeset 715 f76ad10f5802
parent 586 201e115d8031
child 746 6e815617d79f
permissions -rw-r--r--
added call of store_theory after thy file has been read
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
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
    42
  val qed: string -> unit
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
    43
  val get_thm: theory -> string -> thm
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
    44
  val thms_of: theory -> (string * thm) list
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    45
end;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    46
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    47
412
216624270b80 restored old functor name;
wenzelm
parents: 397
diff changeset
    48
functor ReadthyFUN(structure ThySyn: THY_SYN): READTHY =
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    49
struct
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    50
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    51
datatype basetype = Thy  of string
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    52
                  | File of string;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    53
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
    54
val loaded_thys = ref (Symtab.make [("Pure", ThyInfo {path = "", children = [],
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
    55
                                       thy_time = Some "", ml_time = Some "",
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
    56
                                       theory = Some pure_thy,
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
    57
                                       thms = Symtab.null})]);
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    58
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    59
val loadpath = ref ["."];           (*default search path for theory files *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    60
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    61
val delete_tmpfiles = ref true;         (*remove temporary files after use *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    62
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    63
(*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
    64
fun out_name tname = "." ^ tname ^ ".thy.ML";
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    65
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    66
(*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
    67
fun read_thy tname thy_file =
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
    68
  let
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    69
    val instream  = open_in thy_file;
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
    70
    val outstream = open_out (out_name tname);
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
    71
  in
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
    72
    output (outstream, ThySyn.parse tname (input (instream, 999999)));
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    73
    close_out outstream;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    74
    close_in instream
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    75
  end;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    76
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    77
fun file_exists file =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    78
  let val instream = open_in file in close_in instream; true end
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    79
    handle Io _ => false;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    80
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    81
(*Get thy_info for a loaded theory *)
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
    82
fun get_thyinfo tname = Symtab.lookup (!loaded_thys, tname);
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    83
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    84
(*Check if a theory was already loaded *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    85
fun already_loaded thy =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    86
  let val t = get_thyinfo thy
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    87
  in if is_none t then false
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
    88
     else let val ThyInfo {thy_time, ml_time, ...} = the t
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
    89
          in if is_none thy_time orelse is_none ml_time then false
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
    90
             else true
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
    91
          end
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    92
  end;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    93
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    94
(*Check if a theory file has changed since its last use.
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    95
  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
    96
fun thy_unchanged thy thy_file ml_file =
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    97
  let val t = get_thyinfo thy
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
    98
  in if is_some t then
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
    99
       let val ThyInfo {thy_time, ml_time, ...} = the t
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   100
           val tn = is_none thy_time;
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   101
           val mn = is_none ml_time
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   102
       in if not tn andalso not mn then
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   103
              ((file_info thy_file = the thy_time),
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   104
               (file_info ml_file = the ml_time))
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   105
          else if not tn andalso mn then (true, false)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   106
          else (false, false)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   107
       end
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   108
     else (false, false)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   109
  end;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   110
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   111
exception FILE_NOT_FOUND;   (*raised by find_file *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   112
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   113
(*Find a file using a list of paths if no absolute or relative path is
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   114
  specified.*)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   115
fun find_file "" name =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   116
      let fun find_it (curr :: paths) =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   117
                if file_exists (tack_on curr name) then
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   118
                    tack_on curr name
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   119
                else
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   120
                    find_it paths
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   121
           | find_it [] = ""
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   122
      in find_it (!loadpath) end
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   123
  | find_file path name =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   124
      if file_exists (tack_on path name) then tack_on path name
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   125
                                         else "";
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   126
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   127
(*Get absolute pathnames for a new or already loaded theory *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   128
fun get_filenames path name =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   129
  let fun make_absolute file =
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   130
        if file = "" then "" else
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   131
            if hd (explode file) = "/" then file else tack_on (pwd ()) file;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   132
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   133
      fun new_filename () =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   134
        let val found = find_file path (name ^ ".thy")
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   135
                        handle FILE_NOT_FOUND => "";
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   136
            val thy_file = make_absolute found;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   137
            val (thy_path, _) = split_filename thy_file;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   138
            val found = find_file path (name ^ ".ML");
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   139
            val ml_file = if thy_file = "" then make_absolute found
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   140
                          else if file_exists (tack_on thy_path (name ^ ".ML"))
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   141
                          then tack_on thy_path (name ^ ".ML")
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   142
                          else "";
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   143
            val searched_dirs = if path = "" then (!loadpath) else [path]
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   144
        in if thy_file = "" andalso ml_file = "" then
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   145
             error ("Could not find file \"" ^ name ^ ".thy\" or \""
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   146
                    ^ name ^ ".ML\" for theory \"" ^ name ^ "\"\n"
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   147
                    ^ "in the following directories: \"" ^
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   148
                    (space_implode "\", \"" searched_dirs) ^ "\"")
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   149
           else ();
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   150
           (thy_file, ml_file)
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   151
        end;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   152
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   153
      val tinfo = get_thyinfo name;
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   154
  in if is_some tinfo andalso path = "" then
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   155
       let val ThyInfo {path = abs_path, ...} = the tinfo;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   156
           val (thy_file, ml_file) = if abs_path = "" then new_filename ()
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   157
                                     else (find_file abs_path (name ^ ".thy"),
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   158
                                           find_file abs_path (name ^ ".ML"))
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   159
       in if thy_file = "" andalso ml_file = "" then
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   160
            (writeln ("Warning: File \"" ^ (tack_on path name)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   161
                      ^ ".thy\"\ncontaining theory \"" ^ name
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   162
                      ^ "\" no longer exists.");
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   163
             new_filename ()
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   164
            )
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   165
          else (thy_file, ml_file)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   166
       end
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   167
     else new_filename ()
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   168
  end;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   169
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   170
(*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
   171
fun unlink_thy tname =
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   172
  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
   173
        ThyInfo {path = path, children = children \ tname,
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   174
                 thy_time = thy_time, ml_time = ml_time,
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   175
                 theory = theory, thms = thms}
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   176
  in loaded_thys := Symtab.map remove (!loaded_thys) end;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   177
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   178
(*Remove a theory from loaded_thys *)
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   179
fun remove_thy tname =
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   180
  loaded_thys := Symtab.make (filter_out (fn (id, _) => id = tname)
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   181
                 (Symtab.dest (!loaded_thys)));
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   182
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   183
(*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
   184
fun set_info thy_time ml_time tname =
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   185
  let val ThyInfo {path, children, theory, thms, ...} =
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   186
        the (Symtab.lookup (!loaded_thys, tname));
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   187
  in loaded_thys := Symtab.update ((tname,
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   188
       ThyInfo {path = path, children = children,
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   189
                thy_time = Some thy_time, ml_time = Some ml_time,
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   190
                theory = theory, thms = thms}), !loaded_thys)
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   191
  end;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   192
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   193
(*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
   194
fun mark_outdated tname =
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   195
  if already_loaded tname then set_info "" "" tname else ();
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   196
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   197
(*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
   198
  they were last read;
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   199
  loaded_thys is a thy_info list ref containing all theories that have
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   200
  completly been read by this and preceeding use_thy calls.
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   201
  If a theory changed since its last use its children are marked as changed *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   202
fun use_thy name =
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   203
    let val (path, tname) = split_filename name;
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   204
        val (thy_file, ml_file) = get_filenames path tname;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   205
        val (abs_path, _) = if thy_file = "" then split_filename ml_file
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   206
                            else split_filename thy_file;
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   207
        val (thy_uptodate, ml_uptodate) = thy_unchanged tname
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   208
                                                        thy_file ml_file;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   209
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   210
         (*Set absolute path for loaded theory *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   211
         fun set_path () =
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   212
           let val ThyInfo {children, thy_time, ml_time, theory, thms, ...} =
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   213
                 the (Symtab.lookup (!loaded_thys, tname));
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   214
           in loaded_thys := Symtab.update ((tname,
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   215
                               ThyInfo {path = abs_path, children = children,
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   216
                                        thy_time = thy_time, ml_time = ml_time,
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   217
                                        theory = theory, thms = thms}),
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   218
                               !loaded_thys)
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   219
           end;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   220
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   221
         (*Mark all direct descendants of a theory as changed *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   222
         fun mark_children thy =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   223
           let val ThyInfo {children, ...} = the (get_thyinfo thy)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   224
               val loaded = filter already_loaded children
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   225
           in if loaded <> [] then
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   226
                  (writeln ("The following children of theory " ^ (quote thy)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   227
                            ^ " are now out-of-date: "
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   228
                            ^ (quote (space_implode "\",\"" loaded)));
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   229
                   seq mark_outdated loaded
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   230
                  )
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   231
              else ()
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   232
           end;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   233
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   234
        (*Remove all theorems associated with a theory*)
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   235
        fun delete_thms () =
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   236
          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
   237
             Some (ThyInfo {path, children, thy_time, ml_time, theory, ...}) =>
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   238
                 ThyInfo {path = 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,
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   240
                          theory = theory, thms = Symtab.null}
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   241
           | None => ThyInfo {path = "", children = [],
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   242
                              thy_time = None, ml_time = None,
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   243
                              theory = None, thms = Symtab.null};
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   244
         in loaded_thys := Symtab.update ((tname, tinfo), !loaded_thys) end;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   245
    in if thy_uptodate andalso ml_uptodate then ()
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   246
       else
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   247
       (
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   248
         delete_thms ();
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   249
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   250
         if thy_uptodate orelse thy_file = "" then ()
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   251
         else (writeln ("Reading \"" ^ name ^ ".thy\"");
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   252
               read_thy tname thy_file;
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   253
               use (out_name tname);
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   254
               use_string
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   255
                 ["store_theory (" ^ tname ^ ".thy, " ^ quote tname ^ ");"]
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   256
                       (*Store theory object in case it is needed for store_thm
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   257
                         while reading the ML file*)
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   258
              );
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   259
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   260
         if ml_file = "" then ()
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   261
         else (writeln ("Reading \"" ^ name ^ ".ML\"");
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   262
               use ml_file);
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   263
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   264
         use_string ["store_theory (" ^ tname ^ ".thy, " ^ quote tname ^ ");"];
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   265
                    (*Store theory again because it could have been redefined*)
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   266
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   267
         (*Now set the correct info*)
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   268
         set_info (file_info thy_file) (file_info ml_file) tname;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   269
         set_path ();
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   270
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   271
         (*Mark theories that have to be reloaded*)
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   272
         mark_children tname;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   273
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   274
         (*Remove temporary files*)
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   275
         if not (!delete_tmpfiles) orelse (thy_file = "") orelse thy_uptodate
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   276
           then ()
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   277
         else delete_file (out_name tname)
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   278
        )
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   279
    end;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   280
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   281
fun time_use_thy tname = timeit(fn()=>
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   282
   (writeln("\n**** Starting Theory " ^ tname ^ " ****");
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   283
    use_thy tname;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   284
    writeln("\n**** Finished Theory " ^ tname ^ " ****"))
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   285
   );
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   286
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   287
(*Load all thy or ML files that have been changed and also
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   288
  all theories that depend on them *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   289
fun update () =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   290
  let (*List theories in the order they have to be loaded *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   291
      fun load_order [] result = result
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   292
        | load_order thys result =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   293
            let fun next_level (t :: ts) =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   294
                      let val thy = get_thyinfo t
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   295
                      in if is_some thy then
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   296
                             let val ThyInfo {children, ...} = the thy
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   297
                             in children union (next_level ts) end
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   298
                         else next_level ts
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   299
                      end
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   300
                  | next_level [] = [];
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   301
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   302
                val children = next_level thys;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   303
            in load_order children ((result \\ children) @ children) end;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   304
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   305
      fun reload_changed (t :: ts) =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   306
            let val thy = get_thyinfo t;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   307
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   308
                fun abspath () =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   309
                  if is_some thy then
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   310
                    let val ThyInfo {path, ...} = the thy in path end
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   311
                  else "";
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   312
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   313
                val (thy_file, ml_file) = get_filenames (abspath ()) t;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   314
                val (thy_uptodate, ml_uptodate) =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   315
                        thy_unchanged t thy_file ml_file;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   316
            in if thy_uptodate andalso ml_uptodate then ()
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   317
                                                   else use_thy t;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   318
               reload_changed ts
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   319
            end
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   320
        | reload_changed [] = ();
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   321
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   322
     (*Remove all theories that are no descendants of Pure.
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   323
       If there are still children in the deleted theory's list
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   324
       schedule them for reloading *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   325
     fun collect_garbage not_garbage =
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   326
       let fun collect ((tname, ThyInfo {children, ...}) :: ts) =
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   327
                 if tname mem not_garbage then collect ts
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   328
                 else (writeln ("Theory \"" ^ tname
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   329
                         ^ "\" is no longer linked with Pure - removing it.");
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   330
                       remove_thy tname;
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   331
                       seq mark_outdated children)
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   332
             | collect [] = ()
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   333
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   334
       in collect (Symtab.dest (!loaded_thys)) end;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   335
  in collect_garbage ("Pure" :: (load_order ["Pure"] []));
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   336
     reload_changed (load_order ["Pure"] [])
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   337
  end;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   338
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   339
(*Merge theories to build a base for a new theory.
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   340
  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
   341
fun mk_base bases child mk_draft =
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   342
      let (*List all descendants of a theory list *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   343
          fun list_descendants (t :: ts) =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   344
                let val tinfo = get_thyinfo t
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   345
                in if is_some tinfo then
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   346
                     let val ThyInfo {children, ...} = the tinfo
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   347
                     in children union (list_descendants (ts union children))
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   348
                     end
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   349
                   else []
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   350
                end
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   351
            | list_descendants [] = [];
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   352
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   353
          (*Show the cycle that would be created by add_child *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   354
          fun show_cycle base =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   355
            let fun find_it result curr =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   356
                  let val tinfo = get_thyinfo curr
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   357
                  in if base = curr then
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   358
                       error ("Cyclic dependency of theories: "
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   359
                              ^ child ^ "->" ^ base ^ result)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   360
                     else if is_some tinfo then
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   361
                       let val ThyInfo {children, ...} = the tinfo
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   362
                       in seq (find_it ("->" ^ curr ^ result)) children
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   363
                       end
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   364
                     else ()
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   365
                  end
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   366
            in find_it "" child end;
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   367
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   368
          (*Check if a cycle would be created by add_child *)
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   369
          fun find_cycle base =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   370
            if base mem (list_descendants [child]) then show_cycle base
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   371
            else ();
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   372
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   373
          (*Add child to child list of base *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   374
          fun add_child base =
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   375
            let val tinfo =
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   376
                  case Symtab.lookup (!loaded_thys, base) of
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   377
                      Some (ThyInfo {path, children, thy_time, ml_time,
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   378
                                     theory, thms}) =>
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   379
                        ThyInfo {path = path, children = child ins children,
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   380
                                 thy_time = thy_time, ml_time = ml_time,
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   381
                                 theory = theory, thms = thms}
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   382
                    | None => ThyInfo {path = "", children = [child],
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   383
                                       thy_time = None, ml_time = None,
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   384
                                       theory = None, thms = Symtab.null};
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   385
            in loaded_thys := Symtab.update ((base, tinfo), !loaded_thys) end;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   386
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   387
          (*Load a base theory if not already done
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   388
            and no cycle would be created *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   389
          fun load base =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   390
              let val thy_present = already_loaded base
426
767367131b47 replaced "foldl merge_theories" by "merge_thy_list" in base_on
clasohm
parents: 424
diff changeset
   391
                                           (*test this before child is added *)
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   392
              in
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   393
                if child = base then
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   394
                    error ("Cyclic dependency of theories: " ^ child
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   395
                           ^ "->" ^ child)
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   396
                else
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   397
                  (find_cycle base;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   398
                   add_child base;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   399
                   if thy_present then ()
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   400
                   else (writeln ("Autoloading theory " ^ (quote base)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   401
                                  ^ " (used by " ^ (quote child) ^ ")");
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   402
                         use_thy base)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   403
                  )
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   404
              end;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   405
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   406
          (*Load all needed files and make a list of all real theories *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   407
          fun load_base (Thy b :: bs) =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   408
               (load b;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   409
                b :: (load_base bs))
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   410
            | load_base (File b :: bs) =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   411
               (load b;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   412
                load_base bs)    (*don't add it to merge_theories' parameter *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   413
            | load_base [] = [];
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   414
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   415
          (*Get theory object for a loaded theory *)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   416
          fun get_theory name =
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   417
            let val ThyInfo {theory, ...} = the (get_thyinfo name)
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   418
            in the theory end;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   419
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   420
          val mergelist = (unlink_thy child;
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   421
                           load_base bases);
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   422
     in writeln ("Loading theory " ^ (quote child));
426
767367131b47 replaced "foldl merge_theories" by "merge_thy_list" in base_on
clasohm
parents: 424
diff changeset
   423
        merge_thy_list mk_draft (map get_theory mergelist) end;
391
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
(*Change theory object for an existent item of loaded_thys
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   426
  or create a new item *)
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   427
fun store_theory (thy, tname) =
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   428
  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
   429
               Some (ThyInfo {path, children, thy_time, ml_time, thms, ...}) =>
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   430
                 ThyInfo {path = path, children = children,
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   431
                          thy_time = thy_time, ml_time = ml_time,
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   432
                          theory = Some thy, thms = thms}
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   433
             | None => ThyInfo {path = "", children = [],
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   434
                                thy_time = Some "", ml_time = Some "",
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   435
                                theory = Some thy, thms = Symtab.null};
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   436
  in loaded_thys := Symtab.update ((tname, tinfo), !loaded_thys) end;
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   437
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   438
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   439
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   440
(** store and retrieve theorems **)
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   441
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   442
(*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
   443
fun thyinfo_of_sign sg =
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   444
  let
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   445
    val ref xname = hd (#stamps (Sign.rep_sg sg));
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   446
    val opt_info = get_thyinfo xname;
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   447
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   448
    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
   449
      | 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
   450
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   451
    val show_sg = Pretty.str_of o Sign.pretty_sg;
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   452
  in
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   453
    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
   454
      (xname, the opt_info)
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   455
    else
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   456
      (case Symtab.find_first (eq_sg o snd) (! loaded_thys) of
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   457
        Some name_info => name_info
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   458
      | 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
   459
  end;
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   460
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   461
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   462
(*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
   463
fun theory_of_sign sg =
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   464
  (case thyinfo_of_sign sg of
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   465
    (_, ThyInfo {theory = Some thy, ...}) => thy
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   466
  | _ => sys_error "theory_of_sign");
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   467
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   468
(*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
   469
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
   470
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   471
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   472
(* Store theorems *)
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   473
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   474
(*Store a theorem in the thy_info of its theory*)
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   475
fun store_thm (name, thm) =
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   476
  let
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   477
    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
   478
      thyinfo_of_sign (#sign (rep_thm thm));
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   479
    val thms' = Symtab.update_new ((name, thm), thms)
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   480
      handle Symtab.DUPLICATE s => error ("Duplicate theorem name " ^ quote s);
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   481
  in
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   482
    loaded_thys := Symtab.update
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   483
     ((thy_name, ThyInfo {path = path, children = children,
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   484
       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
   485
      ! loaded_thys);
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   486
    thm
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   487
  end;
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   488
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   489
(*Store result of proof in loaded_thys and as ML value*)
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   490
fun qed name =
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   491
  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
   492
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   493
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   494
(* Retrieve theorems *)
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   495
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   496
(*Get all theorems belonging to a given theory object*)
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   497
fun thmtab thy =
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   498
  let val (_, ThyInfo {thms, ...}) = thyinfo_of_sign (sign_of thy)
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   499
  in thms end;
476
836cad329311 added check for concistency of filename and theory name;
clasohm
parents: 426
diff changeset
   500
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   501
(*Get a stored theorem specified by theory and name*)
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   502
fun get_thm thy name =
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   503
  (case Symtab.lookup (thmtab thy, name) of
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   504
    Some thm => thm
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   505
  | None => raise THEORY ("get_thm: no theorem " ^ quote name, [thy]));
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   506
715
f76ad10f5802 added call of store_theory after thy file has been read
clasohm
parents: 586
diff changeset
   507
(*Get stored theorems of a theory*)
559
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   508
val thms_of = Symtab.dest o thmtab;
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   509
00365d2e0c50 added theory_of_sign, theory_of_thm;
wenzelm
parents: 476
diff changeset
   510
391
e960fe156cd8 (was Thy/read.ML)
wenzelm
parents:
diff changeset
   511
end;