src/Pure/ML/ml_process.scala
author wenzelm
Sat, 14 Jun 2025 14:37:34 +0200
changeset 82708 e43ef311d595
parent 82706 e9b9af6da795
child 82709 1008b8e7c78d
permissions -rw-r--r--
clarified modules;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
65477
64e61b0f6972 clarified directories;
wenzelm
parents: 65471
diff changeset
     1
/*  Title:      Pure/ML/ml_process.scala
62544
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
     3
62586
a522a5692832 clarified modules;
wenzelm
parents: 62577
diff changeset
     4
The raw ML process.
62544
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
     5
*/
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
     6
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
     7
package isabelle
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
     8
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
     9
73897
0ddb5de0506e clarified signature: prefer Java interfaces;
wenzelm
parents: 73604
diff changeset
    10
import java.util.{Map => JMap, HashMap}
62573
27f90319a499 isabelle.Build uses ML_Process directly;
wenzelm
parents: 62565
diff changeset
    11
27f90319a499 isabelle.Build uses ML_Process directly;
wenzelm
parents: 62565
diff changeset
    12
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73897
diff changeset
    13
object ML_Process {
82708
e43ef311d595 clarified modules;
wenzelm
parents: 82706
diff changeset
    14
  /* settings */
e43ef311d595 clarified modules;
wenzelm
parents: 82706
diff changeset
    15
e43ef311d595 clarified modules;
wenzelm
parents: 82706
diff changeset
    16
  def ml_identifier(env: Isabelle_System.Settings = Isabelle_System.Settings()): String =
e43ef311d595 clarified modules;
wenzelm
parents: 82706
diff changeset
    17
    Isabelle_System.getenv("ML_IDENTIFIER", env = env)
e43ef311d595 clarified modules;
wenzelm
parents: 82706
diff changeset
    18
e43ef311d595 clarified modules;
wenzelm
parents: 82706
diff changeset
    19
e43ef311d595 clarified modules;
wenzelm
parents: 82706
diff changeset
    20
  /* heaps */
e43ef311d595 clarified modules;
wenzelm
parents: 82706
diff changeset
    21
80124
455ddb251ece clarified signature;
wenzelm
parents: 79674
diff changeset
    22
  def make_shasum(ancestors: List[SHA1.Shasum]): SHA1.Shasum =
455ddb251ece clarified signature;
wenzelm
parents: 79674
diff changeset
    23
    if (ancestors.isEmpty) SHA1.shasum_meta_info(SHA1.digest(Path.explode("$POLYML_EXE")))
455ddb251ece clarified signature;
wenzelm
parents: 79674
diff changeset
    24
    else SHA1.flat_shasum(ancestors)
77650
b1ca8975490a clarified modules;
wenzelm
parents: 77483
diff changeset
    25
77414
0d5994eef9e6 clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents: 77413
diff changeset
    26
  def session_heaps(
78178
a177f71dc79f clarified modules;
wenzelm
parents: 78161
diff changeset
    27
    store: Store,
77414
0d5994eef9e6 clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents: 77413
diff changeset
    28
    session_background: Sessions.Background,
0d5994eef9e6 clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents: 77413
diff changeset
    29
    logic: String = ""
0d5994eef9e6 clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents: 77413
diff changeset
    30
  ): List[Path] = {
0d5994eef9e6 clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents: 77413
diff changeset
    31
    val logic_name = Isabelle_System.default_logic(logic)
0d5994eef9e6 clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents: 77413
diff changeset
    32
0d5994eef9e6 clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents: 77413
diff changeset
    33
    session_background.sessions_structure.selection(logic_name).
0d5994eef9e6 clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents: 77413
diff changeset
    34
      build_requirements(List(logic_name)).
79674
215db9299a1a clarified signature;
wenzelm
parents: 79656
diff changeset
    35
      map(name => store.get_session(name).the_heap)
77414
0d5994eef9e6 clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents: 77413
diff changeset
    36
  }
0d5994eef9e6 clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents: 77413
diff changeset
    37
82708
e43ef311d595 clarified modules;
wenzelm
parents: 82706
diff changeset
    38
e43ef311d595 clarified modules;
wenzelm
parents: 82706
diff changeset
    39
  /* process */
e43ef311d595 clarified modules;
wenzelm
parents: 82706
diff changeset
    40
76729
b045b40a65cc clarified signature;
wenzelm
parents: 76657
diff changeset
    41
  def apply(
b045b40a65cc clarified signature;
wenzelm
parents: 76657
diff changeset
    42
    options: Options,
76657
a8d85b4a588c clarified signature;
wenzelm
parents: 76656
diff changeset
    43
    session_background: Sessions.Background,
77414
0d5994eef9e6 clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents: 77413
diff changeset
    44
    session_heaps: List[Path],
71639
ec84f542e411 clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
wenzelm
parents: 71598
diff changeset
    45
    use_prelude: List[String] = Nil,
ec84f542e411 clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
wenzelm
parents: 71598
diff changeset
    46
    eval_main: String = "",
62544
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
    47
    args: List[String] = Nil,
62556
c115e69f457f more abstract Session.start, without prover command-line;
wenzelm
parents: 62548
diff changeset
    48
    modes: List[String] = Nil,
80224
db92e0b6a11a clarified signature: prefer symbolic isabelle.Path over physical java.io.File;
wenzelm
parents: 80124
diff changeset
    49
    cwd: Path = Path.current,
82706
e9b9af6da795 clarified signature;
wenzelm
parents: 80462
diff changeset
    50
    env: JMap[String, String] = Isabelle_System.Settings.env(),
62557
a4ea3a222e0e tuned signature;
wenzelm
parents: 62556
diff changeset
    51
    redirect: Boolean = false,
76655
b3d458a90aeb clarified signature;
wenzelm
parents: 76654
diff changeset
    52
    cleanup: () => Unit = () => ()
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73897
diff changeset
    53
  ): Bash.Process = {
79656
10e560f2f580 more robust default: Scala imposes explicit "threads" value on ML, both the Poly/ML RTS and Isabelle/ML;
wenzelm
parents: 79654
diff changeset
    54
    val ml_options = options.standard_ml()
10e560f2f580 more robust default: Scala imposes explicit "threads" value on ML, both the Poly/ML RTS and Isabelle/ML;
wenzelm
parents: 79654
diff changeset
    55
62637
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62634
diff changeset
    56
    val eval_init =
77414
0d5994eef9e6 clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents: 77413
diff changeset
    57
      if (session_heaps.isEmpty) {
62544
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
    58
        List(
62912
745d31e63c21 section headings for ROOT.ML;
wenzelm
parents: 62902
diff changeset
    59
          """
745d31e63c21 section headings for ROOT.ML;
wenzelm
parents: 62902
diff changeset
    60
          fun chapter (_: string) = ();
745d31e63c21 section headings for ROOT.ML;
wenzelm
parents: 62902
diff changeset
    61
          fun section (_: string) = ();
745d31e63c21 section headings for ROOT.ML;
wenzelm
parents: 62902
diff changeset
    62
          fun subsection (_: string) = ();
745d31e63c21 section headings for ROOT.ML;
wenzelm
parents: 62902
diff changeset
    63
          fun subsubsection (_: string) = ();
745d31e63c21 section headings for ROOT.ML;
wenzelm
parents: 62902
diff changeset
    64
          fun paragraph (_: string) = ();
745d31e63c21 section headings for ROOT.ML;
wenzelm
parents: 62902
diff changeset
    65
          fun subparagraph (_: string) = ();
745d31e63c21 section headings for ROOT.ML;
wenzelm
parents: 62902
diff changeset
    66
          val ML_file = PolyML.use;
745d31e63c21 section headings for ROOT.ML;
wenzelm
parents: 62902
diff changeset
    67
          """,
62544
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
    68
          if (Platform.is_windows)
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
    69
            "fun exit 0 = OS.Process.exit OS.Process.success" +
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
    70
            " | exit 1 = OS.Process.exit OS.Process.failure" +
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
    71
            " | exit rc = OS.Process.exit (RunCall.unsafeCast (Word8.fromInt rc))"
62560
498f6ff16804 clarified initial ML;
wenzelm
parents: 62557
diff changeset
    72
          else
498f6ff16804 clarified initial ML;
wenzelm
parents: 62557
diff changeset
    73
            "fun exit rc = Posix.Process.exit (Word8.fromInt rc)",
62629
1815513a57f1 clarified prompt: "ML" usually means Isabelle/ML;
wenzelm
parents: 62614
diff changeset
    74
          "PolyML.Compiler.prompt1 := \"Poly/ML> \"",
1815513a57f1 clarified prompt: "ML" usually means Isabelle/ML;
wenzelm
parents: 62614
diff changeset
    75
          "PolyML.Compiler.prompt2 := \"Poly/ML# \"")
62544
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
    76
      }
77413
wenzelm
parents: 77412
diff changeset
    77
      else {
62637
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62634
diff changeset
    78
        List(
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62634
diff changeset
    79
          "(PolyML.SaveState.loadHierarchy " +
77414
0d5994eef9e6 clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents: 77413
diff changeset
    80
            ML_Syntax.print_list(
0d5994eef9e6 clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents: 77413
diff changeset
    81
              ML_Syntax.print_string_bytes)(session_heaps.map(File.platform_path)) +
0d5994eef9e6 clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents: 77413
diff changeset
    82
          "; PolyML.print_depth 0)")
77413
wenzelm
parents: 77412
diff changeset
    83
      }
62544
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
    84
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
    85
    val eval_modes =
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
    86
      if (modes.isEmpty) Nil
66782
193c31b79a33 clarified signature;
wenzelm
parents: 66781
diff changeset
    87
      else List("Print_Mode.add_modes " + ML_Syntax.print_list(ML_Syntax.print_string_bytes)(modes))
62544
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
    88
73897
0ddb5de0506e clarified signature: prefer Java interfaces;
wenzelm
parents: 73604
diff changeset
    89
62544
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
    90
    // options
77414
0d5994eef9e6 clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents: 77413
diff changeset
    91
    val eval_options = if (session_heaps.isEmpty) Nil else List("Options.load_default ()")
62544
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
    92
    val isabelle_process_options = Isabelle_System.tmp_file("options")
78161
4b1b7cbb3e9a clarified signature;
wenzelm
parents: 77650
diff changeset
    93
    File.restrict(File.path(isabelle_process_options))
79656
10e560f2f580 more robust default: Scala imposes explicit "threads" value on ML, both the Poly/ML RTS and Isabelle/ML;
wenzelm
parents: 79654
diff changeset
    94
    File.write(isabelle_process_options, YXML.string_of_body(ml_options.encode))
62544
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
    95
76655
b3d458a90aeb clarified signature;
wenzelm
parents: 76654
diff changeset
    96
    // session resources
77414
0d5994eef9e6 clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents: 77413
diff changeset
    97
    val eval_init_session = if (session_heaps.isEmpty) Nil else List("Resources.init_session_env ()")
72637
fd68c9c1b90b more uniform Resources.init_session via YXML;
wenzelm
parents: 72628
diff changeset
    98
    val init_session = Isabelle_System.tmp_file("init_session")
78161
4b1b7cbb3e9a clarified signature;
wenzelm
parents: 77650
diff changeset
    99
    File.restrict(File.path(init_session))
80462
7a1f9e571046 clarified signature: more explicit XML.Body types, more uniform Symbol.encode_yxml;
wenzelm
parents: 80225
diff changeset
   100
    File.write(init_session, YXML.string_of_body(new Resources(session_background).init_session_xml))
65431
4a3e6cda3b94 provide session base for "isabelle build" and "isabelle console" ML process;
wenzelm
parents: 65420
diff changeset
   101
4a3e6cda3b94 provide session base for "isabelle build" and "isabelle console" ML process;
wenzelm
parents: 65420
diff changeset
   102
    // process
62544
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
   103
    val eval_process =
71639
ec84f542e411 clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
wenzelm
parents: 71598
diff changeset
   104
      proper_string(eval_main).getOrElse(
77414
0d5994eef9e6 clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents: 77413
diff changeset
   105
        if (session_heaps.isEmpty) {
79656
10e560f2f580 more robust default: Scala imposes explicit "threads" value on ML, both the Poly/ML RTS and Isabelle/ML;
wenzelm
parents: 79654
diff changeset
   106
          "PolyML.print_depth " + ML_Syntax.print_int(ml_options.int("ML_print_depth"))
71639
ec84f542e411 clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
wenzelm
parents: 71598
diff changeset
   107
        }
ec84f542e411 clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
wenzelm
parents: 71598
diff changeset
   108
        else "Isabelle_Process.init ()")
62544
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
   109
62601
a937889f0086 create ISABELLE_TMP in Scala (despite odd/obsolete chmod in d84b4d39bce1);
wenzelm
parents: 62600
diff changeset
   110
    // ISABELLE_TMP
a937889f0086 create ISABELLE_TMP in Scala (despite odd/obsolete chmod in d84b4d39bce1);
wenzelm
parents: 62600
diff changeset
   111
    val isabelle_tmp = Isabelle_System.tmp_dir("process")
71881
71de0a253842 proper check of registered Scala functions;
wenzelm
parents: 71876
diff changeset
   112
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73897
diff changeset
   113
    val ml_runtime_options = {
79654
59debf50c9f7 tuned names;
wenzelm
parents: 78178
diff changeset
   114
      val ml_options0 = Word.explode(Isabelle_System.getenv("ML_OPTIONS"))
69702
1adc89c4a795 clarified ML_OPTIONS on Windows;
wenzelm
parents: 69573
diff changeset
   115
      val ml_options1 =
79654
59debf50c9f7 tuned names;
wenzelm
parents: 78178
diff changeset
   116
        if (ml_options0.exists(_.containsSlice("gcthreads"))) ml_options0
79656
10e560f2f580 more robust default: Scala imposes explicit "threads" value on ML, both the Poly/ML RTS and Isabelle/ML;
wenzelm
parents: 79654
diff changeset
   117
        else ml_options0 ::: List("--gcthreads", ml_options.threads().toString)
69702
1adc89c4a795 clarified ML_OPTIONS on Windows;
wenzelm
parents: 69573
diff changeset
   118
      val ml_options2 =
79654
59debf50c9f7 tuned names;
wenzelm
parents: 78178
diff changeset
   119
        if (!Platform.is_windows || ml_options0.exists(_.containsSlice("codepage"))) ml_options1
69702
1adc89c4a795 clarified ML_OPTIONS on Windows;
wenzelm
parents: 69573
diff changeset
   120
        else ml_options1 ::: List("--codepage", "utf8")
72112
3546dd4ade74 ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents: 71881
diff changeset
   121
      ml_options2 ::: List("--exportstats")
64274
c8990e5feac9 re-use "threads" for --gcthreads;
wenzelm
parents: 63986
diff changeset
   122
    }
c8990e5feac9 re-use "threads" for --gcthreads;
wenzelm
parents: 63986
diff changeset
   123
62545
8ebffdaf2ce2 Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents: 62544
diff changeset
   124
    // bash
8ebffdaf2ce2 Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents: 62544
diff changeset
   125
    val bash_args =
64274
c8990e5feac9 re-use "threads" for --gcthreads;
wenzelm
parents: 63986
diff changeset
   126
      ml_runtime_options :::
72637
fd68c9c1b90b more uniform Resources.init_session via YXML;
wenzelm
parents: 72628
diff changeset
   127
      (eval_init ::: eval_modes ::: eval_options ::: eval_init_session).flatMap(List("--eval", _)) :::
71639
ec84f542e411 clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
wenzelm
parents: 71598
diff changeset
   128
      use_prelude.flatMap(List("--use", _)) ::: List("--eval", eval_process) ::: args
62546
wenzelm
parents: 62545
diff changeset
   129
73897
0ddb5de0506e clarified signature: prefer Java interfaces;
wenzelm
parents: 73604
diff changeset
   130
    val bash_env = new HashMap(env)
0ddb5de0506e clarified signature: prefer Java interfaces;
wenzelm
parents: 73604
diff changeset
   131
    bash_env.put("ISABELLE_PROCESS_OPTIONS", File.standard_path(isabelle_process_options))
75590
99b7638d9177 clarified session resources for bootstrap, notably for Scala functions;
wenzelm
parents: 75394
diff changeset
   132
    bash_env.put("ISABELLE_INIT_SESSION", File.standard_path(init_session))
73897
0ddb5de0506e clarified signature: prefer Java interfaces;
wenzelm
parents: 73604
diff changeset
   133
    bash_env.put("ISABELLE_TMP", File.standard_path(isabelle_tmp))
0ddb5de0506e clarified signature: prefer Java interfaces;
wenzelm
parents: 73604
diff changeset
   134
    bash_env.put("POLYSTATSDIR", isabelle_tmp.getAbsolutePath)
0ddb5de0506e clarified signature: prefer Java interfaces;
wenzelm
parents: 73604
diff changeset
   135
79656
10e560f2f580 more robust default: Scala imposes explicit "threads" value on ML, both the Poly/ML RTS and Isabelle/ML;
wenzelm
parents: 79654
diff changeset
   136
    val process_policy = ml_options.string("process_policy")
77480
wenzelm
parents: 77414
diff changeset
   137
    val process_prefix = if_proper(process_policy, process_policy + " ")
77320
7a6fa60298cd tuned: avoid redundant white space;
wenzelm
parents: 76729
diff changeset
   138
77482
10147ecf9196 tuned, following ML_Statistics.monitor;
wenzelm
parents: 77480
diff changeset
   139
    Bash.process(process_prefix + "\"$POLYML_EXE\" -q " + Bash.strings(bash_args),
62614
0a01bc7f0946 prefer Scala over bash function;
wenzelm
parents: 62610
diff changeset
   140
      cwd = cwd,
73897
0ddb5de0506e clarified signature: prefer Java interfaces;
wenzelm
parents: 73604
diff changeset
   141
      env = bash_env,
62614
0a01bc7f0946 prefer Scala over bash function;
wenzelm
parents: 62610
diff changeset
   142
      redirect = redirect,
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   143
      cleanup = { () =>
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   144
        isabelle_process_options.delete
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   145
        init_session.delete
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   146
        Isabelle_System.rm_tree(isabelle_tmp)
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   147
        cleanup()
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   148
      })
62544
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
   149
  }
62586
a522a5692832 clarified modules;
wenzelm
parents: 62577
diff changeset
   150
a522a5692832 clarified modules;
wenzelm
parents: 62577
diff changeset
   151
62835
1a9ce1b13b20 prefer internal tool -- assuming that ISABELLE_TMP_PREFIX is created properly by Isabelle_System.isabelle_tmp_prefix;
wenzelm
parents: 62712
diff changeset
   152
  /* Isabelle tool wrapper */
62586
a522a5692832 clarified modules;
wenzelm
parents: 62577
diff changeset
   153
72763
3cc73d00553c added document antiquotation @{tool};
wenzelm
parents: 72637
diff changeset
   154
  val isabelle_tool = Isabelle_Tool("process", "raw ML process (batch mode)",
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   155
    Scala_Project.here,
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   156
    { args =>
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   157
      var dirs: List[Path] = Nil
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   158
      var eval_args: List[String] = Nil
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   159
      var logic = Isabelle_System.getenv("ISABELLE_LOGIC")
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   160
      var modes: List[String] = Nil
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   161
      var options = Options.init()
62586
a522a5692832 clarified modules;
wenzelm
parents: 62577
diff changeset
   162
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   163
      val getopts = Getopts("""
62634
aa3b47b32100 less physical "logic" argument, with option -l like "isabelle console" etc.;
wenzelm
parents: 62633
diff changeset
   164
Usage: isabelle process [OPTIONS]
62586
a522a5692832 clarified modules;
wenzelm
parents: 62577
diff changeset
   165
a522a5692832 clarified modules;
wenzelm
parents: 62577
diff changeset
   166
  Options are:
62677
0df43889f496 isabelle process -T THEORY;
wenzelm
parents: 62672
diff changeset
   167
    -T THEORY    load theory
62639
699e86051e35 isabelle process -d;
wenzelm
parents: 62638
diff changeset
   168
    -d DIR       include session directory
62586
a522a5692832 clarified modules;
wenzelm
parents: 62577
diff changeset
   169
    -e ML_EXPR   evaluate ML expression on startup
a522a5692832 clarified modules;
wenzelm
parents: 62577
diff changeset
   170
    -f ML_FILE   evaluate ML file on startup
62634
aa3b47b32100 less physical "logic" argument, with option -l like "isabelle console" etc.;
wenzelm
parents: 62633
diff changeset
   171
    -l NAME      logic session name (default ISABELLE_LOGIC=""" + quote(logic) + """)
62586
a522a5692832 clarified modules;
wenzelm
parents: 62577
diff changeset
   172
    -m MODE      add print mode for output
a522a5692832 clarified modules;
wenzelm
parents: 62577
diff changeset
   173
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
a522a5692832 clarified modules;
wenzelm
parents: 62577
diff changeset
   174
62634
aa3b47b32100 less physical "logic" argument, with option -l like "isabelle console" etc.;
wenzelm
parents: 62633
diff changeset
   175
  Run the raw Isabelle ML process in batch mode.
62586
a522a5692832 clarified modules;
wenzelm
parents: 62577
diff changeset
   176
""",
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   177
        "T:" -> (arg =>
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   178
          eval_args = eval_args ::: List("--eval", "use_thy " + ML_Syntax.print_string_bytes(arg))),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   179
        "d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   180
        "e:" -> (arg => eval_args = eval_args ::: List("--eval", arg)),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   181
        "f:" -> (arg => eval_args = eval_args ::: List("--use", arg)),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   182
        "l:" -> (arg => logic = arg),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   183
        "m:" -> (arg => modes = arg :: modes),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   184
        "o:" -> (arg => options = options + arg))
62586
a522a5692832 clarified modules;
wenzelm
parents: 62577
diff changeset
   185
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   186
      val more_args = getopts(args)
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   187
      if (args.isEmpty || more_args.nonEmpty) getopts.usage()
62586
a522a5692832 clarified modules;
wenzelm
parents: 62577
diff changeset
   188
78178
a177f71dc79f clarified modules;
wenzelm
parents: 78161
diff changeset
   189
      val store = Store(options)
76657
a8d85b4a588c clarified signature;
wenzelm
parents: 76656
diff changeset
   190
      val session_background = Sessions.background(options, logic, dirs = dirs).check_errors
77414
0d5994eef9e6 clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents: 77413
diff changeset
   191
      val session_heaps = ML_Process.session_heaps(store, session_background, logic = logic)
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   192
      val result =
77414
0d5994eef9e6 clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents: 77413
diff changeset
   193
        ML_Process(options, session_background, session_heaps, args = eval_args, modes = modes)
0d5994eef9e6 clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents: 77413
diff changeset
   194
          .result(
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   195
            progress_stdout = Output.writeln(_, stdout = true),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   196
            progress_stderr = Output.writeln(_))
72557
6345cce0e576 clarified output of "isabelle process";
wenzelm
parents: 72119
diff changeset
   197
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   198
      sys.exit(result.rc)
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   199
    })
62544
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents:
diff changeset
   200
}