src/Pure/ProofGeneral/pgip_input.ML
author wenzelm
Thu Apr 03 21:23:39 2008 +0200 (2008-04-03)
changeset 26548 41bbcaf3e481
parent 23436 343e84195e2c
child 28020 1ff5167592ba
permissions -rw-r--r--
further cleanup of XML signature;
aspinall@21637
     1
(*  Title:      Pure/ProofGeneral/pgip_input.ML
aspinall@21637
     2
    ID:         $Id$
aspinall@21637
     3
    Author:     David Aspinall
aspinall@21637
     4
wenzelm@21940
     5
PGIP abstraction: input commands.
aspinall@21637
     6
*)
aspinall@21637
     7
aspinall@21637
     8
signature PGIPINPUT =
aspinall@21637
     9
sig
aspinall@21637
    10
    (* These are the PGIP commands to which we respond. *) 
aspinall@21637
    11
    datatype pgipinput = 
aspinall@21649
    12
    (* protocol/prover config *)
aspinall@21649
    13
      Askpgip        of { }
aspinall@21649
    14
    | Askpgml        of { } 
aspinall@21649
    15
    | Askconfig      of { }
aspinall@21649
    16
    | Askprefs       of { }
aspinall@21649
    17
    | Setpref        of { name:string, prefcategory:string option, value:string }
aspinall@21649
    18
    | Getpref        of { name:string, prefcategory:string option }
aspinall@21649
    19
    (* prover control *)
aspinall@21649
    20
    | Proverinit     of { }
aspinall@21649
    21
    | Proverexit     of { }
aspinall@22406
    22
    | Setproverflag  of { flagname:string, value: bool }
aspinall@21649
    23
    (* improper proof commands: control proof state *)
aspinall@21649
    24
    | Dostep         of { text: string }
aspinall@21649
    25
    | Undostep       of { times: int }
aspinall@21649
    26
    | Redostep       of { }
aspinall@21649
    27
    | Abortgoal      of { }
aspinall@21649
    28
    | Forget         of { thyname: string option, name: string option, 
aspinall@21867
    29
                          objtype: PgipTypes.objtype option }
aspinall@21649
    30
    | Restoregoal    of { thmname : string }
aspinall@21649
    31
    (* context inspection commands *)
aspinall@21867
    32
    | Askids         of { url: PgipTypes.pgipurl option,
aspinall@21867
    33
			  thyname: PgipTypes.objname option,
aspinall@21867
    34
			  objtype: PgipTypes.objtype option }
aspinall@22161
    35
    | Askrefs        of { url: PgipTypes.pgipurl option,
aspinall@22161
    36
			  thyname: PgipTypes.objname option,
aspinall@22161
    37
			  objtype: PgipTypes.objtype option,
aspinall@22161
    38
			  name: PgipTypes.objname option }
aspinall@21867
    39
    | Showid         of { thyname: PgipTypes.objname option, 
aspinall@21867
    40
			  objtype: PgipTypes.objtype, 
aspinall@21867
    41
			  name: PgipTypes.objname }
aspinall@21649
    42
    | Askguise       of { }
aspinall@21649
    43
    | Parsescript    of { text: string, location: PgipTypes.location,
aspinall@21649
    44
                          systemdata: string option } 
aspinall@21649
    45
    | Showproofstate of { }
aspinall@21649
    46
    | Showctxt       of { }
aspinall@21649
    47
    | Searchtheorems of { arg: string }
aspinall@21649
    48
    | Setlinewidth   of { width: int }
aspinall@21649
    49
    | Viewdoc        of { arg: string }
aspinall@21649
    50
    (* improper theory-level commands *)
aspinall@21649
    51
    | Doitem         of { text: string }
aspinall@21649
    52
    | Undoitem       of { }
aspinall@21649
    53
    | Redoitem       of { }
aspinall@21649
    54
    | Aborttheory    of { }
aspinall@21649
    55
    | Retracttheory  of { thyname: string }
aspinall@21649
    56
    | Loadfile       of { url: PgipTypes.pgipurl }
aspinall@21649
    57
    | Openfile       of { url: PgipTypes.pgipurl }
aspinall@21649
    58
    | Closefile      of { }
aspinall@21649
    59
    | Abortfile      of { }
