src/Pure/PIDE/protocol.scala
author wenzelm
Tue Mar 17 15:21:41 2015 +0100 (2015-03-17)
changeset 59735 24bee1b11fce
parent 59714 ae322325adbb
child 60879 3dc649cfd512
permissions -rw-r--r--
misc tuning and simplification;
wenzelm@45709
     1
/*  Title:      Pure/PIDE/protocol.scala
wenzelm@38412
     2
    Author:     Makarius
wenzelm@38412
     3
wenzelm@45709
     4
Protocol message formats for interactive proof documents.
wenzelm@38412
     5
*/
wenzelm@38412
     6
wenzelm@38412
     7
package isabelle
wenzelm@38412
     8
wenzelm@38412
     9
wenzelm@45709
    10
object Protocol
wenzelm@38412
    11
{
wenzelm@38567
    12
  /* document editing */
wenzelm@38412
    13
wenzelm@52563
    14
  object Assign_Update
wenzelm@44476
    15
  {
wenzelm@52563
    16
    def unapply(text: String): Option[(Document_ID.Version, Document.Assign_Update)] =
wenzelm@44661
    17
      try {
wenzelm@44661
    18
        import XML.Decode._
wenzelm@44661
    19
        val body = YXML.parse_body(text)
wenzelm@52527
    20
        Some(pair(long, list(pair(long, list(long))))(body))
wenzelm@44661
    21
      }
wenzelm@44661
    22
      catch {
wenzelm@44661
    23
        case ERROR(_) => None
wenzelm@51987
    24
        case _: XML.Error => None
wenzelm@38412
    25
      }
wenzelm@38412
    26
  }
wenzelm@38567
    27
wenzelm@44676
    28
  object Removed
wenzelm@44676
    29
  {
wenzelm@52530
    30
    def unapply(text: String): Option[List[Document_ID.Version]] =
wenzelm@44676
    31
      try {
wenzelm@44676
    32
        import XML.Decode._
wenzelm@44676
    33
        Some(list(long)(YXML.parse_body(text)))
wenzelm@44676
    34
      }
wenzelm@44676
    35
      catch {
wenzelm@44676
    36
        case ERROR(_) => None
wenzelm@51987
    37
        case _: XML.Error => None
wenzelm@44676
    38
      }
wenzelm@44676
    39
  }
wenzelm@44676
    40
wenzelm@38567
    41
wenzelm@46209
    42
  /* command status */
wenzelm@38567
    43
wenzelm@56359
    44
  object Status
wenzelm@56359
    45
  {
wenzelm@56359
    46
    def make(markup_iterator: Iterator[Markup]): Status =
wenzelm@56359
    47
    {
wenzelm@56359
    48
      var touched = false
wenzelm@56359
    49
      var accepted = false
wenzelm@56395
    50
      var warned = false
wenzelm@56359
    51
      var failed = false
wenzelm@56359
    52
      var forks = 0
wenzelm@56359
    53
      var runs = 0
wenzelm@56359
    54
      for (markup <- markup_iterator) {
wenzelm@56359
    55
        markup.name match {
wenzelm@56359
    56
          case Markup.ACCEPTED => accepted = true
wenzelm@56359
    57
          case Markup.FORKED => touched = true; forks += 1
wenzelm@56359
    58
          case Markup.JOINED => forks -= 1
wenzelm@56359
    59
          case Markup.RUNNING => touched = true; runs += 1
wenzelm@56359
    60
          case Markup.FINISHED => runs -= 1
wenzelm@59203
    61
          case Markup.WARNING | Markup.LEGACY => warned = true
wenzelm@56395
    62
          case Markup.FAILED | Markup.ERROR => failed = true
wenzelm@56359
    63
          case _ =>
wenzelm@56359
    64
        }
wenzelm@56359
    65
      }
wenzelm@56395
    66
      Status(touched, accepted, warned, failed, forks, runs)
wenzelm@56359
    67
    }
wenzelm@56359
    68
wenzelm@56359
    69
    val empty = make(Iterator.empty)
wenzelm@56359
    70
wenzelm@56359
    71
    def merge(status_iterator: Iterator[Status]): Status =
wenzelm@56359
    72
      if (status_iterator.hasNext) {
wenzelm@56359
    73
        val status0 = status_iterator.next
wenzelm@56359
    74
        (status0 /: status_iterator)(_ + _)
wenzelm@56359
    75
      }
wenzelm@56359
    76
      else empty
wenzelm@56359
    77
  }
wenzelm@56359
    78
wenzelm@46166
    79
  sealed case class Status(
wenzelm@56352
    80
    private val touched: Boolean,
wenzelm@56352
    81
    private val accepted: Boolean,
wenzelm@56395
    82
    private val warned: Boolean,
wenzelm@56352
    83
    private val failed: Boolean,
wenzelm@56352
    84
    forks: Int,
wenzelm@56352
    85
    runs: Int)
wenzelm@38567
    86
  {
wenzelm@56359
    87
    def + (that: Status): Status =
wenzelm@56395
    88
      Status(
wenzelm@56395
    89
        touched || that.touched,
wenzelm@56395
    90
        accepted || that.accepted,
wenzelm@56395
    91
        warned || that.warned,
wenzelm@56395
    92
        failed || that.failed,
wenzelm@56395
    93
        forks + that.forks,
wenzelm@56395
    94
        runs + that.runs)
wenzelm@56359
    95
wenzelm@49036
    96
    def is_unprocessed: Boolean = accepted && !failed && (!touched || (forks != 0 && runs == 0))
wenzelm@49036
    97
    def is_running: Boolean = runs != 0
wenzelm@56474
    98
    def is_warned: Boolean = warned
wenzelm@56474
    99
    def is_failed: Boolean = failed
wenzelm@49039
   100
    def is_finished: Boolean = !failed && touched && forks == 0 && runs == 0
wenzelm@46166
   101
  }
wenzelm@46166
   102
wenzelm@56395
   103
  val proper_status_elements =
wenzelm@56743
   104
    Markup.Elements(Markup.ACCEPTED, Markup.FORKED, Markup.JOINED, Markup.RUNNING,
wenzelm@55646
   105
      Markup.FINISHED, Markup.FAILED)
wenzelm@55646
   106
wenzelm@56395
   107
  val liberal_status_elements =
wenzelm@59203
   108
    proper_status_elements + Markup.WARNING + Markup.LEGACY + Markup.ERROR
wenzelm@55646
   109
wenzelm@46209
   110
wenzelm@51818
   111
  /* command timing */
wenzelm@51818
   112
wenzelm@51818
   113
  object Command_Timing
wenzelm@51818
   114
  {
wenzelm@52531
   115
    def unapply(props: Properties.T): Option[(Document_ID.Generic, isabelle.Timing)] =
wenzelm@51818
   116
      props match {
wenzelm@51818
   117
        case (Markup.FUNCTION, Markup.COMMAND_TIMING) :: args =>
wenzelm@51818
   118
          (args, args) match {
wenzelm@51818
   119
            case (Position.Id(id), Markup.Timing_Properties(timing)) => Some((id, timing))
wenzelm@51818
   120
            case _ => None
wenzelm@51818
   121
          }
wenzelm@51818
   122
        case _ => None
wenzelm@51818
   123
      }
wenzelm@51818
   124
  }
wenzelm@51818
   125
wenzelm@51818
   126
wenzelm@46209
   127
  /* node status */
wenzelm@46209
   128
wenzelm@46688
   129
  sealed case class Node_Status(
wenzelm@56474
   130
    unprocessed: Int, running: Int, warned: Int, failed: Int, finished: Int)
wenzelm@44866
   131
  {
wenzelm@56474
   132
    def total: Int = unprocessed + running + warned + failed + finished
wenzelm@44866
   133
  }
wenzelm@44613
   134
wenzelm@44613
   135
  def node_status(
wenzelm@44613
   136
    state: Document.State, version: Document.Version, node: Document.Node): Node_Status =
wenzelm@44613
   137
  {
wenzelm@44613
   138
    var unprocessed = 0
wenzelm@44613
   139
    var running = 0
wenzelm@46688
   140
    var warned = 0
wenzelm@44613
   141
    var failed = 0
wenzelm@56474
   142
    var finished = 0
wenzelm@56356
   143
    for (command <- node.commands.iterator) {
wenzelm@56355
   144
      val states = state.command_states(version, command)
wenzelm@56359
   145
      val status = Status.merge(states.iterator.map(_.protocol_status))
wenzelm@56355
   146
wenzelm@56299
   147
      if (status.is_running) running += 1
wenzelm@57843
   148
      else if (status.is_failed) failed += 1
wenzelm@56395
   149
      else if (status.is_warned) warned += 1
wenzelm@56395
   150
      else if (status.is_finished) finished += 1
wenzelm@56299
   151
      else unprocessed += 1
wenzelm@56299
   152
    }
wenzelm@56474
   153
    Node_Status(unprocessed, running, warned, failed, finished)
wenzelm@44613
   154
  }
wenzelm@44613
   155
wenzelm@38887
   156
wenzelm@51533
   157
  /* node timing */
wenzelm@51533
   158
wenzelm@51533
   159
  sealed case class Node_Timing(total: Double, commands: Map[Command, Double])
wenzelm@51533
   160
wenzelm@51533
   161
  val empty_node_timing = Node_Timing(0.0, Map.empty)
wenzelm@51533
   162
wenzelm@51533
   163
  def node_timing(
wenzelm@51533
   164
    state: Document.State,
wenzelm@51533
   165
    version: Document.Version,
wenzelm@51533
   166
    node: Document.Node,
wenzelm@51533
   167
    threshold: Double): Node_Timing =
wenzelm@51533
   168
  {
wenzelm@51533
   169
    var total = 0.0
wenzelm@51533
   170
    var commands = Map.empty[Command, Double]
wenzelm@51533
   171
    for {
wenzelm@51533
   172
      command <- node.commands.iterator
wenzelm@56299
   173
      st <- state.command_states(version, command)
wenzelm@56356
   174
    } {
wenzelm@56356
   175
      val command_timing =
wenzelm@51533
   176
        (0.0 /: st.status)({
wenzelm@51533
   177
          case (timing, Markup.Timing(t)) => timing + t.elapsed.seconds
wenzelm@51533
   178
          case (timing, _) => timing
wenzelm@51533
   179
        })
wenzelm@51533
   180
      total += command_timing
wenzelm@51533
   181
      if (command_timing >= threshold) commands += (command -> command_timing)
wenzelm@51533
   182
    }
wenzelm@51533
   183
    Node_Timing(total, commands)
wenzelm@51533
   184
  }
wenzelm@51533
   185
wenzelm@51533
   186
wenzelm@39441
   187
  /* result messages */
wenzelm@39439
   188
wenzelm@50500
   189
  def is_result(msg: XML.Tree): Boolean =
wenzelm@50500
   190
    msg match {
wenzelm@50500
   191
      case XML.Elem(Markup(Markup.RESULT, _), _) => true
wenzelm@50500
   192
      case _ => false
wenzelm@50500
   193
    }
wenzelm@50500
   194
wenzelm@50157
   195
  def is_tracing(msg: XML.Tree): Boolean =
wenzelm@39622
   196
    msg match {
wenzelm@50201
   197
      case XML.Elem(Markup(Markup.TRACING, _), _) => true
wenzelm@50201
   198
      case XML.Elem(Markup(Markup.TRACING_MESSAGE, _), _) => true
wenzelm@39622
   199
      case _ => false
wenzelm@39622
   200
    }
wenzelm@39622
   201
wenzelm@59184
   202
  def is_state(msg: XML.Tree): Boolean =
wenzelm@50500
   203
    msg match {
wenzelm@59184
   204
      case XML.Elem(Markup(Markup.STATE, _), _) => true
wenzelm@59184
   205
      case XML.Elem(Markup(Markup.STATE_MESSAGE, _), _) => true
wenzelm@59184
   206
      case _ => false
wenzelm@59184
   207
    }
wenzelm@59184
   208
wenzelm@59184
   209
  def is_information(msg: XML.Tree): Boolean =
wenzelm@59184
   210
    msg match {
wenzelm@59184
   211
      case XML.Elem(Markup(Markup.INFORMATION, _), _) => true
wenzelm@59184
   212
      case XML.Elem(Markup(Markup.INFORMATION_MESSAGE, _), _) => true
wenzelm@50500
   213
      case _ => false
wenzelm@50500
   214
    }
wenzelm@50500
   215
wenzelm@39511
   216
  def is_warning(msg: XML.Tree): Boolean =
wenzelm@39511
   217
    msg match {
wenzelm@50201
   218
      case XML.Elem(Markup(Markup.WARNING, _), _) => true
wenzelm@50201
   219
      case XML.Elem(Markup(Markup.WARNING_MESSAGE, _), _) => true
wenzelm@39511
   220
      case _ => false
wenzelm@39511
   221
    }
wenzelm@39511
   222
wenzelm@59203
   223
  def is_legacy(msg: XML.Tree): Boolean =
wenzelm@59203
   224
    msg match {
wenzelm@59203
   225
      case XML.Elem(Markup(Markup.LEGACY, _), _) => true
wenzelm@59203
   226
      case XML.Elem(Markup(Markup.LEGACY_MESSAGE, _), _) => true
wenzelm@59203
   227
      case _ => false
wenzelm@59203
   228
    }
wenzelm@59203
   229
wenzelm@39511
   230
  def is_error(msg: XML.Tree): Boolean =
wenzelm@39511
   231
    msg match {
wenzelm@50201
   232
      case XML.Elem(Markup(Markup.ERROR, _), _) => true
wenzelm@50201
   233
      case XML.Elem(Markup(Markup.ERROR_MESSAGE, _), _) => true
wenzelm@39511
   234
      case _ => false
wenzelm@39511
   235
    }
wenzelm@38887
   236
wenzelm@56495
   237
  def is_inlined(msg: XML.Tree): Boolean =
wenzelm@56495
   238
    !(is_result(msg) || is_tracing(msg) || is_state(msg))
wenzelm@56495
   239
wenzelm@50500
   240
wenzelm@50500
   241
  /* dialogs */
wenzelm@50500
   242
wenzelm@50500
   243
  object Dialog_Args
wenzelm@50500
   244
  {
wenzelm@52531
   245
    def unapply(props: Properties.T): Option[(Document_ID.Generic, Long, String)] =
wenzelm@50500
   246
      (props, props, props) match {
wenzelm@50500
   247
        case (Position.Id(id), Markup.Serial(serial), Markup.Result(result)) =>
wenzelm@50500
   248
          Some((id, serial, result))
wenzelm@50500
   249
        case _ => None
wenzelm@50500
   250
      }
wenzelm@50500
   251
  }
wenzelm@50500
   252
wenzelm@50500
   253
  object Dialog
wenzelm@50500
   254
  {
wenzelm@52531
   255
    def unapply(tree: XML.Tree): Option[(Document_ID.Generic, Long, String)] =
wenzelm@50500
   256
      tree match {
wenzelm@50500
   257
        case XML.Elem(Markup(Markup.DIALOG, Dialog_Args(id, serial, result)), _) =>
wenzelm@50500
   258
          Some((id, serial, result))
wenzelm@50500
   259
        case _ => None
wenzelm@50500
   260
      }
wenzelm@50500
   261
  }
wenzelm@50500
   262
wenzelm@50500
   263
  object Dialog_Result
wenzelm@50500
   264
  {
wenzelm@52531
   265
    def apply(id: Document_ID.Generic, serial: Long, result: String): XML.Elem =
wenzelm@50501
   266
    {
wenzelm@50501
   267
      val props = Position.Id(id) ::: Markup.Serial(serial)
wenzelm@50501
   268
      XML.Elem(Markup(Markup.RESULT, props), List(XML.Text(result)))
wenzelm@50501
   269
    }
wenzelm@50500
   270
wenzelm@50501
   271
    def unapply(tree: XML.Tree): Option[String] =
wenzelm@50500
   272
      tree match {
wenzelm@50501
   273
        case XML.Elem(Markup(Markup.RESULT, _), List(XML.Text(result))) => Some(result)
wenzelm@50500
   274
        case _ => None
wenzelm@50500
   275
      }
wenzelm@50500
   276
  }
wenzelm@38412
   277
}
wenzelm@38412
   278
