src/Pure/Thy/thy_load.ML
author wenzelm
Thu, 02 Sep 1999 15:22:15 +0200
changeset 7438 2e0e4253b6c3
parent 7190 ba6f09cd7769
child 7901 c21c3d2256ef
permissions -rw-r--r--
with_path;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Thy/thy_load.ML
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
     4
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
     5
Theory loader primitives.
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
     6
*)
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
     7
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
     8
signature BASIC_THY_LOAD =
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
     9
sig
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    10
  val show_path: unit -> string list
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    11
  val add_path: string -> unit
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    12
  val del_path: string -> unit
7438
2e0e4253b6c3 with_path;
wenzelm
parents: 7190
diff changeset
    13
  val with_path: string -> ('a -> 'b) -> 'a -> 'b
6205
dd3d3da0f458 added reset_path;
wenzelm
parents: 6168
diff changeset
    14
  val reset_path: unit -> unit
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    15
end;
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    16
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    17
signature THY_LOAD =
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    18
sig
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    19
  include BASIC_THY_LOAD
6205
dd3d3da0f458 added reset_path;
wenzelm
parents: 6168
diff changeset
    20
  val ml_path: string -> Path.T
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    21
  val check_file: Path.T -> (Path.T * File.info) option
7190
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    22
  val may_load_file: bool -> Path.T -> (Path.T * File.info)
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    23
  eqtype master
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    24
  val get_thy: master -> Path.T * File.info
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    25
  val check_thy: Path.T -> string -> master
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    26
  val deps_thy: Path.T -> string -> master * (string list * Path.T list)
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    27
  val load_thy: Path.T -> string -> bool -> bool -> master
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    28
end;
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    29
6363
c784ab29f424 comment;
wenzelm
parents: 6232
diff changeset
    30
(*backdoor sealed later*)
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    31
signature PRIVATE_THY_LOAD =
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    32
sig
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    33
  include THY_LOAD
7190
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    34
  val deps_thy_fn: (string -> Path.T -> string list * Path.T list) ref
6205
dd3d3da0f458 added reset_path;
wenzelm
parents: 6168
diff changeset
    35
  val load_thy_fn: (string -> bool -> bool -> Path.T -> unit) ref
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    36
end;
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    37
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    38
structure ThyLoad: PRIVATE_THY_LOAD =
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    39
struct
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    40
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    41
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    42
(* maintain load path *)
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    43
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    44
val load_path = ref [Path.current];
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    45
fun change_path f = load_path := f (! load_path);
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    46
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    47
fun show_path () = map Path.pack (! load_path);
6484
3f098b0ec683 use_thy etc.: may specify path prefix, which is temporarily used as load path;
wenzelm
parents: 6363
diff changeset
    48
fun add_path s = change_path (cons (Path.unpack s));
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    49
fun del_path s = change_path (filter_out (equal (Path.unpack s)));
6205
dd3d3da0f458 added reset_path;
wenzelm
parents: 6168
diff changeset
    50
fun reset_path () = load_path := [Path.current];
7438
2e0e4253b6c3 with_path;
wenzelm
parents: 7190
diff changeset
    51
fun with_path s f x = Library.setmp load_path (Path.unpack s :: ! load_path) f x;
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    52
7438
2e0e4253b6c3 with_path;
wenzelm
parents: 7190
diff changeset
    53
fun cond_with_path path f x =
7190
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    54
  if Path.is_current path then f x else Library.setmp load_path (path :: ! load_path) f x;
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    55
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    56
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    57
(* file formats *)
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    58
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    59
val ml_exts = ["", "ML", "sml"];
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    60
val ml_path = Path.ext "ML" o Path.basic;
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    61
val thy_path = Path.ext "thy" o Path.basic;
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    62
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    63
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    64
(* check_file *)
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    65
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    66
fun check_file src_path =
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    67
  let
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    68
    val path = Path.expand src_path;
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    69
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    70
    fun find_ext rel_path ext =
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    71
      let val ext_path = Path.ext ext rel_path
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    72
      in apsome (fn info => (File.full_path ext_path, info)) (File.info ext_path) end;
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    73
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    74
    fun find_dir dir =
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    75
      get_first (find_ext (Path.append dir path)) ml_exts;
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    76
  in get_first find_dir (if Path.is_basic path then ! load_path else [Path.current]) end;
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    77
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    78
7190
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    79
(* may_load_file *)
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    80
7190
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    81
fun may_load_file really raw_path =
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    82
  (case check_file raw_path of
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    83
    None => error ("Could not find ML file " ^ quote (Path.pack raw_path))
7190
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    84
  | Some (path, info) => (if really then File.symbol_use path else (); (path, info)));
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    85
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    86
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    87
(* datatype master *)
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    88
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    89
datatype master = Master of (Path.T * File.info) * (Path.T * File.info) option;
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    90
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    91
fun get_thy (Master (thy, _)) = thy;
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    92
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    93
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    94
(* check_thy *)
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    95
7190
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    96
fun check_thy_aux name =
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    97
  (case check_file (thy_path name) of
6484
3f098b0ec683 use_thy etc.: may specify path prefix, which is temporarily used as load path;
wenzelm
parents: 6363
diff changeset
    98
    None => error ("Could not find theory file for " ^ quote name ^ " in dir(s) " ^
3f098b0ec683 use_thy etc.: may specify path prefix, which is temporarily used as load path;
wenzelm
parents: 6363
diff changeset
    99
      commas_quote (show_path ()))
7190
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
   100
  | Some thy_info => (thy_info, check_file (ml_path name)));
6205
dd3d3da0f458 added reset_path;
wenzelm
parents: 6168
diff changeset
   101
7438
2e0e4253b6c3 with_path;
wenzelm
parents: 7190
diff changeset
   102
fun check_thy dir name = Master (cond_with_path dir check_thy_aux name);
6484
3f098b0ec683 use_thy etc.: may specify path prefix, which is temporarily used as load path;
wenzelm
parents: 6363
diff changeset
   103
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   104
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   105
(* process theory files *)
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   106
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   107
(*hooks for theory syntax dependent operations*)
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   108
fun undefined _ = raise Match;
7190
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
   109
val deps_thy_fn = ref (undefined: string -> Path.T -> string list * Path.T list);
6205
dd3d3da0f458 added reset_path;
wenzelm
parents: 6168
diff changeset
   110
val load_thy_fn = ref (undefined: string -> bool -> bool -> Path.T -> unit);
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   111
7190
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
   112
fun process_thy dir f name =
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
   113
  let val master as Master ((path, _), _) = check_thy dir name
7438
2e0e4253b6c3 with_path;
wenzelm
parents: 7190
diff changeset
   114
  in (master, cond_with_path dir f path) end;
6219
b360065c2b07 check_thy: include ML stamp;
wenzelm
parents: 6205
diff changeset
   115
7190
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
   116
fun deps_thy dir name = process_thy dir (! deps_thy_fn name) name;
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
   117
fun load_thy dir name ml time = #1 (process_thy dir (! load_thy_fn name ml time) name);
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   118
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   119
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   120
end;
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   121
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   122
structure BasicThyLoad: BASIC_THY_LOAD = ThyLoad;
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   123
open BasicThyLoad;