formulas.cc 42 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// -*- coding: utf-8 -*-
// Copyright (C) 2012-2017 Laboratoire de Recherche et Developpement
// de l'EPITA (LRDE).
//
// 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
// the Free Software Foundation; either version 3 of the License, or
// (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
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include <cmath>

#include <spot/gen/formulas.hh>
#include <spot/tl/exclusive.hh>
#include <spot/tl/relabel.hh>
#include <spot/tl/parse.hh>

#define G_(x) formula::G(x)
#define F_(x) formula::F(x)
#define X_(x) formula::X(x)
#define Not_(x) formula::Not(x)

#define Implies_(x, y) formula::Implies((x), (y))
#define Equiv_(x, y) formula::Equiv((x), (y))
#define And_(x, y) formula::And({(x), (y)})
#define Or_(x, y) formula::Or({(x), (y)})
#define U_(x, y) formula::U((x), (y))

namespace spot
{
  namespace gen
  {
    namespace
    {
44
45
46
47
48
49
50
51
52
53
54
55
      static formula
      LTL_GF_equiv(int n, const std::string& a, const std::string& z)
      {
        std::vector<formula> gfs;
        for (int i = 0; i < n; ++i)
          gfs.emplace_back(formula::G(formula::F(
                  formula::ap(a + std::to_string(i+1)))));

        return formula::Equiv(formula::And(gfs),
                              formula::G(formula::F(formula::ap(z))));
      }

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
      static formula
      ms_example(const char* a, const char* b, int n)
      {
        formula ax = formula::tt();
        formula fb = formula::tt();
        for (int i = n; i > 0; --i)
          {
            std::ostringstream ans;
            ans << a << i;
            ax = And_(formula::ap(ans.str()), X_(ax));
            ans.str("");
            ans << b << i;
            fb = F_(And_(formula::ap(ans.str()), fb));
          }
        return And_(G_(F_(ax)), fb);
      }

      static formula
      ms_phi_h(const char* a, const char* b, int n)
      {
        formula fa = formula::ap(a);
        formula fb = formula::ap(b);
        formula out = formula::ff();
        do
          {
            out = Or_(F_(G_(Or_(fa, fb))), out);
            fa = Not_(fa);
            fb = X_(fb);
          }
        while (n--);
        return out;
      }

      static formula
      ms_phi_rs(const char* a, const char* b, int n, bool r = true)
      {
        formula fgan = [=]() {
          std::ostringstream ans;
          ans << a << n;
          return F_(G_(formula::ap(ans.str())));
        } ();
        formula gfbn = [=]() {
          std::ostringstream ans;
          ans << b << n;
          return G_(F_(formula::ap(ans.str())));
        } ();
        formula top = r ? And_(fgan, gfbn) : Or_(fgan, gfbn);
        if (n == 0)
          return top;
        formula sub = ms_phi_rs(a, b, n - 1, !r);
        return r ? Or_(sub, top) : And_(sub, top);
      }

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
      // G(p_0 & XF(p_1 & XF(p_2 & ... XF(p_n))))
      // This is a generalization of eh-pattern=9
      static formula
      GXF_and_n(std::string name, int n)
      {
        formula result = formula::tt();

        for (; n >= 0; --n)
          {
            std::ostringstream p;
            p << name << n;
            formula f = formula::ap(p.str());
            result = And_(f, X_(F_(result)));
          }
        return G_(result);
      }

      // F(p_0 | XG(p_1 | XG(p_2 | ... XG(p_n))))
      // This the dual of the above
      static formula
      FXG_or_n(std::string name, int n)
      {
        formula result = formula::ff();

        for (; n >= 0; --n)
          {
            std::ostringstream p;
            p << name << n;
            formula f = formula::ap(p.str());
            result = Or_(f, X_(G_(result)));
          }
        return F_(result);
      }

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
174
175
176
177
178
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
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
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
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
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
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
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
      // F(p_1 & F(p_2 & F(p_3 & ... F(p_n))))
      static formula
      E_n(std::string name, int n)
      {
        if (n <= 0)
          return formula::tt();

        formula result = nullptr;

        for (; n > 0; --n)
          {
            std::ostringstream p;
            p << name << n;
            formula f = formula::ap(p.str());
            if (result)
              result = And_(f, result);
            else
              result = f;
            result = F_(result);
          }
        return result;
      }

      // p & X(p & X(p & ... X(p)))
      static formula
      phi_n(std::string name, int n,
            op oper = op::And)
      {
        if (n <= 0)
          return formula::tt();

        formula result = nullptr;
        formula p = formula::ap(name);
        for (; n > 0; --n)
          {
            if (result)
              result = formula::multop(oper, {p, X_(result)});
            else
              result = p;
          }
        return result;
      }

      static formula
      N_n(std::string name, int n)
      {
        return formula::F(phi_n(name, n));
      }

      // p & X(p) & XX(p) & XXX(p) & ... X^n(p)
      static formula
      phi_prime_n(std::string name, int n,
                  op oper = op::And)
      {
        if (n <= 0)
          return formula::tt();

        formula result = nullptr;
        formula p = formula::ap(name);
        for (; n > 0; --n)
          {
            if (result)
              {
                p = X_(p);
                result = formula::multop(oper, {result, p});
              }
            else
              {
                result = p;
              }
          }
        return result;
      }

      static formula
      N_prime_n(std::string name, int n)
      {
        return F_(phi_prime_n(name, n));
      }

      // GF(p_1) & GF(p_2) & ... & GF(p_n)   if conj == true
      // GF(p_1) | GF(p_2) | ... | GF(p_n)   if conj == false
      static formula
      GF_n(std::string name, int n, bool conj = true)
      {
        if (n <= 0)
          return conj ? formula::tt() : formula::ff();

        formula result = nullptr;

        op o = conj ? op::And : op::Or;

        for (int i = 1; i <= n; ++i)
          {
            std::ostringstream p;
            p << name << i;
            formula f = G_(F_(formula::ap(p.str())));

            if (result)
              result = formula::multop(o, {f, result});
            else
              result = f;
          }
        return result;
      }

      // FG(p_1) | FG(p_2) | ... | FG(p_n)   if conj == false
      // FG(p_1) & FG(p_2) & ... & FG(p_n)   if conj == true
      static formula
      FG_n(std::string name, int n, bool conj = false)
      {
        if (n <= 0)
          return conj ? formula::tt() : formula::ff();

        formula result = nullptr;

        op o = conj ? op::And : op::Or;

        for (int i = 1; i <= n; ++i)
          {
            std::ostringstream p;
            p << name << i;
            formula f = F_(G_(formula::ap(p.str())));

            if (result)
              result = formula::multop(o, {f, result});
            else
              result = f;
          }
        return result;
      }

