src/Pure/ML/ml_process.scala
author wenzelm
Fri Apr 21 14:09:03 2017 +0200 (2017-04-21 ago)
changeset 65532 febfd9f78bd4
parent 65478 7c40477e0a87
child 66712 4c98c929a12a
permissions -rw-r--r--
eliminated default_qualifier: just a constant;
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@65415
    26
    sessions: Option[Sessions.T] = 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@65415
    35
        val (_, selected_sessions) =
wenzelm@65419
    36
          sessions.getOrElse(Sessions.load(options, dirs)).selection(selection)
wenzelm@65420
    37
        (selected_sessions.build_ancestors(logic_name) ::: 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@62638
    78
            ML_Syntax.print_list(ML_Syntax.print_string0)(heaps) +
wenzelm@62637
    79
          "; PolyML.print_depth 0) handle exn => (TextIO.output (TextIO.stdErr, General.exnMessage exn ^ " +
wenzelm@62638
    80
          ML_Syntax.print_string0(": " + 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@62638
    85
      else List("Print_Mode.add_modes " + ML_Syntax.print_list(ML_Syntax.print_string0)(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@65441
   101
                ML_Syntax.print_string, ML_Syntax.print_string))(table)
wenzelm@65532
   102
          List("Resources.init_session_base" +
wenzelm@65532
   103
            " {global_theories = " + print_table(base.global_theories.toList) +
wenzelm@65471
   104
            ", loaded_theories = " + print_table(base.loaded_theories.toList) +
wenzelm@65441
   105
            ", known_theories = " + print_table(base.dest_known_theories) + "}")
wenzelm@65431
   106
      }
wenzelm@65431
   107
wenzelm@65431
   108
    // process
wenzelm@62544
   109
    val eval_process =
wenzelm@62634
   110
      if (heaps.isEmpty)
wenzelm@62712
   111
        List("PolyML.print_depth " + ML_Syntax.print_int(options.int("ML_print_depth")))
wenzelm@62565
   112
      else
wenzelm@62565
   113
        channel match {
wenzelm@62565
   114
          case None =>
wenzelm@62712
   115
            List("Isabelle_Process.init_options ()")
wenzelm@62565
   116
          case Some(ch) =>
wenzelm@62712
   117
            List("Isabelle_Process.init_protocol " + ML_Syntax.print_string0(ch.server_name))
wenzelm@62565
   118
        }
wenzelm@62544
   119
wenzelm@62601
   120
    // ISABELLE_TMP
wenzelm@62601
   121
    val isabelle_tmp = Isabelle_System.tmp_dir("process")
wenzelm@62601
   122
    val env_tmp = Map("ISABELLE_TMP" -> File.standard_path(isabelle_tmp))
wenzelm@62601
   123
wenzelm@64274
   124
    val ml_runtime_options =
wenzelm@64274
   125
    {
wenzelm@64274
   126
      val ml_options = Word.explode(Isabelle_System.getenv("ML_OPTIONS"))
wenzelm@64274
   127
      if (ml_options.exists(_.containsSlice("gcthreads"))) ml_options
wenzelm@64274
   128
      else ml_options ::: List("--gcthreads", options.int("threads").toString)
wenzelm@64274
   129
    }
wenzelm@64274
   130
wenzelm@62545
   131
    // bash
wenzelm@62545
   132
    val bash_args =
wenzelm@64274
   133
      ml_runtime_options :::
wenzelm@65431
   134
      (eval_init ::: eval_modes ::: eval_options ::: eval_session_base ::: eval_process).
wenzelm@62545
   135
        map(eval => List("--eval", eval)).flatten ::: args
wenzelm@62546
   136
wenzelm@63986
   137
    Bash.process(
wenzelm@63986
   138
      "exec " + options.string("ML_process_policy") + """ "$ML_HOME/poly" -q """ +
wenzelm@64304
   139
        Bash.strings(bash_args),
wenzelm@62614
   140
      cwd = cwd,
wenzelm@62614
   141
      env =
wenzelm@62614
   142
        Isabelle_System.library_path(env ++ env_options ++ env_tmp,
wenzelm@62614
   143
          Isabelle_System.getenv_strict("ML_HOME")),
wenzelm@62614
   144
      redirect = redirect,
wenzelm@62602
   145
      cleanup = () =>
wenzelm@62602
   146
        {
wenzelm@62602
   147
          isabelle_process_options.delete
wenzelm@62602
   148
          Isabelle_System.rm_tree(isabelle_tmp)
wenzelm@62603
   149
          cleanup()
wenzelm@62602
   150
        })
wenzelm@62544
   151
  }
wenzelm@62586
   152
wenzelm@62586
   153
wenzelm@62835
   154
  /* Isabelle tool wrapper */
wenzelm@62586
   155
wenzelm@62835
   156
  val isabelle_tool = Isabelle_Tool("process", "raw ML process (batch mode)", args =>
wenzelm@62586
   157
  {
wenzelm@62835
   158
    var dirs: List[Path] = Nil
wenzelm@62835
   159
    var eval_args: List[String] = Nil
wenzelm@62835
   160
    var logic = Isabelle_System.getenv("ISABELLE_LOGIC")
wenzelm@62835
   161
    var modes: List[String] = Nil
wenzelm@62835
   162
    var options = Options.init()
wenzelm@62586
   163
wenzelm@62835
   164
    val getopts = Getopts("""
wenzelm@62634
   165
Usage: isabelle process [OPTIONS]
wenzelm@62586
   166
wenzelm@62586
   167
  Options are:
wenzelm@62677
   168
    -T THEORY    load theory
wenzelm@62639
   169
    -d DIR       include session directory
wenzelm@62586
   170
    -e ML_EXPR   evaluate ML expression on startup
wenzelm@62586
   171
    -f ML_FILE   evaluate ML file on startup
wenzelm@62634
   172
    -l NAME      logic session name (default ISABELLE_LOGIC=""" + quote(logic) + """)
wenzelm@62586
   173
    -m MODE      add print mode for output
wenzelm@62586
   174
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
wenzelm@62586
   175
wenzelm@62634
   176
  Run the raw Isabelle ML process in batch mode.
wenzelm@62586
   177
""",
wenzelm@62835
   178
      "T:" -> (arg =>
wenzelm@62835
   179
        eval_args = eval_args ::: List("--eval", "use_thy " + ML_Syntax.print_string0(arg))),
wenzelm@62835
   180
      "d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))),
wenzelm@62835
   181
      "e:" -> (arg => eval_args = eval_args ::: List("--eval", arg)),
wenzelm@62835
   182
      "f:" -> (arg => eval_args = eval_args ::: List("--use", arg)),
wenzelm@62835
   183
      "l:" -> (arg => logic = arg),
wenzelm@62835
   184
      "m:" -> (arg => modes = arg :: modes),
wenzelm@62835
   185
      "o:" -> (arg => options = options + arg))
wenzelm@62586
   186
wenzelm@62835
   187
    val more_args = getopts(args)
wenzelm@62835
   188
    if (args.isEmpty || !more_args.isEmpty) getopts.usage()
wenzelm@62586
   189
wenzelm@62888
   190
    val rc = ML_Process(options, logic = logic, args = eval_args, dirs = dirs, modes = modes).
wenzelm@62835
   191
      result().print_stdout.rc
wenzelm@62888
   192
    sys.exit(rc)
wenzelm@62835
   193
  })
wenzelm@62544
   194
}