src/Pure/proof_general.ML
author aspinall
Wed Jul 13 11:28:09 2005 +0200 (2005-07-13)
changeset 16788 0c6f5fe30676
parent 16534 95460b6eb712
child 16789 e8f7a6ec92e5
permissions -rw-r--r--
Fix informtheoryloaded/retracted -> informfileloaded/retracted to match pgip.rnc
wenzelm@12780
     1
(*  Title:      Pure/proof_general.ML
wenzelm@12778
     2
    ID:         $Id$
wenzelm@14675
     3
    Author:     David Aspinall and Markus Wenzel
wenzelm@12778
     4
aspinall@15134
     5
Isabelle configuration for Proof General (see http://proofgeneral.inf.ed.ac.uk)
aspinall@15134
     6
Includes support for PGIP control language for Isabelle/Isar.
aspinall@15134
     7
aspinall@15134
     8
===========================================================================
aspinall@15134
     9
NOTE: With this version you will lose support for the Isabelle
aspinall@15173
    10
settings menu in the currently released version of Proof General (3.5).
aspinall@15134
    11
No other changes should be visible in the Emacs interface.
aspinall@15134
    12
wenzelm@16534
    13
The 3.6pre pre-release versions of Emacs Proof General now support the
wenzelm@16534
    14
new PGIP format for preferences and restore the settings menu.
aspinall@15173
    15
Please visit http://proofgeneral.inf.ed.ac.uk/develdownload
aspinall@15134
    16
===========================================================================
aspinall@15134
    17
aspinall@15145
    18
STATUS: this version is an experimental version that supports PGIP 2.X.
wenzelm@12778
    19
*)
wenzelm@12778
    20
wenzelm@12778
    21
signature PROOF_GENERAL =
wenzelm@12778
    22
sig
wenzelm@12778
    23
  val update_thy_only: string -> unit
wenzelm@12778
    24
  val try_update_thy_only: string -> unit
wenzelm@13391
    25
  val inform_file_retracted: string -> unit
wenzelm@12778
    26
  val inform_file_processed: string -> unit
wenzelm@16534
    27
  val preferences:
wenzelm@16534
    28
      (string *
wenzelm@16534
    29
        (string *
wenzelm@16534
    30
         (string *
wenzelm@16534
    31
          (string * (string * (unit -> string)) *
wenzelm@16534
    32
           (string -> unit)))) list) list ref
berghofe@13728
    33
  val process_pgip: string -> unit
wenzelm@12778
    34
  val thms_containing: string list -> unit
wenzelm@12778
    35
  val help: unit -> unit
wenzelm@12778
    36
  val show_context: unit -> theory
wenzelm@12778
    37
  val kill_goal: unit -> unit
wenzelm@12778
    38
  val repeat_undo: int -> unit
wenzelm@12778
    39
  val isa_restart: unit -> unit
wenzelm@12833
    40
  val full_proofs: bool -> unit
aspinall@15134
    41
  val isarcmd: string -> unit
wenzelm@12778
    42
  val init: bool -> unit
aspinall@15134
    43
  val init_pgip: bool -> unit
wenzelm@12778
    44
  val write_keywords: string -> unit
wenzelm@16534
    45
  val xmls_of_str: string -> string list   (*temp for testing parser*)
wenzelm@12778
    46
end;
wenzelm@12778
    47
wenzelm@16534
    48
structure ProofGeneral: PROOF_GENERAL =
wenzelm@12778
    49
struct
wenzelm@12778
    50
wenzelm@16534
    51
structure P = OuterParse;
aspinall@15145
    52
aspinall@15191
    53
aspinall@15134
    54
(* PGIP messaging mode (independent of PGML output) *)
aspinall@15134
    55
aspinall@15134
    56
val pgip_active = ref false;
aspinall@15134
    57
val pgip_emacs_compatibility_flag = ref false;
aspinall@15134
    58
wenzelm@16534
    59
fun pgip () = (! pgip_active);
wenzelm@16534
    60
fun pgip_emacs_compatibility () = (! pgip_emacs_compatibility_flag);
aspinall@15134
    61
aspinall@15134
    62
wenzelm@12778
    63
(* print modes *)
wenzelm@12778
    64
wenzelm@16534
    65
val proof_generalN = "ProofGeneral";  (*token markup (colouring vars, etc.)*)
wenzelm@16534
    66
val xsymbolsN = Symbol.xsymbolsN;     (*X-Symbol symbols*)
wenzelm@16534
    67
val pgmlN = "PGML";                   (*XML escapes and PGML symbol elements*)
wenzelm@16534
    68
val pgmlatomsN = "PGMLatoms";         (*variable kind decorations*)
wenzelm@16534
    69
val thm_depsN = "thm_deps";           (*meta-information about theorem deps*)
wenzelm@12778
    70
wenzelm@16534
    71
fun pgml () = Output.has_mode pgmlN;
wenzelm@16534
    72
wenzelm@12778
    73
aspinall@15134
    74
(* text output: print modes for xsymbol and PGML *)
wenzelm@12778
    75
wenzelm@12778
    76
local
wenzelm@12778
    77
wenzelm@14827
    78
fun xsym_output "\\" = "\\\\"
wenzelm@14827
    79
  | xsym_output s = if Symbol.is_raw s then Symbol.decode_raw s else s;
wenzelm@14827
    80
wenzelm@12778
    81
fun xsymbols_output s =
wenzelm@16534
    82
  if Output.has_mode xsymbolsN andalso exists_string (equal "\\") s then
wenzelm@12778
    83
    let val syms = Symbol.explode s
wenzelm@14827
    84
    in (implode (map xsym_output syms), real (Symbol.length syms)) end
aspinall@15134
    85
  else Symbol.default_output s;
aspinall@15134
    86
aspinall@15134
    87
fun pgml_sym s =
aspinall@15134
    88
  (case Symbol.decode s of
wenzelm@16534
    89
    (*NB: an alternative here would be to output the default print mode alternative
wenzelm@16534
    90
      in the element content, but unfortunately print modes are not that fine grained.*)
aspinall@15266
    91
    Symbol.Char s => XML.text s
aspinall@15403
    92
  | Symbol.Sym sn => XML.element "sym" [("name", sn)] [XML.text s]
wenzelm@16534
    93
  | Symbol.Ctrl sn => XML.element "ctrl" [("name", sn)] [XML.text s] (* FIXME: no such PGML! *)
aspinall@15266
    94
  | Symbol.Raw s => s);
wenzelm@12778
    95
aspinall@15134
    96
fun pgml_output str =
aspinall@15134
    97
  let val syms = Symbol.explode str
aspinall@15134
    98
  in (implode (map pgml_sym syms), real (Symbol.length syms)) end;
aspinall@15134
    99
wenzelm@16534
   100
in
wenzelm@12778
   101
wenzelm@16534
   102
fun setup_xsymbols_output () =
wenzelm@16534
   103
  Output.add_mode xsymbolsN
wenzelm@16534
   104
    (xsymbols_output, Symbol.default_indent, Symbol.encode_raw);
wenzelm@12778
   105
wenzelm@16534
   106
fun setup_pgml_output () =
wenzelm@16534
   107
  Output.add_mode pgmlN
wenzelm@16534
   108
    (pgml_output, Symbol.default_indent, Symbol.encode_raw);
wenzelm@12778
   109
wenzelm@12778
   110
end;
wenzelm@12778
   111
wenzelm@12778
   112
wenzelm@12778
   113
(* token translations *)
wenzelm@12778
   114
wenzelm@12778
   115
local
wenzelm@12778
   116
wenzelm@12778
   117
val end_tag = oct_char "350";
wenzelm@12778
   118
val class_tag = ("class", oct_char "351");
wenzelm@12778
   119
val tfree_tag = ("tfree", oct_char "352");
wenzelm@12778
   120
val tvar_tag = ("tvar", oct_char "353");
wenzelm@12778
   121
val free_tag = ("free", oct_char "354");
wenzelm@12778
   122
val bound_tag = ("bound", oct_char "355");
wenzelm@12778
   123
val var_tag = ("var", oct_char "356");
wenzelm@12778
   124
val skolem_tag = ("skolem", oct_char "357");
wenzelm@12778
   125
wenzelm@12778
   126
fun xml_atom kind x = XML.element "atom" [("kind", kind)] [XML.text x];
wenzelm@12778
   127
wenzelm@12778
   128
fun tagit (kind, bg_tag) x =
wenzelm@16534
   129
 (if Output.has_mode pgmlatomsN then xml_atom kind x
wenzelm@16534
   130
  else bg_tag ^ x ^ end_tag, real (Symbol.length (Symbol.explode x)));
wenzelm@12778
   131
wenzelm@12778
   132
fun free_or_skolem x =
wenzelm@12778
   133
  (case try Syntax.dest_skolem x of
skalberg@15531
   134
    NONE => tagit free_tag x
skalberg@15531
   135
  | SOME x' => tagit skolem_tag x');
wenzelm@12778
   136
wenzelm@12778
   137
fun var_or_skolem s =
wenzelm@15985
   138
  (case Syntax.read_variable s of
wenzelm@15985
   139
    SOME (x, i) =>
wenzelm@12778
   140
      (case try Syntax.dest_skolem x of
skalberg@15531
   141
        NONE => tagit var_tag s
wenzelm@15992
   142
      | SOME x' => tagit skolem_tag
wenzelm@15992
   143
          (setmp show_question_marks true Syntax.string_of_vname (x', i)))
wenzelm@15985
   144
  | NONE => tagit var_tag s);
wenzelm@12778
   145
wenzelm@12778
   146
val proof_general_trans =
wenzelm@12778
   147
 Syntax.tokentrans_mode proof_generalN
wenzelm@12778
   148
  [("class", tagit class_tag),
wenzelm@12778
   149
   ("tfree", tagit tfree_tag),
wenzelm@12778
   150
   ("tvar", tagit tvar_tag),
wenzelm@12778
   151
   ("free", free_or_skolem),
wenzelm@12778
   152
   ("bound", tagit bound_tag),
wenzelm@12778
   153
   ("var", var_or_skolem)];
wenzelm@12778
   154
wenzelm@15801
   155
in
wenzelm@15801
   156
wenzelm@15801
   157
val _ = Context.add_setup [Theory.add_tokentrfuns proof_general_trans];
wenzelm@15801
   158
wenzelm@15801
   159
end;
wenzelm@12778
   160
wenzelm@12778
   161
aspinall@15134
   162
(* assembling PGIP packets *)
aspinall@15134
   163
wenzelm@16534
   164
val pgip_refseq = ref NONE: string option ref;
wenzelm@16534
   165
val pgip_refid = ref NONE: string option ref;
wenzelm@12778
   166
wenzelm@12778
   167
local
wenzelm@16534
   168
  val pgip_class  = "pg";
wenzelm@16534
   169
  val pgip_origin = "Isabelle/Isar";
wenzelm@16534
   170
  val pgip_id =
wenzelm@16534
   171
    getenv "HOSTNAME" ^ "/" ^ getenv "USER" ^ "/" ^ Time.toString (Time.now ());
wenzelm@16534
   172
    (* FIXME da: PPID is empty for me: any way to get process ID? *)
wenzelm@16534
   173
    (* FIXME mak: consider using Path.pack (Path.base (Path.unpack (getenv "ISABELLE_TMP"))),
wenzelm@16534
   174
       which includes USER already; note that pgip_id above is determined at compile time! *)
wenzelm@12778
   175
wenzelm@16534
   176
  fun assemble_pgips pgips =
wenzelm@16534
   177
    XML.element
wenzelm@16534
   178
      "pgip"
wenzelm@16534
   179
      ([("class",  pgip_class),
wenzelm@16534
   180
        ("origin", pgip_origin),
wenzelm@16534
   181
        ("id",     pgip_id)] @
wenzelm@16534
   182
       if_none (Option.map (single o (pair "refseq")) (! pgip_refseq)) [] @
wenzelm@16534
   183
       if_none (Option.map (single o (pair "refid")) (! pgip_refid)) [] @
wenzelm@16534
   184
       [("seq",  string_of_int (Library.serial ()))])
wenzelm@16534
   185
      pgips;
wenzelm@13526
   186
in
wenzelm@12778
   187
wenzelm@16534
   188
fun decorated_output bg en prfx =
wenzelm@16534
   189
  writeln_default o enclose bg en o prefix_lines prfx;
aspinall@15134
   190
wenzelm@16534
   191
(* FIXME: see Rev 1.48 [PG CVS] for cleaner version of this which can be used
wenzelm@16534
   192
   for PG 4.0 which processes PGIP without needing special chars *)
wenzelm@16534
   193
fun issue_pgips ps =
wenzelm@16534
   194
  if pgip_emacs_compatibility() then
wenzelm@16534
   195
    decorated_output (*add urgent message annotation*)
wenzelm@16534
   196
      (oct_char "360") (oct_char "361") ""
wenzelm@16534
   197
      (assemble_pgips ps)
wenzelm@16534
   198
  else
wenzelm@16534
   199
    writeln_default (assemble_pgips ps);
aspinall@15134
   200
wenzelm@16534
   201
fun assemble_pgip resp attrs s = assemble_pgips [XML.element resp attrs [s]];
wenzelm@16534
   202
fun assemble_pgipe resp attrs = assemble_pgips [XML.element resp attrs []];
aspinall@15134
   203
wenzelm@16534
   204
(* FIXME: need to add stuff to gather PGIPs here *)
wenzelm@16534
   205
fun issue_pgip resp attrs txt =
wenzelm@16534
   206
  if pgip_emacs_compatibility () then
wenzelm@16534
   207
    decorated_output (*add urgent message annotation*)
wenzelm@16534
   208
      (oct_char "360") (oct_char "361") ""
wenzelm@16534
   209
      (assemble_pgip resp attrs txt)
wenzelm@16534
   210
  else
wenzelm@16534
   211
    writeln_default (assemble_pgip resp attrs txt);
aspinall@15134
   212
wenzelm@16534
   213
(* FIXME: temporarily to support PG 3.4 *)
wenzelm@16534
   214
fun issue_pgip_maybe_decorated bg en resp attrs s =
wenzelm@16534
   215
  if pgip_emacs_compatibility () then
wenzelm@16534
   216
    (*in PGIP mode, but using escape characters as well*)
wenzelm@16534
   217
    writeln_default (enclose bg en (assemble_pgip resp attrs s))
wenzelm@16534
   218
  else
wenzelm@16534
   219
    issue_pgip resp attrs s;
aspinall@15134
   220
wenzelm@16534
   221
fun issue_pgipe resp attrs = writeln_default (assemble_pgipe resp attrs);
wenzelm@16534
   222
wenzelm@16534
   223
end;
wenzelm@16534
   224
aspinall@15134
   225
aspinall@15134
   226
(* messages and notification *)
aspinall@15134
   227
aspinall@15173
   228
local
wenzelm@16534
   229
  val delay_msgs = ref false;   (*true: accumulate messages*)
wenzelm@16534
   230
  val delayed_msgs = ref [];
aspinall@15173
   231
in
aspinall@15134
   232
wenzelm@16534
   233
fun message resp attrs bg en prfx s =
wenzelm@16534
   234
  if pgip () then
wenzelm@16534
   235
   (if (! delay_msgs) then
wenzelm@16534
   236
      delayed_msgs := (resp, attrs, prefix_lines prfx s) :: ! delayed_msgs (*NB: no decs*)
wenzelm@16534
   237
    else
wenzelm@16534
   238
      issue_pgip_maybe_decorated bg en resp attrs
wenzelm@16534
   239
        (XML.element "pgmltext" [] [prefix_lines prfx s]))
wenzelm@16534
   240
  else decorated_output bg en prfx s;
aspinall@15173
   241
wenzelm@16534
   242
fun start_delay_msgs () = (set delay_msgs; delayed_msgs := []);
aspinall@15173
   243
wenzelm@16534
   244
fun end_delayed_msgs () = (reset delay_msgs;
wenzelm@16534
   245
  (! delayed_msgs) |> map (fn (resp, attrs, s) =>
wenzelm@16534
   246
    XML.element resp attrs [XML.element "pgmltext" [] [s]]));
wenzelm@16534
   247
wenzelm@16534
   248
end;
aspinall@15134
   249
aspinall@15134
   250
local
wenzelm@16534
   251
  val display_area = ("area", "display");
wenzelm@16534
   252
  val message_area = ("area", "message");
wenzelm@16534
   253
  val internal_category = ("messagecategory", "internal");
wenzelm@16534
   254
  val info_category = ("messagecategory", "information");
wenzelm@16534
   255
  val tracing_category = ("messagecategory", "tracing");
wenzelm@16534
   256
  val urgent_indication = ("urgent", "y");
wenzelm@16534
   257
  val nonfatal = ("fatality", "nonfatal");
wenzelm@16534
   258
  val fatal = ("fatality", "fatal");
wenzelm@16534
   259
  val panic = ("fatality", "panic");
aspinall@15134
   260
wenzelm@16534
   261
  val thyname_attr = pair "thyname";
wenzelm@16534
   262
  val url_attr = pair "url";
wenzelm@16534
   263
  fun localfile_url_attr path = url_attr ("file:///" ^ path);
wenzelm@16534
   264
in
wenzelm@12778
   265
wenzelm@12778
   266
fun setup_messages () =
aspinall@15134
   267
 (writeln_fn  := message "normalresponse" [message_area] "" "" "";
aspinall@15134
   268
aspinall@15134
   269
  priority_fn := message "normalresponse" [message_area, urgent_indication]
wenzelm@16534
   270
                         (oct_char "360") (oct_char "361") "";
aspinall@15134
   271
aspinall@15134
   272
  tracing_fn := message "normalresponse"  [message_area, tracing_category]
wenzelm@16534
   273
                        (oct_char "360" ^ oct_char "375") (oct_char "361") "";
aspinall@15134
   274
aspinall@15191
   275
  info_fn := message "normalresponse" [message_area, info_category]
wenzelm@16534
   276
                     (oct_char "362") (oct_char "363") "+++ ";
aspinall@15191
   277
aspinall@15191
   278
  debug_fn := message "normalresponse" [message_area, internal_category]
wenzelm@16534
   279
                      (oct_char "362") (oct_char "363") "+++ ";
aspinall@15191
   280
wenzelm@16534
   281
  warning_fn := message "errorresponse" [nonfatal]
wenzelm@16534
   282
                        (oct_char "362") (oct_char "363") "### ";
aspinall@15134
   283
aspinall@15134
   284
  error_fn := message "errorresponse" [fatal]
wenzelm@16534
   285
                      (oct_char "364") (oct_char "365") "*** ";
aspinall@15134
   286
aspinall@15134
   287
  panic_fn := message "errorresponse" [panic]
wenzelm@16534
   288
                      (oct_char "364") (oct_char "365") "!!! ");
aspinall@15134
   289
aspinall@15134
   290
aspinall@15400
   291
(* accumulate printed output in a single PGIP response (inside <pgmltext>) *)
aspinall@15134
   292
wenzelm@16534
   293
fun with_displaywrap (elt, attrs) dispfn =
wenzelm@16534
   294
  let
wenzelm@16534
   295
    val lines = ref ([]: string list);
wenzelm@16534
   296
    fun wlgrablines s = lines := s :: ! lines;
wenzelm@16534
   297
  in
wenzelm@16534
   298
    setmp writeln_fn wlgrablines dispfn ();
wenzelm@16534
   299
    (* FIXME: XML.element here inefficient, should use stream output *)
wenzelm@16534
   300
    issue_pgip elt attrs (XML.element "pgmltext" [] (! lines))
wenzelm@16534
   301
  end;
wenzelm@12778
   302
aspinall@15134
   303
val emacs_notify = decorated_output (oct_char "360") (oct_char "361") "";
aspinall@15134
   304
wenzelm@16534
   305
fun tell_clear_goals () =
wenzelm@16534
   306
  if pgip () then
wenzelm@16534
   307
    issue_pgipe "cleardisplay" [display_area]
wenzelm@16534
   308
  else
wenzelm@16534
   309
    emacs_notify "Proof General, please clear the goals buffer.";
wenzelm@16534
   310
wenzelm@16534
   311
fun tell_clear_response () =
wenzelm@16534
   312
  if pgip () then
wenzelm@16534
   313
    issue_pgipe "cleardisplay" [message_area]
wenzelm@16534
   314
  else
wenzelm@16534
   315
    emacs_notify "Proof General, please clear the response buffer.";
wenzelm@12778
   316
wenzelm@16534
   317
fun tell_file_loaded path =
wenzelm@16534
   318
  if pgip () then
aspinall@16788
   319
    issue_pgipe "informfileloaded"
aspinall@16788
   320
      [localfile_url_attr  (XML.text (File.platform_path path))]
wenzelm@16534
   321
  else
wenzelm@16534
   322
    emacs_notify ("Proof General, this file is loaded: " ^ quote (File.platform_path path));
aspinall@15134
   323
wenzelm@16534
   324
fun tell_file_retracted path =
wenzelm@16534
   325
  if pgip() then
aspinall@16788
   326
    issue_pgipe "informfileretracted"
aspinall@16788
   327
      [localfile_url_attr  (XML.text (File.platform_path path))]
wenzelm@16534
   328
  else
wenzelm@16534
   329
    emacs_notify ("Proof General, you can unlock the file " ^ quote (File.platform_path path));
wenzelm@12778
   330
wenzelm@12778
   331
wenzelm@12778
   332
(* theory / proof state output *)
wenzelm@12778
   333
wenzelm@12778
   334
local
wenzelm@12778
   335
wenzelm@16534
   336
fun tmp_markers f =
wenzelm@16534
   337
  setmp Display.current_goals_markers (oct_char "366", oct_char "367", "") f ();
wenzelm@12778
   338
wenzelm@16534
   339
fun statedisplay prts =
wenzelm@16534
   340
  issue_pgip "proofstate" []
wenzelm@16534
   341
    (XML.element "pgml" []
wenzelm@16534
   342
      [XML.element "statedisplay" [] [Pretty.output (Pretty.chunks prts)]]);
wenzelm@16534
   343
     
wenzelm@16534
   344
fun print_current_goals n m st =
wenzelm@16534
   345
  if pgml () then statedisplay (Display.pretty_current_goals n m st)
wenzelm@16534
   346
  else tmp_markers (fn () => Display.print_current_goals_default n m st);
wenzelm@13545
   347
wenzelm@16534
   348
fun print_state b st =
wenzelm@16534
   349
  if pgml () then statedisplay (Toplevel.pretty_state b st)
wenzelm@16534
   350
  else tmp_markers (fn () => Toplevel.print_state_default b st);
wenzelm@16534
   351
wenzelm@13526
   352
in
wenzelm@13526
   353
wenzelm@16534
   354
fun setup_state () =
wenzelm@16534
   355
  (Display.print_current_goals_fn := print_current_goals;
wenzelm@16534
   356
   Toplevel.print_state_fn := print_state;
wenzelm@16534
   357
   (* FIXME: check next octal char won't appear in pgip? *)
wenzelm@16534
   358
   Toplevel.prompt_state_fn := (suffix (oct_char "372") o Toplevel.prompt_state_default));
berghofe@13728
   359
wenzelm@16534
   360
end;
wenzelm@16534
   361
wenzelm@16534
   362
end;
berghofe@13728
   363
wenzelm@16022
   364
wenzelm@12778
   365
(* misc commands for ProofGeneral/isa *)
wenzelm@12778
   366
wenzelm@12778
   367
fun thms_containing ss =
wenzelm@16534
   368
  FindTheorems.print_theorems (ProofContext.init (the_context ())) NONE NONE
wenzelm@16022
   369
    (map (fn s => (true, FindTheorems.Pattern s)) ss);
wenzelm@12778
   370
wenzelm@12778
   371
val welcome = priority o Session.welcome;
wenzelm@12778
   372
val help = welcome;
wenzelm@12778
   373
val show_context = Context.the_context;
wenzelm@12778
   374
wenzelm@12778
   375
fun kill_goal () = (Goals.reset_goals (); tell_clear_goals ());
wenzelm@12778
   376
wenzelm@12778
   377
fun no_print_goals f = setmp Display.print_current_goals_fn (fn _ => fn _ => fn _ => ()) f;
wenzelm@12778
   378
wenzelm@12778
   379
fun repeat_undo 0 = ()
wenzelm@12778
   380
  | repeat_undo 1 = undo ()
wenzelm@12778
   381
  | repeat_undo n = (no_print_goals undo (); repeat_undo (n - 1));
wenzelm@12778
   382
wenzelm@12778
   383
aspinall@15134
   384
(* theory loader actions *)
aspinall@15134
   385
aspinall@15134
   386
local
aspinall@15134
   387
aspinall@15134
   388
fun add_master_files name files =
aspinall@15134
   389
  let val masters = [ThyLoad.thy_path name, ThyLoad.ml_path name]
aspinall@15134
   390
  in masters @ gen_rems (op = o pairself Path.base) (files, masters) end;
aspinall@15134
   391
aspinall@15134
   392
fun trace_action action name =
aspinall@15134
   393
  if action = ThyInfo.Update then
skalberg@15570
   394
    List.app tell_file_loaded (ThyInfo.loaded_files name)
aspinall@15134
   395
  else if action = ThyInfo.Outdate orelse action = ThyInfo.Remove then
skalberg@15570
   396
    List.app tell_file_retracted (add_master_files name (ThyInfo.loaded_files name))
aspinall@15134
   397
  else ();
aspinall@15134
   398
aspinall@15134
   399
in
aspinall@15134
   400
  fun setup_thy_loader () = ThyInfo.add_hook trace_action;
skalberg@15570
   401
  fun sync_thy_loader () = List.app (trace_action ThyInfo.Update) (ThyInfo.names ());
aspinall@15134
   402
end;
aspinall@15134
   403
aspinall@15134
   404
aspinall@15134
   405
(* prepare theory context *)
aspinall@15134
   406
aspinall@15134
   407
val thy_name = Path.pack o #1 o Path.split_ext o Path.base o Path.unpack;
wenzelm@16534
   408
wenzelm@16534
   409
(* FIXME general treatment of tracing*)
wenzelm@16534
   410
val update_thy_only = setmp MetaSimplifier.trace_simp false ThyInfo.update_thy_only;
aspinall@15134
   411
wenzelm@16440
   412
fun dynamic_context () =
aspinall@15134
   413
  (case Context.get_context () of
wenzelm@16440
   414
    SOME thy => "  Using current (dynamic!) one: " ^ quote (Context.theory_name thy)
skalberg@15531
   415
  | NONE => "");
aspinall@15134
   416
aspinall@15134
   417
fun try_update_thy_only file =
aspinall@15134
   418
  ThyLoad.cond_add_path (Path.dir (Path.unpack file)) (fn () =>
aspinall@15134
   419
    let val name = thy_name file in
wenzelm@16534
   420
      if is_some (ThyLoad.check_file NONE (ThyLoad.thy_path name))
aspinall@15134
   421
      then update_thy_only name
wenzelm@16440
   422
      else warning ("Unkown theory context of ML file." ^ dynamic_context ())
aspinall@15134
   423
    end) ();
aspinall@15134
   424
aspinall@15134
   425
aspinall@15134
   426
(* get informed about files *)
aspinall@15134
   427
wenzelm@16534
   428
val inform_file_retracted = ThyInfo.if_known_thy ThyInfo.remove_thy o thy_name;
wenzelm@16534
   429
val inform_file_processed = ThyInfo.if_known_thy ThyInfo.touch_child_thys o thy_name;
wenzelm@16534
   430
val openfile_retract = Output.no_warnings (ThyInfo.if_known_thy ThyInfo.remove_thy) o thy_name;
aspinall@15629
   431
aspinall@15134
   432
fun proper_inform_file_processed file state =
aspinall@15134
   433
  let val name = thy_name file in
aspinall@15134
   434
    if Toplevel.is_toplevel state andalso ThyInfo.known_thy name then
aspinall@15134
   435
     (ThyInfo.touch_child_thys name;
aspinall@15134
   436
      transform_error ThyInfo.pretend_use_thy_only name handle ERROR_MESSAGE msg =>
aspinall@15134
   437
       (warning msg; warning ("Failed to register theory: " ^ quote name);
aspinall@15134
   438
        tell_file_retracted (Path.base (Path.unpack file))))
aspinall@15134
   439
    else raise Toplevel.UNDEF
aspinall@15134
   440
  end;
aspinall@15134
   441
aspinall@15134
   442
fun vacuous_inform_file_processed file state =
aspinall@15134
   443
 (warning ("No theory " ^ quote (thy_name file));
aspinall@15134
   444
  tell_file_retracted (Path.base (Path.unpack file)));
aspinall@15134
   445
aspinall@15134
   446
wenzelm@12778
   447
(* restart top-level loop (keeps most state information) *)
wenzelm@12778
   448
wenzelm@12778
   449
local
wenzelm@12778
   450
wenzelm@12778
   451
fun restart isar =
wenzelm@12778
   452
 (if isar then tell_clear_goals () else kill_goal ();
wenzelm@12778
   453
  tell_clear_response ();
wenzelm@12778
   454
  welcome ());
wenzelm@12778
   455
wenzelm@12778
   456
in
wenzelm@12778
   457
wenzelm@12778
   458
fun isa_restart () = restart false;
wenzelm@12778
   459
fun isar_restart () = (sync_thy_loader (); restart true; raise Toplevel.RESTART);
wenzelm@12778
   460
wenzelm@12778
   461
end;
wenzelm@12778
   462
wenzelm@12833
   463
fun full_proofs true = proofs := 2
wenzelm@12833
   464
  | full_proofs false = proofs := 1;
wenzelm@12833
   465
wenzelm@12833
   466
aspinall@15134
   467
(* theorem dependency output *)
aspinall@15134
   468
aspinall@15134
   469
local
aspinall@15134
   470
aspinall@15134
   471
val spaces_quote = space_implode " " o map quote;
aspinall@15134
   472
wenzelm@16534
   473
fun thm_deps_message (thms, deps) =
wenzelm@16534
   474
  if pgip() then
wenzelm@16534
   475
    issue_pgips
wenzelm@16534
   476
      [XML.element
wenzelm@16534
   477
        "metainforesponse"  (* FIXME: get thy name from info here? *)
wenzelm@16534
   478
        [("infotype", "isabelle_theorem_dependencies")]
wenzelm@16534
   479
        [XML.element "value" [("name", "thms")] [XML.text thms],
wenzelm@16534
   480
         XML.element "value" [("name", "deps")] [XML.text deps]]]
wenzelm@16534
   481
  else emacs_notify ("Proof General, theorem dependencies of " ^ thms ^ " are " ^ deps);
aspinall@15134
   482
wenzelm@16534
   483
fun tell_thm_deps ths = conditional (Output.has_mode thm_depsN) (fn () =>
wenzelm@16534
   484
  let
wenzelm@16534
   485
    val names = filter_out (equal "") (map Thm.name_of_thm ths);
wenzelm@16534
   486
    val deps = filter_out (equal "")
wenzelm@16534
   487
      (Symtab.keys (Library.foldl (uncurry Proofterm.thms_of_proof)
wenzelm@16534
   488
        (Symtab.empty, map Thm.proof_of ths)));
wenzelm@16534
   489
  in
wenzelm@16534
   490
    if null names orelse null deps then ()
wenzelm@16534
   491
    else thm_deps_message (spaces_quote names, spaces_quote deps)
wenzelm@16534
   492
  end);
aspinall@15134
   493
aspinall@15134
   494
in
aspinall@15134
   495
aspinall@15134
   496
fun setup_present_hook () =
skalberg@15570
   497
  Present.add_hook (fn _ => fn res => tell_thm_deps (List.concat (map #2 res)));
aspinall@15134
   498
aspinall@15134
   499
end;
aspinall@15134
   500
aspinall@15134
   501
wenzelm@16534
   502
wenzelm@16534
   503
(** preferences **)
aspinall@15134
   504
aspinall@15134
   505
local
aspinall@15134
   506
wenzelm@16534
   507
val pgipnat = XML.element "pgipint" [("min", "0")] [];
wenzelm@16534
   508
fun pgipnatmax max = XML.element "pgipint" [("min", "0"), ("max", string_of_int max)] [];
wenzelm@16534
   509
val pgipbool = XML.element "pgipbool" [] [];
wenzelm@16534
   510
wenzelm@16534
   511
fun with_default f = (f (), f);
wenzelm@16534
   512
aspinall@15134
   513
in
aspinall@15134
   514
aspinall@15134
   515
fun nat_option r = (pgipnat,
wenzelm@16534
   516
  with_default (fn () => string_of_int (! r)),
aspinall@15134
   517
  (fn s => (case Syntax.read_nat s of
wenzelm@16534
   518
      NONE => error ("nat_option: illegal value: " ^ s)
wenzelm@16534
   519
    | SOME i => r := i)));
aspinall@15134
   520
aspinall@15134
   521
fun bool_option r = (pgipbool,
wenzelm@16534
   522
  with_default (fn () => Bool.toString (! r)),
aspinall@15134
   523
  (fn "false" => r := false | "true" => r := true
wenzelm@16534
   524
    | x => error ("bool_option: illegal value: " ^ x)));
aspinall@15134
   525
aspinall@15134
   526
val proof_option = (pgipbool,
wenzelm@16534
   527
  with_default (fn () => Bool.toString (! proofs >= 2)),
aspinall@15134
   528
  (fn "false" => proofs := 1 | "true" => proofs := 2
wenzelm@16534
   529
    | x => error ("proof_option: illegal value: " ^ x)));
aspinall@15134
   530
aspinall@15134
   531
val thm_deps_option = (pgipbool,
wenzelm@16534
   532
  with_default (fn () => Bool.toString (Output.has_mode thm_depsN)),
wenzelm@16534
   533
  (fn "false" => print_mode := (! print_mode \ thm_depsN)
wenzelm@16534
   534
    | "true" => print_mode := (thm_depsN ins ! print_mode)
wenzelm@16534
   535
    | x => error ("thm_deps_option: illegal value: " ^ x)));
aspinall@15134
   536
wenzelm@16534
   537
local
wenzelm@16534
   538
  val pg_print_depth_val = ref 10;
wenzelm@16534
   539
  fun pg_print_depth n = (print_depth n; pg_print_depth_val := n)
aspinall@15134
   540
in
wenzelm@16534
   541
aspinall@15134
   542
val print_depth_option = (pgipnat,
wenzelm@16534
   543
  with_default (fn () => string_of_int (! pg_print_depth_val)),
aspinall@15134
   544
  (fn s => (case Syntax.read_nat s of
wenzelm@16534
   545
      NONE => error ("print_depth_option: illegal value: " ^ s)
wenzelm@16534
   546
    | SOME i => pg_print_depth i)));
wenzelm@16534
   547
end;
aspinall@15134
   548
wenzelm@16534
   549
val preferences = ref
aspinall@15134
   550
  [("Display",
wenzelm@16534
   551
    [("show-types",
wenzelm@16534
   552
      ("Include types in display of Isabelle terms",
aspinall@15134
   553
       bool_option show_types)),
wenzelm@16534
   554
     ("show-sorts",
wenzelm@16534
   555
      ("Include sorts in display of Isabelle terms",
aspinall@15134
   556
       bool_option show_sorts)),
wenzelm@16534
   557
     ("show-consts",
aspinall@15134
   558
      ("Show types of consts in Isabelle goal display",
aspinall@15134
   559
       bool_option show_consts)),
wenzelm@16534
   560
     ("long-names",
wenzelm@16534
   561
      ("Show fully qualified names in Isabelle terms",
wenzelm@16534
   562
       bool_option long_names)),
wenzelm@16534
   563
     ("show-brackets",
aspinall@15134
   564
      ("Show full bracketing in Isabelle terms",
aspinall@15134
   565
       bool_option show_brackets)),
wenzelm@16534
   566
     ("show-main-goal",
wenzelm@16534
   567
      ("Show main goal in proof state display",
wenzelm@16534
   568
       bool_option Proof.show_main_goal)),
wenzelm@16534
   569
     ("eta-contract",
aspinall@15134
   570
      ("Print terms eta-contracted",
aspinall@15134
   571
       bool_option Syntax.eta_contract))]),
aspinall@15134
   572
   ("Advanced Display",
wenzelm@16534
   573
    [("goals-limit",
aspinall@15134
   574
      ("Setting for maximum number of goals printed",
aspinall@15134
   575
       nat_option goals_limit)),
wenzelm@16534
   576
     ("prems-limit",
aspinall@15134
   577
      ("Setting for maximum number of premises printed",
aspinall@15134
   578
       nat_option ProofContext.prems_limit)),
wenzelm@16534
   579
     ("print-depth",
aspinall@15134
   580
      ("Setting for the ML print depth",
berghofe@15472
   581
       print_depth_option)),
wenzelm@15985
   582
     ("show-question-marks",
berghofe@15472
   583
      ("Show leading question mark of variable name",
wenzelm@15985
   584
       bool_option show_question_marks))]),
aspinall@15134
   585
   ("Tracing",
wenzelm@16534
   586
    [("trace-simplifier",
aspinall@15134
   587
      ("Trace simplification rules.",
aspinall@15134
   588
       bool_option trace_simp)),
wenzelm@16534
   589
     ("trace-rules",
wenzelm@16534
   590
      ("Trace application of the standard rules",
wenzelm@16534
   591
       bool_option trace_rules)),
wenzelm@16534
   592
     ("trace-unification",
aspinall@15134
   593
      ("Output error diagnostics during unification",
aspinall@15134
   594
       bool_option Pattern.trace_unify_fail)),
wenzelm@16534
   595
     ("global-timing",
aspinall@15134
   596
      ("Whether to enable timing in Isabelle.",
aspinall@15134
   597
       bool_option Output.timing))]),
wenzelm@16534
   598
   ("Proof",
wenzelm@16534
   599
    [("quick-and-dirty",
wenzelm@16534
   600
      ("Take a few short cuts",
aspinall@15134
   601
       bool_option quick_and_dirty)),
wenzelm@16534
   602
     ("full-proofs",
aspinall@15134
   603
      ("Record full proof objects internally",
aspinall@15134
   604
       proof_option)),
wenzelm@16534
   605
     ("theorem-dependencies",
aspinall@15134
   606
       ("Track theorem dependencies within Proof General",
wenzelm@16534
   607
        thm_deps_option)),
wenzelm@16534
   608
     ("skip-proofs",
berghofe@15238
   609
      ("Skip all proof scripts (interactive-only)",
berghofe@15238
   610
       bool_option Toplevel.skip_proofs))])
aspinall@15134
   611
   ];
wenzelm@16534
   612
end;
wenzelm@16534
   613
aspinall@15134
   614
aspinall@15134
   615
(* Configuration: GUI config, proverinfo messages *)
aspinall@15134
   616
aspinall@15134
   617
local
aspinall@15134
   618
    val config_file = "~~/lib/ProofGeneral/pgip_isar.xml"
aspinall@15134
   619
    val name_attr = pair "name"
aspinall@15134
   620
    val version_attr = pair "version"
aspinall@15134
   621
    val isabelle_www = "http://isabelle.in.tum.de/"
aspinall@15134
   622
in
aspinall@15134
   623
fun send_pgip_config () =
wenzelm@16534
   624
    let
wenzelm@16534
   625
        val path = Path.unpack config_file
wenzelm@16534
   626
        val exists = File.exists path
wenzelm@16534
   627
        val proverinfo =
wenzelm@16534
   628
            XML.element "proverinfo"
aspinall@15134
   629
              [("name",Session.name()), ("version", version),
wenzelm@16534
   630
               ("url", isabelle_www), ("filenameextns", ".thy;")]
wenzelm@16534
   631
            [XML.element "welcomemsg" [] [XML.text (Session.welcome())],
wenzelm@16534
   632
             XML.element "helpdoc"
aspinall@15134
   633
         (* NB: would be nice to generate/display docs from isatool
aspinall@15134
   634
          doc, but that program may not be running on same machine;
aspinall@15134
   635
          front end should be responsible for launching viewer, etc. *)
wenzelm@16534
   636
                      [("name", "Isabelle/HOL Tutorial"),
wenzelm@16534
   637
                       ("descr", "A Gentle Introduction to Isabelle/HOL"),
wenzelm@16534
   638
                       ("url",  "http://isabelle.in.tum.de/dist/Isabelle2004/doc/tutorial.pdf")]
wenzelm@16534
   639
              []]
aspinall@15134
   640
    in
wenzelm@16534
   641
        if exists then
wenzelm@16534
   642
            (issue_pgips [proverinfo]; issue_pgips [File.read path])
wenzelm@16534
   643
        else panic ("PGIP configuration file " ^ config_file ^ " not found")
aspinall@15134
   644
    end;
aspinall@15134
   645
end
aspinall@15134
   646
aspinall@15134
   647
aspinall@15629
   648
(* Reveal some information about prover state *)
aspinall@15629
   649
fun send_informguise (openfile, opentheory, openproofpos) =
wenzelm@16534
   650
    let val guisefile =
wenzelm@16534
   651
            case openfile of SOME f => [XML.element "guisefile"
wenzelm@16534
   652
                                                    [("url",Url.pack (Url.File (Path.unpack f)))] []]
wenzelm@16534
   653
                           | _ => []
wenzelm@16534
   654
        val guisetheory =
wenzelm@16534
   655
            case opentheory of SOME t => [XML.element "guisetheory" [("thyname", t)] []]
wenzelm@16534
   656
                             | _ => []
wenzelm@16534
   657
        val guiseproof =
wenzelm@16534
   658
            case openproofpos of SOME i => [XML.element "guiseproof" [("proofpos", string_of_int i)] []]
wenzelm@16534
   659
                               | _ => []
wenzelm@16534
   660
    in
wenzelm@16534
   661
        issue_pgips [XML.element "informguise" [] (guisefile @ guisetheory @ guiseproof)]
wenzelm@16534
   662
    end
aspinall@15629
   663
aspinall@15629
   664
aspinall@15134
   665
(* PGIP identifier tables (prover objects). [incomplete] *)
aspinall@15134
   666
aspinall@15134
   667
local
aspinall@15134
   668
    val objtype_thy  = "theory"
aspinall@15134
   669
    val objtype_thm  = "theorem"
aspinall@15134
   670
    val objtype_term = "term"
aspinall@15134
   671
    val objtype_type = "type"
wenzelm@16534
   672
wenzelm@16534
   673
    fun xml_idtable ty ctx ids =
wenzelm@16534
   674
        let
skalberg@15531
   675
            fun ctx_attr (SOME c)= [("context", c)]
skalberg@15531
   676
              | ctx_attr NONE    = []
wenzelm@16534
   677
            fun xmlid x = XML.element "identifier" [] [XML.text x];
wenzelm@16534
   678
        in
wenzelm@16534
   679
            XML.element "idtable" (("objtype", ty)::ctx_attr ctx)
wenzelm@16534
   680
                                  (map xmlid ids)
wenzelm@16534
   681
        end
aspinall@15134
   682
in
aspinall@15134
   683
aspinall@15134
   684
fun setids t = issue_pgip "setids" [] t
aspinall@15134
   685
fun addids t = issue_pgip "addids" [] t
aspinall@15134
   686
fun delids t = issue_pgip "delids" [] t
aspinall@15134
   687
skalberg@15531
   688
fun delallids ty = setids (xml_idtable ty NONE [])
aspinall@15134
   689
aspinall@15134
   690
fun send_thy_idtable ctx thys = setids (xml_idtable objtype_thy ctx thys)
aspinall@15134
   691
fun clear_thy_idtable () = delallids objtype_thy
aspinall@15134
   692
wenzelm@16534
   693
fun send_thm_idtable ctx thy =
aspinall@15134
   694
    addids (xml_idtable objtype_thm ctx (map fst (PureThy.thms_of thy)));
aspinall@15134
   695
aspinall@15134
   696
fun clear_thm_idtable () = delallids objtype_thm
aspinall@15134
   697
aspinall@15134
   698
(* fun send_type_idtable thy = TODO, it's a bit low-level messy
aspinall@15134
   699
   & maybe not so useful anyway *)
wenzelm@16534
   700
aspinall@15134
   701
end
aspinall@15134
   702
aspinall@15134
   703
(* Response to interface queries about known objects *)
aspinall@15134
   704
aspinall@15134
   705
local
aspinall@15134
   706
 val topthy = Toplevel.theory_of o Toplevel.get_state
wenzelm@16486
   707
 fun pthm thy name = print_thm (get_thm thy (Name name))
aspinall@15134
   708
aspinall@15134
   709
 fun idvalue tp nm = ("idvalue",[("objtype",tp),("name",nm)])
aspinall@15134
   710
in
skalberg@15531
   711
fun askids (NONE, SOME "theory")  = send_thy_idtable NONE (ThyInfo.names())
skalberg@15531
   712
  | askids (NONE, NONE) =  send_thy_idtable NONE (ThyInfo.names())
aspinall@15134
   713
                           (* only theories known in top context *)
skalberg@15531
   714
  | askids (SOME thy, SOME "theory") = send_thy_idtable (SOME thy) (ThyInfo.get_preds thy)
skalberg@15531
   715
  | askids (SOME thy, SOME "theorem") = send_thm_idtable (SOME thy) (ThyInfo.get_theory thy)
skalberg@15531
   716
  | askids (SOME thy, NONE) = (send_thy_idtable (SOME thy) (ThyInfo.get_preds thy);
wenzelm@16534
   717
                               send_thm_idtable (SOME thy) (ThyInfo.get_theory thy))
skalberg@15531
   718
  | askids (_, SOME ot) = error ("No objects of type "^(quote ot)^" are available here.")
aspinall@15134
   719
wenzelm@16534
   720
fun showid (_,        "theory", n) =
aspinall@15134
   721
    with_displaywrap (idvalue "theory" n) (fn ()=>(print_theory (ThyInfo.get_theory n)))
wenzelm@16534
   722
  | showid (SOME thy, "theorem", t) =
aspinall@15134
   723
    with_displaywrap (idvalue "theorem" t) (fn ()=>(pthm (ThyInfo.get_theory thy) t))
wenzelm@16534
   724
  | showid (NONE,     "theorem", t) =
aspinall@15134
   725
    with_displaywrap (idvalue "theorem" t) (fn ()=>pthm (topthy()) t)
aspinall@15134
   726
  | showid (_, ot, _) = error ("Cannot show objects of type "^ot)
aspinall@15134
   727
aspinall@15134
   728
end
aspinall@15629
   729
aspinall@15629
   730
aspinall@15134
   731
(** Parsing proof scripts without execution **)
aspinall@15134
   732
aspinall@15146
   733
(* Notes.
aspinall@15146
   734
   This is quite tricky, because 1) we need to put back whitespace which
aspinall@15146
   735
   was removed during parsing so we can provide markup around commands;
aspinall@15146
   736
   2) parsing is intertwined with execution in Isar so we have to repeat
aspinall@15146
   737
   the parsing to extract interesting parts of commands.  The trace of
aspinall@15254
   738
   tokens parsed which is now recorded in each transition (added by
aspinall@15254
   739
   Markus June '04) helps do this, but the mechanism is still a bad mess.
wenzelm@16534
   740
aspinall@15254
   741
   If we had proper parse trees it would be easy, although having a
aspinall@15254
   742
   fixed type of trees might be tricky with Isar's extensible parser.
aspinall@15146
   743
aspinall@15146
   744
   Probably the best solution is to produce the meta-information at
wenzelm@16534
   745
   the same time as the parse, for each command, e.g. by recording a
wenzelm@16534
   746
   list of (name,objtype) pairs which record the bindings created by
wenzelm@16534
   747
   a command.  This would require changing the interfaces in
wenzelm@16534
   748
   outer_syntax.ML (or providing new ones),
aspinall@15146
   749
aspinall@15146
   750
    datatype metainfo = Binding of string * string  (* name, objtype *)
aspinall@15146
   751
aspinall@15146
   752
    val command_withmetainfo: string -> string -> string ->
wenzelm@16534
   753
      (token list ->
wenzelm@16534
   754
       ((Toplevel.transition -> Toplevel.transition) * metainfo list) *
aspinall@15146
   755
       token list) -> parser
aspinall@15254
   756
aspinall@15254
   757
   We'd also like to render terms as they appear in output, but this
wenzelm@16534
   758
   will be difficult because inner syntax is extensible and we don't
aspinall@15254
   759
   have the correct syntax to hand when just doing outer parsing
wenzelm@16534
   760
   without execution.  A reasonable approximation could
aspinall@15254
   761
   perhaps be obtained by using the syntax of the current context.
wenzelm@16534
   762
   However, this would mean more mess trying to pick out terms,
aspinall@15254
   763
   so at this stage a more serious change to the Isar functions
aspinall@15254
   764
   seems necessary.
aspinall@15146
   765
*)
aspinall@15146
   766
aspinall@15134
   767
local
aspinall@15145
   768
    fun markup_text str elt = [XML.element elt [] [XML.text str]]
aspinall@15145
   769
    fun markup_text_attrs str elt attrs = [XML.element elt attrs [XML.text str]]
aspinall@15145
   770
    fun empty elt = [XML.element elt [] []]
aspinall@15145
   771
aspinall@15210
   772
    fun whitespace str = XML.element "whitespace" [] [XML.text str]
aspinall@15208
   773
aspinall@15146
   774
    (* an extra token is needed to terminate the command *)
aspinall@15146
   775
    val sync = OuterSyntax.scan "\\<^sync>";
aspinall@15146
   776
wenzelm@16534
   777
    fun named_item_elt_with nameattr toks str elt nameP objtyp =
wenzelm@16534
   778
        let
wenzelm@16534
   779
            val name = (fst (nameP (toks@sync)))
wenzelm@16534
   780
                        handle _ => (error ("Error occurred in name parser for " ^ elt ^
wenzelm@16534
   781
                                            "(objtype: " ^ objtyp ^ ")");
wenzelm@16534
   782
                                     "")
wenzelm@16534
   783
wenzelm@16534
   784
        in
wenzelm@16534
   785
            [XML.element elt
wenzelm@16534
   786
                         ((if name="" then [] else [(nameattr, name)])@
wenzelm@16534
   787
                          (if objtyp="" then [] else [("objtype", objtyp)]))
wenzelm@16534
   788
                         ([XML.text str])]
wenzelm@16534
   789
        end
aspinall@15145
   790
aspinall@15146
   791
    val named_item_elt = named_item_elt_with "name"
aspinall@15146
   792
    val thmnamed_item_elt = named_item_elt_with "thmname"
wenzelm@16534
   793
aspinall@15145
   794
    fun parse_warning msg = warning ("script->PGIP markup parser: " ^ msg)
aspinall@15145
   795
aspinall@15145
   796
    (* FIXME: allow dynamic extensions to language here: we need a hook in
aspinall@15145
   797
       outer_syntax.ML to reset this table. *)
aspinall@15145
   798
wenzelm@16534
   799
    val keywords_classification_table = ref (NONE: string Symtab.table option)
aspinall@15145
   800
wenzelm@16534
   801
    fun get_keywords_classification_table () =
wenzelm@16534
   802
        case (!keywords_classification_table) of
wenzelm@16534
   803
            SOME t => t
wenzelm@16534
   804
          | NONE => (let
wenzelm@16534
   805
                         val tab = Library.foldl (fn (tab,(c,_,k,_))=>Symtab.update((c,k),tab))
wenzelm@16534
   806
                                         (Symtab.empty,OuterSyntax.dest_parsers())
wenzelm@16534
   807
                     in (keywords_classification_table := SOME tab; tab) end)
aspinall@15145
   808
aspinall@15145
   809
wenzelm@16534
   810
aspinall@15145
   811
    fun xmls_of_thy_begin (name,toks,str) = (* see isar_syn.ML/theoryP *)
wenzelm@16534
   812
        let
wenzelm@16534
   813
            val ((thyname,imports,files),_) = ThyHeader.args (toks@sync)
wenzelm@16534
   814
        in
wenzelm@16534
   815
            markup_text_attrs str "opentheory"
wenzelm@16534
   816
                              ([("thyname",thyname)] @
wenzelm@16534
   817
                               (if imports=[] then [] else
wenzelm@16534
   818
                                [("parentnames", (space_implode ";" imports) ^ ";")]))
wenzelm@16534
   819
        end
aspinall@15145
   820
aspinall@15145
   821
    fun xmls_of_thy_decl (name,toks,str) = (* see isar_syn.ML/thy_decl cases  *)
wenzelm@16534
   822
        let
wenzelm@16534
   823
            (* NB: PGIP only deals with single name bindings at the moment;
wenzelm@16534
   824
               potentially we could markup grouped definitions several times but
wenzelm@16534
   825
               that might suggest the wrong structure to the editor?
wenzelm@16534
   826
               Better alternative would be to put naming closer around text,
wenzelm@16534
   827
               but to do that we'd be much better off modifying the real parser
wenzelm@16534
   828
               instead of reconstructing it here. *)
aspinall@15134
   829
wenzelm@16534
   830
            val plain_items = (* no names, unimportant names, or too difficult *)
wenzelm@16534
   831
                ["defaultsort","arities","judgement","finalconsts",
wenzelm@16534
   832
                 "syntax", "nonterminals", "translations",
wenzelm@16534
   833
                 "global", "local", "hide",
wenzelm@16534
   834
                 "ML_setup", "setup", "method_setup",
wenzelm@16534
   835
                 "parse_ast_translation", "parse_translation", "print_translation",
wenzelm@16534
   836
                 "typed_print_translation", "print_ast_translation", "token_translation",
wenzelm@16534
   837
                 "oracle",
wenzelm@16534
   838
                 "declare"]
aspinall@15145
   839
wenzelm@16534
   840
            val plain_item   = markup_text str "theoryitem"
wenzelm@16534
   841
            val comment_item = markup_text str "litcomment"
wenzelm@16534
   842
            val named_item   = named_item_elt toks str "theoryitem"
aspinall@15145
   843
wenzelm@16534
   844
            val opt_overloaded = P.opt_keyword "overloaded";
aspinall@15145
   845
wenzelm@16534
   846
            val thmnameP = (* see isar_syn.ML/theoremsP *)
wenzelm@16534
   847
                let
wenzelm@16534
   848
                    val name_facts = P.and_list1 ((P.opt_thm_name "=" --| P.xthms1) >> #1)
wenzelm@16534
   849
                    val theoremsP = P.locale_target |-- name_facts >> (fn [] => "" | x::_ => x)
wenzelm@16534
   850
                in
wenzelm@16534
   851
                    theoremsP
wenzelm@16534
   852
                end
wenzelm@16534
   853
        in
wenzelm@16534
   854
            (* TODO: ideally we would like to render terms properly, just as they are
wenzelm@16534
   855
               in output. This implies using PGML for symbols and variables/atoms.
wenzelm@16534
   856
               BUT it's rather tricky without having the correct concrete syntax to hand,
wenzelm@16534
   857
               and even if we did, we'd have to mess around here a whole lot more first
wenzelm@16534
   858
               to pick out the terms from the syntax. *)
aspinall@15254
   859
wenzelm@16534
   860
            if (name mem plain_items) then plain_item
wenzelm@16534
   861
            else case name of
wenzelm@16534
   862
                     "text"      => comment_item
wenzelm@16534
   863
                   | "text_raw"  => comment_item
wenzelm@16534
   864
                   | "typedecl"  => named_item (P.type_args |-- P.name) "type"
wenzelm@16534
   865
                   | "types"     => named_item (P.type_args |-- P.name) "type"
wenzelm@16534
   866
                   | "classes"   => named_item P.name "class"
wenzelm@16534
   867
                   | "classrel"  => named_item P.name "class"
wenzelm@16534
   868
                   | "consts"    => named_item (P.const >> #1) "term"
wenzelm@16534
   869
                   | "axioms"    => named_item (P.spec_name >> (#1 o #1)) "theorem"
wenzelm@16534
   870
                   | "defs"      => named_item (opt_overloaded |-- P.spec_name >> (#1 o #1)) "theorem"
wenzelm@16534
   871
                   | "constdefs" => named_item P.name (* FIXME: heavily simplified/wrong *) "term"
wenzelm@16534
   872
                   | "theorems"  => named_item thmnameP "thmset"
wenzelm@16534
   873
                   | "lemmas"    => named_item thmnameP "thmset"
wenzelm@16534
   874
                   | "oracle"    => named_item P.name "oracle"
wenzelm@16534
   875
                   | "locale"    => named_item ((P.opt_keyword "open" >> not) |-- P.name) "locale"
wenzelm@16534
   876
                   | _ => (parse_warning ("Unrecognized thy-decl command: " ^ name); plain_item)
wenzelm@16534
   877
        end
aspinall@15145
   878
wenzelm@16534
   879
    fun xmls_of_thy_goal (name,toks,str) =
wenzelm@16534
   880
        let
wenzelm@16534
   881
            (* see isar_syn.ML/gen_theorem *)
aspinall@15146
   882
         val statement = P.and_list1 (P.opt_thm_name ":" -- Scan.repeat1 P.propp);
wenzelm@16534
   883
         val general_statement =
wenzelm@16534
   884
            statement >> pair [] || Scan.repeat P.locale_elem_or_expr -- (P.$$$ "shows" |-- statement);
wenzelm@16534
   885
wenzelm@16534
   886
            val gen_theoremP =
wenzelm@16534
   887
                P.locale_target -- Scan.optional (P.opt_thm_name ":" --|
aspinall@15146
   888
                 Scan.ahead (P.locale_keyword || P.$$$ "shows")) ("", []) --
wenzelm@16534
   889
                                 general_statement >> (fn ((locale, a), (elems, concl)) =>
wenzelm@16534
   890
                                                         fst a) (* a : (bstring * Args.src list) *)
aspinall@15146
   891
wenzelm@16534
   892
            val nameP = P.locale_target |-- (Scan.optional ((P.opt_thm_name ":") >> #1) "")
wenzelm@16534
   893
            (* TODO: add preference values for attributes
wenzelm@16534
   894
               val prefval = XML.element "prefval" [("name","thm-kind"),("value",name)]
wenzelm@16534
   895
            *)
wenzelm@16534
   896
        in
wenzelm@16534
   897
            (thmnamed_item_elt toks str "opengoal" nameP "") @
wenzelm@16534
   898
            (empty "openblock")
wenzelm@16534
   899
        end
aspinall@15134
   900
aspinall@16788
   901
    fun xmls_of_qed (name,markup) = 
aspinall@16788
   902
	let val qedmarkup  = 
aspinall@16788
   903
		(case name of
aspinall@16788
   904
		     "sorry" => markup "postponegoal"
aspinall@16788
   905
		   | "oops"  => markup "giveupgoal"
aspinall@16788
   906
		   | "done"  => markup "closegoal" 
aspinall@16788
   907
		   | "by"    => markup "closegoal"  (* nested or toplevel *)
aspinall@16788
   908
		   | "qed"   => markup "closegoal"  (* nested or toplevel *)
aspinall@16788
   909
		   | "."     => markup "closegoal"  (* nested or toplevel *)
aspinall@16788
   910
		   | ".."    => markup "closegoal"  (* nested or toplevel *)
aspinall@16788
   911
		   | other => (* default to closegoal: may be wrong for extns *)
aspinall@16788
   912
				  (parse_warning 
aspinall@16788
   913
				       ("Unrecognized qed command: " ^ quote other); 
aspinall@16788
   914
				       markup "closegoal"))
aspinall@16788
   915
	in qedmarkup @ (empty "closeblock") end
aspinall@15145
   916
wenzelm@16534
   917
    fun xmls_of_kind kind (name,toks,str) =
wenzelm@16534
   918
    let val markup = markup_text str in
aspinall@15145
   919
    case kind of
aspinall@15145
   920
      "control"        => markup "badcmd"
aspinall@15145
   921
    | "diag"           => markup "spuriouscmd"
aspinall@15145
   922
    (* theory/files *)
aspinall@15145
   923
    | "theory-begin"   => xmls_of_thy_begin (name,toks,str)
aspinall@15145
   924
    | "theory-decl"    => xmls_of_thy_decl (name,toks,str)
aspinall@15145
   925
    | "theory-heading" => markup "litcomment"
aspinall@15145
   926
    | "theory-script"  => markup "theorystep"
aspinall@15146
   927
    | "theory-end"     => markup "closetheory"
aspinall@15145
   928
    (* proof control *)
aspinall@15145
   929
    | "theory-goal"    => xmls_of_thy_goal (name,toks,str)
aspinall@15145
   930
    | "proof-heading"  => markup "litcomment"
aspinall@15225
   931
    | "proof-goal"     => (markup "opengoal") @ (empty "openblock")  (* nested proof: have, show, ... *)
aspinall@15145
   932
    | "proof-block"    => markup "proofstep" (* context-shift: proof, next; really "newgoal" *)
aspinall@15145
   933
    | "proof-open"     => (empty "openblock") @ (markup "proofstep")
aspinall@15145
   934
    | "proof-close"    => (markup "proofstep") @ (empty "closeblock")
aspinall@15145
   935
    | "proof-script"   => markup "proofstep"
aspinall@15145
   936
    | "proof-chain"    => markup "proofstep"
aspinall@15145
   937
    | "proof-decl"     => markup "proofstep"
aspinall@15145
   938
    | "proof-asm"      => markup "proofstep"
aspinall@15289
   939
    | "proof-asm-goal" => (markup "opengoal") @ (empty "openblock")  (* nested proof: obtain *)
aspinall@15145
   940
    | "qed"            => xmls_of_qed (name,markup)
aspinall@15145
   941
    | "qed-block"      => xmls_of_qed (name,markup)
aspinall@15145
   942
    | "qed-global"     => xmls_of_qed (name,markup)
aspinall@15145
   943
    | other => (* default for anything else is "spuriouscmd", ignored for undo. *)
aspinall@15145
   944
      (parse_warning ("Internal inconsistency, unrecognized keyword kind: " ^ quote other);
aspinall@15145
   945
       markup "spuriouscmd")
wenzelm@16534
   946
    end
aspinall@15134
   947
in
aspinall@15145
   948
wenzelm@16534
   949
fun xmls_of_transition (name,str,toks) =
wenzelm@16534
   950
   let
aspinall@15145
   951
       val classification = Symtab.lookup (get_keywords_classification_table(),name)
aspinall@15145
   952
   in case classification of
wenzelm@16534
   953
          SOME k => (xmls_of_kind k (name,toks,str))
wenzelm@16534
   954
        | NONE => (* an uncategorized keyword ignored for undo (maybe wrong) *)
wenzelm@16534
   955
          (parse_warning ("Uncategorized command found: " ^ name ^ " -- using spuriouscmd");
wenzelm@16534
   956
           markup_text str "spuriouscmd")
wenzelm@16534
   957
   end
aspinall@15145
   958
aspinall@15191
   959
fun markup_toks [] _ = []
aspinall@15191
   960
  | markup_toks toks x = markup_text (implode (map OuterLex.unparse toks)) x
aspinall@15191
   961
aspinall@15191
   962
fun markup_comment_whs [] = []
aspinall@15191
   963
  | markup_comment_whs (toks as t::ts) = (* this would be a lot neater if we could match on toks! *)
wenzelm@16534
   964
    let
wenzelm@16534
   965
        val (prewhs,rest) = take_prefix (OuterLex.is_kind OuterLex.Space) toks
wenzelm@16534
   966
    in
wenzelm@16534
   967
        if (prewhs <> []) then
wenzelm@16534
   968
            whitespace (implode (map OuterLex.unparse prewhs))
wenzelm@16534
   969
            :: (markup_comment_whs rest)
wenzelm@16534
   970
        else
wenzelm@16534
   971
            (markup_text (OuterLex.unparse t) "comment") @
wenzelm@16534
   972
            (markup_comment_whs ts)
aspinall@15191
   973
    end
aspinall@15191
   974
aspinall@15191
   975
fun xmls_pre_cmd [] = ([],[])
wenzelm@16534
   976
  | xmls_pre_cmd toks =
wenzelm@16534
   977
    let
wenzelm@16534
   978
        (* an extra token is needed to terminate the command *)
wenzelm@16534
   979
        val sync = OuterSyntax.scan "\\<^sync>";
wenzelm@16534
   980
        val spaceP = Scan.bulk (Scan.one (fn t =>not (OuterLex.is_proper t)) >> OuterLex.val_of) >> implode
wenzelm@16534
   981
        val text_with_whs =
wenzelm@16534
   982
            ((spaceP || Scan.succeed "") --
wenzelm@16534
   983
             (P.short_ident || P.long_ident || P.sym_ident || P.string || P.number || P.verbatim) >> op^)
wenzelm@16534
   984
             -- (spaceP || Scan.succeed "") >> op^
wenzelm@16534
   985
        val (prewhs,rest1) = take_prefix (not o OuterLex.is_proper) (toks@sync)
aspinall@15191
   986
        (* NB: this collapses  litcomment,(litcomment|whitespace)* to a single litcomment *)
wenzelm@16534
   987
        val (_,rest2) = (Scan.bulk (P.$$$ "--" |-- text_with_whs) >> implode || Scan.succeed "") rest1
wenzelm@16534
   988
        val (postwhs,rest3) = take_prefix (not o OuterLex.is_proper) rest2
aspinall@15145
   989
    in
wenzelm@16534
   990
        ((markup_comment_whs prewhs) @
wenzelm@16534
   991
         (if (length rest2 = length rest1)  then []
wenzelm@16534
   992
          else markup_text (implode
wenzelm@16534
   993
                                (map OuterLex.unparse (Library.take (length rest1 - length rest2, rest1))))
wenzelm@16534
   994
               "litcomment") @
wenzelm@16534
   995
         (markup_comment_whs postwhs),
wenzelm@16534
   996
         Library.take (length rest3 - 1,rest3))
aspinall@15191
   997
    end
aspinall@15191
   998
wenzelm@16534
   999
fun xmls_of_impropers toks =
wenzelm@16534
  1000
    let
wenzelm@16534
  1001
        val (xmls,rest) = xmls_pre_cmd toks
aspinall@15191
  1002
    in
wenzelm@16534
  1003
        xmls @ (markup_toks rest "unparseable")
aspinall@15145
  1004
    end
wenzelm@16534
  1005
wenzelm@16534
  1006
fun markup_unparseable str = markup_text str "unparseable"
aspinall@15173
  1007
aspinall@15145
  1008
end
aspinall@15145
  1009
aspinall@15145
  1010
aspinall@15145
  1011
local
wenzelm@16534
  1012
    (* we have to weave together the whitespace/comments with proper tokens
aspinall@15145
  1013
       to reconstruct the input. *)
aspinall@15191
  1014
    (* TODO: see if duplicating isar_output/parse_thy can help here *)
aspinall@15134
  1015
aspinall@15145
  1016
    fun match_tokens ([],us,vs) = (rev vs,us)  (* used, unused *)
wenzelm@16534
  1017
      | match_tokens (t::ts,w::ws,vs) =
wenzelm@16534
  1018
        if (t = w) then match_tokens (ts,ws,w::vs)
wenzelm@16534
  1019
        else match_tokens (t::ts,ws,w::vs)
aspinall@15145
  1020
      | match_tokens _ = error ("match_tokens: mismatched input parse")
aspinall@15145
  1021
in
aspinall@15145
  1022
    fun xmls_of_str str =
wenzelm@16534
  1023
    let
aspinall@15145
  1024
       (* parsing:   See outer_syntax.ML/toplevel_source *)
aspinall@15145
  1025
       fun parse_loop ([],[],xmls) = xmls
wenzelm@16534
  1026
         | parse_loop ([],itoks,xmls) = xmls @ (xmls_of_impropers itoks)
wenzelm@16534
  1027
         | parse_loop ((nm,toks,_)::trs,itoks,xmls) =
wenzelm@16534
  1028
           let
wenzelm@16534
  1029
               (* first proper token after whitespace/litcomment/whitespace is command *)
wenzelm@16534
  1030
               val (xmls_pre_cmd,cmditoks') = xmls_pre_cmd itoks
wenzelm@16534
  1031
               val (cmdtok,itoks') = case cmditoks' of x::xs => (x,xs)
wenzelm@16534
  1032
                                                     | _ => error("proof_general/parse_loop impossible")
wenzelm@16534
  1033
               val (utoks,itoks'') = match_tokens (toks,itoks',[])
aspinall@15145
  1034
               (* FIXME: should take trailing [w/s+] semicolon too in utoks *)
aspinall@15145
  1035
wenzelm@16534
  1036
               val str = implode (map OuterLex.unparse (cmdtok::utoks))
aspinall@15145
  1037
wenzelm@16534
  1038
               val xmls_tr  = xmls_of_transition (nm,str,toks)
wenzelm@16534
  1039
           in
wenzelm@16534
  1040
               parse_loop(trs,itoks'',xmls @ xmls_pre_cmd @ xmls_tr)
wenzelm@16534
  1041
           end
aspinall@15134
  1042
    in
wenzelm@16534
  1043
        (let val toks = OuterSyntax.scan str
wenzelm@16534
  1044
         in
wenzelm@16534
  1045
             parse_loop (OuterSyntax.read toks, toks, [])
wenzelm@16534
  1046
         end)
wenzelm@16534
  1047
           handle _ => markup_unparseable str
aspinall@15134
  1048
    end
aspinall@15134
  1049
aspinall@15134
  1050
wenzelm@16534
  1051
fun parsescript str attrs =
wenzelm@16534
  1052
    let
wenzelm@16534
  1053
        val _ = start_delay_msgs ()  (* accumulate error messages to put inside parseresult *)
wenzelm@16534
  1054
        val xmls = xmls_of_str str
aspinall@15173
  1055
        val errs = end_delayed_msgs ()
wenzelm@16534
  1056
     in
wenzelm@16534
  1057
        issue_pgips [XML.element "parseresult" attrs (errs@xmls)]
aspinall@15173
  1058
    end
aspinall@15134
  1059
end
aspinall@15134
  1060
aspinall@15134
  1061
aspinall@15134
  1062
aspinall@15134
  1063
(**** PGIP:  Isabelle -> Interface ****)
aspinall@15134
  1064
wenzelm@16534
  1065
val isabelle_pgml_version_supported = "1.0";
aspinall@15145
  1066
val isabelle_pgip_version_supported = "2.0"
aspinall@15134
  1067
wenzelm@16534
  1068
fun usespgip () =
aspinall@15134
  1069
    issue_pgipe "usespgip" [("version", isabelle_pgip_version_supported)];
aspinall@15134
  1070
wenzelm@16534
  1071
fun usespgml () =
aspinall@15134
  1072
    issue_pgipe "usespgml" [("version", isabelle_pgml_version_supported)];
aspinall@15134
  1073
wenzelm@16534
  1074
fun hasprefs () =
skalberg@15570
  1075
    List.app (fn (prefcat, prefs) =>
wenzelm@16534
  1076
            issue_pgips [XML.element "hasprefs" [("prefcategory",prefcat)]
wenzelm@16534
  1077
                 (map (fn (name, (descr, (ty, (default,_),_))) =>
wenzelm@16534
  1078
                       XML.element "haspref" [("name", name),
wenzelm@16534
  1079
                                              ("descr", descr),
wenzelm@16534
  1080
                                              ("default", default)]
wenzelm@16534
  1081
                       [ty]) prefs)]) (!preferences);
aspinall@15134
  1082
skalberg@15570
  1083
fun allprefs () = Library.foldl (op @) ([], map snd (!preferences))
wenzelm@16534
  1084
wenzelm@16534
  1085
fun setpref name value =
aspinall@15134
  1086
    (case assoc (allprefs(), name) of
wenzelm@16534
  1087
         NONE => warning ("Unknown pref: " ^ quote name)
skalberg@15531
  1088
       | SOME (_, (_, _, set)) => set value);
aspinall@15134
  1089
wenzelm@16534
  1090
fun getpref name =
aspinall@15134
  1091
    (case assoc (allprefs(), name) of
wenzelm@16534
  1092
         NONE => warning ("Unknown pref: " ^ quote name)
wenzelm@16534
  1093
       | SOME (_, (_, (_,get), _)) =>
wenzelm@16534
  1094
           issue_pgip "prefval" [("name", name)] (get ()));
aspinall@15134
  1095
aspinall@15134
  1096
aspinall@15134
  1097
aspinall@15134
  1098
(**** PGIP:  Interface -> Isabelle/Isar ****)
aspinall@15134
  1099
aspinall@15134
  1100
exception PGIP of string;
aspinall@15134
  1101
exception PGIP_QUIT;
aspinall@15134
  1102
wenzelm@16534
  1103
aspinall@15134
  1104
(* Sending commands to Isar *)
aspinall@15134
  1105
wenzelm@16534
  1106
fun isarcmd s =
wenzelm@16534
  1107
    s |> OuterSyntax.scan |> OuterSyntax.read
aspinall@15145
  1108
      |> map (Toplevel.position (Position.name "PGIP message") o #3) |> Toplevel.>>>;
aspinall@15134
  1109
aspinall@15145
  1110
fun xmltext ((XML.Text text)::ts) = text ^ (xmltext ts)
aspinall@15145
  1111
  | xmltext [] = ""
aspinall@15145
  1112
  | xmltext _ = raise PGIP "Expected text (PCDATA/CDATA)"
wenzelm@16534
  1113
aspinall@15145
  1114
fun isarscript xmls = isarcmd (xmltext xmls)   (* send a script command *)
aspinall@15134
  1115
aspinall@15134
  1116
aspinall@15134
  1117
(* load an arbitrary (.thy or .ML) file *)
aspinall@15134
  1118
wenzelm@16534
  1119
fun use_thy_or_ml_file file =
aspinall@15134
  1120
    let
wenzelm@16534
  1121
        val (path,extn) = Path.split_ext (Path.unpack file)
aspinall@15134
  1122
    in
wenzelm@16534
  1123
        case extn of
wenzelm@16534
  1124
            "" => isarcmd ("use_thy " ^ (quote (Path.pack path)))
wenzelm@16534
  1125
          | "thy" => isarcmd ("use_thy " ^ (quote (Path.pack path)))
wenzelm@16534
  1126
          | "ML" => isarcmd ("use " ^ quote file)
wenzelm@16534
  1127
          (* instead of error we could guess theory? *)
wenzelm@16534
  1128
          | other => error ("Don't know how to read a file with extension " ^ other)
wenzelm@16534
  1129
    end
aspinall@15134
  1130
aspinall@15134
  1131
aspinall@15173
  1132
(* Proof control commands *)
aspinall@15134
  1133
aspinall@15173
  1134
local
aspinall@15173
  1135
  fun xmlattro attr attrs = assoc(attrs,attr)
aspinall@15134
  1136
wenzelm@16534
  1137
  fun xmlattr attr attrs =
wenzelm@16534
  1138
      (case xmlattro attr attrs of
wenzelm@16534
  1139
           SOME value => value
wenzelm@16534
  1140
         | NONE => raise PGIP ("Missing attribute: " ^ attr))
aspinall@15134
  1141
aspinall@15173
  1142
  val thyname_attro = xmlattro "thyname"
aspinall@15173
  1143
  val thyname_attr = xmlattr "thyname"
aspinall@15173
  1144
  val objtype_attro = xmlattro "objtype"
aspinall@15173
  1145
  val objtype_attr = xmlattr "objtype"
aspinall@15173
  1146
  val name_attr = xmlattr "name"
aspinall@15173
  1147
  val dirname_attr = xmlattr "dir"
aspinall@15134
  1148
  fun comment x = "(* " ^ x ^ " *)"
aspinall@15134
  1149
aspinall@15173
  1150
  fun localfile_of_url url = (* only handle file:/// or file://localhost/ *)
aspinall@15173
  1151
      case Url.unpack url of
wenzelm@16534
  1152
          (Url.File path) => File.platform_path path
wenzelm@16534
  1153
        | _ => error ("Cannot access non-local URL " ^ url)
aspinall@15134
  1154
aspinall@15173
  1155
  val fileurl_of = localfile_of_url o (xmlattr "url")
aspinall@15134
  1156
aspinall@15134
  1157
  val topthy = Toplevel.theory_of o Toplevel.get_state
wenzelm@16440
  1158
  val topthy_name = Context.theory_name o topthy
aspinall@15134
  1159
skalberg@15531
  1160
  val currently_open_file = ref (NONE : string option)
aspinall@15629
  1161
aspinall@15629
  1162
  val topnode = Toplevel.node_of o Toplevel.get_state
wenzelm@16534
  1163
  fun topproofpos () = (case topnode() of Toplevel.Proof ph => SOME(ProofHistory.position ph)
wenzelm@16534
  1164
                                        | _ => NONE) handle Toplevel.UNDEF => NONE
aspinall@15134
  1165
in
aspinall@15134
  1166
wenzelm@16534
  1167
fun process_pgip_element pgipxml = (case pgipxml of
aspinall@15145
  1168
  (XML.Text t) => warning ("Ignored text in PGIP packet: \n" ^ t)
wenzelm@16534
  1169
| (xml as (XML.Elem (elem, attrs, data))) =>
aspinall@15145
  1170
  (case elem of
aspinall@15145
  1171
       (* protocol config *)
wenzelm@16534
  1172
       "askpgip"        => (pgip_emacs_compatibility_flag:=false; (* PG>=3.6 or other PGIP interface *)
wenzelm@16534
  1173
                            usespgip (); send_pgip_config ())
wenzelm@16534
  1174
     | "askpgml"        => usespgml ()
aspinall@15145
  1175
     (* proverconfig *)
wenzelm@16534
  1176
     | "askprefs"       => hasprefs ()
wenzelm@16534
  1177
     | "getpref"        => getpref (name_attr attrs)
wenzelm@16534
  1178
     | "setpref"        => setpref (name_attr attrs) (xmltext data)
aspinall@15145
  1179
     (* provercontrol *)
wenzelm@16534
  1180
     | "proverinit"     => isar_restart ()
wenzelm@16534
  1181
     | "proverexit"     => isarcmd "quit"
wenzelm@16534
  1182
     | "startquiet"     => isarcmd "disable_pr"
wenzelm@16534
  1183
     | "stopquiet"      => isarcmd "enable_pr"
aspinall@15403
  1184
     | "pgmlsymbolson"   => (print_mode := !print_mode @ ["xsymbols"])
aspinall@15403
  1185
     | "pgmlsymbolsoff"  => (print_mode := (!print_mode \ "xsymbols"))
aspinall@15145
  1186
     (* properproofcmd: proper commands which belong in script *)
aspinall@15401
  1187
     (* FIXME: next ten are by Eclipse interface, can be removed in favour of dostep *)
aspinall@15629
  1188
     (* NB: Isar doesn't make lemma name of goal accessible during proof *)
aspinall@15145
  1189
     | "opengoal"       => isarscript data
aspinall@15145
  1190
     | "proofstep"      => isarscript data
aspinall@15145
  1191
     | "closegoal"      => isarscript data
aspinall@15145
  1192
     | "giveupgoal"     => isarscript data
aspinall@15145
  1193
     | "postponegoal"   => isarscript data
wenzelm@16534
  1194
     | "comment"        => isarscript data  (* NB: should be ignored, but process anyway *)
wenzelm@16534
  1195
     | "whitespace"     => isarscript data  (* NB: should be ignored, but process anyway *)
aspinall@15145
  1196
     | "litcomment"     => isarscript data
aspinall@15145
  1197
     | "spuriouscmd"    => isarscript data
wenzelm@16534
  1198
     | "badcmd"         => isarscript data
aspinall@15145
  1199
     (* improperproofcmd: improper commands which *do not* belong in script *)
aspinall@15401
  1200
     | "dostep"         => isarscript data
aspinall@15286
  1201
     | "undostep"       => isarcmd "undos_proof 1"
aspinall@15626
  1202
     | "redostep"       => isarcmd "redo"
wenzelm@16534
  1203
     | "abortgoal"      => isarcmd "ProofGeneral.kill_proof"
wenzelm@16534
  1204
     | "forget"         => error "Not implemented"
aspinall@15145
  1205
     | "restoregoal"    => error "Not implemented"  (* could just treat as forget? *)
aspinall@15145
  1206
     (* proofctxt: improper commands *)
aspinall@15173
  1207
     | "askids"         => askids (thyname_attro attrs, objtype_attro attrs)
wenzelm@16534
  1208
     | "showid"         => showid (thyname_attro attrs, objtype_attr attrs, name_attr attrs)
wenzelm@16534
  1209
     | "askguise"       => send_informguise (!currently_open_file,
wenzelm@16534
  1210
                                             SOME (topthy_name()) handle Toplevel.UNDEF => NONE,
wenzelm@16534
  1211
                                             topproofpos())
aspinall@15173
  1212
     | "parsescript"    => parsescript (xmltext data) attrs (* just pass back attrs unchanged *)
aspinall@15145
  1213
     | "showproofstate" => isarcmd "pr"
aspinall@15145
  1214
     | "showctxt"       => isarcmd "print_theory"   (* more useful than print_context *)
aspinall@15145
  1215
     | "searchtheorems" => isarcmd ("thms_containing " ^ (xmltext data))
aspinall@15145
  1216
     | "setlinewidth"   => isarcmd ("pretty_setmargin " ^ (xmltext data))
wenzelm@16534
  1217
     | "viewdoc"        => isarcmd ("print_" ^ (xmltext data)) (* FIXME: isatool doc? *)
aspinall@15145
  1218
     (* properfilecmd: proper theory-level script commands *)
aspinall@15401
  1219
     (* FIXME: next three are sent by Eclipse, can be removed in favour of doitem *)
aspinall@15145
  1220
     | "opentheory"     => isarscript data
aspinall@15145
  1221
     | "theoryitem"     => isarscript data
wenzelm@16534
  1222
     | "closetheory"    => let
wenzelm@16534
  1223
                              val thyname = topthy_name()
wenzelm@16534
  1224
                           in (isarscript data;
wenzelm@16534
  1225
                               writeln ("Theory \""^thyname^"\" completed."))
wenzelm@16534
  1226
                           end
aspinall@15145
  1227
     (* improperfilecmd: theory-level commands not in script *)
aspinall@15401
  1228
     | "doitem"         => isarscript data
aspinall@15286
  1229
     | "undoitem"       => isarcmd "ProofGeneral.undo"
aspinall@15626
  1230
     | "redoitem"       => isarcmd "ProofGeneral.redo"
aspinall@15145
  1231
     | "aborttheory"    => isarcmd ("init_toplevel")
aspinall@15173
  1232
     | "retracttheory"  => isarcmd ("kill_thy " ^ (quote (thyname_attr attrs)))
aspinall@15173
  1233
     | "loadfile"       => use_thy_or_ml_file (fileurl_of attrs)
aspinall@15629
  1234
     | "openfile"       => (openfile_retract (fileurl_of attrs);
wenzelm@16534
  1235
                            currently_open_file := SOME (fileurl_of attrs))
wenzelm@16534
  1236
     | "closefile"      => (case !currently_open_file of
wenzelm@16534
  1237
                                SOME f => (inform_file_processed f;
wenzelm@16534
  1238
                                           currently_open_file := NONE)
wenzelm@16534
  1239
                              | NONE => raise PGIP ("closefile when no file is open!"))
skalberg@15531
  1240
     | "abortfile"      => (currently_open_file := NONE) (* perhaps error for no file open *)
aspinall@15173
  1241
     | "changecwd"      => ThyLoad.add_path (dirname_attr attrs)
wenzelm@16534
  1242
     | "systemcmd"      => isarscript data
aspinall@15145
  1243
     (* unofficial command for debugging *)
wenzelm@16534
  1244
     | "quitpgip" => raise PGIP_QUIT
aspinall@15145
  1245
     | _ => raise PGIP ("Unrecognized PGIP element: " ^ elem)))
aspinall@15134
  1246
wenzelm@16534
  1247
fun process_pgip_tree xml =
wenzelm@16534
  1248
    (pgip_refseq := NONE;
skalberg@15531
  1249
     pgip_refid := NONE;
aspinall@15173
  1250
     (case xml of
wenzelm@16534
  1251
          XML.Elem ("pgip", attrs, pgips) =>
wenzelm@16534
  1252
          (let
wenzelm@16534
  1253
               val class = xmlattr "class" attrs
wenzelm@16534
  1254
               val _ = (pgip_refseq := xmlattro "seq" attrs;
wenzelm@16534
  1255
                        pgip_refid :=  xmlattro "id" attrs)
wenzelm@16534
  1256
           in
wenzelm@16534
  1257
               if (class = "pa") then
wenzelm@16534
  1258
                   List.app process_pgip_element pgips
wenzelm@16534
  1259
               else
wenzelm@16534
  1260
                   raise PGIP "PGIP packet for me should have class=\"pa\""
wenzelm@16534
  1261
           end)
wenzelm@16534
  1262
        | _ => raise PGIP "Invalid PGIP packet received")
wenzelm@16534
  1263
     handle (PGIP msg) =>
wenzelm@16534
  1264
            (error (msg ^ "\nPGIP error occured in XML text below:\n" ^
wenzelm@16534
  1265
                    (XML.string_of_tree xml))))
aspinall@15134
  1266
aspinall@15191
  1267
val process_pgip = process_pgip_tree o XML.tree_of_string;
aspinall@15191
  1268
aspinall@15134
  1269
end
aspinall@15134
  1270
aspinall@15134
  1271
aspinall@15134
  1272
(* PGIP loop: process PGIP input only *)
aspinall@15134
  1273
wenzelm@16534
  1274
local
aspinall@15134
  1275
aspinall@15145
  1276
exception XML_PARSE
aspinall@15134
  1277
aspinall@15145
  1278
fun loop src : unit =
wenzelm@16534
  1279
    let
wenzelm@16534
  1280
        val _ = issue_pgipe "ready" []
wenzelm@16534
  1281
        val pgipo = (Source.get_single src)
wenzelm@16534
  1282
                        handle e => raise XML_PARSE
aspinall@15134
  1283
    in
wenzelm@16534
  1284
        case pgipo of
wenzelm@16534
  1285
             NONE  => ()
wenzelm@16534
  1286
           | SOME (pgip,src') =>
wenzelm@16534
  1287
             ((process_pgip_tree pgip); loop src') handle e => handler (e,SOME src')
skalberg@15531
  1288
    end handle e => handler (e,SOME src)  (* i.e. error in ready/XML parse *)
aspinall@15134
  1289
wenzelm@16534
  1290
and handler (e,srco) =
aspinall@15145
  1291
    case (e,srco) of
wenzelm@16534
  1292
        (XML_PARSE,SOME src) =>
wenzelm@16534
  1293
        (* (!error_fn "Invalid XML input, aborting"; ()) NB: loop OK for tty, but want exit on EOF *)
aspinall@15145
  1294
         panic "Invalid XML input, aborting"
aspinall@15145
  1295
      | (PGIP_QUIT,_) => ()
skalberg@15531
  1296
      | (ERROR,SOME src) => loop src (* message already given *)
skalberg@15531
  1297
      | (Interrupt,SOME src) => (!error_fn "Interrupt during PGIP processing"; loop src)
skalberg@15531
  1298
      | (Toplevel.UNDEF,SOME src) => (error "No working context defined"; loop src) (* usually *)
skalberg@15531
  1299
      | (e,SOME src) => (error (Toplevel.exn_message e); loop src)
skalberg@15531
  1300
      | (_,NONE) => ()
aspinall@15134
  1301
in
aspinall@15145
  1302
  (* NB: simple tty for now; later might read from other tty-style sources (e.g. sockets). *)
aspinall@15145
  1303
aspinall@15145
  1304
  val xmlP = XML.scan_comment_whspc |-- XML.parse_elem >> single
aspinall@15145
  1305
skalberg@15531
  1306
  val tty_src = Source.set_prompt "" (Source.source Symbol.stopper xmlP NONE Source.tty)
aspinall@15145
  1307
wenzelm@16534
  1308
  val pgip_toplevel =  loop
aspinall@15134
  1309
end
aspinall@15134
  1310
aspinall@15134
  1311
aspinall@15134
  1312
(* additional outer syntax for Isar *)
wenzelm@12778
  1313
wenzelm@12778
  1314
local structure P = OuterParse and K = OuterSyntax.Keyword in
wenzelm@12778
  1315
wenzelm@16534
  1316
val undoP = (*undo without output*)
wenzelm@12778
  1317
  OuterSyntax.improper_command "ProofGeneral.undo" "(internal)" K.control
wenzelm@12778
  1318
    (Scan.succeed (Toplevel.no_timing o IsarCmd.undo));
wenzelm@12778
  1319
wenzelm@16534
  1320
val redoP = (*redo without output*)
aspinall@15626
  1321
  OuterSyntax.improper_command "ProofGeneral.redo" "(internal)" K.control
aspinall@15626
  1322
    (Scan.succeed (Toplevel.no_timing o IsarCmd.redo));
aspinall@15626
  1323
wenzelm@12778
  1324
val context_thy_onlyP =
wenzelm@12778
  1325
  OuterSyntax.improper_command "ProofGeneral.context_thy_only" "(internal)" K.control
wenzelm@12778
  1326
    (P.name >> (Toplevel.no_timing oo IsarThy.init_context update_thy_only));
wenzelm@12778
  1327
wenzelm@12778
  1328
val try_context_thy_onlyP =
wenzelm@12778
  1329
  OuterSyntax.improper_command "ProofGeneral.try_context_thy_only" "(internal)" K.control
wenzelm@12778
  1330
    (P.name >> (Toplevel.no_timing oo
wenzelm@12778
  1331
      (Toplevel.imperative (K ()) oo IsarThy.init_context try_update_thy_only)));
wenzelm@12778
  1332
wenzelm@12778
  1333
val restartP =
wenzelm@12778
  1334
  OuterSyntax.improper_command "ProofGeneral.restart" "(internal)" K.control
wenzelm@12778
  1335
    (P.opt_unit >> (Toplevel.no_timing oo K (Toplevel.imperative isar_restart)));
wenzelm@12778
  1336
wenzelm@12778
  1337
val kill_proofP =
wenzelm@12778
  1338
  OuterSyntax.improper_command "ProofGeneral.kill_proof" "(internal)" K.control
wenzelm@12778
  1339
    (Scan.succeed (Toplevel.no_timing o IsarCmd.kill_proof_notify tell_clear_goals));
wenzelm@12778
  1340
wenzelm@12778
  1341
val inform_file_processedP =
wenzelm@12778
  1342
  OuterSyntax.improper_command "ProofGeneral.inform_file_processed" "(internal)" K.control
wenzelm@14902
  1343
    (P.name >> (fn file => Toplevel.no_timing o
wenzelm@14902
  1344
      Toplevel.keep (vacuous_inform_file_processed file) o
wenzelm@14902
  1345
      Toplevel.kill o
wenzelm@14902
  1346
      Toplevel.keep (proper_inform_file_processed file)));
wenzelm@12778
  1347
wenzelm@12778
  1348
val inform_file_retractedP =
wenzelm@12778
  1349
  OuterSyntax.improper_command "ProofGeneral.inform_file_retracted" "(internal)" K.control
wenzelm@12778
  1350
    (P.name >> (Toplevel.no_timing oo
wenzelm@14902
  1351
      (fn file => Toplevel.imperative (fn () => inform_file_retracted file))));
wenzelm@12778
  1352
wenzelm@14725
  1353
val process_pgipP =
wenzelm@14725
  1354
  OuterSyntax.improper_command "ProofGeneral.process_pgip" "(internal)" K.control
wenzelm@14725
  1355
    (P.text >> (Toplevel.no_timing oo
wenzelm@14725
  1356
      (fn txt => Toplevel.imperative (fn () => process_pgip txt))));
wenzelm@14725
  1357
wenzelm@12778
  1358
fun init_outer_syntax () = OuterSyntax.add_parsers
aspinall@15626
  1359
 [undoP, redoP, restartP, kill_proofP, context_thy_onlyP, try_context_thy_onlyP,
wenzelm@14725
  1360
  inform_file_processedP, inform_file_retractedP, process_pgipP];
wenzelm@12778
  1361
wenzelm@12778
  1362
end;
wenzelm@12778
  1363
wenzelm@12778
  1364
wenzelm@12778
  1365
(* init *)
wenzelm@12778
  1366
wenzelm@12778
  1367
val initialized = ref false;
wenzelm@12778
  1368
wenzelm@16534
  1369
fun set_prompts true _ = ml_prompts "ML> " "ML# "
aspinall@15134
  1370
  | set_prompts false true = ml_prompts "PGIP-ML>" "PGIP-ML# "
wenzelm@16534
  1371
  | set_prompts false false = ml_prompts ("> " ^ oct_char "372") ("- " ^ oct_char "373");
aspinall@15134
  1372
aspinall@15134
  1373
fun init_setup isar pgip =
wenzelm@16534
  1374
  (conditional (not (! initialized)) (fn () =>
wenzelm@12778
  1375
   (if isar then setmp warning_fn (K ()) init_outer_syntax () else ();
wenzelm@12778
  1376
    setup_xsymbols_output ();
aspinall@15134
  1377
    setup_pgml_output ();
wenzelm@12778
  1378
    setup_messages ();
wenzelm@12778
  1379
    setup_state ();
wenzelm@12778
  1380
    setup_thy_loader ();
wenzelm@13526
  1381
    setup_present_hook ();
wenzelm@12778
  1382
    set initialized; ()));
wenzelm@12778
  1383
  sync_thy_loader ();
wenzelm@12778
  1384
  print_mode := proof_generalN :: (! print_mode \ proof_generalN);
wenzelm@16534
  1385
  if pgip then
wenzelm@16534
  1386
    print_mode := pgmlN :: (pgmlatomsN :: (! print_mode \ pgmlN \ pgmlatomsN))
wenzelm@16534
  1387
  else
wenzelm@16534
  1388
    pgip_emacs_compatibility_flag := true;  (* assume this for PG <3.6 compatibility *)
wenzelm@12778
  1389
  set quick_and_dirty;
wenzelm@12778
  1390
  ThmDeps.enable ();
aspinall@15134
  1391
  set_prompts isar pgip;
wenzelm@16534
  1392
  pgip_active := pgip);
aspinall@15134
  1393
wenzelm@16534
  1394
fun init isar =
wenzelm@16534
  1395
 (init_setup isar false;
wenzelm@16534
  1396
  if isar then Isar.sync_main () else isa_restart ());
aspinall@15134
  1397
wenzelm@16534
  1398
fun init_pgip false = panic "PGIP not supported for Isabelle/classic mode."
wenzelm@16534
  1399
  | init_pgip true = (init_setup true true; pgip_toplevel tty_src);
wenzelm@12778
  1400
wenzelm@12778
  1401
wenzelm@12778
  1402
wenzelm@16534
  1403
(** generate elisp file for keyword classification **)
wenzelm@12778
  1404
wenzelm@12778
  1405
local
wenzelm@12778
  1406
wenzelm@12778
  1407
val regexp_meta = explode ".*+?[]^$";
wenzelm@16534
  1408
val regexp_quote =
wenzelm@16534
  1409
  implode o map (fn c => if c mem_string regexp_meta then "\\\\" ^ c else c) o explode;
wenzelm@12778
  1410
wenzelm@12778
  1411
fun defconst name strs =
wenzelm@12778
  1412
  "\n(defconst isar-keywords-" ^ name ^
wenzelm@12778
  1413
  "\n  '(" ^ space_implode "\n    " (map (quote o regexp_quote) strs) ^ "))\n";
wenzelm@12778
  1414
wenzelm@16534
  1415
fun make_elisp_commands commands kind = defconst kind
wenzelm@16534
  1416
  (commands |> List.mapPartial (fn (c, _, k, _) => if k = kind then SOME c else NONE));
wenzelm@12778
  1417
wenzelm@12778
  1418
fun make_elisp_syntax (keywords, commands) =
wenzelm@12778
  1419
  ";;\n\
wenzelm@12778
  1420
  \;; Keyword classification tables for Isabelle/Isar.\n\
wenzelm@12778
  1421
  \;; This file was generated by " ^ Session.name () ^ " -- DO NOT EDIT!\n\
wenzelm@12778
  1422
  \;;\n\
wenzelm@12778
  1423
  \;; $" ^ "Id$\n\
wenzelm@12778
  1424
  \;;\n" ^
wenzelm@12778
  1425
  defconst "major" (map #1 commands) ^
skalberg@15570
  1426
  defconst "minor" (List.filter Syntax.is_identifier keywords) ^
wenzelm@12778
  1427
  implode (map (make_elisp_commands commands) OuterSyntax.Keyword.kinds) ^
wenzelm@12778
  1428
  "\n(provide 'isar-keywords)\n";
wenzelm@12778
  1429
wenzelm@12778
  1430
in
wenzelm@12778
  1431
wenzelm@12778
  1432
fun write_keywords s =
wenzelm@16534
  1433
 (init_outer_syntax ();
wenzelm@16534
  1434
  File.write (Path.unpack ("isar-keywords" ^ (if s = "" then "" else "-" ^ s) ^".el"))
wenzelm@16534
  1435
    (make_elisp_syntax (OuterSyntax.dest_keywords (), OuterSyntax.dest_parsers ())));
wenzelm@12778
  1436
wenzelm@12778
  1437
end;
wenzelm@12778
  1438
wenzelm@16534
  1439
end;