      //  (((p1 OP p2) OP p3)...OP pn)   if right_assoc == false
      //  (p1 OP (p2 OP (p3 OP (... pn)  if right_assoc == true
      static formula
      bin_n(std::string name, int n, op o, bool right_assoc = false)
      {
        if (n <= 0)
          n = 1;

        formula result = nullptr;

        for (int i = 1; i <= n; ++i)
          {
            std::ostringstream p;
            p << name << (right_assoc ? (n + 1 - i) : i);
            formula f = formula::ap(p.str());
            if (!result)
              result = f;
            else if (right_assoc)
              result = formula::binop(o, f, result);
            else
              result = formula::binop(o, result, f);
          }
        return result;
      }

      // (GF(p1)|FG(p2))&(GF(p2)|FG(p3))&...&(GF(pn)|FG(p{n+1}))"
      static formula
      R_n(std::string name, int n)
      {
        if (n <= 0)
          return formula::tt();

        formula pi;

        {
          std::ostringstream p;
          p << name << 1;
          pi = formula::ap(p.str());
        }

        formula result = nullptr;

        for (int i = 1; i <= n; ++i)
          {
            formula gf = G_(F_(pi));
            std::ostringstream p;
            p << name << i + 1;
            pi = formula::ap(p.str());

            formula fg = F_(G_(pi));

            formula f = Or_(gf, fg);

            if (result)
              result = And_(f, result);
            else
              result = f;
          }
        return result;
      }

      // (F(p1)|G(p2))&(F(p2)|G(p3))&...&(F(pn)|G(p{n+1}))"
      static formula
      Q_n(std::string name, int n)
      {
        if (n <= 0)
          return formula::tt();

        formula pi;

        {
          std::ostringstream p;
          p << name << 1;
          pi = formula::ap(p.str());
        }

        formula result = nullptr;

        for (int i = 1; i <= n; ++i)
          {
            formula f = F_(pi);

            std::ostringstream p;
            p << name << i + 1;
            pi = formula::ap(p.str());

            formula g = G_(pi);

            f = Or_(f, g);

            if (result)
              result = And_(f, result);
            else
              result = f;
          }
        return result;
      }

      //  OP(p1) | OP(p2) | ... | OP(Pn) if conj == false
      //  OP(p1) & OP(p2) & ... & OP(Pn) if conj == true
      static formula
      combunop_n(std::string name, int n, op o, bool conj = false)
      {
        if (n <= 0)
          return conj ? formula::tt() : formula::ff();

        formula result = nullptr;

        op cop = conj ? op::And : op::Or;

        for (int i = 1; i <= n; ++i)
          {
            std::ostringstream p;
            p << name << i;
            formula f = formula::unop(o, formula::ap(p.str()));

            if (result)
              result = formula::multop(cop, {f, result});
            else
              result = f;
          }
        return result;
      }

      // !((GF(p1)&GF(p2)&...&GF(pn))->G(q -> F(r)))
      // From "Fast LTL to Büchi Automata Translation" [gastin.01.cav]
      static formula
      fair_response(std::string p, std::string q, std::string r, int n)
      {
        formula fair = GF_n(p, n);
        formula resp = G_(Implies_(formula::ap(q), F_(formula::ap(r))));
        return Not_(Implies_(fair, resp));
      }

      // Builds X(X(...X(p))) with n occurrences of X.
      static formula
      X_n(formula p, int n)
      {
        assert(n >= 0);
        formula res = p;
        while (n--)
          res = X_(res);
        return res;
      }

      // Based on LTLcounter.pl from Kristin Rozier.
      // http://shemesh.larc.nasa.gov/people/kyr/benchmarking_scripts/
      static formula
      ltl_counter(std::string bit, std::string marker, int n, bool linear)
      {
        formula b = formula::ap(bit);
        formula neg_b = Not_(b);
        formula m = formula::ap(marker);
        formula neg_m = Not_(m);

        std::vector<formula> res(4);

        // The marker starts with "1", followed by n-1 "0", then "1" again,
        // n-1 "0", etc.
        if (!linear)
          {
            // G(m -> X(!m)&XX(!m)&XXX(m))          [if n = 3]
            std::vector<formula> v(n);
            for (int i = 0; i + 1 < n; ++i)
              v[i] = X_n(neg_m, i + 1);
            v[n - 1] = X_n(m, n);
            res[0] = And_(m, G_(Implies_(m, formula::And(std::move(v)))));
          }
        else
          {
            // G(m -> X(!m & X(!m X(m))))          [if n = 3]
            formula p = m;
            for (int i = n - 1; i > 0; --i)
              p = And_(neg_m, X_(p));
            res[0] = And_(m, G_(Implies_(m, X_(p))));
          }

        // All bits are initially zero.
        if (!linear)
          {
            // !b & X(!b) & XX(!b)    [if n = 3]
            std::vector<formula> v2(n);
            for (int i = 0; i < n; ++i)
              v2[i] = X_n(neg_b, i);
            res[1] = formula::And(std::move(v2));
          }
        else
          {
            // !b & X(!b & X(!b))     [if n = 3]
            formula p = neg_b;
            for (int i = n - 1; i > 0; --i)
              p = And_(neg_b, X_(p));
            res[1] = p;
          }

#define AndX_(x, y) (linear ? X_(And_((x), (y))) : And_(X_(x), X_(y)))

        // If the least significant bit is 0, it will be 1 at the next time,
        // and other bits stay the same.
        formula Xnm1_b = X_n(b, n - 1);
        formula Xn_b = X_(Xnm1_b);
        res[2] = G_(Implies_(And_(m, neg_b),
                             AndX_(Xnm1_b,
                                   U_(And_(Not_(m), Equiv_(b, Xn_b)), m))));

        // From the least significant bit to the first 0, all the bits
        // are flipped on the next value.  Remaining bits are identical.
        formula Xnm1_negb = X_n(neg_b, n - 1);
        formula Xn_negb = X_(Xnm1_negb);
        res[3] =
          G_(Implies_(And_(m, b),
                      AndX_(Xnm1_negb,
                            U_(And_(And_(b, neg_m), Xn_negb),
                               Or_(m, And_(And_(neg_m, neg_b),
                                           AndX_(Xnm1_b,
                                                 U_(And_(neg_m,
                                                         Equiv_(b, Xn_b)),
                                                    m))))))));
        return formula::And(std::move(res));
      }

