src/Pure/Thy/thy_load.ML
author wenzelm
Wed May 24 19:09:36 2000 +0200 (2000-05-24)
changeset 8965 d46b36785c70
parent 8808 204f4ebbba64
child 9103 ef56f093259d
permissions -rw-r--r--
proper token_translation for latex mode;
wenzelm@6168
     1
(*  Title:      Pure/Thy/thy_load.ML
wenzelm@6168
     2
    ID:         $Id$
wenzelm@6168
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@8808
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@6168
     5
wenzelm@6168
     6
Theory loader primitives.
wenzelm@6168
     7
*)
wenzelm@6168
     8
wenzelm@6168
     9
signature BASIC_THY_LOAD =
wenzelm@6168
    10
sig
wenzelm@6168
    11
  val show_path: unit -> string list
wenzelm@6168
    12
  val add_path: string -> unit
wenzelm@6168
    13
  val del_path: string -> unit
wenzelm@7438
    14
  val with_path: string -> ('a -> 'b) -> 'a -> 'b
wenzelm@6205
    15
  val reset_path: unit -> unit
wenzelm@6168
    16
end;
wenzelm@6168
    17
wenzelm@6168
    18
signature THY_LOAD =
wenzelm@6168
    19
sig
wenzelm@6168
    20
  include BASIC_THY_LOAD
wenzelm@7950
    21
  val cond_with_path: Path.T -> ('a -> 'b) -> 'a -> 'b
wenzelm@6205
    22
  val ml_path: string -> Path.T
wenzelm@7901
    23
  val thy_path: string -> Path.T
wenzelm@6232
    24
  val check_file: Path.T -> (Path.T * File.info) option
wenzelm@7940
    25
  val load_file: Path.T -> (Path.T * File.info)
wenzelm@7190
    26
  eqtype master
wenzelm@7190
    27
  val get_thy: master -> Path.T * File.info
wenzelm@7940
    28
  val check_thy: Path.T -> string -> bool -> master
wenzelm@7940
    29
  val deps_thy: Path.T -> string -> bool -> master * (string list * Path.T list)
wenzelm@7190
    30
  val load_thy: Path.T -> string -> bool -> bool -> master
wenzelm@6168
    31
end;
wenzelm@6168
    32
wenzelm@7940
    33
(*this backdoor sealed later*)
wenzelm@6168
    34
signature PRIVATE_THY_LOAD =
wenzelm@6168
    35
sig
wenzelm@6168
    36
  include THY_LOAD
wenzelm@7940
    37
  val deps_thy_fn: (string -> bool -> Path.T -> string list * Path.T list) ref
wenzelm@6205
    38
  val load_thy_fn: (string -> bool -> bool -> Path.T -> unit) ref
wenzelm@6168
    39
end;
wenzelm@6168
    40
wenzelm@6168
    41
structure ThyLoad: PRIVATE_THY_LOAD =
wenzelm@6168
    42
struct
wenzelm@6168
    43
wenzelm@6168
    44
wenzelm@6232
    45
(* maintain load path *)
wenzelm@6168
    46
wenzelm@6168
    47
val load_path = ref [Path.current];
wenzelm@6168
    48
fun change_path f = load_path := f (! load_path);
wenzelm@6168
    49
wenzelm@6168
    50
fun show_path () = map Path.pack (! load_path);
wenzelm@6484
    51
fun add_path s = change_path (cons (Path.unpack s));
wenzelm@6168
    52
fun del_path s = change_path (filter_out (equal (Path.unpack s)));
wenzelm@6205
    53
fun reset_path () = load_path := [Path.current];
wenzelm@7438
    54
fun with_path s f x = Library.setmp load_path (Path.unpack s :: ! load_path) f x;
wenzelm@6168
    55
wenzelm@7438
    56
fun cond_with_path path f x =
wenzelm@7190
    57
  if Path.is_current path then f x else Library.setmp load_path (path :: ! load_path) f x;
wenzelm@7190
    58
wenzelm@6168
    59
wenzelm@6232
    60
(* file formats *)
wenzelm@6168
    61
wenzelm@6232
    62
val ml_exts = ["", "ML", "sml"];
wenzelm@6232
    63
