src/Pure/System/session.scala
author wenzelm
Thu, 27 May 2010 12:35:40 +0200
changeset 37132 10ef4da1c314
parent 37129 4c83696b340e
child 37689 628eabe2213a
permissions -rw-r--r--
indicate prospective properties;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36676
ac7961d42ac3 some rearrangement of Scala sources;
wenzelm
parents: 35013
diff changeset
     1
/*  Title:      Pure/System/session.scala
ac7961d42ac3 some rearrangement of Scala sources;
wenzelm
parents: 35013
diff changeset
     2
    Author:     Makarius
ac7961d42ac3 some rearrangement of Scala sources;
wenzelm
parents: 35013
diff changeset
     3
ac7961d42ac3 some rearrangement of Scala sources;
wenzelm
parents: 35013
diff changeset
     4
Isabelle session, potentially with running prover.
ac7961d42ac3 some rearrangement of Scala sources;
wenzelm
parents: 35013
diff changeset
     5
*/
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
     6
34871
e596a0b71f3c incorporate "proofdocument" part into main Isabelle/Pure.jar -- except for html_panel.scala, which depends on external library (Lobo/Cobra browser);
wenzelm
parents: 34859
diff changeset
     7
package isabelle
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
     8
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
     9
34820
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
    10
import scala.actors.TIMEOUT
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
    11
import scala.actors.Actor._
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
    12
34815
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    13
34791
b97d5b38dea4 explicit object Session.Global_Settings;
wenzelm
parents: 34780
diff changeset
    14
object Session
b97d5b38dea4 explicit object Session.Global_Settings;
wenzelm
parents: 34780
diff changeset
    15
{
34813
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34810
diff changeset
    16
  /* events */
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34810
diff changeset
    17
34791
b97d5b38dea4 explicit object Session.Global_Settings;
wenzelm
parents: 34780
diff changeset
    18
  case object Global_Settings
34813
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34810
diff changeset
    19
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34810
diff changeset
    20
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34810
diff changeset
    21
  /* managed entities */
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34810
diff changeset
    22
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34810
diff changeset
    23
  type Entity_ID = String
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34810
diff changeset
    24
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34810
diff changeset
    25
  trait Entity
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34810
diff changeset
    26
  {
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34810
diff changeset
    27
    val id: Entity_ID
37129
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
    28
    def consume(message: XML.Tree, forward: Command => Unit): Unit
34813
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34810
diff changeset
    29
  }
34791
b97d5b38dea4 explicit object Session.Global_Settings;
wenzelm
parents: 34780
diff changeset
    30
}
b97d5b38dea4 explicit object Session.Global_Settings;
wenzelm
parents: 34780
diff changeset
    31
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
    32
34851
304a86164dd2 provide global "Isabelle" within interpreter loop -- using import instead of val avoids pontential conflicts with later import isabelle.jedit._;
wenzelm
parents: 34848
diff changeset
    33
