src/Pure/PIDE/resources.ML
author wenzelm
Sat, 30 Mar 2019 20:54:47 +0100
changeset 70015 c8e08d8ffb93
parent 69851 29a4f633609e
child 70049 c1226e4c273e
permissions -rw-r--r--
clarified signature: more explicit type Path.binding; tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 56204
diff changeset
     1
(*  Title:      Pure/PIDE/resources.ML
42002
wenzelm
parents: 41955
diff changeset
     2
    Author:     Makarius
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
     3
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 56204
diff changeset
     4
Resources for theories and auxiliary files.
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
     5
*)
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
     6
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 56204
diff changeset
     7
signature RESOURCES =
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
     8
sig
65532
febfd9f78bd4 eliminated default_qualifier: just a constant;
wenzelm
parents: 65505
diff changeset
     9
  val default_qualifier: string
65478
7c40477e0a87 clarified init_session_base / finish_session_base: retain some information for plain "isabelle process", without rechecking dependencies as in "isabelle console";
wenzelm
parents: 65476
diff changeset
    10
  val init_session_base:
67493
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
    11
    {sessions: (string * Properties.T) list,
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
    12
     docs: string list,
67219
81e9804b2014 added document antiquotation @{session name};
wenzelm
parents: 67217
diff changeset
    13
     global_theories: (string * string) list,
66712
4c98c929a12a session-qualified theory names are mandatory;
wenzelm
parents: 66711
diff changeset
    14
     loaded_theories: string list,
65441
9425e4d8bdb6 more session_base information in ML;
wenzelm
parents: 65433
diff changeset
    15
     known_theories: (string * string) list} -> unit
65478
7c40477e0a87 clarified init_session_base / finish_session_base: retain some information for plain "isabelle process", without rechecking dependencies as in "isabelle console";
wenzelm
parents: 65476
diff changeset
    16
  val finish_session_base: unit -> unit
65457
2bf0d2fcd506 proper import qualifier for global theories;
wenzelm
parents: 65454
diff changeset
    17
  val global_theory: string -> string option
66712
4c98c929a12a session-qualified theory names are mandatory;
wenzelm
parents: 66711
diff changeset
    18
  val loaded_theory: string -> bool
65431
4a3e6cda3b94 provide session base for "isabelle build" and "isabelle console" ML process;
wenzelm
parents: 65058
diff changeset
    19
  val known_theory: string -> Path.T option
67219
81e9804b2014 added document antiquotation @{session name};
wenzelm
parents: 67217
diff changeset
    20
  val check_session: Proof.context -> string * Position.T -> string
67471
bddfa23a4ea9 formal treatment of documentation names;
wenzelm
parents: 67463
diff changeset
    21
  val check_doc: Proof.context -> string * Position.T -> string
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    22
  val master_directory: theory -> Path.T
48927
ef462b5558eb theory def/ref position reports, which enable hyperlinks etc.;
wenzelm
parents: 48924
diff changeset
    23
  val imports_of: theory -> (string * Position.T) list
65505
741fad555d82 exclude theories from other sessions;
wenzelm
parents: 65503
diff changeset
    24
  val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44478
diff changeset
    25
  val thy_path: Path.T -> Path.T
65445
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65443
diff changeset
    26
  val theory_qualifier: string -> string
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents: 67214
diff changeset
    27
  val import_name: string -> Path.T -> string ->
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents: 67214
diff changeset
    28
    {node_name: Path.T, master_dir: Path.T, theory_name: string}
48898
9fc880720663 simplified Thy_Load.check_thy (again) -- no need to pass keywords nor find files in body text;
wenzelm
parents: 48897
diff changeset
    29
  val check_thy: Path.T -> string ->
48928
698fb0e27b11 more accurate defining position of theory;
wenzelm
parents: 48927
diff changeset
    30
   {master: Path.T * SHA1.digest, text: string, theory_pos: Position.T,
51293
05b1bbae748d discontinued obsolete 'uses' within theory header;
wenzelm
parents: 51273
diff changeset
    31
    imports: (string * Position.T) list, keywords: Thy_Header.keywords}
54525
wenzelm
parents: 54523
diff changeset
    32
  val parse_files: string -> (theory -> Token.file list) parser
48906
5b192d6b7a54 tuned signature;
wenzelm
parents: 48905
diff changeset
    33
  val provide: Path.T * SHA1.digest -> theory -> theory
69851
29a4f633609e clarified signature;
wenzelm
parents: 69592
diff changeset
    34
  val provide_file: Token.file -> theory -> theory
