src/Pure/Admin/component_hol_light.scala
author wenzelm
Sat, 18 Jan 2025 21:13:48 +0100
changeset 81916 2bb448f2ac51
parent 81915 02e107686442
child 81917 50cd5037aff7
permissions -rw-r--r--
avoid redundant repository clone;

/*  Title:      Pure/Admin/component_hol_light.scala
    Author:     Makarius

Build component for HOL-Light, with export of facts and proofs, offline
optimization, and import to Isabelle/HOL.
*/

package isabelle


object Component_HOL_Light {
  /* resources */

  val default_hol_light_url = "https://github.com/jrh13/hol-light.git"
  val default_hol_light_rev = "Release-3.0.0"

  val hol_import_dir: Path = Path.explode("~~/src/HOL/Import")
  def patch1: Path = hol_import_dir + Path.explode("patches/stage1.patch")
  def patch2: Path = hol_import_dir + Path.explode("patches/stage2.patch")

  def build_hol_light_import(
    only_offline: Boolean = false,
    progress: Progress = new Progress,
    target_dir: Path = Path.current,
    hol_light_url: String = default_hol_light_url,
    hol_light_rev: String = default_hol_light_rev
  ): Unit = {
    /* system */

    if (!only_offline) {
      Linux.check_system()
      Isabelle_System.require_command("buffer", test = "-i /dev/null")
      Isabelle_System.require_command("patch")
    }


    /* component */

    val component_name = "hol_light_import-" + Date.Format.alt_date(Date.now())
    val component_dir = Components.Directory(target_dir + Path.basic(component_name)).create()

    val platform = Isabelle_Platform.self.ISABELLE_PLATFORM(windows = true, apple = true)
    val platform_dir = Isabelle_System.make_directory(component_dir.path + Path.basic(platform))


    /* settings */

    component_dir.write_settings("""
HOL_LIGHT_IMPORT="$COMPONENT"
HOL_LIGTH_BUNDLE="$HOL_LIGHT_IMPORT/bundle/proofs"
HOL_LIGHT_OFFLINE="$HOL_LIGHT_IMPORT/${ISABELLE_WINDOWS_PLATFORM64:-${ISABELLE_APPLE_PLATFORM64:-$ISABELLE_PLATFORM64}}/offline"
""")


    /* README */

    File.write(component_dir.README,
      """Author: Cezary Kaliszyk, University of Innsbruck, 2013
Author: Alexander Krauss, QAware GmbH, 2013
Author: Sophie Tourret, INRIA, 2024
Author: Stéphane Glondu, INRIA, 2024
LICENSE (export tools): BSD-3 from Isabelle
LICENSE (HOL Light proofs): BSD-2 from HOL Light


This is an export of primitive proofs from HOL Light """ + hol_light_rev + """.

The original repository """ + hol_light_url + """
has been patched in 2 stages. The overall export process works like this:

  cd hol-light
  make

  patch -p1 < """ + patch1 + """
  ./ocaml-hol -I +compiler-libs stage1.ml

  patch -p1 < """ + patch2 + """
  export MAXTMS=10000
  ./ocaml-hol -I +compiler-libs stage2.ml

  gzip -d proofs.gz
  > maps.lst
  "$HOL_LIGHT_IMPORT/x86_64-linux/offline"


      Makarius
      """ + Date.Format.date(Date.now()) + "\n")


    Isabelle_System.with_tmp_dir("build") { tmp_dir =>

      /* OCaml setup */

      progress.echo("Setup OCaml ...")
      progress.bash(
        if (only_offline) "isabelle ocaml_setup_base"
        else "isabelle ocaml_setup && isabelle ocaml_opam install -y camlp5",
        echo = progress.verbose).check

      val opam_env = Isabelle_System.bash("isabelle ocaml_opam env").check.out


      /* "offline" tool */

      progress.echo("Building offline tool ...")

      val offline_path = Path.explode("offline")
      val offline_exe = offline_path.platform_exe
      val offline_dir = Isabelle_System.make_directory(tmp_dir + offline_path)

      Isabelle_System.copy_dir(hol_import_dir + offline_path, offline_dir, direct = true)

      progress.bash("ocamlopt offline.ml -o offline",
        cwd = offline_dir, echo = progress.verbose).check
      Isabelle_System.copy_file(offline_dir + offline_exe, platform_dir + offline_exe)
      File.set_executable(platform_dir + offline_exe)


      /* export */

      if (!only_offline) {
        val hol_light_dir = tmp_dir + Path.basic("hol-light")
        Isabelle_System.git_clone(hol_light_url, hol_light_dir, checkout = hol_light_rev,
          progress = progress)

        progress.echo("Exporting proofs ...")
        progress.bash(
          Library.make_lines("set -e", opam_env,
            "make",
            "patch -p1 < " + File.bash_path(patch1),
            "./ocaml-hol -I +compiler-libs stage1.ml",
            "patch -p1 < " + File.bash_path(patch2),
            "export MAXTMS=10000",
            "./ocaml-hol -I +compiler-libs stage2.ml",
            "gzip -d proofs.gz",
            "> maps.lst",
            File.bash_path(platform_dir + offline_exe) + " proofs"
          ),
          cwd = hol_light_dir, echo = progress.verbose).check

        val bundle_dir = Isabelle_System.make_directory(component_dir.path + Path.explode("bundle"))
        Isabelle_System.copy_file(hol_light_dir + Path.explode("proofs"), bundle_dir)
      }
    }
  }


  /* Isabelle tool wrapper */

  val isabelle_tool =
    Isabelle_Tool("component_hol_light_import", "build Isabelle component for HOL Light import",
      Scala_Project.here,
      { args =>
        var target_dir = Path.current
        var only_offline = false
        var hol_light_url = default_hol_light_url
        var hol_light_rev = default_hol_light_rev
        var verbose = false

        val getopts = Getopts("""
Usage: isabelle component_hol_light_import [OPTIONS]

  Options are:
    -D DIR       target directory (default ".")
    -O           only build the "offline" tool
    -U URL       git URL for original HOL Light repository, default:
                 """ + default_hol_light_url + """
    -r REV       revision or branch to checkout HOL Light (default: """ +
                    default_hol_light_rev + """)
    -v           verbose

  Build Isabelle component for HOL Light import.
""",
          "D:" -> (arg => target_dir = Path.explode(arg)),
          "O" -> (_ => only_offline = true),
          "U:" -> (arg => hol_light_url = arg),
          "r:" -> (arg => hol_light_rev = arg),
          "v" -> (_ => verbose = true))

        val more_args = getopts(args)
        if (more_args.nonEmpty) getopts.usage()

        val progress = new Console_Progress(verbose = verbose)

        build_hol_light_import(
          only_offline = only_offline, progress = progress, target_dir = target_dir,
          hol_light_url = hol_light_url, hol_light_rev = hol_light_rev)
      })
}