src/Pure/Thy/thy_load.ML
author wenzelm
Thu, 04 Feb 1999 18:18:02 +0100
changeset 6232 4336add1c251
parent 6219 b360065c2b07
child 6363 c784ab29f424
permissions -rw-r--r--
include full paths in file info; include (optional) ML file in master info;
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
6205
dd3d3da0f458 added reset_path;
wenzelm
parents: 6168
diff changeset
    13
  val reset_path: unit -> unit
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    14
end;
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    15
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    16
signature THY_LOAD =
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    17
sig
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    18
  include BASIC_THY_LOAD
6205
dd3d3da0f458 added reset_path;
wenzelm
parents: 6168
diff changeset
    19
  val ml_path: string -> Path.T
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    20
  val check_file: Path.T -> (Path.T * File.info) option
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    21
  val load_file: Path.T -> (Path.T * File.info)
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    22
  val check_thy: string -> bool -> (Path.T * File.info) list
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    23
  val deps_thy: string -> bool -> (Path.T * File.info) list * (string list * Path.T list)
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    24
  val load_thy: string -> bool -> bool -> (Path.T * File.info) list
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    25
end;
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    26
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    27
signature PRIVATE_THY_LOAD =
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    28
sig
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    29
  include THY_LOAD
6205
dd3d3da0f458 added reset_path;
wenzelm
parents: 6168
diff changeset
    30
  val deps_thy_fn: (string -> bool -> Path.T -> string list * Path.T list) ref
dd3d3da0f458 added reset_path;
wenzelm
parents: 6168
diff changeset
    31
  val load_thy_fn: (string -> bool -> bool -> Path.T -> unit) ref
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    32
end;
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    33
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    34
structure ThyLoad: PRIVATE_THY_LOAD =
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    35
struct
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    36
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    37
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    38
(* maintain load path *)
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    39
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    40
val load_path = ref [Path.current];
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    41
fun change_path f = load_path := f (! load_path);
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    42
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    43
fun show_path () = map Path.pack (! load_path);
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    44
fun add_path s = change_path (fn path => path @ [Path.unpack s]);
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    45
fun del_path s = change_path (filter_out (equal (Path.unpack s)));
6205
dd3d3da0f458 added reset_path;
wenzelm
parents: 6168
diff changeset
    46
fun reset_path () = load_path := [Path.current];
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    47
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    48
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    49
(* file formats *)
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    50
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    51
val ml_exts = ["", "ML", "sml"];
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    52
val ml_path = Path.ext "ML" o Path.basic;
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    53
val thy_path = Path.ext "thy" o Path.basic;
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    54
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    55
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    56
(* check_file *)
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    57
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    58
fun check_file src_path =
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    59
  let
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    60
    val path = Path.expand src_path;
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    61
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    62
    fun find_ext rel_path ext =
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    63
      let val ext_path = Path.ext ext rel_path
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    64
      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
    65
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    66
    fun find_dir dir =
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    67
      get_first (find_ext (Path.append dir path)) ml_exts;
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    68
  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
    69
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    70
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    71
(* load_file *)
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    72
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    73
fun load_file raw_path =
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    74
  (case check_file raw_path of
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    75
    None => error ("Could not find ML file " ^ quote (Path.pack raw_path))
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    76
  | Some (path, info) => (Symbol.use path; (path, info)));
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
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    79
(* check_thy *)
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    80
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    81
fun check_thy name ml =
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    82
  (case check_file (thy_path name) of
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    83
    None => error ("Could not find theory file for " ^ quote name)
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    84
  | Some thy_info => thy_info ::
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    85
      (case if ml then check_file (ml_path name) else None of
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    86
        None => []
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    87
      | Some info => [info]));
6205
dd3d3da0f458 added reset_path;
wenzelm
parents: 6168
diff changeset
    88
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    89
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    90
(* process theory files *)
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    91
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    92
(*hooks for theory syntax dependent operations*)
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    93
fun undefined _ = raise Match;
6205
dd3d3da0f458 added reset_path;
wenzelm
parents: 6168
diff changeset
    94
val deps_thy_fn = ref (undefined: string -> bool -> Path.T -> string list * Path.T list);
dd3d3da0f458 added reset_path;
wenzelm
parents: 6168
diff changeset
    95
val load_thy_fn = ref (undefined: string -> bool -> bool -> Path.T -> unit);
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    96
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    97
fun process_thy f name ml =
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    98
  let val master = check_thy name ml
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    99
  in (master, f (#1 (hd master))) end;
6219
b360065c2b07 check_thy: include ML stamp;
wenzelm
parents: 6205
diff changeset
   100
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
   101
fun deps_thy name ml = process_thy (! deps_thy_fn name ml) name ml;
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
   102
fun load_thy name ml time = #1 (process_thy (! load_thy_fn name ml time) name ml);
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   103
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   104
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   105
end;
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   106
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   107
structure BasicThyLoad: BASIC_THY_LOAD = ThyLoad;
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   108
open BasicThyLoad;