src/Pure/PIDE/protocol.scala
author wenzelm
Wed Dec 03 20:45:20 2014 +0100 (2014-12-03)
changeset 59085 08a6901eb035
parent 58015 2777096e0adf
child 59184 830bb7ddb3ab
permissions -rw-r--r--
clarified define_command: send tokens more directly, without requiring keywords in ML;
     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           val opt_range =
   324             Position.Range.unapply(props) orElse {
   325               if (name == Symbol.Text_Chunk.Default)
   326                 Position.Range.unapply(command_position)
   327               else None
   328             }
   329           opt_range match {
   330             case Some(symbol_range) =>
   331               chunk.incorporate(symbol_range) match {
   332                 case Some(range) => set + range
   333                 case _ => set
   334               }
   335             case None => set
   336           }
   337         case _ => set
   338       }
   339 
   340     def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
   341       tree match {
   342         case XML.Wrapped_Elem(Markup(name, props), _, body) =>
   343           body.foldLeft(if (position_elements(name)) elem_positions(props, set) else set)(positions)
   344         case XML.Elem(Markup(name, props), body) =>
   345           body.foldLeft(if (position_elements(name)) elem_positions(props, set) else set)(positions)
   346         case XML.Text(_) => set
   347       }
   348 
   349     val set = positions(Set.empty, message)
   350     if (set.isEmpty) elem_positions(message.markup.properties, set)
   351     else set
   352   }
   353 }
   354 
   355 
   356 trait Protocol
   357 {
   358   /* text */
   359 
   360   def encode(s: String): String
   361   def decode(s: String): String
   362 
   363   object Encode
   364   {
   365     val string: XML.Encode.T[String] = (s => XML.Encode.string(encode(s)))
   366   }
   367 
   368 
   369   /* protocol commands */
   370 
   371   def protocol_command_bytes(name: String, args: Bytes*): Unit
   372   def protocol_command(name: String, args: String*): Unit
   373 
   374 
   375   /* options */
   376 
   377   def options(opts: Options): Unit =
   378     protocol_command("Prover.options", YXML.string_of_body(opts.encode))
   379 
   380 
   381   /* interned items */
   382 
   383   def define_blob(digest: SHA1.Digest, bytes: Bytes): Unit =
   384     protocol_command_bytes("Document.define_blob", Bytes(digest.toString), bytes)
   385 
   386   def define_command(command: Command): Unit =
   387   {
   388     val blobs_yxml =
   389     { import XML.Encode._
   390       val encode_blob: T[Command.Blob] =
   391         variant(List(
   392           { case Exn.Res((a, b)) =>
   393               (Nil, pair(string, option(string))((a.node, b.map(p => p._1.toString)))) },
   394           { case Exn.Exn(e) => (Nil, string(Exn.message(e))) }))
   395 
   396       YXML.string_of_body(list(encode_blob)(command.blobs))
   397     }
   398 
   399     val toks = command.span.content
   400     val toks_yxml =
   401     { import XML.Encode._
   402       val encode_tok: T[Token] =
   403         (tok => pair(int, int)((tok.kind.id, Symbol.iterator(tok.source).length)))
   404       YXML.string_of_body(list(encode_tok)(toks))
   405     }
   406 
   407     protocol_command("Document.define_command",
   408       (Document_ID(command.id) :: encode(command.name) :: blobs_yxml :: toks_yxml ::
   409         toks.map(tok => encode(tok.source))): _*)
   410   }
   411 
   412 
   413   /* execution */
   414 
   415   def discontinue_execution(): Unit =
   416     protocol_command("Document.discontinue_execution")
   417 
   418   def cancel_exec(id: Document_ID.Exec): Unit =
   419     protocol_command("Document.cancel_exec", Document_ID(id))
   420 
   421 
   422   /* document versions */
   423 
   424   def update(old_id: Document_ID.Version, new_id: Document_ID.Version,
   425     edits: List[Document.Edit_Command])
   426   {
   427     val edits_yxml =
   428     { import XML.Encode._
   429       def id: T[Command] = (cmd => long(cmd.id))
   430       def encode_edit(name: Document.Node.Name)
   431           : T[Document.Node.Edit[Command.Edit, Command.Perspective]] =
   432         variant(List(
   433           { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
   434           { case Document.Node.Deps(header) =>
   435               val master_dir = Isabelle_System.posix_path_url(name.master_dir)
   436               val theory = Long_Name.base_name(name.theory)
   437               val imports = header.imports.map(_.node)
   438               val keywords = header.keywords.map({ case (a, b, _) => (a, b) })
   439               (Nil,
   440                 pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
   441                   pair(list(pair(Encode.string,
   442                     option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
   443                   list(Encode.string)))))(
   444                 (master_dir, (theory, (imports, (keywords, header.errors)))))) },
   445           { case Document.Node.Perspective(a, b, c) =>
   446               (bool_atom(a) :: b.commands.map(cmd => long_atom(cmd.id)),
   447                 list(pair(id, pair(Encode.string, list(Encode.string))))(c.dest)) }))
   448       def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
   449       {
   450         val (name, edit) = node_edit
   451         pair(string, encode_edit(name))(name.node, edit)
   452       })
   453       YXML.string_of_body(encode_edits(edits)) }
   454     protocol_command("Document.update", Document_ID(old_id), Document_ID(new_id), edits_yxml)
   455   }
   456 
   457   def remove_versions(versions: List[Document.Version])
   458   {
   459     val versions_yxml =
   460       { import XML.Encode._
   461         YXML.string_of_body(list(long)(versions.map(_.id))) }
   462     protocol_command("Document.remove_versions", versions_yxml)
   463   }
   464 
   465 
   466   /* dialog via document content */
   467 
   468   def dialog_result(serial: Long, result: String): Unit =
   469     protocol_command("Document.dialog_result", Properties.Value.Long(serial), result)
   470 
   471 
   472   /* use_theories */
   473 
   474   def use_theories(id: String, master_dir: Path, thys: List[Path]): Unit =
   475     protocol_command("use_theories", (id :: master_dir.implode :: thys.map(_.implode)): _*)
   476 }