acc_cond.ipynb 32.2 KB
Newer Older
1
{
2
3
4
5
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
6
   "metadata": {},
7
8
9
10
11
   "outputs": [],
   "source": [
    "import spot\n",
    "spot.setup()"
   ]
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
44
45
46
47
48
49
50
51
52
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Acceptance conditions\n",
    "\n",
    "The acceptance condition of an automaton specifies which of its paths are accepting.\n",
    "\n",
    "The way acceptance conditions are stored in Spot is derived from the way acceptance conditions are specified in the [HOA format](http://adl.github.io/hoaf/).  In HOA, acceptance conditions are given as a line of the form:\n",
    "\n",
    "    Acceptance: 3 (Inf(0)&Fin(1))|Inf(2)\n",
    "\n",
    "The number `3` gives the number of acceptance sets used (numbered from `0` to `2` in that case), while the rest of the line is a positive Boolean formula over terms of the form:\n",
    "- `Inf(n)`, that is true if and only if the set `n` is seen infinitely often,\n",
    "- `Fin(n)`, that is true if and only if the set `n` should be seen finitely often,\n",
    "- `t`, always true,\n",
    "- `f`, always false.\n",
    "\n",
    "The HOA specifications additionally allows terms of the form `Inf(!n)` or `Fin(!n)` but Spot automatically rewrites those away when reading an HOA file.\n",
    "\n",
    "Note that the number of sets given can be larger than what is actually needed by the acceptance formula.\n",
    "\n",
    "Transitions in automata can be tagged as being part of some member sets, and a path in the automaton is accepting if the set of acceptance sets visited along this path satify the acceptance condition.\n",
    "\n",
    "Definining acceptance conditions in Spot involves three different types of C++ objects:\n",
    "\n",
    "- `spot::acc_cond` is used to represent an acceptance condition, that is: a number of sets and a formula.\n",
    "- `spot::acc_cond::acc_code`, is used to represent Boolean formula for the acceptance condition using a kind of byte code (hence the name)\n",
    "- `spot::acc_cond::mark_t`, is a type of bit-vector used to represent membership to acceptance sets.\n",
    "\n",
    "In because Swig's support for nested class is limited, these types are available respectively as `spot.acc_cond`, `spot.acc_code`, and `spot.mark_t` in Python.\n",
    "\n",
    "## `mark_t`\n",
    "\n",
    "Let's start with the simpler of these three objects. `mark_t` is a type of bit vector.  Its main constructor takes a sequence of set numbers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
53
   "metadata": {},
54
   "outputs": [
55
    {
56
57
58
59
60
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
61
62
63
    }
   ],
   "source": [
64
    "print(spot.mark_t())"
65
66
67
68
69
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
70
   "metadata": {},
71
   "outputs": [
72
    {
73
74
75
76
77
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0,2,3}\n"
     ]
78
79
80
    }
   ],
   "source": [
81
    "print(spot.mark_t([0, 2, 3])) # works with lists"
82
83
84
85
86
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
87
   "metadata": {},
88
   "outputs": [
89
    {
90
91
92
93
94
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0,2,3}\n"
     ]
95
96
97
    }
   ],
   "source": [
98
    "print(spot.mark_t((0, 2, 3))) # works with tuples"
99
100
101
102
103
104
105
106
107
108
109
110
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As seen above, the sequence of set numbers can be specified using a list or a tuple.  While from the Python language point of view, using a tuple is faster than using a list, the overhead to converting all the arguments from Python to C++ and then converting the resuslting back from C++ to Python makes this difference completely negligeable.  In the following, we opted to use lists, because brackets are more readable than nested parentheses."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
111
   "metadata": {},
112
113
114
115
116
117
118
119
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0,2,3,4}\n",
      "{0}\n",
      "{2,3}\n"
