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