wenzelm@38412
   279
wenzelm@57916
   280
trait Protocol
wenzelm@38412
   281
{
wenzelm@57916
   282
  /* text */
wenzelm@57916
   283
wenzelm@57916
   284
  def encode(s: String): String
wenzelm@57916
   285
  def decode(s: String): String
wenzelm@57916
   286
wenzelm@57916
   287
  object Encode
wenzelm@57916
   288
  {
wenzelm@57916
   289
    val string: XML.Encode.T[String] = (s => XML.Encode.string(encode(s)))
wenzelm@57916
   290
  }
wenzelm@57916
   291
wenzelm@57916
   292
wenzelm@57916
   293
  /* protocol commands */
wenzelm@57916
   294
wenzelm@57916
   295
  def protocol_command_bytes(name: String, args: Bytes*): Unit
wenzelm@57916
   296
  def protocol_command(name: String, args: String*): Unit
wenzelm@57916
   297
wenzelm@57916
   298
wenzelm@56387
   299
  /* options */
wenzelm@56387
   300
wenzelm@56387
   301
  def options(opts: Options): Unit =
wenzelm@56387
   302
    protocol_command("Prover.options", YXML.string_of_body(opts.encode))
wenzelm@56387
   303
wenzelm@56387
   304
wenzelm@56387
   305
  /* interned items */
wenzelm@54519
   306
wenzelm@56335
   307
  def define_blob(digest: SHA1.Digest, bytes: Bytes): Unit =
wenzelm@56387
   308
    protocol_command_bytes("Document.define_blob", Bytes(digest.toString), bytes)
wenzelm@38412
   309
wenzelm@59671
   310
  def define_command(command: Command)
wenzelm@54519
   311
  {
wenzelm@54519
   312
    val blobs_yxml =
wenzelm@54519
   313
    { import XML.Encode._
wenzelm@54519
   314
      val encode_blob: T[Command.Blob] =
wenzelm@54519
   315
        variant(List(
wenzelm@54526
   316
          { case Exn.Res((a, b)) =>
wenzelm@56458
   317
              (Nil, pair(string, option(string))((a.node, b.map(p => p._1.toString)))) },
wenzelm@59714
   318
          { case Exn.Exn(e) => (Nil, string(Exn.message(e))) }))
wenzelm@59085
   319
wenzelm@59685
   320
      YXML.string_of_body(pair(list(encode_blob), int)(command.blobs, command.blobs_index))
wenzelm@54519
   321
    }
wenzelm@59085
   322
wenzelm@59085
   323
    val toks = command.span.content
wenzelm@59085
   324
    val toks_yxml =
wenzelm@59085
   325
    { import XML.Encode._
wenzelm@59085
   326
      val encode_tok: T[Token] =
wenzelm@59085
   327
        (tok => pair(int, int)((tok.kind.id, Symbol.iterator(tok.source).length)))
wenzelm@59085
   328
      YXML.string_of_body(list(encode_tok)(toks))
wenzelm@59085
   329
    }
wenzelm@59085
   330
wenzelm@52582
   331
    protocol_command("Document.define_command",
wenzelm@59735
   332
      (Document_ID(command.id) :: encode(command.span.name) :: blobs_yxml :: toks_yxml ::
wenzelm@59085
   333
        toks.map(tok => encode(tok.source))): _*)
wenzelm@54519
   334
  }
wenzelm@38412
   335
wenzelm@38412
   336
wenzelm@52931
   337
  /* execution */
wenzelm@52931
   338
wenzelm@52931
   339
  def discontinue_execution(): Unit =
wenzelm@52931
   340
    protocol_command("Document.discontinue_execution")
wenzelm@38412
   341
wenzelm@52931
   342
  def cancel_exec(id: Document_ID.Exec): Unit =
wenzelm@52931
   343
    protocol_command("Document.cancel_exec", Document_ID(id))
wenzelm@52931
   344
wenzelm@52931
   345
wenzelm@52931
   346
  /* document versions */
wenzelm@47343
   347
wenzelm@52530
   348
  def update(old_id: Document_ID.Version, new_id: Document_ID.Version,
wenzelm@44383
   349
    edits: List[Document.Edit_Command])
wenzelm@38412
   350
  {
wenzelm@44157
   351
    val edits_yxml =
wenzelm@43767
   352
    { import XML.Encode._
wenzelm@44383
   353
      def id: T[Command] = (cmd => long(cmd.id))
wenzelm@46737
   354
      def encode_edit(name: Document.Node.Name)
wenzelm@52849
   355
          : T[Document.Node.Edit[Command.Edit, Command.Perspective]] =
wenzelm@44979
   356
        variant(List(
wenzelm@44979
   357
          { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
wenzelm@48707
   358
          { case Document.Node.Deps(header) =>
wenzelm@56449
   359
              val master_dir = Isabelle_System.posix_path_url(name.master_dir)
wenzelm@56801
   360
              val theory = Long_Name.base_name(name.theory)
wenzelm@59695
   361
              val imports = header.imports.map({ case (a, _) => a.node })
wenzelm@50128
   362
              val keywords = header.keywords.map({ case (a, b, _) => (a, b) })
wenzelm@44979
   363
              (Nil,
wenzelm@48707
   364
                pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
wenzelm@48864
   365
                  pair(list(pair(Encode.string,
wenzelm@48864
   366
                    option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
wenzelm@51294
   367
                  list(Encode.string)))))(
wenzelm@56801
   368
                (master_dir, (theory, (imports, (keywords, header.errors)))))) },
wenzelm@52849
   369
          { case Document.Node.Perspective(a, b, c) =>
wenzelm@52849
   370
              (bool_atom(a) :: b.commands.map(cmd => long_atom(cmd.id)),
wenzelm@52862
   371
                list(pair(id, pair(Encode.string, list(Encode.string))))(c.dest)) }))
wenzelm@48705
   372
      def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
wenzelm@44979
   373
      {
wenzelm@44979
   374
        val (name, edit) = node_edit
wenzelm@46737
   375
        pair(string, encode_edit(name))(name.node, edit)
wenzelm@44979
   376
      })
wenzelm@48705
   377
      YXML.string_of_body(encode_edits(edits)) }
wenzelm@52582
   378
    protocol_command("Document.update", Document_ID(old_id), Document_ID(new_id), edits_yxml)
wenzelm@38412
   379
  }
