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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 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 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 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 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
|
/*++
Copyright (c) 2012 Microsoft Corporation
Module Name:
Program.cs
Abstract:
Z3 Managed API: Example program
Author:
Christoph Wintersteiger (cwinter) 2012-03-16
Notes:
--*/
using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Z3;
namespace test_mapi
{
class Program
{
class TestFailedException : Exception
{
public TestFailedException() : base("Check FAILED") { }
};
/// <summary>
/// Create axiom: function f is injective in the i-th argument.
/// </summary>
/// <remarks>
/// The following axiom is produced:
/// <c>
/// forall (x_0, ..., x_n) finv(f(x_0, ..., x_i, ..., x_{n-1})) = x_i
/// </c>
/// Where, <code>finv</code>is a fresh function declaration.
/// </summary>
public static BoolExpr InjAxiom(Context ctx, FuncDecl f, int i)
{
Sort[] domain = f.Domain;
uint sz = f.DomainSize;
if (i >= sz)
{
Console.WriteLine("failed to create inj axiom");
return null;
}
/* declare the i-th inverse of f: finv */
Sort finv_domain = f.Range;
Sort finv_range = domain[i];
FuncDecl finv = ctx.MkFuncDecl("f_fresh", finv_domain, finv_range);
/* allocate temporary arrays */
Expr[] xs = new Expr[sz];
Symbol[] names = new Symbol[sz];
Sort[] types = new Sort[sz];
/* fill types, names and xs */
for (uint j = 0; j < sz; j++)
{
types[j] = domain[j];
names[j] = ctx.MkSymbol(String.Format("x_{0}", j));
xs[j] = ctx.MkBound(j, types[j]);
}
Expr x_i = xs[i];
/* create f(x_0, ..., x_i, ..., x_{n-1}) */
Expr fxs = f[xs];
/* create f_inv(f(x_0, ..., x_i, ..., x_{n-1})) */
Expr finv_fxs = finv[fxs];
/* create finv(f(x_0, ..., x_i, ..., x_{n-1})) = x_i */
Expr eq = ctx.MkEq(finv_fxs, x_i);
/* use f(x_0, ..., x_i, ..., x_{n-1}) as the pattern for the quantifier */
Pattern p = ctx.MkPattern(new Expr[] { fxs });
/* create & assert quantifier */
BoolExpr q = ctx.MkForall(
types, /* types of quantified variables */
names, /* names of quantified variables */
eq,
1,
new Pattern[] { p } /* patterns */);
return q;
}
/// <summary>
/// Create axiom: function f is injective in the i-th argument.
/// </summary>
/// <remarks>
/// The following axiom is produced:
/// <c>
/// forall (x_0, ..., x_n) finv(f(x_0, ..., x_i, ..., x_{n-1})) = x_i
/// </c>
/// Where, <code>finv</code>is a fresh function declaration.
/// </summary>
public static BoolExpr InjAxiomAbs(Context ctx, FuncDecl f, int i)
{
Sort[] domain = f.Domain;
uint sz = f.DomainSize;
if (i >= sz)
{
Console.WriteLine("failed to create inj axiom");
return null;
}
/* declare the i-th inverse of f: finv */
Sort finv_domain = f.Range;
Sort finv_range = domain[i];
FuncDecl finv = ctx.MkFuncDecl("f_fresh", finv_domain, finv_range);
/* allocate temporary arrays */
Expr[] xs = new Expr[sz];
/* fill types, names and xs */
for (uint j = 0; j < sz; j++)
{
xs[j] = ctx.MkConst(String.Format("x_{0}", j), domain[j]);
}
Expr x_i = xs[i];
/* create f(x_0, ..., x_i, ..., x_{n-1}) */
Expr fxs = f[xs];
/* create f_inv(f(x_0, ..., x_i, ..., x_{n-1})) */
Expr finv_fxs = finv[fxs];
/* create finv(f(x_0, ..., x_i, ..., x_{n-1})) = x_i */
Expr eq = ctx.MkEq(finv_fxs, x_i);
/* use f(x_0, ..., x_i, ..., x_{n-1}) as the pattern for the quantifier */
Pattern p = ctx.MkPattern(new Expr[] { fxs });
/* create & assert quantifier */
BoolExpr q = ctx.MkForall(
xs, /* types of quantified variables */
eq, /* names of quantified variables */
1,
new Pattern[] { p } /* patterns */);
return q;
}
/// <summary>
/// Assert the axiom: function f is commutative.
/// </summary>
/// <remarks>
/// This example uses the SMT-LIB parser to simplify the axiom construction.
/// </remarks>
private static BoolExpr CommAxiom(Context ctx, FuncDecl f)
{
Sort t = f.Range;
Sort[] dom = f.Domain;
if (dom.Length != 2 ||
!t.Equals(dom[0]) ||
!t.Equals(dom[1]))
{
Console.WriteLine("{0} {1} {2} {3}", dom.Length, dom[0], dom[1], t);
throw new Exception("function must be binary, and argument types must be equal to return type");
}
string bench = string.Format("(assert (forall ((x {0}) (y {1})) (= ({2} x y) ({3} y x))))",
t.Name, t.Name, f.Name, f.Name);
return ctx.ParseSMTLIB2String(bench, new Symbol[] { t.Name }, new Sort[] { t }, new Symbol[] { f.Name }, new FuncDecl[] { f })[0];
}
/// <summary>
/// "Hello world" example: create a Z3 logical context, and delete it.
/// </summary>
public static void SimpleExample()
{
Console.WriteLine("SimpleExample");
using (Context ctx = new Context())
{
/* do something with the context */
/* be kind to dispose manually and not wait for the GC. */
ctx.Dispose();
}
}
static Model Check(Context ctx, BoolExpr f, Status sat)
{
Solver s = ctx.MkSolver();
s.Assert(f);
if (s.Check() != sat)
throw new TestFailedException();
if (sat == Status.SATISFIABLE)
return s.Model;
else
return null;
}
static void SolveTactical(Context ctx, Tactic t, Goal g, Status sat)
{
Solver s = ctx.MkSolver(t);
Console.WriteLine("\nTactical solver: " + s);
foreach (BoolExpr a in g.Formulas)
s.Assert(a);
Console.WriteLine("Solver: " + s);
if (s.Check() != sat)
throw new TestFailedException();
}
static ApplyResult ApplyTactic(Context ctx, Tactic t, Goal g)
{
Console.WriteLine("\nGoal: " + g);
ApplyResult res = t.Apply(g);
Console.WriteLine("Application result: " + res);
Status q = Status.UNKNOWN;
foreach (Goal sg in res.Subgoals)
if (sg.IsDecidedSat) q = Status.SATISFIABLE;
else if (sg.IsDecidedUnsat) q = Status.UNSATISFIABLE;
switch (q)
{
case Status.UNKNOWN:
Console.WriteLine("Tactic result: Undecided");
break;
case Status.SATISFIABLE:
Console.WriteLine("Tactic result: SAT");
break;
case Status.UNSATISFIABLE:
Console.WriteLine("Tactic result: UNSAT");
break;
}
return res;
}
static void Prove(Context ctx, BoolExpr f, bool useMBQI = false, params BoolExpr[] assumptions)
{
Console.WriteLine("Proving: " + f);
Solver s = ctx.MkSolver();
Params p = ctx.MkParams();
p.Add("mbqi", useMBQI);
s.Parameters = p;
foreach (BoolExpr a in assumptions)
s.Assert(a);
s.Assert(ctx.MkNot(f));
Status q = s.Check();
switch (q)
{
case Status.UNKNOWN:
Console.WriteLine("Unknown because: " + s.ReasonUnknown);
break;
case Status.SATISFIABLE:
throw new TestFailedException();
case Status.UNSATISFIABLE:
Console.WriteLine("OK, proof: " + s.Proof);
break;
}
}
static void Disprove(Context ctx, BoolExpr f, bool useMBQI = false, params BoolExpr[] assumptions)
{
Console.WriteLine("Disproving: " + f);
Solver s = ctx.MkSolver();
Params p = ctx.MkParams();
p.Add("mbqi", useMBQI);
s.Parameters = p;
foreach (BoolExpr a in assumptions)
s.Assert(a);
s.Assert(ctx.MkNot(f));
Status q = s.Check();
switch (q)
{
case Status.UNKNOWN:
Console.WriteLine("Unknown because: " + s.ReasonUnknown);
break;
case Status.SATISFIABLE:
Console.WriteLine("OK, model: " + s.Model);
break;
case Status.UNSATISFIABLE:
throw new TestFailedException();
}
}
static void ModelConverterTest(Context ctx)
{
Console.WriteLine("ModelConverterTest");
ArithExpr xr = (ArithExpr)ctx.MkConst(ctx.MkSymbol("x"), ctx.MkRealSort());
ArithExpr yr = (ArithExpr)ctx.MkConst(ctx.MkSymbol("y"), ctx.MkRealSort());
Goal g4 = ctx.MkGoal(true);
g4.Assert(ctx.MkGt(xr, ctx.MkReal(10, 1)));
g4.Assert(ctx.MkEq(yr, ctx.MkAdd(xr, ctx.MkReal(1, 1))));
g4.Assert(ctx.MkGt(yr, ctx.MkReal(1, 1)));
ApplyResult ar = ApplyTactic(ctx, ctx.MkTactic("simplify"), g4);
if (ar.NumSubgoals == 1 && (ar.Subgoals[0].IsDecidedSat || ar.Subgoals[0].IsDecidedUnsat))
throw new TestFailedException();
ar = ApplyTactic(ctx, ctx.AndThen(ctx.MkTactic("simplify"), ctx.MkTactic("solve-eqs")), g4);
if (ar.NumSubgoals == 1 && (ar.Subgoals[0].IsDecidedSat || ar.Subgoals[0].IsDecidedUnsat))
throw new TestFailedException();
Solver s = ctx.MkSolver();
foreach (BoolExpr e in ar.Subgoals[0].Formulas)
s.Assert(e);
Status q = s.Check();
Console.WriteLine("Solver says: " + q);
Console.WriteLine("Model: \n" + s.Model);
if (q != Status.SATISFIABLE)
throw new TestFailedException();
}
/// <summary>
/// A simple array example.
/// </summary>
/// <param name="ctx"></param>
static void ArrayExample1(Context ctx)
{
Console.WriteLine("ArrayExample1");
Goal g = ctx.MkGoal(true);
ArraySort asort = ctx.MkArraySort(ctx.IntSort, ctx.MkBitVecSort(32));
ArrayExpr aex = (ArrayExpr)ctx.MkConst(ctx.MkSymbol("MyArray"), asort);
Expr sel = ctx.MkSelect(aex, ctx.MkInt(0));
g.Assert(ctx.MkEq(sel, ctx.MkBV(42, 32)));
Symbol xs = ctx.MkSymbol("x");
IntExpr xc = (IntExpr)ctx.MkConst(xs, ctx.IntSort);
Symbol fname = ctx.MkSymbol("f");
Sort[] domain = { ctx.IntSort };
FuncDecl fd = ctx.MkFuncDecl(fname, domain, ctx.IntSort);
Expr[] fargs = { ctx.MkConst(xs, ctx.IntSort) };
IntExpr fapp = (IntExpr)ctx.MkApp(fd, fargs);
g.Assert(ctx.MkEq(ctx.MkAdd(xc, fapp), ctx.MkInt(123)));
Solver s = ctx.MkSolver();
foreach (BoolExpr a in g.Formulas)
s.Assert(a);
Console.WriteLine("Solver: " + s);
Status q = s.Check();
Console.WriteLine("Status: " + q);
if (q != Status.SATISFIABLE)
throw new TestFailedException();
Console.WriteLine("Model = " + s.Model);
Console.WriteLine("Interpretation of MyArray:\n" + s.Model.ConstInterp(aex.FuncDecl));
Console.WriteLine("Interpretation of x:\n" + s.Model.ConstInterp(xc));
Console.WriteLine("Interpretation of f:\n" + s.Model.FuncInterp(fd));
Console.WriteLine("Interpretation of MyArray as Term:\n" + s.Model.ConstInterp(aex.FuncDecl));
}
/// <summary>
/// Prove <tt>store(a1, i1, v1) = store(a2, i2, v2) implies (i1 = i3 or i2 = i3 or select(a1, i3) = select(a2, i3))</tt>.
/// </summary>
/// <remarks>This example demonstrates how to use the array theory.</remarks>
public static void ArrayExample2(Context ctx)
{
Console.WriteLine("ArrayExample2");
Sort int_type = ctx.IntSort;
Sort array_type = ctx.MkArraySort(int_type, int_type);
ArrayExpr a1 = (ArrayExpr)ctx.MkConst("a1", array_type);
ArrayExpr a2 = ctx.MkArrayConst("a2", int_type, int_type);
Expr i1 = ctx.MkConst("i1", int_type);
Expr i2 = ctx.MkConst("i2", int_type);
Expr i3 = ctx.MkConst("i3", int_type);
Expr v1 = ctx.MkConst("v1", int_type);
Expr v2 = ctx.MkConst("v2", int_type);
Expr st1 = ctx.MkStore(a1, i1, v1);
Expr st2 = ctx.MkStore(a2, i2, v2);
Expr sel1 = ctx.MkSelect(a1, i3);
Expr sel2 = ctx.MkSelect(a2, i3);
/* create antecedent */
BoolExpr antecedent = ctx.MkEq(st1, st2);
/* create consequent: i1 = i3 or i2 = i3 or select(a1, i3) = select(a2, i3) */
BoolExpr consequent = ctx.MkOr(new BoolExpr[] { ctx.MkEq(i1, i3), ctx.MkEq(i2, i3), ctx.MkEq(sel1, sel2) });
/* prove store(a1, i1, v1) = store(a2, i2, v2) implies (i1 = i3 or i2 = i3 or select(a1, i3) = select(a2, i3)) */
BoolExpr thm = ctx.MkImplies(antecedent, consequent);
Console.WriteLine("prove: store(a1, i1, v1) = store(a2, i2, v2) implies (i1 = i3 or i2 = i3 or select(a1, i3) = select(a2, i3))");
Console.WriteLine("{0}", (thm));
Prove(ctx, thm);
}
/// <summary>
/// Show that <code>distinct(a_0, ... , a_n)</code> is
/// unsatisfiable when <code>a_i</code>'s are arrays from boolean to
/// boolean and n > 4.
/// </summary>
/// <remarks>This example also shows how to use the <code>distinct</code> construct.</remarks>
public static void ArrayExample3(Context ctx)
{
Console.WriteLine("ArrayExample3");
for (int n = 2; n <= 5; n++)
{
Console.WriteLine("n = {0}", n);
Sort bool_type = ctx.MkBoolSort();
Sort array_type = ctx.MkArraySort(bool_type, bool_type);
Expr[] a = new Expr[n];
/* create arrays */
for (int i = 0; i < n; i++)
{
a[i] = ctx.MkConst(String.Format("array_{0}", i), array_type);
}
/* assert distinct(a[0], ..., a[n]) */
BoolExpr d = ctx.MkDistinct(a);
Console.WriteLine("{0}", (d));
/* context is satisfiable if n < 5 */
Model model = Check(ctx, d, n < 5 ? Status.SATISFIABLE : Status.UNSATISFIABLE);
if (n < 5)
{
for (int i = 0; i < n; i++)
{
Console.WriteLine("{0} = {1}",
a[i],
model.Evaluate(a[i]));
}
}
}
}
/// <summary>
/// Sudoku solving example.
/// </summary>
static void SudokuExample(Context ctx)
{
Console.WriteLine("SudokuExample");
// 9x9 matrix of integer variables
IntExpr[][] X = new IntExpr[9][];
for (uint i = 0; i < 9; i++)
{
X[i] = new IntExpr[9];
for (uint j = 0; j < 9; j++)
X[i][j] = (IntExpr)ctx.MkConst(ctx.MkSymbol("x_" + (i + 1) + "_" + (j + 1)), ctx.IntSort);
}
// each cell contains a value in {1, ..., 9}
Expr[][] cells_c = new Expr[9][];
for (uint i = 0; i < 9; i++)
{
cells_c[i] = new BoolExpr[9];
for (uint j = 0; j < 9; j++)
cells_c[i][j] = ctx.MkAnd(ctx.MkLe(ctx.MkInt(1), X[i][j]),
ctx.MkLe(X[i][j], ctx.MkInt(9)));
}
// each row contains a digit at most once
BoolExpr[] rows_c = new BoolExpr[9];
for (uint i = 0; i < 9; i++)
rows_c[i] = ctx.MkDistinct(X[i]);
// each column contains a digit at most once
BoolExpr[] cols_c = new BoolExpr[9];
for (uint j = 0; j < 9; j++)
{
IntExpr[] column = new IntExpr[9];
for (uint i = 0; i < 9; i++)
column[i] = X[i][j];
cols_c[j] = ctx.MkDistinct(column);
}
// each 3x3 square contains a digit at most once
BoolExpr[][] sq_c = new BoolExpr[3][];
for (uint i0 = 0; i0 < 3; i0++)
{
sq_c[i0] = new BoolExpr[3];
for (uint j0 = 0; j0 < 3; j0++)
{
IntExpr[] square = new IntExpr[9];
for (uint i = 0; i < 3; i++)
for (uint j = 0; j < 3; j++)
square[3 * i + j] = X[3 * i0 + i][3 * j0 + j];
sq_c[i0][j0] = ctx.MkDistinct(square);
}
}
BoolExpr sudoku_c = ctx.MkTrue();
foreach (BoolExpr[] t in cells_c)
sudoku_c = ctx.MkAnd(ctx.MkAnd(t), sudoku_c);
sudoku_c = ctx.MkAnd(ctx.MkAnd(rows_c), sudoku_c);
sudoku_c = ctx.MkAnd(ctx.MkAnd(cols_c), sudoku_c);
foreach (BoolExpr[] t in sq_c)
sudoku_c = ctx.MkAnd(ctx.MkAnd(t), sudoku_c);
// sudoku instance, we use '0' for empty cells
int[,] instance = {{0,0,0,0,9,4,0,3,0},
{0,0,0,5,1,0,0,0,7},
{0,8,9,0,0,0,0,4,0},
{0,0,0,0,0,0,2,0,8},
{0,6,0,2,0,1,0,5,0},
{1,0,2,0,0,0,0,0,0},
{0,7,0,0,0,0,5,2,0},
{9,0,0,0,6,5,0,0,0},
{0,4,0,9,7,0,0,0,0}};
BoolExpr instance_c = ctx.MkTrue();
for (uint i = 0; i < 9; i++)
for (uint j = 0; j < 9; j++)
instance_c = ctx.MkAnd(instance_c,
(BoolExpr)
ctx.MkITE(ctx.MkEq(ctx.MkInt(instance[i, j]), ctx.MkInt(0)),
ctx.MkTrue(),
ctx.MkEq(X[i][j], ctx.MkInt(instance[i, j]))));
Solver s = ctx.MkSolver();
s.Assert(sudoku_c);
s.Assert(instance_c);
if (s.Check() == Status.SATISFIABLE)
{
Model m = s.Model;
Expr[,] R = new Expr[9, 9];
for (uint i = 0; i < 9; i++)
for (uint j = 0; j < 9; j++)
R[i, j] = m.Evaluate(X[i][j]);
Console.WriteLine("Sudoku solution:");
for (uint i = 0; i < 9; i++)
{
for (uint j = 0; j < 9; j++)
Console.Write(" " + R[i, j]);
Console.WriteLine();
}
}
else
{
Console.WriteLine("Failed to solve sudoku");
throw new TestFailedException();
}
}
/// <summary>
/// A basic example of how to use quantifiers.
/// </summary>
static void QuantifierExample1(Context ctx)
{
Console.WriteLine("QuantifierExample");
Sort[] types = new Sort[3];
IntExpr[] xs = new IntExpr[3];
Symbol[] names = new Symbol[3];
IntExpr[] vars = new IntExpr[3];
for (uint j = 0; j < 3; j++)
{
types[j] = ctx.IntSort;
names[j] = ctx.MkSymbol(String.Format("x_{0}", j));
xs[j] = (IntExpr)ctx.MkConst(names[j], types[j]);
vars[j] = (IntExpr)ctx.MkBound(2 - j, types[j]); // <-- vars reversed!
}
Expr body_vars = ctx.MkAnd(ctx.MkEq(ctx.MkAdd(vars[0], ctx.MkInt(1)), ctx.MkInt(2)),
ctx.MkEq(ctx.MkAdd(vars[1], ctx.MkInt(2)),
ctx.MkAdd(vars[2], ctx.MkInt(3))));
Expr body_const = ctx.MkAnd(ctx.MkEq(ctx.MkAdd(xs[0], ctx.MkInt(1)), ctx.MkInt(2)),
ctx.MkEq(ctx.MkAdd(xs[1], ctx.MkInt(2)),
ctx.MkAdd(xs[2], ctx.MkInt(3))));
Expr x = ctx.MkForall(types, names, body_vars, 1, null, null, ctx.MkSymbol("Q1"), ctx.MkSymbol("skid1"));
Console.WriteLine("Quantifier X: " + x.ToString());
Expr y = ctx.MkForall(xs, body_const, 1, null, null, ctx.MkSymbol("Q2"), ctx.MkSymbol("skid2"));
Console.WriteLine("Quantifier Y: " + y.ToString());
}
static void QuantifierExample2(Context ctx)
{
Console.WriteLine("QuantifierExample2");
Expr q1, q2;
FuncDecl f = ctx.MkFuncDecl("f", ctx.IntSort, ctx.IntSort);
FuncDecl g = ctx.MkFuncDecl("g", ctx.IntSort, ctx.IntSort);
// Quantifier with Exprs as the bound variables.
{
Expr x = ctx.MkConst("x", ctx.IntSort);
Expr y = ctx.MkConst("y", ctx.IntSort);
Expr f_x = ctx.MkApp(f, x);
Expr f_y = ctx.MkApp(f, y);
Expr g_y = ctx.MkApp(g, y);
Expr[] no_pats = new Expr[] { f_y };
Expr[] bound = new Expr[2] { x, y };
Expr body = ctx.MkAnd(ctx.MkEq(f_x, f_y), ctx.MkEq(f_y, g_y));
q1 = ctx.MkForall(bound, body, 1, null, no_pats, ctx.MkSymbol("q"), ctx.MkSymbol("sk"));
Console.WriteLine("{0}", q1);
}
// Quantifier with de-Bruijn indices.
{
Expr x = ctx.MkBound(1, ctx.IntSort);
Expr y = ctx.MkBound(0, ctx.IntSort);
Expr f_x = ctx.MkApp(f, x);
Expr f_y = ctx.MkApp(f, y);
Expr g_y = ctx.MkApp(g, y);
Expr[] no_pats = new Expr[] { f_y };
Symbol[] names = new Symbol[] { ctx.MkSymbol("x"), ctx.MkSymbol("y") };
Sort[] sorts = new Sort[] { ctx.IntSort, ctx.IntSort };
Expr body = ctx.MkAnd(ctx.MkEq(f_x, f_y), ctx.MkEq(f_y, g_y));
q2 = ctx.MkForall(sorts, names, body, 1,
null, // pats,
no_pats,
ctx.MkSymbol("q"),
ctx.MkSymbol("sk")
);
Console.WriteLine("{0}", q2);
}
Console.WriteLine("{0}", (q1.Equals(q2)));
}
/// <summary>
/// Prove that <tt>f(x, y) = f(w, v) implies y = v</tt> when
/// <code>f</code> is injective in the second argument. <seealso cref="inj_axiom"/>
/// </summary>
public static void QuantifierExample3(Context ctx)
{
Console.WriteLine("QuantifierExample3");
/* If quantified formulas are asserted in a logical context, then
the model produced by Z3 should be viewed as a potential model. */
/* declare function f */
Sort I = ctx.IntSort;
FuncDecl f = ctx.MkFuncDecl("f", new Sort[] { I, I }, I);
/* f is injective in the second argument. */
BoolExpr inj = InjAxiom(ctx, f, 1);
/* create x, y, v, w, fxy, fwv */
Expr x = ctx.MkIntConst("x");
Expr y = ctx.MkIntConst("y");
Expr v = ctx.MkIntConst("v");
Expr w = ctx.MkIntConst("w");
Expr fxy = ctx.MkApp(f, x, y);
Expr fwv = ctx.MkApp(f, w, v);
/* f(x, y) = f(w, v) */
BoolExpr p1 = ctx.MkEq(fxy, fwv);
/* prove f(x, y) = f(w, v) implies y = v */
BoolExpr p2 = ctx.MkEq(y, v);
Prove(ctx, p2, false, inj, p1);
/* disprove f(x, y) = f(w, v) implies x = w */
BoolExpr p3 = ctx.MkEq(x, w);
Disprove(ctx, p3, false, inj, p1);
}
/// <summary>
/// Prove that <tt>f(x, y) = f(w, v) implies y = v</tt> when
/// <code>f</code> is injective in the second argument. <seealso cref="inj_axiom"/>
/// </summary>
public static void QuantifierExample4(Context ctx)
{
Console.WriteLine("QuantifierExample4");
/* If quantified formulas are asserted in a logical context, then
the model produced by Z3 should be viewed as a potential model. */
/* declare function f */
Sort I = ctx.IntSort;
FuncDecl f = ctx.MkFuncDecl("f", new Sort[] { I, I }, I);
/* f is injective in the second argument. */
BoolExpr inj = InjAxiomAbs(ctx, f, 1);
/* create x, y, v, w, fxy, fwv */
Expr x = ctx.MkIntConst("x");
Expr y = ctx.MkIntConst("y");
Expr v = ctx.MkIntConst("v");
Expr w = ctx.MkIntConst("w");
Expr fxy = ctx.MkApp(f, x, y);
Expr fwv = ctx.MkApp(f, w, v);
/* f(x, y) = f(w, v) */
BoolExpr p1 = ctx.MkEq(fxy, fwv);
/* prove f(x, y) = f(w, v) implies y = v */
BoolExpr p2 = ctx.MkEq(y, v);
Prove(ctx, p2, false, inj, p1);
/* disprove f(x, y) = f(w, v) implies x = w */
BoolExpr p3 = ctx.MkEq(x, w);
Disprove(ctx, p3, false, inj, p1);
}
/// <summary>
/// Some basic tests.
/// </summary>
static void BasicTests(Context ctx)
{
Console.WriteLine("BasicTests");
Symbol fname = ctx.MkSymbol("f");
Symbol x = ctx.MkSymbol("x");
Symbol y = ctx.MkSymbol("y");
Sort bs = ctx.MkBoolSort();
Sort[] domain = { bs, bs };
FuncDecl f = ctx.MkFuncDecl(fname, domain, bs);
Expr fapp = ctx.MkApp(f, ctx.MkConst(x, bs), ctx.MkConst(y, bs));
Expr[] fargs2 = { ctx.MkFreshConst("cp", bs) };
Sort[] domain2 = { bs };
Expr fapp2 = ctx.MkApp(ctx.MkFreshFuncDecl("fp", domain2, bs), fargs2);
BoolExpr trivial_eq = ctx.MkEq(fapp, fapp);
BoolExpr nontrivial_eq = ctx.MkEq(fapp, fapp2);
Goal g = ctx.MkGoal(true);
g.Assert(trivial_eq);
g.Assert(nontrivial_eq);
Console.WriteLine("Goal: " + g);
Solver solver = ctx.MkSolver();
foreach (BoolExpr a in g.Formulas)
solver.Assert(a);
if (solver.Check() != Status.SATISFIABLE)
throw new TestFailedException();
ApplyResult ar = ApplyTactic(ctx, ctx.MkTactic("simplify"), g);
if (ar.NumSubgoals == 1 && (ar.Subgoals[0].IsDecidedSat || ar.Subgoals[0].IsDecidedUnsat))
throw new TestFailedException();
ar = ApplyTactic(ctx, ctx.MkTactic("smt"), g);
if (ar.NumSubgoals != 1 || !ar.Subgoals[0].IsDecidedSat)
throw new TestFailedException();
g.Assert(ctx.MkEq(ctx.MkNumeral(1, ctx.MkBitVecSort(32)),
ctx.MkNumeral(2, ctx.MkBitVecSort(32))));
ar = ApplyTactic(ctx, ctx.MkTactic("smt"), g);
if (ar.NumSubgoals != 1 || !ar.Subgoals[0].IsDecidedUnsat)
throw new TestFailedException();
Goal g2 = ctx.MkGoal(true, true);
ar = ApplyTactic(ctx, ctx.MkTactic("smt"), g2);
if (ar.NumSubgoals != 1 || !ar.Subgoals[0].IsDecidedSat)
throw new TestFailedException();
g2 = ctx.MkGoal(true, true);
g2.Assert(ctx.MkFalse());
ar = ApplyTactic(ctx, ctx.MkTactic("smt"), g2);
if (ar.NumSubgoals != 1 || !ar.Subgoals[0].IsDecidedUnsat)
throw new TestFailedException();
Goal g3 = ctx.MkGoal(true, true);
Expr xc = ctx.MkConst(ctx.MkSymbol("x"), ctx.IntSort);
Expr yc = ctx.MkConst(ctx.MkSymbol("y"), ctx.IntSort);
g3.Assert(ctx.MkEq(xc, ctx.MkNumeral(1, ctx.IntSort)));
g3.Assert(ctx.MkEq(yc, ctx.MkNumeral(2, ctx.IntSort)));
BoolExpr constr = ctx.MkEq(xc, yc);
g3.Assert(constr);
ar = ApplyTactic(ctx, ctx.MkTactic("smt"), g3);
if (ar.NumSubgoals != 1 || !ar.Subgoals[0].IsDecidedUnsat)
throw new TestFailedException();
ModelConverterTest(ctx);
// Real num/den test.
RatNum rn = ctx.MkReal(42, 43);
Expr inum = rn.Numerator;
Expr iden = rn.Denominator;
Console.WriteLine("Numerator: " + inum + " Denominator: " + iden);
if (inum.ToString() != "42" || iden.ToString() != "43")
throw new TestFailedException();
if (rn.ToDecimalString(3) != "0.976?")
throw new TestFailedException();
BigIntCheck(ctx, ctx.MkReal("-1231231232/234234333"));
BigIntCheck(ctx, ctx.MkReal("-123123234234234234231232/234234333"));
BigIntCheck(ctx, ctx.MkReal("-234234333"));
BigIntCheck(ctx, ctx.MkReal("234234333/2"));
#if !FRAMEWORK_LT_4
string bn = "1234567890987654321";
if (ctx.MkInt(bn).BigInteger.ToString() != bn)
throw new TestFailedException();
if (ctx.MkBV(bn, 128).BigInteger.ToString() != bn)
throw new TestFailedException();
if (ctx.MkBV(bn, 32).BigInteger.ToString() == bn)
throw new TestFailedException();
#endif
// Error handling test.
try
{
IntExpr i = ctx.MkInt("1/2");
throw new TestFailedException(); // unreachable
}
catch (Z3Exception)
{
}
}
/// <summary>
/// Some basic expression casting tests.
/// </summary>
static void CastingTest(Context ctx)
{
Console.WriteLine("CastingTest");
Sort[] domain = { ctx.BoolSort, ctx.BoolSort };
FuncDecl f = ctx.MkFuncDecl("f", domain, ctx.BoolSort);
AST upcast = ctx.MkFuncDecl(ctx.MkSymbol("q"), domain, ctx.BoolSort);
try
{
FuncDecl downcast = (FuncDecl)f; // OK
}
catch (InvalidCastException)
{
throw new TestFailedException();
}
try
{
Expr uc = (Expr)upcast;
throw new TestFailedException(); // should not be reachable!
}
catch (InvalidCastException)
{
}
Symbol s = ctx.MkSymbol(42);
IntSymbol si = s as IntSymbol;
if (si == null) throw new TestFailedException();
try
{
IntSymbol si2 = (IntSymbol)s;
}
catch (InvalidCastException)
{
throw new TestFailedException();
}
s = ctx.MkSymbol("abc");
StringSymbol ss = s as StringSymbol;
if (ss == null) throw new TestFailedException();
try
{
StringSymbol ss2 = (StringSymbol)s;
}
catch (InvalidCastException)
{
throw new TestFailedException();
}
try
{
IntSymbol si2 = (IntSymbol)s;
throw new TestFailedException(); // unreachable
}
catch
{
}
Sort srt = ctx.MkBitVecSort(32);
BitVecSort bvs = null;
try
{
bvs = (BitVecSort)srt;
}
catch (InvalidCastException)
{
throw new TestFailedException();
}
if (bvs.Size != 32)
throw new TestFailedException();
Expr q = ctx.MkAdd(ctx.MkInt(1), ctx.MkInt(2));
Expr q2 = q.Args[1];
Sort qs = q2.Sort;
if (qs as IntSort == null)
throw new TestFailedException();
try
{
IntSort isrt = (IntSort)qs;
}
catch (InvalidCastException)
{
throw new TestFailedException();
}
AST a = ctx.MkInt(42);
Expr ae = a as Expr;
if (ae == null) throw new TestFailedException();
ArithExpr aae = a as ArithExpr;
if (aae == null) throw new TestFailedException();
IntExpr aie = a as IntExpr;
if (aie == null) throw new TestFailedException();
IntNum ain = a as IntNum;
if (ain == null) throw new TestFailedException();
Expr[][] earr = new Expr[2][];
earr[0] = new Expr[2];
earr[1] = new Expr[2];
earr[0][0] = ctx.MkTrue();
earr[0][1] = ctx.MkTrue();
earr[1][0] = ctx.MkFalse();
earr[1][1] = ctx.MkFalse();
foreach (Expr[] ea in earr)
foreach (Expr e in ea)
{
try
{
Expr ns = ctx.MkNot((BoolExpr)e);
BoolExpr ens = (BoolExpr)ns;
}
catch (InvalidCastException)
{
throw new TestFailedException();
}
}
}
/// <summary>
/// Shows how to read an SMT2 file.
/// </summary>
static void SMT2FileTest(string filename)
{
System.DateTime before = System.DateTime.Now;
Console.WriteLine("SMT2 File test ");
System.GC.Collect();
using (Context ctx = new Context(new Dictionary<string, string>() { { "MODEL", "true" } }))
{
BoolExpr[] fmls = ctx.ParseSMTLIB2File(filename);
BoolExpr a = ctx.MkAnd(fmls);
Console.WriteLine("SMT2 file read time: " + (System.DateTime.Now - before).TotalSeconds + " sec");
// Iterate over the formula.
Queue q = new Queue();
q.Enqueue(a);
uint cnt = 0;
while (q.Count > 0)
{
AST cur = (AST)q.Dequeue();
cnt++;
// This here ...
if (cur is Expr)
if (!(cur.IsVar))
foreach (Expr c in ((Expr)cur).Args)
q.Enqueue(c);
// Takes the same time as this:
//switch ((cur).ASTKind)
//{
// case Z3_ast_kind.Z3_APP_AST:
// foreach (Expr e in ((Expr)cur).Args)
// q.Enqueue(e);
// break;
// case Z3_ast_kind.Z3_QUANTIFIER_AST:
// q.Enqueue(((Quantifier)cur).Args[0]);
// break;
// case Z3_ast_kind.Z3_VAR_AST: break;
// case Z3_ast_kind.Z3_NUMERAL_AST: break;
// case Z3_ast_kind.Z3_FUNC_DECL_AST: break;
// case Z3_ast_kind.Z3_SORT_AST: break;
//}
}
Console.WriteLine(cnt + " ASTs");
}
Console.WriteLine("SMT2 file test took " + (System.DateTime.Now - before).TotalSeconds + " sec");
}
/// <summary>
/// Shows how to use Solver(logic)
/// </summary>
/// <param name="ctx"></param>
static void LogicExample(Context ctx)
{
Console.WriteLine("LogicTest");
Microsoft.Z3.Global.ToggleWarningMessages(true);
BitVecSort bvs = ctx.MkBitVecSort(32);
Expr x = ctx.MkConst("x", bvs);
Expr y = ctx.MkConst("y", bvs);
BoolExpr eq = ctx.MkEq(x, y);
// Use a solver for QF_BV
Solver s = ctx.MkSolver("QF_BV");
s.Assert(eq);
Status res = s.Check();
Console.WriteLine("solver result: " + res);
// Or perhaps a tactic for QF_BV
Goal g = ctx.MkGoal(true);
g.Assert(eq);
Tactic t = ctx.MkTactic("qfbv");
ApplyResult ar = t.Apply(g);
Console.WriteLine("tactic result: " + ar);
if (ar.NumSubgoals != 1 || !ar.Subgoals[0].IsDecidedSat)
throw new TestFailedException();
}
/// <summary>
/// Demonstrates how to use the ParOr tactic.
/// </summary>
static void ParOrExample(Context ctx)
{
Console.WriteLine("ParOrExample");
BitVecSort bvs = ctx.MkBitVecSort(32);
Expr x = ctx.MkConst("x", bvs);
Expr y = ctx.MkConst("y", bvs);
BoolExpr q = ctx.MkEq(x, y);
Goal g = ctx.MkGoal(true);
g.Assert(q);
Tactic t1 = ctx.MkTactic("qfbv");
Tactic t2 = ctx.MkTactic("qfbv");
Tactic p = ctx.ParOr(t1, t2);
ApplyResult ar = p.Apply(g);
if (ar.NumSubgoals != 1 || !ar.Subgoals[0].IsDecidedSat)
throw new TestFailedException();
}
static void BigIntCheck(Context ctx, RatNum r)
{
#if !FRAMEWORK_LT_4
Console.WriteLine("Num: " + r.BigIntNumerator);
Console.WriteLine("Den: " + r.BigIntDenominator);
#endif
}
/// <summary>
/// Find a model for <code>x xor y</code>.
/// </summary>
public static void FindModelExample1(Context ctx)
{
Console.WriteLine("FindModelExample1");
BoolExpr x = ctx.MkBoolConst("x");
BoolExpr y = ctx.MkBoolConst("y");
BoolExpr x_xor_y = ctx.MkXor(x, y);
Model model = Check(ctx, x_xor_y, Status.SATISFIABLE);
Console.WriteLine("x = {0}, y = {1}",
model.Evaluate(x),
model.Evaluate(y));
}
/// <summary>
/// Find a model for <tt>x < y + 1, x > 2</tt>.
/// Then, assert <tt>not(x = y)</tt>, and find another model.
/// </summary>
public static void FindModelExample2(Context ctx)
{
Console.WriteLine("FindModelExample2");
IntExpr x = ctx.MkIntConst("x");
IntExpr y = ctx.MkIntConst("y");
IntExpr one = ctx.MkInt(1);
IntExpr two = ctx.MkInt(2);
ArithExpr y_plus_one = ctx.MkAdd(y, one);
BoolExpr c1 = ctx.MkLt(x, y_plus_one);
BoolExpr c2 = ctx.MkGt(x, two);
BoolExpr q = ctx.MkAnd(c1, c2);
Console.WriteLine("model for: x < y + 1, x > 2");
Model model = Check(ctx, q, Status.SATISFIABLE);
Console.WriteLine("x = {0}, y = {1}",
(model.Evaluate(x)),
(model.Evaluate(y)));
/* assert not(x = y) */
BoolExpr x_eq_y = ctx.MkEq(x, y);
BoolExpr c3 = ctx.MkNot(x_eq_y);
q = ctx.MkAnd(q, c3);
Console.WriteLine("model for: x < y + 1, x > 2, not(x = y)");
model = Check(ctx, q, Status.SATISFIABLE);
Console.WriteLine("x = {0}, y = {1}",
(model.Evaluate(x)),
(model.Evaluate(y)));
}
/// <summary>
/// Prove <tt>x = y implies g(x) = g(y)</tt>, and
/// disprove <tt>x = y implies g(g(x)) = g(y)</tt>.
/// </summary>
/// <remarks>This function demonstrates how to create uninterpreted
/// types and functions.</remarks>
public static void ProveExample1(Context ctx)
{
Console.WriteLine("ProveExample1");
/* create uninterpreted type. */
Sort U = ctx.MkUninterpretedSort(ctx.MkSymbol("U"));
/* declare function g */
FuncDecl g = ctx.MkFuncDecl("g", U, U);
/* create x and y */
Expr x = ctx.MkConst("x", U);
Expr y = ctx.MkConst("y", U);
/* create g(x), g(y) */
Expr gx = g[x];
Expr gy = g[y];
/* assert x = y */
BoolExpr eq = ctx.MkEq(x, y);
/* prove g(x) = g(y) */
BoolExpr f = ctx.MkEq(gx, gy);
Console.WriteLine("prove: x = y implies g(x) = g(y)");
Prove(ctx, ctx.MkImplies(eq, f));
/* create g(g(x)) */
Expr ggx = g[gx];
/* disprove g(g(x)) = g(y) */
f = ctx.MkEq(ggx, gy);
Console.WriteLine("disprove: x = y implies g(g(x)) = g(y)");
Disprove(ctx, ctx.MkImplies(eq, f));
/* Print the model using the custom model printer */
Model m = Check(ctx, ctx.MkNot(f), Status.SATISFIABLE);
Console.WriteLine(m);
}
/// <summary>
/// Prove <tt>not(g(g(x) - g(y)) = g(z)), x + z <= y <= x implies z < 0 </tt>.
/// Then, show that <tt>z < -1</tt> is not implied.
/// </summary>
/// <remarks>This example demonstrates how to combine uninterpreted functions
/// and arithmetic.</remarks>
public static void ProveExample2(Context ctx)
{
Console.WriteLine("ProveExample2");
/* declare function g */
Sort I = ctx.IntSort;
FuncDecl g = ctx.MkFuncDecl("g", I, I);
/* create x, y, and z */
IntExpr x = ctx.MkIntConst("x");
IntExpr y = ctx.MkIntConst("y");
IntExpr z = ctx.MkIntConst("z");
/* create gx, gy, gz */
Expr gx = ctx.MkApp(g, x);
Expr gy = ctx.MkApp(g, y);
Expr gz = ctx.MkApp(g, z);
/* create zero */
IntExpr zero = ctx.MkInt(0);
/* assert not(g(g(x) - g(y)) = g(z)) */
ArithExpr gx_gy = ctx.MkSub((IntExpr)gx, (IntExpr)gy);
Expr ggx_gy = ctx.MkApp(g, gx_gy);
BoolExpr eq = ctx.MkEq(ggx_gy, gz);
BoolExpr c1 = ctx.MkNot(eq);
/* assert x + z <= y */
ArithExpr x_plus_z = ctx.MkAdd(x, z);
BoolExpr c2 = ctx.MkLe(x_plus_z, y);
/* assert y <= x */
BoolExpr c3 = ctx.MkLe(y, x);
/* prove z < 0 */
BoolExpr f = ctx.MkLt(z, zero);
Console.WriteLine("prove: not(g(g(x) - g(y)) = g(z)), x + z <= y <= x implies z < 0");
Prove(ctx, f, false, c1, c2, c3);
/* disprove z < -1 */
IntExpr minus_one = ctx.MkInt(-1);
f = ctx.MkLt(z, minus_one);
Console.WriteLine("disprove: not(g(g(x) - g(y)) = g(z)), x + z <= y <= x implies z < -1");
Disprove(ctx, f, false, c1, c2, c3);
}
/// <summary>
/// Show how push & pop can be used to create "backtracking" points.
/// </summary>
/// <remarks>This example also demonstrates how big numbers can be
/// created in ctx.</remarks>
public static void PushPopExample1(Context ctx)
{
Console.WriteLine("PushPopExample1");
/* create a big number */
IntSort int_type = ctx.IntSort;
IntExpr big_number = ctx.MkInt("1000000000000000000000000000000000000000000000000000000");
/* create number 3 */
IntExpr three = (IntExpr)ctx.MkNumeral("3", int_type);
/* create x */
IntExpr x = ctx.MkIntConst("x");
Solver solver = ctx.MkSolver();
/* assert x >= "big number" */
BoolExpr c1 = ctx.MkGe(x, big_number);
Console.WriteLine("assert: x >= 'big number'");
solver.Assert(c1);
/* create a backtracking point */
Console.WriteLine("push");
solver.Push();
/* assert x <= 3 */
BoolExpr c2 = ctx.MkLe(x, three);
Console.WriteLine("assert: x <= 3");
solver.Assert(c2);
/* context is inconsistent at this point */
if (solver.Check() != Status.UNSATISFIABLE)
throw new TestFailedException();
/* backtrack: the constraint x <= 3 will be removed, since it was
asserted after the last ctx.Push. */
Console.WriteLine("pop");
solver.Pop(1);
/* the context is consistent again. */
if (solver.Check() != Status.SATISFIABLE)
throw new TestFailedException();
/* new constraints can be asserted... */
/* create y */
IntExpr y = ctx.MkIntConst("y");
/* assert y > x */
BoolExpr c3 = ctx.MkGt(y, x);
Console.WriteLine("assert: y > x");
solver.Assert(c3);
/* the context is still consistent. */
if (solver.Check() != Status.SATISFIABLE)
throw new TestFailedException();
}
/// <summary>
/// Tuples.
/// </summary>
/// <remarks>Check that the projection of a tuple
/// returns the corresponding element.</remarks>
public static void TupleExample(Context ctx)
{
Console.WriteLine("TupleExample");
Sort int_type = ctx.IntSort;
TupleSort tuple = ctx.MkTupleSort(
ctx.MkSymbol("mk_tuple"), // name of tuple constructor
new Symbol[] { ctx.MkSymbol("first"), ctx.MkSymbol("second") }, // names of projection operators
new Sort[] { int_type, int_type } // types of projection operators
);
FuncDecl first = tuple.FieldDecls[0]; // declarations are for projections
// FuncDecl second = tuple.FieldDecls[1];
Expr x = ctx.MkConst("x", int_type);
Expr y = ctx.MkConst("y", int_type);
Expr n1 = tuple.MkDecl[x, y];
Expr n2 = first[n1];
BoolExpr n3 = ctx.MkEq(x, n2);
Console.WriteLine("Tuple example: {0}", n3);
Prove(ctx, n3);
}
/// <summary>
/// Simple bit-vector example.
/// </summary>
/// <remarks>
/// This example disproves that x - 10 <= 0 IFF x <= 10 for (32-bit) machine integers
/// </remarks>
public static void BitvectorExample1(Context ctx)
{
Console.WriteLine("BitvectorExample1");
Sort bv_type = ctx.MkBitVecSort(32);
BitVecExpr x = (BitVecExpr)ctx.MkConst("x", bv_type);
BitVecNum zero = (BitVecNum)ctx.MkNumeral("0", bv_type);
BitVecNum ten = ctx.MkBV(10, 32);
BitVecExpr x_minus_ten = ctx.MkBVSub(x, ten);
/* bvsle is signed less than or equal to */
BoolExpr c1 = ctx.MkBVSLE(x, ten);
BoolExpr c2 = ctx.MkBVSLE(x_minus_ten, zero);
BoolExpr thm = ctx.MkIff(c1, c2);
Console.WriteLine("disprove: x - 10 <= 0 IFF x <= 10 for (32-bit) machine integers");
Disprove(ctx, thm);
}
/// <summary>
/// Find x and y such that: x ^ y - 103 == x * y
/// </summary>
public static void BitvectorExample2(Context ctx)
{
Console.WriteLine("BitvectorExample2");
/* construct x ^ y - 103 == x * y */
Sort bv_type = ctx.MkBitVecSort(32);
BitVecExpr x = ctx.MkBVConst("x", 32);
BitVecExpr y = ctx.MkBVConst("y", 32);
BitVecExpr x_xor_y = ctx.MkBVXOR(x, y);
BitVecExpr c103 = (BitVecNum)ctx.MkNumeral("103", bv_type);
BitVecExpr lhs = ctx.MkBVSub(x_xor_y, c103);
BitVecExpr rhs = ctx.MkBVMul(x, y);
BoolExpr ctr = ctx.MkEq(lhs, rhs);
Console.WriteLine("find values of x and y, such that x ^ y - 103 == x * y");
/* find a model (i.e., values for x an y that satisfy the constraint */
Model m = Check(ctx, ctr, Status.SATISFIABLE);
Console.WriteLine(m);
}
/// <summary>
/// Demonstrates how to use the SMTLIB parser.
/// </summary>
public static void ParserExample1(Context ctx)
{
Console.WriteLine("ParserExample1");
var fmls = ctx.ParseSMTLIB2String("(declare-const x Int) (declare-const y Int) (assert (> x y)) (assert (> x 0))");
var fml = ctx.MkAnd(fmls);
Console.WriteLine("formula {0}", fml);
Model m = Check(ctx, fml, Status.SATISFIABLE);
}
/// <summary>
/// Demonstrates how to initialize the parser symbol table.
/// </summary>
public static void ParserExample2(Context ctx)
{
Console.WriteLine("ParserExample2");
Symbol[] declNames = { ctx.MkSymbol("a"), ctx.MkSymbol("b") };
FuncDecl a = ctx.MkConstDecl(declNames[0], ctx.MkIntSort());
FuncDecl b = ctx.MkConstDecl(declNames[1], ctx.MkIntSort());
FuncDecl[] decls = new FuncDecl[] { a, b };
BoolExpr f = ctx.ParseSMTLIB2String("(assert (> a b))", null, null, declNames, decls)[0];
Console.WriteLine("formula: {0}", f);
Check(ctx, f, Status.SATISFIABLE);
}
/// <summary>
/// Demonstrates how to initialize the parser symbol table.
/// </summary>
public static void ParserExample3(Context ctx)
{
Console.WriteLine("ParserExample3");
/* declare function g */
Sort I = ctx.MkIntSort();
FuncDecl g = ctx.MkFuncDecl("g", new Sort[] { I, I }, I);
BoolExpr ca = CommAxiom(ctx, g);
BoolExpr thm = ctx.ParseSMTLIB2String("(assert (forall ((x Int) (y Int)) (=> (= x y) (= (gg x 0) (gg 0 y)))))",
null, null,
new Symbol[] { ctx.MkSymbol("gg") },
new FuncDecl[] { g })[0];
Console.WriteLine("formula: {0}", thm);
Prove(ctx, thm, false, ca);
}
/// <summary>
/// Demonstrates how to handle parser errors using Z3 error handling support.
/// </summary>
/// <remarks></remarks>
public static void ParserExample5(Context ctx)
{
Console.WriteLine("ParserExample5");
try
{
ctx.ParseSMTLIB2String(
/* the following string has a parsing error: missing parenthesis */
"(declare-const x Int (declare-const y Int)) (assert (> x y))");
}
catch (Z3Exception e)
{
Console.WriteLine("Z3 error: {0}", e);
}
}
/// <summary>
/// Create an ite-Expr (if-then-else Exprs).
/// </summary>
public static void ITEExample(Context ctx)
{
Console.WriteLine("ITEExample");
BoolExpr f = ctx.MkFalse();
Expr one = ctx.MkInt(1);
Expr zero = ctx.MkInt(0);
Expr ite = ctx.MkITE(f, one, zero);
Console.WriteLine("Expr: {0}", ite);
}
/// <summary>
/// Create an enumeration data type.
/// </summary>
public static void EnumExample(Context ctx)
{
Console.WriteLine("EnumExample");
Symbol name = ctx.MkSymbol("fruit");
EnumSort fruit = ctx.MkEnumSort(ctx.MkSymbol("fruit"), new Symbol[] { ctx.MkSymbol("apple"), ctx.MkSymbol("banana"), ctx.MkSymbol("orange") });
Console.WriteLine("{0}", (fruit.Consts[0]));
Console.WriteLine("{0}", (fruit.Consts[1]));
Console.WriteLine("{0}", (fruit.Consts[2]));
Console.WriteLine("{0}", (fruit.TesterDecls[0]));
Console.WriteLine("{0}", (fruit.TesterDecls[1]));
Console.WriteLine("{0}", (fruit.TesterDecls[2]));
Expr apple = fruit.Consts[0];
Expr banana = fruit.Consts[1];
Expr orange = fruit.Consts[2];
/* Apples are different from oranges */
Prove(ctx, ctx.MkNot(ctx.MkEq(apple, orange)));
/* Apples pass the apple test */
Prove(ctx, (BoolExpr)ctx.MkApp(fruit.TesterDecls[0], apple));
/* Oranges fail the apple test */
Disprove(ctx, (BoolExpr)ctx.MkApp(fruit.TesterDecls[0], orange));
Prove(ctx, (BoolExpr)ctx.MkNot((BoolExpr)ctx.MkApp(fruit.TesterDecls[0], orange)));
Expr fruity = ctx.MkConst("fruity", fruit);
/* If something is fruity, then it is an apple, banana, or orange */
Prove(ctx, ctx.MkOr(new BoolExpr[] { ctx.MkEq(fruity, apple), ctx.MkEq(fruity, banana), ctx.MkEq(fruity, orange) }));
}
/// <summary>
/// Create a list datatype.
/// </summary>
public static void ListExample(Context ctx)
{
Console.WriteLine("ListExample");
Sort int_ty;
ListSort int_list;
Expr nil, l1, l2, x, y, u, v;
BoolExpr fml, fml1;
int_ty = ctx.MkIntSort();
int_list = ctx.MkListSort(ctx.MkSymbol("int_list"), int_ty);
nil = ctx.MkConst(int_list.NilDecl);
l1 = ctx.MkApp(int_list.ConsDecl, ctx.MkInt(1), nil);
l2 = ctx.MkApp(int_list.ConsDecl, ctx.MkInt(2), nil);
/* nil != cons(1, nil) */
Prove(ctx, ctx.MkNot(ctx.MkEq(nil, l1)));
/* cons(2,nil) != cons(1, nil) */
Prove(ctx, ctx.MkNot(ctx.MkEq(l1, l2)));
/* cons(x,nil) = cons(y, nil) => x = y */
x = ctx.MkConst("x", int_ty);
y = ctx.MkConst("y", int_ty);
l1 = ctx.MkApp(int_list.ConsDecl, x, nil);
l2 = ctx.MkApp(int_list.ConsDecl, y, nil);
Prove(ctx, ctx.MkImplies(ctx.MkEq(l1, l2), ctx.MkEq(x, y)));
/* cons(x,u) = cons(x, v) => u = v */
u = ctx.MkConst("u", int_list);
v = ctx.MkConst("v", int_list);
l1 = ctx.MkApp(int_list.ConsDecl, x, u);
l2 = ctx.MkApp(int_list.ConsDecl, y, v);
Prove(ctx, ctx.MkImplies(ctx.MkEq(l1, l2), ctx.MkEq(u, v)));
Prove(ctx, ctx.MkImplies(ctx.MkEq(l1, l2), ctx.MkEq(x, y)));
/* is_nil(u) or is_cons(u) */
Prove(ctx, ctx.MkOr((BoolExpr)ctx.MkApp(int_list.IsNilDecl, u),
(BoolExpr)ctx.MkApp(int_list.IsConsDecl, u)));
/* occurs check u != cons(x,u) */
Prove(ctx, ctx.MkNot(ctx.MkEq(u, l1)));
/* destructors: is_cons(u) => u = cons(head(u),tail(u)) */
fml1 = ctx.MkEq(u, ctx.MkApp(int_list.ConsDecl, ctx.MkApp(int_list.HeadDecl, u),
ctx.MkApp(int_list.TailDecl, u)));
fml = ctx.MkImplies((BoolExpr)ctx.MkApp(int_list.IsConsDecl, u), fml1);
Console.WriteLine("Formula {0}", fml);
Prove(ctx, fml);
Disprove(ctx, fml1);
}
/// <summary>
/// Create a binary tree datatype.
/// </summary>
public static void TreeExample(Context ctx)
{
Console.WriteLine("TreeExample");
Sort cell;
FuncDecl nil_decl, is_nil_decl, cons_decl, is_cons_decl, car_decl, cdr_decl;
Expr nil, l1, l2, x, y, u, v;
BoolExpr fml, fml1;
string[] head_tail = new string[] { "car", "cdr" };
Sort[] sorts = new Sort[] { null, null };
uint[] sort_refs = new uint[] { 0, 0 };
Constructor nil_con, cons_con;
nil_con = ctx.MkConstructor("nil", "is_nil", null, null, null);
cons_con = ctx.MkConstructor("cons", "is_cons", head_tail, sorts, sort_refs);
Constructor[] constructors = new Constructor[] { nil_con, cons_con };
cell = ctx.MkDatatypeSort("cell", constructors);
nil_decl = nil_con.ConstructorDecl;
is_nil_decl = nil_con.TesterDecl;
cons_decl = cons_con.ConstructorDecl;
is_cons_decl = cons_con.TesterDecl;
FuncDecl[] cons_accessors = cons_con.AccessorDecls;
car_decl = cons_accessors[0];
cdr_decl = cons_accessors[1];
nil = ctx.MkConst(nil_decl);
l1 = ctx.MkApp(cons_decl, nil, nil);
l2 = ctx.MkApp(cons_decl, l1, nil);
/* nil != cons(nil, nil) */
Prove(ctx, ctx.MkNot(ctx.MkEq(nil, l1)));
/* cons(x,u) = cons(x, v) => u = v */
u = ctx.MkConst("u", cell);
v = ctx.MkConst("v", cell);
x = ctx.MkConst("x", cell);
y = ctx.MkConst("y", cell);
l1 = ctx.MkApp(cons_decl, x, u);
l2 = ctx.MkApp(cons_decl, y, v);
Prove(ctx, ctx.MkImplies(ctx.MkEq(l1, l2), ctx.MkEq(u, v)));
Prove(ctx, ctx.MkImplies(ctx.MkEq(l1, l2), ctx.MkEq(x, y)));
/* is_nil(u) or is_cons(u) */
Prove(ctx, ctx.MkOr((BoolExpr)ctx.MkApp(is_nil_decl, u), (BoolExpr)ctx.MkApp(is_cons_decl, u)));
/* occurs check u != cons(x,u) */
Prove(ctx, ctx.MkNot(ctx.MkEq(u, l1)));
/* destructors: is_cons(u) => u = cons(car(u),cdr(u)) */
fml1 = ctx.MkEq(u, ctx.MkApp(cons_decl, ctx.MkApp(car_decl, u), ctx.MkApp(cdr_decl, u)));
fml = ctx.MkImplies((BoolExpr)ctx.MkApp(is_cons_decl, u), fml1);
Console.WriteLine("Formula {0}", fml);
Prove(ctx, fml);
Disprove(ctx, fml1);
}
/// <summary>
/// Create a forest of trees.
/// </summary>
/// <remarks>
/// forest ::= nil | cons(tree, forest)
/// tree ::= nil | cons(forest, forest)
/// </remarks>
public static void ForestExample(Context ctx)
{
Console.WriteLine("ForestExample");
Sort tree, forest;
FuncDecl nil1_decl, is_nil1_decl, cons1_decl, is_cons1_decl, car1_decl, cdr1_decl;
FuncDecl nil2_decl, is_nil2_decl, cons2_decl, is_cons2_decl, car2_decl, cdr2_decl;
Expr nil1, nil2, t1, t2, t3, t4, f1, f2, f3, l1, l2, x, y, u, v;
//
// Declare the names of the accessors for cons.
// Then declare the sorts of the accessors.
// For this example, all sorts refer to the new types 'forest' and 'tree'
// being declared, so we pass in null for both sorts1 and sorts2.
// On the other hand, the sort_refs arrays contain the indices of the
// two new sorts being declared. The first element in sort1_refs
// points to 'tree', which has index 1, the second element in sort1_refs array
// points to 'forest', which has index 0.
//
Symbol[] head_tail1 = new Symbol[] { ctx.MkSymbol("head"), ctx.MkSymbol("tail") };
Sort[] sorts1 = new Sort[] { null, null };
uint[] sort1_refs = new uint[] { 1, 0 }; // the first item points to a tree, the second to a forest
Symbol[] head_tail2 = new Symbol[] { ctx.MkSymbol("car"), ctx.MkSymbol("cdr") };
Sort[] sorts2 = new Sort[] { null, null };
uint[] sort2_refs = new uint[] { 0, 0 }; // both items point to the forest datatype.
Constructor nil1_con, cons1_con, nil2_con, cons2_con;
Constructor[] constructors1 = new Constructor[2], constructors2 = new Constructor[2];
Symbol[] sort_names = { ctx.MkSymbol("forest"), ctx.MkSymbol("tree") };
/* build a forest */
nil1_con = ctx.MkConstructor(ctx.MkSymbol("nil"), ctx.MkSymbol("is_nil"), null, null, null);
cons1_con = ctx.MkConstructor(ctx.MkSymbol("cons1"), ctx.MkSymbol("is_cons1"), head_tail1, sorts1, sort1_refs);
constructors1[0] = nil1_con;
constructors1[1] = cons1_con;
/* build a tree */
nil2_con = ctx.MkConstructor(ctx.MkSymbol("nil2"), ctx.MkSymbol("is_nil2"), null, null, null);
cons2_con = ctx.MkConstructor(ctx.MkSymbol("cons2"), ctx.MkSymbol("is_cons2"), head_tail2, sorts2, sort2_refs);
constructors2[0] = nil2_con;
constructors2[1] = cons2_con;
Constructor[][] clists = new Constructor[][] { constructors1, constructors2 };
Sort[] sorts = ctx.MkDatatypeSorts(sort_names, clists);
forest = sorts[0];
tree = sorts[1];
//
// Now that the datatype has been created.
// Query the constructors for the constructor
// functions, testers, and field accessors.
//
nil1_decl = nil1_con.ConstructorDecl;
is_nil1_decl = nil1_con.TesterDecl;
cons1_decl = cons1_con.ConstructorDecl;
is_cons1_decl = cons1_con.TesterDecl;
FuncDecl[] cons1_accessors = cons1_con.AccessorDecls;
car1_decl = cons1_accessors[0];
cdr1_decl = cons1_accessors[1];
nil2_decl = nil2_con.ConstructorDecl;
is_nil2_decl = nil2_con.TesterDecl;
cons2_decl = cons2_con.ConstructorDecl;
is_cons2_decl = cons2_con.TesterDecl;
FuncDecl[] cons2_accessors = cons2_con.AccessorDecls;
car2_decl = cons2_accessors[0];
cdr2_decl = cons2_accessors[1];
nil1 = ctx.MkConst(nil1_decl);
nil2 = ctx.MkConst(nil2_decl);
f1 = ctx.MkApp(cons1_decl, nil2, nil1);
t1 = ctx.MkApp(cons2_decl, nil1, nil1);
t2 = ctx.MkApp(cons2_decl, f1, nil1);
t3 = ctx.MkApp(cons2_decl, f1, f1);
t4 = ctx.MkApp(cons2_decl, nil1, f1);
f2 = ctx.MkApp(cons1_decl, t1, nil1);
f3 = ctx.MkApp(cons1_decl, t1, f1);
/* nil != cons(nil,nil) */
Prove(ctx, ctx.MkNot(ctx.MkEq(nil1, f1)));
Prove(ctx, ctx.MkNot(ctx.MkEq(nil2, t1)));
/* cons(x,u) = cons(x, v) => u = v */
u = ctx.MkConst("u", forest);
v = ctx.MkConst("v", forest);
x = ctx.MkConst("x", tree);
y = ctx.MkConst("y", tree);
l1 = ctx.MkApp(cons1_decl, x, u);
l2 = ctx.MkApp(cons1_decl, y, v);
Prove(ctx, ctx.MkImplies(ctx.MkEq(l1, l2), ctx.MkEq(u, v)));
Prove(ctx, ctx.MkImplies(ctx.MkEq(l1, l2), ctx.MkEq(x, y)));
/* is_nil(u) or is_cons(u) */
Prove(ctx, ctx.MkOr((BoolExpr)ctx.MkApp(is_nil1_decl, u),
(BoolExpr)ctx.MkApp(is_cons1_decl, u)));
/* occurs check u != cons(x,u) */
Prove(ctx, ctx.MkNot(ctx.MkEq(u, l1)));
}
/// <summary>
/// Demonstrate how to use #Eval.
/// </summary>
public static void EvalExample1(Context ctx)
{
Console.WriteLine("EvalExample1");
IntExpr x = ctx.MkIntConst("x");
IntExpr y = ctx.MkIntConst("y");
IntExpr two = ctx.MkInt(2);
Solver solver = ctx.MkSolver();
/* assert x < y */
solver.Assert(ctx.MkLt(x, y));
/* assert x > 2 */
solver.Assert(ctx.MkGt(x, two));
/* find model for the constraints above */
Model model = null;
if (Status.SATISFIABLE == solver.Check())
{
model = solver.Model;
Console.WriteLine("{0}", model);
Console.WriteLine("\nevaluating x+y");
Expr v = model.Evaluate(ctx.MkAdd(x, y));
if (v != null)
{
Console.WriteLine("result = {0}", (v));
}
else
{
Console.WriteLine("Failed to evaluate: x+y");
}
}
else
{
Console.WriteLine("BUG, the constraints are satisfiable.");
}
}
/// <summary>
/// Demonstrate how to use #Eval on tuples.
/// </summary>
public static void EvalExample2(Context ctx)
{
Console.WriteLine("EvalExample2");
Sort int_type = ctx.IntSort;
TupleSort tuple = ctx.MkTupleSort(
ctx.MkSymbol("mk_tuple"), // name of tuple constructor
new Symbol[] { ctx.MkSymbol("first"), ctx.MkSymbol("second") }, // names of projection operators
new Sort[] { int_type, int_type } // types of projection operators
);
FuncDecl first = tuple.FieldDecls[0]; // declarations are for projections
FuncDecl second = tuple.FieldDecls[1];
Expr tup1 = ctx.MkConst("t1", tuple);
Expr tup2 = ctx.MkConst("t2", tuple);
Solver solver = ctx.MkSolver();
/* assert tup1 != tup2 */
solver.Assert(ctx.MkNot(ctx.MkEq(tup1, tup2)));
/* assert first tup1 = first tup2 */
solver.Assert(ctx.MkEq(ctx.MkApp(first, tup1), ctx.MkApp(first, tup2)));
/* find model for the constraints above */
Model model = null;
if (Status.SATISFIABLE == solver.Check())
{
model = solver.Model;
Console.WriteLine("{0}", model);
Console.WriteLine("evaluating tup1 {0}", (model.Evaluate(tup1)));
Console.WriteLine("evaluating tup2 {0}", (model.Evaluate(tup2)));
Console.WriteLine("evaluating second(tup2) {0}",
(model.Evaluate(ctx.MkApp(second, tup2))));
}
else
{
Console.WriteLine("BUG, the constraints are satisfiable.");
}
}
/// <summary>
/// Demonstrate how to use <code>Push</code>and <code>Pop</code>to
/// control the size of models.
/// </summary>
/// <remarks>Note: this test is specialized to 32-bit bitvectors.</remarks>
public static void CheckSmall(Context ctx, Solver solver, BitVecExpr[] to_minimize)
{
Sort bv32 = ctx.MkBitVecSort(32);
int num_Exprs = to_minimize.Length;
UInt32[] upper = new UInt32[num_Exprs];
UInt32[] lower = new UInt32[num_Exprs];
BitVecExpr[] values = new BitVecExpr[num_Exprs];
for (int i = 0; i < upper.Length; ++i)
{
upper[i] = UInt32.MaxValue;
lower[i] = 0;
}
bool some_work = true;
int last_index = -1;
UInt32 last_upper = 0;
while (some_work)
{
solver.Push();
bool check_is_sat = true;
while (check_is_sat && some_work)
{
// Assert all feasible bounds.
for (int i = 0; i < num_Exprs; ++i)
{
solver.Assert(ctx.MkBVULE(to_minimize[i], ctx.MkBV(upper[i], 32)));
}
check_is_sat = Status.SATISFIABLE == solver.Check();
if (!check_is_sat)
{
if (last_index != -1)
{
lower[last_index] = last_upper + 1;
}
break;
}
Console.WriteLine("{0}", solver.Model);
// narrow the bounds based on the current model.
for (int i = 0; i < num_Exprs; ++i)
{
Expr v = solver.Model.Evaluate(to_minimize[i]);
UInt64 ui = ((BitVecNum)v).UInt64;
if (ui < upper[i])
{
upper[i] = (UInt32)ui;
}
Console.WriteLine("{0} {1} {2}", i, lower[i], upper[i]);
}
// find a new bound to add
some_work = false;
last_index = 0;
for (int i = 0; i < num_Exprs; ++i)
{
if (lower[i] < upper[i])
{
last_upper = (upper[i] + lower[i]) / 2;
last_index = i;
solver.Assert(ctx.MkBVULE(to_minimize[i], ctx.MkBV(last_upper, 32)));
some_work = true;
break;
}
}
}
solver.Pop();
}
}
/// <summary>
/// Reduced-size model generation example.
/// </summary>
public static void FindSmallModelExample(Context ctx)
{
Console.WriteLine("FindSmallModelExample");
BitVecExpr x = ctx.MkBVConst("x", 32);
BitVecExpr y = ctx.MkBVConst("y", 32);
BitVecExpr z = ctx.MkBVConst("z", 32);
Solver solver = ctx.MkSolver();
solver.Assert(ctx.MkBVULE(x, ctx.MkBVAdd(y, z)));
CheckSmall(ctx, solver, new BitVecExpr[] { x, y, z });
}
/// <summary>
/// Simplifier example.
/// </summary>
public static void SimplifierExample(Context ctx)
{
Console.WriteLine("SimplifierExample");
IntExpr x = ctx.MkIntConst("x");
IntExpr y = ctx.MkIntConst("y");
IntExpr z = ctx.MkIntConst("z");
IntExpr u = ctx.MkIntConst("u");
Expr t1 = ctx.MkAdd(x, ctx.MkSub(y, ctx.MkAdd(x, z)));
Expr t2 = t1.Simplify();
Console.WriteLine("{0} -> {1}", (t1), (t2));
}
/// <summary>
/// Extract unsatisfiable core example
/// </summary>
public static void UnsatCoreAndProofExample(Context ctx)
{
Console.WriteLine("UnsatCoreAndProofExample");
Solver solver = ctx.MkSolver();
BoolExpr pa = ctx.MkBoolConst("PredA");
BoolExpr pb = ctx.MkBoolConst("PredB");
BoolExpr pc = ctx.MkBoolConst("PredC");
BoolExpr pd = ctx.MkBoolConst("PredD");
BoolExpr p1 = ctx.MkBoolConst("P1");
BoolExpr p2 = ctx.MkBoolConst("P2");
BoolExpr p3 = ctx.MkBoolConst("P3");
BoolExpr p4 = ctx.MkBoolConst("P4");
Expr[] assumptions = new Expr[] { ctx.MkNot(p1), ctx.MkNot(p2), ctx.MkNot(p3), ctx.MkNot(p4) };
BoolExpr f1 = ctx.MkAnd(new BoolExpr[] { pa, pb, pc });
BoolExpr f2 = ctx.MkAnd(new BoolExpr[] { pa, ctx.MkNot(pb), pc });
BoolExpr f3 = ctx.MkOr(ctx.MkNot(pa), ctx.MkNot(pc));
BoolExpr f4 = pd;
solver.Assert(ctx.MkOr(f1, p1));
solver.Assert(ctx.MkOr(f2, p2));
solver.Assert(ctx.MkOr(f3, p3));
solver.Assert(ctx.MkOr(f4, p4));
Status result = solver.Check(assumptions);
if (result == Status.UNSATISFIABLE)
{
Console.WriteLine("unsat");
Console.WriteLine("proof: {0}", solver.Proof);
Console.WriteLine("core: ");
foreach (Expr c in solver.UnsatCore)
{
Console.WriteLine("{0}", c);
}
}
}
/// <summary>
/// Extract unsatisfiable core example with AssertAndTrack
/// </summary>
public static void UnsatCoreAndProofExample2(Context ctx)
{
Console.WriteLine("UnsatCoreAndProofExample2");
Solver solver = ctx.MkSolver();
BoolExpr pa = ctx.MkBoolConst("PredA");
BoolExpr pb = ctx.MkBoolConst("PredB");
BoolExpr pc = ctx.MkBoolConst("PredC");
BoolExpr pd = ctx.MkBoolConst("PredD");
BoolExpr f1 = ctx.MkAnd(new BoolExpr[] { pa, pb, pc });
BoolExpr f2 = ctx.MkAnd(new BoolExpr[] { pa, ctx.MkNot(pb), pc });
BoolExpr f3 = ctx.MkOr(ctx.MkNot(pa), ctx.MkNot(pc));
BoolExpr f4 = pd;
BoolExpr p1 = ctx.MkBoolConst("P1");
BoolExpr p2 = ctx.MkBoolConst("P2");
BoolExpr p3 = ctx.MkBoolConst("P3");
BoolExpr p4 = ctx.MkBoolConst("P4");
solver.AssertAndTrack(f1, p1);
solver.AssertAndTrack(f2, p2);
solver.AssertAndTrack(f3, p3);
solver.AssertAndTrack(f4, p4);
Status result = solver.Check();
if (result == Status.UNSATISFIABLE)
{
Console.WriteLine("unsat");
Console.WriteLine("core: ");
foreach (Expr c in solver.UnsatCore)
{
Console.WriteLine("{0}", c);
}
}
}
public static void FiniteDomainExample(Context ctx)
{
Console.WriteLine("FiniteDomainExample");
FiniteDomainSort s = ctx.MkFiniteDomainSort("S", 10);
FiniteDomainSort t = ctx.MkFiniteDomainSort("T", 10);
FiniteDomainNum s1 = (FiniteDomainNum)ctx.MkNumeral(1, s);
FiniteDomainNum t1 = (FiniteDomainNum)ctx.MkNumeral(1, t);
Console.WriteLine("{0} of size {1}", s, s.Size);
Console.WriteLine("{0} of size {1}", t, t.Size);
Console.WriteLine("{0}", s1);
Console.WriteLine("{0}", t1);
Console.WriteLine("{0}", s1.Int);
Console.WriteLine("{0}", t1.Int);
// But you cannot mix numerals of different sorts
// even if the size of their domains are the same:
// Console.WriteLine("{0}", ctx.MkEq(s1, t1));
}
public static void FloatingPointExample1(Context ctx)
{
Console.WriteLine("FloatingPointExample1");
FPSort s = ctx.MkFPSort(11, 53);
Console.WriteLine("Sort: {0}", s);
FPNum x = (FPNum)ctx.MkNumeral("-1e1", s); /* -1 * 10^1 = -10 */
FPNum y = (FPNum)ctx.MkNumeral("-10", s); /* -10 */
FPNum z = (FPNum)ctx.MkNumeral("-1.25p3", s); /* -1.25 * 2^3 = -1.25 * 8 = -10 */
Console.WriteLine("x={0}; y={1}; z={2}", x.ToString(), y.ToString(), z.ToString());
BoolExpr a = ctx.MkAnd(ctx.MkFPEq(x, y), ctx.MkFPEq(y, z));
Check(ctx, ctx.MkNot(a), Status.UNSATISFIABLE);
/* nothing is equal to NaN according to floating-point
* equality, so NaN == k should be unsatisfiable. */
FPExpr k = (FPExpr)ctx.MkConst("x", s);
FPExpr nan = ctx.MkFPNaN(s);
/* solver that runs the default tactic for QF_FP. */
Solver slvr = ctx.MkSolver("QF_FP");
slvr.Add(ctx.MkFPEq(nan, k));
if (slvr.Check() != Status.UNSATISFIABLE)
throw new TestFailedException();
Console.WriteLine("OK, unsat:" + Environment.NewLine + slvr);
/* NaN is equal to NaN according to normal equality. */
slvr = ctx.MkSolver("QF_FP");
slvr.Add(ctx.MkEq(nan, nan));
if (slvr.Check() != Status.SATISFIABLE)
throw new TestFailedException();
Console.WriteLine("OK, sat:" + Environment.NewLine + slvr);
/* Let's prove -1e1 * -1.25e3 == +100 */
x = (FPNum)ctx.MkNumeral("-1e1", s);
y = (FPNum)ctx.MkNumeral("-1.25p3", s);
FPExpr x_plus_y = (FPExpr)ctx.MkConst("x_plus_y", s);
FPNum r = (FPNum)ctx.MkNumeral("100", s);
slvr = ctx.MkSolver("QF_FP");
slvr.Add(ctx.MkEq(x_plus_y, ctx.MkFPMul(ctx.MkFPRoundNearestTiesToAway(), x, y)));
slvr.Add(ctx.MkNot(ctx.MkFPEq(x_plus_y, r)));
if (slvr.Check() != Status.UNSATISFIABLE)
throw new TestFailedException();
Console.WriteLine("OK, unsat:" + Environment.NewLine + slvr);
}
public static void FloatingPointExample2(Context ctx)
{
Console.WriteLine("FloatingPointExample2");
FPSort double_sort = ctx.MkFPSort(11, 53);
FPRMSort rm_sort = ctx.MkFPRoundingModeSort();
FPRMExpr rm = (FPRMExpr)ctx.MkConst(ctx.MkSymbol("rm"), rm_sort);
BitVecExpr x = (BitVecExpr)ctx.MkConst(ctx.MkSymbol("x"), ctx.MkBitVecSort(64));
FPExpr y = (FPExpr)ctx.MkConst(ctx.MkSymbol("y"), double_sort);
FPExpr fp_val = ctx.MkFP(42, double_sort);
BoolExpr c1 = ctx.MkEq(y, fp_val);
BoolExpr c2 = ctx.MkEq(x, ctx.MkFPToBV(rm, y, 64, false));
BoolExpr c3 = ctx.MkEq(x, ctx.MkBV(42, 64));
BoolExpr c4 = ctx.MkEq(ctx.MkNumeral(42, ctx.RealSort), ctx.MkFPToReal(fp_val));
BoolExpr c5 = ctx.MkAnd(c1, c2, c3, c4);
Console.WriteLine("c5 = " + c5);
/* Generic solver */
Solver s = ctx.MkSolver();
s.Assert(c5);
Console.WriteLine(s);
if (s.Check() != Status.SATISFIABLE)
throw new TestFailedException();
Console.WriteLine("OK, model: {0}", s.Model.ToString());
}
public static void TranslationExample()
{
Context ctx1 = new Context();
Context ctx2 = new Context();
Sort s1 = ctx1.IntSort;
Sort s2 = ctx2.IntSort;
Sort s3 = s1.Translate(ctx2);
Console.WriteLine(s1 == s2);
Console.WriteLine(s1.Equals(s2));
Console.WriteLine(s2.Equals(s3));
Console.WriteLine(s1.Equals(s3));
Expr e1 = ctx1.MkIntConst("e1");
Expr e2 = ctx2.MkIntConst("e1");
Expr e3 = e1.Translate(ctx2);
Console.WriteLine(e1 == e2);
Console.WriteLine(e1.Equals(e2));
Console.WriteLine(e2.Equals(e3));
Console.WriteLine(e1.Equals(e3));
}
static void Main(string[] args)
{
try
{
Microsoft.Z3.Global.ToggleWarningMessages(true);
Log.Open("test.log");
Console.Write("Z3 Major Version: ");
Console.WriteLine(Microsoft.Z3.Version.Major.ToString());
Console.Write("Z3 Full Version: ");
Console.WriteLine(Microsoft.Z3.Version.ToString());
Console.Write("Z3 Full Version String: ");
Console.WriteLine(Microsoft.Z3.Version.FullVersion);
SimpleExample();
// These examples need model generation turned on.
using (Context ctx = new Context(new Dictionary<string, string>() { { "model", "true" } }))
{
BasicTests(ctx);
CastingTest(ctx);
SudokuExample(ctx);
QuantifierExample1(ctx);
QuantifierExample2(ctx);
LogicExample(ctx);
ParOrExample(ctx);
FindModelExample1(ctx);
FindModelExample2(ctx);
PushPopExample1(ctx);
ArrayExample1(ctx);
ArrayExample3(ctx);
BitvectorExample1(ctx);
BitvectorExample2(ctx);
ParserExample1(ctx);
ParserExample2(ctx);
ParserExample5(ctx);
ITEExample(ctx);
EvalExample1(ctx);
EvalExample2(ctx);
FindSmallModelExample(ctx);
SimplifierExample(ctx);
FiniteDomainExample(ctx);
FloatingPointExample1(ctx);
FloatingPointExample2(ctx);
}
// These examples need proof generation turned on.
using (Context ctx = new Context(new Dictionary<string, string>() { { "proof", "true" } }))
{
ProveExample1(ctx);
ProveExample2(ctx);
ArrayExample2(ctx);
TupleExample(ctx);
ParserExample3(ctx);
EnumExample(ctx);
ListExample(ctx);
TreeExample(ctx);
ForestExample(ctx);
UnsatCoreAndProofExample(ctx);
UnsatCoreAndProofExample2(ctx);
}
// These examples need proof generation turned on and auto-config set to false.
using (Context ctx = new Context(new Dictionary<string, string>()
{ {"proof", "true" },
{"auto-config", "false" } }))
{
QuantifierExample3(ctx);
QuantifierExample4(ctx);
}
TranslationExample();
Log.Close();
if (Log.isOpen())
Console.WriteLine("Log is still open!");
}
catch (Z3Exception ex)
{
Console.WriteLine("Z3 Managed Exception: " + ex.Message);
Console.WriteLine("Stack trace: " + ex.StackTrace);
}
catch (TestFailedException ex)
{
Console.WriteLine("TEST CASE FAILED: " + ex.Message);
}
}
}
}
|