src/Pure/Tools/process_theories.scala
author wenzelm
Sun, 10 Aug 2025 18:00:53 +0200
changeset 82977 35b0ef2558da
parent 82975 a28d9192d31e
child 82980 839d86abfe86
permissions -rw-r--r--
support output of prover messages;
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
82977
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
    13
import scala.util.matching.Regex
82975
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
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    16
object Process_Theories {
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    17
  /** process theories **/
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    18
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    19
  def read_files(path: Path): List[Path] =
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    20
    Library.trim_split_lines(File.read(path)).map(Path.explode)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    21
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    22
  def process_theories(
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    23
    options: Options,
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    24
    logic: String,
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    25
    theories: List[String],
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    26
    files: List[Path] = Nil,
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    27
    dirs: List[Path] = Nil,
82977
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
    28
    output_messages: Boolean = false,
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
    29
    message_head: List[Regex],
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
    30
    message_body: List[Regex],
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
    31
    margin: Double = Pretty.default_margin,
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
    32
    breakgain: Double = Pretty.default_breakgain,
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
    33
    metric: Pretty.Metric = Symbol.Metric,
82975
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    34
    progress: Progress = new Progress
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    35
  ): Build.Results = {
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    36
    Isabelle_System.with_tmp_dir("private") { private_dir =>
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    37
      /* options */
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    38
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    39
      val build_engine = Build.Engine(Build.engine_name(options))
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    40
      val build_options = build_engine.build_options(options)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    41
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    42
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    43
      /* session directory and files */
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    44
82977
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
    45
      val private_prefix = private_dir.implode + "/"
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
    46
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
    47
      val session_name = Sessions.DRAFT
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
    48
      val session_dir = Isabelle_System.make_directory(private_dir + Path.basic(session_name))
82975
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    49
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
        var seen = Set.empty[JFile]
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    52
        for (path0 <- files) {
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    53
          val path = path0.canonical
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    54
          val file = path.file
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    55
          if (!seen(file)) {
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    56
            seen += file
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    57
            val target = session_dir + path.base
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    58
            if (target.is_file) {
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    59
              error("Duplicate session source file " + path.base + " --- from " + path)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    60
            }
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    61
            Isabelle_System.copy_file(path, target)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    62
          }
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
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    66
      /* session theories */
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    67
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    68
      val more_theories =
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    69
        for (path <- files; name <- Thy_Header.get_thy_name(path.implode))
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    70
          yield name
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    71
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    72
      val session_theories = theories ::: more_theories
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    73
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    74
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    75
      /* session imports */
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
      val sessions_structure = Sessions.load_structure(build_options, dirs = dirs)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    78
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    79
      val session_imports =
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    80
        Set.from(
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    81
          for {
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    82
            name <- session_theories.iterator
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    83
            session = sessions_structure.theory_qualifier(name)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    84
            if session.nonEmpty
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    85
          } yield session).toList
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    86
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    87
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    88
      /* build adhoc session */
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
      val session_entry =
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    91
        Sessions.Session_Entry(
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    92
          parent = Some(logic),
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    93
          theories = session_theories.map(a => (Nil, List(((a, Position.none), false)))),
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    94
          imports = session_imports)
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
      val session_info =
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    97
        Sessions.Info.make(session_entry, draft_session = true,
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    98
          dir = session_dir, options = options)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
    99
82977
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   100
      def session_setup(setup_session_name: String, session: Session): Unit = {
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   101
        if (output_messages && setup_session_name == session_name) {
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   102
          session.all_messages += Session.Consumer[Prover.Message]("process_theories") {
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   103
            case output: Prover.Output
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   104
              if Protocol.is_exported(output.message) || Protocol.is_state(output.message) =>
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   105
              output.properties match {
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   106
                case Position.Line_File(line, file0) =>
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   107
                  val file = Library.perhaps_unprefix(private_prefix, file0)
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   108
                  val pos = Position.Line_File(line, file)
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   109
                  if (Build.print_log_check(pos, output.message, message_head, message_body)) {
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   110
                    progress.echo(Protocol.message_text(output.message, heading = true, pos = pos,
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   111
                      margin = margin, breakgain = breakgain, metric = metric))
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   112
                  }
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   113
                case _ =>
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   114
              }
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   115
            case _ =>
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   116
          }
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   117
        }
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   118
      }
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   119
82975
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   120
      Build.build(options, private_dir = Some(private_dir), progress = progress,
82977
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   121
        infos = List(session_info), selection = Sessions.Selection.session(session_name),
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   122
        session_setup = session_setup)
82975
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   123
    }
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   124
  }
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   125
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   126
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   127
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   128
  /** Isabelle tool wrapper **/
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   129
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   130
  val isabelle_tool = Isabelle_Tool("process_theories",
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   131
    "process theories within an adhoc session context",
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   132
    Scala_Project.here,
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   133
    { args =>
82977
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   134
      val message_head = new mutable.ListBuffer[Regex]
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   135
      val message_body = new mutable.ListBuffer[Regex]
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   136
      var output_messages = false
82975
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   137
      val dirs = new mutable.ListBuffer[Path]
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   138
      val files = new mutable.ListBuffer[Path]
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   139
      var logic = Isabelle_System.getenv("ISABELLE_LOGIC")
82977
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   140
      var margin = Pretty.default_margin
82975
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   141
      var options = Options.init()
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   142
      var verbose = false
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   143
82977
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   144
82975
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   145
      val getopts = Getopts("""
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   146
Usage: isabelle process_theories [OPTIONS] [THEORIES...]
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   147
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   148
  Options are:
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   149
    -F FILE      include addition session files, listed in FILE
82977
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   150
    -H REGEX     filter messages by matching against head
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   151
    -M REGEX     filter messages by matching against body
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   152
    -O           output messages
82975
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   153
    -d DIR       include session directory
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   154
    -f FILE      include addition session files
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   155
    -l NAME      logic session name (default ISABELLE_LOGIC=""" + quote(logic) + """)
82977
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   156
    -m MARGIN    margin for pretty printing (default: """ + margin + """)
82975
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   157
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   158
    -v           verbose
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   159
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   160
  Process theories within an adhoc session context.
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   161
""",
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   162
        "F:" -> (arg => files ++= read_files(Path.explode(arg))),
82977
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   163
        "H:" -> (arg => message_head += arg.r),
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   164
        "M:" -> (arg => message_body += arg.r),
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   165
        "O" -> (_ => output_messages = true),
82975
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   166
        "d:" -> (arg => dirs += Path.explode(arg)),
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   167
        "f:" -> (arg => files += Path.explode(arg)),
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   168
        "l:" -> (arg => logic = arg),
82977
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   169
        "m:" -> (arg => margin = Value.Double.parse(arg)),
82975
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   170
        "o:" -> (arg => options = options + arg),
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   171
        "v" -> (_ => verbose = true))
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   172
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   173
      val theories = getopts(args)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   174
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   175
      val progress = new Console_Progress(verbose = verbose)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   176
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   177
      val results =
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   178
        progress.interrupt_handler {
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   179
          process_theories(options, logic, theories, files = files.toList, dirs = dirs.toList,
82977
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   180
            output_messages = output_messages, message_head = message_head.toList,
35b0ef2558da support output of prover messages;
wenzelm
parents: 82975
diff changeset
   181
            message_body = message_body.toList, margin = margin, progress = progress)
82975
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   182
        }
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   183
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   184
      sys.exit(results.rc)
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   185
    })
a28d9192d31e Process theories within an adhoc session context.
wenzelm
parents:
diff changeset
   186
}