formula.hh 28.8 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
3
// Copyright (C) 2015 Laboratoire de Recherche et Développement de
// l'Epita (LRDE).
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
4
5
6
7
8
//
// 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
9
// the Free Software Foundation; either version 3 of the License, or
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
10
11
12
13
14
15
16
17
// (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
18
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
19

20
/// \file ltlast/formula.hh
21
/// \brief LTL/PSL formula interface
22
#pragma once
23

24
#include "misc/common.hh"
25
26
27
#include <memory>
#include <cstdint>
#include <initializer_list>
28
#include <cassert>
29
30
31
32
33
#include <vector>
#include <string>
#include <iterator>
#include <iosfwd>
#include <sstream>
34
#include <list>
35
36
#include <cstddef>
#include <initializer_list>
37

38
namespace spot
39
{
40
  namespace ltl
41
  {
42
43
    enum class op: uint8_t
    {
44
45
46
47
      ff,
	tt,
	eword,
	ap,
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
      // unary operators
	Not,
	X,
	F,
	G,
	Closure,
	NegClosure,
	NegClosureMarked,
	// binary operators
	Xor,
	Implies,
	Equiv,
	U,		      ///< until
	R,		      ///< release (dual of until)
	W,		      ///< weak until
	M,		      ///< strong release (dual of weak until)
	EConcat,	      ///< Seq
	EConcatMarked,	      ///< Seq, Marked
	UConcat,	      ///< Triggers
	// n-ary operators
	Or,		      ///< (omega-Rational) Or
	OrRat,		      ///< Rational Or
	And,		      ///< (omega-Rational) And
	AndRat,		      ///< Rational And
	AndNLM,		      ///< Non-Length-Matching Rational-And
	Concat,
	Fusion,
	// star-like operators
	Star,		      ///< Star
	FStar,		      ///< Fustion Star
	};
79

80
81
82
83
84
85
86
87
88
#ifndef SWIG
    class SPOT_API fnode final
    {
    public:
      const fnode* clone() const
      {
	++refs_;
	return this;
      }
89

90
91
92
93
94
95
96
97
      void destroy() const
      {
	// last reference to a node that is not a constant?
	if (SPOT_UNLIKELY(!refs_ && id_ > 2))
	  destroy_aux();
	else
	  --refs_;
      }
98

99
100
101
102
      static constexpr uint8_t unbounded()
      {
	return UINT8_MAX;
      }
103

104
105
106
107
108
109
      static const fnode* ap(const std::string& name);
      static const fnode* unop(op o, const fnode* f);
      static const fnode* binop(op o, const fnode* f, const fnode* g);
      static const fnode* multop(op o, std::vector<const fnode*> l);
      static const fnode* bunop(op o, const fnode* f,
				  uint8_t min, uint8_t max = unbounded());
110

111
112
113
114
      op kind() const
      {
	return op_;
      }
115

116
      std::string kindstr() const;
117

118
119
120
121
      bool is(op o) const
      {
	return op_ == o;
      }
122

123
      bool is(op o1, op o2) const
124
      {
125
	return op_ == o1 || op_ == o2;
126
      }
127

128
129
130
131
132
133
134
135
136
137
138
      bool is(std::initializer_list<op> l) const
      {
	const fnode* n = this;
	for (auto o: l)
	  {
	    if (!n->is(o))
	      return false;
	    n = n->nth(0);
	  }
	return true;
      }
139

140
      /// \brief Remove operator \a o and return the child.
141
      ///
142
143
      /// This works only for unary operators.
      const fnode* get_child_of(op o) const
144
      {
145
146
147
148
	if (op_ != o)
	  return nullptr;
	assert(size_ == 1);
	return nth(0);
149
150
      }

151
152
153
154
155
156
157
      /// \brief Remove all operators in \a l and return the child.
      ///
      /// This works only for a list of unary operators.
      /// For instance if \c f  is a formula for XG(a U b),
      /// then <code>f.get_child_of({op::X, op::G})</code>
      /// will return the subformula a U b.
      const fnode* get_child_of(std::initializer_list<op> l) const
158
      {
159
160
	auto c = this;
	for (auto o: l)
161
	  {
162
163
164
	    c = c->get_child_of(o);
	    if (c == nullptr)
	      return c;
165
	  }
166
167
168
169
170
171
172
173
174
175
176
177
178
	return c;
      }

      uint8_t min() const
      {
	assert(op_ == op::FStar || op_ == op::Star);
	return min_;
      }

      uint8_t max() const
      {
	assert(op_ == op::FStar || op_ == op::Star);
	return max_;
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
      uint8_t size() const
      {
	return size_;
      }

      size_t id() const
      {
	return id_;
      }

      const fnode*const* begin() const
      {
	return children;
      }

      const fnode*const* end() const
      {
	return children + size();
      }

      const fnode* nth(unsigned i) const
      {
	if (i >= size())
	  throw std::runtime_error("access to non-existing child");
	return children[i];
      }

      static const fnode* ff()
      {
	return ff_;
      }

213
      bool is_ff() const
214
      {
215
	return op_ == op::ff;
216
217
218
219
220
221
      }

      static const fnode* tt()
      {
	return tt_;
      }
222

223
      bool is_tt() const
224
      {
225
	return op_ == op::tt;
226
227
      }

228
229
230
231
232
233
234
      static const fnode* eword()
      {
	return ew_;
      }

      bool is_eword() const
      {
235
	return op_ == op::eword;
236
237
238
239
      }

      bool is_constant() const
      {
240
	return op_ == op::ff || op_ == op::tt || op_ == op::eword;
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
      }

      bool is_Kleene_star() const
      {
	if (op_ != op::Star)
	  return false;
	return min_ == 0 && max_ == unbounded();
      }

      static const fnode* one_star()
      {
	if (!one_star_)
	  one_star_ = bunop(op::Star, tt(), 0);
	return one_star_;
      }

      const std::string& ap_name() const;
      std::ostream& dump(std::ostream& os) const;

      const fnode* all_but(unsigned i) const;

      unsigned boolean_count() const
      {
	unsigned pos = 0;
	unsigned s = size();
	while (pos < s && children[pos]->is_boolean())
	  ++pos;
	return pos;
      }

      const fnode* boolean_operands(unsigned* width = nullptr) const;

      /// return true if the unicity map contains only the globally
      /// pre-allocated formulas.
      static bool instances_check();

277
278
279
280
281
282
283
      ////////////////
      // Properties //
      ////////////////

      /// Whether the formula use only boolean operators.
      bool is_boolean() const
      {
284
	return is_.boolean;
285
286
287
288
289
      }

      /// Whether the formula use only AND, OR, and NOT operators.
      bool is_sugar_free_boolean() const
      {
290
	return is_.sugar_free_boolean;
291
292
293
294
295
296
297
298
      }

      /// \brief Whether the formula is in negative normal form.
      ///
      /// A formula is in negative normal form if the not operators
      /// occur only in front of atomic propositions.
      bool is_in_nenoform() const
      {
299
	return is_.in_nenoform;
300
301
      }

302
303
      /// Whether the formula is syntactically stutter_invariant
      bool is_syntactic_stutter_invariant() const
304
      {
305
	return is_.syntactic_si;
306
307
      }

308
      /// Whether the formula avoids the F and G operators.
309
310
      bool is_sugar_free_ltl() const
      {
311
	return is_.sugar_free_ltl;
312
313
      }

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
314
      /// Whether the formula uses only LTL operators.
315
316
      bool is_ltl_formula() const
      {
317
	return is_.ltl_formula;
318
319
      }

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
320
      /// Whether the formula uses only PSL operators.
321
322
      bool is_psl_formula() const
      {
323
	return is_.psl_formula;
324
325
      }

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
326
      /// Whether the formula uses only SERE operators.
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
327
328
      bool is_sere_formula() const
      {
329
	return is_.sere_formula;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
330
331
      }

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
332
333
334
335
      /// Whether a SERE describes a finite language, or an LTL
      /// formula uses no temporal operator but X.
      bool is_finite() const
      {
336
	return is_.finite;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
337
338
      }

339
340
341
      /// \brief Whether the formula is purely eventual.
      ///
      /// Pure eventuality formulae are defined in
342
343
344
345
346
347
348
349
350
351
352
353
354
355
      /** \verbatim
          @InProceedings{	  etessami.00.concur,
          author  	= {Kousha Etessami and Gerard J. Holzmann},
          title		= {Optimizing {B\"u}chi Automata},
          booktitle	= {Proceedings of the 11th International Conference on
          		  Concurrency Theory (Concur'2000)},
          pages		= {153--167},
          year		= {2000},
          editor  	= {C. Palamidessi},
          volume  	= {1877},
          series  	= {Lecture Notes in Computer Science},
          publisher	= {Springer-Verlag}
          }
          \endverbatim */
356
357
358
359
360
      ///
      /// A word that satisfies a pure eventuality can be prefixed by
      /// anything and still satisfies the formula.
      bool is_eventual() const
      {
361
	return is_.eventual;
362
363
364
365
366
      }

      /// \brief Whether a formula is purely universal.
      ///
      /// Purely universal formulae are defined in
367
368
369
370
371
372
373
374
375
376
377
378
379
380
      /** \verbatim
          @InProceedings{	  etessami.00.concur,
          author  	= {Kousha Etessami and Gerard J. Holzmann},
          title		= {Optimizing {B\"u}chi Automata},
          booktitle	= {Proceedings of the 11th International Conference on
          		  Concurrency Theory (Concur'2000)},
          pages		= {153--167},
          year		= {2000},
          editor  	= {C. Palamidessi},
          volume  	= {1877},
          series  	= {Lecture Notes in Computer Science},
          publisher	= {Springer-Verlag}
          }
          \endverbatim */
381
382
383
384
385
      ///
      /// Any (non-empty) suffix of a word that satisfies a purely
      /// universal formula also satisfies the formula.
      bool is_universal() const
      {
386
	return is_.universal;
387
388
      }

389
390
391
      /// Whether a PSL/LTL formula is syntactic safety property.
      bool is_syntactic_safety() const
      {
392
	return is_.syntactic_safety;
393
394
395
396
397
      }

      /// Whether a PSL/LTL formula is syntactic guarantee property.
      bool is_syntactic_guarantee() const
      {
398
	return is_.syntactic_guarantee;
399
400
401
402
403
      }

      /// Whether a PSL/LTL formula is syntactic obligation property.
      bool is_syntactic_obligation() const
      {
404
	return is_.syntactic_obligation;
405
406
407
408
409
      }

      /// Whether a PSL/LTL formula is syntactic recurrence property.
      bool is_syntactic_recurrence() const
      {
410
	return is_.syntactic_recurrence;
411
412
413
414
415
      }

      /// Whether a PSL/LTL formula is syntactic persistence property.
      bool is_syntactic_persistence() const
      {
416
	return is_.syntactic_persistence;
417
418
      }

419
420
421
      /// Whether the formula has an occurrence of EConcatMarked.
      bool is_marked() const
      {
422
	return !is_.not_marked;
423
424
      }

425
426
427
      /// Whether the formula accepts [*0].
      bool accepts_eword() const
      {
428
	return is_.accepting_eword;
429
430
      }

431
432
      bool has_lbt_atomic_props() const
      {
433
	return is_.lbt_atomic_props;
434
435
      }

436
437
      bool has_spin_atomic_props() const
      {
438
	return is_.spin_atomic_props;
439
440
      }

441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
    private:
      void setup_props(op o);
      void destroy_aux() const;

      static const fnode* unique(const fnode*);

      // Destruction may only happen via destroy().
      ~fnode() = default;
      // Disallow copies.
      fnode(const fnode&) = delete;
      fnode& operator=(const fnode&) = delete;



      template<class iter>
      fnode(op o, iter begin, iter end)
457
      {
458
459
460
461
462
463
464
465
	size_t s = std::distance(begin, end);
	if (s > (size_t) UINT16_MAX)
	  throw std::runtime_error("too many children for formula");
	size_ = s;
	auto pos = children;
	for (auto i = begin; i != end; ++i)
	  *pos++ = *i;
	setup_props(o);
466
467
      }

468
469
      fnode(op o, std::initializer_list<const fnode*> l)
	: fnode(o, l.begin(), l.end())
470
      {
471
      }
472
473

      fnode(op o, const fnode* f, uint8_t min, uint8_t max)
474
      {
475
476
477
478
479
	size_ = 1;
	children[0] = f;
	min_ = min;
	max_ = max;
	setup_props(o);
480
      }
481

482
483
484
485
      static const fnode* ff_;
      static const fnode* tt_;
      static const fnode* ew_;
      static const fnode* one_star_;
486

487
488
489
490
491
492
493
494
      op op_;			// operator
      uint8_t min_;		// range minimum (for star-like operators)
      uint8_t max_;		// range maximum;
      //uint8_t unused_;
      uint16_t size_;		// number of children
      mutable uint16_t refs_ = 0; // reference count - 1;
      size_t id_;		// Also used as hash.
      static size_t next_id_;
495

496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
      struct ltl_prop
      {
	// All properties here should be expressed in such a a way
	// that property(f && g) is just property(f)&property(g).
	// This allows us to compute all properties of a compound
	// formula in one operation.
	//
	// For instance we do not use a property that says "has
	// temporal operator", because it would require an OR between
	// the two arguments.  Instead we have a property that
	// says "no temporal operator", and that one is computed
	// with an AND between the arguments.
	//
	// Also choose a name that makes sense when prefixed with
	// "the formula is".
	bool boolean:1;		   // No temporal operators.
	bool sugar_free_boolean:1; // Only AND, OR, and NOT operators.
513
	bool in_nenoform:1;	   // Negative Normal Form.
514
	bool syntactic_si:1;	   // LTL-X or siPSL
515
516
517
	bool sugar_free_ltl:1;	   // No F and G operators.
	bool ltl_formula:1;	   // Only LTL operators.
	bool psl_formula:1;	   // Only PSL operators.
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
518
	bool sere_formula:1;	   // Only SERE operators.
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
519
	bool finite:1;		   // Finite SERE formulae, or Bool+X forms.
520
521
	bool eventual:1;	   // Purely eventual formula.
	bool universal:1;	   // Purely universal formula.
522
523
524
525
526
	bool syntactic_safety:1;   // Syntactic Safety Property.
	bool syntactic_guarantee:1;   // Syntactic Guarantee Property.
	bool syntactic_obligation:1;  // Syntactic Obligation Property.
	bool syntactic_recurrence:1;  // Syntactic Recurrence Property.
	bool syntactic_persistence:1; // Syntactic Persistence Property.
527
528
	bool not_marked:1;	   // No occurrence of EConcatMarked.
	bool accepting_eword:1;	   // Accepts the empty word.
529
	bool lbt_atomic_props:1;   // Use only atomic propositions like p42.
530
	bool spin_atomic_props:1;  // Use only spin-compatible atomic props.
531
532
533
534
535
      };
      union
      {
	// Use an unsigned for fast computation of all properties.
	unsigned props;
536
	ltl_prop is_;
537
      };
538

539
540
      const fnode* children[1];
    };
541

542
    /// Order two atomic propositions.
543
    SPOT_API
544
    int atomic_prop_cmp(const fnode* f, const fnode* g);
545

546
547
    /// \brief Strict Weak Ordering for <code>const fnode*</code>
    ///        inside n-ary operators
548
549
550
    /// \ingroup ltl_essentials
    ///
    /// This is the comparison functor used by to order the
551
    /// n-ary operands.  It keeps Boolean formulae first in
552
553
554
    /// order to speed up implication checks.
    ///
    /// Also keep literal alphabetically ordered.
555
    struct formula_ptr_less_than_bool_first
556
557
    {
      bool
558
      operator()(const fnode* left, const fnode* right) const
559
560
561
562
563
564
565
566
567
568
569
570
571
572
      {
	assert(left);
	assert(right);
	if (left == right)
	  return false;

	// We want Boolean formulae first.
	bool lib = left->is_boolean();
	if (lib != right->is_boolean())
	  return lib;

	// We have two Boolean formulae
	if (lib)
	  {
573
574
	    bool lconst = left->is_constant();
	    if (lconst != right->is_constant())
575
576
	      return lconst;
	    if (!lconst)
577
	      {
578
579
580
581
		auto get_literal = [](const fnode* f) -> const fnode*
		  {
		    if (f->is(op::Not))
		      f = f->nth(0);
582
		    if (f->is(op::ap))
583
584
585
		      return f;
		    return nullptr;
		  };
586
		// Literals should come first
587
588
		const fnode* litl = get_literal(left);
		const fnode* litr = get_literal(right);
589
590
591
592
593
594
595
596
597
		if (!litl != !litr)
		  return litl;
		if (litl)
		  {
		    // And they should be sorted alphabetically
		    int cmp = atomic_prop_cmp(litl, litr);
		    if (cmp)
		      return cmp < 0;
		  }
598
599
600
	      }
	  }

601
602
	size_t l = left->id();
	size_t r = right->id();
603
	if (l != r)
604
	  return l < r;
605
606
607
608
609
610
	// Because the hash code assigned to each formula is the
	// number of formulae constructed so far, it is very unlikely
	// that we will ever reach a case were two different formulae
	// have the same hash.  This will happen only ever with have
	// produced 256**sizeof(size_t) formulae (i.e. max_count has
	// looped back to 0 and started over).  In that case we can
611
	// order two formulas by looking at their text representation.
612
613
614
	// We could be more efficient and look at their AST, but it's
	// not worth the burden.  (Also ordering pointers is ruled out
	// because it breaks the determinism of the implementation.)
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
	std::ostringstream old;
	left->dump(old);
	std::ostringstream ord;
	right->dump(ord);
	return old.str() < ord.str();
      }
    };

#endif // SWIG

    class SPOT_API formula final
    {
      const fnode* ptr_;
    public:
      explicit formula(const fnode* f) noexcept
	: ptr_(f)
      {
      }

      formula(std::nullptr_t) noexcept
	: ptr_(nullptr)
      {
      }

      formula() noexcept
	: ptr_(nullptr)
      {
      }

      formula(const formula& f) noexcept
	: ptr_(f.ptr_)
      {
	if (ptr_)
	  ptr_->clone();
      }

      formula(formula&& f) noexcept
	: ptr_(f.ptr_)
      {
	f.ptr_ = nullptr;
      }

      ~formula()
      {
	if (ptr_)
	  ptr_->destroy();
      }

      const formula& operator=(std::nullptr_t)
      {
	this->~formula();
	ptr_ = nullptr;
	return *this;
      }

      const formula& operator=(const formula& f)
      {
	this->~formula();
	if ((ptr_ = f.ptr_))
	  ptr_->clone();
	return *this;
      }

      const formula& operator=(formula&& f) noexcept
      {
	std::swap(f.ptr_, ptr_);
	return *this;
      }

      bool operator<(const formula& other) const noexcept
      {
686
687
688
689
	if (SPOT_UNLIKELY(!other.ptr_))
	  return false;
	if (SPOT_UNLIKELY(!ptr_))
	  return true;
690
691
692
693
694
	return id() < other.id();
      }

      bool operator<=(const formula& other) const noexcept
      {
695
696
697
698
	if (SPOT_UNLIKELY(!other.ptr_))
	  return !ptr_;
	if (SPOT_UNLIKELY(!ptr_))
	  return true;
699
700
701
702
703
	return id() <= other.id();
      }

      bool operator>(const formula& other) const noexcept
      {
704
705
706
707
	if (SPOT_UNLIKELY(!ptr_))
	  return false;
	if (SPOT_UNLIKELY(!other.ptr_))
	  return true;
708
709
710
711
712
	return id() > other.id();
      }

      bool operator>=(const formula& other) const noexcept
      {
713
714
715
716
	if (SPOT_UNLIKELY(!ptr_))
	  return !!other.ptr_;
	if (SPOT_UNLIKELY(!other.ptr_))
	  return true;
717
718
719
720
721
	return id() >= other.id();
      }

      bool operator==(const formula& other) const noexcept
      {
722
	return other.ptr_ == ptr_;
723
724
725
726
727
728
729
730
731
      }

      bool operator==(std::nullptr_t) const noexcept
      {
	return ptr_ == nullptr;
      }

      bool operator!=(const formula& other) const noexcept
      {
732
	return other.ptr_ != ptr_;
733
734
735
736
737
738
739
      }

      bool operator!=(std::nullptr_t) const noexcept
      {
	return ptr_ != nullptr;
      }

740
741
742
743
744
      operator bool() const
      {
	return ptr_ != nullptr;
      }

745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
      /////////////////////////
      // Forwarded functions //
      /////////////////////////

      static constexpr uint8_t unbounded()
      {
	return fnode::unbounded();
      }

      static formula ap(const std::string& name)
      {
	return formula(fnode::ap(name));
      }

      static formula unop(op o, const formula& f)
      {
	return formula(fnode::unop(o, f.ptr_->clone()));
      }

#ifndef SWIG
      static formula unop(op o, formula&& f)
      {
	return formula(fnode::unop(o, f.to_node_()));
      }
#endif // !SWIG

#ifdef SWIG
#define SPOT_DEF_UNOP(Name)			\
      static formula Name(const formula& f)	\
      {						\
	return unop(op::Name, f);		\
      }
#else // !SWIG
#define SPOT_DEF_UNOP(Name)			\
      static formula Name(const formula& f)	\
      {						\
	return unop(op::Name, f);		\
      }						\
      static formula Name(formula&& f)		\
      {						\
	return unop(op::Name, std::move(f));	\
      }
#endif // !SWIG
      SPOT_DEF_UNOP(Not);
      SPOT_DEF_UNOP(X);
      SPOT_DEF_UNOP(F);
      SPOT_DEF_UNOP(G);
      SPOT_DEF_UNOP(Closure);
      SPOT_DEF_UNOP(NegClosure);
      SPOT_DEF_UNOP(NegClosureMarked);
#undef SPOT_DEF_UNOP

      static formula binop(op o, const formula& f, const formula& g)
      {
	return formula(fnode::binop(o, f.ptr_->clone(), g.ptr_->clone()));
      }

#ifndef SWIG
      static formula binop(op o, const formula& f, formula&& g)
      {
	return formula(fnode::binop(o, f.ptr_->clone(), g.to_node_()));
      }

      static formula binop(op o, formula&& f, const formula& g)
      {
	return formula(fnode::binop(o, f.to_node_(), g.ptr_->clone()));
      }

      static formula binop(op o, formula&& f, formula&& g)
      {
	return formula(fnode::binop(o, f.to_node_(), g.to_node_()));
      }
#endif //SWIG

#ifdef SWIG
#define SPOT_DEF_BINOP(Name)					\
      static formula Name(const formula& f, const formula& g)	\
      {								\
	return binop(op::Name, f, g);				\
      }
#else // !SWIG
#define SPOT_DEF_BINOP(Name)					\
      static formula Name(const formula& f, const formula& g)	\
      {								\
	return binop(op::Name, f, g);				\
      }								\
      static formula Name(const formula& f, formula&& g)	\
      {								\
	return binop(op::Name, f, std::move(g));		\
      }								\
      static formula Name(formula&& f, const formula& g)	\
      {								\
	return binop(op::Name, std::move(f), g);		\
      }								\
      static formula Name(formula&& f, formula&& g)		\
      {								\
	return binop(op::Name, std::move(f), std::move(g));	\
      }
#endif // !SWIG
      SPOT_DEF_BINOP(Xor);
      SPOT_DEF_BINOP(Implies);
      SPOT_DEF_BINOP(Equiv);
      SPOT_DEF_BINOP(U);
      SPOT_DEF_BINOP(R);
      SPOT_DEF_BINOP(W);
      SPOT_DEF_BINOP(M);
      SPOT_DEF_BINOP(EConcat);
      SPOT_DEF_BINOP(EConcatMarked);
      SPOT_DEF_BINOP(UConcat);
#undef SPOT_DEF_BINOP

      static formula multop(op o, const std::vector<formula>& l)
      {
	std::vector<const fnode*> tmp;
	tmp.reserve(l.size());
	for (auto f: l)
	  if (f.ptr_)
	    tmp.push_back(f.ptr_->clone());
	return formula(fnode::multop(o, std::move(tmp)));
      }

#ifndef SWIG
      static formula multop(op o, std::vector<formula>&& l)
      {
	std::vector<const fnode*> tmp;
	tmp.reserve(l.size());
	for (auto f: l)
	  if (f.ptr_)
	    tmp.push_back(f.to_node_());
	return formula(fnode::multop(o, std::move(tmp)));
      }
#endif // !SWIG

#ifdef SWIG
#define SPOT_DEF_MULTOP(Name)					\
      static formula Name(const std::vector<formula>& l)	\
      {								\
	return multop(op::Name, l);				\
      }
#else // !SWIG
#define SPOT_DEF_MULTOP(Name)					\
      static formula Name(const std::vector<formula>& l)	\
      {								\
	return multop(op::Name, l);				\
      }								\
								\
      static formula Name(std::vector<formula>&& l)		\
      {								\
	return multop(op::Name, std::move(l));			\
      }
#endif // !SWIG
      SPOT_DEF_MULTOP(Or);
      SPOT_DEF_MULTOP(OrRat);
      SPOT_DEF_MULTOP(And);
      SPOT_DEF_MULTOP(AndRat);
      SPOT_DEF_MULTOP(AndNLM);
      SPOT_DEF_MULTOP(Concat);
      SPOT_DEF_MULTOP(Fusion);
#undef SPOT_DEF_MULTOP

      static formula bunop(op o, const formula& f,
			   uint8_t min = 0U,
			   uint8_t max = unbounded())
      {
	return formula(fnode::bunop(o, f.ptr_->clone(), min, max));
      }

#ifndef SWIG
      static formula bunop(op o, formula&& f,
			   uint8_t min = 0U,
			   uint8_t max = unbounded())
      {
	return formula(fnode::bunop(o, f.to_node_(), min, max));
      }
#endif // !SWIG

#if SWIG
#define SPOT_DEF_BUNOP(Name)				\
      static formula Name(const formula& f,		\
			  uint8_t min = 0U,		\
			  uint8_t max = unbounded())	\
      {							\
	return bunop(op::Name, f, min, max);		\
      }
#else // !SWIG
#define SPOT_DEF_BUNOP(Name)				\
      static formula Name(const formula& f,		\
			  uint8_t min = 0U,		\
			  uint8_t max = unbounded())	\
      {							\
	return bunop(op::Name, f, min, max);		\
      }							\
      static formula Name(formula&& f,			\
			  uint8_t min = 0U,		\
			  uint8_t max = unbounded())	\
      {							\
	return bunop(op::Name, std::move(f), min, max);	\
      }
#endif
      SPOT_DEF_BUNOP(Star);
      SPOT_DEF_BUNOP(FStar);
#undef SPOT_DEF_BUNOP

      static formula sugar_goto(const formula& b, uint8_t min, uint8_t max);

      static formula sugar_equal(const formula& b, uint8_t min, uint8_t max);

#ifndef SWIG
      const fnode* to_node_()
      {
	auto tmp = ptr_;
	ptr_ = nullptr;
	return tmp;
      }
#endif

      op kind() const
      {
	return ptr_->kind();
      }

      std::string kindstr() const
      {
	return ptr_->kindstr();
      }

      bool is(op o) const
      {
	return ptr_->is(o);
      }

976
#ifndef SWIG
977
978
979
980
981
982
983
984
985
      bool is(op o1, op o2) const
      {
	return ptr_->is(o1, o2);
      }

      bool is(std::initializer_list<op> l) const
      {
	return ptr_->is(l);
      }
986
#endif
987
988
989
990
991
992
993
994
995

      formula get_child_of(op o) const
      {
	auto f = ptr_->get_child_of(o);
	if (f)
	  f->clone();
	return formula(f);
      }

996
#ifndef SWIG
997
998
999
1000
1001
1002
1003
      formula get_child_of(std::initializer_list<op> l) const
      {
	auto f = ptr_->get_child_of(l);
	if (f)
	  f->clone();
	return formula(f);
      }
1004
#endif
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025

      uint8_t min() const
      {
	return ptr_->min();
      }

      uint8_t max() const
      {
	return ptr_->max();
      }

      uint8_t size() const
      {
	return ptr_->size();
      }

      size_t id() const
      {
	return ptr_->id();
      }

1026
#ifndef SWIG
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
      class SPOT_API formula_child_iterator final
      {
	const fnode*const* ptr_;
      public:
	formula_child_iterator()
	  : ptr_(nullptr)
	{
	}

	formula_child_iterator(const fnode*const* f)
	  : ptr_(f)
	{
	}

	bool operator==(formula_child_iterator o)
	{
	  return ptr_ == o.ptr_;
	}

	bool operator!=(formula_child_iterator o)
	{
	  return ptr_ != o.ptr_;
	}

	formula operator*()
	{
	  return formula((*ptr_)->clone());
	}

	formula_child_iterator operator++()
	{
	  ++ptr_;
	  return *this;
	}

	formula_child_iterator operator++(int)
	{
	  auto tmp = *this;
	  ++ptr_;
	  return tmp;
	}
      };

      formula_child_iterator begin() const
      {
	return ptr_->begin();
      }

      formula_child_iterator end() const
      {
	return ptr_->end();
      }

1080
      formula operator[](unsigned i) const
1081
1082
1083
      {
	return formula(ptr_->nth(i)->clone());
      }
1084
#endif
1085
1086
1087
1088
1089
1090

      static formula ff()
      {
	return formula(fnode::ff());
      }

1091
      bool is_ff() const
1092
      {
1093
	return ptr_->is_ff();
1094
1095
1096
1097
1098
1099
1100
      }

      static formula tt()
      {
	return formula(fnode::tt());
      }

1101
      bool is_tt() const
1102
      {
1103
	return ptr_->is_tt();
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
      }

      static formula eword()
      {
	return formula(fnode::eword());
      }

      bool is_eword() const
      {
	return ptr_->is_eword();
      }

      bool is_constant() const
      {
	return ptr_->is_constant();
      }

      bool is_Kleene_star() const
      {
	return ptr_->is_Kleene_star();
      }

      static formula one_star()
      {
	return formula(fnode::one_star()->clone());
      }

      bool is_literal()
      {
1133
	return (is(op::ap) ||
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
		// If f is in nenoform, Not can only occur in front of
		// an atomic proposition.  So this way we do not have
		// to check the type of the child.
		(is(op::Not) && is_boolean() && is_in_nenoform()));
      }

      const std::string& ap_name() const
      {
	return ptr_->ap_name();
      }

      std::ostream& dump(std::ostream& os) const
      {
	return ptr_->dump(os);
      }

      formula all_but(unsigned i) const
      {
	return formula(ptr_->all_but(i));
      }

      unsigned boolean_count() const
      {
	return ptr_->boolean_count();
      }

      formula boolean_operands(unsigned* width = nullptr) const
      {
	return formula(ptr_->boolean_operands(width));
      }

#define SPOT_DEF_PROP(Name)			\
      bool Name() const				\
      {						\
	return ptr_->Name();			\
      }
      SPOT_DEF_PROP(is_boolean);
      SPOT_DEF_PROP(is_sugar_free_boolean);
      SPOT_DEF_PROP(is_in_nenoform);
      SPOT_DEF_PROP(is_syntactic_stutter_invariant);
      SPOT_DEF_PROP(is_sugar_free_ltl);
      SPOT_DEF_PROP(is_ltl_formula);
      SPOT_DEF_PROP(is_psl_formula);
      SPOT_DEF_PROP(is_sere_formula);
      SPOT_DEF_PROP(is_finite);
      SPOT_DEF_PROP(is_eventual);
      SPOT_DEF_PROP(is_universal);
      SPOT_DEF_PROP(is_syntactic_safety);
      SPOT_DEF_PROP(is_syntactic_guarantee);
      SPOT_DEF_PROP(is_syntactic_obligation);
      SPOT_DEF_PROP(is_syntactic_recurrence);
      SPOT_DEF_PROP(is_syntactic_persistence);
      SPOT_DEF_PROP(is_marked);
      SPOT_DEF_PROP(accepts_eword);
      SPOT_DEF_PROP(has_lbt_atomic_props);
      SPOT_DEF_PROP(has_spin_atomic_props);
#undef SPOT_DEF_PROP

      template<typename Trans>
      formula map(Trans trans)
      {
	switch (op o = kind())
	  {
1197
1198
1199
1200
	  case op::ff:
	  case op::tt:
	  case op::eword:
	  case op::ap:
1201
1202
1203
1204
1205
1206
1207
1208
	    return *this;
	  case op::Not:
	  case op::X:
	  case op::F:
	  case op::G:
	  case op::Closure:
	  case op::NegClosure:
	  case op::NegClosureMarked:
1209
	    return unop(o, trans((*this)[0]));
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
	  case op::Xor:
	  case op::Implies:
	  case op::Equiv:
	  case op::U:
	  case op::R:
	  case op::W:
	  case op::M:
	  case op::EConcat:
	  case op::EConcatMarked:
	  case op::UConcat:
	    {
1221
1222
	      formula tmp = trans((*this)[0]);
	      return binop(o, tmp, trans((*this)[1]));
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
	    }
	  case op::Or:
	  case op::OrRat:
	  case op::And:
	  case op::AndRat:
	  case op::AndNLM:
	  case op::Concat:
	  case op::Fusion:
	    {
	      std::vector<formula> tmp;
	      tmp.reserve(size());
	      for (auto f: *this)
		tmp.push_back(trans(f));
	      return multop(o, std::move(tmp));
	    }
	  case op::Star:
	  case op::FStar:
1240
	    return bunop(o, trans((*this)[0]), min(), max());
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
	  }
	SPOT_UNREACHABLE();
      }

      template<typename Func>
      void traverse(Func func)
      {
	if (func(*this))
	  return;
	for (auto f: *this)
	  f.traverse(func);
1252
      }
1253
1254
    };

1255
    /// Print the properties of formula \a f on stream \a out.
1256
    SPOT_API
1257
    std::ostream& print_formula_props(std::ostream& out, const formula& f,
1258
				      bool abbreviated = false);
1259

1260
    /// List the properties of formula \a f.
1261
    SPOT_API
1262
    std::list<std::string> list_formula_props(const formula& f);
1263
1264
1265
  }
}

1266
1267
1268
1269
#ifndef SWIG
namespace std
{
  template <>
1270
  struct hash<spot::ltl::formula>
1271
  {
1272
    size_t operator()(const spot::ltl::formula& x) const noexcept
1273
    {
1274
      return x.id();
1275
1276
1277
1278
    }
  };
}
#endif