      static formula
      ltl_counter_carry(std::string bit, std::string marker,
                        std::string carry, int n, bool linear)
      {
        formula b = formula::ap(bit);
        formula neg_b = Not_(b);
        formula m = formula::ap(marker);
        formula neg_m = Not_(m);
        formula c = formula::ap(carry);
        formula neg_c = Not_(c);

        std::vector<formula> res(6);

        // The marker starts with "1", followed by n-1 "0", then "1" again,
        // n-1 "0", etc.
        if (!linear)
          {
            // G(m -> X(!m)&XX(!m)&XXX(m))          [if n = 3]
            std::vector<formula> v(n);
            for (int i = 0; i + 1 < n; ++i)
              v[i] = X_n(neg_m, i + 1);
            v[n - 1] = X_n(m, n);
            res[0] = And_(m, G_(Implies_(m, formula::And(std::move(v)))));
          }
        else
          {
            // G(m -> X(!m & X(!m X(m))))          [if n = 3]
            formula p = m;
            for (int i = n - 1; i > 0; --i)
              p = And_(neg_m, X_(p));
            res[0] = And_(m, G_(Implies_(m, X_(p))));
          }

        // All bits are initially zero.
        if (!linear)
          {
            // !b & X(!b) & XX(!b)    [if n = 3]
            std::vector<formula> v2(n);
            for (int i = 0; i < n; ++i)
              v2[i] = X_n(neg_b, i);
            res[1] = formula::And(std::move(v2));
          }
        else
          {
            // !b & X(!b & X(!b))     [if n = 3]
            formula p = neg_b;
            for (int i = n - 1; i > 0; --i)
              p = And_(neg_b, X_(p));
            res[1] = p;
          }

        formula Xn_b = X_n(b, n);
        formula Xn_negb = X_n(neg_b, n);

        // If m is 1 and b is 0 then c is 0 and n steps later b is 1.
        res[2] = G_(Implies_(And_(m, neg_b), And_(neg_c, Xn_b)));

        // If m is 1 and b is 1 then c is 1 and n steps later b is 0.
        res[3] = G_(Implies_(And_(m, b), And_(c, Xn_negb)));

        if (!linear)
          {
            // If there's no carry, then all of the bits stay the same
            // n steps later.
            res[4] = G_(Implies_(And_(neg_c, X_(neg_m)),
                                 And_(X_(Not_(c)), Equiv_(X_(b), X_(Xn_b)))));

            // If there's a carry, then add one: flip the bits of b and
            // adjust the carry.
            res[5] = G_(Implies_(c, And_(Implies_(X_(neg_b),
                                                  And_(X_(neg_c), X_(Xn_b))),
                                         Implies_(X_(b),
                                                  And_(X_(c), X_(Xn_negb))))));
          }
        else
          {
            // If there's no carry, then all of the bits stay the same
            // n steps later.
            res[4] = G_(Implies_(And_(neg_c, X_(neg_m)),
                                 X_(And_(Not_(c), Equiv_(b, Xn_b)))));
            // If there's a carry, then add one: flip the bits of b and
            // adjust the carry.
            res[5] = G_(Implies_(c, X_(And_(Implies_(neg_b, And_(neg_c, Xn_b)),
                                            Implies_(b, And_(c, Xn_negb))))));
          }
        return formula::And(std::move(res));
      }

      static formula
      tv_f1(std::string p, std::string q, int n)
      {
        return G_(Implies_(formula::ap(p), phi_prime_n(q, n, op::Or)));
      }

      static formula
      tv_f2(std::string p, std::string q, int n)
      {
        return G_(Implies_(formula::ap(p), phi_n(q, n, op::Or)));
      }

      static formula
      tv_g1(std::string p, std::string q, int n)
      {
        return G_(Implies_(formula::ap(p), phi_prime_n(q, n)));
      }

      static formula
      tv_g2(std::string p, std::string q, int n)
      {
        return G_(Implies_(formula::ap(p), phi_n(q, n)));
      }

      static formula
      tv_uu(std::string name, int n)
      {
        std::ostringstream p;
        p << name << n + 1;
        formula q = formula::ap(p.str());
        formula f = q;

        for (int i = n; i > 0; --i)
          {
            p.str("");
            p << name << i;
            q = formula::ap(p.str());
            f = U_(q, f);
            if (i > 1)
              f = And_(q, f);
          }
        return G_(Implies_(q,  f));
      }

      static void
      bad_number(const char* pattern, int n, int max = 0)
      {
        std::ostringstream err;
        err << "no pattern " << pattern << '=' << n
            << ", supported range is 1..";
        if (max)
          err << max;
        throw std::runtime_error(err.str());
      }

