src/Pure/Thy/thy_load.ML
author wenzelm
Fri, 28 Mar 2008 00:02:54 +0100
changeset 26455 1757a6e049f4
parent 24189 1fa9852643a3
child 26616 b5fd3ad271ec
permissions -rw-r--r--
reorganized signature of ML_Context;
     1 (*  Title:      Pure/Thy/thy_load.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Theory loader primitives, including load path.
     6 *)
     7 
     8 signature BASIC_THY_LOAD =
     9 sig
    10   val show_path: unit -> string list
    11   val add_path: string -> unit
    12   val path_add: string -> unit
    13   val del_path: string -> unit
    14   val with_path: string -> ('a -> 'b) -> 'a -> 'b
    15   val with_paths: string list -> ('a -> 'b) -> 'a -> 'b
    16   val reset_path: unit -> unit
    17 end;
    18 
    19 signature THY_LOAD =
    20 sig
    21   include BASIC_THY_LOAD
    22   val ml_exts: string list
    23   val ml_path: string -> Path.T
    24   val thy_path: string -> Path.T
    25   val check_file: Path.T -> Path.T -> (Path.T * File.ident) option
    26   val check_ml: Path.T -> Path.T -> (Path.T * File.ident) option
    27   val check_thy: Path.T -> string -> Path.T * File.ident
    28   val deps_thy: Path.T -> string ->
    29    {master: Path.T * File.ident, text: string list, imports: string list, uses: Path.T list}
    30   val load_ml: Path.T -> Path.T -> Path.T * File.ident
    31   val load_thy: Path.T -> string -> Position.T -> string list -> bool -> unit
    32 end;
    33 
    34 signature PRIVATE_THY_LOAD =
    35 sig
    36   include THY_LOAD
    37   (*this backdoor is sealed later*)
    38   val load_thy_fn: (Path.T -> string -> Position.T -> string list -> bool -> unit) ref
    39 end;
    40 
    41 structure ThyLoad: PRIVATE_THY_LOAD =
    42 struct
    43 
    44 
    45 (* maintain load path *)
    46 
    47 local val load_path = ref [Path.current] in
    48 
    49 fun show_path () = map Path.implode (! load_path);
    50 
    51 fun del_path s = CRITICAL (fn () => change load_path (remove (op =) (Path.explode s)));
    52 fun add_path s = CRITICAL (fn () => (del_path s; change load_path (cons (Path.explode s))));
    53 fun path_add s =
    54   CRITICAL (fn () => (del_path s; change load_path (fn path => path @ [Path.explode s])));
    55 fun reset_path () = load_path := [Path.current];
    56 
    57 fun with_paths ss f x =
    58   CRITICAL (fn () => Library.setmp load_path (! load_path @ map Path.explode ss) f x);
    59 fun with_path s f x = with_paths [s] f x;
    60 
    61 fun search_path dir path =
    62   distinct (op =) (dir :: (if Path.is_basic path then (! load_path) else [Path.current]));
    63 
    64 end;
    65 
    66 
    67 (* file formats *)
    68 
    69 val ml_exts = ["ML", "sml"];
    70 val ml_path = Path.ext "ML" o Path.basic;
    71 val thy_path = Path.ext "thy" o Path.basic;
    72 
    73 
    74 (* check files *)
    75 
    76 fun check_ext exts paths dir src_path =
    77   let
    78     val path = Path.expand src_path;
    79     val _ = Path.is_current path andalso error "Bad file specification";
    80 
    81     fun try_ext rel_path ext =
    82       let val ext_path = Path.expand (Path.ext ext rel_path)
    83       in Option.map (fn id => (File.full_path ext_path, id)) (File.ident ext_path) end;
    84     fun try_prfx prfx = get_first (try_ext (Path.append prfx path)) ("" :: exts);
    85   in get_first try_prfx paths end;
    86 
    87 fun check_file dir path = check_ext [] (search_path dir path) dir path;
    88 fun check_ml dir path = check_ext ml_exts (search_path dir path) dir path;
    89 
    90 fun check_thy dir name =
    91   let
    92     val thy_file = thy_path name;
    93     val paths = search_path dir thy_file;
    94   in
    95     (case check_ext [] paths dir thy_file of
    96       NONE => error ("Could not find theory file " ^ quote (Path.implode thy_file) ^
    97         " in " ^ commas_quote (map Path.implode paths))
    98     | SOME thy_id => thy_id)
    99   end;
   100 
   101 
   102 (* theory deps *)
   103 
   104 fun deps_thy dir name =
   105   let
   106     val master as (path, _) = check_thy dir name;
   107     val text = explode (File.read path);
   108     val (name', imports, uses) =
   109       ThyHeader.read (Position.path path) (Source.of_list_limited 8000 text);
   110     val _ = name = name' orelse
   111       error ("Filename " ^ quote (Path.implode (Path.base path)) ^
   112         " does not agree with theory name " ^ quote name');
   113     val uses = map (Path.explode o #1) uses;
   114   in {master = master, text = text, imports = imports, uses = uses} end;
   115 
   116 
   117 (* load files *)
   118 
   119 fun load_ml dir raw_path =
   120   (case check_ml dir raw_path of
   121     NONE => error ("Could not find ML file " ^ quote (Path.implode raw_path))
   122   | SOME (path, id) => (ML_Context.eval_file path; (path, id)));
   123 
   124 (*dependent on outer syntax*)
   125 val load_thy_fn =
   126   ref (undefined: Path.T -> string -> Position.T -> string list -> bool -> unit);
   127 fun load_thy dir name pos text time = ! load_thy_fn dir name pos text time;
   128 
   129 end;
   130 
   131 structure BasicThyLoad: BASIC_THY_LOAD = ThyLoad;
   132 open BasicThyLoad;