-
Notifications
You must be signed in to change notification settings - Fork 7
/
JavaExample.java
158 lines (129 loc) · 5.61 KB
/
JavaExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package de.unruh.isabelle;
import de.unruh.isabelle.control.Isabelle;
import de.unruh.isabelle.java.JIsabelle;
import de.unruh.isabelle.misc.Symbols;
import de.unruh.isabelle.mlvalue.MLFunction2;
import de.unruh.isabelle.mlvalue.MLValue;
import de.unruh.isabelle.pure.*;
import de.unruh.javapatterns.Capture;
import de.unruh.javapatterns.MatchException;
import java.nio.file.Path;
import static de.unruh.isabelle.java.JPatterns.*;
import static de.unruh.javapatterns.Match.match;
import static de.unruh.javapatterns.Pattern.capture;
import static de.unruh.javapatterns.Patterns.Any;
import static de.unruh.javapatterns.Patterns.Is;
import static java.lang.System.out;
import static de.unruh.isabelle.pure.Implicits.*;
import static scala.concurrent.ExecutionContext.global;
public class JavaExample {
private Isabelle isabelle = null;
/** A function to replace occurrences of X+1 by X (for all X).
*
* This version is written using only standard Java tools. See {@link #replace2} below for
* a version written using pattern matching.
* */
@SuppressWarnings("InfiniteRecursion")
Term replace(Term term) {
term = term.concrete();
// case App(App(Const("Groups.plus_class.plus", _), x), Const("Groups.zero_class.zero", _)) => replace(x)
match1:
{
if (!(term instanceof App)) break match1;
App app = (App) term;
Term term1 = app.fun().concrete();
Term term2 = app.arg().concrete();
if (!(term1 instanceof App)) break match1;
App app1 = (App) term1;
Term term11 = app1.fun().concrete();
Term x = app1.arg();
if (!(term11 instanceof Const)) break match1;
Const const11 = (Const) term11;
if (!const11.name().equals("Groups.plus_class.plus")) break match1;
if (!(term2 instanceof Const)) break match1;
Const const2 = (Const) term2;
if (!const2.name().equals("Groups.zero_class.zero")) break match1;
return replace(x);
}
// case Abs(name, typ, body) => Abs(name, typ, replace(body))
match2:
{
if (!(term instanceof Abs)) break match2;
Abs abs = (Abs)term;
String name = abs.name();
Typ typ = abs.typ();
Term body = abs.body();
return Abs.apply(name, typ, replace(body), isabelle);
}
// case App(t1, t2) => App(replace(t1), replace(t2))
match3:
{
if (!(term instanceof App)) break match3;
App app = (App)term;
Term t1 = app.fun();
Term t2 = app.arg();
return App.apply(replace(t1), replace(t2), isabelle);
}
// case _ => term
return term;
}
/** A function to replace occurrences of X+1 by X (for all X).
*
* Written using pattern matching, does the same as {@link #replace}.
*/
Term replace2(Term term) {
Capture<Term> x = capture("x");
Capture<Term> t1 = capture("t1");
Capture<Term> t2 = capture("t2");
Capture<String> name = capture("name");
Capture<Typ> typ = capture("typ");
Capture<Term> body = capture("body");
try {
return match(term,
App(App(Const(Is("Groups.plus_class.plus"), Any), x),
Const(Is("Groups.zero_class.zero"), Any)),
() -> replace2(x.v()),
Abs(name, typ, body),
() -> Abs.apply(name.v(), typ.v(), replace2(body.v()), isabelle),
App(t1, t2),
() -> App.apply(replace2(t1.v()), replace2(t2.v()), isabelle),
Any,
() -> term);
} catch (MatchException e) {
// Cannot happen
throw new AssertionError("Unreachable code reached");
}
}
public static void main(String[] args) {
new JavaExample().runExample(args[0]);
}
void runExample(String isabelleHome) {
// Initialize the Isabelle process with session HOL.
// Differs from example in README: we skip building to make tests faster
Isabelle.SetupGeneral setup = JIsabelle.setupSetBuild(false, JIsabelle.setup(Path.of(isabelleHome)));
isabelle = new Isabelle(setup);
// Load the Isabelle/HOL theory "Main" and create a context object
Context ctxt = Context.apply("Main", isabelle);
// Create a term by parsing a string
Term term = Term.apply(ctxt, "x+0 = (y::nat)*1", Symbols.globalInstance(), isabelle);
// Replace x+0 by x in the term above
Term term2 = replace(term);
// And pretty print the term
out.println("term2: " + term2.pretty(ctxt, Symbols.globalInstance()));
// ==> term2: x = y * 1
// Compile an ML function that can be executed directly in the Isabelle process
MLFunction2<Context, Term, Term> simplify =
MLValue.compileFunction("fn (ctxt,t) => Thm.cterm_of ctxt t |> Simplifier.asm_full_rewrite ctxt " +
"|> Thm.rhs_of |> Thm.term_of",
isabelle,
contextConverter(), termConverter(), termConverter());
// Simplify term2
Term term3 = simplify
.apply(ctxt, term2, isabelle, contextConverter(), termConverter())
.retrieveNow(termConverter(), isabelle);
out.println("term3: " + term3.pretty(ctxt, Symbols.globalInstance()));
// ==> term3: x = y
// Destroy to save resources. (Not needed if the application ends here anyway.)
isabelle.destroy();
}
}