src/Pure/ProofGeneral/pgip_output.ML
author aspinall
Tue, 05 Dec 2006 13:56:43 +0100
changeset 21649 40e6fdd26f82
parent 21637 a7b156c404e2
child 21651 99f4a06184dc
permissions -rw-r--r--
Support PGIP communication for preferences in Emacs mode.
     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 (* Construct output XML messages *)
   104         
   105 fun cleardisplay vs = 
   106     let
   107         val area = #area vs
   108     in
   109         XML.Elem ("cleardisplay", (attrs_of_displayarea area), [])
   110     end
   111 
   112 fun normalresponse vs =
   113     let 
   114         val area = #area vs
   115         val urgent = #urgent vs
   116         val messagecategory = #messagecategory vs
   117         val content = #content vs
   118     in
   119         XML.Elem ("normalresponse", 
   120                  (attrs_of_displayarea area) @
   121                  (if urgent then [("urgent", "true")] else []) @
   122                  (attrs_of_messagecategory messagecategory),
   123                  content)
   124     end
   125                                        
   126 fun errorresponse vs =
   127     let 
   128         val area = #area vs
   129         val fatality = #fatality vs
   130         val location = #location vs
   131         val content = #content vs
   132     in
   133         XML.Elem ("errorresponse",
   134                  (the_default [] (Option.map attrs_of_displayarea area)) @
   135                  (attrs_of_fatality fatality) @
   136                  (the_default [] (Option.map attrs_of_location location)),
   137                  content)
   138     end
   139                                        
   140 
   141 fun informfile lr vs =
   142     let 
   143         val url = #url vs
   144     in
   145         XML.Elem ("informfile" ^ lr, attrs_of_pgipurl url, [])
   146     end
   147 
   148 val informfileloaded = informfile "loaded"
   149 val informfileretracted = informfile "retracted"
   150 
   151 fun proofstate vs =
   152     let
   153         val pgml = #pgml vs
   154     in
   155         XML.Elem("proofstate", [], pgml)
   156     end
   157 
   158 fun metainforesponse vs =
   159     let 
   160         val attrs = #attrs vs
   161         val content = #content vs
   162     in
   163         XML.Elem ("metainforesponse", attrs, content)
   164     end
   165 
   166 fun lexicalstructure vs =
   167     let
   168         val content = #content vs
   169     in
   170         XML.Elem ("lexicalstructure", [], content)
   171     end
   172 
   173 fun proverinfo vs =
   174     let
   175         val name = #name vs
   176         val version = #version vs
   177         val instance = #instance vs
   178         val descr = #descr vs
   179         val url = #url vs
   180         val filenameextns = #filenameextns vs
   181     in 
   182         XML.Elem ("proverinfo",
   183                  [("name", name),
   184                   ("version", version),
   185                   ("instance", instance), 
   186                   ("descr", descr),
   187                   ("url", Url.pack url),
   188                   ("filenameextns", filenameextns)],
   189                  [])
   190     end
   191 
   192 fun idtable vs = 
   193     let 
   194         val objtype = #objtype vs
   195         val objtype_attrs = attr [("objtype", objtype)]
   196         val context = #context vs
   197         val context_attrs = opt_attr "context" context
   198         val ids = #ids vs
   199         val ids_content = map (fn x => XML.Elem("identifier",[],[XML.Text x])) ids
   200     in 
   201         XML.Elem ("idtable",
   202                   objtype_attrs @ context_attrs,
   203                   ids_content)
   204     end
   205 
   206 fun setids vs =
   207     let
   208         val idtables = #idtables vs
   209     in
   210         XML.Elem ("setids",[],idtables)
   211     end
   212 
   213 fun addids vs =
   214     let
   215         val idtables = #idtables vs
   216     in
   217         XML.Elem ("addids",[],idtables)
   218     end
   219 
   220 fun delids vs =
   221     let
   222         val idtables = #idtables vs
   223     in
   224         XML.Elem ("delids",[],idtables)
   225     end
   226 
   227 
   228 fun acceptedpgipelems vs = 
   229     let
   230         val pgipelems = #pgipelems vs
   231         fun async_attrs b = if b then [("async","true")] else []
   232         fun attrs_attrs attrs = if attrs=[] then [] else [("attributes",space_implode "," attrs)]
   233         fun singlepgipelem (e,async,attrs) = 
   234             XML.Elem("pgipelem", ((async_attrs async) @ (attrs_attrs attrs)),[])
   235                                                       
   236     in
   237         XML.Elem ("acceptedpgipelems", [],
   238                  map singlepgipelem pgipelems)
   239     end
   240 
   241 
   242 fun hasprefs vs =
   243   let 
   244       val prefcategory = #prefcategory vs
   245       val prefs = #prefs vs
   246   in 
   247       XML.Elem("hasprefs",opt_attr "prefcategory" prefcategory, map haspref prefs)
   248   end
   249 
   250 fun prefval vs =
   251     let 
   252         val name = #name vs
   253         val value = #value vs
   254     in
   255         XML.Elem("prefval", [("name",name)], [XML.Text value])
   256     end 
   257 
   258 fun idvalue vs =
   259     let 
   260         val name = #name vs
   261         val objtype = #objtype vs
   262         val text = #text vs
   263     in
   264         XML.Elem("idvalue", [("name",name),("objtype",objtype)], text)
   265     end 
   266 
   267 
   268 fun informguise vs =
   269   let
   270       val file = #file vs
   271       val theory = #theory vs
   272       val theorem = #theorem vs
   273       val proofpos = #proofpos vs
   274 
   275       fun elto nm attrfn xo = case xo of NONE=>[] | SOME x=>[XML.Elem(nm,attrfn x,[])]
   276 
   277       val guisefile = elto "guisefile" attrs_of_pgipurl file
   278       val guisetheory = elto "guisetheory" (single o (pair "thyname")) theory
   279       val guiseproof = elto "guiseproof" 
   280                             (fn thm=>[("thmname",thm)]@
   281                                      (opt_attr "proofpos" (Option.map Int.toString proofpos))) theorem
   282   in 
   283       XML.Elem("informguise", [], guisefile @ guisetheory @ guiseproof)
   284   end
   285 
   286 fun parseresult vs =
   287     let
   288         val attrs = #attrs vs
   289         val content = #content vs
   290     in 
   291         XML.Elem("parseresult", attrs, content)
   292     end
   293 
   294 fun usespgip vs =
   295     let
   296         val version = #version vs
   297         val acceptedelems = acceptedpgipelems vs
   298     in 
   299         XML.Elem("usespgip", [("version", version)], [acceptedelems])
   300     end
   301 
   302 fun usespgml vs =
   303     let
   304         val version = #version vs
   305     in 
   306         XML.Elem("usespgml", [("version", version)], [])
   307     end
   308 
   309 fun pgip vs =
   310     let 
   311         val tag = #tag vs
   312         val class = #class vs
   313         val seq = #seq vs
   314         val id = #id vs
   315         val destid = #destid vs
   316         val refid = #refid vs
   317         val refseq = #refseq vs
   318         val content = #content vs
   319     in
   320         XML.Elem("pgip",
   321                  opt_attr "tag" tag @
   322                  [("id", id)] @
   323                  opt_attr "destid" destid @
   324                  [("class", class)] @
   325                  opt_attr "refid" refid @
   326                  opt_attr_map string_of_int "refseq" refseq @
   327                  [("seq", string_of_int seq)],
   328                  content)
   329     end
   330 
   331 fun ready vs = XML.Elem("ready",[],[])
   332 
   333 
   334 fun output pgipoutput = case pgipoutput of
   335    Cleardisplay vs         => cleardisplay vs
   336  | Normalresponse vs       => normalresponse vs
   337  | Errorresponse vs        => errorresponse vs
   338  | Informfileloaded vs     => informfileloaded vs
   339  | Informfileretracted vs  => informfileretracted vs
   340  | Proofstate vs           => proofstate vs
   341  | Metainforesponse vs     => metainforesponse vs
   342  | Lexicalstructure vs     => lexicalstructure vs
   343  | Proverinfo vs           => proverinfo vs
   344  | Idtable vs              => idtable vs
   345  | Setids vs               => setids vs
   346  | Addids vs               => addids vs
   347  | Delids vs               => delids vs
   348  | Hasprefs vs             => hasprefs vs
   349  | Prefval vs              => prefval vs
   350  | Idvalue vs              => idvalue vs
   351  | Informguise vs          => informguise vs
   352  | Parseresult vs          => parseresult vs
   353  | Usespgip vs             => usespgip vs
   354  | Usespgml vs             => usespgml vs
   355  | Pgip vs                 => pgip vs
   356  | Ready vs                => ready vs
   357 
   358 end
   359