120
     ]
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    }
   ],
   "source": [
    "x = spot.mark_t([0, 2, 3])\n",
    "y = spot.mark_t([0, 4])\n",
    "print(x | y)\n",
    "print(x & y)\n",
    "print(x - y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The bits can be set, cleared, and tested using the `set()`, `clear()`, and `has()` methods:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
141
   "metadata": {},
142
   "outputs": [
143
    {
144
145
146
147
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0,2,3,5}\n"
148
     ]
149
150
151
152
153
154
155
156
157
158
    }
   ],
   "source": [
    "x.set(5)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
159
   "metadata": {},
160
   "outputs": [
161
    {
162
163
164
165
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0,2,5}\n"
166
     ]
167
168
169
170
171
172
173
174
175
176
    }
   ],
   "source": [
    "x.clear(3)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
177
   "metadata": {},
178
   "outputs": [
179
    {
180
181
182
183
184
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
185
     ]
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
    }
   ],
   "source": [
    "print(x.has(2))\n",
    "print(x.has(3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Left-shifting will increment all set numbers.\n",
    "This operation is useful when building the product of two automata: all the set number of one automaton have to be shift by the number of sets used in the other automaton."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
204
   "metadata": {},
205
   "outputs": [
206
    {
207
208
209
210
211
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{2,4,7}\n"
     ]
212
213
214
    }
   ],
   "source": [
215
    "print(x << 2)"
216
217
218
219
220
221
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
222
    "The different sets can be iterated over with the `sets()` method, that returns a tuple with the index of all bits set."
223
224
225
226
   ]
  },
  {
   "cell_type": "code",
227
228
   "execution_count": 10,
   "metadata": {},
229
230
231
232
233
234
235
236
237
238
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0,2,5}\n",
      "[0, 2, 5]\n",
      "0\n",
      "2\n",
      "5\n"
239
     ]
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
    }
   ],
   "source": [
    "print(x)\n",
    "print(list(x.sets()))\n",
    "for s in x.sets():\n",
    "    print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`count()` return the number of sets in a `mark_t`:"
   ]
  },
  {
   "cell_type": "code",
258
259
   "execution_count": 11,
   "metadata": {},
260
   "outputs": [
261
    {
262
263
264
265
266
     "data": {
      "text/plain": [
       "3"
      ]
     },
267
     "execution_count": 11,
268
     "metadata": {},
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.count()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`lowest()` returns a `mark_t` containing only the lowest set number.  This provides another way to iterate overs all set numbers in cases where you need the result as a `mark_t`."
   ]
  },
  {
   "cell_type": "code",
285
286
   "execution_count": 12,
   "metadata": {},
287
   "outputs": [
288
    {
289
290
     "data": {
      "text/plain": [
291
       "spot.mark_t([1])"
292
293
      ]
     },
294
     "execution_count": 12,
295
     "metadata": {},
296
297
298
299
300
301
302
303
304
     "output_type": "execute_result"
    }
   ],
   "source": [
    "spot.mark_t([1,3,5]).lowest()"
   ]
  },
  {
   "cell_type": "code",
305
306
   "execution_count": 13,
   "metadata": {},
307
308
309
310
311
312
313
314
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1}\n",
      "{3}\n",
      "{5}\n"
315
     ]
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
    }
   ],
   "source": [
    "v = spot.mark_t([1, 3, 5])\n",
    "while v:               # this stops once v is empty\n",
    "    b = v.lowest()\n",
    "    v -= b\n",
    "    print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`max_set()` returns the number of the highest set plus one.  This is usually used to figure out how many sets we need to declare on the `Acceptance:` line of the HOA format:"
   ]
  },
  {
   "cell_type": "code",
335
336
   "execution_count": 14,
   "metadata": {},
337
   "outputs": [
338
    {
339
340
341
342
343
     "data": {
      "text/plain": [
       "6"
      ]
     },
344
     "execution_count": 14,
345
     "metadata": {},
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
     "output_type": "execute_result"
    }
   ],
   "source": [
    "spot.mark_t([1, 3, 5]).max_set()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `acc_code`\n",
    "\n",
    "`acc_code` encodes the formula of the acceptance condition using a kind of bytecode that basically corresponds to an encoding in [reverse Polish notation](http://en.wikipedia.org/wiki/Reverse_Polish_notation) in which conjunctions of `Inf(n)` terms, and disjunctions of `Fin(n)` terms are grouped.  In particular, the frequently-used genaralized-Büchi acceptance conditions (like `Inf(0)&Inf(1)&Inf(2)`) are always encoded as a single term (like `Inf({0,1,2})`).\n",
    "\n",
    "The simplest way to construct an `acc_code` by passing a string that represent the formula to build."
   ]
  },
  {
   "cell_type": "code",
366
367
   "execution_count": 15,
   "metadata": {},
368
   "outputs": [
369
370
371
372
373
374
375
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(Inf(0) & Fin(1)) | Inf(2)\n"
     ]
    },
