src/Pure/Thy/thy_load.ML
author wenzelm
Thu Jul 19 23:18:59 2007 +0200 (2007-07-19 ago)
changeset 23872 f449381e2caa
parent 22145 fedad292f738
child 23876 d3b05e7bc5d2
permissions -rw-r--r--
tuned signature;
load_path: always used, even for partially qualified path names;
check_file: precise (no adding of extensions!);
misc cleanup;
wenzelm@6168
     1
(*  Title:      Pure/Thy/thy_load.ML
wenzelm@6168
     2
    ID:         $Id$
wenzelm@6168
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@6168
     4
wenzelm@15801
     5
Theory loader primitives, including load path.
wenzelm@6168
     6
*)
wenzelm@6168
     7
wenzelm@6168
     8
signature BASIC_THY_LOAD =
wenzelm@6168
     9
sig
wenzelm@6168
    10
  val show_path: unit -> string list
wenzelm@6168
    11
  val add_path: string -> unit
wenzelm@10252
    12
  val path_add: string -> unit
wenzelm@6168
    13
  val del_path: string -> unit
wenzelm@7438
    14
  val with_path: string -> ('a -> 'b) -> 'a -> 'b
wenzelm@9103
    15
  val with_paths: string list -> ('a -> 'b) -> 'a -> 'b
wenzelm@6205
    16
  val reset_path: unit -> unit
wenzelm@6168
    17
end;
wenzelm@6168
    18
wenzelm@6168
    19
signature THY_LOAD =
wenzelm@6168
    20
sig
wenzelm@6168
    21
  include BASIC_THY_LOAD
wenzelm@17366
    22
  val ml_exts: string list
wenzelm@6205
    23
  val ml_path: string -> Path.T
wenzelm@7901
    24
  val thy_path: string -> Path.T
wenzelm@23872
    25
  val check_file: Path.T list -> Path.T -> (Path.T * File.ident) option
wenzelm@23872
    26
  val check_ml: Path.T list -> Path.T -> (Path.T * File.ident) option
wenzelm@23872
    27
  val check_thy: Path.T list -> string -> bool ->
wenzelm@23872
    28
    (Path.T * File.ident) * (Path.T * File.ident) option
wenzelm@23872
    29
  val deps_thy: Path.T list -> string -> bool ->
wenzelm@23872
    30
    ((Path.T * File.ident) * (Path.T * File.ident) option) * (string list * Path.T list)
wenzelm@23872
    31
  val load_ml: Path.T list -> Path.T -> Path.T * File.ident
wenzelm@23872
    32
  val load_thy: Path.T list -> string -> bool -> bool ->
wenzelm@23872
    33
    (Path.T * File.ident) * (Path.T * File.ident) option
wenzelm@6168
    34
end;
wenzelm@6168
    35
wenzelm@7940
    36
(*this backdoor sealed later*)
wenzelm@6168
    37
signature PRIVATE_THY_LOAD =
wenzelm@6168
    38
sig
wenzelm@6168
    39
  include THY_LOAD
wenzelm@23872
    40
  val load_thy_fn: (Path.T list -> string -> bool -> bool ->
wenzelm@23872
    41
    (Path.T * File.ident) * (Path.T * File.ident) option) ref
wenzelm@6168
    42
end;
wenzelm@6168
    43
wenzelm@6168
    44
structure ThyLoad: PRIVATE_THY_LOAD =
wenzelm@6168
    45
struct
wenzelm@6168
    46
wenzelm@6168
    47
wenzelm@6232
    48
(* maintain load path *)
wenzelm@6168
    49
wenzelm@6168
    50
val load_path = ref [Path.current];
wenzelm@6168
    51
wenzelm@21858
    52
fun show_path () = map Path.implode (! load_path);
wenzelm@23872
    53
fun del_path s = change load_path (remove (op =) (Path.explode s));
wenzelm@23872
    54
fun add_path s = (del_path s; change load_path (cons (Path.explode s)));
wenzelm@23872
    55
fun path_add s = (del_path s; change load_path (fn path => path @ [Path.explode s]));
wenzelm@6205
    56
fun reset_path () = load_path := [Path.current];
wenzelm@21858
    57
fun with_paths ss f x = Library.setmp load_path (! load_path @ map Path.explode ss) f x;
wenzelm@9655
    58
fun with_path s f x = with_paths [s] f x;
wenzelm@6168
    59
wenzelm@6168
    60
wenzelm@6232
    61
(* file formats *)
wenzelm@6168
    62
