aboutsummaryrefslogtreecommitdiffstats
path: root/libnormalform_fix_presentation__.c
blob: 8c0f4e0cba5a7b9537016e70a35ca200ab6e7af7 (plain) (blame)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


/**
 * Check whether inverting a sentence would require
 * it to be relaxes before it can be represented
 *
 * @param   this  The sentence to check
 * @param   flags  Flags from `libnormalform_express`
 * @return         1 if the inversion can be exactly represented,
 *                 0 if the inversion requires relaxation be
 */
PURE static int
inversion_would_relax(struct expression *this, uint64_t flags)
{
	size_t i;

beginning:
	switch (this->type) {
	case LIBNORMALFORM_CONJUNCTION:
	case LIBNORMALFORM_DISJUNCTION:
		for (i = 0; i < this->nterms; i++)
			if (inversion_would_relax(this->terms[i], flags))
				return 1;
		return 0;

	case LIBNORMALFORM_EXCLUSIVE_DISJUNCTION:
		for (i = 0; i < this->nterms; i++)
			if (!inversion_would_relax(this->terms[i], flags))
				return 0;
		return 1;

	case LIBNORMALFORM_VARIABLE:
		return flags & LIBNORMALFORM_ELIMINATE_NEGATED_VARIABLE;

	case LIBNORMALFORM_NEGATED_VARIABLE:
		return flags & LIBNORMALFORM_ELIMINATE_VARIABLE;

	case LIBNORMALFORM_FUNCTION:
		return flags & LIBNORMALFORM_ELIMINATE_NEGATED_FUNCTION;

	case LIBNORMALFORM_NEGATED_FUNCTION:
		return flags & LIBNORMALFORM_ELIMINATE_FUNCTION;

	case LIBNORMALFORM_TRANSFORMATION:
		this = this->terms[0];
		goto beginning;

	case LIBNORMALFORM_FOR_ALL:
		if (!(flags & LIBNORMALFORM_ELIMINATE_NEGATED_FOR_ALL))
			return 0;
		if (flags & LIBNORMALFORM_ELIMINATE_FOR_ANY)
			return 1;
		this = this->terms[this->nterms - 1];
		goto beginning;

	case LIBNORMALFORM_NEGATED_FOR_ALL:
		if (!(flags & LIBNORMALFORM_ELIMINATE_FOR_ALL))
			return 0;
		if (flags & LIBNORMALFORM_ELIMINATE_NEGATED_FOR_ANY)
			return 1;
		this = this->terms[this->nterms - 1];
		goto beginning;

	case LIBNORMALFORM_FOR_ANY:
		if (!(flags & LIBNORMALFORM_ELIMINATE_NEGATED_FOR_ANY))
			return 0;
		if (flags & LIBNORMALFORM_ELIMINATE_FOR_ALL)
			return 1;
		this = this->terms[this->nterms - 1];
		goto beginning;

	case LIBNORMALFORM_NEGATED_FOR_ANY:
		if (!(flags & LIBNORMALFORM_ELIMINATE_FOR_ANY))
			return 0;
		if (flags & LIBNORMALFORM_ELIMINATE_NEGATED_FOR_ALL)
			return 1;
		this = this->terms[this->nterms - 1];
		goto beginning;

	case LIBNORMALFORM_FOR_ONE:
		return flags & LIBNORMALFORM_ELIMINATE_NEGATED_FOR_ONE;

	case LIBNORMALFORM_NEGATED_FOR_ONE:
		return flags & LIBNORMALFORM_ELIMINATE_FOR_ONE;

	default:
		abort();
	}
}


