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