author | wenzelm |
Mon, 24 Aug 2020 22:30:34 +0200 | |
changeset 72203 | 733bab4c1be0 |
parent 72201 | 46bc864ec7a8 |
child 72204 | cb746b19e1d7 |
permissions | -rw-r--r-- |
72176 | 1 |
/* Title: HOL/Tools/Nitpick/kodkod.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Scala interface for Kodkod. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle.nitpick |
|
8 |
||
9 |
import isabelle._ |
|
10 |
||
72177 | 11 |
import java.util.concurrent.Executors |
12 |
||
72178
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
13 |
import org.antlr.runtime.{ANTLRInputStream, RecognitionException} |
72177 | 14 |
import de.tum.in.isabelle.Kodkodi.{Context, KodkodiLexer, KodkodiParser} |
72176 | 15 |
|
16 |
||
17 |
object Kodkod |
|
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 | 20 |
{ |
21 |
def ok: Boolean = rc == 0 |
|
22 |
def check: String = |
|
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 | 30 |
} |
72178
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
31 |
|
72179 | 32 |
def execute(source: String, |
72176 | 33 |
solve_all: Boolean = false, |
34 |
prove: Boolean = false, |
|
35 |
max_solutions: Int = Integer.MAX_VALUE, |
|
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 | 38 |
max_threads: Int = 0): Result = |
72176 | 39 |
{ |
72201 | 40 |
val pool_size = if (max_threads == 0) Isabelle_Thread.max_threads() else max_threads |
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 | 46 |
class Exit extends Exception("EXIT") |
72178
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
47 |
|
72177 | 48 |
val context = |
49 |
new Context { |
|
72178
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
50 |
private var rc = 0 |
72177 | 51 |
private val out = new StringBuilder |
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 | 55 |
override def output(s: String): Unit = synchronized { out ++= s; out += '\n' } |
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 | 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 | 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 | 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 | 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 | 103 |
|
72178
2481cdb84832
more realistic kodkod invocation, imitating command-line tool;
wenzelm
parents:
72177
diff
changeset
|
104 |
context.result() |
72176 | 105 |
} |
72179 | 106 |
|
107 |
def warmup(): String = |
|
72181 | 108 |
execute( |
72199 | 109 |
"solver: \"MiniSat\"\n" + |
72181 | 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 | 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) |