int
(libnormalform_fix_presentation__)(struct expression *this, uint64_t flags, unsigned char *require_inversion, int *reduced)
{
#define WILL_ELIMINATE(BASETYPE, INVERT)\
	(flags & (LIBNORMALFORM_ELIMINATE_##BASETYPE <<\
	          ((this->type ^ ((LIBNORMALFORM_##BASETYPE ^ LIBNORMALFORM_NEGATED_##BASETYPE) *\
	                          (INVERT))) -\
	           LIBNORMALFORM_##BASETYPE)))

	size_t i;
	unsigned char zero = 0;
	struct expression *new;
	int may_switch = 1;

	switch (this->type) {
	case LIBNORMALFORM_CONJUNCTION:
	case LIBNORMALFORM_DISJUNCTION:
		this->type ^= LIBNORMALFORM_CONJUNCTION ^ LIBNORMALFORM_DISJUNCTION;
		if (this->invert) {
			if (*require_inversion) {
				this->invert ^= 1;
				*require_inversion = 0;
			}
			for (i = 0; i < this->nterms; i++)
				this->terms[i]->invert ^= 1;
		}
		require_inversion = &zero;
		break;

	case LIBNORMALFORM_EXCLUSIVE_DISJUNCTION:
		if (!this->nterms)
			abort();
		if (*require_inversion) { /* TODO always? */
			this->invert ^= 1;
			*require_inversion = 0;
		}
		for (i = 0; i < this->nterms; i++)
			if (libnormalform_fix_presentation__(this->terms[i], flags, &this->invert, reduced))
				return -1;
		if (this->invert) {
			/* TODO this is not necessarily the best element to invert,
			 *      possibly the previous loop could be used to determine
			 *      which is best (notably, clauses are not analysed) */
			this->invert = 0;
			this->terms[0]->invert ^= 1;
			if (libnormalform_fix_presentation__(this->terms[0], flags, &zero, reduced))
				return -1;
		}
		return 0;

	case LIBNORMALFORM_VARIABLE:
	case LIBNORMALFORM_NEGATED_VARIABLE:
		if (!WILL_ELIMINATE(VARIABLE, this->invert ^ *require_inversion)) {
			this->invert ^= *require_inversion;
			*require_inversion = 0;
		} else if (WILL_ELIMINATE(VARIABLE, this->invert)) {
			goto eliminate;
		}
		this->type ^= (LIBNORMALFORM_VARIABLE ^ LIBNORMALFORM_NEGATED_VARIABLE) * this->invert;
		break;

	case LIBNORMALFORM_FUNCTION:
	case LIBNORMALFORM_NEGATED_FUNCTION:
		if (!WILL_ELIMINATE(FUNCTION, this->invert ^ *require_inversion)) {
			this->invert ^= *require_inversion;
			*require_inversion = 0;
		} else if (WILL_ELIMINATE(FUNCTION, this->invert)) {
			goto eliminate;
		}
		this->type ^= (LIBNORMALFORM_FUNCTION ^ LIBNORMALFORM_NEGATED_FUNCTION) * this->invert;
		break;

	case LIBNORMALFORM_TRANSFORMATION:
		this->terms[0]->invert ^= this->invert;
		break;

	case LIBNORMALFORM_FOR_ALL:
	case LIBNORMALFORM_NEGATED_FOR_ALL:
		if (!WILL_ELIMINATE(FOR_ALL, this->invert ^ *require_inversion)) {
			this->invert ^= *require_inversion;
			*require_inversion = 0;
		} else for_all: if (WILL_ELIMINATE(FOR_ALL, this->invert)) {
			if (!may_switch)
				goto eliminate;
			may_switch = 0;
			this->invert = 0;
			this->terms[this->nterms - 1]->invert ^= 1;
			this->type = LIBNORMALFORM_NEGATED_FOR_ANY - (this->type - LIBNORMALFORM_FOR_ALL);
			goto for_any;
		}
		this->type ^= (LIBNORMALFORM_FOR_ALL ^ LIBNORMALFORM_NEGATED_FOR_ALL) * this->invert;
		this->invert = 0;
		if (flags & (LIBNORMALFORM_AVOID_FOR_ALL << (this->type - LIBNORMALFORM_FOR_ALL))) {
			if (!inversion_would_relax(this, flags)) {
				this->terms[this->nterms - 1]->invert ^= 1;
				this->type = LIBNORMALFORM_NEGATED_FOR_ANY - (this->type - LIBNORMALFORM_FOR_ALL);
				goto for_any_maybe_join_sides;
			}
		}
	for_all_maybe_join_sides:
		if (this->nterms > 1)
			break;
		if (flags & (LIBNORMALFORM_JOIN_SIDES_IN_FOR_ALL << (this->type - LIBNORMALFORM_FOR_ALL)))
			goto for_all_join_sides;
		break;

	case LIBNORMALFORM_FOR_ANY:
	case LIBNORMALFORM_NEGATED_FOR_ANY:
		if (!WILL_ELIMINATE(FOR_ANY, this->invert ^ *require_inversion)) {
			this->invert ^= *require_inversion;
			*require_inversion = 0;
		} else for_any: if (WILL_ELIMINATE(FOR_ANY, this->invert)) {
			if (!may_switch)
				goto eliminate;
			may_switch = 0;
			this->invert = 0;
			this->terms[this->nterms - 1]->invert ^= 1;
			this->type = LIBNORMALFORM_NEGATED_FOR_ALL - (this->type - LIBNORMALFORM_FOR_ANY);
			goto for_all;
		}
		this->type ^= (LIBNORMALFORM_FOR_ANY ^ LIBNORMALFORM_NEGATED_FOR_ANY) * this->invert;
		this->invert = 0;
		if (flags & (LIBNORMALFORM_AVOID_FOR_ANY << (this->type - LIBNORMALFORM_FOR_ANY))) {
			if (!inversion_would_relax(this, flags)) {
				this->terms[this->nterms - 1]->invert ^= 1;
				this->type = LIBNORMALFORM_NEGATED_FOR_ALL - (this->type - LIBNORMALFORM_FOR_ANY);
				goto for_all_maybe_join_sides;
			}
		}
	for_any_maybe_join_sides:
		if (this->nterms > 1)
			break;
		if (flags & (LIBNORMALFORM_JOIN_SIDES_IN_FOR_ANY << (this->type - LIBNORMALFORM_FOR_ANY)))
			goto for_any_join_sides;
		break;

	case LIBNORMALFORM_FOR_ONE:
	case LIBNORMALFORM_NEGATED_FOR_ONE:
		if (!WILL_ELIMINATE(FOR_ONE, this->invert ^ *require_inversion)) {
			this->invert ^= *require_inversion;
			*require_inversion = 0;
		} else if (WILL_ELIMINATE(FOR_ONE, this->invert)) {
			this->type ^= (LIBNORMALFORM_FOR_ONE ^ LIBNORMALFORM_NEGATED_FOR_ONE) * this->invert;
			if (this->type == LIBNORMALFORM_FOR_ONE) {
				this->invert = 0;
				this->reduced = 1;
				*reduced = 1;
				goto for_any;
			}
			goto eliminate;
		}
		this->type ^= (LIBNORMALFORM_FOR_ONE ^ LIBNORMALFORM_NEGATED_FOR_ONE) * this->invert;
		if (this->type == LIBNORMALFORM_FOR_ONE) {
			if (flags & LIBNORMALFORM_ELIMINATE_FOR_ONE) {
				this->type = LIBNORMALFORM_FOR_ANY;
				this->invert = 0;
				this->reduced = 1;
				*reduced = 1;
				goto for_any;
			}
		}
		if (this->nterms > 1)
			break;
		if (flags & (LIBNORMALFORM_JOIN_SIDES_IN_FOR_ONE << (this->type - LIBNORMALFORM_FOR_ONE)))
			goto for_any_join_sides;
		break;

	default:
		abort();
	}

	this->invert = 0;

	for (i = 0; i < this->nterms; i++)
		if (libnormalform_fix_presentation__(this->terms[i], flags, require_inversion, reduced))
			return -1;

	return 0;

eliminate:
	*require_inversion = 0;
	this->refcount = 1;
	this->reduced = 1;
	this->invert = 0;
	while (this->nterms)
		libnormalform_free_expression__(this->terms[--this->nterms]);
	free(this->terms);
	this->terms = NULL;
	this->type = LIBNORMALFORM_CONJUNCTION;
	*reduced = 1;
	return 0;

for_all_join_sides:
	new = libnormalform_make_binary__(1, this->terms[0], 0, this->terms[1], LIBNORMALFORM_DISJUNCTION);
	goto join_sides;

for_any_join_sides:
	new = libnormalform_make_binary__(0, this->terms[0], 0, this->terms[1], LIBNORMALFORM_CONJUNCTION);
join_sides:
	if (!new)
		return -1;
	this->terms[0] = new;
	this->nterms--;
	if (libnormalform_apply_transformation__(this->terms[0]->terms[0], LIBNORMALFORM_DOMAIN_VIEW))
		return -1;
	if (libnormalform_apply_transformation__(this->terms[0]->terms[1], LIBNORMALFORM_IMAGE_VIEW))
		return -1;
	*reduced = 1;
	return 0;

#undef WILL_ELIMINATE
}


#else

TODO_TEST

#endif