48906
5b192d6b7a54 tuned signature;
wenzelm
parents: 48905
diff changeset
    35
  val provide_parse_files: string -> (theory -> Token.file list * theory) parser
54446
31884c67d73a tuned signature;
wenzelm
parents: 53403
diff changeset
    36
  val loaded_files_current: theory -> bool
67209
fca5f2988091 clarified signature;
wenzelm
parents: 67147
diff changeset
    37
  val check_path: Proof.context -> Path.T -> string * Position.T -> Path.T
fca5f2988091 clarified signature;
wenzelm
parents: 67147
diff changeset
    38
  val check_file: Proof.context -> Path.T -> string * Position.T -> Path.T
fca5f2988091 clarified signature;
wenzelm
parents: 67147
diff changeset
    39
  val check_dir: Proof.context -> Path.T -> string * Position.T -> Path.T
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    40
end;
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    41
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 56204
diff changeset
    42
structure Resources: RESOURCES =
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    43
struct
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    44
65431
4a3e6cda3b94 provide session base for "isabelle build" and "isabelle console" ML process;
wenzelm
parents: 65058
diff changeset
    45
(* session base *)
4a3e6cda3b94 provide session base for "isabelle build" and "isabelle console" ML process;
wenzelm
parents: 65058
diff changeset
    46
65532
febfd9f78bd4 eliminated default_qualifier: just a constant;
wenzelm
parents: 65505
diff changeset
    47
val default_qualifier = "Draft";
febfd9f78bd4 eliminated default_qualifier: just a constant;
wenzelm
parents: 65505
diff changeset
    48
67493
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
    49
type entry = {pos: Position.T, serial: serial};
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
    50
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
    51
fun make_entry props : entry =
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
    52
  {pos = Position.of_properties props, serial = serial ()};
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
    53
65478
7c40477e0a87 clarified init_session_base / finish_session_base: retain some information for plain "isabelle process", without rechecking dependencies as in "isabelle console";
wenzelm
parents: 65476
diff changeset
    54
val empty_session_base =
67493
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
    55
  {sessions = []: (string * entry) list,
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
    56
   docs = []: (string * entry) list,
67219
81e9804b2014 added document antiquotation @{session name};
wenzelm
parents: 67217
diff changeset
    57
   global_theories = Symtab.empty: string Symtab.table,
66712
4c98c929a12a session-qualified theory names are mandatory;
wenzelm
parents: 66711
diff changeset
    58
   loaded_theories = Symtab.empty: unit Symtab.table,
65441
9425e4d8bdb6 more session_base information in ML;
wenzelm
parents: 65433
diff changeset
    59
   known_theories = Symtab.empty: Path.T Symtab.table};
65431
4a3e6cda3b94 provide session base for "isabelle build" and "isabelle console" ML process;
wenzelm
parents: 65058
diff changeset
    60
65441
9425e4d8bdb6 more session_base information in ML;
wenzelm
parents: 65433
diff changeset
    61
val global_session_base =
65478
7c40477e0a87 clarified init_session_base / finish_session_base: retain some information for plain "isabelle process", without rechecking dependencies as in "isabelle console";
wenzelm
parents: 65476
diff changeset
    62
  Synchronized.var "Sessions.base" empty_session_base;
65441
9425e4d8bdb6 more session_base information in ML;
wenzelm
parents: 65433
diff changeset
    63
67493
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
    64
fun init_session_base {sessions, docs, global_theories, loaded_theories, known_theories} =
65431
4a3e6cda3b94 provide session base for "isabelle build" and "isabelle console" ML process;
wenzelm
parents: 65058
diff changeset
    65
  Synchronized.change global_session_base
65441
9425e4d8bdb6 more session_base information in ML;
wenzelm
parents: 65433
diff changeset
    66
    (fn _ =>
67493
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
    67
      {sessions = sort_by #1 (map (apsnd make_entry) sessions),
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
    68
       docs = sort_by #1 (map (apsnd make_entry o rpair []) docs),
67219
81e9804b2014 added document antiquotation @{session name};
wenzelm
parents: 67217
diff changeset
    69
       global_theories = Symtab.make global_theories,
66712
4c98c929a12a session-qualified theory names are mandatory;
wenzelm
parents: 66711
diff changeset
    70
       loaded_theories = Symtab.make_set loaded_theories,
65441
9425e4d8bdb6 more session_base information in ML;
wenzelm
parents: 65433
diff changeset
    71
       known_theories = Symtab.make (map (apsnd Path.explode) known_theories)});