      static formula
      dac_pattern(int n)
      {
        static const char* formulas[] = {
          "[](!p0)",
          "<>p2 -> (!p0 U p2)",
          "[](p1 -> [](!p0))",
          "[]((p1 & !p2 & <>p2) -> (!p0 U p2))",
          "[](p1 & !p2 -> (!p0 W p2))",

          "<>(p0)",
          "!p2 W (p0 & !p2)",
          "[](!p1) | <>(p1 & <>p0)",
          "[](p1 & !p2 -> (!p2 W (p0 & !p2)))",
          "[](p1 & !p2 -> (!p2 U (p0 & !p2)))",

          "(!p0 W (p0 W (!p0 W (p0 W []!p0))))",
          "<>p2 -> ((!p0 & !p2) U (p2 | ((p0 & !p2) U (p2 |"
          " ((!p0 & !p2) U (p2 | ((p0 & !p2) U (p2 | (!p0 U p2)))))))))",
          "<>p1 -> (!p1 U (p1 & (!p0 W (p0 W (!p0 W (p0 W []!p0))))))",
          "[]((p1 & <>p2) -> ((!p0 & !p2) U (p2 | ((p0 & !p2) U (p2 |"
          "((!p0 & !p2) U (p2 | ((p0 & !p2) U (p2 | (!p0 U p2))))))))))",
          "[](p1 -> ((!p0 & !p2) U (p2 | ((p0 & !p2) U (p2 | ((!p0 & !p2) U"
          "(p2 | ((p0 & !p2) U (p2 | (!p0 W p2) | []p0)))))))))",

          "[](p0)",
          "<>p2 -> (p0 U p2)",
          "[](p1 -> [](p0))",
          "[]((p1 & !p2 & <>p2) -> (p0 U p2))",
          "[](p1 & !p2 -> (p0 W p2))",

          "!p0 W p3",
          "<>p2 -> (!p0 U (p3 | p2))",
          "[]!p1 | <>(p1 & (!p0 W p3))",
          "[]((p1 & !p2 & <>p2) -> (!p0 U (p3 | p2)))",
          "[](p1 & !p2 -> (!p0 W (p3 | p2)))",

          "[](p0 -> <>p3)",
          "<>p2 -> (p0 -> (!p2 U (p3 & !p2))) U p2",
          "[](p1 -> [](p0 -> <>p3))",
          "[]((p1 & !p2 & <>p2) -> ((p0 -> (!p2 U (p3 & !p2))) U p2))",
          "[](p1 & !p2 -> ((p0 -> (!p2 U (p3 & !p2))) W p2))",

          "<>p0 -> (!p0 U (p3 & !p0 & X(!p0 U p4)))",
          "<>p2 -> (!p0 U (p2 | (p3 & !p0 & X(!p0 U p4))))",
          "([]!p1) | (!p1 U (p1 & <>p0 -> (!p0 U (p3 & !p0 & X(!p0 U p4)))))",
          "[]((p1 & <>p2) -> (!p0 U (p2 | (p3 & !p0 & X(!p0 U p4)))))",
          "[](p1 -> (<>p0 -> (!p0 U (p2 | (p3 & !p0 & X(!p0 U p4))))))",

          "(<>(p3 & X<>p4)) -> ((!p3) U p0)",
          "<>p2 -> ((!(p3 & (!p2) & X(!p2 U (p4 & !p2)))) U (p2 | p0))",
          "([]!p1) | ((!p1) U (p1 & ((<>(p3 & X<>p4)) -> ((!p3) U p0))))",
          "[]((p1 & <>p2)->((!(p3 & (!p2) & X(!p2 U (p4 & !p2)))) U (p2|p0)))",
          "[](p1 -> (!(p3 & (!p2) & X(!p2 U (p4 & !p2))) U (p2 | p0) |"
          " [](!(p3 & X<>p4))))",

          "[] (p3 & X<> p4 -> X(<>(p4 & <> p0)))",
          "<>p2 -> (p3 & X(!p2 U p4) -> X(!p2 U (p4 & <> p0))) U p2",
          "[] (p1 -> [] (p3 & X<> p4 -> X(!p4 U (p4 & <> p0))))",
          "[] ((p1 & <>p2)->(p3 & X(!p2 U p4) -> X(!p2 U (p4 & <> p0))) U p2)",
          "[] (p1 -> (p3 & X(!p2 U p4) -> X(!p2 U (p4 & <> p0))) U (p2 |"
          "[] (p3 & X(!p2 U p4) -> X(!p2 U (p4 & <> p0)))))",

          "[] (p0 -> <>(p3 & X<>p4))",
          "<>p2 -> (p0 -> (!p2 U (p3 & !p2 & X(!p2 U p4)))) U p2",
          "[] (p1 -> [] (p0 -> (p3 & X<> p4)))",
          "[] ((p1 & <>p2) -> (p0 -> (!p2 U (p3 & !p2 & X(!p2 U p4)))) U p2)",
          "[] (p1 -> (p0 -> (!p2 U (p3 & !p2 & X(!p2 U p4)))) U (p2 | []"
          "(p0 -> (p3 & X<> p4))))",

          "[] (p0 -> <>(p3 & !p5 & X(!p5 U p4)))",
          "<>p2 -> (p0 -> (!p2 U (p3 & !p2 & !p5 & X((!p2 & !p5) U p4)))) U p2",
          "[] (p1 -> [] (p0 -> (p3 & !p5 & X(!p5 U p4))))",
          "[] ((p1 & <>p2) -> (p0 -> (!p2 U (p3 & !p2 & !p5 & X((!p2 & !p5) U"
          " p4)))) U p2)",
          "[] (p1 -> (p0 -> (!p2 U (p3 & !p2 & !p5 & X((!p2 & !p5) U p4)))) U "
          "(p2 | [] (p0 -> (p3 & !p5 & X(!p5 U p4)))))",
        };

        constexpr unsigned max = (sizeof formulas)/(sizeof *formulas);
        if (n < 1 || (unsigned) n > max)
          bad_number("dac-patterns", n, max);
        return spot::relabel(parse_formula(formulas[n - 1]), Pnn);
      }

