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