aspinall@21649
    60
    | Retractfile    of { url: PgipTypes.pgipurl }
aspinall@21649
    61
    | Changecwd      of { url: PgipTypes.pgipurl }
aspinall@21649
    62
    | Systemcmd      of { arg: string }
aspinall@21649
    63
    (* unofficial escape command for debugging *)
aspinall@21649
    64
    | Quitpgip       of { }
aspinall@21637
    65
wenzelm@26548
    66
    val input: string * XML.attributes * XML.tree list -> pgipinput option  (* raises PGIP *)
aspinall@21637
    67
end
aspinall@21637
    68
aspinall@21637
    69
structure PgipInput : PGIPINPUT = 
aspinall@21637
    70
struct
aspinall@21637
    71
aspinall@21637
    72
open PgipTypes
aspinall@21637
    73
aspinall@21637
    74
(*** PGIP input ***)
aspinall@21637
    75
aspinall@21637
    76
datatype pgipinput = 
aspinall@21637
    77
	 (* protocol/prover config *)
aspinall@21637
    78
	 Askpgip 	of { }
aspinall@21637
    79
       | Askpgml 	of { } 
aspinall@21637
    80
       | Askconfig	of { }
aspinall@21637
    81
       | Askprefs	of { }
aspinall@21637
    82
       | Setpref 	of { name:string, prefcategory:string option, value:string }
aspinall@21637
    83
       | Getpref 	of { name:string, prefcategory:string option }
aspinall@21637
    84
       (* prover control *)
aspinall@21637
    85
       | Proverinit 	of { }
aspinall@21637
    86
       | Proverexit 	of { }
aspinall@22406
    87
       | Setproverflag  of { flagname:string, value: bool }
aspinall@21637
    88
       (* improper proof commands: control proof state *)
aspinall@21637
    89
       | Dostep		of { text: string }
aspinall@21637
    90
       | Undostep	of { times: int }
aspinall@21637
    91
       | Redostep	of { }
aspinall@21637
    92
       | Abortgoal	of { }
aspinall@21637
    93
       | Forget		of { thyname: string option, name: string option, 
aspinall@21867
    94
			     objtype: PgipTypes.objtype option }
aspinall@21637
    95
       | Restoregoal    of { thmname : string }
aspinall@21637
    96
       (* context inspection commands *)
aspinall@21867
    97
       | Askids         of { url: PgipTypes.pgipurl option,
aspinall@21867
    98
			     thyname: PgipTypes.objname option,
aspinall@21867
    99
			     objtype: PgipTypes.objtype option }
aspinall@22161
   100
       | Askrefs        of { url: PgipTypes.pgipurl option,
aspinall@22161
   101
			     thyname: PgipTypes.objname option,
aspinall@22161
   102
			     objtype: PgipTypes.objtype option,
aspinall@22161
   103
			     name: PgipTypes.objname option }
aspinall@21867
   104
       | Showid         of { thyname: PgipTypes.objname option, 
aspinall@21867
   105
			     objtype: PgipTypes.objtype, 
aspinall@21867
   106
			     name: PgipTypes.objname }
aspinall@21637
   107
       | Askguise	of { }
aspinall@21637
   108
       | Parsescript	of { text: string, location: location,
aspinall@21637
   109
			     systemdata: string option } 
aspinall@21637
   110
       | Showproofstate of { }
aspinall@21637
   111
       | Showctxt	of { }
aspinall@21637
   112
       | Searchtheorems of { arg: string }
aspinall@21637
   113
       | Setlinewidth   of { width: int }
aspinall@21637
   114
       | Viewdoc	of { arg: string }
aspinall@21637
   115
       (* improper theory-level commands *)
aspinall@21637
   116
       | Doitem		of { text: string }
aspinall@21637
   117
       | Undoitem	of { }
aspinall@21637
   118
       | Redoitem	of { }
aspinall@21637
   119
       | Aborttheory	of { }
aspinall@21637
   120
       | Retracttheory  of { thyname: string }
aspinall@21637
   121
       | Loadfile 	of { url: pgipurl }
aspinall@21637
   122
       | Openfile 	of { url: pgipurl }
aspinall@21637
   123
       | Closefile	of { }
aspinall@21637
   124
       | Abortfile	of { }
