src/HOL/Tools/Nitpick/kodkod.scala
author wenzelm
Mon, 24 Aug 2020 22:30:34 +0200
changeset 72203 733bab4c1be0
parent 72201 46bc864ec7a8
child 72204 cb746b19e1d7
permissions -rw-r--r--
suppress odd warning for context.exit();
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72176
22c11f65ddf9 rudiments of Scala interface for Kodkod;
wenzelm
parents:
diff changeset
     1
/*  Title:      HOL/Tools/Nitpick/kodkod.scala
22c11f65ddf9 rudiments of Scala interface for Kodkod;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
22c11f65ddf9 rudiments of Scala interface for Kodkod;
wenzelm
parents:
diff changeset
     3
22c11f65ddf9 rudiments of Scala interface for Kodkod;
wenzelm
parents:
diff changeset
     4
Scala interface for Kodkod.
22c11f65ddf9 rudiments of Scala interface for Kodkod;
wenzelm
parents:
diff changeset
     5
*/
22c11f65ddf9 rudiments of Scala interface for Kodkod;
wenzelm
parents:
diff changeset
     6
22c11f65ddf9 rudiments of Scala interface for Kodkod;
wenzelm
parents:
diff changeset
     7
package isabelle.nitpick
22c11f65ddf9 rudiments of Scala interface for Kodkod;
wenzelm
parents:
diff changeset
     8
22c11f65ddf9 rudiments of Scala interface for Kodkod;
wenzelm
parents:
diff changeset
     9
import isabelle._
22c11f65ddf9 rudiments of Scala interface for Kodkod;
wenzelm
parents:
diff changeset
    10
72177
fbaa6b40b439 update to kodkodi-1.5.4;
wenzelm
parents: 72176
diff changeset
    11
import java.util.concurrent.Executors
fbaa6b40b439 update to kodkodi-1.5.4;
wenzelm
parents: 72176
diff changeset
    12
72178
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    13
import org.antlr.runtime.{ANTLRInputStream, RecognitionException}
72177
fbaa6b40b439 update to kodkodi-1.5.4;
wenzelm
parents: 72176
diff changeset
    14
import de.tum.in.isabelle.Kodkodi.{Context, KodkodiLexer, KodkodiParser}
72176
22c11f65ddf9 rudiments of Scala interface for Kodkod;
wenzelm
parents:
diff changeset
    15
22c11f65ddf9 rudiments of Scala interface for Kodkod;
wenzelm
parents:
diff changeset
    16
22c11f65ddf9 rudiments of Scala interface for Kodkod;
wenzelm
parents:
diff changeset
    17
