src/Pure/Tools/process_theories.scala
author wenzelm
Sun, 10 Aug 2025 15:17:13 +0200
changeset 82975 a28d9192d31e
child 82977 35b0ef2558da
permissions -rw-r--r--
Process theories within an adhoc session context.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
82975
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Tools/process_theories.scala
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
     3
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
     4
Process theories within an adhoc session context.
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
     5
*/
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
     6
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
     7
package isabelle
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
     8
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
     9
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    10
import java.io.{File => JFile}
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    11
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    12
import scala.collection.mutable
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    13
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    14
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    15
object Process_Theories {
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    16
  /** process theories **/
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    17
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    18
  def read_files(path: Path): List[Path] =
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    19
    Library.trim_split_lines(File.read(path)).map(Path.explode)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    20
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    21
  def process_theories(
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    22
    options: Options,
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    23
    logic: String,
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    24
    theories: List[String],
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    25
    files: List[Path] = Nil,
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    26
    dirs: List[Path] = Nil,
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    27
    progress: Progress = new Progress
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    28
  ): Build.Results = {
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    29
    Isabelle_System.with_tmp_dir("private") { private_dir =>
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    30
      /* options */
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    31
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    32
      val build_engine = Build.Engine(Build.engine_name(options))
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    33
      val build_options = build_engine.build_options(options)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    34
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    35
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    36
      /* session directory and files */
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    37
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    38
      val session_dir = Isabelle_System.make_directory(private_dir + Path.basic("session"))
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    39
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    40
      {
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    41
        var seen = Set.empty[JFile]
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    42
        for (path0 <- files) {
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    43
          val path = path0.canonical
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    44
          val file = path.file
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    45
          if (!seen(file)) {
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    46
            seen += file
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    47
            val target = session_dir + path.base
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    48
            if (target.is_file) {
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    49
              error("Duplicate session source file " + path.base + " --- from " + path)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    50
            }
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    51
            Isabelle_System.copy_file(path, target)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    52
          }
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    53
        }
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    54
      }
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    55
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    56
      /* session theories */
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    57
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    58
      val more_theories =
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    59
        for (path <- files; name <- Thy_Header.get_thy_name(path.implode))
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    60
          yield name
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    61
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    62
      val session_theories = theories ::: more_theories
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    63
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    64
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    65
      /* session imports */
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    66
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    67
      val sessions_structure = Sessions.load_structure(build_options, dirs = dirs)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    68
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    69
      val session_imports =
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    70
        Set.from(
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    71
          for {
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    72
            name <- session_theories.iterator
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    73
            session = sessions_structure.theory_qualifier(name)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    74
            if session.nonEmpty
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    75
          } yield session).toList
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    76
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    77
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    78
      /* build adhoc session */
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    79
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    80
      val session_entry =
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    81
        Sessions.Session_Entry(
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    82
          parent = Some(logic),
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    83
          theories = session_theories.map(a => (Nil, List(((a, Position.none), false)))),
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    84
          imports = session_imports)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    85
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    86
      val session_info =
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    87
        Sessions.Info.make(session_entry, draft_session = true,
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    88
          dir = session_dir, options = options)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    89
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    90
      Build.build(options, private_dir = Some(private_dir), progress = progress,
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    91
        infos = List(session_info), selection = Sessions.Selection.session(session_info.name))
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    92
    }
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    93
  }
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    94
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    95
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    96
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    97
  /** Isabelle tool wrapper **/
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    98
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    99
  val isabelle_tool = Isabelle_Tool("process_theories",
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   100
    "process theories within an adhoc session context",
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   101
    Scala_Project.here,
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   102
    { args =>
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   103
      val dirs = new mutable.ListBuffer[Path]
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   104
      val files = new mutable.ListBuffer[Path]
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   105
      var logic = Isabelle_System.getenv("ISABELLE_LOGIC")
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   106
      var options = Options.init()
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   107
      var verbose = false
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   108
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   109
      val getopts = Getopts("""
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   110
Usage: isabelle process_theories [OPTIONS] [THEORIES...]
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   111
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   112
  Options are:
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   113
    -F FILE      include addition session files, listed in FILE
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   114
    -d DIR       include session directory
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   115
    -f FILE      include addition session files
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   116
    -l NAME      logic session name (default ISABELLE_LOGIC=""" + quote(logic) + """)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   117
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   118
    -v           verbose
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   119
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   120
  Process theories within an adhoc session context.
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   121
""",
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   122
        "F:" -> (arg => files ++= read_files(Path.explode(arg))),
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   123
        "d:" -> (arg => dirs += Path.explode(arg)),
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   124
        "f:" -> (arg => files += Path.explode(arg)),
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   125
        "l:" -> (arg => logic = arg),
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   126
        "o:" -> (arg => options = options + arg),
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   127
        "v" -> (_ => verbose = true))
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   128
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   129
      val theories = getopts(args)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   130
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   131
      val progress = new Console_Progress(verbose = verbose)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   132
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   133
      val results =
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   134
        progress.interrupt_handler {
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   135
          process_theories(options, logic, theories, files = files.toList, dirs = dirs.toList,
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   136
            progress = progress)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   137
        }
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   138
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   139
      sys.exit(results.rc)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   140
    })
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   141
}