aboutsummaryrefslogtreecommitdiffstats
path: root/libnormalform_xor2.c
blob: a00c575d3b8e5672801fa7369d5bae2e467f1891 (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
/* See LICENSE file for copyright and license details. */
#ifndef TEST
#include "common.h"


LIBNORMALFORM_SENTENCE *
(libnormalform_xor2)(LIBNORMALFORM_SENTENCE *l, LIBNORMALFORM_SENTENCE *r)
{
	int inv;

	if (!l || !r) {
		libnormalform_free(l);
		libnormalform_free(r);
		return NULL;
	}

        if (l->equals(l, r, &inv)) {
		libnormalform_free(l);
		libnormalform_free(r);

		if (!inv) {
			/* x ⊕ x = 0 */
			return libnormalform_false();

		} else {
			/* x ⊕ ¬x = 1 */
			return libnormalform_true();
		}

	} else if (l->type == TYPE_TRUE) {
		/* 1 ⊕ x = (1 ∨ x) ∧ ¬(1 ∧ x) = 1 ∧ ¬x = ¬x */
		r = libnormalform_not(r);
	return_r:
		libnormalform_free(l);
		return r;

	} else if (l->type == TYPE_FALSE) {
		/* 0 ⊕ x = (0 ∨ x) ∧ ¬(0 ∧ x) = x ∧ ¬0 = x ∧ 1 = x */
		goto return_r;

	} else if (r->type == TYPE_TRUE) {
		/* x ⊕ 1 = 1 ⊕ x = ¬x */
		l = libnormalform_not(l);
	return_l:
		libnormalform_free(r);
		return l;

	} else if (r->type == TYPE_FALSE) {
		/* x ⊕ 0 = 0 ⊕ x = x */
		goto return_l;

	} else {
		return libnormalform_xor2__(l, r);
	}
}


#else

#define USE_TWO
#include "libnormalform_xor.c"

#endif