aspinall@21637
   125
       | Retractfile    of { url: pgipurl }
aspinall@21637
   126
       | Changecwd 	of { url: pgipurl }
aspinall@21637
   127
       | Systemcmd 	of { arg: string }
aspinall@21637
   128
       (* unofficial escape command for debugging *)
aspinall@21637
   129
       | Quitpgip	of { }
aspinall@21637
   130
aspinall@21637
   131
(* Extracting content from input XML elements to make a PGIPinput *)
aspinall@21637
   132
local
aspinall@21637
   133
aspinall@21637
   134
    val thyname_attro = get_attr_opt "thyname"
aspinall@21637
   135
    val thyname_attr = get_attr "thyname"
aspinall@21637
   136
    val name_attr = get_attr "name"
aspinall@21637
   137
    val name_attro = get_attr_opt "name"
aspinall@21637
   138
    val thmname_attr = get_attr "thmname"
aspinall@22406
   139
    val flagname_attr = get_attr "flagname"
aspinall@22406
   140
    val value_attr = get_attr "value"
aspinall@21637
   141
aspinall@21867
   142
    fun objtype_attro attrs = if has_attr "objtype" attrs then
aspinall@21867
   143
				  SOME (objtype_of_attrs attrs)
aspinall@21867
   144
			      else NONE
aspinall@21867
   145
aspinall@21867
   146
    fun pgipurl_attro attrs = if has_attr "url" attrs then
aspinall@21867
   147
				  SOME (pgipurl_of_attrs attrs)
aspinall@21867
   148
			      else NONE
aspinall@21867
   149
aspinall@21637
   150
    val times_attr = read_pgipnat o (get_attr_dflt "times" "1")
aspinall@21637
   151
    val prefcat_attr = get_attr_opt "prefcategory"
aspinall@21637
   152
wenzelm@21940
   153
    fun xmltext (XML.Text text :: ts) = text ^ xmltext ts
aspinall@21637
   154
      | xmltext [] = ""
aspinall@21637
   155
      | xmltext _ = raise PGIP "Expected text (PCDATA/CDATA)"
aspinall@21637
   156
aspinall@21637
   157
    exception Unknown
aspinall@21637
   158
    exception NoAction
aspinall@21637
   159
in
aspinall@21637
   160
aspinall@21637
   161
(* Return a valid PGIP input command.
aspinall@21637
   162
   Raise PGIP exception for invalid data.
aspinall@21637
   163
   Return NONE for unknown/unhandled commands. 
aspinall@21637
   164
*)
aspinall@21637
   165
fun input (elem, attrs, data) =
aspinall@21637
   166
