multop.hh 10.3 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
3
// Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Laboratoire de
// Recherche et Développement de l'Epita (LRDE).
Guillaume Sadegh's avatar
Guillaume Sadegh committed
4
// Copyright (C) 2003, 2004 Laboratoire d'Informatique de Paris
5
6
// 6 (LIP6), département Systèmes Répartis Coopératifs (SRC),
// Université Pierre et Marie Curie.
7
8
9
10
11
//
// This file is part of Spot, a model checking library.
//
// Spot is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
12
// the Free Software Foundation; either version 3 of the License, or
13
14
15
16
17
18
19
20
// (at your option) any later version.
//
// Spot is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General Public License
21
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
22

23
24
25
26
/// \file ltlast/multop.hh
/// \brief LTL multi-operand operators
#ifndef SPOT_LTLAST_MULTOP_HH
# define SPOT_LTLAST_MULTOP_HH
27

28
#include "refformula.hh"
29
30
#include <vector>
#include <map>
31
#include <iosfwd>
32
33
34

namespace spot
{
35
  namespace ltl
36
37
  {

38
    /// \ingroup ltl_ast
39
    /// \brief Multi-operand operators.
40
    class SPOT_API multop : public ref_formula
41
42
    {
    public:
43
      enum type { Or, OrRat, And, AndRat, AndNLM, Concat, Fusion };
44
45

      /// List of formulae.
46
      typedef std::vector<const formula*> vec;
47

48
      /// \brief Build a spot::ltl::multop with two children.
49
      ///
50
      /// If one of the children itself is a spot::ltl::multop
51
52
      /// with the same type, it will be inlined.  I.e., children
      /// of that child will be added, and that child itself will
53
      /// be destroyed.  This allows incremental building of
54
      /// n-ary ltl::multop.
55
      ///
56
      /// This functions can perform slight optimizations and
57
58
      /// may not return an ltl::multop object. See the other
      /// instance function for the list of rewritings.
59
60
      static const formula*
      instance(type op, const formula* first, const formula* second);
61
62

      /// \brief Build a spot::ltl::multop with many children.
63
64
      ///
      /// Same as the other instance() function, but take a vector of
65
      /// formulae as argument.  This vector is acquired by the
66
67
      /// spot::ltl::multop class, the caller should allocate it with
      /// \c new, but not use it (especially not destroy it) after it
68
69
      /// has been passed to spot::ltl::multop.  Inside the vector,
      /// null pointers are ignored.
70
      ///
71
72
73
74
75
      /// Most operators (Or, OrRat, And, AndRat, Concat) are
      /// associative, and are automatically inlined.  Or, OrRat, And,
      /// and AndRat are commutative, so their arguments are also
      /// sorted, to ensure that "a & b" is equal to "b & a", also
      /// duplicate arguments are removed.
76
77
78
79
80
      ///
      /// Furthermore this function can perform slight optimizations
      /// and may not return an ltl::multop object.  For instance if
      /// the vector contains only one unique element, this this
      /// formula will be returned as-is.  Neutral and absorbent element
81
      /// are also taken care of.  The following rewritings are performed
82
83
84
85
86
      /// (the left patterns are rewritten as shown on the right):
      ///
      /// - And(Exps1...,1,Exps2...) = And(Exps1...,Exps2...)
      /// - And(Exps1...,0,Exps2...) = 0
      /// - And(Exp) = Exp
87
88
89
90
91
92
93
      /// - Or(Exps1...,1,Exps2...) = 1
      /// - Or(Exps1...,0,Exps2...) = Or(Exps1...,Exps2...)
      /// - Or(Exp) = Exp
      /// - AndNLM(FExps1...,1,Exps2...) = AndNLM(Exps2...)
      ///     if Fexps1... accept [*0], and Exps2... don't.
      /// - AndNLM(FExps1...,1,FExps2...) = 1
      ///     if Fexps1...,FExps2... all accept[*0].
94
95
96
      /// - AndNLM(Exps1...,0,Exps2...) = 0
      /// - AndNLM(Exps1...,[*0],Exps2...) = AndNLM(Exps1...,Exps2...)
      /// - AndNLM(Exp) = Exp
97
98
      /// - AndNLM(Exps1...,BoolExp1,Exps2...,BoolExp2,Exps3...) =
      ///    AndNLM(Exps1...,Exps2...,Exps3...,And(BoolExp1,BoolExp2))
99
100
101
102
103
      /// - AndRat(Exps1...,0,Exps2...) = 0
      /// - AndRat(Exps1...,BoolExp1,Exps2...,BoolExps2...) =
      ///    AndRat(Exps1...,Exps2...,And(BoolExp1,BoolExps2...))
      /// - AndRat(Exps1...,[*0],Exps2...) = [*0] if all Expi accept [*0]
      /// - AndRat(Exps1...,[*0],Exps2...) = 0 if some Expi reject [*0]
104
      /// - AndRat(Exps1...,1[*],Exps2...) = AndRat(Exps1...,Exps2...)
105
106
107
      /// - OrRat(Exps1...,0,Exps2...) = OrRat(Exps1...,Exps2...)
      /// - OrRat(Exps1...,BoolExp1,Exps2...,BoolExps2...) =
      ///    OrRat(Exps1...,Exps2...,Or(BoolExp1,BoolExps2...))
108
      /// - OrRat(Exps1...,1[*],Exps2...) = 1[*]
109
      /// - Concat(Exps1...,0,Exps2...) = 0
110
      /// - Concat(Exps1...,[*0],Exps2...) = Concat(Exps1...,Exps2...)
111
112
      /// - Concat(Exps1...,FExps2...,1[*],FExps3...,Exps4) =
      ///     Concat(Exps1...,1[*],Exps4) if FExps2...FExps3... all accept [*0]
113
      /// - Concat(Exp) = Exp
114
115
      /// - Concat(Exps1...,E,E[*i..j],E[*k..l],Exps2...) =
      ///     Concat(Exps1...,E[*1+i+k..j+l],Exps2...)  and similar forms
116
117
      /// - Fusion(Exps1...1,Exps2...) = Fusion(Exps1...,Exps2...)
      ///     if at least one exp reject [*0]
118
119
120
      /// - Fusion(Exps1...,0,Exps2...) = 0
      /// - Fusion(Exps1...,[*0],Exps2...) = 0
      /// - Fusion(Exp) = Exp
121
      /// - Fusion(Exps1...,BoolExp1...BoolExpN,Exps2,Exps3...) =
122
      ///     Fusion(Exps1...,And(BoolExp1...BoolExpN),Exps2,Exps3...)
123
      static const formula* instance(type op, vec* v);
124

125
      virtual void accept(visitor& v) const;
126
127

      /// Get the number of children.
128
129
130
131
132
      unsigned size() const
      {
	return children_->size();
      }

133
      /// \brief Get the nth child.
134
135
      ///
      /// Starting with \a n = 0.
136
137
138
139
      const formula* nth(unsigned n) const
      {
	return (*children_)[n];
      }
140

141
142
      /// \brief construct a formula without the nth child.
      ///
143
      /// If the formula \c f is <code>a|b|c|d</code> and <code>c</code>
144
145
      /// is child number 2, then calling <code>f->all_but(2)</code> will
      /// return a new formula <code>a|b|d</code>.
146
      const formula* all_but(unsigned n) const;
147

148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
      /// \brief return the number of Boolean operands in the binop.
      ///
      /// For instance if \c f <code>a|b|Xc|Gd</code>, this
      /// returns 2.
      unsigned boolean_count() const;

      /// \brief return the Boolean part of the binop.
      ///
      /// For instance if \c f <code>a|b|Xc|Gd</code>, this
      /// returns <code>a|b</code>.  Return 0 if there is no
      /// Boolean operand.
      ///
      /// If \a width is not null, it is filled with the number
      /// of Boolean operands extracted (i.e., the result
      /// of boolean_count())
      const formula* boolean_operands(unsigned* width = 0) const;

165
      /// Get the type of this operator.
166
167
168
169
170
      type op() const
      {
	return op_;
      }

171
172
173
      /// Get the type of this operator, as a string.
      const char* op_name() const;

174
175
176
      /// Return a canonic representation of the atomic proposition
      virtual std::string dump() const;

177
178
179
      /// Number of instantiated multi-operand operators.  For debugging.
      static unsigned instance_count();

180
181
182
      /// Dump all instances.  For debugging.
      static std::ostream& dump_instances(std::ostream& os);

183
    protected:
184
      typedef std::pair<type, vec*> key;
185
      /// Comparison functor used internally by ltl::multop.
186
187
188
      struct paircmp
      {
	bool
189
	operator()(const key& p1, const key& p2) const
190
191
192
193
194
195
	{
	  if (p1.first != p2.first)
	    return p1.first < p2.first;
	  return *p1.second < *p2.second;
	}
      };
196
      typedef std::map<key, const multop*, paircmp> map;
197
198
199
200
201
202
203
204
205
206
      static map instances;

      multop(type op, vec* v);
      virtual ~multop();

    private:
      type op_;
      vec* children_;
    };

207
208
209
210
211
212

    /// \brief Cast \a f into a multop.
    ///
    /// Cast \a f into a multop iff it is a multop instance.  Return 0
    /// otherwise.  This is faster than \c dynamic_cast.
    inline
213
214
    const multop*
    is_multop(const formula* f)
215
216
217
    {
      if (f->kind() != formula::MultOp)
	return 0;
218
      return static_cast<const multop*>(f);
219
220
221
222
223
224
225
    }

    /// \brief Cast \a f into a multop if it has type \a op.
    ///
    /// Cast \a f into a multop iff it is a multop instance with operator \a op.
    /// Returns 0 otherwise.
    inline
226
    const multop*
227
    is_multop(const formula* f, multop::type op)
228
    {
229
230
231
232
      if (const multop* mo = is_multop(f))
	if (mo->op() == op)
	  return mo;
      return 0;
233
234
235
236
    }

    /// \brief Cast \a f into a multop if it has type \a op1 or \a op2.
    ///
237
238
    /// Cast \a f into a multop iff it is a multop instance with
    /// operator \a op1 or \a op2.  Returns 0 otherwise.
239
    inline
240
    const multop*
241
242
    is_multop(const formula* f, multop::type op1, multop::type op2)
    {
243
244
245
246
      if (const multop* mo = is_multop(f))
	if (mo->op() == op1 || mo->op() == op2)
	  return mo;
      return 0;
247
248
249
250
251
252
    }

    /// \brief Cast \a f into a multop if it is an And.
    ///
    /// Return 0 otherwise.
    inline
253
    const multop*
254
    is_And(const formula* f)
255
256
257
258
    {
      return is_multop(f, multop::And);
    }

259
260
261
262
    /// \brief Cast \a f into a multop if it is an AndRat.
    ///
    /// Return 0 otherwise.
    inline
263
    const multop*
264
265
266
267
268
269
270
271
272
    is_AndRat(const formula* f)
    {
      return is_multop(f, multop::AndRat);
    }

    /// \brief Cast \a f into a multop if it is an AndNLM.
    ///
    /// Return 0 otherwise.
    inline
273
    const multop*
274
275
276
277
278
    is_AndNLM(const formula* f)
    {
      return is_multop(f, multop::AndNLM);
    }

279
280
281
282
    /// \brief Cast \a f into a multop if it is an Or.
    ///
    /// Return 0 otherwise.
    inline
283
    const multop*
284
    is_Or(const formula* f)
285
286
287
    {
      return is_multop(f, multop::Or);
    }
288

289
290
291
292
    /// \brief Cast \a f into a multop if it is an OrRat.
    ///
    /// Return 0 otherwise.
    inline
293
    const multop*
294
295
296
297
298
    is_OrRat(const formula* f)
    {
      return is_multop(f, multop::OrRat);
    }

299
300
301
302
    /// \brief Cast \a f into a multop if it is a Concat.
    ///
    /// Return 0 otherwise.
    inline
303
    const multop*
304
305
306
307
308
309
310
311
312
    is_Concat(const formula* f)
    {
      return is_multop(f, multop::Concat);
    }

    /// \brief Cast \a f into a multop if it is a Fusion.
    ///
    /// Return 0 otherwise.
    inline
313
    const multop*
314
315
316
317
    is_Fusion(const formula* f)
    {
      return is_multop(f, multop::Fusion);
    }
318
319
320
  }
}

321
#endif // SPOT_LTLAST_MULTOP_HH