spot/tests/python/formulas.ipynb
Alexandre Duret-Lutz 6fac026454 implement SVA's first_match operator
* NEWS, doc/tl/tl.tex, doc/tl/tl.bib: Document it.
* spot/parsetl/parsetl.yy, spot/parsetl/scantl.ll: Parse it.
* spot/tl/formula.cc, spot/tl/formula.hh, spot/tl/dot.cc,
spot/tl/mutation.cc, spot/tl/print.cc, spot/tl/randomltl.cc,
spot/twaalgos/ltl2tgba_fm.cc: Adjust to support first_match.
* spot/tl/mark.cc, spot/tl/simplify.cc, spot/tl/snf.cc,
spot/tl/unabbrev.cc, spot/twa/formula2bdd.cc,
spot/twaalgos/ltl2taa.cc: Ignore it.
* tests/core/acc_word.test, tests/core/randpsl.test: Add more tests.
* tests/core/rand.test, tests/core/unambig.test,
tests/python/randltl.ipynb: Adjust.
* tests/python/formulas.ipynb: Show first_match.
2019-05-06 15:11:30 +02:00

973 lines
33 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Handling LTL and PSL formulas\n",
"============================="
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import spot\n",
"from IPython.display import display # not needed with recent Jupyter"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For interactive use, formulas can be entered as text strings and passed to the `spot.formula` constructor."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$p_{1} \\mathbin{\\mathsf{U}} (p_{2} \\mathbin{\\mathsf{R}} (p_{3} \\land \\lnot p_{4}))$"
],
"text/plain": [
"spot.formula(\"p1 U (p2 R (p3 & !p4))\")"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f = spot.formula('p1 U p2 R (p3 & !p4)')\n",
"f"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\{a \\mathbin{\\mathsf{;}} \\mathsf{first\\_match}(b^{\\star} \\mathbin{\\mathsf{;}} c^+ \\mathbin{\\mathsf{;}} d)\\}\\mathrel{\\Diamond\\kern-1.7pt\\raise.4pt\\hbox{$\\mathord{\\rightarrow}$}} (c \\land \\mathsf{G} \\mathsf{F} b)$"
],
"text/plain": [
"spot.formula(\"{a;first_match(b[*];c[+];d)}<>-> (c & GFb)\")"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g = spot.formula('{a;first_match(b*;c[+];d)}<>->(GFb & c)'); g"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By default the parser recognizes an infix syntax, but when this fails, it tries to read the formula with the [LBT](http://www.tcs.hut.fi/Software/maria/tools/lbt/) syntax:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$c \\land (a \\lor b)$"
],
"text/plain": [
"spot.formula(\"c & (a | b)\")"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"h = spot.formula('& | a b c'); h"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Passing a `formula` to `spot.formula` simply returns the formula."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$c \\land (a \\lor b)$"
],
"text/plain": [
"spot.formula(\"c & (a | b)\")"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spot.formula(h)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By default, a formula object is presented using mathjax as above.\n",
"When a formula is converted to string you get Spot's syntax by default:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'p1 U (p2 R (p3 & !p4))'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"str(f)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you prefer to print the string in another syntax, you may use the `to_str()` method, with an argument that indicates the output format to use. The `latex` format assumes that you will the define macros such as `\\U`, `\\R` to render all operators as you wish. On the otherhand, the `sclatex` (with `sc` for self-contained) format hard-codes the rendering of each of those operators: this is almost the same output that is used to render formulas using MathJax in a notebook. `sclatex` and `mathjax` only differ in the rendering of double-quoted atomic propositions."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"spot p1 U (p2 R (p3 & !p4))\n",
"spin p1 U (p2 V (p3 && !p4))\n",
"lbt U p1 V p2 & p3 ! p4\n",
"wring (p1=1) U ((p2=1) R ((p3=1) * (p4=0)))\n",
"utf8 p1 U (p2 R (p3∧¬p4))\n",
"latex p_{1} \\U (p_{2} \\R (p_{3} \\land \\lnot p_{4}))\n",
"sclatex p_{1} \\mathbin{\\mathsf{U}} (p_{2} \\mathbin{\\mathsf{R}} (p_{3} \\land \\lnot p_{4}))\n",
"mathjax p_{1} \\mathbin{\\mathsf{U}} (p_{2} \\mathbin{\\mathsf{R}} (p_{3} \\land \\lnot p_{4}))\n"
]
}
],
"source": [
"for i in ['spot', 'spin', 'lbt', 'wring', 'utf8', 'latex', 'sclatex', 'mathjax']:\n",
" print(\"%-10s%s\" % (i, f.to_str(i)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Formulas output via `format()` can also use some convenient shorthand to select the syntax:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Spin: p1 U (p2 V (p3 && !p4))\n",
"Spin+parentheses: (p1) U ((p2) V ((p3) && (!(p4))))\n",
"Spot (default): p1 U (p2 R (p3 & !p4))\n",
"Spot+shell quotes: 'p1 U (p2 R (p3 & !p4))'\n",
"LBT, right aligned: ~~~~~~~~~~~~~~~~~~~~~U p1 V p2 & p3 ! p4\n",
"LBT, no M/W/R: U p1 U & p3 ! p4 | & & p2 p3 ! p4 G & p3 ! p4\n"
]
}
],
"source": [
"print(\"\"\"\\\n",
"Spin: {0:s}\n",
"Spin+parentheses: {0:sp}\n",
"Spot (default): {0}\n",
"Spot+shell quotes: {0:q}\n",
"LBT, right aligned: {0:l:~>40}\n",
"LBT, no M/W/R: {0:[MWR]l}\"\"\".format(f))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The specifiers that can be used with `format` are documented as follows:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function __format__ in module spot:\n",
"\n",
"__format__(self, spec)\n",
" Format the formula according to `spec`.\n",
" \n",
" Parameters\n",
" ----------\n",
" spec : str, optional\n",
" a list of letters that specify how the formula\n",
" should be formatted.\n",
" \n",
" Supported specifiers\n",
" --------------------\n",
" \n",
" - 'f': use Spot's syntax (default)\n",
" - '8': use Spot's syntax in UTF-8 mode\n",
" - 's': use Spin's syntax\n",
" - 'l': use LBT's syntax\n",
" - 'w': use Wring's syntax\n",
" - 'x': use LaTeX output\n",
" - 'X': use self-contained LaTeX output\n",
" - 'j': use self-contained LaTeX output, adjusted for MathJax\n",
" \n",
" Add some of those letters for additional options:\n",
" \n",
" - 'p': use full parentheses\n",
" - 'c': escape the formula for CSV output (this will\n",
" enclose the formula in double quotes, and escape\n",
" any included double quotes)\n",
" - 'h': escape the formula for HTML output\n",
" - 'd': escape double quotes and backslash,\n",
" for use in C-strings (the outermost double\n",
" quotes are *not* added)\n",
" - 'q': quote and escape for shell output, using single\n",
" quotes or double quotes depending on the contents.\n",
" - '[...]': rewrite away all the operators specified in brackets,\n",
" using spot.unabbreviate().\n",
" \n",
" - ':spec': pass the remaining specification to the\n",
" formating function for strings.\n",
"\n"
]
}
],
"source": [
"help(spot.formula.__format__)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A `spot.formula` object has a number of built-in predicates whose value have been computed when the formula was constructed. For instance you can check whether a formula is in negative normal form using `is_in_nenoform()`, and you can make sure it is an LTL formula (i.e. not a PSL formula) using `is_ltl_formula()`:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f.is_in_nenoform() and f.is_ltl_formula()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g.is_ltl_formula()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Similarly, `is_syntactic_stutter_invariant()` tells wether the structure of the formula guarranties it to be stutter invariant. For LTL formula, this means the `X` operator should not be used. For PSL formula, this function capture all formulas built using the [siPSL grammar](http://www.daxc.de/eth/paper/09atva.pdf)."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f.is_syntactic_stutter_invariant()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spot.formula('{a[*];b}<>->c').is_syntactic_stutter_invariant()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spot.formula('{a[+];b[*]}<>->d').is_syntactic_stutter_invariant()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`spot.relabel` renames the atomic propositions that occur in a formula, using either letters, or numbered propositions:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\unicode{x201C}\\mathit{a > b}\\unicode{x201D} \\land \\unicode{x201C}\\mathit{proc[2]@init}\\unicode{x201D} \\land \\mathsf{G} \\mathsf{F} \\mathit{\\_foo\\_}$"
],
"text/plain": [
"spot.formula(\"\\\"a > b\\\" & \\\"proc[2]@init\\\" & GF_foo_\")"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gf = spot.formula('(GF_foo_) && \"a > b\" && \"proc[2]@init\"'); gf"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$a \\land b \\land \\mathsf{G} \\mathsf{F} c$"
],
"text/plain": [
"spot.formula(\"a & b & GFc\")"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spot.relabel(gf, spot.Abc)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$p_{0} \\land p_{1} \\land \\mathsf{G} \\mathsf{F} p_{2}$"
],
"text/plain": [
"spot.formula(\"p0 & p1 & GFp2\")"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spot.relabel(gf, spot.Pnn)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The AST of any formula can be displayed with `show_ast()`. Despite the name, this is not a tree but a DAG, because identical subtrees are merged. Binary operators have their left and right operands denoted with `L` and `R`, while non-commutative n-ary operators have their operands numbered."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{a;first_match(b[*];c[+];d)}<>-> (c & GFb)\n"
]
},
{
"data": {
"image/svg+xml": [
"<svg height=\"404pt\" viewBox=\"0.00 0.00 371.00 404.00\" width=\"371pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g class=\"graph\" id=\"graph0\" transform=\"scale(1 1) rotate(0) translate(4 400)\">\n",
"<title>G</title>\n",
"<polygon fill=\"#ffffff\" points=\"-4,4 -4,-400 367,-400 367,4 -4,4\" stroke=\"transparent\"/>\n",
"<!-- 0 -->\n",
"<g class=\"node\" id=\"node1\">\n",
"<title>0</title>\n",
"<ellipse cx=\"161\" cy=\"-378\" fill=\"none\" rx=\"40.8928\" ry=\"18\" stroke=\"#000000\"/>\n",
"<text fill=\"#000000\" font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"161\" y=\"-374.3\">EConcat</text>\n",
"</g>\n",
"<!-- 1 -->\n",
"<g class=\"node\" id=\"node2\">\n",
"<title>1</title>\n",
"<ellipse cx=\"129\" cy=\"-306\" fill=\"none\" rx=\"35.9954\" ry=\"18\" stroke=\"#000000\"/>\n",
"<text fill=\"#000000\" font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"129\" y=\"-302.3\">Concat</text>\n",
"</g>\n",
"<!-- 0&#45;&gt;1 -->\n",
"<g class=\"edge\" id=\"edge9\">\n",
"<title>0-&gt;1</title>\n",
"<path d=\"M153.0899,-360.2022C149.4473,-352.0064 145.0455,-342.1024 141.0065,-333.0145\" fill=\"none\" stroke=\"#000000\"/>\n",
"<polygon fill=\"#000000\" points=\"144.0789,-331.3096 136.8191,-323.593 137.6822,-334.1527 144.0789,-331.3096\" stroke=\"#000000\"/>\n",
"<text fill=\"#000000\" font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"148.5899\" y=\"-349.0022\">L</text>\n",
"</g>\n",
"<!-- 10 -->\n",
"<g class=\"node\" id=\"node11\">\n",
"<title>10</title>\n",
"<ellipse cx=\"225\" cy=\"-234\" fill=\"none\" rx=\"27\" ry=\"18\" stroke=\"#000000\"/>\n",
"<text fill=\"#000000\" font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"225\" y=\"-230.3\">And</text>\n",
"</g>\n",
"<!-- 0&#45;&gt;10 -->\n",
"<g class=\"edge\" id=\"edge14\">\n",
"<title>0-&gt;10</title>\n",
"<path d=\"M168.9762,-360.0535C180.0668,-335.0998 200.23,-289.7326 213.1015,-260.7717\" fill=\"none\" stroke=\"#000000\"/>\n",
"<polygon fill=\"#000000\" points=\"216.4602,-261.8321 217.3233,-251.2725 210.0635,-258.9891 216.4602,-261.8321\" stroke=\"#000000\"/>\n",
"<text fill=\"#000000\" font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"163.9762\" y=\"-348.8535\">R</text>\n",
"</g>\n",
"<!-- 2 -->\n",
"<g class=\"node\" id=\"node3\">\n",
"<title>2</title>\n",
"<polygon fill=\"none\" points=\"54,-36 0,-36 0,0 54,0 54,-36\" stroke=\"#000000\"/>\n",
"<text fill=\"#000000\" font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"27\" y=\"-14.3\">a</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;2 -->\n",
"<g class=\"edge\" id=\"edge1\">\n",
"<title>1-&gt;2</title>\n",
"<path d=\"M106.7369,-291.6428C93.666,-282.0164 77.9949,-268.1646 69,-252 31.7473,-185.0541 26.4358,-92.1681 26.3309,-46.1548\" fill=\"none\" stroke=\"#000000\"/>\n",
"<polygon fill=\"#000000\" points=\"29.8315,-46.0506 26.3923,-36.0296 22.8317,-46.0081 29.8315,-46.0506\" stroke=\"#000000\"/>\n",
"<text fill=\"#000000\" font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"103.2369\" y=\"-280.4428\">1</text>\n",
"</g>\n",
"<!-- 3 -->\n",
"<g class=\"node\" id=\"node4\">\n",
"<title>3</title>\n",
"<ellipse cx=\"129\" cy=\"-234\" fill=\"none\" rx=\"51.1914\" ry=\"18\" stroke=\"#000000\"/>\n",
"<text fill=\"#000000\" font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"129\" y=\"-230.3\">first_match</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;3 -->\n",
"<g class=\"edge\" id=\"edge8\">\n",
"<title>1-&gt;3</title>\n",
"<path d=\"M129,-287.8314C129,-280.131 129,-270.9743 129,-262.4166\" fill=\"none\" stroke=\"#000000\"/>\n",
"<polygon fill=\"#000000\" points=\"132.5001,-262.4132 129,-252.4133 125.5001,-262.4133 132.5001,-262.4132\" stroke=\"#000000\"/>\n",
"<text fill=\"#000000\" font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"125.5\" y=\"-276.6314\">2</text>\n",
"</g>\n",
"<!-- 4 -->\n",
"<g class=\"node\" id=\"node5\">\n",
"<title>4</title>\n",
"<ellipse cx=\"137\" cy=\"-162\" fill=\"none\" rx=\"35.9954\" ry=\"18\" stroke=\"#000000\"/>\n",
"<text fill=\"#000000\" font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"137\" y=\"-158.3\">Concat</text>\n",
"</g>\n",
"<!-- 3&#45;&gt;4 -->\n",
"<g class=\"edge\" id=\"edge7\">\n",
"<title>3-&gt;4</title>\n",
"<path d=\"M131.0187,-215.8314C131.8743,-208.131 132.8917,-198.9743 133.8426,-190.4166\" fill=\"none\" stroke=\"#000000\"/>\n",
"<polygon fill=\"#000000\" points=\"137.3283,-190.7386 134.9541,-180.4133 130.3711,-189.9656 137.3283,-190.7386\" stroke=\"#000000\"/>\n",
"</g>\n",
"<!-- 5 -->\n",
"<g class=\"node\" id=\"node6\">\n",
"<title>5</title>\n",
"<ellipse cx=\"264\" cy=\"-90\" fill=\"none\" rx=\"27\" ry=\"18\" stroke=\"#000000\"/>\n",
"<text fill=\"#000000\" font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"264\" y=\"-86.3\">Star</text>\n",
"</g>\n",
"<!-- 4&#45;&gt;5 -->\n",
"<g class=\"edge\" id=\"edge3\">\n",
"<title>4-&gt;5</title>\n",
"<path d=\"M160.8877,-148.4574C181.7279,-136.6424 212.0459,-119.4543 234.416,-106.772\" fill=\"none\" stroke=\"#000000\"/>\n",
"<polygon fill=\"#000000\" points=\"236.191,-109.7891 243.1641,-101.8125 232.7387,-103.6997 236.191,-109.7891\" stroke=\"#000000\"/>\n",
"<text fill=\"#000000\" font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"157.3877\" y=\"-137.2574\">1</text>\n",
"</g>\n",
"<!-- 7 -->\n",
"<g class=\"node\" id=\"node8\">\n",
"<title>7</title>\n",
"<ellipse cx=\"154\" cy=\"-90\" fill=\"none\" rx=\"27\" ry=\"18\" stroke=\"#000000\"/>\n",
"<text fill=\"#000000\" font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"154\" y=\"-86.3\">Star</text>\n",
"</g>\n",
"<!-- 4&#45;&gt;7 -->\n",
"<g class=\"edge\" id=\"edge5\">\n",
"<title>4-&gt;7</title>\n",
"<path d=\"M141.2898,-143.8314C143.1562,-135.9266 145.385,-126.4872 147.4511,-117.7365\" fill=\"none\" stroke=\"#000000\"/>\n",
"<polygon fill=\"#000000\" points=\"150.8788,-118.4501 149.7704,-107.9134 144.0661,-116.8415 150.8788,-118.4501\" stroke=\"#000000\"/>\n",
"<text fill=\"#000000\" font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"137.7898\" y=\"-132.6314\">2</text>\n",
"</g>\n",
"<!-- 9 -->\n",
"<g class=\"node\" id=\"node10\">\n",
"<title>9</title>\n",
"<polygon fill=\"none\" points=\"126,-36 72,-36 72,0 126,0 126,-36\" stroke=\"#000000\"/>\n",
"<text fill=\"#000000\" font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"99\" y=\"-14.3\">d</text>\n",
"</g>\n",
"<!-- 4&#45;&gt;9 -->\n",
"<g class=\"edge\" id=\"edge6\">\n",
"<title>4-&gt;9</title>\n",
"<path d=\"M130.0742,-144.187C126.1822,-133.7494 121.4322,-120.2494 118,-108 112.2642,-87.5291 107.3558,-64.0109 103.9655,-46.1505\" fill=\"none\" stroke=\"#000000\"/>\n",
"<polygon fill=\"#000000\" points=\"107.3661,-45.2923 102.1034,-36.097 100.4832,-46.5672 107.3661,-45.2923\" stroke=\"#000000\"/>\n",
"<text fill=\"#000000\" font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"126.5742\" y=\"-132.987\">3</text>\n",
"</g>\n",
"<!-- 6 -->\n",
"<g class=\"node\" id=\"node7\">\n",
"<title>6</title>\n",
"<polygon fill=\"none\" points=\"299,-36 245,-36 245,0 299,0 299,-36\" stroke=\"#000000\"/>\n",
"<text fill=\"#000000\" font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"272\" y=\"-14.3\">b</text>\n",
"</g>\n",
"<!-- 5&#45;&gt;6 -->\n",
"<g class=\"edge\" id=\"edge2\">\n",
"<title>5-&gt;6</title>\n",
"<path d=\"M266.0187,-71.8314C266.8743,-64.131 267.8917,-54.9743 268.8426,-46.4166\" fill=\"none\" stroke=\"#000000\"/>\n",
"<polygon fill=\"#000000\" points=\"272.3283,-46.7386 269.9541,-36.4133 265.3711,-45.9656 272.3283,-46.7386\" stroke=\"#000000\"/>\n",
"</g>\n",
"<!-- 8 -->\n",
"<g class=\"node\" id=\"node9\">\n",
"<title>8</title>\n",
"<polygon fill=\"none\" points=\"213,-36 159,-36 159,0 213,0 213,-36\" stroke=\"#000000\"/>\n",
"<text fill=\"#000000\" font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"186\" y=\"-14.3\">c</text>\n",
"</g>\n",
"<!-- 7&#45;&gt;8 -->\n",
"<g class=\"edge\" id=\"edge4\">\n",
"<title>7-&gt;8</title>\n",
"<path d=\"M161.7463,-72.5708C165.398,-64.3544 169.8396,-54.3608 173.9191,-45.1821\" fill=\"none\" stroke=\"#000000\"/>\n",
"<polygon fill=\"#000000\" points=\"177.1277,-46.5802 177.9908,-36.0206 170.7311,-43.7372 177.1277,-46.5802\" stroke=\"#000000\"/>\n",
"</g>\n",
"<!-- 10&#45;&gt;8 -->\n",
"<g class=\"edge\" id=\"edge10\">\n",
"<title>10-&gt;8</title>\n",
"<path d=\"M221.742,-215.9555C214.9457,-178.3144 199.2143,-91.1867 191.078,-46.1246\" fill=\"none\" stroke=\"#000000\"/>\n",
"<polygon fill=\"#000000\" points=\"194.4974,-45.364 189.2762,-36.1451 187.6088,-46.6079 194.4974,-45.364\" stroke=\"#000000\"/>\n",
"</g>\n",
"<!-- 11 -->\n",
"<g class=\"node\" id=\"node12\">\n",
"<title>11</title>\n",
"<ellipse cx=\"300\" cy=\"-162\" fill=\"none\" rx=\"27\" ry=\"18\" stroke=\"#000000\"/>\n",
"<text fill=\"#000000\" font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"300\" y=\"-158.3\">G</text>\n",
"</g>\n",
"<!-- 10&#45;&gt;11 -->\n",
"<g class=\"edge\" id=\"edge13\">\n",
"<title>10-&gt;11</title>\n",
"<path d=\"M240.54,-219.0816C251.099,-208.945 265.2444,-195.3654 277.0892,-183.9944\" fill=\"none\" stroke=\"#000000\"/>\n",
"<polygon fill=\"#000000\" points=\"279.6898,-186.3495 284.4799,-176.8993 274.8421,-181.2998 279.6898,-186.3495\" stroke=\"#000000\"/>\n",
"</g>\n",
"<!-- 12 -->\n",
"<g class=\"node\" id=\"node13\">\n",
"<title>12</title>\n",
"<ellipse cx=\"336\" cy=\"-90\" fill=\"none\" rx=\"27\" ry=\"18\" stroke=\"#000000\"/>\n",
"<text fill=\"#000000\" font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"336\" y=\"-86.3\">F</text>\n",
"</g>\n",
"<!-- 11&#45;&gt;12 -->\n",
"<g class=\"edge\" id=\"edge12\">\n",
"<title>11-&gt;12</title>\n",
"<path d=\"M308.7146,-144.5708C312.9597,-136.0807 318.1536,-125.6929 322.8663,-116.2674\" fill=\"none\" stroke=\"#000000\"/>\n",
"<polygon fill=\"#000000\" points=\"326.024,-117.7782 327.3657,-107.2687 319.763,-114.6477 326.024,-117.7782\" stroke=\"#000000\"/>\n",
"</g>\n",
"<!-- 12&#45;&gt;6 -->\n",
"<g class=\"edge\" id=\"edge11\">\n",
"<title>12-&gt;6</title>\n",
"<path d=\"M322.113,-74.3771C314.1445,-65.4125 303.9417,-53.9344 294.8244,-43.6774\" fill=\"none\" stroke=\"#000000\"/>\n",
"<polygon fill=\"#000000\" points=\"297.3785,-41.2826 288.1189,-36.1338 292.1466,-45.9332 297.3785,-41.2826\" stroke=\"#000000\"/>\n",
"</g>\n",
"</g>\n",
"</svg>"
],
"text/plain": [
"<IPython.core.display.SVG object>"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(g); g.show_ast()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Any formula can also be classified in the temporal hierarchy of Manna & Pnueli"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<svg height=\"210\" version=\"1.1\" width=\"220\" xmlns=\"http://www.w3.org/2000/svg\">\n",
"<polygon fill=\"cyan\" opacity=\".2\" points=\"20,0 200,120 200,210 20,210\"/>\n",
"<polygon fill=\"cyan\" opacity=\".2\" points=\"20,120 155,210 20,210\"/>\n",
"<polygon fill=\"magenta\" opacity=\".15\" points=\"200,0 20,120 20,210 200,210\"/>\n",
"<polygon fill=\"magenta\" opacity=\".15\" points=\"200,120 65,210 200,210\"/>\n",
"<g transform=\"translate(40,80)\">\n",
" <line stroke=\"red\" stroke-width=\"5\" x1=\"-10\" x2=\"10\" y1=\"-10\" y2=\"10\"/>\n",
" <line stroke=\"red\" stroke-width=\"5\" x1=\"-10\" x2=\"10\" y1=\"10\" y2=\"-10\"/>\n",
" </g>\n",
"<g font-size=\"14\" text-anchor=\"middle\">\n",
"<text x=\"110\" y=\"20\">Reactivity</text>\n",
"<text x=\"60\" y=\"65\">Recurrence</text>\n",
"<text x=\"160\" y=\"65\">Persistence</text>\n",
"<text x=\"110\" y=\"125\">Obligation</text>\n",
"<text x=\"60\" y=\"185\">Safety</text>\n",
"<text x=\"160\" y=\"185\">Guarantee</text>\n",
"</g>\n",
"<g font-size=\"14\">\n",
"<text fill=\"gray\" text-anchor=\"begin\" transform=\"rotate(-90,18,210)\" x=\"18\" y=\"210\">Monitor</text>\n",
"<text fill=\"gray\" text-anchor=\"end\" transform=\"rotate(-90,18,0)\" x=\"18\" y=\"0\">Deterministic Büchi</text>\n",
"<text fill=\"gray\" text-anchor=\"begin\" transform=\"rotate(-90,214,210)\" x=\"214\" y=\"210\">Terminal Büchi</text>\n",
"<text fill=\"gray\" text-anchor=\"end\" transform=\"rotate(-90,214,0)\" x=\"214\" y=\"0\">Weak Büchi</text>\n",
"</g>\n",
"</svg>"
],
"text/plain": [
"<IPython.core.display.SVG object>"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g.show_mp_hierarchy()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'recurrence'"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spot.mp_class(g, 'v')"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\mathsf{F} (a \\land \\mathsf{X} (\\lnot a \\land b))$"
],
"text/plain": [
"spot.formula(\"F(a & X(!a & b))\")"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f = spot.formula('F(a & X(!a & b))'); f"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Etessami's rule for removing X (valid only in stutter-invariant formulas)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\mathsf{F} (a \\land ((a \\land (a \\mathbin{\\mathsf{U}} (\\lnot a \\land b)) \\land ((\\lnot b \\mathbin{\\mathsf{U}} \\lnot a) \\lor (b \\mathbin{\\mathsf{U}} \\lnot a))) \\lor (\\lnot a \\land (\\lnot a \\mathbin{\\mathsf{U}} (a \\land \\lnot a \\land b)) \\land ((\\lnot b \\mathbin{\\mathsf{U}} a) \\lor (b \\mathbin{\\mathsf{U}} a))) \\lor (b \\land (b \\mathbin{\\mathsf{U}} (\\lnot a \\land b \\land \\lnot b)) \\land ((\\lnot a \\mathbin{\\mathsf{U}} \\lnot b) \\lor (a \\mathbin{\\mathsf{U}} \\lnot b))) \\lor (\\lnot b \\land (\\lnot b \\mathbin{\\mathsf{U}} (\\lnot a \\land b)) \\land ((\\lnot a \\mathbin{\\mathsf{U}} b) \\lor (a \\mathbin{\\mathsf{U}} b))) \\lor (\\lnot a \\land b \\land (\\mathsf{G} \\lnot a \\lor \\mathsf{G} a) \\land (\\mathsf{G} \\lnot b \\lor \\mathsf{G} b))))$"
],
"text/plain": [
"spot.formula(\"F(a & ((a & (a U (!a & b)) & ((!b U !a) | (b U !a))) | (!a & (!a U (a & !a & b)) & ((!b U a) | (b U a))) | (b & (b U (!a & b & !b)) & ((!a U !b) | (a U !b))) | (!b & (!b U (!a & b)) & ((!a U b) | (a U b))) | (!a & b & (G!a | Ga) & (G!b | Gb))))\")"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spot.remove_x(f)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Removing abbreviated operators"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$(\\bot \\mathbin{\\mathsf{R}} \\lnot (a \\leftrightarrow b)) \\rightarrow (\\top \\mathbin{\\mathsf{U}} (a \\leftrightarrow b))$"
],
"text/plain": [
"spot.formula(\"(0 R !(a <-> b)) -> (1 U (a <-> b))\")"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f = spot.formula(\"G(a xor b) -> F(a <-> b)\")\n",
"spot.unabbreviate(f, \"GF^\")"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$(\\top \\mathbin{\\mathsf{U}} ((a \\land b) \\lor (\\lnot a \\land \\lnot b))) \\lor \\lnot (\\bot \\mathbin{\\mathsf{R}} ((\\lnot a \\land b) \\lor (a \\land \\lnot b)))$"
],
"text/plain": [
"spot.formula(\"(1 U ((a & b) | (!a & !b))) | !(0 R ((!a & b) | (a & !b)))\")"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spot.unabbreviate(f, \"GF^ei\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nesting level of operators"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"U 3\n",
"F 2\n",
"U 3\n",
"F 2\n",
"FU 4\n"
]
}
],
"source": [
"f = spot.formula('F(b & X(a U b U ((a W Fb) | (c U d))))')\n",
"print(\"U\", spot.nesting_depth(f, spot.op_U))\n",
"print(\"F\", spot.nesting_depth(f, spot.op_F))\n",
"# These following two are syntactic sugar for the above two\n",
"print(\"U\", spot.nesting_depth(f, \"U\"))\n",
"print(\"F\", spot.nesting_depth(f, \"F\"))\n",
"# If you want to consider \"U\" and \"F\" are a similar type of\n",
"# operator, you can count both with\n",
"print(\"FU\", spot.nesting_depth(f, \"FU\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Collecting the set of atomic propositions used by a formula:"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"spot.atomic_prop_set([spot.formula(\"a\"), spot.formula(\"b\"), spot.formula(\"c\"), spot.formula(\"d\")])\n",
"{\"a\", \"b\", \"c\", \"d\"}\n"
]
},
{
"data": {
"text/latex": [
"$\\{\\unicode{x201C}a\\unicode{x201D}, \\unicode{x201C}b\\unicode{x201D}, \\unicode{x201C}c\\unicode{x201D}, \\unicode{x201C}d\\unicode{x201D}\\}$"
],
"text/plain": [
"spot.atomic_prop_set([spot.formula(\"a\"), spot.formula(\"b\"), spot.formula(\"c\"), spot.formula(\"d\")])"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"ap = spot.atomic_prop_collect(f)\n",
"print(repr(ap)) # print as an atomic_prop_set object\n",
"print(ap) # print as a string\n",
"display(ap) # LaTeX-style, for notebooks"
]
}
],
"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",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}