src/Pure/PIDE/protocol.scala
author wenzelm
Tue Aug 12 18:36:43 2014 +0200 (2014-08-12)
changeset 57916 2c2c24dbf0a4
parent 57911 dcb758188aa6
child 58015 2777096e0adf
permissions -rw-r--r--
generic process wrapping in Prover;
clarified module arrangement;
     1 /*  Title:      Pure/PIDE/protocol.scala
     2     Author:     Makarius
     3 
     4 Protocol message formats for interactive proof documents.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 object Protocol
    11 {
    12   /* document editing */
    13 
    14   object Assign_Update
    15   {
    16     def unapply(text: String): Option[(Document_ID.Version, Document.Assign_Update)] =
    17       try {
    18         import XML.Decode._
    19         val body = YXML.parse_body(text)
    20         Some(pair(long, list(pair(long, list(long))))(body))
    21       }
    22       catch {
    23         case ERROR(_) => None
    24         case _: XML.Error => None
    25       }
    26   }
    27 
    28   object Removed
    29   {
    30     def unapply(text: String): Option[List[Document_ID.Version]] =
    31       try {
    32         import XML.Decode._
    33         Some(list(long)(YXML.parse_body(text)))
    34       }
    35       catch {
    36         case ERROR(_) => None
    37         case _: XML.Error => None
    38       }
    39   }
    40 
    41 
    42   /* command status */
    43 
    44   object Status
    45   {
    46     def make(markup_iterator: Iterator[Markup]): Status =
    47     {
    48       var touched = false
    49       var accepted = false
    50       var warned = false
    51       var failed = false
    52       var forks = 0
    53       var runs = 0
    54       for (markup <- markup_iterator) {
    55         markup.name match {
    56           case Markup.ACCEPTED => accepted = true
    57           case Markup.FORKED => touched = true; forks += 1
    58           case Markup.JOINED => forks -= 1
    59           case Markup.RUNNING => touched = true; runs += 1
    60           case Markup.FINISHED => runs -= 1
    61           case Markup.WARNING => warned = true
    62           case Markup.FAILED | Markup.ERROR => failed = true
    63           case _ =>
    64         }
    65       }
    66       Status(touched, accepted, warned, failed, forks, runs)
    67     }
    68 
    69     val empty = make(Iterator.empty)
    70 
    71     def merge(status_iterator: Iterator[Status]): Status =
    72       if (status_iterator.hasNext) {
    73         val status0 = status_iterator.next
    74         (status0 /: status_iterator)(_ + _)
    75       }
    76       else empty
    77   }
    78 
    79   sealed case class Status(
    80     private val touched: Boolean,
    81     private val accepted: Boolean,
    82     private val warned: Boolean,
    83     private val failed: Boolean,
    84     forks: Int,
    85     runs: Int)
    86   {
    87     def + (that: Status): Status =
    88       Status(
    89         touched || that.touched,
    90         accepted || that.accepted,
    91         warned || that.warned,
    92         failed || that.failed,
    93         forks + that.forks,
    94         runs + that.runs)
    95 
    96     def is_unprocessed: Boolean = accepted && !failed && (!touched || (forks != 0 && runs == 0))
    97     def is_running: Boolean = runs != 0
    98     def is_warned: Boolean = warned
    99     def is_failed: Boolean = failed
   100     def is_finished: Boolean = !failed && touched && forks == 0 && runs == 0
   101   }
   102 
   103   val proper_status_elements =
   104     Markup.Elements(Markup.ACCEPTED, Markup.FORKED, Markup.JOINED, Markup.RUNNING,
   105       Markup.FINISHED, Markup.FAILED)
   106 
   107   val liberal_status_elements =
   108     proper_status_elements + Markup.WARNING + Markup.ERROR
   109 
   110 
   111   /* command timing */
   112 
   113   object Command_Timing
   114   {
   115     def unapply(props: Properties.T): Option[(Document_ID.Generic, isabelle.Timing)] =
   116       props match {
   117         case (Markup.FUNCTION, Markup.COMMAND_TIMING) :: args =>
   118           (args, args) match {
   119             case (Position.Id(id), Markup.Timing_Properties(timing)) => Some((id, timing))
   120             case _ => None
   121           }
   122         case _ => None
   123       }
   124   }
   125 
   126 
   127   /* node status */
   128 
   129   sealed case class Node_Status(
   130     unprocessed: Int, running: Int, warned: Int, failed: Int, finished: Int)
   131   {
   132     def total: Int = unprocessed + running + warned + failed + finished
   133   }
   134 
   135   def node_status(
   136     state: Document.State, version: Document.Version, node: Document.Node): Node_Status =
   137   {
   138     var unprocessed = 0
   139     var running = 0
   140     var warned = 0
   141     var failed = 0
   142     var finished = 0
   143     for (command <- node.commands.iterator) {
   144       val states = state.command_states(version, command)
   145       val status = Status.merge(states.iterator.map(_.protocol_status))
   146 
   147       if (status.is_running) running += 1
   148       else if (status.is_failed) failed += 1
   149       else if (status.is_warned) warned += 1
   150       else if (status.is_finished) finished += 1
   151       else unprocessed += 1
   152     }
   153     Node_Status(unprocessed, running, warned, failed, finished)
   154   }
   155 
   156 
   157   /* node timing */
   158 
   159   sealed case class Node_Timing(total: Double, commands: Map[Command, Double])
   160 
   161   val empty_node_timing = Node_Timing(0.0, Map.empty)
   162 
   163   def node_timing(
   164     state: Document.State,
   165     version: Document.Version,
   166     node: Document.Node,
   167     threshold: Double): Node_Timing =
   168   {
   169     var total = 0.0
   170     var commands = Map.empty[Command, Double]
   171     for {
   172       command <- node.commands.iterator
   173       st <- state.command_states(version, command)
   174     } {
   175       val command_timing =
   176         (0.0 /: st.status)({
   177           case (timing, Markup.Timing(t)) => timing + t.elapsed.seconds
   178           case (timing, _) => timing
   179         })
   180       total += command_timing
   181       if (command_timing >= threshold) commands += (command -> command_timing)
   182     }
   183     Node_Timing(total, commands)
   184   }
   185 
   186 
   187   /* result messages */
   188 
   189   private val clean_elements =
   190     Markup.Elements(Markup.REPORT, Markup.NO_REPORT)
   191 
   192   def clean_message(body: XML.Body): XML.Body =
   193     body filter {
   194       case XML.Wrapped_Elem(Markup(name, _), _, _) => !clean_elements(name)
   195       case XML.Elem(Markup(name, _), _) => !clean_elements(name)
   196       case _ => true
   197     } map {
   198       case XML.Wrapped_Elem(markup, body, ts) => XML.Wrapped_Elem(markup, body, clean_message(ts))
   199       case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts))
   200       case t => t
   201     }
   202 
   203   def message_reports(props: Properties.T, body: XML.Body): List[XML.Elem] =
   204     body flatMap {
   205       case XML.Wrapped_Elem(Markup(Markup.REPORT, ps), body, ts) =>
   206         List(XML.Wrapped_Elem(Markup(Markup.REPORT, props ::: ps), body, ts))
   207       case XML.Elem(Markup(Markup.REPORT, ps), ts) =>
   208         List(XML.Elem(Markup(Markup.REPORT, props ::: ps), ts))
   209       case XML.Wrapped_Elem(_, _, ts) => message_reports(props, ts)
   210       case XML.Elem(_, ts) => message_reports(props, ts)
   211       case XML.Text(_) => Nil
   212     }
   213 
   214 
   215   /* specific messages */
   216 
   217   def is_result(msg: XML.Tree): Boolean =
   218     msg match {
   219       case XML.Elem(Markup(Markup.RESULT, _), _) => true
   220       case _ => false
   221     }
   222 
   223   def is_tracing(msg: XML.Tree): Boolean =
   224     msg match {
   225       case XML.Elem(Markup(Markup.TRACING, _), _) => true
   226       case XML.Elem(Markup(Markup.TRACING_MESSAGE, _), _) => true
   227       case _ => false
   228     }
   229 
   230   def is_writeln_markup(msg: XML.Tree, name: String): Boolean =
   231     msg match {
   232       case XML.Elem(Markup(Markup.WRITELN, _),
   233         List(XML.Elem(markup, _))) => markup.name == name
   234       case XML.Elem(Markup(Markup.WRITELN_MESSAGE, _),
   235         List(XML.Elem(markup, _))) => markup.name == name
   236       case _ => false
   237     }
   238 
   239   def is_warning_markup(msg: XML.Tree, name: String): Boolean =
   240     msg match {
   241       case XML.Elem(Markup(Markup.WARNING, _),
   242         List(XML.Elem(markup, _))) => markup.name == name
   243       case XML.Elem(Markup(Markup.WARNING_MESSAGE, _),
   244         List(XML.Elem(markup, _))) => markup.name == name
   245       case _ => false
   246     }
   247 
   248   def is_warning(msg: XML.Tree): Boolean =
   249     msg match {
   250       case XML.Elem(Markup(Markup.WARNING, _), _) => true
   251       case XML.Elem(Markup(Markup.WARNING_MESSAGE, _), _) => true
   252       case _ => false
   253     }
   254 
   255   def is_error(msg: XML.Tree): Boolean =
   256     msg match {
   257       case XML.Elem(Markup(Markup.ERROR, _), _) => true
   258       case XML.Elem(Markup(Markup.ERROR_MESSAGE, _), _) => true
   259       case _ => false
   260     }
   261 
   262   def is_state(msg: XML.Tree): Boolean = is_writeln_markup(msg, Markup.STATE)
   263   def is_information(msg: XML.Tree): Boolean = is_writeln_markup(msg, Markup.INFORMATION)
   264   def is_legacy(msg: XML.Tree): Boolean = is_warning_markup(msg, Markup.LEGACY)
   265 
   266   def is_inlined(msg: XML.Tree): Boolean =
   267     !(is_result(msg) || is_tracing(msg) || is_state(msg))
   268 
   269 
   270   /* dialogs */
   271 
   272   object Dialog_Args
   273   {
   274     def unapply(props: Properties.T): Option[(Document_ID.Generic, Long, String)] =
   275       (props, props, props) match {
   276         case (Position.Id(id), Markup.Serial(serial), Markup.Result(result)) =>
   277           Some((id, serial, result))
   278         case _ => None
   279       }
   280   }
   281 
   282   object Dialog
   283   {
   284     def unapply(tree: XML.Tree): Option[(Document_ID.Generic, Long, String)] =
   285       tree match {
   286         case XML.Elem(Markup(Markup.DIALOG, Dialog_Args(id, serial, result)), _) =>
   287           Some((id, serial, result))
   288         case _ => None
   289       }
   290   }
   291 
   292   object Dialog_Result
   293   {
   294     def apply(id: Document_ID.Generic, serial: Long, result: String): XML.Elem =
   295     {
   296       val props = Position.Id(id) ::: Markup.Serial(serial)
   297       XML.Elem(Markup(Markup.RESULT, props), List(XML.Text(result)))
   298     }
   299 
   300     def unapply(tree: XML.Tree): Option[String] =
   301       tree match {
   302         case XML.Elem(Markup(Markup.RESULT, _), List(XML.Text(result))) => Some(result)
   303         case _ => None
   304       }
   305   }
   306 
   307 
   308   /* reported positions */
   309 
   310   private val position_elements =
   311     Markup.Elements(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
   312 
   313   def message_positions(
   314     self_id: Document_ID.Generic => Boolean,
   315     command_position: Position.T,
   316     chunk_name: Symbol.Text_Chunk.Name,
   317     chunk: Symbol.Text_Chunk,
   318     message: XML.Elem): Set[Text.Range] =
   319   {
   320     def elem_positions(props: Properties.T, set: Set[Text.Range]): Set[Text.Range] =
   321       props match {
   322         case Position.Identified(id, name) if self_id(id) && name == chunk_name =>
   323           Position.Range.unapply(props) orElse Position.Range.unapply(command_position) match {
   324             case Some(symbol_range) =>
   325               chunk.incorporate(symbol_range) match {
   326                 case Some(range) => set + range
   327                 case _ => set
   328               }
   329             case None => set
   330           }
   331         case _ => set
   332       }
   333 
   334     def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
   335       tree match {
   336         case XML.Wrapped_Elem(Markup(name, props), _, body) =>
   337           body.foldLeft(if (position_elements(name)) elem_positions(props, set) else set)(positions)
   338         case XML.Elem(Markup(name, props), body) =>
   339           body.foldLeft(if (position_elements(name)) elem_positions(props, set) else set)(positions)
   340         case XML.Text(_) => set
   341       }
   342 
   343     val set = positions(Set.empty, message)
   344     if (set.isEmpty) elem_positions(message.markup.properties, set)
   345     else set
   346   }
   347 }
   348 
   349 
   350 trait Protocol
   351 {
   352   /* text */
   353 
   354   def encode(s: String): String
   355   def decode(s: String): String
   356 
   357   object Encode
   358   {
   359     val string: XML.Encode.T[String] = (s => XML.Encode.string(encode(s)))
   360   }
   361 
   362 
   363   /* protocol commands */
   364 
   365   def protocol_command_bytes(name: String, args: Bytes*): Unit
   366   def protocol_command(name: String, args: String*): Unit
   367 
   368 
   369   /* options */
   370 
   371   def options(opts: Options): Unit =
   372     protocol_command("Prover.options", YXML.string_of_body(opts.encode))
   373 
   374 
   375   /* interned items */
   376 
   377   def define_blob(digest: SHA1.Digest, bytes: Bytes): Unit =
   378     protocol_command_bytes("Document.define_blob", Bytes(digest.toString), bytes)
   379 
   380   def define_command(command: Command): Unit =
   381   {
   382     val blobs_yxml =
   383     { import XML.Encode._
   384       val encode_blob: T[Command.Blob] =
   385         variant(List(
   386           { case Exn.Res((a, b)) =>
   387               (Nil, pair(string, option(string))((a.node, b.map(p => p._1.toString)))) },
   388           { case Exn.Exn(e) => (Nil, string(Exn.message(e))) }))
   389       YXML.string_of_body(list(encode_blob)(command.blobs))
   390     }
   391     protocol_command("Document.define_command",
   392       Document_ID(command.id), encode(command.name), blobs_yxml, encode(command.source))
   393   }
   394 
   395 
   396   /* execution */
   397 
   398   def discontinue_execution(): Unit =
   399     protocol_command("Document.discontinue_execution")
   400 
   401   def cancel_exec(id: Document_ID.Exec): Unit =
   402     protocol_command("Document.cancel_exec", Document_ID(id))
   403 
   404 
   405   /* document versions */
   406 
   407   def update(old_id: Document_ID.Version, new_id: Document_ID.Version,
   408     edits: List[Document.Edit_Command])
   409   {
   410     val edits_yxml =
   411     { import XML.Encode._
   412       def id: T[Command] = (cmd => long(cmd.id))
   413       def encode_edit(name: Document.Node.Name)
   414           : T[Document.Node.Edit[Command.Edit, Command.Perspective]] =
   415         variant(List(
   416           { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
   417           { case Document.Node.Deps(header) =>
   418               val master_dir = Isabelle_System.posix_path_url(name.master_dir)
   419               val theory = Long_Name.base_name(name.theory)
   420               val imports = header.imports.map(_.node)
   421               val keywords = header.keywords.map({ case (a, b, _) => (a, b) })
   422               (Nil,
   423                 pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
   424                   pair(list(pair(Encode.string,
   425                     option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
   426                   list(Encode.string)))))(
   427                 (master_dir, (theory, (imports, (keywords, header.errors)))))) },
   428           { case Document.Node.Perspective(a, b, c) =>
   429               (bool_atom(a) :: b.commands.map(cmd => long_atom(cmd.id)),
   430                 list(pair(id, pair(Encode.string, list(Encode.string))))(c.dest)) }))
   431       def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
   432       {
   433         val (name, edit) = node_edit
   434         pair(string, encode_edit(name))(name.node, edit)
   435       })
   436       YXML.string_of_body(encode_edits(edits)) }
   437     protocol_command("Document.update", Document_ID(old_id), Document_ID(new_id), edits_yxml)
   438   }
   439 
   440   def remove_versions(versions: List[Document.Version])
   441   {
   442     val versions_yxml =
   443       { import XML.Encode._
   444         YXML.string_of_body(list(long)(versions.map(_.id))) }
   445     protocol_command("Document.remove_versions", versions_yxml)
   446   }
   447 
   448 
   449   /* dialog via document content */
   450 
   451   def dialog_result(serial: Long, result: String): Unit =
   452     protocol_command("Document.dialog_result", Properties.Value.Long(serial), result)
   453 
   454 
   455   /* use_theories */
   456 
   457   def use_theories(id: String, master_dir: Path, thys: List[Path]): Unit =
   458     protocol_command("use_theories", (id :: master_dir.implode :: thys.map(_.implode)): _*)
   459 }