class Session(system: Isabelle_System)
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
    34
{
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    35
  /* pervasive event buses */
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    36
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    37
  val global_settings = new Event_Bus[Session.Global_Settings.type]
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    38
  val raw_results = new Event_Bus[Isabelle_Process.Result]
37065
2a73253b5898 separate event bus and dockable for raw output (stdout);
wenzelm
parents: 37063
diff changeset
    39
  val raw_output = new Event_Bus[Isabelle_Process.Result]
37129
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
    40
  val commands_changed = new Event_Bus[Command_Set]
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    41
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    42
34778
8eccd35e975e removed unused Session.prover_logic;
wenzelm
parents: 34777
diff changeset
    43
  /* unique ids */
8eccd35e975e removed unused Session.prover_logic;
wenzelm
parents: 34777
diff changeset
    44
8eccd35e975e removed unused Session.prover_logic;
wenzelm
parents: 34777
diff changeset
    45
  private var id_count: BigInt = 0
34813
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34810
diff changeset
    46
  def create_id(): Session.Entity_ID = synchronized { id_count += 1; "j" + id_count }
34778
8eccd35e975e removed unused Session.prover_logic;
wenzelm
parents: 34777
diff changeset
    47
8eccd35e975e removed unused Session.prover_logic;
wenzelm
parents: 34777
diff changeset
    48
34826
6b38fc0b3406 eliminated redundant session documents database;
wenzelm
parents: 34825
diff changeset
    49
6b38fc0b3406 eliminated redundant session documents database;
wenzelm
parents: 34825
diff changeset
    50
  /** main actor **/
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    51
34851
304a86164dd2 provide global "Isabelle" within interpreter loop -- using import instead of val avoids pontential conflicts with later import isabelle.jedit._;
wenzelm
parents: 34848
diff changeset
    52
  @volatile private var syntax = new Outer_Syntax(system.symbols)
34819
86cb7f8e5a0d tuned signature;
wenzelm
parents: 34818
diff changeset
    53
  def current_syntax: Outer_Syntax = syntax
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    54
34816
d33312514220 maintain generic session entities -- cover commands, states, etc. (but not yet documents);
wenzelm
parents: 34815
diff changeset
    55
  @volatile private var entities = Map[Session.Entity_ID, Session.Entity]()
d33312514220 maintain generic session entities -- cover commands, states, etc. (but not yet documents);
wenzelm
parents: 34815
diff changeset
    56
  def lookup_entity(id: Session.Entity_ID): Option[Session.Entity] = entities.get(id)
34835
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34833
diff changeset
    57
  def lookup_command(id: Session.Entity_ID): Option[Command] =
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34833
diff changeset
    58
    lookup_entity(id) match {
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34833
diff changeset
    59
      case Some(cmd: Command) => Some(cmd)
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34833
diff changeset
    60
      case _ => None
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34833
diff changeset
    61
    }
34816
d33312514220 maintain generic session entities -- cover commands, states, etc. (but not yet documents);
wenzelm
parents: 34815
diff changeset
    62
34820
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
    63
  private case class Start(timeout: Int, args: List[String])
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
    64
  private case object Stop
34808
e462572536e9 eliminated global Session.document_0 -- did not work due to hardwired id;
wenzelm
parents: 34807
diff changeset
    65
  private case class Begin_Document(path: String)
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
    66
37129
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
    67
  private lazy val session_actor = actor {
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    68
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    69
    var prover: Isabelle_Process with Isar_Document = null
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    70
34816
d33312514220 maintain generic session entities -- cover commands, states, etc. (but not yet documents);
wenzelm
parents: 34815
diff changeset
    71
    def register(entity: Session.Entity) { entities += (entity.id -> entity) }
d33312514220 maintain generic session entities -- cover commands, states, etc. (but not yet documents);
wenzelm
parents: 34815
diff changeset
    72
34835
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34833
diff changeset
    73
    var documents = Map[Isar_Document.Document_ID, Document]()
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34833
diff changeset
    74
    def register_document(doc: Document) { documents += (doc.id -> doc) }
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34833
diff changeset
    75
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    76
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    77
    /* document changes */
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    78
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    79
    def handle_change(change: Change)
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    80
    {
34824
ac35eee85f5c renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents: 34823
diff changeset
    81
      require(change.parent.isDefined)
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    82
34853
32b49207ca20 misc tuning and clarification of Document/Change;
wenzelm
parents: 34851
diff changeset
    83
      val (changes, doc) = change.result.join
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    84
      val id_changes = changes map {
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    85
        case (c1, c2) =>
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    86
          (c1.map(_.id).getOrElse(""),
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    87
           c2 match {
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    88
              case None => None
34835
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34833
diff changeset
    89
              case Some(command) =>
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34833
diff changeset
    90
                if (!lookup_command(command.id).isDefined) {
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34833
diff changeset
    91
                  register(command)
34859
f986d84dd44b renamed Command.content to source;
wenzelm
parents: 34853
diff changeset
    92
                  prover.define_command(command.id, system.symbols.encode(command.source))
34835
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34833
diff changeset
    93
                }
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    94
                Some(command.id)
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    95
            })
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    96
      }
34835
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34833
diff changeset
    97
      register_document(doc)
34833
683ddf358698 recovered explicit Changed.id (copy of future document) -- avoids premature joining of result;
wenzelm
parents: 34831
diff changeset
    98
      prover.edit_document(change.parent.get.id, doc.id, id_changes)
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
    99
    }
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   100
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   101
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   102
    /* prover results */
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   103
34836
b83c7a738eb8 singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents: 34835
diff changeset
   104
    def bad_result(result: Isabelle_Process.Result)
b83c7a738eb8 singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents: 34835
diff changeset
   105
    {
37041
dae419819a80 bad_result: report fully explicit message;
wenzelm
parents: 36947
diff changeset
   106
      System.err.println("Ignoring prover result: " + result.message.toString)
34836
b83c7a738eb8 singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents: 34835
diff changeset
   107
    }
b83c7a738eb8 singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents: 34835
diff changeset
   108
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   109
    def handle_result(result: Isabelle_Process.Result)
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   110
    {
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   111
      raw_results.event(result)
34799
0330a4284a9b just one variable for outer syntax keywords and completion;
wenzelm
parents: 34795
diff changeset
   112
34835
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34833
diff changeset
   113
      val target_id: Option[Session.Entity_ID] = Position.get_id(result.props)
34813
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34810
diff changeset
   114
      val target: Option[Session.Entity] =
34835
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34833
diff changeset
   115
        target_id match {
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   116
          case None => None
34835
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34833
diff changeset
   117
          case Some(id) => lookup_entity(id)
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   118
        }
37129
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   119
      if (target.isDefined) target.get.consume(result.message, indicate_command_change)
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   120
      else if (result.kind == Isabelle_Process.Kind.STATUS) {
34818
7df68a8f0e3e register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents: 34817
diff changeset
   121
        // global status message
34836
b83c7a738eb8 singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents: 34835
diff changeset
   122
        result.body match {
34835
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34833
diff changeset
   123
35013
f3d491658893 fixed spelling;
wenzelm
parents: 34871
diff changeset
   124
          // document state assignment
36682
3f989067f87d extractors for document updates;
wenzelm
parents: 36676
diff changeset
   125
          case List(Isar_Document.Assign(edits)) if target_id.isDefined =>
34836
b83c7a738eb8 singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents: 34835
diff changeset
   126
            documents.get(target_id.get) match {
b83c7a738eb8 singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents: 34835
diff changeset
   127
              case Some(doc) =>
b83c7a738eb8 singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents: 34835
diff changeset
   128
                val states =
b83c7a738eb8 singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents: 34835
diff changeset
   129
                  for {
36682
3f989067f87d extractors for document updates;
wenzelm
parents: 36676
diff changeset
   130
                    Isar_Document.Edit(cmd_id, state_id) <- edits
34836
b83c7a738eb8 singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents: 34835
diff changeset
   131
                    cmd <- lookup_command(cmd_id)
b83c7a738eb8 singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents: 34835
diff changeset
   132
                  } yield {
b83c7a738eb8 singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents: 34835
diff changeset
   133
                    val st = cmd.assign_state(state_id)
b83c7a738eb8 singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents: 34835
diff changeset
   134
                    register(st)
b83c7a738eb8 singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents: 34835
diff changeset
   135
                    (cmd, st)
b83c7a738eb8 singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents: 34835
diff changeset
   136
                  }
b83c7a738eb8 singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents: 34835
diff changeset
   137
                doc.assign_states(states)
b83c7a738eb8 singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents: 34835
diff changeset
   138
              case None => bad_result(result)
b83c7a738eb8 singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents: 34835
diff changeset
   139
            }
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   140
36682
3f989067f87d extractors for document updates;
wenzelm
parents: 36676
diff changeset
   141
          // keyword declarations
36947
285b39022372 renamed Outer_Keyword to Keyword (in Scala);
wenzelm
parents: 36782
diff changeset
   142
          case List(Keyword.Command_Decl(name, kind)) => syntax += (name, kind)
285b39022372 renamed Outer_Keyword to Keyword (in Scala);
wenzelm
parents: 36782
diff changeset
   143
          case List(Keyword.Keyword_Decl(name)) => syntax += name
34836
b83c7a738eb8 singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents: 34835
diff changeset
   144
34839
3457436a1110 more precise notion of bad messages;
wenzelm
parents: 34837
diff changeset
   145
          case _ => if (!result.is_ready) bad_result(result)
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   146
        }
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   147
      }
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   148
      else if (result.kind == Isabelle_Process.Kind.EXIT)
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   149
        prover = null
37065
2a73253b5898 separate event bus and dockable for raw output (stdout);
wenzelm
parents: 37063
diff changeset
   150
      else if (result.is_raw)
2a73253b5898 separate event bus and dockable for raw output (stdout);
wenzelm
parents: 37063
diff changeset
   151
        raw_output.event(result)
37063
492bc98a8809 ignore system messages;
wenzelm
parents: 37041
diff changeset
   152
      else if (!result.is_system)   // FIXME syslog (!?)
34837
aa73039d5d14 result.is_ready is not bad;
wenzelm
parents: 34836
diff changeset
   153
        bad_result(result)
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   154
    }
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   155
34820
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   156
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   157
    /* prover startup */
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   158
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   159
    def startup_error(): String =
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   160
    {
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   161
      val buf = new StringBuilder
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   162
      while (
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   163
        receiveWithin(0) {
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   164
          case result: Isabelle_Process.Result =>
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   165
            if (result.is_raw) {
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   166
              for (text <- XML.content(result.message))
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   167
                buf.append(text)
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   168
            }
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   169
            true
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   170
          case TIMEOUT => false
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   171
        }) {}
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   172
      buf.toString
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   173
    }
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   174
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   175
    def prover_startup(timeout: Int): Option[String] =
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   176
    {
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   177
      receiveWithin(timeout) {
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   178
        case result: Isabelle_Process.Result
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   179
          if result.kind == Isabelle_Process.Kind.INIT =>
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   180
          while (receive {
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   181
            case result: Isabelle_Process.Result =>
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   182
              handle_result(result); !result.is_ready
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   183
            }) {}
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   184
          None
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   185
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   186
        case result: Isabelle_Process.Result
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   187
          if result.kind == Isabelle_Process.Kind.EXIT =>
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   188
          Some(startup_error())
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   189
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   190
        case TIMEOUT =>  // FIXME clarify
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   191
          prover.kill; Some(startup_error())
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   192
      }
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   193
    }
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   194
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   195
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   196
    /* main loop */
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   197
34820
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   198
    val xml_cache = new XML.Cache(131071)
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   199
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   200
    loop {
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   201
      react {
34820
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   202
        case Start(timeout, args) =>
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   203
          if (prover == null) {
34851
304a86164dd2 provide global "Isabelle" within interpreter loop -- using import instead of val avoids pontential conflicts with later import isabelle.jedit._;
wenzelm
parents: 34848
diff changeset
   204
            prover = new Isabelle_Process(system, self, args:_*) with Isar_Document
34820
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   205
            val origin = sender
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   206
            val opt_err = prover_startup(timeout)
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   207
            if (opt_err.isDefined) prover = null
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   208
            origin ! opt_err
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   209
          }
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   210
          else reply(None)
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   211
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   212
        case Stop =>  // FIXME clarify; synchronous
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   213
          if (prover != null) {
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   214
            prover.kill
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   215
            prover = null
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   216
          }
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   217
34820
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   218
        case Begin_Document(path: String) if prover != null =>
34808
e462572536e9 eliminated global Session.document_0 -- did not work due to hardwired id;
wenzelm
parents: 34807
diff changeset
   219
          val id = create_id()
34823
2f3ea37c5958 renamed Proof_Document to Document;
wenzelm
parents: 34820
diff changeset
   220
          val doc = Document.empty(id)
34835
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34833
diff changeset
   221
          register_document(doc)
34808
e462572536e9 eliminated global Session.document_0 -- did not work due to hardwired id;
wenzelm
parents: 34807
diff changeset
   222
          prover.begin_document(id, path)
e462572536e9 eliminated global Session.document_0 -- did not work due to hardwired id;
wenzelm
parents: 34807
diff changeset
   223
          reply(doc)
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   224
34820
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   225
        case change: Change if prover != null =>
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   226
          handle_change(change)
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   227
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   228
        case result: Isabelle_Process.Result =>
34795
c97335b7e8c3 cache results;
wenzelm
parents: 34792
diff changeset
   229
          handle_result(result.cache(xml_cache))
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   230
36782
0499d05663dd ignore spurious TIMEOUT messages, maybe caused by change of actor semantics in scala-2.8;
wenzelm
parents: 36682
diff changeset
   231
        case TIMEOUT =>  // FIXME clarify!
0499d05663dd ignore spurious TIMEOUT messages, maybe caused by change of actor semantics in scala-2.8;
wenzelm
parents: 36682
diff changeset
   232
34820
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   233
        case bad if prover != null =>
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   234
          System.err.println("session_actor: ignoring bad message " + bad)
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   235
      }
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   236
    }
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   237
  }
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   238
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   239
37129
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   240
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   241
  /** buffered command changes -- delay_first discipline **/
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   242
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   243
  private lazy val command_change_buffer = actor {
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   244
    import scala.compat.Platform.currentTime
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   245
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   246
    var changed: Set[Command] = Set()
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   247
    var flush_time: Option[Long] = None
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   248
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   249
    def flush_timeout: Long =
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   250
      flush_time match {
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   251
        case None => 5000L
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   252
        case Some(time) => (time - currentTime) max 0
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   253
      }
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   254
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   255
    def flush()
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   256
    {
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   257
      if (!changed.isEmpty) commands_changed.event(Command_Set(changed))
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   258
      changed = Set()
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   259
      flush_time = None
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   260
    }
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   261
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   262
    def invoke()
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   263
    {
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   264
      val now = currentTime
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   265
      flush_time match {
37132
10ef4da1c314 indicate prospective properties;
wenzelm
parents: 37129
diff changeset
   266
        case None => flush_time = Some(now + 100)   // FIXME output_delay property
37129
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   267
        case Some(time) => if (now >= time) flush()
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   268
      }
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   269
    }
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   270
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   271
    loop {
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   272
      reactWithin(flush_timeout) {
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   273
        case command: Command => changed += command; invoke()
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   274
        case TIMEOUT => flush()
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   275
        case bad => System.err.println("command_change_buffer: ignoring bad message " + bad)
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   276
      }
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   277
    }
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   278
  }
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   279
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   280
  def indicate_command_change(command: Command)
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   281
  {
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   282
    command_change_buffer ! command
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   283
  }
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   284
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 37065
diff changeset
   285
34809
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   286
  /* main methods */
0fed930f2992 misc tuning;
wenzelm
parents: 34808
diff changeset
   287
34820
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   288
  def start(timeout: Int, args: List[String]): Option[String] =
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   289
    (session_actor !? Start(timeout, args)).asInstanceOf[Option[String]]
a8ba6cde13e9 basic setup for synchronous / modal (!) prover startup;
wenzelm
parents: 34819
diff changeset
   290
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   291
  def stop() { session_actor ! Stop }
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   292
  def input(change: Change) { session_actor ! change }
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   293
34823
2f3ea37c5958 renamed Proof_Document to Document;
wenzelm
parents: 34820
diff changeset
   294
  def begin_document(path: String): Document =
2f3ea37c5958 renamed Proof_Document to Document;
wenzelm
parents: 34820
diff changeset
   295
    (session_actor !? Begin_Document(path)).asInstanceOf[Document]
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff changeset
   296
}