src/Pure/ProofGeneral/pgip_output.ML
author aspinall
Mon, 04 Dec 2006 20:40:11 +0100
changeset 21637 a7b156c404e2
child 21649 40e6fdd26f82
permissions -rw-r--r--
Revamped Proof General interface.
     1 (*  Title:      Pure/ProofGeneral/pgip_output.ML
     2     ID:         $Id$
     3     Author:     David Aspinall
     4 
     5 PGIP abstraction: output commands
     6 *)
     7 
     8 signature PGIPOUTPUT =
     9 sig
    10     (* These are the PGIP messages which the prover emits. *) 
    11     datatype pgipoutput = 
    12 	     Cleardisplay        of { area: PgipTypes.displayarea }
    13 	   | Normalresponse      of { area: PgipTypes.displayarea, 
    14 				      urgent: bool, 
    15 				      messagecategory: PgipTypes.messagecategory, 
    16 				      content: XML.tree list }
    17 	   | Errorresponse       of { fatality: PgipTypes.fatality, 
    18 				      area: PgipTypes.displayarea option, 
    19 				      location: PgipTypes.location option, 
    20 				      content: XML.tree list }
    21 	   | Informfileloaded    of { url: PgipTypes.pgipurl }
    22 	   | Informfileretracted of { url: PgipTypes.pgipurl }
    23 	   | Proofstate	         of { pgml: XML.tree list }
    24 	   | Metainforesponse    of { attrs: XML.attributes, 
    25 				      content: XML.tree list }
    26 	   | Lexicalstructure    of { content: XML.tree list }
    27 	   | Proverinfo	         of { name: string, 
    28 				      version: string, 
    29 				      instance: string,
    30 				      descr: string, 
    31 				      url: Url.T, 
    32 				      filenameextns: string }
    33 	   | Idtable	         of { objtype: string, 
    34 				      context: string option, 
    35 				      ids: string list }
    36            | Setids		 of { idtables: XML.tree list }
    37            | Delids		 of { idtables: XML.tree list }
    38            | Addids		 of { idtables: XML.tree list }
    39 	   | Hasprefs	         of { prefcategory: string option, 
    40 				      prefs: PgipTypes.preference list }
    41            | Prefval		 of { name: string, value: string }
    42            | Idvalue		 of { name: string, objtype: string, text: XML.tree list }
    43 	   | Informguise	 of { file : PgipTypes.pgipurl option,  
    44 				      theory: string option, 
    45 				      theorem: string option, 
    46 				      proofpos: int option }
    47 	   | Parseresult	 of { attrs: XML.attributes, content: XML.tree list }
    48            | Usespgip		 of { version: string, 
    49 				      pgipelems: (string * bool * string list) list }
    50            | Usespgml		 of { version: string }
    51 	   | Pgip		 of { tag: string option, 
    52 				      class: string, 
    53 				      seq: int, id: string, 
    54 				      destid: string option,
    55 				      refid: string option,
    56 				      refseq: int option,
    57 				      content: XML.tree list }
    58 	   | Ready		 of { }
    59 
    60     val output : pgipoutput -> XML.tree					 
    61 end
    62 
    63 structure PgipOutput : PGIPOUTPUT =
    64 struct
    65 open PgipTypes
    66 
    67 datatype pgipoutput = 
    68 	 Cleardisplay        of { area: displayarea }
    69        | Normalresponse      of { area: displayarea, urgent: bool, 
    70 				  messagecategory: messagecategory, content: XML.tree list }
    71        | Errorresponse       of { area: displayarea option, fatality: fatality, 
    72 				  location: location option, content: XML.tree list }
    73        | Informfileloaded    of { url: Path.T }
    74        | Informfileretracted of { url: Path.T }
    75        | Proofstate	     of { pgml: XML.tree list }
    76        | Metainforesponse    of { attrs: XML.attributes, content: XML.tree list }
    77        | Lexicalstructure    of { content: XML.tree list }
    78        | Proverinfo	     of { name: string, version: string, instance: string,
    79 				  descr: string, url: Url.T, filenameextns: string }
    80        | Idtable	     of { objtype: string, context: string option, ids: string list }
    81        | Setids		     of { idtables: XML.tree list }
    82        | Delids		     of { idtables: XML.tree list }
    83        | Addids		     of { idtables: XML.tree list }
    84        | Hasprefs	     of { prefcategory: string option, prefs: preference list }
    85        | Prefval  	     of { name: string, value: string }
    86        | Idvalue	     of { name: string, objtype: string, text: XML.tree list }
    87        | Informguise	     of { file : pgipurl option,  theory: string option, 
    88 				  theorem: string option, proofpos: int option }
    89        | Parseresult	     of { attrs: XML.attributes, content: XML.tree list }
    90        | Usespgip	     of { version: string, 
    91 				  pgipelems: (string * bool * string list) list }
    92        | Usespgml	     of { version: string }
    93        | Pgip		     of { tag: string option, 
    94 				  class: string, 
    95 				  seq: int, id: string, 
    96 				  destid: string option,
    97 				  refid: string option,
    98 				  refseq: int option,
    99 				  content: XML.tree list}
   100        | Ready		     of { }
   101 
   102 
   103 (* util *)
   104 
   105 fun attrsome attr f x = case x of NONE => [] | SOME x => [(attr,f x)]
   106 
   107 (* Construct output XML messages *)
   108 	
   109 fun cleardisplay vs = 
   110     let
   111 	val area = #area vs
   112     in
   113 	XML.Elem ("cleardisplay", (attrs_of_displayarea area), [])
   114     end
   115 
   116 fun normalresponse vs =
   117     let 
   118 	val area = #area vs
   119 	val urgent = #urgent vs
   120 	val messagecategory = #messagecategory vs
   121 	val content = #content vs
   122     in
   123 	XML.Elem ("normalresponse", 
   124 		 (attrs_of_displayarea area) @
   125 		 (if urgent then [("urgent", "true")] else []) @
   126 		 (attrs_of_messagecategory messagecategory),
   127 		 content)
   128     end
   129 				       
   130 fun errorresponse vs =
   131     let 
   132 	val area = #area vs
   133 	val fatality = #fatality vs
   134 	val location = #location vs
   135 	val content = #content vs
   136     in
   137 	XML.Elem ("errorresponse",
   138 		 (the_default [] (Option.map attrs_of_displayarea area)) @
   139 		 (attrs_of_fatality fatality) @
   140 		 (the_default [] (Option.map attrs_of_location location)),
   141 		 content)
   142     end
   143 				       
   144 
   145 fun informfile lr vs =
   146     let 
   147 	val url = #url vs
   148     in
   149 	XML.Elem ("informfile" ^ lr, attrs_of_pgipurl url, [])
   150     end
   151 
   152 val informfileloaded = informfile "loaded"
   153 val informfileretracted = informfile "retracted"
   154 
   155 fun proofstate vs =
   156     let
   157 	val pgml = #pgml vs
   158     in
   159 	XML.Elem("proofstate", [], pgml)
   160     end
   161 
   162 fun metainforesponse vs =
   163     let 
   164 	val attrs = #attrs vs
   165 	val content = #content vs
   166     in
   167 	XML.Elem ("metainforesponse", attrs, content)
   168     end
   169 
   170 fun lexicalstructure vs =
   171     let
   172 	val content = #content vs
   173     in
   174 	XML.Elem ("lexicalstructure", [], content)
   175     end
   176 
   177 fun proverinfo vs =
   178     let
   179 	val name = #name vs
   180 	val version = #version vs
   181 	val instance = #instance vs
   182 	val descr = #descr vs
   183 	val url = #url vs
   184 	val filenameextns = #filenameextns vs
   185     in 
   186 	XML.Elem ("proverinfo",
   187 		 [("name", name),
   188 		  ("version", version),
   189 		  ("instance", instance), 
   190 		  ("descr", descr),
   191 		  ("url", Url.pack url),
   192 		  ("filenameextns", filenameextns)],
   193 		 [])
   194     end
   195 
   196 fun idtable vs = 
   197     let 
   198 	val objtype = #objtype vs
   199 	val objtype_attrs = [("objtype", objtype)]
   200 	val context = #context vs
   201 	val context_attrs = opt_attr "context" context
   202 	val ids = #ids vs
   203 	val ids_content = map (fn x => XML.Elem("identifier",[],[XML.Text x])) ids
   204     in 
   205 	XML.Elem ("idtable",
   206 		  objtype_attrs @ context_attrs,
   207 		  ids_content)
   208     end
   209 
   210 fun setids vs =
   211     let
   212 	val idtables = #idtables vs
   213     in
   214 	XML.Elem ("setids",[],idtables)
   215     end
   216 
   217 fun addids vs =
   218     let
   219 	val idtables = #idtables vs
   220     in
   221 	XML.Elem ("addids",[],idtables)
   222     end
   223 
   224 fun delids vs =
   225     let
   226 	val idtables = #idtables vs
   227     in
   228 	XML.Elem ("delids",[],idtables)
   229     end
   230 
   231 
   232 fun acceptedpgipelems vs = 
   233     let
   234 	val pgipelems = #pgipelems vs
   235 	fun async_attrs b = if b then [("async","true")] else []
   236 	fun attrs_attrs attrs = if attrs=[] then [] else [("attributes",space_implode "," attrs)]
   237 	fun singlepgipelem (e,async,attrs) = 
   238 	    XML.Elem("pgipelem", ((async_attrs async) @ (attrs_attrs attrs)),[])
   239 						      
   240     in
   241 	XML.Elem ("acceptedpgipelems", [],
   242 		 map singlepgipelem pgipelems)
   243     end
   244 
   245 
   246 fun attro x yo = case yo of NONE => [] | SOME y => [(x,y)] : XML.attributes
   247 
   248 fun hasprefs vs =
   249   let 
   250       val prefcategory = #prefcategory vs
   251       val prefs = #prefs vs
   252   in 
   253       XML.Elem("hasprefs",attro "prefcategory" prefcategory, map haspref prefs)
   254   end
   255 
   256 fun prefval vs =
   257     let 
   258 	val name = #name vs
   259 	val value = #value vs
   260     in
   261 	XML.Elem("prefval", [("name",name)], [XML.Text value])
   262     end 
   263 
   264 fun idvalue vs =
   265     let 
   266 	val name = #name vs
   267 	val objtype = #objtype vs
   268 	val text = #text vs
   269     in
   270 	XML.Elem("idvalue", [("name",name),("objtype",objtype)], text)
   271     end 
   272 
   273 
   274 fun informguise vs =
   275   let
   276       val file = #file vs
   277       val theory = #theory vs
   278       val theorem = #theorem vs
   279       val proofpos = #proofpos vs
   280 
   281       fun elto nm attrfn xo = case xo of NONE=>[] | SOME x=>[XML.Elem(nm,attrfn x,[])]
   282 
   283       val guisefile = elto "guisefile" attrs_of_pgipurl file
   284       val guisetheory = elto "guisetheory" (single o (pair "thyname")) theory
   285       val guiseproof = elto "guiseproof" 
   286 			    (fn thm=>[("thmname",thm)]@
   287 				     (attro "proofpos" (Option.map Int.toString proofpos))) theorem
   288   in 
   289       XML.Elem("informguise", [], guisefile @ guisetheory @ guiseproof)
   290   end
   291 
   292 fun parseresult vs =
   293     let
   294 	val attrs = #attrs vs
   295 	val content = #content vs
   296     in 
   297 	XML.Elem("parseresult", attrs, content)
   298     end
   299 
   300 fun usespgip vs =
   301     let
   302 	val version = #version vs
   303 	val acceptedelems = acceptedpgipelems vs
   304     in 
   305 	XML.Elem("usespgip", [("version", version)], [acceptedelems])
   306     end
   307 
   308 fun usespgml vs =
   309     let
   310 	val version = #version vs
   311     in 
   312 	XML.Elem("usespgml", [("version", version)], [])
   313     end
   314 
   315 fun pgip vs =
   316     let 
   317 	val tag = #tag vs
   318 	val class = #class vs
   319 	val seq = #seq vs
   320 	val id = #id vs
   321 	val destid = #destid vs
   322 	val refid = #refid vs
   323 	val refseq = #refseq vs
   324 	val content = #content vs
   325     in
   326 	XML.Elem("pgip",
   327 		 opt_attr "tag" tag @
   328 		 [("id", id)] @
   329 		 opt_attr "destid" destid @
   330 		 [("class", class)] @
   331 		 opt_attr "refid" refid @
   332 		 opt_attr_map string_of_int "refseq" refseq @
   333 		 [("seq", string_of_int seq)],
   334 		 content)
   335     end
   336 
   337 fun ready vs = 
   338     XML.Elem("ready",[],[])
   339 
   340 
   341 fun output pgipoutput = case pgipoutput of
   342    Cleardisplay vs 	   => cleardisplay vs
   343  | Normalresponse vs       => normalresponse vs
   344  | Errorresponse vs        => errorresponse vs
   345  | Informfileloaded vs	   => informfileloaded vs
   346  | Informfileretracted vs  => informfileretracted vs
   347  | Proofstate vs	   => proofstate vs
   348  | Metainforesponse vs	   => metainforesponse vs
   349  | Lexicalstructure vs	   => lexicalstructure vs
   350  | Proverinfo vs	   => proverinfo vs
   351  | Idtable vs		   => idtable vs
   352  | Setids vs		   => setids vs
   353  | Addids vs		   => addids vs
   354  | Delids vs		   => delids vs
   355  | Hasprefs vs		   => hasprefs vs
   356  | Prefval vs		   => prefval vs
   357  | Idvalue vs		   => idvalue vs
   358  | Informguise vs	   => informguise vs
   359  | Parseresult vs	   => parseresult vs
   360  | Usespgip vs		   => usespgip vs
   361  | Usespgml vs		   => usespgml vs
   362  | Pgip vs		   => pgip vs
   363  | Ready vs		   => ready vs
   364 
   365 end
   366