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