376
    {
377
378
     "data": {
      "text/plain": [
379
       "spot.acc_code(\"(Inf(0) & Fin(1)) | Inf(2)\")"
380
381
      ]
     },
382
     "execution_count": 15,
383
     "metadata": {},
384
385
386
387
     "output_type": "execute_result"
    }
   ],
   "source": [
388
389
390
    "acc = spot.acc_code('(Inf(0)&Fin(1))|Inf(2)')\n",
    "print(acc)   # shows just the formula\n",
    "acc          # shows the acc_code object"
391
392
393
394
395
396
397
398
399
400
401
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You may also use a named acceptance condition:"
   ]
  },
  {
   "cell_type": "code",
402
403
   "execution_count": 16,
   "metadata": {},
404
   "outputs": [
405
    {
406
407
     "data": {
      "text/plain": [
408
       "spot.acc_code(\"(Fin(0) & Inf(1)) | (Fin(2) & Inf(3))\")"
409
410
      ]
     },
411
     "execution_count": 16,
412
     "metadata": {},
413
414
415
416
417
418
419
420
421
422
423
     "output_type": "execute_result"
    }
   ],
   "source": [
    "spot.acc_code('Rabin 2')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Clément Gillard's avatar
Clément Gillard committed
424
    "The recognized names are the valid values for `acc-name:` in the [HOA format](http://adl.github.io/hoaf/).  Additionally, numbers may be replaced by ranges of the form `n..m`, in which case a random number is selected in that range."
425
426
427
428
   ]
  },
  {
   "cell_type": "code",
429
430
   "execution_count": 17,
   "metadata": {},
431
   "outputs": [
432
    {
433
434
435
436
437
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(Fin(0) | Inf(1)) & (Fin(2) | Inf(3)) & (Fin(4) | Inf(5)) & (Fin(6) | Inf(7))\n",
      "(Fin(0) | Inf(1)) & (Fin(2) | Inf(3))\n"
438
     ]
439
440
441
442
443
444
445
446
447
448
449
    }
   ],
   "source": [
    "print(spot.acc_code('Streett 2..4'))\n",
    "print(spot.acc_code('Streett 2..4'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
450
    "It may also be convenient to generate a random acceptance condition.  Below we require between 3 and 5 acceptance sets:"
451
452
453
454
   ]
  },
  {
   "cell_type": "code",
455
456
   "execution_count": 18,
   "metadata": {},
457
   "outputs": [
458
    {
459
460
     "data": {
      "text/plain": [
461
       "spot.acc_code(\"(Fin(3) | Inf(1)) & Inf(4) & (Fin(0)|Fin(2))\")"
462
463
      ]
     },
464
     "execution_count": 18,
465
     "metadata": {},
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
     "output_type": "execute_result"
    }
   ],
   "source": [
    "spot.acc_code('random 3..5')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `to_cnf()` and `to_dnf()` functions can be used to rewrite the formula into Conjunctive or Disjunctive normal forms.  This functions will simplify the resulting formulas to make them irredundant."
   ]
  },
  {
   "cell_type": "code",
482
483
   "execution_count": 19,
   "metadata": {},
484
   "outputs": [
485
    {
486
487
     "data": {
      "text/plain": [
488
       "spot.acc_code(\"Fin(0) & (Inf(1) | (Fin(2) & (Inf(3) | Fin(4))))\")"
489
490
      ]
     },
491
     "execution_count": 19,
492
     "metadata": {},
493
494
495
496
497
498
499
500
501
502
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = spot.acc_code('parity min odd 5')\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
503
504
   "execution_count": 20,
   "metadata": {},
505
   "outputs": [
506
    {
507
508
     "data": {
      "text/plain": [
509
       "spot.acc_code(\"Fin(0) & (Inf(1) | Fin(2)) & (Inf(1) | Inf(3) | Fin(4))\")"
510
511
      ]
     },
512
     "execution_count": 20,
513
     "metadata": {},
514
515
516
517
518
519
520
521
522
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.to_cnf()"
   ]
  },
  {
   "cell_type": "code",
523
524
   "execution_count": 21,
   "metadata": {},
525
   "outputs": [
526
    {
527
528
     "data": {
      "text/plain": [
529
       "spot.acc_code(\"(Fin(0) & Inf(1)) | (Fin(0) & Fin(2) & Inf(3)) | (Fin(0) & Fin(2) & Fin(4))\")"
530
531
      ]
     },
532
     "execution_count": 21,
533
     "metadata": {},
534
535
536
537
538
539
540
541
542
543
544
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.to_dnf()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
545
    "The manipulation of `acc_code` objects is quite rudimentary at the moment: they are easy to build, but are harder take appart.  In fact we won't attempt to disassemble an `acc_code` object in Python: those things are better done in C++\n",
546
547
548
549
550
551
552
    "\n",
    "Operators `|`, `|=`, `&`, `&=`, `<<`, and `<<=` can be used with their obvious semantics.\n",
    "Whenever possible, the inplace versions (`|=`, `&=`, `<<=`) should be prefered, because they create less temporary acceptance conditions."
   ]
  },
  {
   "cell_type": "code",
553
554
   "execution_count": 22,
   "metadata": {},
555
   "outputs": [
556
    {
557
558
559
560
561
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(Fin(0) & Inf(1)) | (Fin(2) & Inf(3))\n",
      "(Fin(4) & Inf(5)) | (Fin(6) & Inf(7))\n"
562
     ]
563
564
565
566
567
568
569
570
571
572
573
    }
   ],
   "source": [
    "x = spot.acc_code('Rabin 2')\n",
    "y = spot.acc_code('Rabin 2') << 4\n",
    "print(x)\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
574
575
   "execution_count": 23,
   "metadata": {},
576
   "outputs": [
577
    {
578
579
580
581
582
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(Fin(4) & Inf(5)) | (Fin(6) & Inf(7)) | (Fin(0) & Inf(1)) | (Fin(2) & Inf(3))\n",
      "((Fin(4) & Inf(5)) | (Fin(6) & Inf(7))) & ((Fin(0) & Inf(1)) | (Fin(2) & Inf(3)))\n"
583
     ]
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
    }
   ],
   "source": [
    "print(x | y)\n",
    "print(x & y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `complement()` method returns the complemented acceptance condition:"
   ]
  },
  {
   "cell_type": "code",
600
601
   "execution_count": 24,
   "metadata": {},
602
   "outputs": [
603
    {
604
605
606
607
608
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(Fin(0) & Inf(1)) | (Fin(2) & Inf(3))\n",
      "(Inf(0) | Fin(1)) & (Inf(2) | Fin(3))\n"
609
     ]
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
    }
   ],
   "source": [
    "print(x)\n",
    "print(x.complement())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Instead of using `acc_code('string')`, it is also possible to build an acceptance formula from atoms like `Inf({...})`, `Fin({...})`, `t`, or `f`.\n",
    "\n",
    "Remember that in our encoding for the formula, terms like `Inf(1)&Inf(2)` and `Fin(3)|Fin(4)|Fin(5)` are actually stored as `Inf({1,2})` and `Fin({3,4,5})`, where `{1,2}` and `{3,4,5}` are instance of `mark_t`.  These terms can be generated with the\n",
    "functions `spot.acc_code.inf(mark)` and `spot.acc_code.fin(mark)`.\n",
    "\n",
    "`Inf({})` is equivalent to `t`, and `Fin({})` is equivalent to `f`, but it's better to use the functions `spot.acc_code.t()` or `spot.acc_code.f()` directly."
   ]
  },
  {
   "cell_type": "code",
631
632
   "execution_count": 25,
   "metadata": {},
633
   "outputs": [
634
    {
635
636
     "data": {
      "text/plain": [
637
       "spot.acc_code(\"(Fin(3)|Fin(4)|Fin(5)) & (Inf(1)&Inf(2))\")"
638
639
      ]
     },
640
     "execution_count": 25,
641
     "metadata": {},
642
643
644
645
646
647
648
649
650
     "output_type": "execute_result"
    }
   ],
   "source": [
    "spot.acc_code.inf([1,2]) & spot.acc_code.fin([3,4,5])"
   ]
  },
  {
   "cell_type": "code",
651
652
   "execution_count": 26,
   "metadata": {},
653
   "outputs": [
654
    {
655
656
     "data": {
      "text/plain": [
657
       "spot.acc_code(\"t\")"
658
659
      ]
     },
660
     "execution_count": 26,
661
     "metadata": {},
662
663
664
665
666
667
668
669
670
     "output_type": "execute_result"
    }
   ],
   "source": [
    "spot.acc_code.inf([])"
   ]
  },
  {
   "cell_type": "code",
671
672
   "execution_count": 27,
   "metadata": {},
673
   "outputs": [
674
    {
675
676
     "data": {
      "text/plain": [
677
       "spot.acc_code(\"t\")"
678
679
      ]
     },
680
     "execution_count": 27,
681
     "metadata": {},
682
683
684
685
686
687
688
689
690
     "output_type": "execute_result"
    }
   ],
   "source": [
    "spot.acc_code.t()"
   ]
  },
  {
   "cell_type": "code",
691
692
   "execution_count": 28,
   "metadata": {},
693
   "outputs": [
694
    {
695
696
     "data": {
      "text/plain": [
697
       "spot.acc_code(\"f\")"
698
699
      ]
     },
700
     "execution_count": 28,
701
     "metadata": {},
702
703
704
705
706
707
708
709
710
     "output_type": "execute_result"
    }
   ],
   "source": [
    "spot.acc_code.fin([])"
   ]
  },
  {
   "cell_type": "code",
711
712
   "execution_count": 29,
   "metadata": {},
713
   "outputs": [
714
    {
715
716
     "data": {
      "text/plain": [
717
       "spot.acc_code(\"f\")"
718
719
      ]
     },
720
     "execution_count": 29,
721
     "metadata": {},
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
     "output_type": "execute_result"
    }
   ],
   "source": [
    "spot.acc_code.f()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To evaluate an acceptance condition formula on a run, build a `mark_t` containing all the acceptance sets that are seen infinitely often along this run, and call the `accepting()` method."
   ]
  },
  {
   "cell_type": "code",
738
739
   "execution_count": 30,
   "metadata": {},
740
741
742
743
744
745
746
747
748
749
750
751
752
753
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "acc = (Fin(0) & Inf(1)) | Inf(2)\n",
      "acc.accepting([0, 1, 2]) = True\n",
      "acc.accepting([1, 2]) = True\n",
      "acc.accepting([0, 1]) = False\n",
      "acc.accepting([0, 2]) = True\n",
      "acc.accepting([0]) = False\n",
      "acc.accepting([1]) = True\n",
      "acc.accepting([2]) = True\n",
      "acc.accepting([]) = False\n"
754
     ]
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
    }
   ],
   "source": [
    "acc = spot.acc_code('Fin(0) & Inf(1) | Inf(2)')\n",
    "print(\"acc =\", acc)\n",
    "for x in ([0, 1, 2], [1, 2], [0, 1], [0, 2], [0], [1], [2], []):\n",
    "    print(\"acc.accepting({}) = {}\".format(x, acc.accepting(x)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally the method `used_sets()` returns a `mark_t` with all the sets appearing in the formula:"
   ]
  },
  {
   "cell_type": "code",
773
774
   "execution_count": 31,
   "metadata": {},
775
776
777
778
779
780
781
782
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fin(0) & Inf(2)\n",
      "{0,2}\n",
      "3\n"
783
     ]