wenzelm@43748
   380
wenzelm@44673
   381
  def remove_versions(versions: List[Document.Version])
wenzelm@44673
   382
  {
wenzelm@44673
   383
    val versions_yxml =
wenzelm@59364
   384
    { import XML.Encode._
wenzelm@59364
   385
      YXML.string_of_body(list(long)(versions.map(_.id))) }
wenzelm@52582
   386
    protocol_command("Document.remove_versions", versions_yxml)
wenzelm@44673
   387
  }
wenzelm@44673
   388
wenzelm@43748
   389
wenzelm@50498
   390
  /* dialog via document content */
wenzelm@50498
   391
wenzelm@52931
   392
  def dialog_result(serial: Long, result: String): Unit =
wenzelm@52582
   393
    protocol_command("Document.dialog_result", Properties.Value.Long(serial), result)
wenzelm@56616
   394
wenzelm@56616
   395
wenzelm@59364
   396
  /* build_theories */
wenzelm@56616
   397
wenzelm@59364
   398
  def build_theories(id: String, master_dir: Path, theories: List[(Options, List[Path])])
wenzelm@59364
   399
  {
wenzelm@59364
   400
    val theories_yxml =
wenzelm@59364
   401
    { import XML.Encode._
wenzelm@59364
   402
      YXML.string_of_body(list(pair(Options.encode, list(Path.encode)))(theories)) }
wenzelm@59364
   403
    protocol_command("build_theories", id, master_dir.implode, theories_yxml)
wenzelm@59364
   404
  }
wenzelm@38412
   405
}