      static formula
      hkrss_pattern(int n)
      {
        static const char* formulas[] = {
          "G(Fp0 & F!p0)",
          "GFp0 & GF!p0",
          "GF(!(p1 <-> Xp1) | !(p0 <-> Xp0))",
          "GF(!(p1 <-> Xp1) | !(p0 <-> Xp0) | !(p2 <-> Xp2) | !(p3 <-> Xp3))",
          "G!p0",
          "G((p0 -> F!p0) & (!p0 -> Fp0))",
          "G(p0 -> F(p0 & p1))",
          "G(p0 -> F((!p0 & p1 & p2 & p3) -> Fp4))",
          "G(p0 -> F!p1)",
          "G(p0 -> Fp1)",
          "G(p0 -> F(p1 -> Fp2))",
          "G(p0 -> F((p1 & p2) -> Fp3))",
          "G((p0 -> Fp1) & (p2 -> Fp3) & (p4 -> Fp5) & (p6 -> Fp7))",
          "G(!p0 & !p1)",
          "G!(p0 & p1)",
          "G(p0 -> p1)",
          "G((p0 -> !p1) & (p1 -> !p0))",
          "G(!p0 -> (p1 <-> !p2))",
          "G((!p0 & (p1 | p2 | p3)) -> p4)",
          "G((p0 & p1) -> (p2 | !(p3 & p4)))",
          "G((!p0 & p1 & !p2 & !p3 & !p4) -> F(!p5 & !p6 & !p7 & !p8))",
          "G((p0 & p1 & !p2 & !p3 & !p4) -> F(p5 & !p6 & !p7 & !p8))",
          "G(!p0 -> !(p1 & p2 & p3 & p4 & p5))",
          "G(!p0 -> ((p1 & p2 & p3 & p4) -> !p5))",
          "G((p0 & p1) -> (p2 | p3 | !(p4 & p5)))",
          "G((!p0 & (p1 | p2 | p3 | p4)) -> (!p5 <-> p6))",
          "G((p0 & p1) -> (p2 | p3 | p4 | !(p5 & p6)))",
          "G((p0 & p1) -> (p2 | p3 | p4 | p5 | !(p6 & p7)))",
          "G((p0 & p1 & !p2 & Xp2) -> X(p3 | X(!p1 | p3)))",
          "G((p0 & p1 & !p2 & Xp2)->X(X!p1 | (p2 U (!p2 U (p2 U (!p1|p3))))))",
          "G(p0 & p1 & !p2 & Xp2)->X(X!p1 | (p2 U (!p2 U (p2 U (!p1 | p3)))))",
          "G(p0 -> (p1 U (!p1 U (!p2 | p3))))",
          "G(p0 -> (p1 U (!p1 U (p2 | p3))))",
          "G((!p0 & p1) -> Xp2)",
          "G(p0 -> X(p0 | p1))",
          ("G((!(p1 <-> Xp1) | !(p0 <-> Xp0) | !(p2 <-> Xp2) | !(p3 <-> Xp3)) "
           "-> (X!p4 & X(!(!(p1 <-> Xp1) | !(p0 <-> Xp0) | !(p2 <-> Xp2) | "
           "!(p3 <-> Xp3)) U p4)))"),
          "G((p0 & !p1 & Xp1 & Xp0) -> (p2 -> Xp3))",
          "G(p0 -> X(!p0 U p1))",
          "G((!p0 & Xp0) -> X((p0 U p1) | Gp0))",
          "G((!p0 & Xp0) -> X(p0 U (p0 & !p1 & X(p0 & p1))))",
          ("G((!p0 & Xp0) -> X(p0 U (p0 & !p1 & X(p0 & p1 & (p0 U (p0 & !p1 & "
           "X(p0 & p1)))))))"),
          ("G((p0 & X!p0) -> X(!p0 U (!p0 & !p1 & X(!p0 & p1 & (!p0 U (!p0 & "
           "!p1 & X(!p0 & p1)))))))"),
          ("G((p0 & X!p0) -> X(!p0 U (!p0 & !p1 & X(!p0 & p1 & (!p0 U (!p0 & "
           "!p1 & X(!p0 & p1 & (!p0 U (!p0 & !p1 & X(!p0 & p1))))))))))"),
          "G((!p0 & Xp0) -> X(!(!p0 & Xp0) U (!p1 & Xp1)))",
          ("G(!p0 | X(!p0 | X(!p0 | X(!p0 | X(!p0 | X(!p0 | X(!p0 | X(!p0 | "
           "X(!p0 | X(!p0 | X(!p0 | X!p0)))))))))))"),
          "G((Xp0 -> p0) -> (p1 <-> Xp1))",
          "G((Xp0 -> p0) -> ((p1 -> Xp1) & (!p1 -> X!p1)))",
          ("!p0 U G!((p1 & p2) | (p3 & p4) | (p2 & p3) | (p2 & p4) | "
           "(p1 & p4) | (p1 & p3))"),
          "!p0 U p1",
          "(p0 U p1) | Gp0",
          "p0 & XG!p0",
          "XG(p0 -> (G!p1 | (!Xp1 U p2)))",
          "XG((p0 & !p1) -> (G!p1 | (!p1 U p2)))",
          "XG((p0 & p1) -> ((p1 U p2) | Gp1))",
          "Xp0 & G((!p0 & Xp0) -> XXp0)",
        };

        constexpr unsigned max = (sizeof formulas)/(sizeof *formulas);
        if (n < 1 || (unsigned) n > max)
          bad_number("hkrss-patterns", n, max);
        return spot::relabel(parse_formula(formulas[n - 1]), Pnn);
      }

      static formula
      eh_pattern(int n)
      {
        static const char* formulas[] = {
          "p0 U (p1 & G(p2))",
          "p0 U (p1 & X(p2 U p3))",
          "p0 U (p1 & X(p2 & (F(p3 & X(F(p4 & X(F(p5 & X(F(p6))))))))))",
          "F(p0 & X(G(p1)))",
          "F(p0 & X(p1 & X(F(p2))))",
          "F(p0 & X(p1 U p2))",
          "(F(G(p0))) | (G(F(p1)))",
          "G(p0 -> (p1 U p2))",
          "G(p0 & X(F(p1 & X(F(p2 & X(F(p3)))))))",
          "(G(F(p0))) & (G(F(p1))) & (G(F(p2))) & (G(F(p3))) & (G(F(p4)))",
          "(p0 U (p1 U p2)) | (p1 U (p2 U p0)) | (p2 U (p0 U p1))",
          "G(p0 -> (p1 U ((G(p2)) | (G(p3)))))",
        };

        constexpr unsigned max = (sizeof formulas)/(sizeof *formulas);
        if (n < 1 || (unsigned) n > max)
          bad_number("eh-patterns", n, max);
        return spot::relabel(parse_formula(formulas[n - 1]), Pnn);
      }

      static formula
      p_pattern(int n)
      {
        static const char* formulas[] = {
          "G(p0 -> Fp1)",
          "(GFp1 & GFp0) -> GFp2",
          "G(p0 -> (p1 & (p2 U p3)))",
          "F(p0 | p1)",
          "GF(p0 | p1)",
          "(p0 U p1) -> ((p2 U p3) | Gp2)",
          "G(p0 -> (!p1 U (p1 U (!p1 & (p2 R !p1)))))",
          "G(p0 -> (p1 R !p2))",
          "G(!p0 -> Fp0)",
          "G(p0 -> F(p1 | p2))",
          "!(!(p0 | p1) U p2) & G(p3 -> !(!(p0 | p1) U p2))",
          "G!p0 -> G!p1",
          "G(p0 -> (G!p1 | (!p2 U p1)))",
          "G(p0 -> (p1 R (p1 | !p2)))",
          "G((p0 & p1) -> (!p1 R (p0 | !p1)))",
          "G(p0 -> F(p1 & p2))",
          "G(p0 -> (!p1 U (p1 U (p1 & p2))))",
          "G(p0 -> (!p1 U (p1 U (!p1 U (p1 U (p1 & p2))))))",
          "GFp0 -> GFp1",
          "GF(p0 | p1) & GF(p1 | p2)",
        };

        constexpr unsigned max = (sizeof formulas)/(sizeof *formulas);
        if (n < 1 || (unsigned) n > max)
          bad_number("p-patterns", n, max);
        return spot::relabel(parse_formula(formulas[n - 1]), Pnn);
      }