784
785
786
787
788
789
790
791
792
    }
   ],
   "source": [
    "acc = spot.acc_code('Fin(0) & Inf(2)')\n",
    "print(acc)\n",
    "print(acc.used_sets())\n",
    "print(acc.used_sets().max_set())"
   ]
  },
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
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `used_inf_fin_sets()` returns a pair of marks instead, the first one with all sets occuring in `Inf`, and the second one with all sets appearing in `Fin`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(spot.mark_t([2]), spot.mark_t([0]))"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.used_inf_fin_sets()"
   ]
  },
820
821
822
823
824
825
826
827
828
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# `acc_cond`\n",
    "\n",
    "Automata store their acceptance condition as an instance of the `acc_cond` class.\n",
    "This class can be thought of as a pair `(n, code)`, where `n` is an integer that tells how many acceptance sets are used, while the `code` is an instance of `acc_code` and encodes the formula over *a subset* of these acceptance sets.  We usually have `n == code.used_sets().max_set())`, but `n` can be larger.\n",
    "\n",
829
    "It is OK if an automaton declares that is uses 3 sets, even if the acceptance condition formula only uses set number 1.  The extra two sets will have no impact on the language, even if they are used in the automaton.\n",
830
831
832
833
834
835
    "\n",
    "The `acc_cond` objects are usually not created by hand: automata have dedicated methods for that.  But for the purpose of this notebook, let's do it:"
   ]
  },
  {
   "cell_type": "code",
836
837
   "execution_count": 33,
   "metadata": {},
838
   "outputs": [
839
    {
840
841
     "data": {
      "text/plain": [
842
       "spot.acc_cond(4, \"(Fin(0) & Inf(1)) | (Fin(2) & Inf(3))\")"
843
844
      ]
     },
845
     "execution_count": 33,
846
     "metadata": {},
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc = spot.acc_cond(4, spot.acc_code('Rabin 2'))\n",
    "acc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For convenience, you can pass the string directly:"
   ]
  },
  {
   "cell_type": "code",
864
865
   "execution_count": 34,
   "metadata": {},
866
   "outputs": [
867
    {
868
869
     "data": {
      "text/plain": [
870
       "spot.acc_cond(4, \"(Fin(0) & Inf(1)) | (Fin(2) & Inf(3))\")"
871
872
      ]
     },
873
     "execution_count": 34,
874
     "metadata": {},
875
876
877
878
879
880
881
882
883
884
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc = spot.acc_cond(4, 'Rabin 2')\n",
    "acc"
   ]
  },
  {
   "cell_type": "code",
885
886
   "execution_count": 35,
   "metadata": {},
887
   "outputs": [
888
    {
889
890
891
892
893
     "data": {
      "text/plain": [
       "4"
      ]
     },
894
     "execution_count": 35,
895
     "metadata": {},
896
897
898
899
900
901
902
903
904
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.num_sets()"
   ]
  },
  {
   "cell_type": "code",
905
906
   "execution_count": 36,
   "metadata": {},
907
   "outputs": [
908
    {
909
910
     "data": {
      "text/plain": [
911
       "spot.acc_code(\"(Fin(0) & Inf(1)) | (Fin(2) & Inf(3))\")"
912
913
      ]
     },
914
     "execution_count": 36,
915
     "metadata": {},
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.get_acceptance()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `acc_cond` object can also be constructed using only a number of sets.  In that case, the acceptance condition defaults to `t`, and it can be changed to something else later (using `set_acceptance()`).  The number of acceptance sets can also be augmented with `add_sets()`."
   ]
  },
  {
   "cell_type": "code",
932
933
   "execution_count": 37,
   "metadata": {},
934
   "outputs": [
935
    {
936
937
     "data": {
      "text/plain": [
938
       "spot.acc_cond(4, \"t\")"
939
940
      ]
     },
941
     "execution_count": 37,
942
     "metadata": {},
943
944
945
946
947
948
949
950
951
952
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc = spot.acc_cond(4)\n",
    "acc"
   ]
  },
  {
   "cell_type": "code",
953
954
   "execution_count": 38,
   "metadata": {},
955
   "outputs": [
956
    {
957
958
     "data": {
      "text/plain": [
959
       "spot.acc_cond(6, \"t\")"
960
961
      ]
     },
962
     "execution_count": 38,
963
     "metadata": {},
964
965
966
967
968
969
970
971
972
973
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.add_sets(2)\n",
    "acc"
   ]
  },
  {
   "cell_type": "code",
974
975
   "execution_count": 39,
   "metadata": {},
976
   "outputs": [
977
    {
978
979
     "data": {
      "text/plain": [
980
       "spot.acc_cond(6, \"(Fin(0) | Inf(1)) & (Fin(2) | Inf(3))\")"
981
982
      ]
     },
983
     "execution_count": 39,
984
     "metadata": {},
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.set_acceptance('Streett 2')\n",
    "acc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Calling the constructor of `acc_cond` by passing just an instance of `acc_code` (or a string that will be passed to the `acc_code` constructor) will automatically set the number of acceptance sets to the minimum needed by the formula:"
   ]
  },
  {
   "cell_type": "code",
1002
1003
   "execution_count": 40,
   "metadata": {},
1004
   "outputs": [
1005
    {
1006
1007
     "data": {
      "text/plain": [
1008
       "spot.acc_cond(4, \"(Fin(0) | Inf(1)) & (Fin(2) | Inf(3))\")"
1009
1010
      ]
     },
1011
     "execution_count": 40,
1012
     "metadata": {},
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc = spot.acc_cond('Streett 2')\n",
    "acc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The above is in fact just syntactic sugar for:"
   ]
  },
  {
   "cell_type": "code",
1030
1031
   "execution_count": 41,
   "metadata": {},
1032
   "outputs": [
1033
    {
1034
1035
     "data": {
      "text/plain": [
1036
       "spot.acc_cond(4, \"(Fin(0) | Inf(1)) & (Fin(2) | Inf(3))\")"
1037
1038
      ]
     },
1039
     "execution_count": 41,
1040
     "metadata": {},
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
     "output_type": "execute_result"
    }
   ],
   "source": [
    "code = spot.acc_code('Streett 2')\n",
    "acc = spot.acc_cond(code.used_sets().max_set(), code)\n",
    "acc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The common scenario of setting generalized Büchi acceptance can be achieved more efficiently by first setting the number of acceptance sets, and then requiring generalized Büchi acceptance:"
   ]
  },
  {
   "cell_type": "code",
1059
1060
   "execution_count": 42,
   "metadata": {},
1061
   "outputs": [
1062
    {
1063
1064
     "data": {
      "text/plain": [
1065
       "spot.acc_cond(4, \"Inf(0)&Inf(1)&Inf(2)&Inf(3)\")"
1066
1067
      ]
     },
1068
     "execution_count": 42,
1069
     "metadata": {},
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc = spot.acc_cond(4)\n",
    "acc.set_generalized_buchi()\n",
    "acc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `acc_cond` class has several methods for detecting acceptance conditions that match the named acceptance conditions of the HOA format.  Note that in the HOA format, `Inf(0)&Inf(1)&Inf(2)&Inf(3)` is only called generalized Büchi if exactly 4 acceptance sets are used.  So the following behavior should not be surprising:"
   ]
  },
  {
   "cell_type": "code",
1088
1089
   "execution_count": 43,
   "metadata": {},
1090
1091
1092
1093
1094
1095
1096
1097
1098
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4, Inf(0)&Inf(1)&Inf(2)&Inf(3))\n",
      "True\n",
      "(5, Inf(0)&Inf(1)&Inf(2)&Inf(3))\n",
      "False\n"
1099
     ]
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
    }
   ],
   "source": [
    "print(acc)\n",
    "print(acc.is_generalized_buchi())\n",
    "acc.add_sets(1)\n",
    "print(acc)\n",
    "print(acc.is_generalized_buchi())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Similar methods like `is_t()`, `is_f()`, `is_buchi()`, `is_co_buchi()`, `is_generalized_co_buchi()` all return a Boolean.\n",
    "\n",
    "The `is_rabin()` and `is_streett()` methods, however, return a number of pairs.  The number of pairs is always `num_sets()/2` on success, or -1 on failure."
   ]
  },
  {
   "cell_type": "code",
1121
1122
   "execution_count": 44,
   "metadata": {},
1123
1124
1125
1126
1127
1128
1129
1130
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4, (Fin(0) & Inf(1)) | (Fin(2) & Inf(3)))\n",
      "2\n",
      "-1\n"
1131
     ]
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
    }
   ],
   "source": [
    "acc = spot.acc_cond('Rabin 2')\n",
    "print(acc)\n",
    "print(acc.is_rabin())\n",
    "print(acc.is_streett())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The check for parity acceptance returns three Boolean in a list of the form `[matched, max?, odd?]`.  If `matched` is `False`, the other values should be ignored."
   ]
  },
  {
   "cell_type": "code",
1150
1151
   "execution_count": 45,
   "metadata": {},
1152
1153
1154
1155
1156
1157
1158
1159
1160
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4, Fin(0) & (Inf(1) | (Fin(2) & Inf(3))))\n",
      "[True, False, True]\n",
      "(4, Inf(0)&Inf(1)&Inf(2)&Inf(3))\n",
      "[False, False, False]\n"
1161
     ]
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
    }
   ],
   "source": [
    "acc = spot.acc_cond('parity min odd 4')\n",
    "print(acc)\n",
    "print(acc.is_parity())\n",
    "acc.set_generalized_buchi()\n",
    "print(acc)\n",
    "print(acc.is_parity())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If the acceptance condition has some known name, it can be retrieved using the `name()` method.  By default the name given is a human-readable string close that used in the HOA format, but with proper accents, and support for name like `Streett-like` or `Rabin-like`.  The argument `arg` can specify a different style using the same syntax as in `--format='%[arg]g'` when using the command-line tools."
   ]
  },
  {
   "cell_type": "code",
1182
1183
   "execution_count": 46,
   "metadata": {},
1184
1185
1186
1187
1188
1189
1190
1191
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "generalized-Büchi 4\n",
      "gen. Büchi 4\n",
      "generalized-Buchi\n"
1192
     ]
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
    }
   ],
   "source": [
    "print(acc.name())\n",
    "print(acc.name(\"d\"))  # <- Style used by print_dot(aut, \"a\")\n",
    "print(acc.name(\"0\"))  # <- no parameters"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`acc_cond` contains a few functions for manipulating `mark_t` instances, these are typically functions that require known the total number of accepting sets declared.\n",
    "\n",
    "For instance complementing a `mark_t`:"
   ]
  },
  {
   "cell_type": "code",
1212
1213
   "execution_count": 47,
   "metadata": {},
1214
   "outputs": [
1215
    {
1216
1217
1218
1219
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0,2}\n"
1220
     ]
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
    }
   ],
   "source": [
    "m = spot.mark_t([1, 3])\n",
    "print(acc.comp(m))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`all_sets()` returns a `mark_t` listing all the declared sets: "
   ]
  },
  {
   "cell_type": "code",
1237
1238
   "execution_count": 48,
   "metadata": {},
1239
   "outputs": [
1240
    {
1241
1242
     "data": {
      "text/plain": [
1243
       "spot.mark_t([0, 1, 2, 3])"
1244
1245
      ]
     },
1246
     "execution_count": 48,
1247
     "metadata": {},
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.all_sets()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For convencience, the `accepting()` method of `acc_cond` delegates to that of the `acc_code`.  \n",
    "Any set passed to `accepting()` that is not used by the acceptance formula has no influence."
   ]
  },
  {
   "cell_type": "code",
1265
1266
   "execution_count": 49,
   "metadata": {},
1267
1268
1269
1270
1271
1272
1273
1274
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "acc = (4, Inf(0)&Inf(1)&Inf(2)&Inf(3))\n",
      "acc.accepting([0, 1, 2, 3, 10]) = True\n",
      "acc.accepting([1, 2]) = False\n"
1275
     ]
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
    }
   ],
   "source": [
    "print(\"acc =\", acc)\n",
    "for x in ([0, 1, 2, 3, 10], [1, 2]):\n",
    "    print(\"acc.accepting({}) = {}\".format(x, acc.accepting(x)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally the `unsat_mark()` method of `acc_cond` computes an instance of `mark_t` that is unaccepting (i.e., passing this value to `acc.accepting(...)` will return `False` when such a value exist.  Not all acceptance conditions have an satisfiable mark.  Obviously the `t` acceptance is always satisfiable, and so are all equivalent acceptances (for instance `Fin(1)|Inf(1)`).\n",
    "\n",
    "For this reason, `unsat_mark()` actually returns a pair: `(bool, mark_t)` where the Boolean is `False` iff the acceptance is always satisfiable.  When the Boolean is `True`, then the second element of the pair gives a non-accepting mark."
   ]
  },
  {
   "cell_type": "code",
1295
1296
   "execution_count": 50,
   "metadata": {},
1297
   "outputs": [
1298
    {
1299
1300
1301
1302
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4, Inf(0)&Inf(1)&Inf(2)&Inf(3))\n",
1303
      "(True, spot.mark_t([]))\n"
1304
     ]
1305
1306
1307
1308
1309
1310
1311
1312
1313
    }
   ],
   "source": [
    "print(acc)\n",
    "print(acc.unsat_mark())"
   ]
  },
  {
   "cell_type": "code",
1314
1315
   "execution_count": 51,
   "metadata": {},
1316
   "outputs": [
1317
    {
1318
1319
1320
1321
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, t)\n",
1322
      "(False, spot.mark_t([]))\n"
1323
     ]
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
    }
   ],
   "source": [
    "acc = spot.acc_cond(0)   # use 0 acceptance sets, and the default formula (t)\n",
    "print(acc)\n",
    "print(acc.unsat_mark())"
   ]
  },
  {
   "cell_type": "code",
1334
1335
   "execution_count": 52,
   "metadata": {},
1336
   "outputs": [
1337
    {
1338
1339
1340
1341
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4, (Fin(0) | Inf(1)) & (Fin(2) | Inf(3)))\n",
1342
      "(True, spot.mark_t([2]))\n"
1343
     ]
1344
1345
    }
   ],
1346
1347
1348
1349
1350
   "source": [
    "acc = spot.acc_cond('Streett 2')\n",
    "print(acc)\n",
    "print(acc.unsat_mark())"
   ]
1351
  }
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
1369
   "version": "3.6.7"
1370
1371
1372
1373
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
1374
}