src/Pure/Thy/thy_load.ML
author wenzelm
Wed, 27 Oct 1999 17:09:31 +0200
changeset 7950 720af28e6354
parent 7940 def6db239934
child 8750 36b165788421
permissions -rw-r--r--
export cond_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
7950
720af28e6354 export cond_with_path;
wenzelm
parents: 7940
diff changeset
    20
  val cond_with_path: Path.T -> ('a -> 'b) -> 'a -> 'b
6205
dd3d3da0f458 added reset_path;
wenzelm
parents: 6168
diff changeset
    21
  val ml_path: string -> Path.T
7901
c21c3d2256ef export thy_path;
wenzelm
parents: 7438
diff changeset
    22
  val thy_path: string -> Path.T
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    23
  val check_file: Path.T -> (Path.T * File.info) option
7940
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
    24
  val load_file: Path.T -> (Path.T * File.info)
7190
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    25
  eqtype master
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    26
  val get_thy: master -> Path.T * File.info
7940
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
    27
  val check_thy: Path.T -> string -> bool -> master
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
    28
  val deps_thy: Path.T -> string -> bool -> master * (string list * Path.T list)
7190
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    29
  val load_thy: Path.T -> string -> bool -> bool -> master
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    30
end;
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    31
7940
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
    32
(*this backdoor sealed later*)
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    33
signature PRIVATE_THY_LOAD =
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    34
sig
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    35
  include THY_LOAD
7940
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
    36
  val deps_thy_fn: (string -> bool -> Path.T -> string list * Path.T list) ref
6205
dd3d3da0f458 added reset_path;
wenzelm
parents: 6168
diff changeset
    37
  val load_thy_fn: (string -> bool -> bool -> Path.T -> unit) ref
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    38
end;
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    39
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    40
structure ThyLoad: PRIVATE_THY_LOAD =
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    41
struct
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    42
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    43
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    44
(* maintain load path *)
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    45
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    46
val load_path = ref [Path.current];
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    47
fun change_path f = load_path := f (! load_path);
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    48
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    49
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
    50
fun add_path s = change_path (cons (Path.unpack s));
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    51
fun del_path s = change_path (filter_out (equal (Path.unpack s)));
6205
dd3d3da0f458 added reset_path;
wenzelm
parents: 6168
diff changeset
    52
fun reset_path () = load_path := [Path.current];
7438
2e0e4253b6c3 with_path;
wenzelm
parents: 7190
diff changeset
    53
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
    54
7438
2e0e4253b6c3 with_path;
wenzelm
parents: 7190
diff changeset
    55
fun cond_with_path path f x =
7190
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    56
  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
    57
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    58
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    59
(* file formats *)
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    60
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    61
val ml_exts = ["", "ML", "sml"];
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    62
val ml_path = Path.ext "ML" o Path.basic;
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    63
val thy_path = Path.ext "thy" o Path.basic;
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    64
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    65
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    66
(* check_file *)
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    67
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    68
fun check_file src_path =
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    69
  let
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    70
    val path = Path.expand src_path;
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    71
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    72
    fun find_ext rel_path ext =
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    73
      let val ext_path = Path.ext ext rel_path
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    74
      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
    75
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    76
    fun find_dir dir =
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    77
      get_first (find_ext (Path.append dir path)) ml_exts;
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    78
  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
    79
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    80
7940
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
    81
(* load_file *)
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    82
7940
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
    83
fun load_file raw_path =
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    84
  (case check_file raw_path of
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    85
    None => error ("Could not find ML file " ^ quote (Path.pack raw_path))
7940
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
    86
  | Some (path, info) => (File.symbol_use path; (path, info)));
7190
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    87
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 *)
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    90
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    91
datatype master = Master of (Path.T * File.info) * (Path.T * File.info) option;
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    92
ba6f09cd7769 simplified handling of ML file;
wenzelm
parents: 6978
diff changeset
    93
fun get_thy (Master (thy, _)) = thy;
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    94
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    95
7940
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
    96
(* check / process theory files *)
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    97
7940
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
    98
local
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
    99
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
   100
fun check_thy_aux (name, ml) =
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
   101
  (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
   102
    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
   103
      commas_quote (show_path ()))
7940
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
   104
  | Some thy_info => (thy_info, if ml then check_file (ml_path name) else None));
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
   105
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
   106
in
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
   107
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
   108
fun check_thy dir name ml = Master (cond_with_path dir check_thy_aux (name, ml));
6205
dd3d3da0f458 added reset_path;
wenzelm
parents: 6168
diff changeset
   109
7940
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
   110
fun process_thy dir f name ml =
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
   111
  let val master as Master ((path, _), _) = check_thy dir name ml
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
   112
  in (master, cond_with_path dir f path) end;
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
   113
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
   114
end;
6484
3f098b0ec683 use_thy etc.: may specify path prefix, which is temporarily used as load path;
wenzelm
parents: 6363
diff changeset
   115
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   116
7940
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
   117
(* deps_thy and load_thy *)
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   118
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   119
(*hooks for theory syntax dependent operations*)
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   120
fun undefined _ = raise Match;
7940
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
   121
val deps_thy_fn = ref (undefined: string -> bool -> Path.T -> string list * Path.T list);
6205
dd3d3da0f458 added reset_path;
wenzelm
parents: 6168
diff changeset
   122
val load_thy_fn = ref (undefined: string -> bool -> bool -> Path.T -> unit);
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   123
7940
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
   124
fun deps_thy dir name ml = process_thy dir (! deps_thy_fn name ml) name ml;
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
   125
fun load_thy dir name ml time = #1 (process_thy dir (! load_thy_fn name ml time) name ml);
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   126
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   127
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   128
end;
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   129
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   130
structure BasicThyLoad: BASIC_THY_LOAD = ThyLoad;
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   131
open BasicThyLoad;