src/Pure/PIDE/protocol.scala
author wenzelm
Sat Mar 14 19:51:36 2015 +0100 (2015-03-14)
changeset 59695 a03e0561bdbf
parent 59685 c043306d2598
child 59706 bf6ca55aae13
permissions -rw-r--r--
clarified positions of theory imports;
     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 | Markup.LEGACY => 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.LEGACY + 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_state(msg: XML.Tree): Boolean =
   231     msg match {
   232       case XML.Elem(Markup(Markup.STATE, _), _) => true
   233       case XML.Elem(Markup(Markup.STATE_MESSAGE, _), _) => true
   234       case _ => false
   235     }
   236 
   237   def is_information(msg: XML.Tree): Boolean =
   238     msg match {
   239       case XML.Elem(Markup(Markup.INFORMATION, _), _) => true
   240       case XML.Elem(Markup(Markup.INFORMATION_MESSAGE, _), _) => true
   241       case _ => false
   242     }
   243 
   244   def is_warning(msg: XML.Tree): Boolean =
   245     msg match {
   246       case XML.Elem(Markup(Markup.WARNING, _), _) => true
   247       case XML.Elem(Markup(Markup.WARNING_MESSAGE, _), _) => true
   248       case _ => false
   249     }
   250 
   251   def is_legacy(msg: XML.Tree): Boolean =
   252     msg match {
   253       case XML.Elem(Markup(Markup.LEGACY, _), _) => true
   254       case XML.Elem(Markup(Markup.LEGACY_MESSAGE, _), _) => true
   255       case _ => false
   256     }
   257 
   258   def is_error(msg: XML.Tree): Boolean =
   259     msg match {
   260       case XML.Elem(Markup(Markup.ERROR, _), _) => true
   261       case XML.Elem(Markup(Markup.ERROR_MESSAGE, _), _) => true
   262       case _ => false
   263     }
   264 
   265   def is_inlined(msg: XML.Tree): Boolean =
   266     !(is_result(msg) || is_tracing(msg) || is_state(msg))
   267 
   268 
   269   /* dialogs */
   270 
   271   object Dialog_Args
   272   {
   273     def unapply(props: Properties.T): Option[(Document_ID.Generic, Long, String)] =
   274       (props, props, props) match {
   275         case (Position.Id(id), Markup.Serial(serial), Markup.Result(result)) =>
   276           Some((id, serial, result))
   277         case _ => None
   278       }
   279   }
   280 
   281   object Dialog
   282   {
   283     def unapply(tree: XML.Tree): Option[(Document_ID.Generic, Long, String)] =
   284       tree match {
   285         case XML.Elem(Markup(Markup.DIALOG, Dialog_Args(id, serial, result)), _) =>
   286           Some((id, serial, result))
   287         case _ => None
   288       }
   289   }
   290 
   291   object Dialog_Result
   292   {
   293     def apply(id: Document_ID.Generic, serial: Long, result: String): XML.Elem =
   294     {
   295       val props = Position.Id(id) ::: Markup.Serial(serial)
   296       XML.Elem(Markup(Markup.RESULT, props), List(XML.Text(result)))
   297     }
   298 
   299     def unapply(tree: XML.Tree): Option[String] =
   300       tree match {
   301         case XML.Elem(Markup(Markup.RESULT, _), List(XML.Text(result))) => Some(result)
   302         case _ => None
   303       }
   304   }
   305 
   306 
   307   /* reported positions */
   308 
   309   private val position_elements =
   310     Markup.Elements(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
   311 
   312   def message_positions(
   313     self_id: Document_ID.Generic => Boolean,
   314     command_position: Position.T,
   315     chunk_name: Symbol.Text_Chunk.Name,
   316     chunk: Symbol.Text_Chunk,
   317     message: XML.Elem): Set[Text.Range] =
   318   {
   319     def elem_positions(props: Properties.T, set: Set[Text.Range]): Set[Text.Range] =
   320       props match {
   321         case Position.Identified(id, name) if self_id(id) && name == chunk_name =>
   322           val opt_range =
   323             Position.Range.unapply(props) orElse {
   324               if (name == Symbol.Text_Chunk.Default)
   325                 Position.Range.unapply(command_position)
   326               else None
   327             }
   328           opt_range match {
   329             case Some(symbol_range) =>
   330               chunk.incorporate(symbol_range) match {
   331                 case Some(range) => set + range
   332                 case _ => set
   333               }
   334             case None => set
   335           }
   336         case _ => set
   337       }
   338 
   339     def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
   340       tree match {
   341         case XML.Wrapped_Elem(Markup(name, props), _, body) =>
   342           body.foldLeft(if (position_elements(name)) elem_positions(props, set) else set)(positions)
   343         case XML.Elem(Markup(name, props), body) =>
   344           body.foldLeft(if (position_elements(name)) elem_positions(props, set) else set)(positions)
   345         case XML.Text(_) => set
   346       }
   347 
   348     val set = positions(Set.empty, message)
   349     if (set.isEmpty) elem_positions(message.markup.properties, set)
   350     else set
   351   }
   352 }
   353 
   354 
   355 trait Protocol
   356 {
   357   /* text */
   358 
   359   def encode(s: String): String
   360   def decode(s: String): String
   361 
   362   object Encode
   363   {
   364     val string: XML.Encode.T[String] = (s => XML.Encode.string(encode(s)))
   365   }
   366 
   367 
   368   /* protocol commands */
   369 
   370   def protocol_command_bytes(name: String, args: Bytes*): Unit
   371   def protocol_command(name: String, args: String*): Unit
   372 
   373 
   374   /* options */
   375 
   376   def options(opts: Options): Unit =
   377     protocol_command("Prover.options", YXML.string_of_body(opts.encode))
   378 
   379 
   380   /* interned items */
   381 
   382   def define_blob(digest: SHA1.Digest, bytes: Bytes): Unit =
   383     protocol_command_bytes("Document.define_blob", Bytes(digest.toString), bytes)
   384 
   385   def define_command(command: Command)
   386   {
   387     val blobs_yxml =
   388     { import XML.Encode._
   389       val encode_blob: T[Command.Blob] =
   390         variant(List(
   391           { case Exn.Res((a, b)) =>
   392               (Nil, pair(string, option(string))((a.node, b.map(p => p._1.toString)))) },
   393           { case Exn.Exn(e) => (Nil, string(Exn.message(e))) }))
   394 
   395       YXML.string_of_body(pair(list(encode_blob), int)(command.blobs, command.blobs_index))
   396     }
   397 
   398     val toks = command.span.content
   399     val toks_yxml =
   400     { import XML.Encode._
   401       val encode_tok: T[Token] =
   402         (tok => pair(int, int)((tok.kind.id, Symbol.iterator(tok.source).length)))
   403       YXML.string_of_body(list(encode_tok)(toks))
   404     }
   405 
   406     protocol_command("Document.define_command",
   407       (Document_ID(command.id) :: encode(command.name) :: blobs_yxml :: toks_yxml ::
   408         toks.map(tok => encode(tok.source))): _*)
   409   }
   410 
   411 
   412   /* execution */
   413 
   414   def discontinue_execution(): Unit =
   415     protocol_command("Document.discontinue_execution")
   416 
   417   def cancel_exec(id: Document_ID.Exec): Unit =
   418     protocol_command("Document.cancel_exec", Document_ID(id))
   419 
   420 
   421   /* document versions */
   422 
   423   def update(old_id: Document_ID.Version, new_id: Document_ID.Version,
   424     edits: List[Document.Edit_Command])
   425   {
   426     val edits_yxml =
   427     { import XML.Encode._
   428       def id: T[Command] = (cmd => long(cmd.id))
   429       def encode_edit(name: Document.Node.Name)
   430           : T[Document.Node.Edit[Command.Edit, Command.Perspective]] =
   431         variant(List(
   432           { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
   433           { case Document.Node.Deps(header) =>
   434               val master_dir = Isabelle_System.posix_path_url(name.master_dir)
   435               val theory = Long_Name.base_name(name.theory)
   436               val imports = header.imports.map({ case (a, _) => a.node })
   437               val keywords = header.keywords.map({ case (a, b, _) => (a, b) })
   438               (Nil,
   439                 pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
   440                   pair(list(pair(Encode.string,
   441                     option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
   442                   list(Encode.string)))))(
   443                 (master_dir, (theory, (imports, (keywords, header.errors)))))) },
   444           { case Document.Node.Perspective(a, b, c) =>
   445               (bool_atom(a) :: b.commands.map(cmd => long_atom(cmd.id)),
   446                 list(pair(id, pair(Encode.string, list(Encode.string))))(c.dest)) }))
   447       def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
   448       {
   449         val (name, edit) = node_edit
   450         pair(string, encode_edit(name))(name.node, edit)
   451       })
   452       YXML.string_of_body(encode_edits(edits)) }
   453     protocol_command("Document.update", Document_ID(old_id), Document_ID(new_id), edits_yxml)
   454   }
   455 
   456   def remove_versions(versions: List[Document.Version])
   457   {
   458     val versions_yxml =
   459     { import XML.Encode._
   460       YXML.string_of_body(list(long)(versions.map(_.id))) }
   461     protocol_command("Document.remove_versions", versions_yxml)
   462   }
   463 
   464 
   465   /* dialog via document content */
   466 
   467   def dialog_result(serial: Long, result: String): Unit =
   468     protocol_command("Document.dialog_result", Properties.Value.Long(serial), result)
   469 
   470 
   471   /* build_theories */
   472 
   473   def build_theories(id: String, master_dir: Path, theories: List[(Options, List[Path])])
   474   {
   475     val theories_yxml =
   476     { import XML.Encode._
   477       YXML.string_of_body(list(pair(Options.encode, list(Path.encode)))(theories)) }
   478     protocol_command("build_theories", id, master_dir.implode, theories_yxml)
   479   }
   480 }