      static formula
      sb_pattern(int n)
      {
        static const char* formulas[] = {
          "p0 U p1",
          "p0 U (p1 U p2)",
          "!(p0 U (p1 U p2))",
          "G(F(p0)) -> G(F(p1))",
          "(F(p0)) U (G(p1))",
          "(G(p0)) U p1",
          "!((F(F(p0))) <-> (F(p)))",
          "!((G(F(p0))) -> (G(F(p))))",
          "!((G(F(p0))) <-> (G(F(p))))",
          "p0 R (p0 | p1)",
          "(Xp0 U Xp1) | !X(p0 U p1)",
          "(Xp0 U p1) | !X(p0 U (p0 & p1))",
          "G(p0 -> F(p1)) & (((X(p0)) U p1) | !X(p0 U (p0 & p1)))",
          "G(p0 -> F(p1)) & (((X(p0)) U X(p1)) | !X(p0 U p1))",
          "G(p0 -> F(p1))",
          "!G(p0 -> X(p1 R p2))",
          "!(F(G(p0)) | F(G(p1)))",
          "G(F(p0) & F(p1))",
          "F(p0) & F(!p0)",
          "(X(p1) & p2) R X(((p3 U p0) R p2) U (p3 R p2))",
          "(G(p1 | G(F(p0))) & G(p2 | G(F(!p0)))) | G(p1) | G(p2)",
          "(G(p1 | F(G(p0))) & G(p2 | F(G(!p0)))) | G(p1) | G(p2)",
          "!((G(p1 | G(F(p0))) & G(p2 | G(F(!p0)))) | G(p1) | G(p2))",
          "!((G(p1 | F(G(p0))) & G(p2 | F(G(!p0)))) | G(p1) | G(p2))",
          "(G(p1 | X(G p0))) & (G (p2 | X(G !p0)))",
          "G(p1 | (Xp0 & X!p0))",
          // p0 U p0 can't be represented other than as p0 in Spot
          "(p0 U p0) | (p1 U p0)",
        };

        constexpr unsigned max = (sizeof formulas)/(sizeof *formulas);
        if (n < 1 || (unsigned) n > max)
          bad_number("sb-patterns", n, max);
        return spot::relabel(parse_formula(formulas[n - 1]), Pnn);
      }

      static formula
      X_n_kv_exp(formula a, int n, formula b)
      {
        formula f = X_n(a, n);
        return And_(f, G_(Implies_(b, f)));
      }

      static formula
      kv_exp(int n, std::string a, std::string b, std::string c, std::string d)
      {
        formula fa = formula::ap(a);
        formula fb = formula::ap(b);
        formula fc = formula::ap(c);
        formula fd = formula::ap(d);

        exclusive_ap m;
        m.add_group({ fa, fb, fc, fd });

        formula xn = X_(G_(fc));
        for (int i = 0; i < n; i++)
          xn = X_(And_(Or_(fa, fb), xn));
        formula f1 = U_(Not_(fd), And_(fd, xn));

        formula f_and = nullptr;
        for (int i = 1; i <= n; i++)
          f_and = And_(f_and, Or_(X_n_kv_exp(fa, i, fd),
                                  X_n_kv_exp(fb, i, fd)));

        formula f2 = F_(And_(fc, And_(f_and, X_n(fc, n + 1))));

        return m.constrain(And_(f1, f2));
      }

      static formula
      bit_ni(unsigned i, unsigned j, formula fbin[2])
      {
        return fbin[((1u << j) & (i - 1)) != 0];
      }

      static formula
      binary_ki(int k, unsigned i, formula fbin[2])
      {
        formula res = bit_ni(i, k - 1, fbin);
        for (int j = k - 1; j >= 1; j--)
          res = And_(bit_ni(i, j - 1, fbin), X_(res));
        return res;
      }

      static formula
      kr1_exp_1(int k, formula fc, formula fd, formula fbin[2])
      {
        return And_(fc, X_(Or_(binary_ki(k, 1, fbin), fd)));
      }

      static formula
      kr1_exp_2(int n, int k, formula fa, formula fb, formula fbin[2])
      {
        formula res = formula::tt();
        for (int i = 1; i <= n - 1; i++)
          res = And_(res,
                     Implies_(binary_ki(k, i, fbin),
                              X_n(And_(Or_(fa, fb),
                                       X_(binary_ki(k, i + 1, fbin))), k)));

        return G_(res);
      }

      static formula
      kr1_exp_3(int n, int k, formula fa, formula fb, formula fc, formula fd,
                formula fbin[2])
      {
        return G_(Implies_(binary_ki(k, n, fbin),
                           X_n(And_(Or_(fa, fb),
                                    X_(And_(fc,
                                            X_(Or_(binary_ki(k, 1, fbin),
                                                   Or_(fd,
                                                       G_(fc))))))), k)));
      }

      static formula
      kr1_exp_4(int n, int k, formula fc, formula fd, formula fbin[2])
      {
        return U_(Not_(fd),
                  And_(fd, X_(And_(binary_ki(k, 1, fbin),
                                   X_n(G_(fc), n * (k + 1))))));
      }

      static formula
      kr1_exp_5_r(int k, int i, formula fr, formula fd, formula fbin[2])
      {
        return And_(fr, F_(And_(fd, F_(And_(binary_ki(k, i, fbin),
                                            X_n(fr, k))))));
      }

      static formula
      kr1_exp_5(int n, int k, formula fa, formula fb, formula fc, formula fd,
                formula fbin[2])
      {
        formula fand = formula::tt();
        for (int i = 1; i <= n; i++)
          {
            formula for1 = kr1_exp_5_r(k, i, fa, fd, fbin);
            formula for2 = kr1_exp_5_r(k, i, fb, fd, fbin);
            fand = And_(fand, Implies_(binary_ki(k, i, fbin),
                                       X_n(Or_(for1, for2), k)));
          }

        return F_(And_(fc,
                       X_(And_(Not_(fc),
                               U_(fand, fc)))));
      }

