1 | PROBLEM EvenParity
|
---|
2 | CODE <<
|
---|
3 | const int N = 5;
|
---|
4 | >>
|
---|
5 |
|
---|
6 | NONTERMINALS
|
---|
7 | Expr<<bool[] d, out bool p>>.
|
---|
8 | AND<<bool[] d, out bool p>>.
|
---|
9 | OR<<bool[] d, out bool p>>.
|
---|
10 | NAND<<bool[] d, out bool p>>.
|
---|
11 | NOR<<bool[] d, out bool p>>.
|
---|
12 |
|
---|
13 | TERMINALS
|
---|
14 | D<<out int val>>
|
---|
15 | CONSTRAINTS
|
---|
16 | val IN SET <<Enumerable.Range(0, N);>>
|
---|
17 | .
|
---|
18 |
|
---|
19 | RULES
|
---|
20 | Expr<<bool[] d, out bool p>> = LOCAL<< int val = 0; >>
|
---|
21 | AND<<d, out p>>
|
---|
22 | | OR<<d, out p>>
|
---|
23 | | NAND<<d, out p>>
|
---|
24 | | NOR<<d, out p>>
|
---|
25 | | D<<out val>> SEM<< p = d[val]; >>
|
---|
26 | .
|
---|
27 |
|
---|
28 | AND<<bool[] d, out bool p>> = LOCAL << bool p1, p2; >>
|
---|
29 | Expr<<d, out p1>> Expr<<d, out p2>> SEM << p = p1 & p2; >>
|
---|
30 | .
|
---|
31 |
|
---|
32 | OR<<bool[] d, out bool p>> = LOCAL << bool p1, p2; >>
|
---|
33 | Expr<<d, out p1>> Expr<<d, out p2>> SEM << p = p1 | p2; >>
|
---|
34 | .
|
---|
35 |
|
---|
36 | NAND<<bool[] d, out bool p>> = LOCAL << bool p1, p2; >>
|
---|
37 | Expr<<d, out p1>> Expr<<d, out p2>> SEM << p = !(p1 & p2); >>
|
---|
38 | .
|
---|
39 |
|
---|
40 | NOR<<bool[] d, out bool p>> = LOCAL << bool p1, p2; >>
|
---|
41 | Expr<<d, out p1>> Expr<<d, out p2>> SEM << p = !(p1 | p2); >>
|
---|
42 | .
|
---|
43 |
|
---|
44 | MINIMIZE
|
---|
45 | <<
|
---|
46 | bool[] d = new bool[N];
|
---|
47 | int mistakes = 0;
|
---|
48 | for(int i = 0; i < Math.Pow(2, N); i++) {
|
---|
49 | // generate bit-vector and count number of 1-bits
|
---|
50 | int x = i;
|
---|
51 | int nBitsOne = 0;
|
---|
52 | for(int j = 0; j < N; j++) {
|
---|
53 | if((x % 2) == 1) {
|
---|
54 | d[N - j - 1] = true;
|
---|
55 | nBitsOne++;
|
---|
56 | } else {
|
---|
57 | d[N - j - 1] = false;
|
---|
58 | }
|
---|
59 | x = (x / 2);
|
---|
60 | }
|
---|
61 | bool target = (nBitsOne % 2) == 0;
|
---|
62 | // apply model
|
---|
63 | bool p;
|
---|
64 | Expr(d, out p);
|
---|
65 |
|
---|
66 | if(p != target) mistakes++;
|
---|
67 | }
|
---|
68 | return mistakes;
|
---|
69 | >>
|
---|
70 | END EvenParity.
|
---|