SOME 
aspinall@21637
   167
 (case elem of 
aspinall@21637
   168
     "askpgip"        => Askpgip { }
aspinall@21637
   169
   | "askpgml"        => Askpgml { }
aspinall@21637
   170
   | "askconfig"      => Askconfig { }
aspinall@21637
   171
   (* proverconfig *)
aspinall@21637
   172
   | "askprefs"       => Askprefs { }
aspinall@21637
   173
   | "getpref"        => Getpref { name = name_attr attrs, 
aspinall@21637
   174
				   prefcategory = prefcat_attr attrs }
aspinall@21637
   175
   | "setpref"        => Setpref { name = name_attr attrs, 
aspinall@21637
   176
				   prefcategory = prefcat_attr attrs,
aspinall@21637
   177
				   value = xmltext data }
aspinall@21637
   178
   (* provercontrol *)
aspinall@21637
   179
   | "proverinit"     => Proverinit { }
aspinall@21637
   180
   | "proverexit"     => Proverexit { }
aspinall@22406
   181
   | "setproverflag"  => Setproverflag { flagname = flagname_attr attrs,
aspinall@22406
   182
					 value = read_pgipbool (value_attr attrs) }
aspinall@21637
   183
   (* improperproofcmd: improper commands not in script *)
aspinall@21637
   184
   | "dostep"         => Dostep    { text = xmltext data }
aspinall@21637
   185
   | "undostep"       => Undostep  { times = times_attr attrs }
aspinall@21637
   186
   | "redostep"       => Redostep  { } 
aspinall@21637
   187
   | "abortgoal"      => Abortgoal { }
aspinall@21637
   188
   | "forget"         => Forget { thyname = thyname_attro attrs, 
aspinall@21637
   189
				  name = name_attro attrs,
aspinall@21637
   190
				  objtype = objtype_attro attrs }
aspinall@21637
   191
   | "restoregoal"    => Restoregoal { thmname = thmname_attr attrs}
aspinall@21637
   192
   (* proofctxt: improper commands *)
aspinall@21867
   193
   | "askids"         => Askids { url = pgipurl_attro attrs,
aspinall@21867
   194
				  thyname = thyname_attro attrs, 
aspinall@21637
   195
				  objtype = objtype_attro attrs }
aspinall@22161
   196
   | "askrefs"        => Askrefs { url = pgipurl_attro attrs,
aspinall@22161
   197
				   thyname = thyname_attro attrs, 
aspinall@22161
   198
				   objtype = objtype_attro attrs,
aspinall@22161
   199
				   name = name_attro attrs }
aspinall@21637
   200
   | "showid"         => Showid { thyname = thyname_attro attrs,
aspinall@21867
   201
				  objtype = objtype_of_attrs attrs,
aspinall@21637
   202
				  name = name_attr attrs }
aspinall@21637
   203
   | "askguise"       => Askguise { }
aspinall@21637
   204
   | "parsescript"    => Parsescript { text = (xmltext data),
aspinall@21637
   205
				       systemdata = get_attr_opt "systemdata" attrs,
aspinall@21637
   206
				       location = location_of_attrs attrs }
aspinall@21637
   207
   | "showproofstate" => Showproofstate { }
aspinall@21637
   208
   | "showctxt"       => Showctxt { }
aspinall@21637
   209
   | "searchtheorems" => Searchtheorems { arg = xmltext data }
aspinall@21637
   210
   | "setlinewidth"   => Setlinewidth   { width = read_pgipnat (xmltext data) }
aspinall@21637
   211
   | "viewdoc"        => Viewdoc { arg = xmltext data }
aspinall@21637
   212
   (* improperfilecmd: theory-level commands not in script *)
aspinall@21637
   213
   | "doitem"         => Doitem  { text = xmltext data }
aspinall@21637
   214
   | "undoitem"       => Undoitem { }
aspinall@21637
   215
   | "redoitem"       => Redoitem { }
aspinall@21637
   216
   | "aborttheory"    => Aborttheory { }
aspinall@21637
   217
   | "retracttheory"  => Retracttheory { thyname = thyname_attr attrs }
aspinall@21637
   218
   | "loadfile"       => Loadfile { url = pgipurl_of_attrs attrs }
aspinall@21637
   219
   | "openfile"       => Openfile { url = pgipurl_of_attrs attrs }
aspinall@21637
   220
   | "closefile"      => Closefile { }
aspinall@21637
   221
   | "abortfile"      => Abortfile { }
aspinall@21637
   222
   | "retractfile"    => Retractfile { url = pgipurl_of_attrs attrs }
aspinall@21637
   223
   | "changecwd"      => Changecwd { url = pgipurl_of_attrs attrs }
aspinall@21637
   224
   | "systemcmd"      => Systemcmd { arg = xmltext data }
aspinall@21637
   225
   (* unofficial command for debugging *)
aspinall@21637
   226
   | "quitpgip"	=> Quitpgip { }
aspinall@21637
   227
aspinall@21637
   228
   (* We allow sending proper document markup too; we map it back to dostep   *)
aspinall@21637
   229
   (* and strip out metainfo elements. Markup correctness isn't checked: this *)
aspinall@21637
   230
   (* is a compatibility measure to make it easy for interfaces.              *)
aspinall@21655
   231
   | x => if (x mem PgipMarkup.doc_markup_elements) then
aspinall@21655
   232
	      if (x mem PgipMarkup.doc_markup_elements_ignored) then
aspinall@21655
   233
		  raise NoAction
aspinall@21655
   234
	      else 
aspinall@21655
   235
		  Dostep { text = xmltext data } (* could separate out Doitem too *)
aspinall@21637
   236
	  else raise Unknown) 
aspinall@21637
   237
    handle Unknown => NONE | NoAction => NONE
aspinall@21637
   238
end
aspinall@21637
   239
aspinall@21637
   240
end