src/Pure/Thy/thy_load.ML
author wenzelm
Thu, 19 Jul 2007 23:18:59 +0200
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;
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
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15570
diff changeset
     5
Theory loader primitives, including load path.
6168
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
10252
dd46544e259d added path_add;
wenzelm
parents: 9696
diff changeset
    12
  val path_add: string -> unit
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    13
  val del_path: string -> unit
7438
2e0e4253b6c3 with_path;
wenzelm
parents: 7190
diff changeset
    14
  val with_path: string -> ('a -> 'b) -> 'a -> 'b
9103
ef56f093259d added with_paths;
wenzelm
parents: 8808
diff changeset
    15
  val with_paths: string list -> ('a -> 'b) -> 'a -> 'b
6205
dd3d3da0f458 added reset_path;
wenzelm
parents: 6168
diff changeset
    16
  val reset_path: unit -> unit
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    17
end;
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    18
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    19
signature THY_LOAD =
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    20
sig
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    21
  include BASIC_THY_LOAD
17366
325707c676e2 export ml_exts;
wenzelm
parents: 16269
diff changeset
    22
  val ml_exts: string list
6205
dd3d3da0f458 added reset_path;
wenzelm
parents: 6168
diff changeset
    23
  val ml_path: string -> Path.T
7901
c21c3d2256ef export thy_path;
wenzelm
parents: 7438
diff changeset
    24
  val thy_path: string -> Path.T
23872
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    25
  val check_file: Path.T list -> Path.T -> (Path.T * File.ident) option
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    26
  val check_ml: Path.T list -> Path.T -> (Path.T * File.ident) option
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    27
  val check_thy: Path.T list -> string -> bool ->
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    28
    (Path.T * File.ident) * (Path.T * File.ident) option
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    29
  val deps_thy: Path.T list -> string -> bool ->
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    30
    ((Path.T * File.ident) * (Path.T * File.ident) option) * (string list * Path.T list)
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    31
  val load_ml: Path.T list -> Path.T -> Path.T * File.ident
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    32
  val load_thy: Path.T list -> string -> bool -> bool ->
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    33
    (Path.T * File.ident) * (Path.T * File.ident) option
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    34
end;
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    35
7940
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
    36
(*this backdoor sealed later*)
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    37
signature PRIVATE_THY_LOAD =
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    38
sig
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    39
  include THY_LOAD
23872
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    40
  val load_thy_fn: (Path.T list -> string -> bool -> bool ->
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    41
    (Path.T * File.ident) * (Path.T * File.ident) option) ref
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    42
end;
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    43
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    44
structure ThyLoad: PRIVATE_THY_LOAD =
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    45
struct
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    46
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    47
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    48
(* maintain load path *)
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    49
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    50
val load_path = ref [Path.current];
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    51
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 17366
diff changeset
    52
fun show_path () = map Path.implode (! load_path);
23872
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    53
fun del_path s = change load_path (remove (op =) (Path.explode s));
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    54
fun add_path s = (del_path s; change load_path (cons (Path.explode s)));
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    55
fun path_add s = (del_path s; change load_path (fn path => path @ [Path.explode s]));
6205
dd3d3da0f458 added reset_path;
wenzelm
parents: 6168
diff changeset
    56
fun reset_path () = load_path := [Path.current];
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 17366
diff changeset
    57
fun with_paths ss f x = Library.setmp load_path (! load_path @ map Path.explode ss) f x;
9655
a4d2da014ec3 renamed cond_with_path to cond_add_path (add to front);
wenzelm
parents: 9103
diff changeset
    58
fun with_path s f x = with_paths [s] f x;
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    59
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    60
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    61
(* file formats *)
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    62
23872
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    63
val ml_exts = ["ML", "sml"];
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    64
val ml_path = Path.ext "ML" o Path.basic;
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    65
val thy_path = Path.ext "thy" o Path.basic;
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    66
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    67
23872
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    68
(* check files *)
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    69
23872
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    70
fun augment_path (ps: Path.T list) qs = distinct (op =) (ps @ qs);
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    71
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    72
fun check_ext exts dirs src_path =
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    73
  let
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    74
    val path = Path.expand src_path;
23872
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    75
    val _ = Path.is_current path andalso error "Bad file specification";
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    76
    val paths = augment_path dirs (! load_path);
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    77
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    78
    fun find_ext rel_path ext =
8750
36b165788421 check_file: keep expanded (!) absolute path;
wenzelm
parents: 7950
diff changeset
    79
      let val ext_path = Path.expand (Path.ext ext rel_path)
23872
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    80
      in Option.map (fn id => (File.full_path ext_path, id)) (File.ident ext_path) end;
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    81
    fun find_dir dir = get_first (find_ext (Path.append dir path)) ("" :: exts);
13533
70de987e9fe3 check_file: disallow current dir (typically "");
wenzelm
parents: 12120
diff changeset
    82
  in get_first find_dir paths end;
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    83
23872
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    84
val check_file = check_ext [];
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    85
val check_ml = check_ext ml_exts;
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    86
23872
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    87
fun check_thy dirs name ml =
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    88
  let val thy_file = thy_path name in
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    89
    (case check_file dirs thy_file of
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    90
      NONE => error ("Could not find theory file " ^ quote (Path.implode thy_file) ^
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    91
        " in dir(s) " ^ commas_quote (map Path.implode (augment_path dirs (! load_path))))
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    92
    | SOME thy_id => (thy_id, if ml then check_file dirs (ml_path name) else NONE))
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    93
  end;
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
23872
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    96
(* theory deps *)
7940
def6db239934 improved ml handling;
wenzelm
parents: 7901
diff changeset
    97
23872
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    98
fun deps_thy dirs name ml =
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    99
  let
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
   100
    val master as ((path, _), _) = check_thy dirs name ml;
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
   101
    val (name', imports, uses) =
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
   102
      ThyHeader.read (Position.path path) (Source.of_string (File.read path));
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
   103
    val _ = name = name' orelse
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
   104
      error ("Filename " ^ quote (Path.implode (Path.base path)) ^
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
   105
        " does not agree with theory name " ^ quote name');
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
   106
    val ml_file =
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
   107
      if ml andalso is_some (check_file [] (ml_path name))
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
   108
      then [ml_path name] else [];
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
   109
  in (master, (imports, map (Path.explode o #1) uses @ ml_file)) end;
6484
3f098b0ec683 use_thy etc.: may specify path prefix, which is temporarily used as load path;
wenzelm
parents: 6363
diff changeset
   110
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   111
23872
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
   112
(* load files *)
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   113
23872
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
   114
fun load_ml dirs raw_path =
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
   115
  (case check_ml dirs raw_path of
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
   116
    NONE => error ("Could not find ML file " ^ quote (Path.implode raw_path))
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
   117
  | SOME (path, id) => (ML_Context.use path; (path, id)));
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   118
23872
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
   119
(*dependent on outer syntax*)
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
   120
val load_thy_fn = ref (undefined: Path.T list -> string -> bool -> bool ->
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
   121
  (Path.T * File.ident) * (Path.T * File.ident) option);
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
   122
fun load_thy dirs name ml time = ! load_thy_fn dirs name ml time;
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   123
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   124
end;
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   125
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   126
structure BasicThyLoad: BASIC_THY_LOAD = ThyLoad;
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   127
open BasicThyLoad;