65431
4a3e6cda3b94 provide session base for "isabelle build" and "isabelle console" ML process;
wenzelm
parents: 65058
diff changeset
    72
65478
7c40477e0a87 clarified init_session_base / finish_session_base: retain some information for plain "isabelle process", without rechecking dependencies as in "isabelle console";
wenzelm
parents: 65476
diff changeset
    73
fun finish_session_base () =
7c40477e0a87 clarified init_session_base / finish_session_base: retain some information for plain "isabelle process", without rechecking dependencies as in "isabelle console";
wenzelm
parents: 65476
diff changeset
    74
  Synchronized.change global_session_base
7c40477e0a87 clarified init_session_base / finish_session_base: retain some information for plain "isabelle process", without rechecking dependencies as in "isabelle console";
wenzelm
parents: 65476
diff changeset
    75
    (fn {global_theories, loaded_theories, ...} =>
67219
81e9804b2014 added document antiquotation @{session name};
wenzelm
parents: 67217
diff changeset
    76
      {sessions = [],
67493
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
    77
       docs = [],
67219
81e9804b2014 added document antiquotation @{session name};
wenzelm
parents: 67217
diff changeset
    78
       global_theories = global_theories,
65478
7c40477e0a87 clarified init_session_base / finish_session_base: retain some information for plain "isabelle process", without rechecking dependencies as in "isabelle console";
wenzelm
parents: 65476
diff changeset
    79
       loaded_theories = loaded_theories,
7c40477e0a87 clarified init_session_base / finish_session_base: retain some information for plain "isabelle process", without rechecking dependencies as in "isabelle console";
wenzelm
parents: 65476
diff changeset
    80
       known_theories = #known_theories empty_session_base});
65441
9425e4d8bdb6 more session_base information in ML;
wenzelm
parents: 65433
diff changeset
    81
9425e4d8bdb6 more session_base information in ML;
wenzelm
parents: 65433
diff changeset
    82
fun get_session_base f = f (Synchronized.value global_session_base);
65431
4a3e6cda3b94 provide session base for "isabelle build" and "isabelle console" ML process;
wenzelm
parents: 65058
diff changeset
    83
65457
2bf0d2fcd506 proper import qualifier for global theories;
wenzelm
parents: 65454
diff changeset
    84
fun global_theory a = Symtab.lookup (get_session_base #global_theories) a;
66712
4c98c929a12a session-qualified theory names are mandatory;
wenzelm
parents: 66711
diff changeset
    85
fun loaded_theory a = Symtab.defined (get_session_base #loaded_theories) a;
65441
9425e4d8bdb6 more session_base information in ML;
wenzelm
parents: 65433
diff changeset
    86
fun known_theory a = Symtab.lookup (get_session_base #known_theories) a;
65431
4a3e6cda3b94 provide session base for "isabelle build" and "isabelle console" ML process;
wenzelm
parents: 65058
diff changeset
    87
67471
bddfa23a4ea9 formal treatment of documentation names;
wenzelm
parents: 67463
diff changeset
    88
fun check_name which kind markup ctxt (name, pos) =
67493
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
    89
  let val entries = get_session_base which in
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
    90
    (case AList.lookup (op =) entries name of
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
    91
      SOME entry => (Context_Position.report ctxt pos (markup name entry); name)
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
    92
    | NONE =>
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
    93
        let
69289
bf6937af7fe8 clarified signature;
wenzelm
parents: 69282
diff changeset
    94
          val completion_report =
bf6937af7fe8 clarified signature;
wenzelm
parents: 69282
diff changeset
    95
            Completion.make_report (name, pos)
bf6937af7fe8 clarified signature;
wenzelm
parents: 69282
diff changeset
    96
              (fn completed =>
67493
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
    97
                entries
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
    98
                |> map #1
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
    99
                |> filter completed
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
   100
                |> sort_strings
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
   101
                |> map (fn a => (a, (kind, a))));
69289
bf6937af7fe8 clarified signature;
wenzelm
parents: 69282
diff changeset
   102
        in error ("Bad " ^ kind ^ " " ^ quote name ^ Position.here pos ^ completion_report) end)
67219
81e9804b2014 added document antiquotation @{session name};
wenzelm
parents: 67217
diff changeset
   103
  end;
81e9804b2014 added document antiquotation @{session name};
wenzelm
parents: 67217
diff changeset
   104
67493
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
   105
fun markup_session name {pos, serial} =
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
   106
  Markup.properties
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
   107
    (Position.entity_properties_of false serial pos) (Markup.entity Markup.sessionN name);
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
   108
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
   109
val check_session = check_name #sessions "session" markup_session;
c4e9e0c50487 treat sessions as entities with defining position;
wenzelm
parents: 67473
diff changeset
   110
val check_doc = check_name #docs "documentation" (fn name => fn _ => Markup.doc name);
67471
bddfa23a4ea9 formal treatment of documentation names;
wenzelm
parents: 67463
diff changeset
   111
bddfa23a4ea9 formal treatment of documentation names;
wenzelm
parents: 67463
diff changeset
   112
65431
4a3e6cda3b94 provide session base for "isabelle build" and "isabelle console" ML process;
wenzelm
parents: 65058
diff changeset
   113
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   114
(* manage source files *)
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   115
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   116
type files =
40741
17d6293a1e26 moved file identification to thy_load.ML (where it is actually used);
wenzelm
parents: 40625
diff changeset
   117
 {master_dir: Path.T,  (*master directory of theory source*)
48927
ef462b5558eb theory def/ref position reports, which enable hyperlinks etc.;
wenzelm
parents: 48924
diff changeset
   118
  imports: (string * Position.T) list,  (*source specification of imports*)
48896
bb1f461a7815 simplified Thy_Load.provide: do not store full path;
wenzelm
parents: 48888
diff changeset
   119
  provided: (Path.T * SHA1.digest) list};  (*source path, digest*)
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   120
48886
9604c6563226 discontinued separate list of required files -- maintain only provided files as they occur at runtime;
wenzelm
parents: 48881
diff changeset
   121
fun make_files (master_dir, imports, provided): files =
9604c6563226 discontinued separate list of required files -- maintain only provided files as they occur at runtime;
wenzelm
parents: 48881
diff changeset
   122
 {master_dir = master_dir, imports = imports, provided = provided};
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   123
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   124
structure Files = Theory_Data
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   125
(
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   126
  type T = files;
48886
9604c6563226 discontinued separate list of required files -- maintain only provided files as they occur at runtime;
wenzelm
parents: 48881
diff changeset
   127
  val empty = make_files (Path.current, [], []);
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   128
  fun extend _ = empty;
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   129
  fun merge _ = empty;
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   130
);
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   131
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   132
fun map_files f =
48886
9604c6563226 discontinued separate list of required files -- maintain only provided files as they occur at runtime;
wenzelm
parents: 48881
diff changeset
   133
  Files.map (fn {master_dir, imports, provided} =>
9604c6563226 discontinued separate list of required files -- maintain only provided files as they occur at runtime;
wenzelm
parents: 48881
diff changeset
   134
    make_files (f (master_dir, imports, provided)));
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   135
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   136
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   137
val master_directory = #master_dir o Files.get;
41548
bd0bebf93fa6 Thy_Load.begin_theory: maintain source specification of imports;
wenzelm
parents: 41414
diff changeset
   138
val imports_of = #imports o Files.get;
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   139
65505
741fad555d82 exclude theories from other sessions;
wenzelm
parents: 65503
diff changeset
   140
fun begin_theory master_dir {name, imports, keywords} parents =
741fad555d82 exclude theories from other sessions;
wenzelm
parents: 65503
diff changeset
   141
  Theory.begin_theory name parents
69238
d98cfb369cbd prefer symbolic path: it is potentially persisted;
wenzelm
parents: 68090
diff changeset
   142
  |> map_files (fn _ => (Path.explode (Path.smart_implode master_dir), imports, []))
65505
741fad555d82 exclude theories from other sessions;
wenzelm
parents: 65503
diff changeset
   143
  |> Thy_Header.add_keywords keywords;
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   144
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   145
54525
wenzelm
parents: 54523
diff changeset
   146
(* theory files *)
wenzelm
parents: 54523
diff changeset
   147
wenzelm
parents: 54523
diff changeset
   148
val thy_path = Path.ext "thy";
48869
wenzelm
parents: 48868
diff changeset
   149
65445
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65443
diff changeset
   150
fun theory_qualifier theory =
65457
2bf0d2fcd506 proper import qualifier for global theories;
wenzelm
parents: 65454
diff changeset
   151
  (case global_theory theory of
2bf0d2fcd506 proper import qualifier for global theories;
wenzelm
parents: 65454
diff changeset
   152
    SOME qualifier => qualifier
2bf0d2fcd506 proper import qualifier for global theories;
wenzelm
parents: 65454
diff changeset
   153
  | NONE => Long_Name.qualifier theory);
65445
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65443
diff changeset
   154
66712
4c98c929a12a session-qualified theory names are mandatory;
wenzelm
parents: 66711
diff changeset
   155
fun theory_name qualifier theory =
67104
a2fa0c6a7aff clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents: 66771
diff changeset
   156
  if Long_Name.is_qualified theory orelse is_some (global_theory theory)
a2fa0c6a7aff clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents: 66771
diff changeset
   157
  then theory
a2fa0c6a7aff clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents: 66771
diff changeset
   158
  else Long_Name.qualify qualifier theory;
65471
05e5bffcf1d8 clarified loaded_theories: map to qualified theory name;
wenzelm
parents: 65457
diff changeset
   159
65454
2b22b7d8649f clarified, according to Scala version;
wenzelm
parents: 65445
diff changeset
   160
fun import_name qualifier dir s =
67104
a2fa0c6a7aff clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents: 66771
diff changeset
   161
  let val theory = theory_name qualifier (Thy_Header.import_name s) in
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents: 67214
diff changeset
   162
    if loaded_theory theory
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents: 67214
diff changeset
   163
    then {node_name = Path.basic theory, master_dir = Path.current, theory_name = theory}
67104
a2fa0c6a7aff clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents: 66771
diff changeset
   164
    else
a2fa0c6a7aff clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents: 66771
diff changeset
   165
      let
a2fa0c6a7aff clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents: 66771
diff changeset
   166
        val node_name =
a2fa0c6a7aff clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents: 66771
diff changeset
   167
          (case known_theory theory of
a2fa0c6a7aff clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents: 66771
diff changeset
   168
            SOME node_name => node_name
a2fa0c6a7aff clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents: 66771
diff changeset
   169
          | NONE =>
a2fa0c6a7aff clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents: 66771
diff changeset
   170
              if Thy_Header.is_base_name s andalso Long_Name.is_qualified s
a2fa0c6a7aff clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents: 66771
diff changeset
   171
              then Path.explode s
a2fa0c6a7aff clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents: 66771
diff changeset
   172
              else File.full_path dir (thy_path (Path.expand (Path.explode s))));
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents: 67214
diff changeset
   173
      in {node_name = node_name, master_dir = Path.dir node_name, theory_name = theory} end
67104
a2fa0c6a7aff clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents: 66771
diff changeset
   174
  end;
65443
dccbfc715904 provide Resources.import_name in ML, similar to Scala version;
wenzelm
parents: 65442
diff changeset
   175
48878
5e850e6fa3c3 tuned errors;
wenzelm
parents: 48877
diff changeset
   176
fun check_file dir file = File.check_file (File.full_path dir file);
5e850e6fa3c3 tuned errors;
wenzelm
parents: 48877
diff changeset
   177
54525
wenzelm
parents: 54523
diff changeset
   178
fun check_thy dir thy_name =
wenzelm
parents: 54523
diff changeset
   179
  let
65433
a260181505c1 refer to known_theory;
wenzelm
parents: 65431
diff changeset
   180
    val thy_base_name = Long_Name.base_name thy_name;
a260181505c1 refer to known_theory;
wenzelm
parents: 65431
diff changeset
   181
    val master_file =
a260181505c1 refer to known_theory;
wenzelm
parents: 65431
diff changeset
   182
      (case known_theory thy_name of
65442
1ca6d8a2a00d clarified;
wenzelm
parents: 65441
diff changeset
   183
        SOME known_path => check_file Path.current known_path
1ca6d8a2a00d clarified;
wenzelm
parents: 65441
diff changeset
   184
      | NONE => check_file dir (thy_path (Path.basic thy_base_name)));
54525
wenzelm
parents: 54523
diff changeset
   185
    val text = File.read master_file;
wenzelm
parents: 54523
diff changeset
   186
wenzelm
parents: 54523
diff changeset
   187
    val {name = (name, pos), imports, keywords} =
wenzelm
parents: 54523
diff changeset
   188
      Thy_Header.read (Path.position master_file) text;
65433
a260181505c1 refer to known_theory;
wenzelm
parents: 65431
diff changeset
   189
    val _ =
a260181505c1 refer to known_theory;
wenzelm
parents: 65431
diff changeset
   190
      thy_base_name <> name andalso
a260181505c1 refer to known_theory;
wenzelm
parents: 65431
diff changeset
   191
        error ("Bad theory name " ^ quote name ^
65442
1ca6d8a2a00d clarified;
wenzelm
parents: 65441
diff changeset
   192
          " for file " ^ Path.print (Path.base master_file) ^ Position.here pos);
54525
wenzelm
parents: 54523
diff changeset
   193
  in
wenzelm
parents: 54523
diff changeset
   194
   {master = (master_file, SHA1.digest text), text = text, theory_pos = pos,
wenzelm
parents: 54523
diff changeset
   195
    imports = imports, keywords = keywords}
wenzelm
parents: 54523
diff changeset
   196
  end;
wenzelm
parents: 54523
diff changeset
   197
wenzelm
parents: 54523
diff changeset
   198
54526
92961f196d9e load files that are not provided by PIDE blobs;
wenzelm
parents: 54525
diff changeset
   199
(* load files *)
48898
9fc880720663 simplified Thy_Load.check_thy (again) -- no need to pass keywords nor find files in body text;
wenzelm
parents: 48897
diff changeset
   200
9fc880720663 simplified Thy_Load.check_thy (again) -- no need to pass keywords nor find files in body text;
wenzelm
parents: 48897
diff changeset
   201
fun parse_files cmd =
9fc880720663 simplified Thy_Load.check_thy (again) -- no need to pass keywords nor find files in body text;
wenzelm
parents: 48897
diff changeset
   202
  Scan.ahead Parse.not_eof -- Parse.path >> (fn (tok, name) => fn thy =>
9fc880720663 simplified Thy_Load.check_thy (again) -- no need to pass keywords nor find files in body text;
wenzelm
parents: 48897
diff changeset
   203
    (case Token.get_files tok of
54526
92961f196d9e load files that are not provided by PIDE blobs;
wenzelm
parents: 54525
diff changeset
   204
      [] =>
92961f196d9e load files that are not provided by PIDE blobs;
wenzelm
parents: 54525
diff changeset
   205
        let
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58923
diff changeset
   206
          val keywords = Thy_Header.get_keywords thy;
54526
92961f196d9e load files that are not provided by PIDE blobs;
wenzelm
parents: 54525
diff changeset
   207
          val master_dir = master_directory thy;
55708
f4b114070675 tuned signature;
wenzelm
parents: 55614
diff changeset
   208
          val pos = Token.pos_of tok;
58923
cb9b69cca999 more explicit Keyword.keywords;
wenzelm
parents: 58918
diff changeset
   209
          val src_paths = Keyword.command_files keywords cmd (Path.explode name);
54526
92961f196d9e load files that are not provided by PIDE blobs;
wenzelm
parents: 54525
diff changeset
   210
        in map (Command.read_file master_dir pos) src_paths end
54520
cee77d2e9582 release file errors at runtime: Command.eval instead of Command.read;
wenzelm
parents: 54519
diff changeset
   211
    | files => map Exn.release files));
48869
wenzelm
parents: 48868
diff changeset
   212
48906
5b192d6b7a54 tuned signature;
wenzelm
parents: 48905
diff changeset
   213
fun provide (src_path, id) =
5b192d6b7a54 tuned signature;
wenzelm
parents: 48905
diff changeset
   214
  map_files (fn (master_dir, imports, provided) =>
5b192d6b7a54 tuned signature;
wenzelm
parents: 48905
diff changeset
   215
    if AList.defined (op =) provided src_path then
5b192d6b7a54 tuned signature;
wenzelm
parents: 48905
diff changeset
   216
      error ("Duplicate use of source file: " ^ Path.print src_path)
5b192d6b7a54 tuned signature;
wenzelm
parents: 48905
diff changeset
   217
    else (master_dir, imports, (src_path, id) :: provided));
5b192d6b7a54 tuned signature;
wenzelm
parents: 48905
diff changeset
   218
69851
29a4f633609e clarified signature;
wenzelm
parents: 69592
diff changeset
   219
fun provide_file (file: Token.file) = provide (#src_path file, #digest file);
29a4f633609e clarified signature;
wenzelm
parents: 69592
diff changeset
   220
48906
5b192d6b7a54 tuned signature;
wenzelm
parents: 48905
diff changeset
   221
fun provide_parse_files cmd =
5b192d6b7a54 tuned signature;
wenzelm
parents: 48905
diff changeset
   222
  parse_files cmd >> (fn files => fn thy =>
5b192d6b7a54 tuned signature;
wenzelm
parents: 48905
diff changeset
   223
    let
5b192d6b7a54 tuned signature;
wenzelm
parents: 48905
diff changeset
   224
      val fs = files thy;
55788
67699e08e969 store blobs / inlined files as separate text lines: smaller values are more healthy for the Poly/ML RTS and allow implicit sharing;
wenzelm
parents: 55708
diff changeset
   225
      val thy' = fold (fn {src_path, digest, ...} => provide (src_path, digest)) fs thy;
48906
5b192d6b7a54 tuned signature;
wenzelm
parents: 48905
diff changeset
   226
    in (fs, thy') end);
5b192d6b7a54 tuned signature;
wenzelm
parents: 48905
diff changeset
   227
43702
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
   228
fun load_file thy src_path =
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
   229
  let
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
   230
    val full_path = check_file (master_directory thy) src_path;
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
   231
    val text = File.read full_path;
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
   232
    val id = SHA1.digest text;
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
   233
  in ((full_path, id), text) end;
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
   234
54446
31884c67d73a tuned signature;
wenzelm
parents: 53403
diff changeset
   235
fun loaded_files_current thy =
48886
9604c6563226 discontinued separate list of required files -- maintain only provided files as they occur at runtime;
wenzelm
parents: 48881
diff changeset
   236
  #provided (Files.get thy) |>
48896
bb1f461a7815 simplified Thy_Load.provide: do not store full path;
wenzelm
parents: 48888
diff changeset
   237
    forall (fn (src_path, id) =>
43702
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
   238
      (case try (load_file thy) src_path of
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   239
        NONE => false
48886
9604c6563226 discontinued separate list of required files -- maintain only provided files as they occur at runtime;
wenzelm
parents: 48881
diff changeset
   240
      | SOME ((_, id'), _) => id = id'));
37952
f6c40213675b Thy_Load.check_loaded via Theory.at_end;
wenzelm
parents: 37950
diff changeset
   241
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   242
67217
wenzelm
parents: 67215
diff changeset
   243
(* formal check *)
49244
fb669aff821e formal markup for @{file} (for hyperlinks etc.) -- interpret path wrt. master directory as usual;
wenzelm
parents: 48992
diff changeset
   244
67217
wenzelm
parents: 67215
diff changeset
   245
fun formal_check check_file ctxt dir (name, pos) =
54705
0dff3326d12a provide @{file_unchecked} in Isabelle/Pure;
wenzelm
parents: 54526
diff changeset
   246
  let
67214
wenzelm
parents: 67209
diff changeset
   247
    fun err msg = error (msg ^ Position.here pos);
56034
1c59b555ac4a some Markup.language_path to prevent completion of symbols (notably "~") -- always "delimited" for simplicity in contrast to 42ac3cfb89f6;
wenzelm
parents: 55882
diff changeset
   248
67214
wenzelm
parents: 67209
diff changeset
   249
    val _ = Context_Position.report ctxt pos Markup.language_path;
wenzelm
parents: 67209
diff changeset
   250
    val path = Path.append dir (Path.explode name) handle ERROR msg => err msg;
wenzelm
parents: 67209
diff changeset
   251
    val _ = Path.expand path handle ERROR msg => err msg;
56134
4a7a07c01857 clarified Path.smart_implode;
wenzelm
parents: 56034
diff changeset
   252
    val _ = Context_Position.report ctxt pos (Markup.path (Path.smart_implode path));
67214
wenzelm
parents: 67209
diff changeset
   253
    val _ = check_file path handle ERROR msg => err msg;
63675
e217525d6b64 uniform ML and document antiquotations;
wenzelm
parents: 63673
diff changeset
   254
  in path end;
e217525d6b64 uniform ML and document antiquotations;
wenzelm
parents: 63673
diff changeset
   255
67217
wenzelm
parents: 67215
diff changeset
   256
val check_path = formal_check I;
wenzelm
parents: 67215
diff changeset
   257
val check_file = formal_check File.check_file;
wenzelm
parents: 67215
diff changeset
   258
val check_dir = formal_check File.check_dir;
wenzelm
parents: 67215
diff changeset
   259
wenzelm
parents: 67215
diff changeset
   260
wenzelm
parents: 67215
diff changeset
   261
(* antiquotations *)
wenzelm
parents: 67215
diff changeset
   262
wenzelm
parents: 67215
diff changeset
   263
local
wenzelm
parents: 67215
diff changeset
   264
69281
599b6d0d199b tuned signature;
wenzelm
parents: 69238
diff changeset
   265
fun document_antiq check =
599b6d0d199b tuned signature;
wenzelm
parents: 69238
diff changeset
   266
  Args.context -- Scan.lift (Parse.position Parse.path) >> (fn (ctxt, (name, pos)) =>
599b6d0d199b tuned signature;
wenzelm
parents: 69238
diff changeset
   267
    let
599b6d0d199b tuned signature;
wenzelm
parents: 69238
diff changeset
   268
      val dir = master_directory (Proof_Context.theory_of ctxt);
599b6d0d199b tuned signature;
wenzelm
parents: 69238
diff changeset
   269
      val _: Path.T = check ctxt dir (name, pos);
599b6d0d199b tuned signature;
wenzelm
parents: 69238
diff changeset
   270
      val latex =
599b6d0d199b tuned signature;
wenzelm
parents: 69238
diff changeset
   271
        space_explode "/" name
599b6d0d199b tuned signature;
wenzelm
parents: 69238
diff changeset
   272
        |> map Latex.output_ascii
599b6d0d199b tuned signature;
wenzelm
parents: 69238
diff changeset
   273
        |> space_implode (Latex.output_ascii "/" ^ "\\discretionary{}{}{}");
599b6d0d199b tuned signature;
wenzelm
parents: 69238
diff changeset
   274
    in Latex.enclose_block "\\isatt{" "}" [Latex.string latex] end);
54705
0dff3326d12a provide @{file_unchecked} in Isabelle/Pure;
wenzelm
parents: 54526
diff changeset
   275
69281
599b6d0d199b tuned signature;
wenzelm
parents: 69238
diff changeset
   276
fun ML_antiq check =
599b6d0d199b tuned signature;
wenzelm
parents: 69238
diff changeset
   277
  Args.context -- Scan.lift (Parse.position Parse.path) >> (fn (ctxt, (name, pos)) =>
599b6d0d199b tuned signature;
wenzelm
parents: 69238
diff changeset
   278
    check ctxt Path.current (name, pos) |> ML_Syntax.print_path);
63675
e217525d6b64 uniform ML and document antiquotations;
wenzelm
parents: 63673
diff changeset
   279
56135
efa24d31e595 added ML antiquotation @{path};
wenzelm
parents: 56134
diff changeset
   280
in
efa24d31e595 added ML antiquotation @{path};
wenzelm
parents: 56134
diff changeset
   281
53171
a5e54d4d9081 added Theory.setup convenience;
wenzelm
parents: 52788
diff changeset
   282
val _ = Theory.setup
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   283
 (Thy_Output.antiquotation_verbatim_embedded \<^binding>\<open>session\<close>
69349
7cef9e386ffe more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents: 69289
diff changeset
   284
    (Scan.lift Parse.embedded_position) check_session #>
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   285
  Thy_Output.antiquotation_verbatim_embedded \<^binding>\<open>doc\<close>
69349
7cef9e386ffe more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents: 69289
diff changeset
   286
    (Scan.lift Parse.embedded_position) check_doc #>
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   287
  Thy_Output.antiquotation_raw_embedded \<^binding>\<open>path\<close> (document_antiq check_path) (K I) #>
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   288
  Thy_Output.antiquotation_raw_embedded \<^binding>\<open>file\<close> (document_antiq check_file) (K I) #>
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   289
  Thy_Output.antiquotation_raw_embedded \<^binding>\<open>dir\<close> (document_antiq check_dir) (K I) #>
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   290
  ML_Antiquotation.value_embedded \<^binding>\<open>path\<close> (ML_antiq check_path) #>
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   291
  ML_Antiquotation.value_embedded \<^binding>\<open>file\<close> (ML_antiq check_file) #>
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   292
  ML_Antiquotation.value_embedded \<^binding>\<open>dir\<close> (ML_antiq check_dir) #>
70015
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 69851
diff changeset
   293
  ML_Antiquotation.value_embedded \<^binding>\<open>path_binding\<close>
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 69851
diff changeset
   294
    (Scan.lift (Parse.position Parse.path) >>
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 69851
diff changeset
   295
      (ML_Syntax.print_path_binding o Path.explode_binding)) #>
69282
94fa3376ba33 added ML antiquotation @{master_dir};
wenzelm
parents: 69281
diff changeset
   296
  ML_Antiquotation.value \<^binding>\<open>master_dir\<close>
94fa3376ba33 added ML antiquotation @{master_dir};
wenzelm
parents: 69281
diff changeset
   297
    (Args.theory >> (ML_Syntax.print_path o master_directory)));
49244
fb669aff821e formal markup for @{file} (for hyperlinks etc.) -- interpret path wrt. master directory as usual;
wenzelm
parents: 48992
diff changeset
   298
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   299
end;
56135
efa24d31e595 added ML antiquotation @{path};
wenzelm
parents: 56134
diff changeset
   300
efa24d31e595 added ML antiquotation @{path};
wenzelm
parents: 56134
diff changeset
   301
end;