wenzelm@23872
    63
val ml_exts = ["ML", "sml"];
wenzelm@6232
    64
val ml_path = Path.ext "ML" o Path.basic;
wenzelm@6232
    65
val thy_path = Path.ext "thy" o Path.basic;
wenzelm@6168
    66
wenzelm@6168
    67
wenzelm@23872
    68
(* check files *)
wenzelm@6232
    69
wenzelm@23872
    70
fun augment_path (ps: Path.T list) qs = distinct (op =) (ps @ qs);
wenzelm@23872
    71
wenzelm@23872
    72
fun check_ext exts dirs src_path =
wenzelm@6232
    73
  let
wenzelm@6232
    74
    val path = Path.expand src_path;
wenzelm@23872
    75
    val _ = Path.is_current path andalso error "Bad file specification";
wenzelm@23872
    76
    val paths = augment_path dirs (! load_path);
wenzelm@6168
    77
wenzelm@6232
    78
    fun find_ext rel_path ext =
wenzelm@8750
    79
      let val ext_path = Path.expand (Path.ext ext rel_path)
wenzelm@23872
    80
      in Option.map (fn id => (File.full_path ext_path, id)) (File.ident ext_path) end;
wenzelm@23872
    81
    fun find_dir dir = get_first (find_ext (Path.append dir path)) ("" :: exts);
wenzelm@13533
    82
  in get_first find_dir paths end;
wenzelm@6232
    83
wenzelm@23872
    84
val check_file = check_ext [];
wenzelm@23872
    85
val check_ml = check_ext ml_exts;
wenzelm@6168
    86
wenzelm@23872
    87
fun check_thy dirs name ml =
wenzelm@23872
    88
  let val thy_file = thy_path name in
wenzelm@23872
    89
    (case check_file dirs thy_file of
wenzelm@23872
    90
      NONE => error ("Could not find theory file " ^ quote (Path.implode thy_file) ^
wenzelm@23872
    91
        " in dir(s) " ^ commas_quote (map Path.implode (augment_path dirs (! load_path))))
wenzelm@23872
    92
    | SOME thy_id => (thy_id, if ml then check_file dirs (ml_path name) else NONE))
wenzelm@23872
    93
  end;
wenzelm@6232
    94
wenzelm@6232
    95
wenzelm@23872
    96
(* theory deps *)
wenzelm@7940
    97
wenzelm@23872
    98
fun deps_thy dirs name ml =
wenzelm@23872
    99
  let
wenzelm@23872
   100
    val master as ((path, _), _) = check_thy dirs name ml;
wenzelm@23872
   101
    val (name', imports, uses) =
wenzelm@23872
   102
      ThyHeader.read (Position.path path) (Source.of_string (File.read path));
wenzelm@23872
   103
    val _ = name = name' orelse
wenzelm@23872
   104
      error ("Filename " ^ quote (Path.implode (Path.base path)) ^
wenzelm@23872
   105
        " does not agree with theory name " ^ quote name');
wenzelm@23872
   106
    val ml_file =
wenzelm@23872
   107
      if ml andalso is_some (check_file [] (ml_path name))
wenzelm@23872
   108
      then [ml_path name] else [];
wenzelm@23872
   109
  in (master, (imports, map (Path.explode o #1) uses @ ml_file)) end;
wenzelm@6484
   110
wenzelm@6168
   111
wenzelm@23872
   112
(* load files *)
wenzelm@6168
   113
wenzelm@23872
   114
fun load_ml dirs raw_path =
wenzelm@23872
   115
  (case check_ml dirs raw_path of
wenzelm@23872
   116
    NONE => error ("Could not find ML file " ^ quote (Path.implode raw_path))
wenzelm@23872
   117
  | SOME (path, id) => (ML_Context.use path; (path, id)));
wenzelm@6168
   118
wenzelm@23872
   119
(*dependent on outer syntax*)
wenzelm@23872
   120
val load_thy_fn = ref (undefined: Path.T list -> string -> bool -> bool ->
wenzelm@23872
   121
  (Path.T * File.ident) * (Path.T * File.ident) option);
wenzelm@23872
   122
fun load_thy dirs name ml time = ! load_thy_fn dirs name ml time;
wenzelm@6168
   123
wenzelm@6168
   124
end;
wenzelm@6168
   125
wenzelm@6168
   126
structure BasicThyLoad: BASIC_THY_LOAD = ThyLoad;
wenzelm@6168
   127
open BasicThyLoad;