src/Pure/ProofGeneral/proof_general_pgip.ML
author wenzelm
Sat, 06 Jun 2009 21:11:23 +0200
changeset 31478 5e412e4c6546
parent 30726 a3adc9a96a16
child 32111 30e2ffbba718
permissions -rw-r--r--
ML_Compiler.exn_message;
aspinall@21637
     1
(*  Title:      Pure/ProofGeneral/proof_general_pgip.ML
aspinall@21637
     2
    Author:     David Aspinall and Markus Wenzel
aspinall@21637
     3
aspinall@21637
     4
Isabelle configuration for Proof General using PGIP protocol.
wenzelm@21940
     5
See http://proofgeneral.inf.ed.ac.uk/kit
aspinall@21637
     6
*)
aspinall@21637
     7
aspinall@21637
     8
signature PROOF_GENERAL_PGIP =
aspinall@21637
     9
sig
wenzelm@28100
    10
  val new_thms_deps: theory -> theory -> string list * string list
wenzelm@21940
    11
  val init_pgip: bool -> unit             (* main PGIP loop with true; fail with false *)
aspinall@21642
    12
aspinall@21649
    13
  (* These two are just to support the semi-PGIP Emacs mode *)
wenzelm@21940
    14
  val init_pgip_channel: (string -> unit) -> unit
wenzelm@21940
    15
  val process_pgip: string -> unit
aspinall@22042
    16
aspinall@23435
    17
  (* More message functions... *)
aspinall@23435
    18
  val nonfatal_error : string -> unit     (* recoverable (batch) error: carry on scripting *)
wenzelm@22228
    19
  val log_msg : string -> unit            (* for internal log messages *)
aspinall@23759
    20
  val error_with_pos : PgipTypes.displayarea -> PgipTypes.fatality -> Position.T -> string -> unit
aspinall@22159
    21
aspinall@22163
    22
  val get_currently_open_file : unit -> Path.T option  (* interface focus *)
wenzelm@28588
    23
  val add_preference: string -> Preferences.preference -> unit
aspinall@21637
    24
end
aspinall@21637
    25
wenzelm@28037
    26
structure ProofGeneralPgip : PROOF_GENERAL_PGIP =
aspinall@21637
    27
struct
aspinall@21637
    28
aspinall@21637
    29
open Pgip;
aspinall@21637
    30
wenzelm@21949
    31
wenzelm@23641
    32
(** print mode **)
aspinall@21637
    33
aspinall@22408
    34
val proof_generalN = "ProofGeneral";
aspinall@22408
    35
val pgmlsymbols_flag = ref true;
aspinall@21637
    36
wenzelm@23641
    37
wenzelm@23641
    38
(* assembling and issuing PGIP packets *)
aspinall@21637
    39
wenzelm@28037
    40
val pgip_refid = ref NONE: string option ref;
aspinall@21637
    41
val pgip_refseq = ref NONE: int option ref;
aspinall@21637
    42
aspinall@21637
    43
local
aspinall@21637
    44
  val pgip_class  = "pg"
aspinall@21637
    45
  val pgip_tag = "Isabelle/Isar"
aspinall@21637
    46
  val pgip_id = ref ""
aspinall@21637
    47
  val pgip_seq = ref 0
aspinall@21637
    48
  fun pgip_serial () = inc pgip_seq
aspinall@21637
    49
aspinall@21637
    50
  fun assemble_pgips pgips =
wenzelm@21940
    51
    Pgip { tag = SOME pgip_tag,
wenzelm@21940
    52
           class = pgip_class,
wenzelm@28037
    53
           seq = pgip_serial (),
wenzelm@28037
    54
           id = ! pgip_id,
wenzelm@28037
    55
           destid = ! pgip_refid,
wenzelm@21940
    56
           (* destid=refid since Isabelle only communicates back to sender *)
wenzelm@28037
    57
           refid = ! pgip_refid,
wenzelm@28037
    58
           refseq = ! pgip_refseq,
wenzelm@21940
    59
           content = pgips }
aspinall@21637
    60
in
aspinall@21637
    61
aspinall@21637
    62
fun init_pgip_session_id () =
aspinall@21637
    63
    pgip_id := getenv "HOSTNAME" ^ "/" ^ getenv "USER" ^ "/" ^
aspinall@21637
    64
               getenv "ISABELLE_PID" ^ "/" ^ Time.toString (Time.now ())
aspinall@21637
    65
wenzelm@28037
    66
fun matching_pgip_id id = (id = ! pgip_id)
aspinall@21637
    67
wenzelm@22590
    68
val output_xml_fn = ref Output.writeln_default
wenzelm@28037
    69
fun output_xml s = ! output_xml_fn (XML.string_of s);
aspinall@21637
    70
wenzelm@28037
    71
val output_pgips = XML.string_of o PgipOutput.output o assemble_pgips o map PgipOutput.output;
wenzelm@23641
    72
wenzelm@28037
    73
val output_pgmlterm = XML.string_of o Pgml.pgmlterm_to_xml;
wenzelm@28037
    74
val output_pgmltext = XML.string_of o Pgml.pgml_to_xml;
aspinall@23759
    75
aspinall@23759
    76
wenzelm@21940
    77
fun issue_pgip_rawtext str =
wenzelm@28037
    78
  output_xml (PgipOutput.output (assemble_pgips (YXML.parse_body str)));
aspinall@21637
    79
aspinall@21637
    80
fun issue_pgip pgipop =
wenzelm@28037
    81
  output_xml (PgipOutput.output (assemble_pgips [PgipOutput.output pgipop]));
aspinall@21637
    82
aspinall@21637
    83
end;
aspinall@21637
    84
aspinall@21637
    85
wenzelm@23641
    86
aspinall@21637
    87
(** messages and notification **)
aspinall@21637
    88
wenzelm@28037
    89
(* PGML terms *)
wenzelm@28037
    90
aspinall@21637
    91
local
aspinall@21637
    92
wenzelm@28037
    93
fun pgml_sym s =
wenzelm@28037
    94
  if ! pgmlsymbols_flag then
wenzelm@28037
    95
    (case Symbol.decode s of
wenzelm@28037
    96
      Symbol.Sym name => Pgml.Sym {name = name, content = s}
wenzelm@28037
    97
    | _ => Pgml.Str s)
wenzelm@28037
    98
  else Pgml.Str s;
aspinall@23759
    99
wenzelm@28037
   100
val pgml_syms = map pgml_sym o Symbol.explode;
wenzelm@28037
   101
wenzelm@28037
   102
val token_markups =
wenzelm@28037
   103
 [Markup.tclassN, Markup.tfreeN, Markup.tvarN, Markup.freeN,
wenzelm@28037
   104
  Markup.boundN, Markup.varN, Markup.skolemN];
aspinall@23759
   105
aspinall@21637
   106
in
wenzelm@28037
   107
wenzelm@28037
   108
fun pgml_terms (XML.Elem (name, atts, body)) =
wenzelm@28037
   109
      if member (op =) token_markups name then
wenzelm@28037
   110
        let val content = pgml_syms (Buffer.content (fold XML.add_content body Buffer.empty))
wenzelm@28037
   111
        in [Pgml.Atoms {kind = SOME name, content = content}] end
wenzelm@28037
   112
      else
wenzelm@28037
   113
        let val content = maps pgml_terms body in
wenzelm@28037
   114
          if name = Markup.blockN then
wenzelm@28037
   115
            [Pgml.Box {orient = NONE, indent = Properties.get_int atts Markup.indentN, content = content}]
wenzelm@28037
   116
          else if name = Markup.breakN then
wenzelm@28037
   117
            [Pgml.Break {mandatory = NONE, indent = Properties.get_int atts Markup.widthN}]
wenzelm@28037
   118
          else if name = Markup.fbreakN then
wenzelm@28037
   119
            [Pgml.Break {mandatory = SOME true, indent = NONE}]
wenzelm@28037
   120
          else content
wenzelm@21940
   121
        end
wenzelm@28037
   122
  | pgml_terms (XML.Text text) = map (Pgml.Raw o Pgml.atom_to_xml) (pgml_syms text);
aspinall@21637
   123
aspinall@21637
   124
end;
aspinall@21637
   125
wenzelm@28037
   126
wenzelm@28037
   127
(* messages *)
wenzelm@28037
   128
wenzelm@28037
   129
fun pgml area content =
wenzelm@28037
   130
  Pgml.Pgml {version = NONE, systemid = NONE, area = SOME area, content = content};
wenzelm@28037
   131
wenzelm@28037
   132
