src/Pure/Thy/read.ML
author clasohm
Tue, 26 Oct 1993 22:24:20 +0100
changeset 81 4cc5a34292a9
parent 78 e76a1edc2e49
child 123 0a2f744e008a
permissions -rw-r--r--
corrected some spelling mistakes; removed a bug that made it impossible to read theories that don't have a ML file; extended syntax for bases in syntax.ML: a string can be used to specify a theory that is to be read but is not merged into the base (useful for pseudo theories used to document the dependencies of ML files)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     1
(*  Title:      Pure/Thy/read
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    ID:         $Id$
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     3
    Author:     Sonia Mahjoub / Tobias Nipkow / L C Paulson
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     4
    Copyright   1992  TU Muenchen
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     5
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
Reading and writing the theory definition files.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     8
For theory XXX, the  input file is called XXX.thy
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     9
                the output file is called .XXX.thy.ML
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    10
                and it then tries to read XXX.ML
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    11
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    12
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    13
signature READTHY =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    14
sig
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    15
  type thy_info
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
    16
  datatype BaseType = Thy  of string
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
    17
                    | File of string
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    18
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    19
  val use_thy        : string -> unit
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    20
  val update         : unit -> unit
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    21
  val time_use_thy   : string -> unit
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
    22
  val base_on        : BaseType list -> string -> Thm.theory
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    23
  val store_theory   : string -> Thm.theory -> unit
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    24
  val list_loaded    : unit -> thy_info list
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    25
  val set_loaded     : thy_info list -> unit
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    26
  val set_loadpath   : string list -> unit
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    27
  val relations      : string -> unit
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    28
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    29
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    30
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    31
functor ReadthyFUN (structure ThySyn: THYSYN) : READTHY =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    32
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    33
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
    34
datatype thy_info = ThyInfo of {name: string, children: string list, 
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    35
                                thy_info: string, ml_info: string, 
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    36
                                theory: Thm.theory};
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    37
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
    38
datatype BaseType = Thy  of string
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
    39
                  | File of string;
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
    40
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
    41
val loaded_thys = ref [ThyInfo {name = "pure", children = [], thy_info = "",
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    42
                       ml_info = "", theory = Thm.pure_thy}];
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    43
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    44
val loadpath = ref ["."];
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    45
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    46
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    47
(*Make name of the output ML file for a theory *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    48
fun out_name thy = "." ^ thy ^ ".thy.ML";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    49
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    50
(*Read a file specified by thy_file containing theory thy *)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    51
fun read_thy thy thy_file =
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    52
    let val instream  = open_in thy_file;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    53
        val outstream = open_out (out_name thy)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    54
    in  output (outstream, ThySyn.read (explode(input(instream, 999999))));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    55
        close_out outstream;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    56
        close_in instream
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    57
    end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    58
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    59
fun file_exists file =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    60
  let val instream = open_in file in close_in instream; true end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    61
    handle Io _ => false;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    62
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    63
exception FILE_NOT_FOUND; (*raised by find_file *)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    64
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    65
(*Find a file using a list of paths if no absolute or relative path is
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    66
  specified.*)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    67
fun find_file "" name =
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    68
      let fun find_it (curr :: paths) =
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    69
                if file_exists (tack_on curr name) then
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    70
                    tack_on curr name
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    71
                else 
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    72
                    find_it paths
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    73
           | find_it [] = raise FILE_NOT_FOUND
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    74
      in find_it (!loadpath) end
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    75
  | find_file path name =
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    76
      if file_exists (tack_on path name) then tack_on path name
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    77
                                         else raise FILE_NOT_FOUND;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    78
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    79
(*Check if a theory was already loaded *)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    80
fun already_loaded thy =
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    81
  let fun do_search ((ThyInfo {name, ...}) :: loaded) =
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    82
              if name = thy then true else do_search loaded
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    83
        | do_search [] = false
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    84
  in do_search (!loaded_thys) end;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    85
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    86
(*Get thy_info for a loaded theory *)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    87
fun get_thyinfo thy =
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    88
  let fun do_search (t :: loaded : thy_info list) =
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    89
            let val ThyInfo {name, ...} = t
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    90
            in if name = thy then t else do_search loaded end
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    91
        | do_search [] = error ("Internal error (get_thyinfo): theory " 
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    92
                                ^ thy ^ " not found.")
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    93
  in do_search (!loaded_thys) end;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    94
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    95
(*Check if a theory file has changed since its last use.
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
    96
  Return a pair of boolean values for .thy and for .ML *)
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    97
fun thy_unchanged thy thy_file ml_file = 
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
    98
      if already_loaded thy then 
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
    99
        let val ThyInfo {thy_info, ml_info, ...} = get_thyinfo thy
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   100
        in ((file_info (thy_file) = thy_info), (file_info (ml_file) = ml_info))
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   101
        end
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   102
      else (false, false);
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   103
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   104
(*Get theory object for a loaded theory *)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   105
fun get_theory name =
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   106
  let val ThyInfo {theory, ...} = get_thyinfo name
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   107
  in theory end;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   108
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   109
(*Read .thy and .ML files that haven't been read yet or have changed since 
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   110
  they were last read;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   111
  loaded_thys is a thy_info list ref containing all theories that have 
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   112
  completly been read by this and preceeding use_thy calls.
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   113
  If a theory changed since its last use its children are marked as changed *)
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   114
fun use_thy name =
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   115
    let val (path, thy_name) = split_filename name;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   116
        val thy = to_lower thy_name;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   117
        val thy_file = (find_file path (thy ^ ".thy")
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   118
                        handle FILE_NOT_FOUND => "");
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   119
        val (thy_path, _) = split_filename thy_file;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   120
        val ml_file = if thy_file = ""
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   121
                      then (find_file path (thy ^ ".ML")
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   122
                            handle FILE_NOT_FOUND => 
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   123
                             error ("Could find no .thy or .ML file for theory "
78
e76a1edc2e49 removed a bug that occured when a path was specified for use_thy's parameter
clasohm
parents: 74
diff changeset
   124
                                    ^ thy_name))
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   125
                      else if file_exists (thy_path ^ thy ^ ".ML")
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   126
                      then thy_path ^ thy ^ ".ML"
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   127
                      else ""
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   128
        val (thy_uptodate, ml_uptodate) = thy_unchanged thy thy_file ml_file;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   129
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   130
        (*Remove theory from all child lists in loaded_thys.
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   131
          Afterwards add_child should be called for the (new) base theories *)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   132
        fun remove_child thy =
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   133
          let fun do_remove (ThyInfo {name, children, thy_info, ml_info, theory}
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   134
                            :: loaded) result =
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   135
                    do_remove loaded 
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   136
                      (ThyInfo {name = name, children = children \ thy, 
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   137
                                thy_info = thy_info, ml_info = ml_info,
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   138
                                theory = theory} :: result)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   139
                | do_remove [] result = result;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   140
          in loaded_thys := do_remove (!loaded_thys) [] end;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   141
          
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   142
         exception THY_NOT_FOUND;  (*Raised by set_info *)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   143
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   144
         (*Change thy_info and ml_info for an existent item or create
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   145
           a new item with empty child list *)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   146
         fun set_info thy_new ml_new thy =
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   147
               let fun make_change (t :: loaded) =
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   148
                         let val ThyInfo {name, children, theory, ...} = t
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   149
                         in if name = thy then            
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   150
                              ThyInfo {name = name, children = children,
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   151
                                       thy_info = thy_new, ml_info = ml_new,
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   152
                                       theory = theory} :: loaded
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   153
                            else
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   154
                              t :: (make_change loaded)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   155
                         end
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   156
                     | make_change [] = raise THY_NOT_FOUND
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   157
               in loaded_thys := make_change (!loaded_thys) end;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   158
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   159
         (*Mark all direct and indirect descendants of a theory as changed *)
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   160
         fun mark_children thy =
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   161
           let val ThyInfo {children, ...} = get_thyinfo thy
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   162
           in if children <> [] then
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   163
                  (writeln ("The following children of theory " ^ (quote thy)
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   164
                            ^ " are now out-of-date: \""
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   165
                            ^ (space_implode "\",\"" children) ^ "\"");
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   166
                   seq (set_info "" "") children
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   167
                   handle THY_NOT_FOUND => ()
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   168
                        (*If this theory was automatically loaded by a child 
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   169
                          then the child cannot be found in loaded_thys *)
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   170
                  )
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   171
              else ()
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   172
           end
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   173
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   174
    in if thy_uptodate andalso ml_uptodate then ()
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   175
       else
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   176
       (
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   177
         writeln ("Loading theory " ^ (quote name));
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   178
         if thy_uptodate orelse (thy_file = "") then ()
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   179
         else
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   180
         (
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   181
             (*Allow dependency lists to be rebuild completely *)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   182
             remove_child thy;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   183
                
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   184
             read_thy thy thy_file
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   185
         );
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   186
         
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   187
         (*Actually read files!*)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   188
         if thy_uptodate orelse (thy_file = "") then ()
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   189
                         else use (out_name thy);
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   190
         if (thy_file = "") then          (*theory object created in .ML file*)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   191
             (
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   192
                 use ml_file;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   193
                 let val outstream = open_out (".tmp.ML") 
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   194
                 in
78
e76a1edc2e49 removed a bug that occured when a path was specified for use_thy's parameter
clasohm
parents: 74
diff changeset
   195
                    output (outstream, "store_theory " ^ quote thy_name ^ " "
e76a1edc2e49 removed a bug that occured when a path was specified for use_thy's parameter
clasohm
parents: 74
diff changeset
   196
                                       ^ thy_name ^ ".thy;\n");
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   197
                    close_out outstream 
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   198
                 end;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   199
                 use ".tmp.ML";
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   200
                 delete_file ".tmp.ML"
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   201
             )
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   202
         else if ml_file <> "" then use ml_file
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   203
         else ();
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   204
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   205
         (*Now set the correct info.*)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   206
         (set_info (file_info thy_file) (file_info ml_file) thy
78
e76a1edc2e49 removed a bug that occured when a path was specified for use_thy's parameter
clasohm
parents: 74
diff changeset
   207
          handle THY_NOT_FOUND => error ("Could not find theory \"" ^ thy
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   208
                                         ^ "\" (wrong filename?)"));
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   209
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   210
         (*Mark theories that have to be reloaded.*)
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   211
         mark_children thy;
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   212
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   213
         delete_file (out_name thy)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   214
       )
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   215
    end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   216
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   217
fun time_use_thy tname = timeit(fn()=>
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   218
   (writeln("\n**** Starting Theory " ^ tname ^ " ****");  
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   219
    use_thy tname;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   220
    writeln("\n**** Finished Theory " ^ tname ^ " ****"))
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   221
   );
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   222
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   223
(*Load all thy or ML files that have been changed and also
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   224
  all theories that depend on them *)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   225
fun update () =
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   226
  let (*List theories in the order they have to be loaded *)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   227
      fun load_order [] result = result
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   228
        | load_order thys result =
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   229
            let fun next_level (t :: ts) =
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   230
                      let val ThyInfo {children, ...} = get_thyinfo t
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   231
                      in children union (next_level ts)
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   232
                      end
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   233
                  | next_level [] = [];
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   234
                  
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   235
                val children = next_level thys
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   236
            in load_order children ((result \\ children) @ children) end;
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   237
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   238
      fun reload_changed (t :: ts) =
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   239
            let val curr = get_thyinfo t;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   240
                val thy_file = (find_file "" (t ^ ".thy")
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   241
                                handle FILE_NOT_FOUND => "");
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   242
                val (thy_path, _) = split_filename thy_file;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   243
                val ml_file = if thy_file = ""
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   244
                              then (find_file "" (t ^ ".ML")
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   245
                                    handle FILE_NOT_FOUND => 
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   246
                             error ("Could find no .thy or .ML file for theory "
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   247
                                    ^ t))
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   248
                              else if file_exists (thy_path ^ t ^ ".ML")
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   249
                              then thy_path ^ t ^ ".ML"
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   250
                              else ""
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   251
                val (thy_uptodate, ml_uptodate) =
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   252
                      thy_unchanged t thy_file ml_file;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   253
 
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   254
            in if thy_uptodate andalso ml_uptodate then ()
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   255
                                                   else use_thy t;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   256
               reload_changed ts
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   257
            end
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   258
        | reload_changed [] = ()
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   259
  in reload_changed (load_order ["pure"] []) end;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   260
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   261
(*Merge theories to build a base for a new theory.
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   262
  Base members are only loaded if they are missing. *)
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   263
fun base_on bases child =
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   264
      let val childl = to_lower child;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   265
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   266
          (*List all descendants of a theory list *)
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   267
          fun list_descendants (t :: ts) =
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   268
                if already_loaded t then
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   269
                  let val ThyInfo {children, ...} = get_thyinfo t
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   270
                  in children union 
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   271
                     (list_descendants (ts union children))
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   272
                  end
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   273
                else []
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   274
            | list_descendants [] = [];
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   275
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   276
          (*Show the cycle that would be created by add_child *)
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   277
          fun show_cycle base =
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   278
            let fun find_it result curr =
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   279
                  if base = curr then 
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   280
                      error ("Cyclic dependency of theories: "
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   281
                             ^ childl ^ "->" ^ base ^ result)
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   282
                  else if already_loaded curr then
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   283
                    let val ThyInfo {children, ...} = get_thyinfo curr
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   284
                    in seq (find_it ("->" ^ curr ^ result)) children
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   285
                    end
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   286
                  else ()
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   287
            in find_it "" childl end;
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   288
        
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   289
          (*Check if a cycle will be created by add_child *)
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   290
          fun find_cycle base =
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   291
            if base mem (list_descendants [childl]) then show_cycle base
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   292
            else ();
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   293
                   
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   294
          (*Add child to child list of base *)
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   295
          fun add_child (t :: loaded) base =
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   296
                let val ThyInfo {name, children, thy_info, ml_info, theory} = t
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   297
                in if name = base then
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   298
                     ThyInfo {name = name, 
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   299
                              children = childl ins children,
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   300
                              thy_info = thy_info, ml_info = ml_info,
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   301
                              theory = theory} :: loaded
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   302
                   else
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   303
                     t :: (add_child loaded base)
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   304
                end
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   305
           | add_child [] base =
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   306
               [ThyInfo {name = base, children = [childl], 
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   307
                         thy_info = "", ml_info = "",
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   308
                         theory = Thm.pure_thy}];
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   309
                                            (*Thm.pure_thy is used as a dummy *)
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   310
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   311
          fun do_load thy =
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   312
              let val basel = to_lower thy;
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   313
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   314
                  val thy_present = already_loaded basel
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   315
                                            (*test this before child is added *)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   316
              in
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   317
                if childl = basel then
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   318
                    error ("Cyclic dependency of theories: " ^ child
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   319
                           ^ "->" ^ child)
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   320
                else 
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   321
                  (find_cycle thy;
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   322
                   loaded_thys := add_child (!loaded_thys) basel;
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   323
                   if thy_present then ()
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   324
                   else (writeln ("Autoloading theory " ^ (quote thy)
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   325
                                  ^ " (used by " ^ (quote child) ^ ")");
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   326
                         use_thy thy)
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   327
                  )
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   328
              end; 
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   329
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   330
          fun load_base (Thy b :: bs) =
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   331
               (do_load b;
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   332
                (to_lower b) :: (load_base bs))
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   333
            | load_base (File b :: bs) =
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   334
               (do_load b;
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   335
                load_base bs)     (*don't add it to merge_theories' parameter *)
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   336
            | load_base [] = [];
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   337
              
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   338
          val (t :: ts) = load_base bases
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   339
     in foldl Thm.merge_theories (get_theory t, map get_theory ts) end;
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   340
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   341
(*Change theory object for an existent item of loaded_thys 
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   342
  or create a new item *)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   343
fun store_theory thy_name thy =
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   344
  let fun make_change (t :: loaded) =
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   345
            let val ThyInfo {name, children, thy_info, ml_info, ...} = t
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   346
            in if name = (to_lower thy_name) then            
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   347
                    ThyInfo {name = name, children = children,
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   348
                             thy_info = thy_info, ml_info = ml_info,
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   349
                             theory = thy} :: loaded
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   350
               else
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   351
                    t :: (make_change loaded)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   352
            end
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   353
        | make_change [] =
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   354
            [ThyInfo {name = (to_lower thy_name), children = [], thy_info = "", 
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   355
                      ml_info = "", theory = thy}]
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   356
  in loaded_thys := make_change (!loaded_thys) end;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   357
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   358
(*Create a list of all theories loaded by this structure *)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   359
fun list_loaded () = (!loaded_thys);
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   360
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   361
(*Change the list of loaded theories *)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   362
fun set_loaded [] =
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   363
      loaded_thys := [ThyInfo {name = "pure", children = [], thy_info = "",
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   364
                      ml_info = "", theory = Thm.pure_thy}]
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   365
  | set_loaded ts =
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   366
      loaded_thys := ts;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   367
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   368
(*Change the path list that is to be searched for .thy and .ML files *)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   369
fun set_loadpath new_path =
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   370
  loadpath := new_path;
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   371
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   372
(*This function is for debugging purposes only *)
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   373
fun relations thy =
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   374
  let val ThyInfo {children, ...} = get_thyinfo thy
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   375
  in
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   376
     writeln (thy ^ ": " ^ (space_implode ", " children));
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 78
diff changeset
   377
     seq relations children
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   378
  end
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   379
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   380
end;
74
208ab8773a73 changes in Readthy:
clasohm
parents: 0
diff changeset
   381