      static formula
      kr1_exp(int n, std::string a, std::string b, std::string c, std::string d,
              std::string bin0, std::string bin1)
      {
        int k = ceil(log2(n)) + (n == 1);

        if (n <= 0)
          bad_number("kr1-exp", n);

        formula fa = formula::ap(a);
        formula fb = formula::ap(b);
        formula fc = formula::ap(c);
        formula fd = formula::ap(d);

        formula fbin0 = formula::ap(bin0);
        formula fbin1 = formula::ap(bin1);

        exclusive_ap m;
        m.add_group({ fa, fb, fc, fd, fbin0, fbin1 });

        formula fbin[2] = { fbin0, fbin1 };

        formula res = formula::And({ kr1_exp_1(k, fc, fd, fbin),
              kr1_exp_2(n, k, fa, fb, fbin),
              kr1_exp_3(n, k, fa, fb, fc, fd, fbin),
              kr1_exp_4(n, k, fc, fd, fbin),
              kr1_exp_5(n, k, fa, fb, fc, fd, fbin) });

        return m.constrain(res);
      }

      static formula
      kr2_exp_1(formula* fa, formula* fb, formula fc, formula fd)
      {
        (void) fd;
        return And_(fc,
                    X_(Or_(fa[0],
                           Or_(fb[0], fd))));
      }

      static formula
      kr2_exp_2(int n, formula* fa, formula* fb)
      {
        formula res = formula::tt();
        for (int i = 1; i <= n - 1; i++)
          res = And_(res, Implies_(Or_(fa[i - 1], fb[i - 1]),
                                   X_(Or_(fa[i], fb[i]))));

        return G_(res);
      }

      static formula
      kr2_exp_3(int n, formula* fa, formula* fb, formula fc, formula fd)
      {
        return G_(Implies_(Or_(fa[n - 1], fb[n - 1]),
                           X_(And_(fc,
                                   X_(Or_(fa[0],
                                          Or_(fb[0],
                                              Or_(fd, G_(fc)))))))));
      }

      static formula
      kr2_exp_4(int n, formula* fa, formula* fb, formula fc, formula fd)
      {
        return U_(Not_(fd),
                  And_(fd, X_(And_(Or_(fa[0], fb[0]), X_n(G_(fc), n)))));
      }

      static formula
      kr2_exp_5_r(int i, formula* fr, formula fd)
      {
        return And_(fr[i - 1], F_(And_(fd, F_(fr[i - 1]))));
      }

      static formula
      kr2_exp_5(int n, formula* fa, formula* fb, formula fc, formula fd)
      {
        formula facc = formula::ff();
        for (int i = 1; i <= n; i++)
          {
            formula for1 = kr2_exp_5_r(i, fa, fd);
            formula for2 = kr2_exp_5_r(i, fb, fd);
            facc = Or_(facc, (Or_(for1, for2)));
          }

        return F_(And_(fc,
                       X_(And_(Not_(fc), U_(facc, fc)))));
      }

      static formula
      kr2_exp_mutex(int n, formula* fa, formula* fb, formula fc, formula fd)
      {
        formula f1or = formula::ff();
        formula f3and = formula::tt();

        for (int i = 1; i <= n; i++)
          {
            f1or = Or_(f1or, Or_(fa[i - 1], fb[i - 1]));
            f3and = And_(f3and, Implies_(fa[i - 1], Not_(fb[i - 1])));
          }

        formula f1 = G_(Implies_(Or_(fc, fd), Not_(f1or)));
        formula f2 = G_(Implies_(fc, Not_(fd)));
        formula f3 = G_(f3and);

        return And_(f1, And_(f2, f3));
      }