fun message_content default_area s =
wenzelm@28037
   133
  let
wenzelm@28037
   134
    val body = YXML.parse_body s;
wenzelm@28037
   135
    val area =
wenzelm@28037
   136
      (case body of
wenzelm@28037
   137
        [XML.Elem (name, _, _)] =>
wenzelm@28037
   138
          if name = Markup.stateN then PgipTypes.Display else default_area
wenzelm@28037
   139
      | _ => default_area);
wenzelm@28037
   140
  in Pgml.pgml_to_xml (pgml area (maps pgml_terms body)) end;
wenzelm@28037
   141
wenzelm@28037
   142
wenzelm@28037
   143
fun normalmsg area s = issue_pgip
wenzelm@28037
   144
  (Normalresponse {content = [message_content area s]});
wenzelm@28037
   145
wenzelm@28037
   146
fun errormsg area fatality s = issue_pgip
wenzelm@28037
   147
  (Errorresponse {fatality = fatality, location = NONE, content = [message_content area s]});
wenzelm@28037
   148
wenzelm@28037
   149
(*error responses with useful locations*)
wenzelm@28037
   150
fun error_with_pos area fatality pos s = issue_pgip
wenzelm@28037
   151
  (Errorresponse {
wenzelm@28037
   152
    fatality = fatality,
wenzelm@28037
   153
    location = SOME (PgipIsabelle.location_of_position pos),
wenzelm@28037
   154
    content = [message_content area s]});
wenzelm@28037
   155
wenzelm@28037
   156
fun panic s = (errormsg Message Panic ("## SYSTEM EXIT ##\n" ^ s); exit 1);
wenzelm@28037
   157
fun nonfatal_error s = errormsg Message Nonfatal s;
wenzelm@28037
   158
fun log_msg s = errormsg Message Log s;
wenzelm@28037
   159
wenzelm@28037
   160