val ml_path = Path.ext "ML" o Path.basic;
wenzelm@6232
    64
val thy_path = Path.ext "thy" o Path.basic;
wenzelm@6168
    65
wenzelm@6168
    66
wenzelm@6232
    67
(* check_file *)
wenzelm@6232
    68
wenzelm@6232
    69
fun check_file src_path =
wenzelm@6232
    70
  let
wenzelm@6232
    71
    val path = Path.expand src_path;
wenzelm@6168
    72
wenzelm@6232
    73
    fun find_ext rel_path ext =
wenzelm@8750
    74
      let val ext_path = Path.expand (Path.ext ext rel_path)
wenzelm@6232
    75
      in apsome (fn info => (File.full_path ext_path, info)) (File.info ext_path) end;
wenzelm@6232
    76
wenzelm@6232
    77
    fun find_dir dir =
wenzelm@6232
    78
      get_first (find_ext (Path.append dir path)) ml_exts;
wenzelm@6232
    79
  in get_first find_dir (if Path.is_basic path then ! load_path else [Path.current]) end;
wenzelm@6232
    80
wenzelm@6232
    81
wenzelm@7940
    82
(* load_file *)
wenzelm@6168
    83
wenzelm@7940
    84
fun load_file raw_path =
wenzelm@6232
    85
  (case check_file raw_path of
wenzelm@6232
    86
    None => error ("Could not find ML file " ^ quote (Path.pack raw_path))
wenzelm@7940
    87
  | Some (path, info) => (File.symbol_use path; (path, info)));
wenzelm@7190
    88
wenzelm@7190
    89
wenzelm@7190
    90
(* datatype master *)
wenzelm@7190
    91
wenzelm@7190
    92
datatype master = Master of (Path.T * File.info) * (Path.T * File.info) option;
wenzelm@7190
    93
wenzelm@7190
    94
fun get_thy (Master (thy, _)) = thy;
wenzelm@6232
    95
wenzelm@6232
    96
wenzelm@7940
    97
(* check / process theory files *)
wenzelm@6168
    98
wenzelm@7940
    99
local
wenzelm@7940
   100
wenzelm@7940
   101
fun check_thy_aux (name, ml) =
wenzelm@6232
   102
  (case check_file (thy_path name) of
wenzelm@6484
   103
    None => error ("Could not find theory file for " ^ quote name ^ " in dir(s) " ^
wenzelm@6484
   104
      commas_quote (show_path ()))
wenzelm@7940
   105
  | Some thy_info => (thy_info, if ml then check_file (ml_path name) else None));
wenzelm@7940
   106
wenzelm@7940
   107
in
wenzelm@7940
   108
wenzelm@7940
   109
fun check_thy dir name ml = Master (cond_with_path dir check_thy_aux (name, ml));
wenzelm@6205
   110
wenzelm@7940
   111
fun process_thy dir f name ml =
wenzelm@7940
   112
  let val master as Master ((path, _), _) = check_thy dir name ml
wenzelm@7940
   113
  in (master, cond_with_path dir f path) end;
wenzelm@7940
   114
wenzelm@7940
   115
end;
wenzelm@6484
   116
wenzelm@6168
   117
wenzelm@7940
   118
(* deps_thy and load_thy *)
wenzelm@6168
   119
wenzelm@6168
   120
(*hooks for theory syntax dependent operations*)
wenzelm@6168
   121
fun undefined _ = raise Match;
wenzelm@7940
   122
val deps_thy_fn = ref (undefined: string -> bool -> Path.T -> string list * Path.T list);
wenzelm@6205
   123
val load_thy_fn = ref (undefined: string -> bool -> bool -> Path.T -> unit);
wenzelm@6168
   124
wenzelm@7940
   125
fun deps_thy dir name ml = process_thy dir (! deps_thy_fn name ml) name ml;
wenzelm@7940
   126
fun load_thy dir name ml time = #1 (process_thy dir (! load_thy_fn name ml time) name ml);
wenzelm@6168
   127
wenzelm@6168
   128
wenzelm@6168
   129
end;
wenzelm@6168
   130
wenzelm@6168
   131
structure BasicThyLoad: BASIC_THY_LOAD = ThyLoad;
wenzelm@6168
   132
open BasicThyLoad;