nenoform.cc 3.5 KB
Newer Older
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
#include "nenoform.hh"
#include "ltlast/allnodes.hh"

namespace spot
{
  namespace ltl
  {

    class negative_normal_form_visitor : public const_visitor
    {
    public:
      negative_normal_form_visitor(bool negated)
	: negated_(negated)
      {
      }

      virtual 
      ~negative_normal_form_visitor()
      {
      }

      formula* result() const
      {
	return result_;
      }
      
      void 
      visit(const atomic_prop* ap)
      {
30
	formula* f = new atomic_prop(ap->name(), ap->env());
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
	if (negated_)
	  result_ = new unop(unop::Not, f);
	else
	  result_ = f;
      }

      void 
      visit(const constant* c)
      {
	if (! negated_)
	  {
	    result_ = new constant(c->val());
	    return;
	  }

	switch (c->val())
	  {
	  case constant::True:
	    result_ = new constant(constant::False);
	    return;
	  case constant::False:
	    result_ = new constant(constant::True);
	    return;
	  }
	/* Unreachable code.  */
	assert(0);
      }

      void 
      visit(const unop* uo)
      {
	const formula* f = uo->child();
	switch (uo->op())
	  {
	  case unop::Not:
	    result_ = recurse_(f, negated_ ^ true);
	    return;
	  case unop::X:
	    /* !Xa == X!a */
	    result_ = new unop(unop::X, recurse(f));
	    return;
	  case unop::F:
	    /* !Fa == G!a */
	    result_ = new unop(negated_ ? unop::G : unop::F, recurse(f));
	    return;
	  case unop::G:
	    /* !Ga == F!a */
	    result_ = new unop(negated_ ? unop::F : unop::G, recurse(f));
	    return;
	  }
	/* Unreachable code.  */
	assert(0);
      }

      void 
      visit(const binop* bo)
      {
	const formula* f1 = bo->first();
	const formula* f2 = bo->second();
	switch (bo->op())
	  {
	  case binop::Xor:
	    /* !(a ^ b) == a <=> b */
	    result_ = new binop(negated_ ? binop::Equiv : binop::Xor,
				recurse_(f1, false), recurse_(f2, false));
	    return;
	  case binop::Equiv:
	    /* !(a <=> b) == a ^ b */
	    result_ = new binop(negated_ ? binop::Xor : binop::Equiv,
				recurse_(f1, false), recurse_(f2, false));
	    return;
	  case binop::Implies:
	    if (negated_)
	      /* !(a => b) == a & !b */
	      result_ = new multop(multop::And,
				   recurse_(f1, false), recurse_(f2, true));
	    else
	      result_ = new binop(binop::Implies, recurse(f1), recurse(f2));
	    return;
	  case binop::U:
	    /* !(a U b) == !a R !b */
	    result_ = new binop(negated_ ? binop::R : binop::U,
				recurse(f1), recurse(f2));
	    return;
	  case binop::R:
	    /* !(a R b) == !a U !b */
	    result_ = new binop(negated_ ? binop::U : binop::R,
				recurse(f1), recurse(f2));
	    return;
	  }
	/* Unreachable code.  */
	assert(0);
      }

      void 
      visit(const multop* mo)
      {
	/* !(a & b & c) == !a | !b | !c  */
	/* !(a | b | c) == !a & !b & !c  */
	multop::type op = mo->op();
	if (negated_)
	  switch (op)
	    {
	    case multop::And:
	      op = multop::Or;
	      break;
	    case multop::Or:
	      op = multop::And;
	      break;
	    }
	multop* res = new multop(op);
	unsigned mos = mo->size();
	for (unsigned i = 0; i < mos; ++i)
	  res->add(recurse(mo->nth(i)));
	result_ = res;
      }
	
      formula* 
      recurse_(const formula* f, bool negated)
      {
	return negative_normal_form(f, negated);
      }

      formula* 
      recurse(const formula* f)
      {
	return recurse_(f, negated_);
      }
      
    protected:
      formula* result_;
      bool negated_;
    };

    formula* 
    negative_normal_form(const formula* f, bool negated)
    {
      negative_normal_form_visitor v(negated);
      f->accept(v);
      return v.result();
    }
  }
}