src/Pure/PIDE/protocol.scala
author wenzelm
Wed Apr 30 22:34:11 2014 +0200 (2014-04-30)
changeset 56801 8dd9df88f647
parent 56746 d37a5d09a277
child 57843 d8966c09025c
permissions -rw-r--r--
some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
     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_warned) warned += 1
   149       else if (status.is_failed) failed += 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     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.Reported(id, name, symbol_range)
   322         if self_id(id) && name == chunk_name =>
   323           chunk.incorporate(symbol_range) match {
   324             case Some(range) => set + range
   325             case _ => set
   326           }
   327         case _ => set
   328       }
   329 
   330     def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
   331       tree match {
   332         case XML.Wrapped_Elem(Markup(name, props), _, body) =>
   333           body.foldLeft(if (position_elements(name)) elem_positions(props, set) else set)(positions)
   334         case XML.Elem(Markup(name, props), body) =>
   335           body.foldLeft(if (position_elements(name)) elem_positions(props, set) else set)(positions)
   336         case XML.Text(_) => set
   337       }
   338 
   339     val set = positions(Set.empty, message)
   340     if (set.isEmpty) elem_positions(message.markup.properties, set)
   341     else set
   342   }
   343 }
   344 
   345 
   346 trait Protocol extends Prover
   347 {
   348   /* options */
   349 
   350   def options(opts: Options): Unit =
   351     protocol_command("Prover.options", YXML.string_of_body(opts.encode))
   352 
   353 
   354   /* interned items */
   355 
   356   def define_blob(digest: SHA1.Digest, bytes: Bytes): Unit =
   357     protocol_command_bytes("Document.define_blob", Bytes(digest.toString), bytes)
   358 
   359   def define_command(command: Command): Unit =
   360   {
   361     val blobs_yxml =
   362     { import XML.Encode._
   363       val encode_blob: T[Command.Blob] =
   364         variant(List(
   365           { case Exn.Res((a, b)) =>
   366               (Nil, pair(string, option(string))((a.node, b.map(p => p._1.toString)))) },
   367           { case Exn.Exn(e) => (Nil, string(Exn.message(e))) }))
   368       YXML.string_of_body(list(encode_blob)(command.blobs))
   369     }
   370     protocol_command("Document.define_command",
   371       Document_ID(command.id), encode(command.name), blobs_yxml, encode(command.source))
   372   }
   373 
   374 
   375   /* execution */
   376 
   377   def discontinue_execution(): Unit =
   378     protocol_command("Document.discontinue_execution")
   379 
   380   def cancel_exec(id: Document_ID.Exec): Unit =
   381     protocol_command("Document.cancel_exec", Document_ID(id))
   382 
   383 
   384   /* document versions */
   385 
   386   def update(old_id: Document_ID.Version, new_id: Document_ID.Version,
   387     edits: List[Document.Edit_Command])
   388   {
   389     val edits_yxml =
   390     { import XML.Encode._
   391       def id: T[Command] = (cmd => long(cmd.id))
   392       def encode_edit(name: Document.Node.Name)
   393           : T[Document.Node.Edit[Command.Edit, Command.Perspective]] =
   394         variant(List(
   395           { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
   396           { case Document.Node.Deps(header) =>
   397               val master_dir = Isabelle_System.posix_path_url(name.master_dir)
   398               val theory = Long_Name.base_name(name.theory)
   399               val imports = header.imports.map(_.node)
   400               val keywords = header.keywords.map({ case (a, b, _) => (a, b) })
   401               (Nil,
   402                 pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
   403                   pair(list(pair(Encode.string,
   404                     option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
   405                   list(Encode.string)))))(
   406                 (master_dir, (theory, (imports, (keywords, header.errors)))))) },
   407           { case Document.Node.Perspective(a, b, c) =>
   408               (bool_atom(a) :: b.commands.map(cmd => long_atom(cmd.id)),
   409                 list(pair(id, pair(Encode.string, list(Encode.string))))(c.dest)) }))
   410       def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
   411       {
   412         val (name, edit) = node_edit
   413         pair(string, encode_edit(name))(name.node, edit)
   414       })
   415       YXML.string_of_body(encode_edits(edits)) }
   416     protocol_command("Document.update", Document_ID(old_id), Document_ID(new_id), edits_yxml)
   417   }
   418 
   419   def remove_versions(versions: List[Document.Version])
   420   {
   421     val versions_yxml =
   422       { import XML.Encode._
   423         YXML.string_of_body(list(long)(versions.map(_.id))) }
   424     protocol_command("Document.remove_versions", versions_yxml)
   425   }
   426 
   427 
   428   /* dialog via document content */
   429 
   430   def dialog_result(serial: Long, result: String): Unit =
   431     protocol_command("Document.dialog_result", Properties.Value.Long(serial), result)
   432 
   433 
   434   /* use_theories */
   435 
   436   def use_theories(id: String, master_dir: Path, thys: List[Path]): Unit =
   437     protocol_command("use_theories", (id :: master_dir.implode :: thys.map(_.implode)): _*)
   438 }