      static formula
      kr2_exp(int n, std::string a, std::string b, std::string c, std::string d)
      {
        if (n <= 0)
          bad_number("kr-n", n);

        formula fc = formula::ap(c);
        formula fd = formula::ap(d);

        formula* fa = new formula[n];
        formula* fb = new formula[n];

        for (int i = 0; i < n; i++)
          {
            fa[i] = formula::ap(a + std::to_string(i + 1));
            fb[i] = formula::ap(b + std::to_string(i + 1));
          }

        formula res = formula::And({ kr2_exp_1(fa, fb, fc, fd),
              kr2_exp_2(n, fa, fb),
              kr2_exp_3(n, fa, fb, fc, fd),
              kr2_exp_4(n, fa, fb, fc, fd),
              kr2_exp_5(n, fa, fb, fc, fd),
              kr2_exp_mutex(n, fa, fb, fc, fd) });

        return res;
      }
    }

1143
    formula ltl_pattern(ltl_pattern_id pattern, int n)
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
    {
      if (n < 0)
        {
          std::ostringstream err;
          err << "pattern argument for " << ltl_pattern_name(pattern)
              << " should be positive";
          throw std::runtime_error(err.str());
        }

      switch (pattern)
        {
          // Keep this alphabetically-ordered!
1156
        case LTL_AND_F:
1157
          return combunop_n("p", n, op::F, true);
1158
        case LTL_AND_FG:
1159
          return FG_n("p", n, true);
1160
        case LTL_AND_GF:
1161
          return GF_n("p", n, true);
1162
        case LTL_CCJ_ALPHA:
1163
          return formula::And({E_n("p", n), E_n("q", n)});
1164
        case LTL_CCJ_BETA:
1165
          return formula::And({N_n("p", n), N_n("q", n)});
1166
        case LTL_CCJ_BETA_PRIME:
1167
          return formula::And({N_prime_n("p", n), N_prime_n("q", n)});
1168
        case LTL_DAC_PATTERNS:
1169
          return dac_pattern(n);
1170
        case LTL_EH_PATTERNS:
1171
          return eh_pattern(n);
1172
1173
        case LTL_FXG_OR:
          return FXG_or_n("p", n);
1174
1175
        case LTL_GF_EQUIV:
          return LTL_GF_equiv(n, "a", "z");
1176
        case LTL_GH_Q:
1177
          return Q_n("p", n);
1178
        case LTL_GH_R:
1179
          return R_n("p", n);
1180
        case LTL_GO_THETA:
1181
          return fair_response("p", "q", "r", n);
1182
1183
        case LTL_GXF_AND:
          return GXF_and_n("p", n);
1184
        case LTL_HKRSS_PATTERNS:
1185
          return hkrss_pattern(n);
1186
        case LTL_KR_N:
1187
          return kr2_exp(n, "a", "b", "c", "d");
1188
        case LTL_KR_NLOGN:
1189
          return kr1_exp(n, "a", "b", "c", "d", "y", "z");
1190
        case LTL_KV_PSI:
1191
          return kv_exp(n, "a", "b", "c", "d");
1192
        case LTL_OR_FG:
1193
          return FG_n("p", n, false);
1194
        case LTL_OR_G:
1195
          return combunop_n("p", n, op::G, false);
1196
        case LTL_OR_GF:
1197
          return GF_n("p", n, false);
1198
1199
1200
1201
1202
1203
1204
1205
        case LTL_MS_EXAMPLE:
          return ms_example("a", "b", n);
        case LTL_MS_PHI_H:
          return ms_phi_h("a", "b", n);
        case LTL_MS_PHI_R:
          return ms_phi_rs("a", "b", n, true);
        case LTL_MS_PHI_S:
          return ms_phi_rs("a", "b", n, false);
1206
        case LTL_P_PATTERNS:
1207
          return p_pattern(n);
1208
        case LTL_R_LEFT:
1209
          return bin_n("p", n, op::R, false);
1210
        case LTL_R_RIGHT:
1211
          return bin_n("p", n, op::R, true);
1212
        case LTL_RV_COUNTER_CARRY:
1213
          return ltl_counter_carry("b", "m", "c", n, false);
1214
        case LTL_RV_COUNTER_CARRY_LINEAR:
1215
          return ltl_counter_carry("b", "m", "c", n, true);
1216
        case LTL_RV_COUNTER:
1217
          return ltl_counter("b", "m", n, false);
1218
        case LTL_RV_COUNTER_LINEAR:
1219
          return ltl_counter("b", "m", n, true);
1220
        case LTL_SB_PATTERNS:
1221
          return sb_pattern(n);
1222
        case LTL_TV_F1:
1223
          return tv_f1("p", "q", n);
1224
        case LTL_TV_F2:
1225
          return tv_f2("p", "q", n);
1226
        case LTL_TV_G1:
1227
          return tv_g1("p", "q", n);
1228
        case LTL_TV_G2:
1229
          return tv_g2("p", "q", n);
1230
        case LTL_TV_UU:
1231
          return tv_uu("p", n);
1232
        case LTL_U_LEFT:
1233
          return bin_n("p", n, op::U, false);
1234
        case LTL_U_RIGHT:
1235
          return bin_n("p", n, op::U, true);
1236
        case LTL_END:
1237
1238
1239
1240
1241
1242
          break;
        }
      throw std::runtime_error("unsupported pattern");
    }


1243
    const char* ltl_pattern_name(ltl_pattern_id pattern)
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
    {
      static const char* const class_name[] =
        {
          "and-f",
          "and-fg",
          "and-gf",
          "ccj-alpha",
          "ccj-beta",
          "ccj-beta-prime",
          "dac-patterns",
          "eh-patterns",
1255
          "fxg-or",
1256
          "gf-equiv",
1257
1258
1259
          "gh-q",
          "gh-r",
          "go-theta",
1260
          "gxf-and",
1261
1262
1263
1264
          "hkrss-patterns",
          "kr-n",
          "kr-nlogn",
          "kv-psi",
1265
1266
1267
1268
          "ms-example",
          "ms-phi-h",
          "ms-phi-r",
          "ms-phi-s",
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
          "or-fg",
          "or-g",
          "or-gf",
          "p-patterns",
          "r-left",
          "r-right",
          "rv-counter",
          "rv-counter-carry",
          "rv-counter-carry-linear",
          "rv-counter-linear",
          "sb-patterns",
          "tv-f1",
          "tv-f2",
          "tv-g1",
          "tv-g2",
          "tv-uu",
          "u-left",
          "u-right",
        };
      // Make sure we do not forget to update the above table every
      // time a new pattern is added.
      static_assert(sizeof(class_name)/sizeof(*class_name)
1291
1292
                    == LTL_END - LTL_BEGIN, "size mismatch");
      if (pattern < LTL_BEGIN || pattern >= LTL_END)
1293
        throw std::runtime_error("unsupported pattern");
1294
      return class_name[pattern - LTL_BEGIN];
1295
1296
    }

1297
1298
1299
1300
1301
    int ltl_pattern_max(ltl_pattern_id pattern)
    {
      switch (pattern)
        {
          // Keep this alphabetically-ordered!
1302
1303
1304
1305
1306
1307
        case LTL_AND_F:
        case LTL_AND_FG:
        case LTL_AND_GF:
        case LTL_CCJ_ALPHA:
        case LTL_CCJ_BETA:
        case LTL_CCJ_BETA_PRIME:
1308
          return 0;
1309
        case LTL_DAC_PATTERNS:
1310
          return 55;
1311
        case LTL_EH_PATTERNS:
1312
          return 12;
1313
        case LTL_FXG_OR:
1314
        case LTL_GF_EQUIV:
1315
1316
1317
        case LTL_GH_Q:
        case LTL_GH_R:
        case LTL_GO_THETA:
1318
        case LTL_GXF_AND:
1319
          return 0;
1320
        case LTL_HKRSS_PATTERNS:
1321
          return 55;
1322
1323
1324
        case LTL_KR_N:
        case LTL_KR_NLOGN:
        case LTL_KV_PSI:
1325
1326
1327
1328
        case LTL_MS_EXAMPLE:
        case LTL_MS_PHI_H:
        case LTL_MS_PHI_R:
        case LTL_MS_PHI_S:
1329
1330
1331
        case LTL_OR_FG:
        case LTL_OR_G:
        case LTL_OR_GF:
1332
          return 0;
1333
        case LTL_P_PATTERNS:
1334
          return 20;
1335
1336
1337
1338
1339
1340
        case LTL_R_LEFT:
        case LTL_R_RIGHT:
        case LTL_RV_COUNTER_CARRY:
        case LTL_RV_COUNTER_CARRY_LINEAR:
        case LTL_RV_COUNTER:
        case LTL_RV_COUNTER_LINEAR:
1341
          return 0;
1342
        case LTL_SB_PATTERNS:
1343
          return 27;
1344
1345
1346
1347
1348
1349
1350
        case LTL_TV_F1:
        case LTL_TV_F2:
        case LTL_TV_G1:
        case LTL_TV_G2:
        case LTL_TV_UU:
        case LTL_U_LEFT:
        case LTL_U_RIGHT:
1351
          return 0;
1352
        case LTL_END:
1353
1354
1355
1356
1357
          break;
        }
      throw std::runtime_error("unsupported pattern");

    }
1358
1359
1360

  }
}