(* NB: all of standard functions print strings terminated with new lines, but we don't
wenzelm@22228
   161
   add new lines explicitly in PGIP: they are left implicit.  It means that PGIP messages
aspinall@22042
   162
   can't be written without newlines. *)
aspinall@21637
   163
fun setup_messages () =
aspinall@23759
   164
 (Output.writeln_fn := (fn s => normalmsg Message s);
wenzelm@27604
   165
  Output.status_fn := (fn _ => ());
aspinall@23840
   166
  Output.priority_fn := (fn s => normalmsg Status s);
wenzelm@28037
   167
  Output.tracing_fn := (fn s => normalmsg Tracing s);
aspinall@23759
   168
  Output.warning_fn := (fn s => errormsg Message Warning s);
aspinall@23759
   169
  Output.error_fn := (fn s => errormsg Message Fatal s);
aspinall@23759
   170
  Output.debug_fn := (fn s => errormsg Message Debug s));
aspinall@22042
   171
aspinall@21637
   172
aspinall@21637
   173
(* immediate messages *)
aspinall@21637
   174
wenzelm@26706
   175
fun tell_clear_goals () =
aspinall@23759
   176
    issue_pgip (Normalresponse { content = [Pgml.pgml_to_xml (pgml Display [])] })
wenzelm@26706
   177
fun tell_clear_response () =
aspinall@23759
   178
    issue_pgip (Normalresponse { content = [Pgml.pgml_to_xml (pgml Message [])] })
aspinall@22042
   179
wenzelm@22228
   180
fun tell_file_loaded completed path   =
aspinall@22042
   181
    issue_pgip (Informfileloaded {url=PgipTypes.pgipurl_of_path path,
wenzelm@22228
   182
                                  completed=completed})
wenzelm@22228
   183
fun tell_file_outdated completed path   =
aspinall@22042
   184
    issue_pgip (Informfileoutdated {url=PgipTypes.pgipurl_of_path path,
wenzelm@22228
   185
                                    completed=completed})
wenzelm@22228
   186
fun tell_file_retracted completed path =
aspinall@22042
   187
    issue_pgip (Informfileretracted {url=PgipTypes.pgipurl_of_path path,
wenzelm@22228
   188
                                     completed=completed})
aspinall@21637
   189
aspinall@21637
   190
aspinall@21637
   191
(* theory loader actions *)
aspinall@21637
   192
aspinall@21637
   193
local
wenzelm@22228
   194
  (* da: TODO: PGIP has a completed flag so the prover can indicate to the
wenzelm@22228
   195
     interface which files are busy performing a particular action.
aspinall@22042
   196
     To make use of this we need to adjust the hook in thy_info.ML
wenzelm@22228
   197
     (may actually be difficult to tell the interface *which* action is in
aspinall@22042
   198
      progress, but we could add a generic "Lock" action which uses
aspinall@22042
   199
      informfileloaded: the broker/UI should not infer too much from incomplete
aspinall@22042
   200
      operations).
wenzelm@22228
   201
   *)
aspinall@21637
   202
fun trace_action action name =
aspinall@21637
   203
  if action = ThyInfo.Update then
wenzelm@22228
   204
    List.app (tell_file_loaded true) (ThyInfo.loaded_files name)
aspinall@22042
   205
  else if action = ThyInfo.Outdate then
aspinall@22042
   206
    List.app (tell_file_outdated true) (ThyInfo.loaded_files name)
aspinall@22042
   207
  else if action = ThyInfo.Remove then
wenzelm@22228
   208
      List.app (tell_file_retracted true) (ThyInfo.loaded_files name)
aspinall@22042
   209
  else ()
aspinall@22042
   210
aspinall@21637
   211
aspinall@21637
   212
in
aspinall@21637
   213
  fun setup_thy_loader () = ThyInfo.add_hook trace_action;
wenzelm@26613
   214
  fun sync_thy_loader () = List.app (trace_action ThyInfo.Update) (ThyInfo.get_names ());
aspinall@21637
   215
end;
aspinall@21637
   216
aspinall@21637
   217
wenzelm@21949
   218
(* get informed about files *)
aspinall@21637
   219
aspinall@22042
   220
val thy_name = Path.implode o #1 o Path.split_ext o Path.base;
aspinall@21637
   221
aspinall@21637
   222
val inform_file_retracted = ThyInfo.if_known_thy ThyInfo.remove_thy o thy_name;
aspinall@21637
   223
val inform_file_processed = ThyInfo.if_known_thy ThyInfo.touch_child_thys o thy_name;
aspinall@21637
   224
aspinall@22042
   225
fun proper_inform_file_processed path state =
aspinall@22042
   226
  let val name = thy_name path in
aspinall@21637
   227
    if Toplevel.is_toplevel state andalso ThyInfo.known_thy name then
aspinall@21637
   228
     (ThyInfo.touch_child_thys name;
wenzelm@24079
   229
      ThyInfo.register_thy name handle ERROR msg =>
wenzelm@23913
   230
       (warning (cat_lines [msg, "Failed to register theory: " ^ quote name]);
aspinall@22042
   231
        tell_file_retracted true (Path.base path)))
aspinall@21637
   232
    else raise Toplevel.UNDEF
aspinall@21637
   233
  end;
aspinall@21637
   234
aspinall@21637
   235
aspinall@21637
   236
(* restart top-level loop (keeps most state information) *)
aspinall@21637
   237
aspinall@21637
   238
val welcome = priority o Session.welcome;
aspinall@21637
   239
aspinall@21637
   240
fun restart () =
aspinall@21637
   241
    (sync_thy_loader ();
aspinall@21637
   242
     tell_clear_goals ();
aspinall@21637
   243
     tell_clear_response ();
wenzelm@29349
   244
     Isar.init ();
wenzelm@27578
   245
     welcome ());
aspinall@21637
   246
aspinall@21637
   247
wenzelm@28097
   248
(* theorem dependencies *)
wenzelm@28097
   249
wenzelm@28097
   250
local
wenzelm@28097
   251
wenzelm@28809
   252
fun add_proof_body (PBody {thms, ...}) =
wenzelm@28817
   253
  thms |> fold (fn (_, (name, _, _)) => name <> "" ? Symtab.update (name, ()));
wenzelm@28809
   254
wenzelm@28809
   255
fun add_thm th =
wenzelm@28814
   256
  (case Thm.proof_body_of th of 
wenzelm@28809
   257
    PBody {proof = PThm (_, ((name, _, _), body)), ...} =>
wenzelm@28809
   258
      if Thm.has_name_hint th andalso Thm.get_name_hint th = name
wenzelm@29635
   259
      then add_proof_body (Future.join body)
wenzelm@28097
   260
      else I
wenzelm@28809
   261
  | body => add_proof_body body);
wenzelm@28809
   262
wenzelm@28809
   263
in
wenzelm@28097
   264
wenzelm@28097
   265
fun thms_deps ths =
wenzelm@28097
   266
  let
wenzelm@28097
   267
    (* FIXME proper derivation names!? *)
wenzelm@28097
   268
    val names = map Thm.get_name_hint (filter Thm.has_name_hint ths);
wenzelm@28809
   269
    val deps = Symtab.keys (fold add_thm ths Symtab.empty);
wenzelm@28097
   270
  in (names, deps) end;
wenzelm@28097
   271
wenzelm@28100
   272
fun new_thms_deps thy thy' =
wenzelm@28097
   273
  let
wenzelm@28100
   274
    val prev_facts = PureThy.facts_of thy;
wenzelm@28100
   275
    val facts = PureThy.facts_of thy';
wenzelm@28100
   276
  in thms_deps (maps #2 (Facts.dest_static [prev_facts] facts)) end;
wenzelm@28097
   277
wenzelm@28097
   278
end;
wenzelm@28097
   279
wenzelm@28097
   280
wenzelm@28097
   281
(* theorem dependeny output *)
aspinall@22408
   282
aspinall@22408
   283
val show_theorem_dependencies = ref false;
aspinall@22408
   284
aspinall@21637
   285
local
aspinall@21637
   286
aspinall@21637
   287
val spaces_quote = space_implode " " o map quote;
aspinall@21637
   288
aspinall@21637
   289
fun thm_deps_message (thms, deps) =
wenzelm@28097
   290
  let
wenzelm@28097
   291
    val valuethms = XML.Elem ("value", [("name", "thms")], [XML.Text thms]);
wenzelm@28097
   292
    val valuedeps = XML.Elem ("value", [("name", "deps")], [XML.Text deps]);
wenzelm@28097
   293
  in
wenzelm@28097
   294
    issue_pgip (Metainforesponse
wenzelm@28097
   295
      {attrs = [("infotype", "isabelle_theorem_dependencies")],
wenzelm@28097
   296
       content = [valuethms, valuedeps]})
wenzelm@28097
   297
  end;
aspinall@21637
   298
aspinall@21637
   299
in
aspinall@21637
   300
wenzelm@28103
   301
fun setup_present_hook () = Toplevel.add_hook (fn _ => fn state => fn state' =>
wenzelm@28100
   302
  if ! show_theorem_dependencies andalso
wenzelm@28103
   303
    can Toplevel.theory_of state andalso Toplevel.is_theory state'
wenzelm@28100
   304
  then
wenzelm@28100
   305
    let val (names, deps) = new_thms_deps (Toplevel.theory_of state) (Toplevel.theory_of state') in
wenzelm@28097
   306
      if null names orelse null deps then ()
wenzelm@28097
   307
      else thm_deps_message (spaces_quote names, spaces_quote deps)
wenzelm@28097
   308
    end
wenzelm@28097
   309
  else ());
aspinall@21637
   310
aspinall@21637
   311
end;
aspinall@21637
   312
wenzelm@28097
   313
aspinall@21637
   314
(** lexicalstructure element with keywords (PGIP version of elisp keywords file) **)
aspinall@21637
   315
wenzelm@21940
   316
fun lexicalstructure_keywords () =
wenzelm@27353
   317
    let val keywords = OuterKeyword.dest_keywords ()
wenzelm@27353
   318
        val commands = OuterKeyword.dest_commands ()
wenzelm@27353
   319
        fun keyword_elt kind keyword =
wenzelm@27353
   320
            XML.Elem("keyword", [("word", keyword), ("category", kind)], [])
wenzelm@21940
   321
        in
aspinall@21637
   322
            (* Also, note we don't call init_outer_syntax here to add interface commands,
aspinall@21637
   323
            but they should never appear in scripts anyway so it shouldn't matter *)
wenzelm@27353
   324
            Lexicalstructure
wenzelm@27353
   325
              {content = map (keyword_elt "minor") keywords @ map (keyword_elt "major") commands}
aspinall@21637
   326
        end
aspinall@21637
   327
aspinall@21637
   328
(* TODO: we can issue a lexicalstructure/keyword when the syntax gets extended dynamically;
aspinall@21637
   329
   hooks needed in outer_syntax.ML to do that. *)
aspinall@21637
   330
aspinall@21637
   331
aspinall@21637
   332
(* Configuration: GUI config, proverinfo messages *)
aspinall@21637
   333
aspinall@21637
   334
local
aspinall@21637
   335
    val isabellewww = "http://isabelle.in.tum.de/"
aspinall@21637
   336
    val staticconfig = "~~/lib/ProofGeneral/pgip_isar.xml"
wenzelm@21940
   337
    fun orenv v d = case getenv v of "" => d  | s => s
aspinall@21637
   338
    fun config_file()  = orenv "ISABELLE_PGIPCONFIG" staticconfig
aspinall@21637
   339
    fun isabelle_www() = orenv "ISABELLE_HOMEPAGE" isabellewww
aspinall@21637
   340
in
aspinall@21637
   341
fun send_pgip_config () =
aspinall@21637
   342
    let
wenzelm@21858
   343
        val path = Path.explode (config_file())
wenzelm@21940
   344
        val ex = File.exists path
aspinall@21637
   345
wenzelm@21940
   346
        val wwwpage =
wenzelm@21940
   347
            (Url.explode (isabelle_www()))
wenzelm@21969
   348
            handle ERROR _ =>
wenzelm@22699
   349
                   (panic ("Error in URL in environment variable ISABELLE_HOMEPAGE.");
wenzelm@21940
   350
                        Url.explode isabellewww)
wenzelm@21940
   351
wenzelm@21940
   352
        val proverinfo =
aspinall@21637
   353
            Proverinfo { name = "Isabelle",
wenzelm@26109
   354
                         version = Distribution.version,
wenzelm@21940
   355
                         instance = Session.name(),
wenzelm@21940
   356
                         descr = "The Isabelle/Isar theorem prover",
wenzelm@21940
   357
                         url = wwwpage,
wenzelm@21940
   358
                         filenameextns = ".thy;" }
aspinall@21637
   359
    in
wenzelm@21940
   360
        if ex then
wenzelm@21940
   361
            (issue_pgip proverinfo;
wenzelm@21940
   362
             issue_pgip_rawtext (File.read path);
wenzelm@21940
   363
             issue_pgip (lexicalstructure_keywords()))
wenzelm@22699
   364
        else panic ("PGIP configuration file \"" ^ config_file() ^ "\" not found")
aspinall@21637
   365
    end;
aspinall@21637
   366
end
aspinall@21637
   367
aspinall@21637
   368
aspinall@22216
   369
(* Preferences: tweak for PGIP interfaces *)
aspinall@22216
   370
wenzelm@28588
   371
val preferences = ref Preferences.pure_preferences;
aspinall@22216
   372
nipkow@28066
   373
fun add_preference cat pref =
wenzelm@28588
   374
  CRITICAL (fn () => change preferences (Preferences.add cat pref));
nipkow@28066
   375
wenzelm@28588
   376
fun setup_preferences_tweak () =
wenzelm@28588
   377
  CRITICAL (fn () => change preferences
wenzelm@28588
   378
   (Preferences.set_default ("show-question-marks", "false") #>
wenzelm@28588
   379
    Preferences.remove "show-question-marks" #>   (* we use markup, not ?s *)
wenzelm@28588
   380
    Preferences.remove "theorem-dependencies" #>  (* set internally *)
wenzelm@28588
   381
    Preferences.remove "full-proofs"));           (* set internally *)
wenzelm@22228
   382
aspinall@21637
   383
aspinall@21637
   384
aspinall@21637
   385
(* Sending commands to Isar *)
aspinall@21637
   386
wenzelm@26622
   387
fun isarcmd s = Isar.>>> (OuterSyntax.parse Position.none s);
aspinall@21637
   388
wenzelm@21940
   389
(* TODO:
wenzelm@27565
   390
    - apply a command given a transition function;
aspinall@21885
   391
    - fix position from path of currently open file [line numbers risk garbling though].
aspinall@21885
   392
*)
aspinall@21637
   393
aspinall@21637
   394
(* load an arbitrary file (must be .thy or .ML) *)
aspinall@21637
   395
aspinall@21637
   396
fun use_thy_or_ml_file file =
aspinall@21637
   397
    let
wenzelm@21858
   398
        val (path,extn) = Path.split_ext (Path.explode file)
aspinall@21637
   399
    in
aspinall@21637
   400
        case extn of
wenzelm@21940
   401
            "" => isarcmd ("use_thy " ^ quote (Path.implode path))
wenzelm@21940
   402
          | "thy" => isarcmd ("use_thy " ^ quote (Path.implode path))
aspinall@21637
   403
          | "ML" => isarcmd ("use " ^ quote file)
aspinall@22028
   404
          | other => error ("Don't know how to read a file with extension " ^ quote other)
aspinall@21637
   405
    end
aspinall@21637
   406
aspinall@21637
   407
aspinall@21867
   408
(******* PGIP actions *******)
aspinall@21637
   409
aspinall@21637
   410
wenzelm@21940
   411
(* Responses to each of the PGIP input commands.
aspinall@21637
   412
   These are programmed uniformly for extensibility. *)
aspinall@21637
   413
wenzelm@23834
   414
fun askpgip (Askpgip _) =
aspinall@23435
   415
    (issue_pgip
aspinall@23435
   416
         (Usespgip { version = PgipIsabelle.isabelle_pgip_version_supported,
aspinall@23435
   417
                     pgipelems = PgipIsabelle.accepted_inputs });
aspinall@23435
   418
     send_pgip_config())
aspinall@21637
   419
wenzelm@23834
   420
fun askpgml (Askpgml _) =
aspinall@21637
   421
    issue_pgip
wenzelm@21940
   422
        (Usespgml { version = PgipIsabelle.isabelle_pgml_version_supported })
aspinall@21637
   423
wenzelm@23834
   424
fun askprefs (Askprefs _) =
wenzelm@21940
   425
    let
wenzelm@21940
   426
        fun preference_of {name, descr, default, pgiptype, get, set } =
wenzelm@21940
   427
            { name = name, descr = SOME descr, default = SOME default,
wenzelm@21940
   428
              pgiptype = pgiptype }
aspinall@21637
   429
    in
wenzelm@21940
   430
        List.app (fn (prefcat, prefs) =>
wenzelm@21940
   431
                     issue_pgip (Hasprefs {prefcategory=SOME prefcat,
wenzelm@21940
   432
                                           prefs=map preference_of prefs}))
aspinall@22216
   433
                 (!preferences)
wenzelm@21940
   434
    end
aspinall@21637
   435
wenzelm@23834
   436
fun askconfig (Askconfig _) = () (* TODO: add config response *)
aspinall@21637
   437
aspinall@21637
   438
local
wenzelm@21940
   439
    fun lookuppref pref =
wenzelm@21940
   440
        case AList.lookup (op =)
wenzelm@21940
   441
                          (map (fn p => (#name p,p))
aspinall@22216
   442
                               (maps snd (!preferences))) pref of
wenzelm@21940
   443
            NONE => error ("Unknown prover preference: " ^ quote pref)
wenzelm@21940
   444
          | SOME p => p
aspinall@21637
   445
in
wenzelm@21940
   446
fun setpref (Setpref vs) =
wenzelm@21940
   447
    let
wenzelm@21940
   448
        val name = #name vs
wenzelm@21940
   449
        val value = #value vs
wenzelm@21940
   450
        val set = #set (lookuppref name)
aspinall@21637
   451
    in
wenzelm@21940
   452
        set value
aspinall@21637
   453
    end
aspinall@21637
   454
haftmann@21902
   455
fun getpref (Getpref vs) =
wenzelm@21940
   456
    let
wenzelm@21940
   457
        val name = #name vs
wenzelm@21940
   458
        val get = #get (lookuppref name)
wenzelm@21940
   459
    in
aspinall@21637
   460
        issue_pgip (Prefval {name=name, value=get ()})
aspinall@21637
   461
    end
aspinall@21637
   462
end
aspinall@21637
   463
wenzelm@23834
   464
fun proverinit _ = restart ()
aspinall@21637
   465
wenzelm@23834
   466
fun proverexit _ = isarcmd "quit"
aspinall@21637
   467
wenzelm@26706
   468
fun set_proverflag_quiet b =
aspinall@22408
   469
    isarcmd (if b then "disable_pr" else "enable_pr")
aspinall@21637
   470
aspinall@22408
   471
fun set_proverflag_pgmlsymbols b =
aspinall@22408
   472
    (pgmlsymbols_flag := b;
wenzelm@24614
   473
      NAMED_CRITICAL "print_mode" (fn () =>
wenzelm@26706
   474
        change print_mode
wenzelm@22590
   475
            (fn mode =>
wenzelm@24614
   476
                remove (op =) Symbol.xsymbolsN mode @ (if b then [Symbol.xsymbolsN] else []))))
aspinall@21637
   477
aspinall@22408
   478
fun set_proverflag_thmdeps b =
aspinall@22408
   479
    (show_theorem_dependencies := b;
wenzelm@25223
   480
     Proofterm.proofs := (if b then 1 else 2))
aspinall@21637
   481
aspinall@22408
   482
fun setproverflag (Setproverflag vs) =
wenzelm@26706
   483
    let
wenzelm@22590
   484
        val flagname = #flagname vs
wenzelm@22590
   485
        val value = #value vs
aspinall@22408
   486
    in
wenzelm@22590
   487
        (case flagname of
wenzelm@22590
   488
             "quiet"            => set_proverflag_quiet value
wenzelm@22590
   489
           | "pgmlsymbols"      => set_proverflag_pgmlsymbols value
wenzelm@26706
   490
           | "metainfo:thmdeps" => set_proverflag_thmdeps value
wenzelm@26706
   491
           | _ => log_msg ("Unrecognised prover control flag: " ^
wenzelm@23801
   492
                           (quote flagname) ^ " ignored."))
wenzelm@26706
   493
    end
aspinall@22408
   494
aspinall@21637
   495
wenzelm@21940
   496
fun dostep (Dostep vs) =
wenzelm@21940
   497
    let
wenzelm@21940
   498
        val text = #text vs
wenzelm@21940
   499
    in
wenzelm@21940
   500
        isarcmd text
aspinall@21637
   501
    end
aspinall@21637
   502
haftmann@21902
   503
fun undostep (Undostep vs) =
wenzelm@21940
   504
    let
wenzelm@21940
   505
        val times = #times vs
wenzelm@21940
   506
    in
wenzelm@21940
   507
        isarcmd ("undos_proof " ^ Int.toString times)
aspinall@21637
   508
    end
aspinall@21637
   509
wenzelm@27565
   510
fun redostep _ = sys_error "redo unavailable"
wenzelm@21940
   511
wenzelm@23834
   512
fun abortgoal _ = isarcmd "kill" (* was: ProofGeneral.kill_proof *)
aspinall@21637
   513
aspinall@21637
   514
aspinall@21867
   515
(*** PGIP identifier tables ***)
aspinall@21867
   516
wenzelm@22228
   517
(* TODO: these ones should be triggered by hooks after a
aspinall@22159
   518
   declaration addition/removal, to be sent automatically. *)
aspinall@21867
   519
aspinall@22159
   520
fun addids t  = issue_pgip (Addids {idtables = [t]})
aspinall@22159
   521
fun delids t  = issue_pgip (Delids {idtables = [t]})
aspinall@21867
   522
wenzelm@27177
   523
wenzelm@27177
   524
local
wenzelm@27177
   525
wenzelm@27177
   526
fun theory_facts name =
wenzelm@27177
   527
  let val thy = ThyInfo.get_theory name
wenzelm@27177
   528
  in (map PureThy.facts_of (Theory.parents_of thy), PureThy.facts_of thy) end;
wenzelm@27177
   529
wenzelm@27177
   530
fun thms_of_thy name = map fst (theory_facts name |-> Facts.extern_static);
wenzelm@27177
   531
fun qualified_thms_of_thy name = map fst (theory_facts name |-> Facts.dest_static);
wenzelm@27177
   532
wenzelm@27177
   533
in
wenzelm@27177
   534
wenzelm@21940
   535
fun askids (Askids vs) =
aspinall@21637
   536
    let
wenzelm@21940
   537
        val url = #url vs            (* ask for identifiers within a file *)
wenzelm@21940
   538
        val thyname = #thyname vs    (* ask for identifiers within a theory *)
wenzelm@21940
   539
        val objtype = #objtype vs    (* ask for identifiers of a particular type *)
aspinall@21867
   540
wenzelm@21940
   541
        fun idtable ty ctx ids = {objtype=ty,context=ctx,ids=ids}
aspinall@21867
   542
wenzelm@22228
   543
        fun setids t = issue_pgip (Setids {idtables = [t]})
aspinall@22159
   544
aspinall@22225
   545
        (* fake one-level nested "subtheories" by picking apart names. *)
wenzelm@30364
   546
        val immed_thms_of_thy = filter_out Long_Name.is_qualified o thms_of_thy
wenzelm@30364
   547
        fun thy_prefix s = case Long_Name.explode s of
wenzelm@22228
   548
                                    x::_::_ => SOME x  (* String.find? *)
wenzelm@22228
   549
                                  | _ => NONE
wenzelm@22228
   550
        fun subthys_of_thy s =
wenzelm@23178
   551
            List.foldl  (fn (NONE,xs) => xs | (SOME x,xs) => insert op= x xs) []
wenzelm@22228
   552
                   (map thy_prefix (thms_of_thy s))
wenzelm@22228
   553
        fun subthms_of_thy thy =
wenzelm@22228
   554
            (case thy_prefix thy of
wenzelm@22228
   555
                 NONE => immed_thms_of_thy thy
wenzelm@30364
   556
               | SOME prf => filter (String.isPrefix (unprefix (prf ^ Long_Name.separator) thy))
wenzelm@22228
   557
                                    (thms_of_thy prf))
wenzelm@26706
   558
    in
wenzelm@21940
   559
        case (thyname,objtype) of
wenzelm@22228
   560
           (NONE, NONE) =>
wenzelm@26613
   561
           setids (idtable ObjFile NONE (ThyInfo.get_names())) (*FIXME: uris*)
wenzelm@22228
   562
         | (NONE, SOME ObjFile) =>
wenzelm@26613
   563
           setids (idtable ObjFile NONE (ThyInfo.get_names())) (*FIXME: uris*)
wenzelm@22228
   564
         | (SOME fi, SOME ObjFile) =>
wenzelm@22228
   565
           setids (idtable ObjTheory (SOME fi) [fi])       (* TODO: check exists *)
aspinall@22225
   566
         | (NONE, SOME ObjTheory) =>
wenzelm@26613
   567
           setids (idtable ObjTheory NONE (ThyInfo.get_names()))
aspinall@22225
   568
         | (SOME thy, SOME ObjTheory) =>
wenzelm@22228
   569
           setids (idtable ObjTheory (SOME thy) (subthys_of_thy thy))
wenzelm@22228
   570
         | (SOME thy, SOME ObjTheorem) =>
wenzelm@22228
   571
           setids (idtable ObjTheorem (SOME thy) (subthms_of_thy thy))
wenzelm@22228
   572
         | (NONE, SOME ObjTheorem) =>
wenzelm@22228
   573
           (* A large query, but not unreasonable. ~5000 results for HOL.*)
wenzelm@22228
   574
           (* Several setids should be allowed, but Eclipse code is currently broken:
wenzelm@23226
   575
              List.app (fn thy => setids (idtable ObjTheorem (SOME thy) (subthms_of_thy thy)))
wenzelm@26613
   576
                         (ThyInfo.get_names()) *)
aspinall@22225
   577
           setids (idtable ObjTheorem NONE (* this one gives ~7000 for HOL *)
wenzelm@26613
   578
                           (maps qualified_thms_of_thy (ThyInfo.get_names())))
aspinall@22225
   579
         | _ => warning ("askids: ignored argument combination")
aspinall@21637
   580
    end
aspinall@21637
   581
wenzelm@27177
   582
end;
wenzelm@27177
   583
aspinall@22159
   584
fun askrefs (Askrefs vs) =
aspinall@22159
   585
    let
aspinall@22159
   586
        val url = #url vs            (* ask for references of a file (i.e. immediate pre-requisites) *)
aspinall@22159
   587
        val thyname = #thyname vs    (* ask for references of a theory (other theories) *)
aspinall@22159
   588
        val objtype = #objtype vs    (* ask for references of a particular type... *)
aspinall@22159
   589
        val name = #name vs          (*   ... with this name *)
aspinall@22159
   590
aspinall@22159
   591
        fun idtable ty ctx ids = {objtype=ty,context=ctx,ids=ids}
aspinall@22159
   592
wenzelm@22228
   593
        val thy_name = Path.implode o #1 o Path.split_ext o Path.base
aspinall@22159
   594
wenzelm@22228
   595
        fun filerefs f =
wenzelm@22228
   596
            let val thy = thy_name f
wenzelm@24189
   597
                val filerefs = #uses (ThyLoad.deps_thy (Path.dir f) thy)
wenzelm@22228
   598
            in
wenzelm@22228
   599
                issue_pgip (Setrefs {url=url, thyname=NONE, objtype=SOME PgipTypes.ObjFile,
wenzelm@22228
   600
                                     name=NONE, idtables=[], fileurls=filerefs})
wenzelm@22228
   601
            end
aspinall@22159
   602
wenzelm@22228
   603
        fun thyrefs thy =
wenzelm@24189
   604
            let val thyrefs = #imports (ThyLoad.deps_thy Path.current thy)
wenzelm@22228
   605
            in
wenzelm@22228
   606
                issue_pgip (Setrefs {url=url, thyname=thyname, objtype=SOME PgipTypes.ObjTheory,
wenzelm@22228
   607
                                     name=NONE, idtables=[{context=NONE, objtype=PgipTypes.ObjTheory,
wenzelm@22228
   608
                                                           ids=thyrefs}], fileurls=[]})
wenzelm@22228
   609
            end
aspinall@22159
   610
wenzelm@22228
   611
        fun thmrefs thmname =
wenzelm@22228
   612
            let
wenzelm@22228
   613
                (* TODO: interim: this is probably not right.
wenzelm@22228
   614
                   What we want is mapping onto simple PGIP name/context model. *)
wenzelm@26603
   615
                val ctx = Toplevel.context_of (Isar.state ()) (* NB: raises UNDEF *)
wenzelm@22228
   616
                val thy = Context.theory_of_proof ctx
wenzelm@26343
   617
                val ths = [PureThy.get_thm thy thmname]
wenzelm@28809
   618
                val deps = #2 (thms_deps ths);
wenzelm@22228
   619
            in
wenzelm@22228
   620
                if null deps then ()
wenzelm@22228
   621
                else issue_pgip (Setrefs {url=url, thyname=thyname, name=name,
wenzelm@22228
   622
                                          objtype=SOME PgipTypes.ObjTheorem,
wenzelm@22228
   623
                                          idtables=[{context=NONE, objtype=PgipTypes.ObjTheorem,
wenzelm@22228
   624
                                                     ids=deps}], fileurls=[]})
wenzelm@22228
   625
            end
aspinall@22159
   626
    in
aspinall@22159
   627
        case (url,thyname,objtype,name) of
wenzelm@22228
   628
            (SOME file, NONE, _, _)  => filerefs file
wenzelm@22228
   629
          | (_,SOME thy,_,_)         => thyrefs thy
wenzelm@22228
   630
          | (_,_,SOME PgipTypes.ObjTheorem,SOME thmname) => thmrefs thmname
aspinall@22159
   631
          | _  => error ("Unimplemented/invalid case of <askrefs>")
aspinall@22159
   632
    end
aspinall@22159
   633
aspinall@22159
   634
aspinall@22159
   635
wenzelm@21940
   636
fun showid (Showid vs) =
aspinall@21637
   637
    let
wenzelm@21940
   638
        val thyname = #thyname vs
wenzelm@21940
   639
        val objtype = #objtype vs
wenzelm@21940
   640
        val name = #name vs
aspinall@22337
   641
wenzelm@26603
   642
        val topthy = Toplevel.theory_of o Isar.state
aspinall@21637
   643
wenzelm@22590
   644
        fun splitthy id =
wenzelm@30364
   645
            let val comps = Long_Name.explode id
wenzelm@22590
   646
            in case comps of
wenzelm@22590
   647
                   (thy::(rest as _::_)) => (ThyInfo.get_theory thy, space_implode "." rest)
wenzelm@22590
   648
                 | [plainid] => (topthy(),plainid)
wenzelm@22590
   649
                 | _ => raise Toplevel.UNDEF (* assert false *)
wenzelm@26706
   650
            end
wenzelm@26706
   651
aspinall@21637
   652
aspinall@22337
   653
        fun idvalue strings =
wenzelm@26706
   654
            issue_pgip (Idvalue { thyname=thyname, objtype=objtype, name=name,
aspinall@23759
   655
                                  text=[XML.Elem("pgml",[],
wenzelm@28037
   656
                                                 maps YXML.parse_body strings)] })
aspinall@22337
   657
wenzelm@30726
   658
        fun string_of_thm th = Pretty.string_of (Display.pretty_thm_aux
wenzelm@30726
   659
            (Syntax.pp_global (Thm.theory_of_thm th))
wenzelm@30726
   660
            {quote = false, show_hyps = false, show_status = true} [] th)
aspinall@22337
   661
wenzelm@26343
   662
        fun strings_of_thm (thy, name) = map string_of_thm (PureThy.get_thms thy name)
aspinall@22337
   663
wenzelm@28037
   664
        val string_of_thy = Pretty.string_of o ProofDisplay.pretty_full_theory false
wenzelm@21940
   665
    in
wenzelm@21940
   666
        case (thyname, objtype) of
wenzelm@28037
   667
            (_, ObjTheory) => idvalue [string_of_thy (ThyInfo.get_theory name)]
aspinall@22337
   668
          | (SOME thy, ObjTheorem) => idvalue (strings_of_thm (ThyInfo.get_theory thy, name))
aspinall@22337
   669
          | (NONE, ObjTheorem) => idvalue (strings_of_thm (splitthy name))
wenzelm@21940
   670
          | (_, ot) => error ("Cannot show objects of type "^(PgipTypes.name_of_objtype ot))
aspinall@21637
   671
    end
aspinall@21637
   672
aspinall@21867
   673
(*** Inspecting state ***)
aspinall@21867
   674
wenzelm@21940
   675
(* The file which is currently being processed interactively.
aspinall@21637
   676
   In the pre-PGIP code, this was informed to Isabelle and the theory loader
aspinall@21637
   677
   on completion, but that allows for circularity in case we read
aspinall@21637
   678
   ourselves.  So PGIP opens the filename at the start of a script.
aspinall@21637
   679
   We ought to prevent problems by modifying the theory loader to know
wenzelm@21940
   680
   about this special status, but for now we just keep a local reference.
wenzelm@21940
   681
*)
aspinall@21637
   682
aspinall@21637
   683
val currently_open_file = ref (NONE : pgipurl option)
aspinall@21637
   684
aspinall@22163
   685
fun get_currently_open_file () = ! currently_open_file;
aspinall@22163
   686
wenzelm@23834
   687
fun askguise _ =
aspinall@21637
   688
    (* The "guise" is the PGIP abstraction of the prover's state.
aspinall@21637
   689
       The <informguise> message is merely used for consistency checking. *)
wenzelm@21940
   690
    let
wenzelm@21940
   691
        val openfile = !currently_open_file
aspinall@21637
   692
wenzelm@26603
   693
        val topthy = Toplevel.theory_of o Isar.state
wenzelm@21940
   694
        val topthy_name = Context.theory_name o topthy
aspinall@21637
   695
wenzelm@21940
   696
        val opentheory = SOME (topthy_name()) handle Toplevel.UNDEF => NONE
aspinall@21637
   697
wenzelm@21940
   698
        fun topproofpos () = try Toplevel.proof_position_of (Isar.state ());
wenzelm@21940
   699
        val openproofpos = topproofpos()
aspinall@21637
   700
    in
aspinall@21637
   701
        issue_pgip (Informguise { file = openfile,
wenzelm@21940
   702
                                  theory = opentheory,
wenzelm@21940
   703
                                  (* would be nice to get thm name... *)
wenzelm@21940
   704
                                  theorem = NONE,
wenzelm@21940
   705
                                  proofpos = openproofpos })
aspinall@21637
   706
    end
aspinall@21637
   707
haftmann@21902
   708
fun parsescript (Parsescript vs) =
aspinall@21637
   709
    let
wenzelm@21940
   710
        val text = #text vs
wenzelm@21940
   711
        val systemdata = #systemdata vs
wenzelm@21940
   712
        val location = #location vs   (* TODO: extract position *)
aspinall@21637
   713
wenzelm@28020
   714
        val doc = PgipParser.pgip_parser Position.none text
aspinall@21637
   715
wenzelm@21940
   716
        val sysattrs = PgipTypes.opt_attr "systemdata" systemdata
wenzelm@21940
   717
        val locattrs = PgipTypes.attrs_of_location location
aspinall@21637
   718
     in
aspinall@21637
   719
        issue_pgip (Parseresult { attrs= sysattrs@locattrs,
wenzelm@21940
   720
                                  doc = doc,
wenzelm@28037
   721
                                  errs = [] })
aspinall@21637
   722
    end
aspinall@21637
   723
wenzelm@23834
   724
fun showproofstate _ = isarcmd "pr"
aspinall@21637
   725
wenzelm@23834
   726
fun showctxt _ = isarcmd "print_context"
aspinall@21637
   727
haftmann@21902
   728
fun searchtheorems (Searchtheorems vs) =
wenzelm@21940
   729
    let
wenzelm@21940
   730
        val arg = #arg vs
aspinall@21637
   731
    in
wenzelm@21969
   732
        isarcmd ("find_theorems " ^ arg)
aspinall@21637
   733
    end
aspinall@21637
   734
wenzelm@21940
   735
fun setlinewidth (Setlinewidth vs) =
wenzelm@21940
   736
    let
wenzelm@21940
   737
        val width = #width vs
aspinall@21637
   738
    in
wenzelm@21940
   739
        isarcmd ("pretty_setmargin " ^ Int.toString width) (* FIXME: conversion back/forth! *)
aspinall@21637
   740
    end
aspinall@21637
   741
haftmann@21902
   742
fun viewdoc (Viewdoc vs) =
wenzelm@21940
   743
    let
wenzelm@21940
   744
        val arg = #arg vs
wenzelm@21940
   745
    in
wenzelm@28504
   746
        isarcmd ("print_" ^ arg)   (* FIXME: isabelle doc?.  Return URLs, maybe? *)
aspinall@21637
   747
    end
aspinall@21637
   748
aspinall@21867
   749
(*** Theory ***)
aspinall@21867
   750
haftmann@21902
   751
fun doitem (Doitem vs) =
aspinall@21637
   752
    let
wenzelm@21940
   753
        val text = #text vs
aspinall@21637
   754
    in
wenzelm@21940
   755
        isarcmd text
aspinall@21637
   756
    end
aspinall@21637
   757
wenzelm@23834
   758
fun undoitem _ =
aspinall@21972
   759
    isarcmd "undo"
aspinall@21637
   760
wenzelm@23834
   761
fun redoitem _ =
aspinall@21972
   762
    isarcmd "redo"
aspinall@21637
   763
wenzelm@23834
   764
fun aborttheory _ =
aspinall@21972
   765
    isarcmd "kill"  (* was: "init_toplevel" *)
aspinall@21637
   766
haftmann@21902
   767
fun retracttheory (Retracttheory vs) =
wenzelm@21940
   768
    let
wenzelm@21940
   769
        val thyname = #thyname vs
aspinall@21637
   770
    in
wenzelm@21940
   771
        isarcmd ("kill_thy " ^ quote thyname)
aspinall@21637
   772
    end
aspinall@21637
   773
aspinall@21867
   774
aspinall@21867
   775
(*** Files ***)
aspinall@21867
   776
aspinall@21867
   777
(* Path management: we allow theory files to have dependencies in
aspinall@21867
   778
   their own directory, but when we change directory for a new file we
aspinall@21867
   779
   remove the path.  Leaving it there can cause confusion with
aspinall@21867
   780
   difference in batch mode.
wenzelm@21940
   781
   NB: PGIP does not assume that the prover has a load path.
aspinall@21867
   782
*)
aspinall@21867
   783
aspinall@21867
   784
local
aspinall@21867
   785
    val current_working_dir = ref (NONE : string option)
aspinall@21867
   786
in
wenzelm@21940
   787
fun changecwd_dir newdirpath =
wenzelm@21940
   788
   let
aspinall@21867
   789
       val newdir = File.platform_path newdirpath
wenzelm@21940
   790
   in
aspinall@21867
   791
       (case (!current_working_dir) of
aspinall@21867
   792
            NONE => ()
aspinall@21867
   793
          | SOME dir => ThyLoad.del_path dir;
aspinall@21867
   794
        ThyLoad.add_path newdir;
aspinall@21867
   795
        current_working_dir := SOME newdir)
aspinall@21867
   796
   end
aspinall@21867
   797
end
aspinall@21867
   798
wenzelm@21940
   799
fun changecwd (Changecwd vs) =
wenzelm@21940
   800
    let
wenzelm@21940
   801
        val url = #url vs
wenzelm@21940
   802
        val newdir = PgipTypes.path_of_pgipurl url
aspinall@21867
   803
    in
wenzelm@21940
   804
        changecwd_dir url
aspinall@21867
   805
    end
aspinall@21867
   806
haftmann@21902
   807
fun openfile (Openfile vs) =
wenzelm@21940
   808
  let
aspinall@21867
   809
      val url = #url vs
aspinall@21867
   810
      val filepath = PgipTypes.path_of_pgipurl url
aspinall@21867
   811
      val filedir = Path.dir filepath
aspinall@21867
   812
      val thy_name = Path.implode o #1 o Path.split_ext o Path.base
aspinall@21867
   813
      val openfile_retract = Output.no_warnings (ThyInfo.if_known_thy ThyInfo.remove_thy) o thy_name;
aspinall@21867
   814
  in
aspinall@21867
   815
      case !currently_open_file of
aspinall@22028
   816
          SOME f => raise PGIP ("<openfile> when a file is already open!\nCurrently open file: " ^
wenzelm@22228
   817
                                PgipTypes.string_of_pgipurl url)
aspinall@21867
   818
        | NONE => (openfile_retract filepath;
wenzelm@21940
   819
                   changecwd_dir filedir;
wenzelm@22228
   820
                   priority ("Working in file: " ^ PgipTypes.string_of_pgipurl url);
wenzelm@21940
   821
                   currently_open_file := SOME url)
aspinall@21867
   822
  end
aspinall@21867
   823
wenzelm@23834
   824
fun closefile _ =
aspinall@21867
   825
    case !currently_open_file of
aspinall@22042
   826
        SOME f => (proper_inform_file_processed f (Isar.state());
wenzelm@22228
   827
                   priority ("Finished working in file: " ^ PgipTypes.string_of_pgipurl f);
aspinall@21867
   828
                   currently_open_file := NONE)
aspinall@21867
   829
      | NONE => raise PGIP ("<closefile> when no file is open!")
aspinall@21867
   830
wenzelm@21940
   831
fun loadfile (Loadfile vs) =
wenzelm@21940
   832
    let
wenzelm@21940
   833
        val url = #url vs
wenzelm@21940
   834
    in
aspinall@22171
   835
        (* da: this doesn't seem to cause a problem, batch loading uses
aspinall@22171
   836
           a different state context.  Of course confusion is still possible,
aspinall@22171
   837
           e.g. file loaded depends on open file which is not yet saved. *)
aspinall@22171
   838
        (* case !currently_open_file of
aspinall@22028
   839
            SOME f => raise PGIP ("<loadfile> when a file is open!\nCurrently open file: " ^
wenzelm@22228
   840
                                  PgipTypes.string_of_pgipurl url)
aspinall@22171
   841
          | NONE => *)
wenzelm@22228
   842
        use_thy_or_ml_file (File.platform_path url)
aspinall@21637
   843
    end
aspinall@21637
   844
wenzelm@23834
   845
fun abortfile _ =
aspinall@21637
   846
    case !currently_open_file of
aspinall@21637
   847
        SOME f => (isarcmd "init_toplevel";
wenzelm@22228
   848
                   priority ("Aborted working in file: " ^
wenzelm@22228
   849
                             PgipTypes.string_of_pgipurl f);
wenzelm@21940
   850
                   currently_open_file := NONE)
aspinall@21637
   851
      | NONE => raise PGIP ("<abortfile> when no file is open!")
aspinall@21637
   852
wenzelm@21940
   853
fun retractfile (Retractfile vs) =
wenzelm@21940
   854
    let
wenzelm@21940
   855
        val url = #url vs
aspinall@21637
   856
    in
wenzelm@21940
   857
        case !currently_open_file of
aspinall@21637
   858
            SOME f => raise PGIP ("<retractfile> when a file is open!")
aspinall@22028
   859
          | NONE => (priority ("Retracting file: " ^ PgipTypes.string_of_pgipurl url);
wenzelm@22228
   860
                     (* TODO: next should be in thy loader, here just for testing *)
wenzelm@22228
   861
                     let
wenzelm@22228
   862
                         val name = thy_name url
wenzelm@22228
   863
                     in List.app (tell_file_retracted false) (ThyInfo.loaded_files name) end;
wenzelm@22228
   864
                     inform_file_retracted url)
aspinall@21637
   865
    end
aspinall@21637
   866
aspinall@21637
   867
aspinall@21867
   868
(*** System ***)
aspinall@21637
   869
haftmann@21902
   870
fun systemcmd (Systemcmd vs) =
wenzelm@21940
   871
  let
aspinall@21637
   872
      val arg = #arg vs
aspinall@21637
   873
  in
aspinall@21637
   874
      isarcmd arg
aspinall@21637
   875
  end
aspinall@21637
   876
aspinall@21637
   877
exception PGIP_QUIT;
wenzelm@23834
   878
fun quitpgip _ = raise PGIP_QUIT
aspinall@21637
   879
haftmann@21902
   880
fun process_input inp = case inp
haftmann@21902
   881
 of Pgip.Askpgip _          => askpgip inp
haftmann@21902
   882
  | Pgip.Askpgml _          => askpgml inp
wenzelm@21940
   883
  | Pgip.Askprefs _         => askprefs inp
haftmann@21902
   884
  | Pgip.Askconfig _        => askconfig inp
haftmann@21902
   885
  | Pgip.Getpref _          => getpref inp
haftmann@21902
   886
  | Pgip.Setpref _          => setpref inp
haftmann@21902
   887
  | Pgip.Proverinit _       => proverinit inp
haftmann@21902
   888
  | Pgip.Proverexit _       => proverexit inp
aspinall@22408
   889
  | Pgip.Setproverflag _    => setproverflag inp
haftmann@21902
   890
  | Pgip.Dostep _           => dostep inp
haftmann@21902
   891
  | Pgip.Undostep _         => undostep inp
haftmann@21902
   892
  | Pgip.Redostep _         => redostep inp
aspinall@23435
   893
  | Pgip.Forget _           => error "<forget> not implemented by Isabelle"
aspinall@23435
   894
  | Pgip.Restoregoal _      => error "<restoregoal> not implemented by Isabelle"
haftmann@21902
   895
  | Pgip.Abortgoal _        => abortgoal inp
haftmann@21902
   896
  | Pgip.Askids _           => askids inp
aspinall@22159
   897
  | Pgip.Askrefs _          => askrefs inp
haftmann@21902
   898
  | Pgip.Showid _           => showid inp
haftmann@21902
   899
  | Pgip.Askguise _         => askguise inp
haftmann@21902
   900
  | Pgip.Parsescript _      => parsescript inp
haftmann@21902
   901
  | Pgip.Showproofstate _   => showproofstate inp
haftmann@21902
   902
  | Pgip.Showctxt _         => showctxt inp
haftmann@21902
   903
  | Pgip.Searchtheorems _   => searchtheorems inp
haftmann@21902
   904
  | Pgip.Setlinewidth _     => setlinewidth inp
haftmann@21902
   905
  | Pgip.Viewdoc _          => viewdoc inp
haftmann@21902
   906
  | Pgip.Doitem _           => doitem inp
haftmann@21902
   907
  | Pgip.Undoitem _         => undoitem inp
haftmann@21902
   908
  | Pgip.Redoitem _         => redoitem inp
haftmann@21902
   909
  | Pgip.Aborttheory _      => aborttheory inp
haftmann@21902
   910
  | Pgip.Retracttheory _    => retracttheory inp
haftmann@21902
   911
  | Pgip.Loadfile _         => loadfile inp
haftmann@21902
   912
  | Pgip.Openfile _         => openfile inp
haftmann@21902
   913
  | Pgip.Closefile _        => closefile inp
haftmann@21902
   914
  | Pgip.Abortfile _        => abortfile inp
haftmann@21902
   915
  | Pgip.Retractfile _      => retractfile inp
haftmann@21902
   916
  | Pgip.Changecwd _        => changecwd inp
haftmann@21902
   917
  | Pgip.Systemcmd _        => systemcmd inp
haftmann@21902
   918
  | Pgip.Quitpgip _         => quitpgip inp
aspinall@21637
   919
aspinall@21637
   920
wenzelm@21940
   921
fun process_pgip_element pgipxml =
aspinall@21637
   922
    case pgipxml of
wenzelm@21969
   923
        xml as (XML.Elem elem) =>
wenzelm@21940
   924
        (case Pgip.input elem of
wenzelm@21940
   925
             NONE => warning ("Unrecognized PGIP command, ignored: \n" ^
wenzelm@26541
   926
                              (XML.string_of xml))
wenzelm@21940
   927
           | SOME inp => (process_input inp)) (* errors later; packet discarded *)
wenzelm@21969
   928
      | XML.Text t => ignored_text_warning t
aspinall@21637
   929
and ignored_text_warning t =
wenzelm@21940
   930
    if size (Symbol.strip_blanks t) > 0 then
wenzelm@21940
   931
           warning ("Ignored text in PGIP packet: \n" ^ t)
aspinall@21637
   932
    else ()
aspinall@21637
   933
aspinall@21637
   934
fun process_pgip_tree xml =
aspinall@21637
   935
    (pgip_refid := NONE;
aspinall@21637
   936
     pgip_refseq := NONE;
aspinall@21637
   937
     (case xml of
aspinall@21637
   938
          XML.Elem ("pgip", attrs, pgips) =>
aspinall@21637
   939
          (let
aspinall@21637
   940
               val class = PgipTypes.get_attr "class" attrs
aspinall@21637
   941
               val dest  = PgipTypes.get_attr_opt "destid" attrs
wenzelm@21940
   942
               val seq = PgipTypes.read_pgipnat (PgipTypes.get_attr "seq" attrs)
aspinall@21637
   943
               (* Respond to prover broadcasts, or messages for us. Ignore rest *)
wenzelm@21940
   944
               val processit =
wenzelm@21940
   945
                   case dest of
aspinall@21637
   946
                       NONE =>    class = "pa"
wenzelm@21940
   947
                     | SOME id => matching_pgip_id id
wenzelm@21940
   948
           in if processit then
wenzelm@21940
   949
                  (pgip_refid :=  PgipTypes.get_attr_opt "id" attrs;
wenzelm@21940
   950
                   pgip_refseq := SOME seq;
wenzelm@21940
   951
                   List.app process_pgip_element pgips;
wenzelm@21940
   952
                   (* return true to indicate <ready/> *)
wenzelm@21940
   953
                   true)
wenzelm@21940
   954
              else
wenzelm@21940
   955
                  (* no response to ignored messages. *)
wenzelm@21940
   956
                  false
aspinall@21637
   957
           end)
aspinall@21637
   958
        | _ => raise PGIP "Invalid PGIP packet received")
aspinall@21637
   959
     handle PGIP msg =>
aspinall@21637
   960
            (Output.error_msg ((msg ^ "\nPGIP error occured in XML text below:\n") ^
wenzelm@26541
   961
                               (XML.string_of xml));
wenzelm@21940
   962
             true))
aspinall@21637
   963
aspinall@21649
   964
(* External input *)
aspinall@21649
   965
wenzelm@26541
   966
val process_pgip_plain = K () o process_pgip_tree o XML.parse
aspinall@21637
   967
aspinall@21637
   968
(* PGIP loop: process PGIP input only *)
aspinall@21637
   969
aspinall@21637
   970
local
aspinall@21637
   971
aspinall@21637
   972
exception XML_PARSE
aspinall@21637
   973
aspinall@21637
   974
fun loop ready src =
aspinall@21637
   975
    let
aspinall@21637
   976
        val _ = if ready then issue_pgip (Ready ()) else ()
wenzelm@21969
   977
        val pgipo =
wenzelm@21969
   978
          (case try Source.get_single src of
wenzelm@21969
   979
            SOME pgipo => pgipo
wenzelm@21969
   980
          | NONE => raise XML_PARSE)
aspinall@21637
   981
    in
aspinall@21637
   982
        case pgipo of
aspinall@21637
   983
             NONE  => ()
aspinall@21637
   984
           | SOME (pgip,src') =>
aspinall@21637
   985
             let
wenzelm@21940
   986
                 val ready' = (process_pgip_tree pgip)
aspinall@22337
   987
                                handle PGIP_QUIT => raise PGIP_QUIT
wenzelm@22590
   988
                                     | e => (handler (e,SOME src'); true)
aspinall@21637
   989
             in
aspinall@21637
   990
                 loop ready' src'
aspinall@21637
   991
             end
aspinall@21637
   992
    end handle e => handler (e,SOME src)  (* error in XML parse or Ready issue *)
aspinall@21637
   993
aspinall@21637
   994
and handler (e,srco) =
aspinall@21637
   995
    case (e,srco) of
aspinall@21637
   996
        (XML_PARSE,SOME src) =>
wenzelm@22699
   997
        panic "Invalid XML input, aborting" (* TODO: attempt recovery  *)
wenzelm@28443
   998
      | (Exn.Interrupt,SOME src) =>
wenzelm@21940
   999
        (Output.error_msg "Interrupt during PGIP processing"; loop true src)
wenzelm@21940
  1000
      | (Toplevel.UNDEF,SOME src) =>
wenzelm@21940
  1001
        (Output.error_msg "No working context defined"; loop true src)
wenzelm@21940
  1002
      | (e,SOME src) =>
wenzelm@31478
  1003
        (Output.error_msg (ML_Compiler.exn_message e); loop true src)
aspinall@22337
  1004
      | (PGIP_QUIT,_) => ()
aspinall@21637
  1005
      | (_,NONE) => ()
aspinall@21637
  1006
in
aspinall@21637
  1007
  (* TODO: add socket interface *)
aspinall@21637
  1008
wenzelm@26552
  1009
  val xmlP = XML.parse_comments |-- XML.parse_element >> single
aspinall@21637
  1010
aspinall@21637
  1011
  val tty_src = Source.set_prompt "" (Source.source Symbol.stopper xmlP NONE Source.tty)
aspinall@21637
  1012
aspinall@21637
  1013
  fun pgip_toplevel x = loop true x
aspinall@21637
  1014
end
aspinall@21637
  1015
aspinall@21637
  1016
aspinall@21972
  1017
(* Extra command for embedding prover-control inside document (obscure/debug usage). *)
aspinall@21637
  1018
wenzelm@24867
  1019
fun init_outer_syntax () =
aspinall@21972
  1020
  OuterSyntax.improper_command "ProofGeneral.process_pgip" "(internal)" OuterKeyword.control
aspinall@21972
  1021
    (OuterParse.text >> (Toplevel.no_timing oo
aspinall@21649
  1022
      (fn txt => Toplevel.imperative (fn () => process_pgip_plain txt))));
aspinall@21637
  1023
aspinall@21637
  1024
aspinall@21637
  1025
(* init *)
aspinall@21637
  1026
aspinall@21637
  1027
val initialized = ref false;
aspinall@21637
  1028
wenzelm@22699
  1029
fun init_pgip false = panic "No Proof General interface support for Isabelle/classic mode."
wenzelm@21969
  1030
  | init_pgip true =
wenzelm@21969
  1031
      (! initialized orelse
aspinall@25445
  1032
        (setup_preferences_tweak ();
wenzelm@28037
  1033
         Output.add_mode proof_generalN Output.default_output Output.default_escape;
wenzelm@28037
  1034
         Markup.add_mode proof_generalN YXML.output_markup;
aspinall@25445
  1035
         setup_messages ();
aspinall@25445
  1036
         Output.no_warnings init_outer_syntax ();
aspinall@25445
  1037
         setup_thy_loader ();
aspinall@25445
  1038
         setup_present_hook ();
aspinall@25445
  1039
         init_pgip_session_id ();
aspinall@25445
  1040
         welcome ();
aspinall@25445
  1041
         set initialized);
wenzelm@21969
  1042
        sync_thy_loader ();
wenzelm@28037
  1043
       change print_mode (update (op =) proof_generalN);
wenzelm@21969
  1044
       pgip_toplevel tty_src);
aspinall@21637
  1045
aspinall@21649
  1046
aspinall@21649
  1047
aspinall@21649
  1048
(** Out-of-loop PGIP commands (for Emacs hybrid mode) **)
aspinall@21649
  1049
aspinall@21649
  1050
local
wenzelm@22590
  1051
    val pgip_output_channel = ref Output.writeln_default
aspinall@21649
  1052
in
aspinall@21649
  1053
aspinall@21649
  1054
(* Set recipient for PGIP results *)
aspinall@21649
  1055
fun init_pgip_channel writefn =
wenzelm@21940
  1056
    (init_pgip_session_id();
wenzelm@21940
  1057
     pgip_output_channel := writefn)
aspinall@21649
  1058
wenzelm@21940
  1059
(* Process a PGIP command.
wenzelm@21940
  1060
   This works for preferences but not generally guaranteed
aspinall@21649
  1061
   because we haven't done full setup here (e.g., no pgml mode)  *)
aspinall@21649
  1062
fun process_pgip str =
aspinall@21649
  1063
     setmp output_xml_fn (!pgip_output_channel) process_pgip_plain str
aspinall@21649
  1064
aspinall@21649
  1065
end
aspinall@21637
  1066
aspinall@21637
  1067
end;