src/Pure/ProofGeneral/proof_general_pgip.ML
author wenzelm
Tue Jan 27 00:29:37 2009 +0100 (2009-01-27)
changeset 29635 31d14e9fa0da
parent 29606 fedb8be05f24
child 30364 577edc39b501
permissions -rw-r--r--
proof_body: turned lazy into future -- ensures that body is fulfilled eventually, without explicit force;
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);
wenzelm@23641
    70
wenzelm@28037
    71
val output_pgips = XML.string_of o PgipOutput.output o assemble_pgips o map PgipOutput.output;
aspinall@23759
    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
aspinall@21637
   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@22408
   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@22243
   546
        val immed_thms_of_thy = filter_out NameSpace.is_qualified o thms_of_thy
aspinall@22225
   547
        fun thy_prefix s = case space_explode NameSpace.separator 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@22228
   556
               | SOME prf => filter (String.isPrefix (unprefix (prf ^ NameSpace.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@22590
   645
            let val comps = NameSpace.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@28037
   658
        fun string_of_thm th = Pretty.string_of
wenzelm@22590
   659
                                   (Display.pretty_thm_aux
wenzelm@26939
   660
                                        (Syntax.pp_global (Thm.theory_of_thm th))
wenzelm@22590
   661
                                        false (* quote *)
wenzelm@22590
   662
                                        false (* show hyps *)
wenzelm@22590
   663
                                        [] (* asms *)
wenzelm@28037
   664
                                        th)
aspinall@22337
   665
wenzelm@26343
   666
        fun strings_of_thm (thy, name) = map string_of_thm (PureThy.get_thms thy name)
aspinall@22337
   667
wenzelm@28037
   668
        val string_of_thy = Pretty.string_of o ProofDisplay.pretty_full_theory false
wenzelm@21940
   669
    in
wenzelm@21940
   670
        case (thyname, objtype) of
wenzelm@28037
   671
            (_, ObjTheory) => idvalue [string_of_thy (ThyInfo.get_theory name)]
aspinall@22337
   672
          | (SOME thy, ObjTheorem) => idvalue (strings_of_thm (ThyInfo.get_theory thy, name))
aspinall@22337
   673
          | (NONE, ObjTheorem) => idvalue (strings_of_thm (splitthy name))
wenzelm@21940
   674
          | (_, ot) => error ("Cannot show objects of type "^(PgipTypes.name_of_objtype ot))
aspinall@21637
   675
    end
aspinall@21637
   676
aspinall@21867
   677
(*** Inspecting state ***)
aspinall@21867
   678
wenzelm@21940
   679
(* The file which is currently being processed interactively.
aspinall@21637
   680
   In the pre-PGIP code, this was informed to Isabelle and the theory loader
aspinall@21637
   681
   on completion, but that allows for circularity in case we read
aspinall@21637
   682
   ourselves.  So PGIP opens the filename at the start of a script.
aspinall@21637
   683
   We ought to prevent problems by modifying the theory loader to know
wenzelm@21940
   684
   about this special status, but for now we just keep a local reference.
wenzelm@21940
   685
*)
aspinall@21637
   686
aspinall@21637
   687
val currently_open_file = ref (NONE : pgipurl option)
aspinall@21637
   688
aspinall@22163
   689
fun get_currently_open_file () = ! currently_open_file;
aspinall@22163
   690
wenzelm@23834
   691
fun askguise _ =
aspinall@21637
   692
    (* The "guise" is the PGIP abstraction of the prover's state.
aspinall@21637
   693
       The <informguise> message is merely used for consistency checking. *)
wenzelm@21940
   694
    let
wenzelm@21940
   695
        val openfile = !currently_open_file
aspinall@21637
   696
wenzelm@26603
   697
        val topthy = Toplevel.theory_of o Isar.state
wenzelm@21940
   698
        val topthy_name = Context.theory_name o topthy
aspinall@21637
   699
wenzelm@21940
   700
        val opentheory = SOME (topthy_name()) handle Toplevel.UNDEF => NONE
aspinall@21637
   701
wenzelm@21940
   702
        fun topproofpos () = try Toplevel.proof_position_of (Isar.state ());
wenzelm@21940
   703
        val openproofpos = topproofpos()
aspinall@21637
   704
    in
aspinall@21637
   705
        issue_pgip (Informguise { file = openfile,
wenzelm@21940
   706
                                  theory = opentheory,
wenzelm@21940
   707
                                  (* would be nice to get thm name... *)
wenzelm@21940
   708
                                  theorem = NONE,
wenzelm@21940
   709
                                  proofpos = openproofpos })
aspinall@21637
   710
    end
aspinall@21637
   711
haftmann@21902
   712
fun parsescript (Parsescript vs) =
aspinall@21637
   713
    let
wenzelm@21940
   714
        val text = #text vs
wenzelm@21940
   715
        val systemdata = #systemdata vs
wenzelm@21940
   716
        val location = #location vs   (* TODO: extract position *)
aspinall@21637
   717
wenzelm@28020
   718
        val doc = PgipParser.pgip_parser Position.none text
aspinall@21637
   719
wenzelm@21940
   720
        val sysattrs = PgipTypes.opt_attr "systemdata" systemdata
wenzelm@21940
   721
        val locattrs = PgipTypes.attrs_of_location location
aspinall@21637
   722
     in
aspinall@21637
   723
        issue_pgip (Parseresult { attrs= sysattrs@locattrs,
wenzelm@21940
   724
                                  doc = doc,
wenzelm@28037
   725
                                  errs = [] })
aspinall@21637
   726
    end
aspinall@21637
   727
wenzelm@23834
   728
fun showproofstate _ = isarcmd "pr"
aspinall@21637
   729
wenzelm@23834
   730
fun showctxt _ = isarcmd "print_context"
aspinall@21637
   731
haftmann@21902
   732
fun searchtheorems (Searchtheorems vs) =
wenzelm@21940
   733
    let
wenzelm@21940
   734
        val arg = #arg vs
aspinall@21637
   735
    in
wenzelm@21969
   736
        isarcmd ("find_theorems " ^ arg)
aspinall@21637
   737
    end
aspinall@21637
   738
wenzelm@21940
   739
fun setlinewidth (Setlinewidth vs) =
wenzelm@21940
   740
    let
wenzelm@21940
   741
        val width = #width vs
aspinall@21637
   742
    in
wenzelm@21940
   743
        isarcmd ("pretty_setmargin " ^ Int.toString width) (* FIXME: conversion back/forth! *)
aspinall@21637
   744
    end
aspinall@21637
   745
haftmann@21902
   746
fun viewdoc (Viewdoc vs) =
wenzelm@21940
   747
    let
wenzelm@21940
   748
        val arg = #arg vs
wenzelm@21940
   749
    in
wenzelm@28504
   750
        isarcmd ("print_" ^ arg)   (* FIXME: isabelle doc?.  Return URLs, maybe? *)
aspinall@21637
   751
    end
aspinall@21637
   752
aspinall@21867
   753
(*** Theory ***)
aspinall@21867
   754
haftmann@21902
   755
fun doitem (Doitem vs) =
aspinall@21637
   756
    let
wenzelm@21940
   757
        val text = #text vs
aspinall@21637
   758
    in
wenzelm@21940
   759
        isarcmd text
aspinall@21637
   760
    end
aspinall@21637
   761
wenzelm@23834
   762
fun undoitem _ =
aspinall@21972
   763
    isarcmd "undo"
aspinall@21637
   764
wenzelm@23834
   765
fun redoitem _ =
aspinall@21972
   766
    isarcmd "redo"
aspinall@21637
   767
wenzelm@23834
   768
fun aborttheory _ =
aspinall@21972
   769
    isarcmd "kill"  (* was: "init_toplevel" *)
aspinall@21637
   770
haftmann@21902
   771
fun retracttheory (Retracttheory vs) =
wenzelm@21940
   772
    let
wenzelm@21940
   773
        val thyname = #thyname vs
aspinall@21637
   774
    in
wenzelm@21940
   775
        isarcmd ("kill_thy " ^ quote thyname)
aspinall@21637
   776
    end
aspinall@21637
   777
aspinall@21867
   778
aspinall@21867
   779
(*** Files ***)
aspinall@21867
   780
aspinall@21867
   781
(* Path management: we allow theory files to have dependencies in
aspinall@21867
   782
   their own directory, but when we change directory for a new file we
aspinall@21867
   783
   remove the path.  Leaving it there can cause confusion with
aspinall@21867
   784
   difference in batch mode.
wenzelm@21940
   785
   NB: PGIP does not assume that the prover has a load path.
aspinall@21867
   786
*)
aspinall@21867
   787
aspinall@21867
   788
local
aspinall@21867
   789
    val current_working_dir = ref (NONE : string option)
aspinall@21867
   790
in
wenzelm@21940
   791
fun changecwd_dir newdirpath =
wenzelm@21940
   792
   let
aspinall@21867
   793
       val newdir = File.platform_path newdirpath
wenzelm@21940
   794
   in
aspinall@21867
   795
       (case (!current_working_dir) of
aspinall@21867
   796
            NONE => ()
aspinall@21867
   797
          | SOME dir => ThyLoad.del_path dir;
aspinall@21867
   798
        ThyLoad.add_path newdir;
aspinall@21867
   799
        current_working_dir := SOME newdir)
aspinall@21867
   800
   end
aspinall@21867
   801
end
aspinall@21867
   802
wenzelm@21940
   803
fun changecwd (Changecwd vs) =
wenzelm@21940
   804
    let
wenzelm@21940
   805
        val url = #url vs
wenzelm@21940
   806
        val newdir = PgipTypes.path_of_pgipurl url
aspinall@21867
   807
    in
wenzelm@21940
   808
        changecwd_dir url
aspinall@21867
   809
    end
aspinall@21867
   810
haftmann@21902
   811
fun openfile (Openfile vs) =
wenzelm@21940
   812
  let
aspinall@21867
   813
      val url = #url vs
aspinall@21867
   814
      val filepath = PgipTypes.path_of_pgipurl url
aspinall@21867
   815
      val filedir = Path.dir filepath
aspinall@21867
   816
      val thy_name = Path.implode o #1 o Path.split_ext o Path.base
aspinall@21867
   817
      val openfile_retract = Output.no_warnings (ThyInfo.if_known_thy ThyInfo.remove_thy) o thy_name;
aspinall@21867
   818
  in
aspinall@21867
   819
      case !currently_open_file of
aspinall@22028
   820
          SOME f => raise PGIP ("<openfile> when a file is already open!\nCurrently open file: " ^
wenzelm@22228
   821
                                PgipTypes.string_of_pgipurl url)
aspinall@21867
   822
        | NONE => (openfile_retract filepath;
wenzelm@21940
   823
                   changecwd_dir filedir;
wenzelm@22228
   824
                   priority ("Working in file: " ^ PgipTypes.string_of_pgipurl url);
wenzelm@21940
   825
                   currently_open_file := SOME url)
aspinall@21867
   826
  end
aspinall@21867
   827
wenzelm@23834
   828
fun closefile _ =
aspinall@21867
   829
    case !currently_open_file of
aspinall@22042
   830
        SOME f => (proper_inform_file_processed f (Isar.state());
wenzelm@22228
   831
                   priority ("Finished working in file: " ^ PgipTypes.string_of_pgipurl f);
aspinall@21867
   832
                   currently_open_file := NONE)
aspinall@21867
   833
      | NONE => raise PGIP ("<closefile> when no file is open!")
aspinall@21867
   834
wenzelm@21940
   835
fun loadfile (Loadfile vs) =
wenzelm@21940
   836
    let
wenzelm@21940
   837
        val url = #url vs
wenzelm@21940
   838
    in
aspinall@22171
   839
        (* da: this doesn't seem to cause a problem, batch loading uses
aspinall@22171
   840
           a different state context.  Of course confusion is still possible,
aspinall@22171
   841
           e.g. file loaded depends on open file which is not yet saved. *)
aspinall@22171
   842
        (* case !currently_open_file of
aspinall@22028
   843
            SOME f => raise PGIP ("<loadfile> when a file is open!\nCurrently open file: " ^
wenzelm@22228
   844
                                  PgipTypes.string_of_pgipurl url)
aspinall@22171
   845
          | NONE => *)
wenzelm@22228
   846
        use_thy_or_ml_file (File.platform_path url)
aspinall@21637
   847
    end
aspinall@21637
   848
wenzelm@23834
   849
fun abortfile _ =
aspinall@21637
   850
    case !currently_open_file of
aspinall@21637
   851
        SOME f => (isarcmd "init_toplevel";
wenzelm@22228
   852
                   priority ("Aborted working in file: " ^
wenzelm@22228
   853
                             PgipTypes.string_of_pgipurl f);
wenzelm@21940
   854
                   currently_open_file := NONE)
aspinall@21637
   855
      | NONE => raise PGIP ("<abortfile> when no file is open!")
aspinall@21637
   856
wenzelm@21940
   857
fun retractfile (Retractfile vs) =
wenzelm@21940
   858
    let
wenzelm@21940
   859
        val url = #url vs
aspinall@21637
   860
    in
wenzelm@21940
   861
        case !currently_open_file of
aspinall@21637
   862
            SOME f => raise PGIP ("<retractfile> when a file is open!")
aspinall@22028
   863
          | NONE => (priority ("Retracting file: " ^ PgipTypes.string_of_pgipurl url);
wenzelm@22228
   864
                     (* TODO: next should be in thy loader, here just for testing *)
wenzelm@22228
   865
                     let
wenzelm@22228
   866
                         val name = thy_name url
wenzelm@22228
   867
                     in List.app (tell_file_retracted false) (ThyInfo.loaded_files name) end;
wenzelm@22228
   868
                     inform_file_retracted url)
aspinall@21637
   869
    end
aspinall@21637
   870
aspinall@21637
   871
aspinall@21867
   872
(*** System ***)
aspinall@21637
   873
haftmann@21902
   874
fun systemcmd (Systemcmd vs) =
wenzelm@21940
   875
  let
aspinall@21637
   876
      val arg = #arg vs
aspinall@21637
   877
  in
aspinall@21637
   878
      isarcmd arg
aspinall@21637
   879
  end
aspinall@21637
   880
aspinall@21637
   881
exception PGIP_QUIT;
wenzelm@23834
   882
fun quitpgip _ = raise PGIP_QUIT
aspinall@21637
   883
haftmann@21902
   884
fun process_input inp = case inp
haftmann@21902
   885
 of Pgip.Askpgip _          => askpgip inp
haftmann@21902
   886
  | Pgip.Askpgml _          => askpgml inp
wenzelm@21940
   887
  | Pgip.Askprefs _         => askprefs inp
haftmann@21902
   888
  | Pgip.Askconfig _        => askconfig inp
haftmann@21902
   889
  | Pgip.Getpref _          => getpref inp
haftmann@21902
   890
  | Pgip.Setpref _          => setpref inp
haftmann@21902
   891
  | Pgip.Proverinit _       => proverinit inp
haftmann@21902
   892
  | Pgip.Proverexit _       => proverexit inp
aspinall@22408
   893
  | Pgip.Setproverflag _    => setproverflag inp
haftmann@21902
   894
  | Pgip.Dostep _           => dostep inp
haftmann@21902
   895
  | Pgip.Undostep _         => undostep inp
haftmann@21902
   896
  | Pgip.Redostep _         => redostep inp
aspinall@23435
   897
  | Pgip.Forget _           => error "<forget> not implemented by Isabelle"
aspinall@23435
   898
  | Pgip.Restoregoal _      => error "<restoregoal> not implemented by Isabelle"
haftmann@21902
   899
  | Pgip.Abortgoal _        => abortgoal inp
haftmann@21902
   900
  | Pgip.Askids _           => askids inp
aspinall@22159
   901
  | Pgip.Askrefs _          => askrefs inp
haftmann@21902
   902
  | Pgip.Showid _           => showid inp
haftmann@21902
   903
  | Pgip.Askguise _         => askguise inp
haftmann@21902
   904
  | Pgip.Parsescript _      => parsescript inp
haftmann@21902
   905
  | Pgip.Showproofstate _   => showproofstate inp
haftmann@21902
   906
  | Pgip.Showctxt _         => showctxt inp
haftmann@21902
   907
  | Pgip.Searchtheorems _   => searchtheorems inp
haftmann@21902
   908
  | Pgip.Setlinewidth _     => setlinewidth inp
haftmann@21902
   909
  | Pgip.Viewdoc _          => viewdoc inp
haftmann@21902
   910
  | Pgip.Doitem _           => doitem inp
haftmann@21902
   911
  | Pgip.Undoitem _         => undoitem inp
haftmann@21902
   912
  | Pgip.Redoitem _         => redoitem inp
haftmann@21902
   913
  | Pgip.Aborttheory _      => aborttheory inp
haftmann@21902
   914
  | Pgip.Retracttheory _    => retracttheory inp
haftmann@21902
   915
  | Pgip.Loadfile _         => loadfile inp
haftmann@21902
   916
  | Pgip.Openfile _         => openfile inp
haftmann@21902
   917
  | Pgip.Closefile _        => closefile inp
haftmann@21902
   918
  | Pgip.Abortfile _        => abortfile inp
haftmann@21902
   919
  | Pgip.Retractfile _      => retractfile inp
haftmann@21902
   920
  | Pgip.Changecwd _        => changecwd inp
haftmann@21902
   921
  | Pgip.Systemcmd _        => systemcmd inp
haftmann@21902
   922
  | Pgip.Quitpgip _         => quitpgip inp
aspinall@21637
   923
aspinall@21637
   924
wenzelm@21940
   925
fun process_pgip_element pgipxml =
aspinall@21637
   926
    case pgipxml of
wenzelm@21969
   927
        xml as (XML.Elem elem) =>
wenzelm@21940
   928
        (case Pgip.input elem of
wenzelm@21940
   929
             NONE => warning ("Unrecognized PGIP command, ignored: \n" ^
wenzelm@26541
   930
                              (XML.string_of xml))
wenzelm@21940
   931
           | SOME inp => (process_input inp)) (* errors later; packet discarded *)
wenzelm@21969
   932
      | XML.Text t => ignored_text_warning t
aspinall@21637
   933
and ignored_text_warning t =
wenzelm@21940
   934
    if size (Symbol.strip_blanks t) > 0 then
wenzelm@21940
   935
           warning ("Ignored text in PGIP packet: \n" ^ t)
aspinall@21637
   936
    else ()
aspinall@21637
   937
aspinall@21637
   938
fun process_pgip_tree xml =
aspinall@21637
   939
    (pgip_refid := NONE;
aspinall@21637
   940
     pgip_refseq := NONE;
aspinall@21637
   941
     (case xml of
aspinall@21637
   942
          XML.Elem ("pgip", attrs, pgips) =>
aspinall@21637
   943
          (let
aspinall@21637
   944
               val class = PgipTypes.get_attr "class" attrs
aspinall@21637
   945
               val dest  = PgipTypes.get_attr_opt "destid" attrs
wenzelm@21940
   946
               val seq = PgipTypes.read_pgipnat (PgipTypes.get_attr "seq" attrs)
aspinall@21637
   947
               (* Respond to prover broadcasts, or messages for us. Ignore rest *)
wenzelm@21940
   948
               val processit =
wenzelm@21940
   949
                   case dest of
aspinall@21637
   950
                       NONE =>    class = "pa"
wenzelm@21940
   951
                     | SOME id => matching_pgip_id id
wenzelm@21940
   952
           in if processit then
wenzelm@21940
   953
                  (pgip_refid :=  PgipTypes.get_attr_opt "id" attrs;
wenzelm@21940
   954
                   pgip_refseq := SOME seq;
wenzelm@21940
   955
                   List.app process_pgip_element pgips;
wenzelm@21940
   956
                   (* return true to indicate <ready/> *)
wenzelm@21940
   957
                   true)
wenzelm@21940
   958
              else
wenzelm@21940
   959
                  (* no response to ignored messages. *)
wenzelm@21940
   960
                  false
aspinall@21637
   961
           end)
aspinall@21637
   962
        | _ => raise PGIP "Invalid PGIP packet received")
aspinall@21637
   963
     handle PGIP msg =>
aspinall@21637
   964
            (Output.error_msg ((msg ^ "\nPGIP error occured in XML text below:\n") ^
wenzelm@26541
   965
                               (XML.string_of xml));
wenzelm@21940
   966
             true))
aspinall@21637
   967
aspinall@21649
   968
(* External input *)
aspinall@21649
   969
wenzelm@26541
   970
val process_pgip_plain = K () o process_pgip_tree o XML.parse
aspinall@21637
   971
aspinall@21637
   972
(* PGIP loop: process PGIP input only *)
aspinall@21637
   973
aspinall@21637
   974
local
aspinall@21637
   975
aspinall@21637
   976
exception XML_PARSE
aspinall@21637
   977
aspinall@21637
   978
fun loop ready src =
aspinall@21637
   979
    let
aspinall@21637
   980
        val _ = if ready then issue_pgip (Ready ()) else ()
wenzelm@21969
   981
        val pgipo =
wenzelm@21969
   982
          (case try Source.get_single src of
wenzelm@21969
   983
            SOME pgipo => pgipo
wenzelm@21969
   984
          | NONE => raise XML_PARSE)
aspinall@21637
   985
    in
aspinall@21637
   986
        case pgipo of
aspinall@21637
   987
             NONE  => ()
aspinall@21637
   988
           | SOME (pgip,src') =>
aspinall@21637
   989
             let
wenzelm@21940
   990
                 val ready' = (process_pgip_tree pgip)
aspinall@22337
   991
                                handle PGIP_QUIT => raise PGIP_QUIT
wenzelm@22590
   992
                                     | e => (handler (e,SOME src'); true)
aspinall@21637
   993
             in
aspinall@21637
   994
                 loop ready' src'
aspinall@21637
   995
             end
aspinall@21637
   996
    end handle e => handler (e,SOME src)  (* error in XML parse or Ready issue *)
aspinall@21637
   997
aspinall@21637
   998
and handler (e,srco) =
aspinall@21637
   999
    case (e,srco) of
aspinall@21637
  1000
        (XML_PARSE,SOME src) =>
wenzelm@22699
  1001
        panic "Invalid XML input, aborting" (* TODO: attempt recovery  *)
wenzelm@28443
  1002
      | (Exn.Interrupt,SOME src) =>
wenzelm@21940
  1003
        (Output.error_msg "Interrupt during PGIP processing"; loop true src)
wenzelm@21940
  1004
      | (Toplevel.UNDEF,SOME src) =>
wenzelm@21940
  1005
        (Output.error_msg "No working context defined"; loop true src)
wenzelm@21940
  1006
      | (e,SOME src) =>
wenzelm@21940
  1007
        (Output.error_msg (Toplevel.exn_message e); loop true src)
aspinall@22337
  1008
      | (PGIP_QUIT,_) => ()
aspinall@21637
  1009
      | (_,NONE) => ()
aspinall@21637
  1010
in
aspinall@21637
  1011
  (* TODO: add socket interface *)
aspinall@21637
  1012
wenzelm@26552
  1013
  val xmlP = XML.parse_comments |-- XML.parse_element >> single
aspinall@21637
  1014
aspinall@21637
  1015
  val tty_src = Source.set_prompt "" (Source.source Symbol.stopper xmlP NONE Source.tty)
aspinall@21637
  1016
aspinall@21637
  1017
  fun pgip_toplevel x = loop true x
aspinall@21637
  1018
end
aspinall@21637
  1019
aspinall@21637
  1020
aspinall@21972
  1021
(* Extra command for embedding prover-control inside document (obscure/debug usage). *)
aspinall@21637
  1022
wenzelm@24867
  1023
fun init_outer_syntax () =
aspinall@21972
  1024
  OuterSyntax.improper_command "ProofGeneral.process_pgip" "(internal)" OuterKeyword.control
aspinall@21972
  1025
    (OuterParse.text >> (Toplevel.no_timing oo
aspinall@21649
  1026
      (fn txt => Toplevel.imperative (fn () => process_pgip_plain txt))));
aspinall@21637
  1027
aspinall@21637
  1028
aspinall@21637
  1029
(* init *)
aspinall@21637
  1030
aspinall@21637
  1031
val initialized = ref false;
aspinall@21637
  1032
wenzelm@22699
  1033
fun init_pgip false = panic "No Proof General interface support for Isabelle/classic mode."
wenzelm@21969
  1034
  | init_pgip true =
wenzelm@21969
  1035
      (! initialized orelse
aspinall@25445
  1036
        (setup_preferences_tweak ();
wenzelm@28037
  1037
         Output.add_mode proof_generalN Output.default_output Output.default_escape;
wenzelm@28037
  1038
         Markup.add_mode proof_generalN YXML.output_markup;
aspinall@25445
  1039
         setup_messages ();
aspinall@25445
  1040
         Output.no_warnings init_outer_syntax ();
aspinall@25445
  1041
         setup_thy_loader ();
aspinall@25445
  1042
         setup_present_hook ();
aspinall@25445
  1043
         init_pgip_session_id ();
aspinall@25445
  1044
         welcome ();
aspinall@25445
  1045
         set initialized);
wenzelm@21969
  1046
        sync_thy_loader ();
wenzelm@28037
  1047
       change print_mode (update (op =) proof_generalN);
wenzelm@21969
  1048
       pgip_toplevel tty_src);
aspinall@21637
  1049
aspinall@21649
  1050
aspinall@21649
  1051
aspinall@21649
  1052
(** Out-of-loop PGIP commands (for Emacs hybrid mode) **)
aspinall@21649
  1053
aspinall@21649
  1054
local
wenzelm@22590
  1055
    val pgip_output_channel = ref Output.writeln_default
aspinall@21649
  1056
in
aspinall@21649
  1057
aspinall@21649
  1058
(* Set recipient for PGIP results *)
aspinall@21649
  1059
fun init_pgip_channel writefn =
wenzelm@21940
  1060
    (init_pgip_session_id();
wenzelm@21940
  1061
     pgip_output_channel := writefn)
aspinall@21649
  1062
wenzelm@21940
  1063
(* Process a PGIP command.
wenzelm@21940
  1064
   This works for preferences but not generally guaranteed
aspinall@21649
  1065
   because we haven't done full setup here (e.g., no pgml mode)  *)
aspinall@21649
  1066
fun process_pgip str =
aspinall@21649
  1067
     setmp output_xml_fn (!pgip_output_channel) process_pgip_plain str
aspinall@21649
  1068
aspinall@21649
  1069
end
aspinall@21637
  1070
aspinall@21637
  1071
end;