|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| package org.qedeq.kernel.bo.logic; |
|
19 |
| |
|
20 |
| |
|
21 |
| import org.qedeq.kernel.base.elli.Element; |
|
22 |
| import org.qedeq.kernel.base.elli.ElementList; |
|
23 |
| import org.qedeq.kernel.dto.elli.ElementSet; |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| public final class FormulaChecker { |
|
34 |
| |
|
35 |
| |
|
36 |
| public static final String CONJUNCTION_OPERATOR = "AND"; |
|
37 |
| |
|
38 |
| |
|
39 |
| public static final String DISJUNCTION_OPERATOR = "OR"; |
|
40 |
| |
|
41 |
| |
|
42 |
| public static final String IMPLICATION_OPERATOR = "IMPL"; |
|
43 |
| |
|
44 |
| |
|
45 |
| public static final String EQUIVALENCE_OPERATOR = "EQUI"; |
|
46 |
| |
|
47 |
| |
|
48 |
| public static final String NEGATION_OPERATOR = "NOT"; |
|
49 |
| |
|
50 |
| |
|
51 |
| public static final String PREDICATE_OPERATOR = "PRED"; |
|
52 |
| |
|
53 |
| |
|
54 |
| public static final String SUBJECT_VARIABLE_OPERATOR = "VAR"; |
|
55 |
| |
|
56 |
| |
|
57 |
| public static final String PROPOSITION_VARIABLE_OPERATOR = "PROP"; |
|
58 |
| |
|
59 |
| |
|
60 |
| public static final String PREDICATE_VARIABLE_OPERATOR = "PREDVAR"; |
|
61 |
| |
|
62 |
| |
|
63 |
| public static final String LIST_OPERATOR = "L"; |
|
64 |
| |
|
65 |
| |
|
66 |
| public static final String UNIVERSAL_QUANTIFIER_OPERATOR = "FORALL"; |
|
67 |
| |
|
68 |
| |
|
69 |
| public static final String EXISTENTIAL_QUANTIFIER_OPERATOR = "EXISTS"; |
|
70 |
| |
|
71 |
| |
|
72 |
| private static final String FIRST_ARGUMENT_MUST_BE_AN_ATOM = "first argument must be an atom"; |
|
73 |
| |
|
74 |
| |
|
75 |
| private static final String SECOND_ARGUMENT_MUST_BE_A_LIST = "second argument must be a list"; |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
0
| private FormulaChecker() {
|
|
82 |
| |
|
83 |
| } |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
0
| public static final void checkFormula(final Element element)
|
|
92 |
| throws CheckException { |
|
93 |
0
| if (element.isAtom()) {
|
|
94 |
0
| throw new CheckException("an atom is no formula", element);
|
|
95 |
| } |
|
96 |
0
| final ElementList list = element.getList();
|
|
97 |
0
| final String operator = list.getOperator();
|
|
98 |
0
| if (operator.equals(CONJUNCTION_OPERATOR)
|
|
99 |
| || operator.equals(DISJUNCTION_OPERATOR) |
|
100 |
| || operator.equals(IMPLICATION_OPERATOR) |
|
101 |
| || operator.equals(EQUIVALENCE_OPERATOR)) { |
|
102 |
0
| if (list.size() <= 1) {
|
|
103 |
0
| throw new CheckException(
|
|
104 |
| "more than one argument expected", element); |
|
105 |
| } |
|
106 |
| |
|
107 |
| |
|
108 |
0
| final ElementSet free = new ElementSet();
|
|
109 |
0
| final ElementSet bound = new ElementSet();
|
|
110 |
0
| for (int i = 0; i < list.size(); i++) {
|
|
111 |
0
| FormulaChecker.checkFormula(list.getElement(i));
|
|
112 |
0
| final ElementSet newFree
|
|
113 |
| = FormulaChecker.getFreeSubjectVariables(list.getElement(i)); |
|
114 |
0
| final ElementSet newBound
|
|
115 |
| = FormulaChecker.getBoundSubjectVariables(list.getElement(i)); |
|
116 |
0
| if (!newFree.newIntersection(bound).isEmpty()) {
|
|
117 |
0
| throw new CheckException(
|
|
118 |
| "these free variables are already bound in previous formulas: " |
|
119 |
| + newFree, list.getElement(i)); |
|
120 |
| } |
|
121 |
0
| if (!newBound.newIntersection(free).isEmpty()) {
|
|
122 |
0
| throw new CheckException(
|
|
123 |
| "these bound variables are already free in previous formulas: " |
|
124 |
| + newBound, list.getElement(i)); |
|
125 |
| } |
|
126 |
0
| bound.union(newBound);
|
|
127 |
0
| free.union(newFree);
|
|
128 |
| } |
|
129 |
0
| } else if (operator.equals(NEGATION_OPERATOR)) {
|
|
130 |
0
| if (list.size() != 1) {
|
|
131 |
0
| throw new CheckException(
|
|
132 |
| "negation requires exactly one argument", element); |
|
133 |
| } |
|
134 |
0
| FormulaChecker.checkFormula(list.getElement(0));
|
|
135 |
0
| } else if (operator.equals(PROPOSITION_VARIABLE_OPERATOR)) {
|
|
136 |
| |
|
137 |
0
| if (list.size() != 1) {
|
|
138 |
0
| throw new CheckException("exactly one numeric argument expected",
|
|
139 |
| element); |
|
140 |
| } |
|
141 |
| |
|
142 |
0
| if (!list.getElement(0).isAtom()) {
|
|
143 |
0
| throw new CheckException(FIRST_ARGUMENT_MUST_BE_AN_ATOM, element);
|
|
144 |
| } |
|
145 |
0
| } else if (operator.equals(PREDICATE_VARIABLE_OPERATOR)) {
|
|
146 |
0
| if (list.size() != 2) {
|
|
147 |
0
| throw new CheckException("exactly two arguments expected",
|
|
148 |
| element); |
|
149 |
| } |
|
150 |
| |
|
151 |
0
| if (!list.getElement(0).isAtom()) {
|
|
152 |
0
| throw new CheckException(FIRST_ARGUMENT_MUST_BE_AN_ATOM, element);
|
|
153 |
| } |
|
154 |
| |
|
155 |
| |
|
156 |
0
| if (!list.getElement(1).isList()) {
|
|
157 |
0
| throw new CheckException(SECOND_ARGUMENT_MUST_BE_A_LIST, element);
|
|
158 |
| } |
|
159 |
0
| final ElementList vars = list.getElement(1).getList();
|
|
160 |
0
| if (!vars.getOperator().equals(LIST_OPERATOR)) {
|
|
161 |
0
| throw new CheckException("list operator expected", vars);
|
|
162 |
| } |
|
163 |
0
| for (int i = 0; i < list.size(); i++) {
|
|
164 |
0
| FormulaChecker.checkSubjectVariable(list.getElement(i));
|
|
165 |
| } |
|
166 |
| |
|
167 |
0
| } else if (list.getOperator().equals(EXISTENTIAL_QUANTIFIER_OPERATOR)
|
|
168 |
| || list.getOperator().equals(UNIVERSAL_QUANTIFIER_OPERATOR)) { |
|
169 |
0
| if (list.size() != 2) {
|
|
170 |
0
| throw new CheckException("exactly two arguments expected",
|
|
171 |
| element); |
|
172 |
| } |
|
173 |
| |
|
174 |
0
| FormulaChecker.checkSubjectVariable(list.getElement(0));
|
|
175 |
| |
|
176 |
| |
|
177 |
0
| FormulaChecker.checkFormula(list.getElement(1));
|
|
178 |
| |
|
179 |
| |
|
180 |
0
| if (!FormulaChecker.getFreeSubjectVariables(list.getElement(1)).contains(
|
|
181 |
| list.getElement(0))) { |
|
182 |
0
| throw new CheckException("subject variable not free in formula",
|
|
183 |
| list.getElement(0)); |
|
184 |
| } |
|
185 |
| } else { |
|
186 |
0
| throw new CheckException("not a logical operator", element);
|
|
187 |
| } |
|
188 |
| } |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
0
| public static final boolean isSubjectVariable(final Element element) {
|
|
198 |
0
| try {
|
|
199 |
0
| FormulaChecker.checkSubjectVariable(element);
|
|
200 |
| } catch (CheckException e) { |
|
201 |
0
| return false;
|
|
202 |
| } |
|
203 |
0
| return true;
|
|
204 |
| } |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
0
| public static final void checkSubjectVariable(final Element element)
|
|
214 |
| throws CheckException { |
|
215 |
0
| if (!element.isList() && element.getList().getOperator().equals(SUBJECT_VARIABLE_OPERATOR)
|
|
216 |
| && element.getList().size() == 1) { |
|
217 |
| |
|
218 |
0
| if (!element.getList().getElement(0).isAtom()) {
|
|
219 |
0
| throw new CheckException(FIRST_ARGUMENT_MUST_BE_AN_ATOM, element);
|
|
220 |
| } |
|
221 |
| } else { |
|
222 |
0
| throw new CheckException("subject variable expected", element);
|
|
223 |
| } |
|
224 |
| } |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
0
| public static final ElementSet getFreeSubjectVariables(final Element element) {
|
|
234 |
0
| final ElementSet free = new ElementSet();
|
|
235 |
0
| if (FormulaChecker.isSubjectVariable(element)) {
|
|
236 |
0
| free.add(element);
|
|
237 |
0
| } else if (element.isList()) {
|
|
238 |
0
| final ElementList list = element.getList();
|
|
239 |
0
| final String operator = list.getOperator();
|
|
240 |
0
| if (operator.equals(EXISTENTIAL_QUANTIFIER_OPERATOR)
|
|
241 |
| || operator.equals(UNIVERSAL_QUANTIFIER_OPERATOR)) { |
|
242 |
0
| for (int i = 1; i < list.size(); i++) {
|
|
243 |
0
| free.union(FormulaChecker.getFreeSubjectVariables(list.getElement(i)));
|
|
244 |
| } |
|
245 |
0
| free.remove(list.getElement(0));
|
|
246 |
| } else { |
|
247 |
0
| for (int i = 0; i < list.size(); i++) {
|
|
248 |
0
| free.union(FormulaChecker.getFreeSubjectVariables(list.getElement(i)));
|
|
249 |
| } |
|
250 |
| } |
|
251 |
| } |
|
252 |
0
| return free;
|
|
253 |
| } |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
| |
|
259 |
| |
|
260 |
| |
|
261 |
0
| public static final ElementSet getBoundSubjectVariables(final Element element) {
|
|
262 |
0
| final ElementSet bound = new ElementSet();
|
|
263 |
0
| if (element.isList()) {
|
|
264 |
0
| ElementList list = element.getList();
|
|
265 |
0
| final String operator = list.getOperator();
|
|
266 |
0
| if (operator.equals(EXISTENTIAL_QUANTIFIER_OPERATOR)
|
|
267 |
| || operator.equals(UNIVERSAL_QUANTIFIER_OPERATOR)) { |
|
268 |
0
| for (int i = 1; i < list.size(); i++) {
|
|
269 |
0
| bound.union(FormulaChecker.getBoundSubjectVariables(
|
|
270 |
| list.getElement(i))); |
|
271 |
| } |
|
272 |
0
| bound.add(list.getElement(0));
|
|
273 |
| } else { |
|
274 |
0
| for (int i = 0; i < list.size(); i++) {
|
|
275 |
0
| bound.union(FormulaChecker.getBoundSubjectVariables(list.getElement(i)));
|
|
276 |
| } |
|
277 |
| } |
|
278 |
| } |
|
279 |
0
| return bound;
|
|
280 |
| } |
|
281 |
| |
|
282 |
| |
|
283 |
| } |