src/Pure/ML/ml_process.scala
author wenzelm
Sat Dec 16 21:53:07 2017 +0100 (19 months ago)
changeset 67219 81e9804b2014
parent 67052 caf87d4b9b61
child 67471 bddfa23a4ea9
permissions -rw-r--r--
added document antiquotation @{session name};
renamed protocol function "Prover.session_base" to "Prover.init_session_base" according to the ML/Scala operation;
wenzelm@65477
     1
/*  Title:      Pure/ML/ml_process.scala
wenzelm@62544
     2
    Author:     Makarius
wenzelm@62544
     3
wenzelm@62586
     4
The raw ML process.
wenzelm@62544
     5
*/
wenzelm@62544
     6
wenzelm@62544
     7
package isabelle
wenzelm@62544
     8
wenzelm@62544
     9
wenzelm@62573
    10
import java.io.{File => JFile}
wenzelm@62573
    11
wenzelm@62573
    12
wenzelm@62544
    13
object ML_Process
wenzelm@62544
    14
{
wenzelm@62544
    15
  def apply(options: Options,
wenzelm@62634
    16
    logic: String = "",
wenzelm@62544
    17
    args: List[String] = Nil,
wenzelm@62637
    18
    dirs: List[Path] = Nil,
wenzelm@62556
    19
    modes: List[String] = Nil,
wenzelm@62643
    20
    raw_ml_system: Boolean = false,
wenzelm@62573
    21
    cwd: JFile = null,
wenzelm@62610
    22
    env: Map[String, String] = Isabelle_System.settings(),
wenzelm@62557
    23
    redirect: Boolean = false,
wenzelm@62603
    24
    cleanup: () => Unit = () => (),
wenzelm@62633
    25
    channel: Option[System_Channel] = None,
wenzelm@67052
    26
    sessions: Option[Sessions.Structure] = None,
wenzelm@65431
    27
    session_base: Option[Sessions.Base] = None,
wenzelm@62633
    28
    store: Sessions.Store = Sessions.store()): Bash.Process =
wenzelm@62544
    29
  {
wenzelm@62637
    30
    val logic_name = Isabelle_System.default_logic(logic)
wenzelm@62637
    31
    val heaps: List[String] =
wenzelm@62643
    32
      if (raw_ml_system) Nil
wenzelm@62637
    33
      else {
wenzelm@65419
    34
        val selection = Sessions.Selection(sessions = List(logic_name))
wenzelm@67025
    35
        val selected_sessions =
wenzelm@67026
    36
          sessions.getOrElse(Sessions.load_structure(options, dirs = dirs)).selection(selection)
wenzelm@66848
    37
        selected_sessions.build_requirements(List(logic_name)).
wenzelm@62637
    38
          map(a => File.platform_path(store.heap(a)))
wenzelm@62544
    39
      }
wenzelm@62544
    40
wenzelm@62637
    41
    val eval_init =
wenzelm@62634
    42
      if (heaps.isEmpty) {
wenzelm@62544
    43
        List(
wenzelm@62912
    44
          """
wenzelm@62912
    45
          fun chapter (_: string) = ();
wenzelm@62912
    46
          fun section (_: string) = ();
wenzelm@62912
    47
          fun subsection (_: string) = ();
wenzelm@62912
    48
          fun subsubsection (_: string) = ();
wenzelm@62912
    49
          fun paragraph (_: string) = ();
wenzelm@62912
    50
          fun subparagraph (_: string) = ();
wenzelm@62912
    51
          val ML_file = PolyML.use;
wenzelm@62912
    52
          """,
wenzelm@64557
    53
          if (Isabelle_System.getenv("ML_SYSTEM") == "polyml-5.6")
wenzelm@64557
    54
            """
wenzelm@64557
    55
              structure FixedInt = IntInf;
wenzelm@64557
    56
              structure RunCall =
wenzelm@64557
    57
              struct
wenzelm@64557
    58
                open RunCall
wenzelm@64557
    59
                val loadWord: word * word -> word =
wenzelm@64557
    60
                  run_call2 RuntimeCalls.POLY_SYS_load_word;
wenzelm@64557
    61
                val storeWord: word * word * word -> unit =
wenzelm@64557
    62
                  run_call3 RuntimeCalls.POLY_SYS_assign_word;
wenzelm@64557
    63
              end;
wenzelm@64557
    64
            """
wenzelm@62855
    65
          else "",
wenzelm@62544
    66
          if (Platform.is_windows)
wenzelm@62544
    67
            "fun exit 0 = OS.Process.exit OS.Process.success" +
wenzelm@62544
    68
            " | exit 1 = OS.Process.exit OS.Process.failure" +
wenzelm@62544
    69
            " | exit rc = OS.Process.exit (RunCall.unsafeCast (Word8.fromInt rc))"
wenzelm@62560
    70
          else
wenzelm@62560
    71
            "fun exit rc = Posix.Process.exit (Word8.fromInt rc)",
wenzelm@62629
    72
          "PolyML.Compiler.prompt1 := \"Poly/ML> \"",
wenzelm@62629
    73
          "PolyML.Compiler.prompt2 := \"Poly/ML# \"")
wenzelm@62544
    74
      }
wenzelm@62637
    75
      else
wenzelm@62637
    76
        List(
wenzelm@62637
    77
          "(PolyML.SaveState.loadHierarchy " +
wenzelm@66782
    78
            ML_Syntax.print_list(ML_Syntax.print_string_bytes)(heaps) +
wenzelm@62637
    79
          "; PolyML.print_depth 0) handle exn => (TextIO.output (TextIO.stdErr, General.exnMessage exn ^ " +
wenzelm@66782
    80
          ML_Syntax.print_string_bytes(": " + logic_name + "\n") +
wenzelm@62637
    81
          "); OS.Process.exit OS.Process.failure)")
wenzelm@62544
    82
wenzelm@62544
    83
    val eval_modes =
wenzelm@62544
    84
      if (modes.isEmpty) Nil
wenzelm@66782
    85
      else List("Print_Mode.add_modes " + ML_Syntax.print_list(ML_Syntax.print_string_bytes)(modes))
wenzelm@62544
    86
wenzelm@62544
    87
    // options
wenzelm@62544
    88
    val isabelle_process_options = Isabelle_System.tmp_file("options")
wenzelm@62544
    89
    File.write(isabelle_process_options, YXML.string_of_body(options.encode))
wenzelm@62573
    90
    val env_options = Map("ISABELLE_PROCESS_OPTIONS" -> File.standard_path(isabelle_process_options))
wenzelm@62634
    91
    val eval_options = if (heaps.isEmpty) Nil else List("Options.load_default ()")
wenzelm@62544
    92
wenzelm@65431
    93
    // session base
wenzelm@65431
    94
    val eval_session_base =
wenzelm@65431
    95
      session_base match {
wenzelm@65431
    96
        case None => Nil
wenzelm@65431
    97
        case Some(base) =>
wenzelm@65441
    98
          def print_table(table: List[(String, String)]): String =
wenzelm@65431
    99
            ML_Syntax.print_list(
wenzelm@65431
   100
              ML_Syntax.print_pair(
wenzelm@66782
   101
                ML_Syntax.print_string_bytes, ML_Syntax.print_string_bytes))(table)
wenzelm@66712
   102
          def print_list(list: List[String]): String =
wenzelm@66782
   103
            ML_Syntax.print_list(ML_Syntax.print_string_bytes)(list)
wenzelm@65532
   104
          List("Resources.init_session_base" +
wenzelm@67219
   105
            " {sessions = " + print_list(base.known.sessions.toList) +
wenzelm@67219
   106
            ", global_theories = " + print_table(base.global_theories.toList) +
wenzelm@66717
   107
            ", loaded_theories = " + print_list(base.loaded_theories.keys) +
wenzelm@65441
   108
            ", known_theories = " + print_table(base.dest_known_theories) + "}")
wenzelm@65431
   109
      }
wenzelm@65431
   110
wenzelm@65431
   111
    // process
wenzelm@62544
   112
    val eval_process =
wenzelm@62634
   113
      if (heaps.isEmpty)
wenzelm@62712
   114
        List("PolyML.print_depth " + ML_Syntax.print_int(options.int("ML_print_depth")))
wenzelm@62565
   115
      else
wenzelm@62565
   116
        channel match {
wenzelm@62565
   117
          case None =>
wenzelm@62712
   118
            List("Isabelle_Process.init_options ()")
wenzelm@62565
   119
          case Some(ch) =>
wenzelm@66782
   120
            List("Isabelle_Process.init_protocol " + ML_Syntax.print_string_bytes(ch.server_name))
wenzelm@62565
   121
        }
wenzelm@62544
   122
wenzelm@62601
   123
    // ISABELLE_TMP
wenzelm@62601
   124
    val isabelle_tmp = Isabelle_System.tmp_dir("process")
wenzelm@62601
   125
    val env_tmp = Map("ISABELLE_TMP" -> File.standard_path(isabelle_tmp))
wenzelm@62601
   126
wenzelm@64274
   127
    val ml_runtime_options =
wenzelm@64274
   128
    {
wenzelm@64274
   129
      val ml_options = Word.explode(Isabelle_System.getenv("ML_OPTIONS"))
wenzelm@64274
   130
      if (ml_options.exists(_.containsSlice("gcthreads"))) ml_options
wenzelm@64274
   131
      else ml_options ::: List("--gcthreads", options.int("threads").toString)
wenzelm@64274
   132
    }
wenzelm@64274
   133
wenzelm@62545
   134
    // bash
wenzelm@62545
   135
    val bash_args =
wenzelm@64274
   136
      ml_runtime_options :::
wenzelm@65431
   137
      (eval_init ::: eval_modes ::: eval_options ::: eval_session_base ::: eval_process).
wenzelm@62545
   138
        map(eval => List("--eval", eval)).flatten ::: args
wenzelm@62546
   139
wenzelm@63986
   140
    Bash.process(
wenzelm@63986
   141
      "exec " + options.string("ML_process_policy") + """ "$ML_HOME/poly" -q """ +
wenzelm@64304
   142
        Bash.strings(bash_args),
wenzelm@62614
   143
      cwd = cwd,
wenzelm@62614
   144
      env =
wenzelm@62614
   145
        Isabelle_System.library_path(env ++ env_options ++ env_tmp,
wenzelm@62614
   146
          Isabelle_System.getenv_strict("ML_HOME")),
wenzelm@62614
   147
      redirect = redirect,
wenzelm@62602
   148
      cleanup = () =>
wenzelm@62602
   149
        {
wenzelm@62602
   150
          isabelle_process_options.delete
wenzelm@62602
   151
          Isabelle_System.rm_tree(isabelle_tmp)
wenzelm@62603
   152
          cleanup()
wenzelm@62602
   153
        })
wenzelm@62544
   154
  }
wenzelm@62586
   155
wenzelm@62586
   156
wenzelm@62835
   157
  /* Isabelle tool wrapper */
wenzelm@62586
   158
wenzelm@62835
   159
  val isabelle_tool = Isabelle_Tool("process", "raw ML process (batch mode)", args =>
wenzelm@62586
   160
  {
wenzelm@62835
   161
    var dirs: List[Path] = Nil
wenzelm@62835
   162
    var eval_args: List[String] = Nil
wenzelm@62835
   163
    var logic = Isabelle_System.getenv("ISABELLE_LOGIC")
wenzelm@62835
   164
    var modes: List[String] = Nil
wenzelm@62835
   165
    var options = Options.init()
wenzelm@62586
   166
wenzelm@62835
   167
    val getopts = Getopts("""
wenzelm@62634
   168
Usage: isabelle process [OPTIONS]
wenzelm@62586
   169
wenzelm@62586
   170
  Options are:
wenzelm@62677
   171
    -T THEORY    load theory
wenzelm@62639
   172
    -d DIR       include session directory
wenzelm@62586
   173
    -e ML_EXPR   evaluate ML expression on startup
wenzelm@62586
   174
    -f ML_FILE   evaluate ML file on startup
wenzelm@62634
   175
    -l NAME      logic session name (default ISABELLE_LOGIC=""" + quote(logic) + """)
wenzelm@62586
   176
    -m MODE      add print mode for output
wenzelm@62586
   177
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
wenzelm@62586
   178
wenzelm@62634
   179
  Run the raw Isabelle ML process in batch mode.
wenzelm@62586
   180
""",
wenzelm@62835
   181
      "T:" -> (arg =>
wenzelm@66782
   182
        eval_args = eval_args ::: List("--eval", "use_thy " + ML_Syntax.print_string_bytes(arg))),
wenzelm@62835
   183
      "d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))),
wenzelm@62835
   184
      "e:" -> (arg => eval_args = eval_args ::: List("--eval", arg)),
wenzelm@62835
   185
      "f:" -> (arg => eval_args = eval_args ::: List("--use", arg)),
wenzelm@62835
   186
      "l:" -> (arg => logic = arg),
wenzelm@62835
   187
      "m:" -> (arg => modes = arg :: modes),
wenzelm@62835
   188
      "o:" -> (arg => options = options + arg))
wenzelm@62586
   189
wenzelm@62835
   190
    val more_args = getopts(args)
wenzelm@62835
   191
    if (args.isEmpty || !more_args.isEmpty) getopts.usage()
wenzelm@62586
   192
wenzelm@62888
   193
    val rc = ML_Process(options, logic = logic, args = eval_args, dirs = dirs, modes = modes).
wenzelm@62835
   194
      result().print_stdout.rc
wenzelm@62888
   195
    sys.exit(rc)
wenzelm@62835
   196
  })
wenzelm@62544
   197
}