object Kodkod
22c11f65ddf9 rudiments of Scala interface for Kodkod;
wenzelm
parents:
diff changeset
    18
{
72178
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    19
  sealed case class Result(rc: Int, out: String, err: String)
72179
0841895ca438 clarified signature;
wenzelm
parents: 72178
diff changeset
    20
  {
0841895ca438 clarified signature;
wenzelm
parents: 72178
diff changeset
    21
    def ok: Boolean = rc == 0
0841895ca438 clarified signature;
wenzelm
parents: 72178
diff changeset
    22
    def check: String =
0841895ca438 clarified signature;
wenzelm
parents: 72178
diff changeset
    23
      if (ok) out else error(if (err.isEmpty) "Error" else err)
72196
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
    24
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
    25
    def encode: XML.Body =
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
    26
    {
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
    27
      import XML.Encode._
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
    28
      triple(int, string, string)((rc, out, err))
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
    29
    }
72179
0841895ca438 clarified signature;
wenzelm
parents: 72178
diff changeset
    30
  }
72178
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    31
72179
0841895ca438 clarified signature;
wenzelm
parents: 72178
diff changeset
    32
  def execute(source: String,
72176
22c11f65ddf9 rudiments of Scala interface for Kodkod;
wenzelm
parents:
diff changeset
    33
    solve_all: Boolean = false,
22c11f65ddf9 rudiments of Scala interface for Kodkod;
wenzelm
parents:
diff changeset
    34
    prove: Boolean = false,
22c11f65ddf9 rudiments of Scala interface for Kodkod;
wenzelm
parents:
diff changeset
    35
    max_solutions: Int = Integer.MAX_VALUE,
22c11f65ddf9 rudiments of Scala interface for Kodkod;
wenzelm
parents:
diff changeset
    36
    cleanup_inst: Boolean = false,
72178
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    37
    timeout: Time = Time.zero,
72199
8dc2e4d9deaa proper default for max_threads;
wenzelm
parents: 72196
diff changeset
    38
    max_threads: Int = 0): Result =
72176
22c11f65ddf9 rudiments of Scala interface for Kodkod;
wenzelm
parents:
diff changeset
    39
  {
72201
wenzelm
parents: 72199
diff changeset
    40
    val pool_size = if (max_threads == 0) Isabelle_Thread.max_threads() else max_threads
wenzelm
parents: 72199
diff changeset
    41
    val executor = Executors.newFixedThreadPool(pool_size)
72178
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    42
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    43
    def executor_kill(): Unit =
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    44
      if (!executor.isShutdown) Isabelle_Thread.fork() { executor.shutdownNow() }
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    45
72203
733bab4c1be0 suppress odd warning for context.exit();
wenzelm
parents: 72201
diff changeset
    46
    class Exit extends Exception("EXIT")
72178
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    47
72177
fbaa6b40b439 update to kodkodi-1.5.4;
wenzelm
parents: 72176
diff changeset
    48
    val context =
fbaa6b40b439 update to kodkodi-1.5.4;
wenzelm
parents: 72176
diff changeset
    49
      new Context {
72178
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    50
        private var rc = 0
72177
fbaa6b40b439 update to kodkodi-1.5.4;
wenzelm
parents: 72176
diff changeset
    51
        private val out = new StringBuilder
fbaa6b40b439 update to kodkodi-1.5.4;
wenzelm
parents: 72176
diff changeset
    52
        private val err = new StringBuilder
72178
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    53
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    54
        def return_code(i: Int): Unit = synchronized { rc = rc max i}
72177
fbaa6b40b439 update to kodkodi-1.5.4;
wenzelm
parents: 72176
diff changeset
    55
        override def output(s: String): Unit = synchronized { out ++= s; out += '\n' }
fbaa6b40b439 update to kodkodi-1.5.4;
wenzelm
parents: 72176
diff changeset
    56
        override def error(s: String): Unit = synchronized { err ++= s; err += '\n' }
72178
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    57
        override def exit(i: Int): Unit =
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    58
          synchronized {
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    59
            return_code(i)
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    60
            executor_kill()
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    61
            throw new Exit
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    62
          }
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    63
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    64
        def result(): Result = synchronized { Result(rc, out.toString, err.toString) }
72177
fbaa6b40b439 update to kodkodi-1.5.4;
wenzelm
parents: 72176
diff changeset
    65
      }
72178
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    66
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    67
    try {
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    68
      val lexer = new KodkodiLexer(new ANTLRInputStream(Bytes(source).stream))
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    69
      val parser =
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    70
        KodkodiParser.create(context, executor,
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    71
          false, solve_all, prove, max_solutions, cleanup_inst, lexer)
72176
22c11f65ddf9 rudiments of Scala interface for Kodkod;
wenzelm
parents:
diff changeset
    72
72178
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    73
      val timeout_request =
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    74
        if (timeout.is_zero) None
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    75
        else {
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    76
          Some(Event_Timer.request(Time.now() + timeout) {
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    77
            context.error("Ran out of time")
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    78
            context.return_code(2)
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    79
            executor_kill()
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    80
          })
72177
fbaa6b40b439 update to kodkodi-1.5.4;
wenzelm
parents: 72176
diff changeset
    81
        }
72178
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    82
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    83
      try { parser.problems() }
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    84
      catch { case exn: RecognitionException => parser.reportError(exn) }
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    85
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    86
      timeout_request.foreach(_.cancel)
72177
fbaa6b40b439 update to kodkodi-1.5.4;
wenzelm
parents: 72176
diff changeset
    87
72178
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    88
      if (parser.getTokenStream.LA(1) != KodkodiParser.EOF) {
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    89
        context.error("Error: trailing tokens")
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    90
        context.exit(1)
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    91
      }
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    92
      if (lexer.getNumberOfSyntaxErrors + parser.getNumberOfSyntaxErrors > 0) {
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    93
        context.exit(1)
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    94
      }
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    95
    }
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    96
    catch {
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    97
      case _: Exit =>
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    98
      case exn: Throwable =>
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
    99
        val message = exn.getMessage
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
   100
        context.error(if (message.isEmpty) exn.toString else "Error: " + message)
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
   101
        context.return_code(1)
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
   102
    }
72177
fbaa6b40b439 update to kodkodi-1.5.4;
wenzelm
parents: 72176
diff changeset
   103
72178
2481cdb84832 more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents: 72177
diff changeset
   104
    context.result()
72176
22c11f65ddf9 rudiments of Scala interface for Kodkod;
wenzelm
parents:
diff changeset
   105
  }
72179
0841895ca438 clarified signature;
wenzelm
parents: 72178
diff changeset
   106
0841895ca438 clarified signature;
wenzelm
parents: 72178
diff changeset
   107
  def warmup(): String =
72181
6241cbbf5a58 preload library;
wenzelm
parents: 72179
diff changeset
   108
    execute(
72199
8dc2e4d9deaa proper default for max_threads;
wenzelm
parents: 72196
diff changeset
   109
      "solver: \"MiniSat\"\n" +
72181
6241cbbf5a58 preload library;
wenzelm
parents: 72179
diff changeset
   110
      File.read(Path.explode("$KODKODI/examples/weber3.kki"))).check
72196
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   111
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   112
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   113
  /* scala function */
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   114
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   115
  object Fun extends Scala.Fun("kodkod")
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   116
  {
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   117
    def apply(args: String): String =
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   118
    {
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   119
      val (timeout, (solve_all, (max_solutions, (max_threads, kki)))) =
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   120
      {
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   121
        import XML.Decode._
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   122
        pair(int, pair(bool, pair(int, pair(int, string))))(YXML.parse_body(args))
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   123
      }
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   124
      val result =
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   125
        execute(kki,
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   126
          solve_all = solve_all,
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   127
          max_solutions = max_solutions,
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   128
          timeout = Time.ms(timeout),
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   129
          max_threads = max_threads)
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   130
      YXML.string_of_body(result.encode)
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   131
    }
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   132
  }
72176
22c11f65ddf9 rudiments of Scala interface for Kodkod;
wenzelm
parents:
diff changeset
   133
}
72196
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   134
6dba090358d2 invoke Nitpick/Kodkod via Isabelle/Scala (instead of external process);
wenzelm
parents: 72181
diff changeset
   135
class Scala_Functions extends Scala.Functions(Kodkod.Fun)