everystate 0.1.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +121 -86
- package/bin/everystate.mjs +35 -0
- package/native/darwin-arm64/everystate +0 -0
- package/native/darwin-x64/everystate +0 -0
- package/native/linux-arm64/everystate +0 -0
- package/native/linux-x64/everystate +0 -0
- package/native/win32-x64/everystate.exe +0 -0
- package/package.json +29 -24
- package/skills/formal-verification/SKILL.md +115 -0
- package/skills/formal-verification/example/Charge.cfg +7 -0
- package/skills/formal-verification/example/Charge.tla +61 -0
- package/skills/formal-verification/example/ChargeBroken.cfg +7 -0
- package/wasm/cli.mjs +26 -0
- package/wasm/everystate.d.ts +33 -0
- package/wasm/everystate.mjs +112 -0
- package/wasm/everystate.wasm +0 -0
- package/CONFORMANCE.md +0 -64
- package/dist/bundled.d.ts +0 -1
- package/dist/bundled.js +0 -2
- package/dist/cfg.d.ts +0 -24
- package/dist/cfg.js +0 -252
- package/dist/checker.d.ts +0 -96
- package/dist/checker.js +0 -520
- package/dist/cli.d.ts +0 -2
- package/dist/cli.js +0 -4
- package/dist/community.d.ts +0 -17
- package/dist/community.js +0 -1381
- package/dist/entry.d.ts +0 -2
- package/dist/entry.js +0 -39
- package/dist/eval.d.ts +0 -119
- package/dist/eval.js +0 -1533
- package/dist/index.d.ts +0 -33
- package/dist/index.js +0 -24
- package/dist/java.d.ts +0 -2
- package/dist/java.js +0 -6
- package/dist/lexer.d.ts +0 -17
- package/dist/lexer.js +0 -255
- package/dist/liveness.d.ts +0 -80
- package/dist/liveness.js +0 -855
- package/dist/main.d.ts +0 -40
- package/dist/main.js +0 -494
- package/dist/model.d.ts +0 -56
- package/dist/model.js +0 -582
- package/dist/modules.d.ts +0 -91
- package/dist/modules.js +0 -649
- package/dist/node-host.d.ts +0 -2
- package/dist/node-host.js +0 -25
- package/dist/parser.d.ts +0 -30
- package/dist/parser.js +0 -1114
- package/dist/pprint.d.ts +0 -7
- package/dist/pprint.js +0 -647
- package/dist/stdlib.d.ts +0 -14
- package/dist/stdlib.js +0 -588
- package/dist/values.d.ts +0 -150
- package/dist/values.js +0 -822
package/dist/bundled.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
// GENERATED by scripts/bundle.mjs - TLA+ sources of bundled library modules. Do not edit.
|
|
2
|
-
export const BUNDLED = { "BagsExt": "------------------------------ MODULE BagsExt ------------------------------\n(***************************************************************************)\n(* Additional operators on bags (multisets). *)\n(* *)\n(* Bags represent multisets - collections where elements can appear *)\n(* multiple times. In TLA+, bags are represented as functions from *)\n(* elements to positive natural numbers, where bag[x] gives the *)\n(* multiplicity (count) of element x. *)\n(* *)\n(* This module extends the standard Bags module with: *)\n(* - Element manipulation (add, remove individual/all occurrences) *)\n(* - Folding operations (generalized aggregation over bag elements) *)\n(* - Arithmetic operations (sum, product of bag elements) *)\n(* *)\n(* Example bag representation: *)\n(* The bag {| 1, 1, 2, 3, 3, 3 |} is represented as: *)\n(* (1 :> 2) @@ (2 :> 1) @@ (3 :> 3) *)\n(***************************************************************************)\n\nLOCAL INSTANCE Bags\nLOCAL INSTANCE Integers\nLOCAL INSTANCE Folds\n\n(***************************************************************************)\n(* Add one occurrence of element x to bag B. *)\n(* *)\n(* Increases the multiplicity of x by 1. If x is not in the bag, *)\n(* adds it with multiplicity 1. *)\n(* *)\n(* This is equivalent to B (+) SetToBag({x}. *)\n(* *)\n(* Examples: *)\n(* BagAdd((1 :> 2) @@ (2 :> 1), 1) = (1 :> 3) @@ (2 :> 1) *)\n(* BagAdd((1 :> 2) @@ (2 :> 1), 3) = (1 :> 2) @@ (2 :> 1) @@ (3 :> 1) *)\n(***************************************************************************)\nBagAdd(B, x) ==\n IF x \\in DOMAIN B\n THEN [e \\in DOMAIN B |-> IF e=x THEN B[e]+1 ELSE B[e]]\n ELSE [e \\in DOMAIN B \\union {x} |-> IF e=x THEN 1 ELSE B[e]]\n\n(***************************************************************************)\n(* Remove one occurrence of element x from bag B. *)\n(* *)\n(* Decreases the multiplicity of x by 1. If x has multiplicity 1, *)\n(* removes it entirely from the bag. If x is not in the bag, equals *)\n(* the bag unchanged. *)\n(* *)\n(* This is equivalent to B (-) SetToBag({x}). *)\n(* *)\n(* Examples: *)\n(* BagRemove((1 :> 2) @@ (2 :> 1), 1) = (1 :> 1) @@ (2 :> 1) *)\n(* BagRemove((1 :> 1) @@ (2 :> 1), 1) = (2 :> 1) *)\n(* BagRemove((1 :> 2) @@ (2 :> 1), 3) = (1 :> 2) @@ (2 :> 1) *)\n(***************************************************************************)\nBagRemove(B,x) ==\n IF x \\in DOMAIN B\n THEN IF B[x] = 1\n THEN [e \\in DOMAIN B \\ {x} |-> B[e]]\n ELSE [e \\in DOMAIN B |-> IF e=x THEN B[e]-1 ELSE B[e]]\n ELSE B\n\n(***************************************************************************)\n(* Remove all occurrences of element x from bag B. *)\n(* *)\n(* Completely removes element x from the bag, regardless of its *)\n(* multiplicity. If x is not in the bag, returns the bag unchanged. *)\n(* *)\n(* Examples: *)\n(* BagRemoveAll((1 :> 3) @@ (2 :> 1), 1) = (2 :> 1) *)\n(* BagRemoveAll((1 :> 3) @@ (2 :> 1), 3) = (1 :> 3) @@ (2 :> 1) *)\n(***************************************************************************)\nBagRemoveAll(B,x) ==\n [e \\in DOMAIN B \\ {x} |-> B[e]]\n \n(***********************************************************************)\n(* Fold operation op over the images through f of all elements of bag *)\n(* B, starting from base. The parameter choose indicates the order in *)\n(* which elements of the bag are processed; all replicas of an element *)\n(* are processed consecutively. *)\n(* *)\n(* Examples: *)\n(* MapThenFoldBag(LAMBDA x,y : x+y, *)\n(* 0, *)\n(* LAMBDA x: 1, *)\n(* LAMBDA B : CHOOSE x \\in DOMAIN B : TRUE, *)\n(* (1 :> 2) @@ (2 :> 1) @@ (3 :> 3)) = 6 *)\n(* *)\n(* MapThenFoldBag(LAMBDA x,y : x \\o y, *)\n(* << >>, *)\n(* LAMBDA x: << x >>, *)\n(* LAMBDA S: CHOOSE x \\in S : \\A y \\in S : x <= y, *)\n(* (1 :> 2) @@ (2 :> 1) @@ (3 :> 3)) *)\n(* = <<1,1,2,3,3,3>> *)\n(* *)\n(* The fifth argument represents the bag {| 1, 1, 2, 3, 3, 3 |}. *)\n(* The first example counts the elements of the bag. *)\n(* The second example computes a sorted sequence of all elements *)\n(* of the bag. It is brittle because concatenation of sequences is *)\n(* non-commutative, and the result therefore relies on the *)\n(* definition of MapThenFoldSet. *)\n(***********************************************************************)\n MapThenFoldBag(op(_,_), base, f(_), choose(_), B) ==\n LET handle(x) == \\* handle all occurrences of x in B\n LET pow[n \\in Nat \\ {0}] ==\n op(IF n=1 THEN base ELSE pow[n-1], f(x))\n IN pow[B[x]]\n IN MapThenFoldSet(op, base, handle,\n LAMBDA S : CHOOSE x \\in S : TRUE,\n DOMAIN B)\n\n(************************************************************************)\n(* Fold op over all elements of bag B, starting with value base. *)\n(* *)\n(* Example: *)\n(* FoldBag(LAMBDA x,y : x+y, *)\n(* 0, *)\n(* (1 :> 2) @@ (2 :> 1) @@ (3 :> 3)) = 13 *)\n(* The third argument represents the bag {| 1, 1, 2, 3, 3, 3 |}. *)\n(************************************************************************)\nFoldBag(op(_,_), base, B) ==\n MapThenFoldBag(op, base, LAMBDA x: x, LAMBDA S : CHOOSE x \\in S : TRUE, B)\n\n(************************************************************************)\n(* Compute the sum of the elements of B. *)\n(************************************************************************)\nSumBag(B) ==\n FoldBag(LAMBDA x,y : x+y, 0, B)\n\n(************************************************************************)\n(* Compute the product of the elements of B. *)\n(************************************************************************)\nProductBag(B) ==\n FoldBag(LAMBDA x,y : x*y, 1, B)\n\n=============================================================================\n", "Bitwise": "------------------------------- MODULE Bitwise -------------------------------\nLOCAL INSTANCE Integers\n\n\\* https://en.wikipedia.org/wiki/Bitwise_operation#Mathematical_equivalents\nRECURSIVE And(_,_,_,_)\nLOCAL And(x,y,n,m) == \n LET exp == 2^n\n IN IF m = 0 \n THEN 0\n ELSE exp * ((x \\div exp) % 2) * ((y \\div exp) % 2) \n + And(x,y,n+1,m \\div 2)\n\n(***************************************************************************)\n(* Bitwise AND of (non-negative) x and y (defined for Nat \\cup {0}). *)\n(***************************************************************************)\nx & y == \n IF x >= y THEN And(x, y, 0, x) ELSE And(y, x, 0, y) \\* Infix variant of And(x,y)\n\n-------------------------------------------------------------------------------\n\nRECURSIVE Or(_,_,_,_)\nLOCAL Or(x,y,n,m) == \n LET exp == 2^n\n xdm == (x \\div exp) % 2\n ydm == (y \\div exp) % 2\n IN IF m = 0 \n THEN 0\n ELSE exp * (((xdm + ydm) + (xdm * ydm)) % 2)\n + Or(x,y,n+1,m \\div 2)\n\n(***************************************************************************)\n(* Bitwise OR of (non-negative) x and y (defined for Nat \\cup {0}). *)\n(***************************************************************************)\nx | y == \n IF x >= y THEN Or(x, y, 0, x) ELSE Or(y, x, 0, y) \\* Infix variant of Or(x,y)\n\n-------------------------------------------------------------------------------\n\nRECURSIVE Xor(_,_,_,_)\nLOCAL Xor(x,y,n,m) == \n LET exp == 2^n\n IN IF m = 0 \n THEN 0\n ELSE exp * (((x \\div exp) + (y \\div exp)) % 2) \n + Xor(x,y,n+1,m \\div 2)\n\n(***************************************************************************)\n(* Bitwise XOR of (non-negative) x and y (defined for Nat \\cup {0}). *)\n(***************************************************************************)\nx ^^ y == \\* single \"^\" already taken by Naturals.tla\n IF x >= y THEN Xor(x, y, 0, x) ELSE Xor(y, x, 0, y) \\* Infix variant of Xor(x,y)\n\n-------------------------------------------------------------------------------\n\nRECURSIVE NotR(_,_,_)\nLOCAL NotR(x,n,m) == \n LET exp == 2^n\n xdm == (x \\div exp) % 2\n IN IF m = 0 \n THEN 0\n ELSE exp * ((xdm + 1) % 2)\n + NotR(x,n+1,m \\div 2)\n\n(***************************************************************************)\n(* Bitwise NOT of (non-negative) x (defined for Nat \\cup {0}). *)\n(***************************************************************************)\nNot(a) ==\n NotR(a,0,a)\n\n-------------------------------------------------------------------------------\n\n(***************************************************************************)\n(* Logical bit-shifting the (non-negative) n by pos positions to the right *)\n(* shifting zeros in from the left/MSB (defined for Nat \\cup {0}). *)\n(***************************************************************************)\nRECURSIVE shiftR(_,_)\nshiftR(n,pos) == \n IF pos = 0 \n THEN n\n ELSE LET odd(z) == z % 2 = 1\n m == IF odd(n) THEN (n-1) \\div 2 ELSE n \\div 2\n IN shiftR(m, pos - 1)\n\n=============================================================================\n\\* Modification History\n\\* Last modified Thu April 25 10:56:12 CEST 2018 by markus\n\\* Created Mon May 16 15:09:18 CEST 2016 by markus\n", "CBOR": "-------------------------------- MODULE CBOR --------------------------------\n(***************************************************************************)\n(* Encodes TLA+ values as CBOR (RFC 8949) and decodes them back. *)\n(* CBORSerialize and CBORDeserialize exchange values through files. *)\n(* Public ToCBOR and FromCBOR work in memory, without file I/O. *)\n(* ToCBOR returns a sequence of integers in 0..255, one per encoded byte. *)\n(* Use its length to check message sizes, or FromCBOR to interpret bytes. *)\n(* *)\n(* In a module that uses CBOR: *)\n(* EXTENDS Naturals, Sequences, CBOR *)\n(* Message == [id |-> 1, payload |-> <<TRUE, FALSE>>] *)\n(* Fits == Len(ToCBOR(Message)) <= 1024 *)\n(* Decoded == FromCBOR(<<130, 1, 245>>) \\* equals <<1, TRUE>> *)\n(* *)\n(* For files, using the same imports and Message definition: *)\n(* WriteMessage == CBORSerialize(\"/tmp/message.cbor\", Message) *)\n(* ReadMessage == CBORDeserialize(\"/tmp/message.cbor\") *)\n(* *)\n(* Requires CommunityModules.jar or CommunityModules-deps.jar on TLC's *)\n(* classpath. See README.md for setup. *)\n(* Values must be finite and encodable: integers within TLC's range and *)\n(* strings of valid Unicode. Set elements and function arguments must be *)\n(* comparable by TLC for FromCBOR(ToCBOR(v)) = v to hold. Deep nesting *)\n(* can exhaust the JVM stack, which a larger -Xss setting can relieve. *)\n(***************************************************************************)\n\nLOCAL INSTANCE TLC\n\n(***************************************************************************)\n(* The CBOR encoding of value as a sequence of integers in 0..255, one *)\n(* per byte. Sets use tag 258, model values tag 39, and functions that *)\n(* are neither sequences nor records tag 33000 (registered with IANA). *)\n(* Equal values give identical bytes. TLC reports an error if value *)\n(* cannot be encoded. *)\n(***************************************************************************)\nToCBOR(value) ==\n Assert(FALSE, \"ToCBOR needs CommunityModules.jar on TLC's classpath.\")\n\n(***************************************************************************)\n(* The TLA+ value of the CBOR data item in bytes, a sequence of integers *)\n(* in 0..255. TLC reports an error for malformed input and for anything *)\n(* it cannot represent, such as floats or a model value it does not know. *)\n(***************************************************************************)\nFromCBOR(bytes) ==\n Assert(FALSE, \"FromCBOR needs CommunityModules.jar on TLC's classpath.\")\n\n(***************************************************************************)\n(* Writes the bytes of ToCBOR(value) to the file absoluteFilename and *)\n(* equals TRUE. Creates missing parent directories and replaces an *)\n(* existing file. If value cannot be encoded, TLC reports an error and *)\n(* leaves the file untouched. *)\n(***************************************************************************)\nCBORSerialize(absoluteFilename, value) ==\n Assert(FALSE, \"CBORSerialize needs CommunityModules.jar on TLC's classpath.\")\n\n(***************************************************************************)\n(* FromCBOR applied to the bytes of the file absoluteFilename. A *)\n(* definition without parameters is constant-level, so TLC reuses its *)\n(* value instead of reading the file again in each state. *)\n(***************************************************************************)\nCBORDeserialize(absoluteFilename) ==\n Assert(FALSE, \"CBORDeserialize needs CommunityModules.jar on TLC's classpath.\")\n\n=============================================================================\n", "CSV": "-------------------------------- MODULE CSV --------------------------------\n(***************************************************************************)\n(* This module provides operators for reading and writing CSV (Comma *)\n(* Separated Values) files. These operators are overridden by Java *)\n(* implementations that perform the actual file I/O operations. *)\n(* *)\n(* The CSV module is particularly useful for: *)\n(* - Logging TLA+ values during model checking *)\n(* - Exporting computation results for analysis *)\n(* - Creating test data files *)\n(* - Interfacing with external data processing tools *)\n(* *)\n(* Note: All operators in this module evaluate to TRUE in TLA+ but *)\n(* perform side effects when executed by TLC with the Java overrides. *)\n(***************************************************************************)\n\nLOCAL INSTANCE TLC\nLOCAL INSTANCE Integers\n (*************************************************************************)\n (* Imports the definitions from the modules, but doesn't export them. *)\n (*************************************************************************)\n\n(***************************************************************************)\n(* Writes a sequence of values to a CSV file using a format template. *)\n(* *)\n(* Parameters: *)\n(* - template: Printf-style format string with placeholders *)\n(* - val: Sequence of values to write *)\n(* - file: Absolute path to the output file *)\n(* *)\n(* The template uses Java's String.format syntax: *)\n(* - %1$s refers to the first element in val *)\n(* - %2$s refers to the second element in val, etc. *)\n(* *)\n(* Example: *)\n(* CSVWrite(\"%1$s#%2$s#%3$s\", *)\n(* <<\"abc\", 42, {\"x\", \"y\"}>>, *)\n(* \"/tmp/output.csv\") *)\n(* Writes: \"abc#42#{\\\"x\\\", \\\"y\\\"}\" to /tmp/output.csv *)\n(***************************************************************************)\nCSVWrite(template, val, file) == \n TRUE\n\n(***************************************************************************)\n(* Writes a record to a CSV file with optional headers. *)\n(* *)\n(* Parameters: *)\n(* - record: A record (function) to write *)\n(* - delim: Field delimiter string *)\n(* - headers: Boolean indicating whether to write headers *)\n(* - file: Absolute path to the output file *)\n(* *)\n(* If headers is TRUE and the file is empty, field names are written *)\n(* as the first row. The record values are then written as a data row. *)\n(* *)\n(* Example: *)\n(* CSVWriteRecord([name |-> \"Alice\", age |-> 30], *)\n(* \",\", *)\n(* CSVRecords(\"/tmp/people.csv\") = 0, *)\n(* \"/tmp/people.csv\") *)\n(* First call writes: \"name,age\" then \"Alice,30\" *)\n(* Subsequent calls write only data rows *)\n(***************************************************************************)\nCSVWriteRecord(record, delim, headers, file) == \n TRUE\n\n(***************************************************************************)\n(* Reads a CSV file and returns the data as a sequence of records. *)\n(* *)\n(* Parameters: *)\n(* - columns: Sequence of column names for the records *)\n(* - delimiter: Field delimiter character *)\n(* - file: Absolute path to the input file *)\n(* *)\n(* Returns a sequence where each element is a record mapping column *)\n(* names to their string values. All values are strings and must be *)\n(* converted if numeric processing is needed. *)\n(* *)\n(* Example: *)\n(* CSVRead(<<\"name\", \"age\", \"city\">>, \",\", \"/tmp/people.csv\") *)\n(* might return: *)\n(* <<[name |-> \"Alice\", age |-> \"30\", city |-> \"NYC\"], *)\n(* [name |-> \"Bob\", age |-> \"25\", city |-> \"LA\"]>> *)\n(***************************************************************************)\nCSVRead(columns, delimiter, file) == \n TRUE\n\n(***************************************************************************)\n(* Returns the number of records in a CSV file. *)\n(* *)\n(* This includes all rows in the file, including any header row. *)\n(* For files that don't exist, returns 0. *)\n(* *)\n(* Example: *)\n(* CSVRecords(\"/tmp/data.csv\") = 5 \\* File has 5 rows *)\n(* CSVRecords(\"/nonexistent.csv\") = 0 *)\n(***************************************************************************)\nCSVRecords(file) == \n CHOOSE n : n \\in Nat\n\n============================================================================\n", "Combinatorics": "------------------------- MODULE Combinatorics -------------------------\nEXTENDS Naturals\n\nfactorial[n \\in Nat] == \n IF n = 0 THEN 1 ELSE n * factorial[n-1]\n\nchoose(n, k) ==\n factorial[n] \\div (factorial[k] * factorial[n - k])\n\n=========================================================================", "DifferentialEquations": "----------------------- MODULE DifferentialEquations ------------------------\r\nLOCAL INSTANCE Reals\r\nLOCAL INSTANCE Sequences\r\nLOCAL PosReal == {r \\in Real : r > 0}\r\nLOCAL OpenInterval(a, b) == {s \\in Real : a < s /\\ s < b}\r\nLOCAL Nbhd(r,e) == OpenInterval(r-e, r+e)\r\nLOCAL IsFirstDeriv(df, f) ==\r\n /\\ df \\in [DOMAIN f -> Real]\r\n /\\ \\A r \\in DOMAIN f : \r\n \\A e \\in PosReal :\r\n \\E d \\in PosReal : \r\n \\A s \\in Nbhd(r,d) \\ {r} :\r\n (f[s] - f[r])/(s - r) \\in Nbhd(df[r], e)\r\n\r\nLOCAL IsDeriv(n, df, f) == \r\n LET IsD[k \\in 0..n, g \\in [DOMAIN f -> Real]] ==\r\n IF k = 0 \r\n THEN g = f\r\n ELSE \\E gg \\in [DOMAIN f -> Real] : /\\ IsFirstDeriv(g, gg)\r\n /\\ IsD[k-1, gg]\r\n IN IsD[n, df]\r\n\r\nIntegrate(D, a, b, InitVals) ==\r\n LET n == Len(InitVals)\r\n gg == CHOOSE g : \r\n \\E e \\in PosReal : \r\n /\\ g \\in [0..n -> [OpenInterval(a-e, b+e) -> Real]]\r\n /\\ \\A i \\in 1..n : /\\ IsDeriv(i, g[i], g[0])\r\n /\\ g[i-1][a] = InitVals[i]\r\n /\\ \\A r \\in OpenInterval(a-e, b+e) :\r\n D[ <<r>> \\o [i \\in 1..(n+1) |-> g[i-1][r]] ] = 0\r\n IN [i \\in 1..n |-> gg[i-1][b]]\r\n=============================================================================\r\n\r\n\r\n\r\n", "DyadicRationals": "-------------------------- MODULE DyadicRationals ---------------------------\n\\* https://en.wikipedia.org/wiki/Dyadic_rational\nLOCAL INSTANCE FiniteSets\nLOCAL INSTANCE Integers\nLOCAL INSTANCE Sequences\nLOCAL INSTANCE FiniteSetsExt\nLOCAL INSTANCE TLC\n\nLOCAL Divides(p, q) == \n \\E d \\in 1..q : q = p * d\n\nLOCAL Divisors(q) ==\n {d \\in 1..q : Divides(d, q)}\n\nLOCAL GCD(n, m) ==\n Max(Divisors(n) \\cap Divisors(m))\n\n------------------------------------------------------------------------------\n\nLOCAL Rational(num, den) ==\n [num |-> num, den |-> den]\n\nLOCAL Reduce(p) ==\n LET gcd == GCD(p.num, p.den)\n IN IF gcd = 1 THEN p\n ELSE Rational(p.num \\div gcd, p.den \\div gcd)\n\nIsDyadicRational(r) ==\n \\E i \\in 0..r.den: 2^i = r.den \n\nZero == Rational(0,1)\n\nOne == Rational(1,1)\n\nAdd(p, q) ==\n IF p = Zero THEN q ELSE\n LET lcn == Max({p.den, q.den}) \\* shortcut because dyadic!\n qq == Rational(q.num * (lcn \\div q.den), q.den * (lcn \\div q.den))\n pp == Rational(p.num * (lcn \\div p.den), p.den * (lcn \\div p.den))\n IN Reduce(Rational(qq.num + pp.num, lcn))\n\nHalf(p) ==\n Reduce(Rational(p.num, p.den * 2))\n\nPrettyPrint(p) ==\n IF p = Zero THEN \"0\" ELSE\n IF p = One THEN \"1\" ELSE\n ToString(p.num) \\o \"/\" \\o ToString(p.den)\n\n===============================================================================\n\\* Modification History\n\\* Last modified Mon Dec 27 17:52:54 PST 2021 by Markus Kuppe\n\\* Created Sun Dec 26 01:36:40 PDT 2021 by Markus Kuppe", "FiniteSetsExt": "--------------------------- MODULE FiniteSetsExt ---------------------------\nEXTENDS Integers, FiniteSets, Folds, Functions \\* Replace with LOCAL INSTANCE when https://github.com/tlaplus/tlapm/issues/119 is resolved.\n\\* LOCAL INSTANCE Integers\n\\* LOCAL INSTANCE FiniteSets\n\n(*************************************************************************)\n(* Fold op over the elements of set using base as starting value. *)\n(* *)\n(* Example: *)\n(* FoldSet(LAMBA x,y : x + y, 0, 0 .. 10) = 55 *)\n(*************************************************************************)\nFoldSet(op(_,_), base, set) ==\n MapThenFoldSet(op, base, LAMBDA x : x, LAMBDA s : CHOOSE x \\in s : TRUE, set)\n\n(*************************************************************************)\n(* Calculate the sum of the elements in set. *)\n(* *)\n(* Example: *)\n(* SumSet(0 .. 10) = 55 *)\n(*************************************************************************)\nSumSet(set) ==\n FoldSet(+, 0, set)\n\n(*************************************************************************)\n(* Calculuate the product of the elements in set. *)\n(* *)\n(* Example: *)\n(* ProductSet(1 .. 3) = 6 *)\n(*************************************************************************)\nProductSet(set) ==\n FoldSet(LAMBDA x, y: x * y, 1, set)\n\n(*************************************************************************)\n(* An alias for FoldSet. ReduceSet was used instead of FoldSet in *)\n(* earlier versions of the community modules. *)\n(*************************************************************************)\nReduceSet(op(_, _), set, acc) == \n FoldSet(op, acc, set)\n\n(*************************************************************************)\n(* Calculate the sum of projections of the elements in a set. *)\n(* *)\n(* Example: *)\n(* MapThenSumSet( *)\n(* LAMBDA e : e.n, *)\n(* {[n |-> 0], [n |-> 1], [n |-> 2]} *)\n(* ) = 3 *)\n(*************************************************************************)\nMapThenSumSet(op(_), set) ==\n MapThenFoldSet(+, 0, op, LAMBDA s : CHOOSE x \\in s : TRUE, set)\n\n(***************************************************************************)\n(* Flattens a set of sets into a single set containing all elements. *)\n(* *)\n(* This is equivalent to TLA+ UNION operator. *)\n(* *)\n(* NOTE: Use UNION directly instead of this operator. FlattenSet is only *)\n(* kept for backward compatibility. *)\n(* *)\n(* Example: *)\n(* FlattenSet({{1,2}, {2,3}, {4}}) = {1,2,3,4} *)\n(* FlattenSet({}) = {} *)\n(* FlattenSet({{}}) = {} *)\n(***************************************************************************)\nFlattenSet(S) ==\n UNION S\n\n(***************************************************************************)\n(* The symmetric difference of two sets. *)\n(* *)\n(* The symmetric difference of sets A and B contains elements that are *)\n(* in exactly one of A or B, but not in both. This is equivalent to *)\n(* (A ∪ B) \\ (A ∩ B). *)\n(* *)\n(* Examples: *)\n(* SymDiff({1,2,3}, {2,3,4}) = {1,4} *)\n(* SymDiff({1,2}, {3,4}) = {1,2,3,4} *)\n(* SymDiff({1,2}, {1,2}) = {} *)\n(***************************************************************************)\nSymDiff(A, B) == (A \\ B) \\cup (B \\ A)\n\n-----------------------------------------------------------------------------\n\n(*************************************************************************)\n(* Quantify the elements in S matching the predicate (LAMDBA) P. *)\n(* *)\n(* This operator is overridden by FiniteSetsExt#quantify whose *)\n(* implementation does *not* enumerate the intermediate set! This is *)\n(* the only advantage that Quantify(...) has over Cardinality(...). *)\n(* *)\n(* Example: *)\n(* Quantify(1..9, LAMBDA n : n % 2 = 0) = 4 *)\n(*************************************************************************)\nQuantify(S, P(_)) ==\n Cardinality({s \\in S : P(s)})\n\n-----------------------------------------------------------------------------\n\n(*************************************************************************)\n(* A k-subset ks of a set S has Cardinality(ks) = k. The number of *)\n(* k-subsets of a set S with Cardinality(S) = n is given by the binomial *)\n(* coefficients n over k. A set S with Cardinality(S) = n has 2^n *)\n(* k-subsets. \\A k \\notin 0..Cardinality(S): kSubset(k, S) = {}. *)\n(* *)\n(* This operator is overridden by FiniteSetsExt#kSubset whose *)\n(* implementation, contrary to { s \\in SUBSET S : Cardinality(s) = k }, *)\n(* only enumerates the k-subsets of S and not all subsets. *)\n(* *)\n(* Example: *)\n(* kSubset(2, 1..3) = {{1,2},{2,3},{3,1}} *)\n(*************************************************************************)\nkSubset(k, S) == \n { s \\in SUBSET S : Cardinality(s) = k }\n\n-----------------------------------------------------------------------------\n\n(***************************************************************************)\n(* We define Max(S) and Min(S) to be the maximum and minimum, *)\n(* respectively, of a finite, non-empty set S of integers. *)\n(***************************************************************************)\nMax(S) == CHOOSE x \\in S : \\A y \\in S : x >= y\nMin(S) == CHOOSE x \\in S : \\A y \\in S : x =< y\n\n-----------------------------------------------------------------------------\n\n(***************************************************************************)\n(* Compute all possible choice sets from a collection of sets. *)\n(* *)\n(* Given a collection of sets, this operator equals all possible sets *)\n(* that can be formed by choosing exactly one element from each input set. *)\n(* This is related to the Cartesian product. *)\n(* *)\n(* The implementation uses choice functions: functions that map each *)\n(* input set to one of its elements. The range of each such function *)\n(* gives us one possible choice set. *)\n(* *)\n(* Examples: *)\n(* Choices({{1,2}, {3}}) = {{1,3}, {2,3}} *)\n(* Choices({{1,2}, {2,3}, {5}}) = *)\n(* {{1,2,5}, {1,3,5}, {2,3,5}, {2,5}} *)\n(* (Note: {2,5} appears because we choose 2 from both sets) *)\n(* Choices({}) = {{}} (empty choice from empty collection) *)\n(***************************************************************************)\nChoices(Sets) == LET ChoiceFunction(Ts) == { f \\in [Ts -> UNION Ts] : \n \\A T \\in Ts : f[T] \\in T }\n IN { Range(f) : f \\in ChoiceFunction(Sets) }\n\n-----------------------------------------------------------------------------\n\n(***************************************************************************)\n(* Chooses unique element from the input set matching the predicate *)\n(* (LAMDBA) P. *)\n(* *)\n(* Contrary to CHOOSE, fails with *)\n(* \"CHOOSE x \\\\in S: P, but no element of S satisfied P: *)\n(* not just if P(_) holds for none of the elements in S, but also if *)\n(* P(_) holds for more than one element in S. *)\n(* *)\n(* Example: *)\n(* ChooseUnique({2, 3, 4, 5}, LAMBDA x : x % 3 = 1) = 4 *)\n(* *)\n(***************************************************************************)\nChooseUnique(S, P(_)) == CHOOSE x \\in S :\n P(x) /\\ \\A y \\in S : P(y) => y = x\n\n=============================================================================\n", "FiniteSetsExtTheorems": "-------------------------- MODULE FiniteSetsExtTheorems -------------------\nEXTENDS FiniteSetsExt, FiniteSets, Integers\n(*************************************************************************)\n(* This module only lists theorem statements for reference. The proofs *)\n(* can be found in module FiniteSetsExtTheorems_proofs.tla. *)\n(*************************************************************************)\n\n(*************************************************************************)\n(* Folding over an empty set produces the base value. *)\n(*************************************************************************)\nTHEOREM FoldSetEmpty ==\n ASSUME NEW op(_,_), NEW base\n PROVE FoldSet(op, base, {}) = base\n\n(*************************************************************************)\n(* Folding over a finite non-empty set corresponds to applying the *)\n(* underlying binary operator to some element of the set and the result *)\n(* of folding the remaining set elements. *)\n(*************************************************************************)\nTHEOREM FoldSetNonempty ==\n ASSUME NEW op(_,_), NEW base, NEW S, S # {}, IsFiniteSet(S)\n PROVE \\E x \\in S : FoldSet(op, base, S) = op(x, FoldSet(op, base, S \\ {x}))\n\n(*************************************************************************)\n(* The type of folding a set corresponds to the type associated with the *)\n(* binary operator that underlies the fold. *)\n(*************************************************************************)\nTHEOREM FoldSetType ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ,\n NEW S \\in SUBSET Typ, IsFiniteSet(S),\n \\A t,u \\in Typ : op(t,u) \\in Typ\n PROVE FoldSet(op, base, S) \\in Typ\n\n(*************************************************************************)\n(* If the binary operator is associative and commutative, the result of *)\n(* folding over a non-empty set corresponds to applying the operator to *)\n(* an arbitrary element of the set and the result of folding the *)\n(* remainder of the set. *)\n(*************************************************************************)\nTHEOREM FoldSetAC ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ, \n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n NEW S \\in SUBSET Typ, IsFiniteSet(S),\n NEW x \\in S\n PROVE FoldSet(op, base, S) = op(x, FoldSet(op, base, S \\ {x}))\n\n\\* reformulation in terms of adding an element to the set\nTHEOREM FoldSetACAddElement ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ, \n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n NEW S \\in SUBSET Typ, IsFiniteSet(S),\n NEW x \\in Typ \\ S\n PROVE FoldSet(op, base, S \\union {x}) = op(x, FoldSet(op, base, S))\n\n(*************************************************************************)\n(* For an AC operator `op` for which `base` is the neutral element, *)\n(* folding commutes with set union, for disjoint sets. *)\n(*************************************************************************)\nTHEOREM FoldSetDisjointUnion ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ, \n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n \\A t \\in Typ : op(base, t) = t,\n NEW S \\in SUBSET Typ, IsFiniteSet(S),\n NEW T \\in SUBSET Typ, IsFiniteSet(T), S \\cap T = {}\n PROVE FoldSet(op, base, S \\union T) = \n op(FoldSet(op, base, S), FoldSet(op, base, T))\n\n---------------------------------------------------------------------------\n\n(*************************************************************************)\n(* The sum of a set of natural numbers, resp. integers is a natural *)\n(* number, resp. an integer. *)\n(*************************************************************************)\nTHEOREM SumSetNat ==\n ASSUME NEW S \\in SUBSET Nat, IsFiniteSet(S)\n PROVE SumSet(S) \\in Nat\n\nTHEOREM SumSetInt ==\n ASSUME NEW S \\in SUBSET Int, IsFiniteSet(S)\n PROVE SumSet(S) \\in Int\n\n(*************************************************************************)\n(* The sum of the empty set is 0. *)\n(*************************************************************************)\nTHEOREM SumSetEmpty == SumSet({}) = 0\n\n(*************************************************************************)\n(* The sum of a finite non-empty set is the sum of an arbitrary element *)\n(* and the sum of the remaining elements. *)\n(*************************************************************************)\nTHEOREM SumSetNonempty ==\n ASSUME NEW S \\in SUBSET Int, IsFiniteSet(S), NEW x \\in S \n PROVE SumSet(S) = x + SumSet(S \\ {x})\n\n\\* reformulation of the above in terms of adding an element to a set\nTHEOREM SumSetAddElement ==\n ASSUME NEW S \\in SUBSET Int, IsFiniteSet(S), NEW x \\in Int \\ S\n PROVE SumSet(S \\union {x}) = x + SumSet(S)\n\n(*************************************************************************)\n(* Set summation distributes over union, for disjoint sets of integers. *)\n(*************************************************************************)\nTHEOREM SumSetDisjointUnion ==\n ASSUME NEW S \\in SUBSET Int, IsFiniteSet(S),\n NEW T \\in SUBSET Int, IsFiniteSet(T), S \\cap T = {}\n PROVE SumSet(S \\union T) = SumSet(S) + SumSet(T)\n\n(*************************************************************************)\n(* The sum of a subset of a set of natural numbers is bounded by the sum *)\n(* of the full set. *)\n(*************************************************************************)\nTHEOREM SumSetNatSubset ==\n ASSUME NEW S \\in SUBSET Nat, IsFiniteSet(S),\n NEW T \\in SUBSET S\n PROVE SumSet(T) <= SumSet(S)\n\n(*************************************************************************)\n(* The sum of a set of natural numbers is zero only if the set is empty *)\n(* or the singleton {0}. *)\n(*************************************************************************)\nTHEOREM SumSetNatZero ==\n ASSUME NEW S \\in SUBSET Nat, IsFiniteSet(S)\n PROVE SumSet(S) = 0 <=> S \\subseteq {0}\n\n---------------------------------------------------------------------------\n\n(*************************************************************************)\n(* The product of a set of natural numbers, resp. integers is a natural *)\n(* number, resp. an integer. *)\n(*************************************************************************)\nTHEOREM ProductSetNat ==\n ASSUME NEW S \\in SUBSET Nat, IsFiniteSet(S)\n PROVE ProductSet(S) \\in Nat\n\nTHEOREM ProductSetInt ==\n ASSUME NEW S \\in SUBSET Int, IsFiniteSet(S)\n PROVE ProductSet(S) \\in Int\n\n(*************************************************************************)\n(* The product of the empty set is 1. *)\n(*************************************************************************)\nTHEOREM ProductSetEmpty == ProductSet({}) = 1\n\n(*************************************************************************)\n(* The product of a finite non-empty set is the product of an arbitrary *)\n(* element and the product of the remaining elements. *)\n(*************************************************************************)\nTHEOREM ProductSetNonempty ==\n ASSUME NEW S \\in SUBSET Int, IsFiniteSet(S), NEW x \\in S \n PROVE ProductSet(S) = x * ProductSet(S \\ {x})\n\n\\* reformulation of the above in terms of adding an element to the set\nTHEOREM ProductSetAddElement ==\n ASSUME NEW S \\in SUBSET Int, IsFiniteSet(S), NEW x \\in Int \\ S\n PROVE ProductSet(S \\union {x}) = x * ProductSet(S)\n\n(*************************************************************************)\n(* Set product distributes over union, for disjoint sets of integers. *)\n(*************************************************************************)\nTHEOREM ProductSetDisjointUnion ==\n ASSUME NEW S \\in SUBSET Int, IsFiniteSet(S),\n NEW T \\in SUBSET Int, IsFiniteSet(T), S \\cap T = {}\n PROVE ProductSet(S \\union T) = ProductSet(S) * ProductSet(T)\n\n(*************************************************************************)\n(* The product of a set of natural numbers is one only if the set is *)\n(* empty or the singleton {1}. *)\n(*************************************************************************)\nTHEOREM ProductSetNatOne ==\n ASSUME NEW S \\in SUBSET Nat, IsFiniteSet(S)\n PROVE ProductSet(S) = 1 <=> S \\subseteq {1}\n\n(*************************************************************************)\n(* The product of a set of integers is zero only if the set contains 0. *)\n(*************************************************************************)\nTHEOREM ProductSetZero ==\n ASSUME NEW S \\in SUBSET Int, IsFiniteSet(S)\n PROVE ProductSet(S) = 0 <=> 0 \\in S \n\n(*************************************************************************)\n(* The product of a subset of a set of non-zero natural numbers is *)\n(* bounded by the product of the full set. *)\n(*************************************************************************)\nTHEOREM ProductSetNatSubset ==\n ASSUME NEW S \\in SUBSET (Nat \\ {0}), IsFiniteSet(S),\n NEW T \\in SUBSET S\n PROVE ProductSet(T) <= ProductSet(S)\n\n---------------------------------------------------------------------------\n\n(*************************************************************************)\n(* For an operator that maps set elements to natural numbers, resp. *)\n(* integers, MapThenSumSet yields a natural number, resp. an integer. *)\n(*************************************************************************)\nTHEOREM MapThenSumSetNat ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW op(_), \\A s \\in S : op(s) \\in Nat\n PROVE MapThenSumSet(op, S) \\in Nat\n\nTHEOREM MapThenSumSetInt ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW op(_), \\A s \\in S : op(s) \\in Int\n PROVE MapThenSumSet(op, S) \\in Int\n\n(*************************************************************************)\n(* The sum of mapping the empty set is 0. *)\n(*************************************************************************)\nTHEOREM MapThenSumSetEmpty == \n ASSUME NEW op(_)\n PROVE MapThenSumSet(op, {}) = 0\n\n(*************************************************************************)\n(* The sum of mapping a finite non-empty set is the sum of the image of *)\n(* an arbitrary element and the sum of mapping the remaining elements. *)\n(*************************************************************************)\nTHEOREM MapThenSumSetNonempty ==\n ASSUME NEW S, IsFiniteSet(S), NEW x \\in S,\n NEW op(_), \\A s \\in S : op(s) \\in Int\n PROVE MapThenSumSet(op, S) = op(x) + MapThenSumSet(op, S \\ {x})\n\n\\* reformulation of the above in terms of adding an element to the set\nTHEOREM MapThenSumSetAddElement ==\n ASSUME NEW S, IsFiniteSet(S), NEW x, x \\notin S,\n NEW op(_), \\A s \\in S \\union {x} : op(s) \\in Int\n PROVE MapThenSumSet(op, S \\union {x}) = op(x) + MapThenSumSet(op, S)\n\n(*************************************************************************)\n(* Mapping then summing distributes over union, for disjoint finite sets *)\n(* of integers. *)\n(*************************************************************************)\nTHEOREM MapThenSumSetDisjointUnion ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW T, IsFiniteSet(T), S \\cap T = {},\n NEW op(_), \\A x \\in S \\union T : op(x) \\in Int\n PROVE MapThenSumSet(op, S \\union T) = \n MapThenSumSet(op, S) + MapThenSumSet(op, T)\n\n(*************************************************************************)\n(* The map-sum of a subset of a finite set of natural numbers is bounded *)\n(* by the product of the full set. *)\n(*************************************************************************)\nTHEOREM MapThenSumSetNatSubset ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW T \\in SUBSET S,\n NEW op(_), \\A x \\in S : op(x) \\in Nat\n PROVE MapThenSumSet(op, T) <= MapThenSumSet(op, S)\n\n(*************************************************************************)\n(* The map-sum of a finite set of natural numbers is zero only if all *)\n(* set elements are mapped to zero. *)\n(*************************************************************************)\nTHEOREM MapThenSumSetZero ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW op(_), \\A s \\in S : op(s) \\in Nat \n PROVE MapThenSumSet(op, S) = 0 <=> \\A s \\in S : op(s) = 0\n\n(*************************************************************************)\n(* The map-sum of a finite set of natural numbers is monotonic in the *)\n(* function argument *)\n(*************************************************************************)\nTHEOREM MapThenSumSetMonotonic ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW f(_), \\A s \\in S : f(s) \\in Int,\n NEW g(_), \\A s \\in S : g(s) \\in Int,\n \\A s \\in S : f(s) <= g(s)\n PROVE MapThenSumSet(f, S) <= MapThenSumSet(g, S)\n\n(*************************************************************************)\n(* Moreover, if the second function returns a larger value for at least *)\n(* one element of the set, then the map-sum will be strictly larger. *)\n(*************************************************************************)\nTHEOREM MapThenSumSetStrictlyMonotonic ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW f(_), \\A s \\in S : f(s) \\in Int,\n NEW g(_), \\A s \\in S : g(s) \\in Int,\n \\A s \\in S : f(s) <= g(s),\n NEW s \\in S, f(s) < g(s)\n PROVE MapThenSumSet(f, S) < MapThenSumSet(g, S)\n\n---------------------------------------------------------------------------\n\n(*************************************************************************)\n(* Theorems about Min and Max of sets of integers. *)\n(*************************************************************************)\n\n(*************************************************************************)\n(* If a set of integers contains an upper bound, it is the maximum. *)\n(*************************************************************************)\nTHEOREM MaxInt ==\n ASSUME NEW S \\in SUBSET Int, NEW x \\in S, \\A y \\in S : x >= y\n PROVE Max(S) = x\n\n(*************************************************************************)\n(* Any finite non-empty set of integers has a maximum. *)\n(*************************************************************************)\nTHEOREM MaxIntFinite ==\n ASSUME NEW S \\in SUBSET Int, S # {}, IsFiniteSet(S)\n PROVE /\\ Max(S) \\in S\n /\\ \\A y \\in S : Max(S) >= y\n\n(*************************************************************************)\n(* Any non-empty set of integers that has an upper bound has a maximum. *)\n(*************************************************************************)\nTHEOREM MaxIntBounded ==\n ASSUME NEW S \\in SUBSET Int, S # {}, NEW x \\in Int, \\A y \\in S : x >= y\n PROVE /\\ Max(S) \\in S \n /\\ \\A y \\in S : Max(S) >= y\n\n(*************************************************************************)\n(* The maximum of a non-empty interval is its upper bound. *)\n(*************************************************************************)\nTHEOREM MaxInterval ==\n ASSUME NEW a \\in Int, NEW b \\in Int, a <= b \n PROVE Max(a..b) = b\n\n(*************************************************************************)\n(* If a set of integers contains an lower bound, it is the minimum. *)\n(*************************************************************************)\nTHEOREM MinInt ==\n ASSUME NEW S \\in SUBSET Int, NEW x \\in S, \\A y \\in S : x <= y\n PROVE Min(S) = x\n\n(*************************************************************************)\n(* Any finite non-empty set of integers has a minimum. *)\n(*************************************************************************)\nTHEOREM MinIntFinite ==\n ASSUME NEW S \\in SUBSET Int, S # {}, IsFiniteSet(S)\n PROVE /\\ Min(S) \\in S\n /\\ \\A y \\in S : Min(S) <= y\n\n(*************************************************************************)\n(* Any non-empty set of integers that has a lower bound has a minimum. *)\n(*************************************************************************)\nTHEOREM MinIntBounded ==\n ASSUME NEW S \\in SUBSET Int, S # {}, NEW x \\in Int, \\A y \\in S : x <= y\n PROVE /\\ Min(S) \\in S \n /\\ \\A y \\in S : Min(S) <= y\n\n(*************************************************************************)\n(* The minimum of a non-empty interval is its lower bound. *)\n(*************************************************************************)\nTHEOREM MinInterval ==\n ASSUME NEW a \\in Int, NEW b \\in Int, a <= b \n PROVE Min(a..b) = a\n\n(*************************************************************************)\n(* Any non-empty set of natural numbers has a minimum. *)\n(*************************************************************************)\nTHEOREM MinNat ==\n ASSUME NEW S \\in SUBSET Nat, S # {}\n PROVE /\\ Min(S) \\in S \n /\\ \\A y \\in S : Min(S) <= y\n\n---------------------------------------------------------------------------\n\n(*************************************************************************)\n(* Theorems about majorities. A \"majority\" of a finite set U is any *)\n(* subset whose cardinality is more than half that of U. This *)\n(* generalizes the notion of a quorum used in distributed-consensus *)\n(* specifications, where U is the set of servers. *)\n(*************************************************************************)\n\n(*************************************************************************)\n(* Any superset (within U) of a majority of U is itself a majority. *)\n(*************************************************************************)\nTHEOREM SupersetOfMajorityIsMajority ==\n ASSUME NEW U, IsFiniteSet(U),\n NEW Q1 \\in SUBSET U, NEW Q2 \\in SUBSET U, Q1 \\subseteq Q2,\n 2 * Cardinality(Q1) > Cardinality(U)\n PROVE 2 * Cardinality(Q2) > Cardinality(U)\n\n\n===========================================================================\n", "FiniteSetsExtTheorems_proofs": "----------------- MODULE FiniteSetsExtTheorems_proofs -----------------------\nEXTENDS FiniteSetsExt, Integers, FiniteSets, FiniteSetTheorems, FoldsTheorems, TLAPS\n\n(***************************************************************************)\n(* Theorems about FoldSet, derived from those about the underlying *)\n(* operator MapThenFoldSet. *)\n(***************************************************************************)\n\nTHEOREM FoldSetEmpty ==\n ASSUME NEW op(_,_), NEW base\n PROVE FoldSet(op, base, {}) = base\nBY MapThenFoldSetEmpty, Zenon DEF FoldSet\n\nTHEOREM FoldSetNonempty ==\n ASSUME NEW op(_,_), NEW base, NEW S, S # {}, IsFiniteSet(S)\n PROVE \\E x \\in S : FoldSet(op, base, S) = op(x, FoldSet(op, base, S \\ {x}))\n<1>1. \\A T : T # {} => (CHOOSE y \\in T : TRUE) \\in T\n OBVIOUS\n<1>. QED BY <1>1, MapThenFoldSetNonempty, Isa DEF FoldSet\n\nTHEOREM FoldSetType ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ,\n NEW S \\in SUBSET Typ, IsFiniteSet(S),\n \\A t,u \\in Typ : op(t,u) \\in Typ\n PROVE FoldSet(op, base, S) \\in Typ\n<1>1. /\\ \\A T : T # {} => (CHOOSE y \\in T : TRUE) \\in T\n /\\ \\A x \\in S : x \\in Typ\n OBVIOUS\n<1>. QED BY <1>1, MapThenFoldSetType, Isa DEF FoldSet\n\nTHEOREM FoldSetAC ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ, \n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n NEW S \\in SUBSET Typ, IsFiniteSet(S),\n NEW x \\in S\n PROVE FoldSet(op, base, S) = op(x, FoldSet(op, base, S \\ {x}))\n<1>. /\\ \\A T : T # {} => (CHOOSE t \\in T : TRUE) \\in T \n /\\ \\A s \\in S : s \\in Typ\n OBVIOUS\n<1>. QED BY MapThenFoldSetAC, Isa DEF FoldSet\n\nTHEOREM FoldSetACAddElement ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ, \n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n NEW S \\in SUBSET Typ, IsFiniteSet(S),\n NEW x \\in Typ \\ S\n PROVE FoldSet(op, base, S \\union {x}) = op(x, FoldSet(op, base, S))\n<1>. /\\ \\A T : T # {} => (CHOOSE t \\in T : TRUE) \\in T \n /\\ \\A s \\in S \\union {x} : s \\in Typ\n OBVIOUS\n<1>. QED BY MapThenFoldSetACAddElement, Isa DEF FoldSet\n\nTHEOREM FoldSetDisjointUnion ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ, \n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n \\A t \\in Typ : op(base, t) = t,\n NEW S \\in SUBSET Typ, IsFiniteSet(S),\n NEW T \\in SUBSET Typ, IsFiniteSet(T), S \\cap T = {}\n PROVE FoldSet(op, base, S \\union T) = \n op(FoldSet(op, base, S), FoldSet(op, base, T))\n<1>. /\\ \\A U : U # {} => (CHOOSE u \\in U : TRUE) \\in U \n /\\ \\A x \\in S \\union T : x \\in Typ\n OBVIOUS \n<1>. QED BY MapThenFoldSetDisjointUnion, Isa DEF FoldSet\n\n(***************************************************************************)\n(* Theorems about SumSet. *)\n(***************************************************************************)\n\\* we prove two type correctness theorems for Nat and for Int\nTHEOREM SumSetNat ==\n ASSUME NEW S \\in SUBSET Nat, IsFiniteSet(S)\n PROVE SumSet(S) \\in Nat\n<1>. /\\ 0 \\in Nat \n /\\ \\A i,j \\in Nat : i+j \\in Nat\n OBVIOUS\n<1>. QED BY FoldSetType, IsaM(\"iprover\") DEF SumSet \n\nTHEOREM SumSetInt ==\n ASSUME NEW S \\in SUBSET Int, IsFiniteSet(S)\n PROVE SumSet(S) \\in Int\n<1>. /\\ 0 \\in Int\n /\\ \\A i,j \\in Int : i+j \\in Int\n OBVIOUS\n<1>. QED BY FoldSetType, IsaM(\"iprover\") DEF SumSet \n\nTHEOREM SumSetEmpty == SumSet({}) = 0\nBY FoldSetEmpty, Zenon DEF SumSet\n\nTHEOREM SumSetNonempty ==\n ASSUME NEW S \\in SUBSET Int, IsFiniteSet(S), NEW x \\in S \n PROVE SumSet(S) = x + SumSet(S \\ {x})\n<1>. DEFINE op(i,j) == i + j\n<1>1. /\\ 0 \\in Int\n /\\ \\A i,j \\in Int : op(i,j) \\in Int\n /\\ \\A i,j \\in Int : op(i,j) = op(j,i)\n /\\ \\A i,j,k \\in Int : op(i, op(j,k)) = op(op(i,j), k)\n OBVIOUS\n<1>. HIDE DEF op \n<1>2. FoldSet(op, 0, S) = op(x, FoldSet(op, 0, S \\ {x}))\n BY <1>1, FoldSetAC, IsaM(\"iprover\")\n<1>. QED BY <1>2 DEF SumSet, op\n\nTHEOREM SumSetAddElement ==\n ASSUME NEW S \\in SUBSET Int, IsFiniteSet(S), NEW x \\in Int \\ S\n PROVE SumSet(S \\union {x}) = x + SumSet(S)\n<1>. DEFINE op(i,j) == i + j\n<1>1. /\\ 0 \\in Int\n /\\ \\A i,j \\in Int : op(i,j) \\in Int\n /\\ \\A i,j \\in Int : op(i,j) = op(j,i)\n /\\ \\A i,j,k \\in Int : op(i, op(j,k)) = op(op(i,j), k)\n OBVIOUS\n<1>. HIDE DEF op \n<1>2. FoldSet(op, 0, S \\union {x}) = op(x, FoldSet(op, 0, S))\n BY <1>1, FoldSetACAddElement, IsaM(\"iprover\")\n<1>. QED BY <1>2 DEF SumSet, op\n\nTHEOREM SumSetDisjointUnion ==\n ASSUME NEW S \\in SUBSET Int, IsFiniteSet(S),\n NEW T \\in SUBSET Int, IsFiniteSet(T), S \\cap T = {}\n PROVE SumSet(S \\union T) = SumSet(S) + SumSet(T)\n<1>. DEFINE op(i,j) == i + j\n<1>1. /\\ 0 \\in Int\n /\\ \\A i,j \\in Int : op(i,j) \\in Int\n /\\ \\A i,j \\in Int : op(i,j) = op(j,i)\n /\\ \\A i,j,k \\in Int : op(i, op(j,k)) = op(op(i,j), k)\n /\\ \\A i \\in Int : op(0, i) = i\n OBVIOUS\n<1>. HIDE DEF op \n<1>2. FoldSet(op, 0, S \\union T) = op(FoldSet(op, 0, S), FoldSet(op, 0, T))\n BY <1>1, FoldSetDisjointUnion, IsaM(\"iprover\")\n<1>. QED BY <1>2 DEF SumSet, op \n\nTHEOREM SumSetNatSubset ==\n ASSUME NEW S \\in SUBSET Nat, IsFiniteSet(S),\n NEW T \\in SUBSET S\n PROVE SumSet(T) <= SumSet(S)\n<1>. DEFINE U == S \\ T\n<1>1. /\\ IsFiniteSet(T)\n /\\ IsFiniteSet(U)\n BY FS_Subset, FS_Difference\n<1>2. SumSet(S) = SumSet(T \\union U)\n BY Zenon\n<1>3. @ = SumSet(T) + SumSet(U)\n BY <1>1, SumSetDisjointUnion\n<1>4. /\\ SumSet(T) \\in Nat \n /\\ SumSet(U) \\in Nat \n BY <1>1, SumSetNat \n<1>. QED BY <1>2, <1>3, <1>4\n\nTHEOREM SumSetNatZero ==\n ASSUME NEW S \\in SUBSET Nat, IsFiniteSet(S)\n PROVE SumSet(S) = 0 <=> S \\subseteq {0}\n<1>. DEFINE P(T) == SumSet(T) = 0 <=> T \\subseteq {0}\n<1>1. P({})\n BY SumSetEmpty\n<1>2. ASSUME NEW T \\in SUBSET S, IsFiniteSet(T), P(T), NEW x \\in S \\ T \n PROVE P(T \\union {x})\n <2>1. /\\ IsFiniteSet(T \\union {x})\n /\\ T \\union {x} \\in SUBSET Nat\n /\\ x \\in T \\union {x}\n /\\ (T \\union {x}) \\ {x} = T\n BY <1>2, FS_AddElement\n <2>2. SumSet(T \\union {x}) = x + SumSet(T)\n BY ONLY <2>1, SumSetNonempty\n <2>3. SumSet(T) \\in Nat \n BY <1>2, SumSetNat\n <2>4. SumSet(T \\union {x}) = 0 <=> x = 0 /\\ SumSet(T) = 0\n BY <2>2, <2>3\n <2>. QED BY <1>2, <2>4\n<1>. QED\n <2>. HIDE DEF P \n <2>. P(S) BY <1>1, <1>2, FS_Induction, IsaM(\"iprover\")\n <2>. QED BY DEF P\n\n(***************************************************************************)\n(* Theorems about ProductSet. *)\n(***************************************************************************)\nTHEOREM ProductSetNat ==\n ASSUME NEW S \\in SUBSET Nat, IsFiniteSet(S)\n PROVE ProductSet(S) \\in Nat\n<1>. /\\ 1 \\in Nat \n /\\ \\A i,j \\in Nat : i*j \\in Nat\n OBVIOUS\n<1>. QED BY FoldSetType, IsaM(\"iprover\") DEF ProductSet \n\nTHEOREM ProductSetInt ==\n ASSUME NEW S \\in SUBSET Int, IsFiniteSet(S)\n PROVE ProductSet(S) \\in Int\n<1>. /\\ 1 \\in Int\n /\\ \\A i,j \\in Int : i*j \\in Int\n OBVIOUS\n<1>. QED BY FoldSetType, IsaM(\"iprover\") DEF ProductSet \n\nTHEOREM ProductSetEmpty == ProductSet({}) = 1\nBY FoldSetEmpty, Zenon DEF ProductSet\n\nTHEOREM ProductSetNonempty ==\n ASSUME NEW S \\in SUBSET Int, IsFiniteSet(S), NEW x \\in S \n PROVE ProductSet(S) = x * ProductSet(S \\ {x})\n<1>. DEFINE op(i,j) == i * j\n<1>1. /\\ 1 \\in Int\n /\\ \\A i,j \\in Int : op(i,j) \\in Int\n /\\ \\A i,j \\in Int : op(i,j) = op(j,i)\n /\\ \\A i,j,k \\in Int : op(i, op(j,k)) = op(op(i,j), k)\n OBVIOUS\n<1>. HIDE DEF op \n<1>2. FoldSet(op, 1, S) = op(x, FoldSet(op, 1, S \\ {x}))\n BY <1>1, FoldSetAC, IsaM(\"iprover\")\n<1>. QED BY <1>2 DEF ProductSet, op\n\nTHEOREM ProductSetAddElement ==\n ASSUME NEW S \\in SUBSET Int, IsFiniteSet(S), NEW x \\in Int \\ S\n PROVE ProductSet(S \\union {x}) = x * ProductSet(S)\n<1>. DEFINE op(i,j) == i * j\n<1>1. /\\ 1 \\in Int\n /\\ \\A i,j \\in Int : op(i,j) \\in Int\n /\\ \\A i,j \\in Int : op(i,j) = op(j,i)\n /\\ \\A i,j,k \\in Int : op(i, op(j,k)) = op(op(i,j), k)\n OBVIOUS\n<1>. HIDE DEF op \n<1>2. FoldSet(op, 1, S \\union {x}) = op(x, FoldSet(op, 1, S))\n BY <1>1, FoldSetACAddElement, IsaM(\"iprover\")\n<1>. QED BY <1>2 DEF ProductSet, op\n\nTHEOREM ProductSetDisjointUnion ==\n ASSUME NEW S \\in SUBSET Int, IsFiniteSet(S),\n NEW T \\in SUBSET Int, IsFiniteSet(T), S \\cap T = {}\n PROVE ProductSet(S \\union T) = ProductSet(S) * ProductSet(T)\n<1>. DEFINE op(i,j) == i * j\n<1>1. /\\ 1 \\in Int\n /\\ \\A i,j \\in Int : op(i,j) \\in Int\n /\\ \\A i,j \\in Int : op(i,j) = op(j,i)\n /\\ \\A i,j,k \\in Int : op(i, op(j,k)) = op(op(i,j), k)\n /\\ \\A i \\in Int : op(1, i) = i\n OBVIOUS\n<1>. HIDE DEF op \n<1>2. FoldSet(op, 1, S \\union T) = op(FoldSet(op, 1, S), FoldSet(op, 1, T))\n BY <1>1, FoldSetDisjointUnion, IsaM(\"iprover\")\n<1>. QED BY <1>2 DEF ProductSet, op \n\nTHEOREM ProductSetNatOne ==\n ASSUME NEW S \\in SUBSET Nat, IsFiniteSet(S)\n PROVE ProductSet(S) = 1 <=> S \\subseteq {1}\n<1>. DEFINE P(T) == ProductSet(T) = 1 <=> T \\subseteq {1}\n<1>1. P({})\n BY ProductSetEmpty\n<1>2. ASSUME NEW T \\in SUBSET S, IsFiniteSet(T), P(T), NEW x \\in S \\ T \n PROVE P(T \\union {x})\n <2>1. /\\ IsFiniteSet(T \\union {x})\n /\\ T \\union {x} \\in SUBSET Nat\n /\\ x \\in T \\union {x}\n /\\ (T \\union {x}) \\ {x} = T\n BY <1>2, FS_AddElement\n <2>2. ProductSet(T \\union {x}) = x * ProductSet(T)\n BY ONLY <2>1, ProductSetNonempty\n <2>3. ProductSet(T) \\in Nat \n BY <1>2, ProductSetNat\n <2>4. ProductSet(T \\union {x}) = 1 <=> x = 1 /\\ ProductSet(T) = 1\n BY <2>2, <2>3\n <2>. QED BY <1>2, <2>4\n<1>. QED\n <2>. HIDE DEF P \n <2>. P(S) BY <1>1, <1>2, FS_Induction, IsaM(\"iprover\")\n <2>. QED BY DEF P\n\nTHEOREM ProductSetZero ==\n ASSUME NEW S \\in SUBSET Int, IsFiniteSet(S)\n PROVE ProductSet(S) = 0 <=> 0 \\in S \n<1>. DEFINE P(T) == ProductSet(T) = 0 <=> 0 \\in T \n<1>1. P({})\n BY ProductSetEmpty\n<1>2. ASSUME NEW T \\in SUBSET S, IsFiniteSet(T), P(T), NEW x \\in S \\ T \n PROVE P(T \\union {x})\n <2>1. /\\ IsFiniteSet(T \\union {x})\n /\\ T \\union {x} \\in SUBSET Int\n /\\ x \\in T \\union {x}\n /\\ (T \\union {x}) \\ {x} = T\n BY <1>2, FS_AddElement\n <2>2. ProductSet(T \\union {x}) = x * ProductSet(T)\n BY ONLY <2>1, ProductSetNonempty\n <2>3. ProductSet(T) \\in Int\n BY <1>2, ProductSetInt\n <2>4. ProductSet(T \\union {x}) = 0 <=> x = 0 \\/ ProductSet(T) = 0\n BY <2>2, <2>3\n <2>. QED BY <1>2, <2>4\n<1>. QED\n <2>. HIDE DEF P \n <2>. P(S) BY <1>1, <1>2, FS_Induction, IsaM(\"iprover\")\n <2>. QED BY DEF P\n\nTHEOREM ProductSetNatSubset ==\n ASSUME NEW S \\in SUBSET (Nat \\ {0}), IsFiniteSet(S),\n NEW T \\in SUBSET S\n PROVE ProductSet(T) <= ProductSet(S)\n<1>. DEFINE U == S \\ T\n<1>1. /\\ IsFiniteSet(T)\n /\\ IsFiniteSet(U)\n BY FS_Subset, FS_Difference\n<1>2. ProductSet(S) = ProductSet(T \\union U)\n BY Zenon\n<1>3. @ = ProductSet(T) * ProductSet(U)\n BY <1>1, ProductSetDisjointUnion\n<1>4. /\\ ProductSet(T) \\in Nat \n /\\ ProductSet(U) \\in Nat \n BY <1>1, ProductSetNat \n<1>5. /\\ ProductSet(T) # 0\n /\\ ProductSet(U) # 0 \n BY <1>1, ProductSetZero\n<1>. QED BY <1>2, <1>3, <1>4, <1>5\n\n\n(***************************************************************************)\n(* Theorems about MapThenSumSet. *)\n(***************************************************************************)\nTHEOREM MapThenSumSetNat ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW op(_), \\A s \\in S : op(s) \\in Nat\n PROVE MapThenSumSet(op, S) \\in Nat\n<1>. \\A T \\in SUBSET S : T # {} => (CHOOSE x \\in T : TRUE) \\in T \n OBVIOUS\n<1>. /\\ 0 \\in Nat\n /\\ \\A i,j \\in Nat : i+j \\in Nat \n OBVIOUS\n<1>. QED BY MapThenFoldSetType, IsaM(\"iprover\") DEF MapThenSumSet\n\nTHEOREM MapThenSumSetInt ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW op(_), \\A s \\in S : op(s) \\in Int\n PROVE MapThenSumSet(op, S) \\in Int\n<1>. \\A T \\in SUBSET S : T # {} => (CHOOSE x \\in T : TRUE) \\in T \n OBVIOUS\n<1>. /\\ 0 \\in Int\n /\\ \\A i,j \\in Int : i+j \\in Int\n OBVIOUS\n<1>. QED BY MapThenFoldSetType, IsaM(\"iprover\") DEF MapThenSumSet\n\nTHEOREM MapThenSumSetEmpty == \n ASSUME NEW op(_)\n PROVE MapThenSumSet(op, {}) = 0\nBY MapThenFoldSetEmpty, Zenon DEF MapThenSumSet \n\nTHEOREM MapThenSumSetNonempty ==\n ASSUME NEW S, IsFiniteSet(S), NEW x \\in S,\n NEW op(_), \\A s \\in S : op(s) \\in Int\n PROVE MapThenSumSet(op, S) = op(x) + MapThenSumSet(op, S \\ {x})\n<1>. \\A T \\in SUBSET S : T # {} => (CHOOSE t \\in T : TRUE) \\in T \n OBVIOUS\n<1>. /\\ 0 \\in Int \n /\\ \\A i,j \\in Int : i+j \\in Int\n /\\ \\A i,j \\in Int : i+j = j+i \n /\\ \\A i,j,k \\in Int : i+(j+k) = (i+j)+k\n OBVIOUS\n<1>. QED BY MapThenFoldSetAC, IsaM(\"iprover\") DEF MapThenSumSet \n\nTHEOREM MapThenSumSetAddElement ==\n ASSUME NEW S, IsFiniteSet(S), NEW x, x \\notin S,\n NEW op(_), \\A s \\in S \\union {x} : op(s) \\in Int\n PROVE MapThenSumSet(op, S \\union {x}) = op(x) + MapThenSumSet(op, S)\n<1>. \\A T \\in SUBSET (S \\union {x}) : T # {} => (CHOOSE t \\in T : TRUE) \\in T \n OBVIOUS\n<1>. /\\ 0 \\in Int \n /\\ \\A i,j \\in Int : i+j \\in Int\n /\\ \\A i,j \\in Int : i+j = j+i \n /\\ \\A i,j,k \\in Int : i+(j+k) = (i+j)+k\n OBVIOUS\n<1>. QED BY MapThenFoldSetACAddElement, IsaM(\"iprover\") DEF MapThenSumSet \n\nTHEOREM MapThenSumSetDisjointUnion ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW T, IsFiniteSet(T), S \\cap T = {},\n NEW op(_), \\A x \\in S \\union T : op(x) \\in Int\n PROVE MapThenSumSet(op, S \\union T) = \n MapThenSumSet(op, S) + MapThenSumSet(op, T)\n<1>1. \\A U \\in SUBSET (S \\union T) : U # {} => (CHOOSE u \\in U : TRUE) \\in U \n OBVIOUS\n<1>2. /\\ 0 \\in Int \n /\\ \\A i,j \\in Int : i+j \\in Int\n /\\ \\A i,j \\in Int : i+j = j+i\n /\\ \\A i,j,k \\in Int : i+(j+k) = (i+j)+k\n /\\ \\A i \\in Int : 0+i = i\n OBVIOUS\n<1>. QED BY <1>1, <1>2, MapThenFoldSetDisjointUnion, IsaM(\"iprover\") DEF MapThenSumSet \n\nTHEOREM MapThenSumSetNatSubset ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW T \\in SUBSET S,\n NEW op(_), \\A x \\in S : op(x) \\in Nat\n PROVE MapThenSumSet(op, T) <= MapThenSumSet(op, S)\n<1>. DEFINE U == S \\ T\n<1>1. /\\ IsFiniteSet(T)\n /\\ IsFiniteSet(U)\n /\\ T \\cap U = {}\n /\\ \\A x \\in T \\union U : op(x) \\in Int\n BY FS_Subset, FS_Difference\n<1>2. MapThenSumSet(op, S) = MapThenSumSet(op, T \\union U)\n BY Zenon\n<1>3. @ = MapThenSumSet(op, T) + MapThenSumSet(op, U)\n BY <1>1, MapThenSumSetDisjointUnion, IsaM(\"iprover\")\n<1>4. /\\ \\A x \\in T : op(x) \\in Nat \n /\\ \\A x \\in U : op(x) \\in Nat \n OBVIOUS\n<1>5. /\\ MapThenSumSet(op, T) \\in Nat \n /\\ MapThenSumSet(op, U) \\in Nat \n BY <1>1, <1>4, MapThenSumSetNat, IsaM(\"iprover\")\n<1>. QED BY <1>2, <1>3, <1>5\n\nTHEOREM MapThenSumSetZero ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW op(_), \\A s \\in S : op(s) \\in Nat \n PROVE MapThenSumSet(op, S) = 0 <=> \\A s \\in S : op(s) = 0\n<1>. DEFINE P(T) == MapThenSumSet(op, T) = 0 <=> \\A s \\in T : op(s) = 0\n<1>1. P({})\n BY MapThenSumSetEmpty, Zenon\n<1>2. ASSUME NEW T \\in SUBSET S, IsFiniteSet(T), P(T), NEW x \\in S \\ T \n PROVE P(T \\union {x})\n <2>1. /\\ IsFiniteSet(T \\union {x})\n /\\ \\A s \\in T \\union {x} : op(s) \\in Int\n BY <1>2, FS_AddElement\n <2>2. MapThenSumSet(op, T \\union {x}) = op(x) + MapThenSumSet(op, (T \\union {x}) \\ {x})\n BY ONLY <2>1, MapThenSumSetNonempty, IsaM(\"blast\")\n <2>3. @ = op(x) + MapThenSumSet(op, T)\n BY Zenon\n <2>4. MapThenSumSet(op, T) \\in Nat \n BY <1>2, \\A s \\in T : op(s) \\in Nat, MapThenSumSetNat, IsaM(\"iprover\")\n <2>5. MapThenSumSet(op, T \\union {x}) = 0 <=> op(x) = 0 /\\ MapThenSumSet(op, T) = 0\n BY <2>2, <2>3, <2>4\n <2>. QED BY <1>2, <2>5\n<1>. QED \n <2>. HIDE DEF P \n <2>. P(S) BY <1>1, <1>2, FS_Induction, IsaM(\"iprover\")\n <2>. QED BY DEF P\n\nTHEOREM MapThenSumSetMonotonic ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW f(_), \\A s \\in S : f(s) \\in Int,\n NEW g(_), \\A s \\in S : g(s) \\in Int,\n \\A s \\in S : f(s) <= g(s)\n PROVE MapThenSumSet(f, S) <= MapThenSumSet(g, S)\n<1>. DEFINE P(T) == MapThenSumSet(f, T) <= MapThenSumSet(g, T)\n<1>1. P({})\n BY MapThenSumSetEmpty, Isa\n<1>2. ASSUME NEW T \\in SUBSET S, IsFiniteSet(T), P(T), NEW x \\in S \\ T \n PROVE P(T \\union {x})\n <2>1. /\\ IsFiniteSet(T \\union {x})\n /\\ \\A s \\in T \\union {x} : f(s) \\in Int\n /\\ \\A s \\in T \\union {x} : g(s) \\in Int\n\\* /\\ \\A s \\in T \\union {x} : f(s) <= g(s)\n BY <1>2, FS_AddElement\n <2>2. MapThenSumSet(f, T \\union {x}) = f(x) + MapThenSumSet(f, (T \\union {x}) \\ {x})\n BY ONLY <2>1, MapThenSumSetNonempty, IsaM(\"blast\")\n <2>3. @ = f(x) + MapThenSumSet(f, T)\n BY Zenon\n <2>4. MapThenSumSet(f, T) \\in Int\n BY <1>2, \\A s \\in T : f(s) \\in Int, MapThenSumSetInt, IsaM(\"iprover\")\n <2>5. MapThenSumSet(g, T \\union {x}) = g(x) + MapThenSumSet(g, (T \\union {x}) \\ {x})\n BY ONLY <2>1, MapThenSumSetNonempty, IsaM(\"blast\")\n <2>6. @ = g(x) + MapThenSumSet(g, T)\n BY Zenon\n <2>7. MapThenSumSet(g, T) \\in Int\n BY <1>2, \\A s \\in T : g(s) \\in Int, MapThenSumSetInt, IsaM(\"iprover\")\n <2>. QED BY <1>2, <2>2, <2>3, <2>4, <2>5, <2>6, <2>7\n<1>. QED \n <2>. HIDE DEF P \n <2>. P(S) BY <1>1, <1>2, FS_Induction, IsaM(\"iprover\")\n <2>. QED BY DEF P\n\nTHEOREM MapThenSumSetStrictlyMonotonic ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW f(_), \\A s \\in S : f(s) \\in Int,\n NEW g(_), \\A s \\in S : g(s) \\in Int,\n \\A s \\in S : f(s) <= g(s),\n NEW s \\in S, f(s) < g(s)\n PROVE MapThenSumSet(f, S) < MapThenSumSet(g, S)\n<1>1. MapThenSumSet(f,S) = f(s) + MapThenSumSet(f, S \\ {s})\n BY MapThenSumSetNonempty, Isa\n<1>2. MapThenSumSet(g,S) = g(s) + MapThenSumSet(g, S \\ {s})\n BY MapThenSumSetNonempty, Isa\n<1>3. /\\ IsFiniteSet(S \\ {s})\n /\\ \\A x \\in S \\ {s} : f(x) \\in Int \n /\\ \\A x \\in S \\ {s} : g(x) \\in Int \n /\\ \\A x \\in S \\ {s} : f(x) <= g(x)\n BY FS_RemoveElement\n<1>4. MapThenSumSet(f, S \\ {s}) <= MapThenSumSet(g, S \\ {s})\n BY <1>3, MapThenSumSetMonotonic, IsaM(\"blast\")\n<1>5. /\\ MapThenSumSet(f, S \\ {s}) \\in Int \n /\\ MapThenSumSet(g, S \\ {s}) \\in Int \n BY <1>3, MapThenSumSetInt, Isa\n<1>. QED BY <1>1, <1>2, <1>4, <1>5\n\n(***************************************************************************)\n(* Theorems about Max and Min. *)\n(***************************************************************************)\n\nTHEOREM MaxInt ==\n ASSUME NEW S \\in SUBSET Int, NEW x \\in S, \\A y \\in S : x >= y\n PROVE Max(S) = x\nBY DEF Max\n\nTHEOREM MaxIntFinite ==\n ASSUME NEW S \\in SUBSET Int, S # {}, IsFiniteSet(S)\n PROVE /\\ Max(S) \\in S\n /\\ \\A y \\in S : Max(S) >= y\n<1>. DEFINE P(T) == T # {} => /\\ Max(T) \\in T \n /\\ \\A y \\in T : Max(T) >= y\n<1>1. P({})\n OBVIOUS\n<1>2. ASSUME NEW T \\in SUBSET S, P(T), NEW a \\in S \\ T \n PROVE P(T \\union {a})\n <2>1. CASE \\A y \\in T : a >= y\n BY <1>1, <2>1 DEF Max \n <2>2. CASE \\E y \\in T : ~(a >= y)\n <3>. DEFINE m == Max(T)\n <3>1. /\\ m \\in T \\union {a}\n /\\ \\A y \\in T \\union {a} : m >= y\n BY <1>2, <2>2\n <3>. QED BY <3>1, Zenon DEF Max\n <2>. QED BY <2>1, <2>2\n<1>3. P(S)\n <2>. HIDE DEF P \n <2>. QED BY <1>1, <1>2, FS_Induction, IsaM(\"blast\")\n<1>. QED BY <1>3\n\nTHEOREM MaxIntBounded ==\n ASSUME NEW S \\in SUBSET Int, S # {}, NEW x \\in Int, \\A y \\in S : x >= y\n PROVE /\\ Max(S) \\in S \n /\\ \\A y \\in S : Max(S) >= y\n<1>1. PICK s \\in S : TRUE\n OBVIOUS\n<1>2. IsFiniteSet(s .. x)\n BY FS_Interval\n<1>. DEFINE T == (s .. x) \\cap S\n<1>3. /\\ T \\in SUBSET Int \n /\\ T # {}\n /\\ IsFiniteSet(T)\n BY <1>2, FS_Subset\n<1>4. /\\ Max(T) \\in T \n /\\ \\A y \\in T : Max(T) >= y\n BY <1>3, MaxIntFinite \n<1>5. /\\ Max(T) \\in S\n /\\ \\A y \\in S : Max(T) >= y\n BY <1>4\n<1>. QED BY <1>5, Zenon DEF Max \n\nTHEOREM MaxInterval ==\n ASSUME NEW a \\in Int, NEW b \\in Int, a <= b \n PROVE Max(a..b) = b\n<1>1. b \\in a..b\n OBVIOUS\n<1>2. \\A y \\in a..b : b >= y\n OBVIOUS\n<1>. QED BY <1>1, <1>2, MaxInt\n\nTHEOREM MinInt ==\n ASSUME NEW S \\in SUBSET Int, NEW x \\in S, \\A y \\in S : x <= y\n PROVE Min(S) = x\nBY DEF Min\n\nTHEOREM MinIntFinite ==\n ASSUME NEW S \\in SUBSET Int, S # {}, IsFiniteSet(S)\n PROVE /\\ Min(S) \\in S\n /\\ \\A y \\in S : Min(S) <= y\n<1>. DEFINE P(T) == T # {} => /\\ Min(T) \\in T \n /\\ \\A y \\in T : Min(T) <= y\n<1>1. P({})\n OBVIOUS\n<1>2. ASSUME NEW T \\in SUBSET S, P(T), NEW a \\in S \\ T \n PROVE P(T \\union {a})\n <2>1. CASE \\A y \\in T : a <= y\n BY <1>1, <2>1 DEF Min \n <2>2. CASE \\E y \\in T : ~(a <= y)\n <3>. DEFINE m == Min(T)\n <3>1. /\\ m \\in T \\union {a}\n /\\ \\A y \\in T \\union {a} : m <= y\n BY <1>2, <2>2\n <3>. QED BY <3>1, Zenon DEF Min\n <2>. QED BY <2>1, <2>2\n<1>3. P(S)\n <2>. HIDE DEF P \n <2>. QED BY <1>1, <1>2, FS_Induction, IsaM(\"blast\")\n<1>. QED BY <1>3\n\nTHEOREM MinIntBounded ==\n ASSUME NEW S \\in SUBSET Int, S # {}, NEW x \\in Int, \\A y \\in S : x <= y\n PROVE /\\ Min(S) \\in S \n /\\ \\A y \\in S : Min(S) <= y\n<1>1. PICK s \\in S : TRUE\n OBVIOUS\n<1>2. IsFiniteSet(x .. s)\n BY FS_Interval\n<1>. DEFINE T == (x .. s) \\cap S\n<1>3. /\\ T \\in SUBSET Int \n /\\ T # {}\n /\\ IsFiniteSet(T)\n BY <1>2, FS_Subset\n<1>4. /\\ Min(T) \\in T \n /\\ \\A y \\in T : Min(T) <= y\n BY <1>3, MinIntFinite \n<1>5. /\\ Min(T) \\in S\n /\\ \\A y \\in S : Min(T) <= y\n BY <1>4\n<1>. QED BY <1>5, Zenon DEF Min\n\nTHEOREM MinInterval ==\n ASSUME NEW a \\in Int, NEW b \\in Int, a <= b \n PROVE Min(a..b) = a\n<1>1. a \\in a..b\n OBVIOUS\n<1>2. \\A y \\in a..b : a <= y\n OBVIOUS\n<1>. QED BY <1>1, <1>2, MinInt\n\nTHEOREM MinNat ==\n ASSUME NEW S \\in SUBSET Nat, S # {}\n PROVE /\\ Min(S) \\in S \n /\\ \\A y \\in S : Min(S) <= y\nBY MinIntBounded, \\A y \\in S : 0 <= y\n\n---------------------------------------------------------------------------\n\n(*************************************************************************)\n(* Any superset (within U) of a majority of U is itself a majority. *)\n(*************************************************************************)\nTHEOREM SupersetOfMajorityIsMajority ==\n ASSUME NEW U, IsFiniteSet(U),\n NEW Q1 \\in SUBSET U, NEW Q2 \\in SUBSET U, Q1 \\subseteq Q2,\n 2 * Cardinality(Q1) > Cardinality(U)\n PROVE 2 * Cardinality(Q2) > Cardinality(U)\n<1>1. IsFiniteSet(Q2) /\\ Cardinality(Q2) <= Cardinality(U) BY FS_Subset\n<1>2. IsFiniteSet(Q1) /\\ Cardinality(Q1) <= Cardinality(Q2) BY <1>1, FS_Subset\n<1>3. Cardinality(Q1) \\in Nat /\\ Cardinality(Q2) \\in Nat /\\ Cardinality(U) \\in Nat\n BY FS_CardinalityType, <1>1, <1>2\n<1>. QED BY <1>2, <1>3\n\n================================================================================\n", "Folds": "------------------------------- MODULE Folds -------------------------------\n(***************************************************************************)\n(* This module provides a general framework for folding operations over *)\n(* sets with flexible element selection and transformation. *)\n(* *)\n(* The main operator MapThenFoldSet is the foundation for many other *)\n(* folding operations found in: *)\n(* - FiniteSetsExt.FoldSet (simplified for sets) *)\n(* - Functions.FoldFunction (specialized for functions) *)\n(* - SequencesExt.FoldSeq (specialized for sequences) *)\n(* *)\n(***************************************************************************)\n\nMapThenFoldSet(op(_,_), base, f(_), choose(_), S) ==\n(******************************************************************************)\n(* Starting from base, apply op to f(x), for all x \\in S, by choosing the set *)\n(* elements with `choose`. If there are multiple ways for choosing an element,*)\n(* op should be associative and commutative. Otherwise, the result may depend *)\n(* on the concrete implementation of `choose`. *)\n(* *)\n(* FoldSet, a simpler version for sets is contained in FiniteSetsEx. *)\n(* FoldFunction, a simpler version for functions is contained in Functions. *)\n(* FoldSequence, a simpler version for sequences is contained in SequencesExt.*)\n(* *)\n(* Example: *)\n(* *)\n(* MapThenFoldSet(LAMBDA x,y: x \\cup y, *)\n(* {}, *)\n(* LAMBDA x: {{x}}, *)\n(* LAMBDA set: CHOOSE x \\in set: TRUE, *)\n(* {1,2}) *)\n(* = {{1},{2}} *)\n(******************************************************************************)\n LET iter[s \\in SUBSET S] ==\n IF s = {} THEN base\n ELSE LET x == choose(s)\n IN op(f(x), iter[s \\ {x}])\n IN iter[S]\n\n\n=============================================================================\n", "FoldsTheorems": "--------------------------- MODULE FoldsTheorems --------------------------\n(*************************************************************************)\n(* Theorems about the basic folding operator. *)\n(* This module only lists theorem statements for reference. The proofs *)\n(* can be found in module FoldsTheorems_proofs.tla. *)\n(*************************************************************************)\nEXTENDS Folds, FiniteSets\n\n(*************************************************************************)\n(* MapThenFoldSet is well-defined, i.e., it conforms to the equation *)\n(* used for defining the operator. *)\n(*************************************************************************)\nTHEOREM MapThenFoldSetDef ==\n ASSUME NEW op(_,_), NEW base, NEW f(_), NEW choose(_), NEW S,\n IsFiniteSet(S),\n \\A T \\in SUBSET S : T # {} => choose(T) \\in T\n PROVE MapThenFoldSet(op, base, f, choose, S) =\n IF S = {} THEN base\n ELSE LET x == choose(S)\n IN op(f(x), MapThenFoldSet(op, base, f, choose, S \\ {x}))\n\n(*************************************************************************)\n(* The following two theorems correspond to the two cases in the *)\n(* equation in theorem MapThenFoldSetDef, but are easier to use. *)\n(*************************************************************************)\nTHEOREM MapThenFoldSetEmpty ==\n ASSUME NEW op(_,_), NEW base, NEW f(_), NEW choose(_)\n PROVE MapThenFoldSet(op, base, f, choose, {}) = base\n\n\\* fold of a non-empty set\nTHEOREM MapThenFoldSetNonempty ==\n ASSUME NEW op(_,_), NEW base, NEW f(_), NEW choose(_), NEW S, S # {},\n IsFiniteSet(S),\n \\A T \\in SUBSET S : T # {} => choose(T) \\in T\n PROVE MapThenFoldSet(op, base, f, choose, S) = \n LET x == choose(S)\n IN op(f(x), MapThenFoldSet(op, base, f, choose, S \\ {x}))\n\n(*************************************************************************)\n(* Folding over two finite sets S and T that are related by a bijection *)\n(* that is compatible with the choice and mapping functions yields the *)\n(* same result. *)\n(*************************************************************************)\nTHEOREM MapThenFoldSetBijection ==\n ASSUME NEW op(_,_), NEW base,\n NEW S, IsFiniteSet(S), NEW fS(_), NEW chS(_),\n \\A U \\in SUBSET S : U # {} => chS(U) \\in U,\n NEW T, IsFiniteSet(T), NEW fT(_), NEW chT(_),\n \\A U \\in SUBSET T : U # {} => chT(U) \\in U,\n NEW bij(_),\n \\A s \\in S : bij(s) \\in T,\n \\A t \\in T : \\E s \\in S : bij(s) = t,\n \\A s1, s2 \\in S : bij(s1) = bij(s2) => s1 = s2,\n \\A s \\in S : fS(s) = fT(bij(s)),\n \\A U \\in SUBSET S : U # {} => bij(chS(U)) = chT({bij(s) : s \\in U})\n PROVE MapThenFoldSet(op, base, fS, chS, S) = MapThenFoldSet(op, base, fT, chT, T)\n\n(*************************************************************************)\n(* In particular, folding over the same set, using two mapping functions *)\n(* that agree over the elements of the set, yields the same result. *)\n(*************************************************************************)\nTHEOREM MapThenFoldSetEqual ==\n ASSUME NEW op(_,_), NEW base, NEW choose(_), NEW S, IsFiniteSet(S),\n \\A T \\in SUBSET S : T # {} => choose(T) \\in T,\n NEW f(_), NEW g(_), \n \\A s \\in S : f(s) = g(s)\n PROVE MapThenFoldSet(op, base, f, choose, S) = \n MapThenFoldSet(op, base, g, choose, S)\n\n(*************************************************************************)\n(* The type of a fold corresponds to the type associated with the binary *)\n(* operator that underlies the fold. *)\n(*************************************************************************)\nTHEOREM MapThenFoldSetType ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW Typ, NEW op(_,_), NEW base \\in Typ, NEW f(_), NEW choose(_),\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A s \\in S : f(s) \\in Typ,\n \\A T \\in SUBSET S : T # {} => choose(T) \\in T\n PROVE MapThenFoldSet(op, base, f, choose, S) \\in Typ\n\n(*************************************************************************)\n(* In many applications the operator used for folding is associative and *)\n(* commutative. In that case, the following theorem may be more useful *)\n(* for reasoning about folding a non-empty set. *)\n(*************************************************************************)\nTHEOREM MapThenFoldSetAC ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ, NEW f(_),\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n NEW S, IsFiniteSet(S), NEW x \\in S,\n NEW choose(_), \\A T \\in SUBSET S : T # {} => choose(T) \\in T,\n \\A s \\in S : f(s) \\in Typ\n PROVE MapThenFoldSet(op, base, f, choose, S) = \n op(f(x), MapThenFoldSet(op, base, f, choose, S \\ {x}))\n\n\\* reformulation for adding an element to the set\nTHEOREM MapThenFoldSetACAddElement ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ, NEW f(_),\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n NEW S, IsFiniteSet(S), NEW x, x \\notin S,\n NEW choose(_), \\A T \\in SUBSET (S \\union {x}) : T # {} => choose(T) \\in T,\n \\A s \\in S \\union {x} : f(s) \\in Typ\n PROVE MapThenFoldSet(op, base, f, choose, S \\union {x}) = \n op(f(x), MapThenFoldSet(op, base, f, choose, S))\n\n(*************************************************************************)\n(* Moreover, for an AC operator with base as the neutral element, *)\n(* folding commutes with set union, for disjoint sets. *)\n(*************************************************************************)\nTHEOREM MapThenFoldSetDisjointUnion ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ, NEW f(_),\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n \\A t \\in Typ : op(base, t) = t,\n NEW S, IsFiniteSet(S),\n NEW T, IsFiniteSet(T), S \\cap T = {},\n NEW choose(_), \n \\A U \\in SUBSET (S \\union T) : U # {} => choose(U) \\in U,\n \\A x \\in S \\union T : f(x) \\in Typ\n PROVE MapThenFoldSet(op, base, f, choose, S \\union T) = \n op(MapThenFoldSet(op, base, f, choose, S),\n MapThenFoldSet(op, base, f, choose, T))\n\n===========================================================================\n", "FoldsTheorems_proofs": "------------------------ MODULE FoldsTheorems_proofs ----------------------\n(*************************************************************************)\n(* Theorems about the basic folding operator. *)\n(*************************************************************************)\nEXTENDS Folds, FiniteSets, TLAPS\nLOCAL INSTANCE FiniteSetTheorems\nLOCAL INSTANCE FunctionTheorems\nLOCAL INSTANCE WellFoundedInduction\n\n\\* MapThenFoldSet is well-defined\nTHEOREM MapThenFoldSetDef ==\n ASSUME NEW op(_,_), NEW base, NEW f(_), NEW choose(_), NEW S,\n IsFiniteSet(S),\n \\A T \\in SUBSET S : T # {} => choose(T) \\in T\n PROVE MapThenFoldSet(op, base, f, choose, S) =\n IF S = {} THEN base\n ELSE LET x == choose(S)\n IN op(f(x), MapThenFoldSet(op, base, f, choose, S \\ {x}))\n<1>. DEFINE FoldDef(g, s) ==\n IF s = {} THEN base\n ELSE LET x == choose(s) IN op(f(x), g[s \\ {x}])\n iter(T) == CHOOSE g : g = [s \\in SUBSET T |-> FoldDef(g, s)]\n<1>1. ASSUME NEW T \\in SUBSET S, NEW U \\in SUBSET T\n PROVE iter(T)[U] = FoldDef(iter(T), U)\n <2>. FiniteSubsetsOf(T) = SUBSET T\n BY FS_Subset, FS_FiniteSubsetsOfFinite\n <2>1. IsWellFoundedOn(StrictSubsetOrdering(T), FiniteSubsetsOf(T))\n BY FS_Subset, FS_StrictSubsetOrderingWellFounded\n <2>2. WFDefOn(StrictSubsetOrdering(T), FiniteSubsetsOf(T), FoldDef)\n BY DEF WFDefOn, SetLessThan, StrictSubsetOrdering\n <2>3. OpDefinesFcn(iter(T), FiniteSubsetsOf(T), FoldDef)\n BY Zenon DEF MapThenFoldSet, OpDefinesFcn\n <2>. HIDE DEF FoldDef\n <2>4. WFInductiveDefines(iter(T), SUBSET T, FoldDef)\n BY <2>1, <2>2, <2>3, WFInductiveDef, Isa\n <2>. QED BY <2>4 DEF WFInductiveDefines\n<1>. HIDE DEF iter\n<1>2. ASSUME NEW T \\in SUBSET S, NEW U \\in SUBSET T\n PROVE iter(S)[U] = iter(T)[U]\n <2>. DEFINE P(V) == iter(S)[V] = iter(T)[V]\n <2>1. IsFiniteSet(U)\n BY FS_Subset\n <2>2. ASSUME NEW V \\in SUBSET U,\n \\A W \\in (SUBSET V) \\ {V} : P(W)\n PROVE P(V)\n <3>1. /\\ iter(S)[V] = FoldDef(iter(S), V)\n /\\ iter(T)[V] = FoldDef(iter(T), V)\n BY <1>1\n <3>2. CASE V = {}\n BY <3>1, <3>2\n <3>3. CASE V # {}\n <4>. DEFINE x == choose(V) W == V \\ {x}\n <4>1. /\\ FoldDef(iter(S), V) = op(f(x), iter(S)[W])\n /\\ FoldDef(iter(T), V) = op(f(x), iter(T)[W])\n BY <3>3\n <4>2. W \\in (SUBSET V) \\ {V}\n BY <3>3\n <4>. QED BY <2>2, <3>1, <4>1, <4>2\n <3>. QED BY <3>2, <3>3\n <2>3. P(U)\n <3>. HIDE DEF P \n <3>. QED BY ONLY <2>1, <2>2, FS_WFInduction, IsaM(\"blast\")\n <2>. QED BY <2>3\n<1>3. MapThenFoldSet(op, base, f, choose, S) = iter(S)[S]\n BY Zenon DEF MapThenFoldSet, iter\n<1>4. @ = FoldDef(iter(S), S)\n BY <1>1, Zenon\n<1>. DEFINE T == S \\ {choose(S)}\n<1>5. iter(S)[T] = iter(T)[T]\n BY <1>2\n<1>6. iter(T)[T] = MapThenFoldSet(op, base, f, choose, T)\n BY Zenon DEF MapThenFoldSet, iter\n<1>. QED BY <1>3, <1>4, <1>5, <1>6\n\n\\* folding the empty set yields the base value\nTHEOREM MapThenFoldSetEmpty ==\n ASSUME NEW op(_,_), NEW base, NEW f(_), NEW choose(_)\n PROVE MapThenFoldSet(op, base, f, choose, {}) = base\n<1>1. /\\ IsFiniteSet({})\n /\\ \\A T \\in SUBSET {} : T # {} => choose(T) \\in T\n BY FS_EmptySet\n<1>2. MapThenFoldSet(op, base, f, choose, {}) = \n IF {} = {} THEN base\n ELSE LET x == choose({})\n IN op(f(x), MapThenFoldSet(op, base, f, choose, {} \\ {x}))\n BY <1>1, MapThenFoldSetDef, IsaM(\"iprover\")\n<1>. QED BY <1>2\n\n\\* fold of a non-empty set\nTHEOREM MapThenFoldSetNonempty ==\n ASSUME NEW op(_,_), NEW base, NEW f(_), NEW choose(_), NEW S, S # {},\n IsFiniteSet(S),\n \\A T \\in SUBSET S : T # {} => choose(T) \\in T\n PROVE MapThenFoldSet(op, base, f, choose, S) = \n LET x == choose(S)\n IN op(f(x), MapThenFoldSet(op, base, f, choose, S \\ {x}))\nBY MapThenFoldSetDef, Isa\n\n\\* equality of MapThenFoldSet for a bijection that behaves homomorphically\nTHEOREM MapThenFoldSetBijection ==\n ASSUME NEW op(_,_), NEW base,\n NEW S, IsFiniteSet(S), NEW fS(_), NEW chS(_),\n \\A U \\in SUBSET S : U # {} => chS(U) \\in U,\n NEW T, IsFiniteSet(T), NEW fT(_), NEW chT(_),\n \\A U \\in SUBSET T : U # {} => chT(U) \\in U,\n NEW bij(_),\n \\A s \\in S : bij(s) \\in T,\n \\A t \\in T : \\E s \\in S : bij(s) = t,\n \\A s1, s2 \\in S : bij(s1) = bij(s2) => s1 = s2,\n \\A s \\in S : fS(s) = fT(bij(s)),\n \\A U \\in SUBSET S : U # {} => bij(chS(U)) = chT({bij(s) : s \\in U})\n PROVE MapThenFoldSet(op, base, fS, chS, S) = MapThenFoldSet(op, base, fT, chT, T)\n<1>. DEFINE P(U) == MapThenFoldSet(op, base, fS, chS, U) =\n MapThenFoldSet(op, base, fT, chT, {bij(s) : s \\in U})\n<1>1. ASSUME NEW U \\in SUBSET S, \\A V \\in (SUBSET U) \\ {U} : P(V)\n PROVE P(U)\n <2>1. CASE U = {}\n BY <2>1, MapThenFoldSetEmpty, Isa \n <2>2. CASE U # {}\n <3>. DEFINE BU == {bij(s) : s \\in U}\n xS == chS(U) xT == chT(BU) \n VS == U \\ {xS} VT == BU \\ {xT}\n <3>. SUFFICES MapThenFoldSet(op, base, fS, chS, U) =\n MapThenFoldSet(op, base, fT, chT, BU)\n OBVIOUS\n <3>1. /\\ IsFiniteSet(U)\n /\\ \\A V \\in SUBSET U : V # {} => chS(V) \\in V \n BY <2>2, FS_Subset\n <3>2. MapThenFoldSet(op, base, fS, chS, U) =\n op(fS(xS), MapThenFoldSet(op, base, fS, chS, VS))\n BY <2>2, <3>1, MapThenFoldSetNonempty, Isa \n <3>3. IsFiniteSet(BU)\n BY <3>1, FS_Image, Isa\n <3>4. /\\ BU # {}\n /\\ \\A V \\in SUBSET BU : V # {} => chT(V) \\in V \n BY <2>2\n <3>5. MapThenFoldSet(op, base, fT, chT, BU) =\n op(fT(xT), MapThenFoldSet(op, base, fT, chT, VT))\n <4>. HIDE DEF BU \n <4>. QED BY <3>3, <3>4, MapThenFoldSetNonempty, Isa\n <3>6. /\\ xS \\in U \n /\\ xT = bij(xS)\n /\\ fS(xS) = fT(xT)\n /\\ VT = {bij(s) : s \\in VS}\n BY <2>2, <3>1\n <3>7. P(VS)\n BY <1>1, <3>6\n <3>. HIDE DEF BU, xS, xT, VS, VT\n <3>. QED BY <3>2, <3>5, <3>6, <3>7\n <2>. QED BY <2>1, <2>2\n<1>2. P(S)\n BY <1>1, FS_WFInduction, IsaM(\"iprover\")\n<1>3. {bij(s) : s \\in S} = T\n OBVIOUS\n<1>. QED BY <1>2, <1>3\n\n\\* special case when the bijection is the identity\nTHEOREM MapThenFoldSetEqual ==\n ASSUME NEW op(_,_), NEW base, NEW choose(_), NEW S, IsFiniteSet(S),\n \\A T \\in SUBSET S : T # {} => choose(T) \\in T,\n NEW f(_), NEW g(_), \n \\A s \\in S : f(s) = g(s)\n PROVE MapThenFoldSet(op, base, f, choose, S) = \n MapThenFoldSet(op, base, g, choose, S)\n<1>. DEFINE id(s) == s \n<1>1. /\\ \\A s \\in S : id(s) \\in S\n /\\ \\A t \\in S : \\E s \\in S : id(s) = t\n /\\ \\A s1, s2 \\in S : id(s1) = id(s2) => s1 = s2\n /\\ \\A s \\in S : f(s) = g(id(s))\n /\\ \\A U \\in SUBSET S : U # {} => id(choose(U)) = choose({id(s) : s \\in U})\n BY Zenon\n<1>. HIDE DEF id\n<1>. QED BY <1>1, MapThenFoldSetBijection, IsaM(\"iprover\")\n\n\\* type of a fold\nTHEOREM MapThenFoldSetType ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW Typ, NEW op(_,_), NEW base \\in Typ, NEW f(_), NEW choose(_),\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A s \\in S : f(s) \\in Typ,\n \\A T \\in SUBSET S : T # {} => choose(T) \\in T\n PROVE MapThenFoldSet(op, base, f, choose, S) \\in Typ\n<1>1. ASSUME NEW T \\in SUBSET S,\n \\A U \\in (SUBSET T) \\ {T} : MapThenFoldSet(op, base, f, choose, U) \\in Typ\n PROVE MapThenFoldSet(op, base, f, choose, T) \\in Typ\n <2>1. CASE T = {}\n BY <2>1, MapThenFoldSetEmpty, Zenon\n <2>2. CASE T # {}\n <3>. DEFINE x == choose(T) U == T \\ {x}\n <3>1. /\\ IsFiniteSet(T)\n /\\ \\A V \\in SUBSET T : V # {} => choose(V) \\in V\n BY FS_Subset\n <3>2. MapThenFoldSet(op, base, f, choose, T) =\n op(f(x), MapThenFoldSet(op, base, f, choose, U))\n BY ONLY <2>2, <3>1, MapThenFoldSetNonempty, Isa\n <3>. QED BY <1>1, <2>2, <3>2\n <2>. QED BY <2>1, <2>2\n<1>. QED BY <1>1, FS_WFInduction, IsaM(\"iprover\")\n\n(*************************************************************************)\n(* In many applications the operator used for folding is associative and *)\n(* commutative. In that case, the following theorem may be more useful *)\n(* for reasoning about folding a non-empty set. *)\n(*************************************************************************)\nTHEOREM MapThenFoldSetAC ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ, NEW f(_),\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n NEW S, IsFiniteSet(S), NEW x \\in S,\n NEW choose(_), \\A T \\in SUBSET S : T # {} => choose(T) \\in T,\n \\A s \\in S : f(s) \\in Typ\n PROVE MapThenFoldSet(op, base, f, choose, S) = \n op(f(x), MapThenFoldSet(op, base, f, choose, S \\ {x}))\n<1>. DEFINE P(U) == \\A y \\in U : MapThenFoldSet(op, base, f, choose, U) = \n op(f(y), MapThenFoldSet(op, base, f, choose, U \\ {y}))\n<1>1. ASSUME NEW U \\in SUBSET S,\n \\A V \\in (SUBSET U) \\ {U} : P(V)\n PROVE P(U)\n <2>1. IsFiniteSet(U)\n BY FS_Subset\n <2>. SUFFICES ASSUME NEW y \\in U \n PROVE MapThenFoldSet(op, base, f, choose, U) = \n op(f(y), MapThenFoldSet(op, base, f, choose, U \\ {y}))\n OBVIOUS\n <2>. DEFINE z == choose(U)\n <2>2. /\\ U # {}\n /\\ \\A T \\in SUBSET U : T # {} => choose(T) \\in T \n OBVIOUS\n <2>3. MapThenFoldSet(op, base, f, choose, U) = op(f(z), MapThenFoldSet(op, base, f, choose, U \\ {z}))\n BY <2>1, <2>2, MapThenFoldSetNonempty, IsaM(\"iprover\")\n <2>4. CASE z # y\n <3>1. /\\ y \\in U \\ {z}\n /\\ U \\ {z} \\in (SUBSET U) \\ {U}\n /\\ IsFiniteSet(U \\ {z})\n /\\ IsFiniteSet((U \\ {z}) \\ {y})\n BY <2>1, <2>4, FS_RemoveElement\n <3>a. /\\ \\A u \\in U \\ {z} : f(u) \\in Typ\n /\\ \\A T \\in SUBSET (U \\ {z}) : T # {} => choose(T) \\in T\n /\\ \\A u \\in (U \\ {z}) \\ {y} : f(u) \\in Typ\n /\\ \\A T \\in SUBSET ((U \\ {z}) \\ {y}) : T # {} => choose(T) \\in T\n OBVIOUS\n <3>2. /\\ MapThenFoldSet(op, base, f, choose, U \\ {z}) \\in Typ\n /\\ MapThenFoldSet(op, base, f, choose, (U \\ {z}) \\ {y}) \\in Typ\n BY <3>1, <3>a, MapThenFoldSetType, Isa\n <3>3. MapThenFoldSet(op, base, f, choose, U \\ {z}) = \n op(f(y), MapThenFoldSet(op, base, f, choose, (U \\ {z}) \\ {y}))\n BY <1>1, <3>1\n <3>4. op(f(z), op(f(y), MapThenFoldSet(op, base, f, choose, (U \\ {z}) \\ {y}))) =\n op(f(y), op(f(z), MapThenFoldSet(op, base, f, choose, (U \\ {z}) \\ {y})))\n BY <3>2\n <3>5. /\\ (U \\ {z}) \\ {y} = (U \\ {y}) \\ {z}\n /\\ z \\in U \\ {y}\n /\\ U \\ {y} \\in (SUBSET U) \\ {U}\n /\\ IsFiniteSet(U \\ {y})\n BY <2>1, <2>4, FS_RemoveElement\n <3>6. MapThenFoldSet(op, base, f, choose, U \\ {y}) = \n op(f(z), MapThenFoldSet(op, base, f, choose, (U \\ {y}) \\ {z}))\n BY <1>1, <3>5\n <3>. QED BY <2>3, <3>3, <3>4, <3>5, <3>6\n <2>. QED BY <2>3, <2>4\n<1>. QED \n <2>. HIDE DEF P \n <2>. P(S) BY <1>1, FS_WFInduction, IsaM(\"iprover\")\n <2>. QED BY DEF P\n\n\\* reformulation for adding an element to the set\nTHEOREM MapThenFoldSetACAddElement ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ, NEW f(_),\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n NEW S, IsFiniteSet(S), NEW x, x \\notin S,\n NEW choose(_), \\A T \\in SUBSET (S \\union {x}) : T # {} => choose(T) \\in T,\n \\A s \\in S \\union {x} : f(s) \\in Typ\n PROVE MapThenFoldSet(op, base, f, choose, S \\union {x}) = \n op(f(x), MapThenFoldSet(op, base, f, choose, S))\n<1>. DEFINE U == S \\union {x}\n<1>1. MapThenFoldSet(op, base, f, choose, U) = \n op(f(x), MapThenFoldSet(op, base, f, choose, U \\ {x}))\n <2>. /\\ IsFiniteSet(U)\n /\\ x \\in U \n /\\ U \\ {x} = S \n /\\ \\A T \\in SUBSET U : T # {} => choose(T) \\in T \n /\\ \\A s \\in U : f(s) \\in Typ \n BY FS_AddElement\n <2>. HIDE DEF U \n <2>. QED BY MapThenFoldSetAC, IsaM(\"iprover\")\n<1>. QED BY <1>1, Zenon\n\n(*************************************************************************)\n(* Moreover, for an AC operator with base as the neutral element, *)\n(* folding commutes with set union, for disjoint sets. *)\n(*************************************************************************)\nTHEOREM MapThenFoldSetDisjointUnion ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ, NEW f(_),\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n \\A t \\in Typ : op(base, t) = t,\n NEW S, IsFiniteSet(S),\n NEW T, IsFiniteSet(T), S \\cap T = {},\n NEW choose(_), \n \\A U \\in SUBSET (S \\union T) : U # {} => choose(U) \\in U,\n \\A x \\in S \\union T : f(x) \\in Typ\n PROVE MapThenFoldSet(op, base, f, choose, S \\union T) = \n op(MapThenFoldSet(op, base, f, choose, S),\n MapThenFoldSet(op, base, f, choose, T))\n<1>. DEFINE P(U) == MapThenFoldSet(op, base, f, choose, U \\union T) = \n op(MapThenFoldSet(op, base, f, choose, U),\n MapThenFoldSet(op, base, f, choose, T))\n<1>1. MapThenFoldSet(op, base, f, choose, T) \\in Typ\n <2>. /\\ \\A U \\in SUBSET T : U # {} => choose(U) \\in U \n /\\ \\A x \\in T : f(x) \\in Typ\n OBVIOUS\n <2>. QED BY MapThenFoldSetType, IsaM(\"iprover\")\n<1>2. P({})\n BY <1>1, MapThenFoldSetEmpty, Isa\n<1>3. ASSUME NEW U \\in SUBSET S, IsFiniteSet(U), P(U), NEW x \\in S \\ U\n PROVE P(U \\union {x})\n <2>1. /\\ IsFiniteSet((U \\union {x}) \\union T)\n /\\ x \\in (U \\union {x}) \\union T\n /\\ \\A y \\in (U \\union {x}) \\union T : f(y) \\in Typ\n /\\ \\A V \\in SUBSET ((U \\union {x}) \\union T) : V # {} => choose(V) \\in V\n BY <1>3, FS_Union, FS_AddElement\n <2>2. MapThenFoldSet(op, base, f, choose, (U \\union {x}) \\union T) =\n op(f(x), MapThenFoldSet(op, base, f, choose, ((U \\union {x}) \\union T) \\ {x}))\n BY <2>1, MapThenFoldSetAC, IsaM(\"iprover\")\n <2>3. ((U \\union {x}) \\union T) \\ {x} = U \\union T\n OBVIOUS\n <2>4. MapThenFoldSet(op, base, f, choose, U \\union T) =\n op(MapThenFoldSet(op, base, f, choose, U), MapThenFoldSet(op, base, f, choose, T))\n BY <1>3\n <2>5. /\\ IsFiniteSet(U \\union {x})\n /\\ x \\in U \\union {x}\n /\\ \\A y \\in U \\union {x} : f(y) \\in Typ\n /\\ \\A V \\in SUBSET (U \\union {x}) : V # {} => choose(V) \\in V\n BY <1>3, FS_AddElement\n <2>6. /\\ IsFiniteSet(U)\n /\\ \\A y \\in U : f(y) \\in Typ\n /\\ \\A V \\in SUBSET U : V # {} => choose(V) \\in V\n BY <1>3\n <2>7. MapThenFoldSet(op, base, f, choose, U) \\in Typ \n BY <2>6, MapThenFoldSetType, Isa\n <2>8. op(f(x), op(MapThenFoldSet(op, base, f, choose, U), MapThenFoldSet(op, base, f, choose, T))) =\n op(op(f(x), MapThenFoldSet(op, base, f, choose, U)), MapThenFoldSet(op, base, f, choose, T))\n BY <1>1, <2>7\n <2>9. MapThenFoldSet(op, base, f, choose, U \\union {x}) =\n op(f(x), MapThenFoldSet(op, base, f, choose, (U \\union {x}) \\ {x}))\n BY <2>5, MapThenFoldSetAC, IsaM(\"iprover\")\n <2>10. (U \\union {x}) \\ {x} = U \n OBVIOUS\n <2>. QED BY <2>2, <2>3, <2>4, <2>8, <2>9, <2>10\n<1>. QED\n <2>. HIDE DEF P \n <2>. P(S) BY <1>2, <1>3, FS_Induction, IsaM(\"iprover\")\n <2>. QED BY DEF P \n\n===========================================================================\n", "FunctionTheorems": "------------------------ MODULE FunctionTheorems ----------------------------\n(***************************************************************************)\n(* `^{\\large \\vspace{12pt} *)\n(* Proofs of facts about functions. *)\n(* Originally contributed by Tom Rodeheffer, MSR. *)\n(* \\vspace{12pt}}^' *)\n(***************************************************************************)\n\nEXTENDS Functions, Integers, FiniteSets\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Function restriction. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_RestrictProperties ==\n ASSUME NEW S, NEW T, NEW f \\in [S -> T], NEW A \\in SUBSET S\n PROVE /\\ Restrict(f,A) \\in [A -> T]\n /\\ \\A x \\in A : Restrict(f,A)[x] = f[x]\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Range of a function. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_RangeProperties ==\n ASSUME NEW S, NEW T, NEW f \\in [S -> T]\n PROVE /\\ Range(f) \\subseteq T\n /\\ \\A y \\in Range(f) : \\E x \\in S : f[x] = y\n /\\ f \\in Surjection(S, Range(f))\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Inverse of a function. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_InverseProperties ==\n ASSUME NEW S, NEW T, NEW f \\in [S -> T]\n PROVE /\\ (S = {} => T = {}) => Inverse(f,S,T) \\in [T -> S]\n /\\ \\A y \\in Range(f) : f[Inverse(f,S,T)[y]] = y\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Introduction rules for injections, surjections, bijections. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_IsInj ==\n ASSUME NEW S, NEW T, NEW F \\in [S -> T],\n \\A a,b \\in S : F[a] = F[b] => a = b\n PROVE F \\in Injection(S,T)\n\nTHEOREM Fun_IsSurj ==\n ASSUME NEW S, NEW T, NEW F \\in [S -> T],\n \\A t \\in T : \\E s \\in S : F[s] = t\n PROVE F \\in Surjection(S,T)\n\nTHEOREM Fun_IsBij ==\n ASSUME NEW S, NEW T, NEW F,\n \\/ F \\in Injection(S,T)\n \\/ (F \\in [S -> T] /\\ \\A a,b \\in S : F[a] = F[b] => a = b),\n\n \\/ F \\in Surjection(S,T)\n \\/ (F \\in [S -> T] /\\ \\A t \\in T : \\E s \\in S : F[s] = t)\n PROVE F \\in Bijection(S,T)\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Properties of injections, surjections, and bijections. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_InjectionProperties ==\n ASSUME NEW S, NEW T, NEW F \\in Injection(S,T)\n PROVE /\\ F \\in [S -> T]\n /\\ \\A a,b \\in S : F[a] = F[b] => a = b\n\nTHEOREM Fun_SurjectionProperties ==\n ASSUME NEW S, NEW T, NEW F \\in Surjection(S,T)\n PROVE /\\ F \\in [S -> T]\n /\\ \\A t \\in T : \\E s \\in S : F[s] = t\n /\\ Range(F) = T\n\nTHEOREM Fun_BijectionProperties ==\n ASSUME NEW S, NEW T, NEW F \\in Bijection(S,T)\n PROVE /\\ F \\in [S -> T]\n /\\ F \\in Injection(S,T)\n /\\ F \\in Surjection(S,T)\n /\\ \\A a,b \\in S : F[a] = F[b] => a = b\n /\\ \\A t \\in T : \\E s \\in S : F[s] = t\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* A surjection in [S -> T] such that there is no surjection from any *)\n(* subset of S to T is a bijection. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_SmallestSurjectionIsBijection ==\n ASSUME NEW S, NEW T, NEW f \\in Surjection(S,T),\n \\A U \\in SUBSET S : U # S => Surjection(U,T) = {}\n PROVE f \\in Bijection(S,T)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Transitivity of injections, surjections, bijections. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_InjTransitive ==\n ASSUME NEW S, NEW T, NEW U,\n NEW F \\in Injection(S,T),\n NEW G \\in Injection(T,U)\n PROVE [s \\in S |-> G[F[s]]] \\in Injection(S,U)\n\nTHEOREM Fun_SurjTransitive ==\n ASSUME NEW S, NEW T, NEW U,\n NEW F \\in Surjection(S,T),\n NEW G \\in Surjection(T,U)\n PROVE [s \\in S |-> G[F[s]]] \\in Surjection(S,U)\n\nTHEOREM Fun_BijTransitive ==\n ASSUME NEW S, NEW T, NEW U,\n NEW F \\in Bijection(S,T),\n NEW G \\in Bijection(T,U)\n PROVE [s \\in S |-> G[F[s]]] \\in Bijection(S,U)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The inverse of a surjection is an injection and vice versa. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_SurjInverse ==\n ASSUME NEW S, NEW T, NEW f \\in Surjection(S,T)\n PROVE Inverse(f,S,T) \\in Injection(T,S)\n\nTHEOREM Fun_InjInverse ==\n ASSUME NEW S, NEW T, NEW f \\in Injection(S,T), S = {} => T = {}\n PROVE Inverse(f,S,T) \\in Surjection(T,S)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Properties of the inverse of a bijection. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_BijInverse ==\n ASSUME NEW S, NEW T, NEW f \\in Bijection(S,T)\n PROVE /\\ Inverse(f,S,T) \\in Bijection(T,S)\n /\\ \\A s \\in S : Inverse(f,S,T)[f[s]] = s\n /\\ Inverse(Inverse(f,S,T), T,S) = f\n\n(***************************************************************************)\n(* We cannot conclude that a function f is a bijection if its inverse is *)\n(* a bijection, and the following is a counter-example. *)\n(* *)\n(* S = {1,2} T = {3,4} *)\n(* f = [x \\in S |-> 3] *)\n(* *)\n(* Then, Inverse(f,S,T) can very well be (3 :> 1) @@ (4 :> 2), which is a *)\n(* bijection although f is not. *)\n(***************************************************************************)\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The restriction of a bijection is a bijection. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_BijRestrict ==\n ASSUME NEW S, NEW T, NEW F \\in Bijection(S,T),\n NEW R \\in SUBSET S\n PROVE Restrict(F, R) \\in Bijection(R, Range(Restrict(F, R)))\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Given F an injection from S to T, then F is a bijection from S to F(S). *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_InjMeansBijImage ==\n ASSUME NEW S, NEW T, NEW F \\in Injection(S,T)\n PROVE F \\in Bijection(S, Range(F))\n\n\n-----------------------------------------------------------------------------\n(***************************************************************************)\n(* `^{\\large \\vspace{12pt} *)\n(* Facts about exists jections. *)\n(* \\vspace{12pt}}^' *)\n(***************************************************************************)\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Definitions restated as facts. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_ExistsInj ==\n \\A S,T : ExistsInjection(S,T) <=> Injection(S,T) # {}\n\nTHEOREM Fun_ExistsSurj ==\n \\A S,T : ExistsSurjection(S,T) <=> Surjection(S,T) # {}\n\nTHEOREM Fun_ExistsBij ==\n \\A S,T : ExistsBijection(S,T) <=> Bijection(S,T) # {}\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* There is a surjection from any set S to any non-empty subset T of S. *)\n(* (Note that there cannot be a surjection to {} except if S is empty.) *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_ExistsSurjSubset ==\n ASSUME NEW S, NEW T \\in SUBSET S, T # {}\n PROVE ExistsSurjection(S,T)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* If there is a surjection from S to T, then there is an injection from T *)\n(* to S. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_ExistsSurjMeansExistsRevInj ==\n ASSUME NEW S, NEW T, ExistsSurjection(S,T)\n PROVE ExistsInjection(T,S)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* ExistsBijection is reflexive, symmetric, and transitive. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_ExistsBijReflexive ==\n ASSUME NEW S\n PROVE ExistsBijection(S,S)\n\nTHEOREM Fun_ExistsBijSymmetric ==\n ASSUME NEW S, NEW T, ExistsBijection(S,T)\n PROVE ExistsBijection(T,S)\n\nTHEOREM Fun_ExistsBijTransitive ==\n ASSUME NEW S, NEW T, NEW U, ExistsBijection(S,T), ExistsBijection(T,U)\n PROVE ExistsBijection(S,U)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Existence of injections and surjections is reflexive and transitive. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_ExistsInjReflexive ==\n ASSUME NEW S\n PROVE ExistsInjection(S,S)\n\nTHEOREM Fun_ExistsSurjReflexive ==\n ASSUME NEW S\n PROVE ExistsSurjection(S,S)\n\nTHEOREM Fun_ExistsInjTransitive ==\n ASSUME NEW S, NEW T, NEW U,\n ExistsInjection(S,T), ExistsInjection(T,U)\n PROVE ExistsInjection(S,U)\n\nTHEOREM Fun_ExistsSurjTransitive ==\n ASSUME NEW S, NEW T, NEW U,\n ExistsSurjection(S,T), ExistsSurjection(T,U)\n PROVE ExistsSurjection(S,U)\n\n\n-----------------------------------------------------------------------------\n(***************************************************************************)\n(* `^{\\large \\vspace{12pt} *)\n(* The Cantor-Bernstein-Schroeder theorem. *)\n(* \\vspace{12pt}}^' *)\n(***************************************************************************)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* If there exists an injection from S to T, where T is a subset of S, *)\n(* then there exists a bijection from S to T. *)\n(* *)\n(* A lemma for the Cantor-Bernstein-Schroeder theorem. *)\n(* *)\n(* This proof is formalized from *)\n(* `^\\url{http://www.proofwiki.org/wiki/Cantor-Bernstein-Schroeder\\_Theorem/Lemma}^' *)\n(* retrieved April 29, 2013. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_CantorBernsteinSchroeder_Lemma ==\n ASSUME NEW S, NEW T, T \\subseteq S, ExistsInjection(S,T)\n PROVE ExistsBijection(S,T)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* If an injection exists from S to T and an injection exists from T to S, *)\n(* then there is a bijection from S to T. *)\n(* *)\n(* This is the Cantor-Bernstein-Schroeder theorem. *)\n(* *)\n(* This proof is formalized from *)\n(* `^\\url{http://www.proofwiki.org/wiki/Cantor-Bernstein-Schroeder_Theorem/Proof_5}^' *)\n(* retrieved April 29, 2013. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_CantorBernsteinSchroeder ==\n ASSUME NEW S, NEW T,\n ExistsInjection(S,T), ExistsInjection(T,S)\n PROVE ExistsBijection(S,T)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Applications of the Cantor-Bernstein-Schroeder Theorem. *)\n(* If there exists an injection f: A->B and a surjection g: A->B, then *)\n(* there exists a bijection between A and B. *)\n(* Also, if there are surjections between A and B, then there is a *)\n(* bijection. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\n\nTHEOREM Fun_ExistInjAndSurjThenBij ==\n ASSUME NEW S, NEW T,\n ExistsInjection(S,T), ExistsSurjection(S,T)\n PROVE ExistsBijection(S,T)\n\nTHEOREM Fun_ExistSurjAndSurjThenBij ==\n ASSUME NEW S, NEW T,\n ExistsSurjection(S,T), ExistsSurjection(T,S)\n PROVE ExistsBijection(S,T)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Equivalences for ExistsBijection. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_ExistsBijEquiv ==\n ASSUME NEW S, NEW T\n PROVE /\\ ExistsBijection(S,T) <=> ExistsBijection(T,S)\n /\\ ExistsBijection(S,T) <=> ExistsInjection(S,T) /\\ ExistsInjection(T,S)\n /\\ ExistsBijection(S,T) <=> ExistsInjection(S,T) /\\ ExistsSurjection(S,T)\n /\\ ExistsBijection(S,T) <=> ExistsInjection(T,S) /\\ ExistsSurjection(T,S)\n /\\ ExistsBijection(S,T) <=> ExistsSurjection(S,T) /\\ ExistsSurjection(T,S)\n\n\n-----------------------------------------------------------------------------\n(***************************************************************************)\n(* `^{\\large \\vspace{12pt} *)\n(* Facts about jections involving 1..n. *)\n(* \\vspace{12pt}}^' *)\n(***************************************************************************)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* There is a bijection from 1..b-a+1 to a..b for integers a,b with a <= b.*)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_ExistsBijInterval ==\n ASSUME NEW a \\in Int, NEW b \\in Int, a <= b\n PROVE ExistsBijection(1 .. b-a+1, a .. b)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* There is an injection from 1..n to 1..m iff n \\leq m. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_NatInjLeq ==\n ASSUME NEW n \\in Nat, NEW m \\in Nat\n PROVE ExistsInjection(1..n,1..m) <=> n \\leq m\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* If a surjection from 1..n to S exists (for some n \\in Nat) then a *)\n(* bijection from 1..m to S exists (for some m \\in Nat) and m \\leq n. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_NatSurjImpliesNatBij ==\n ASSUME NEW S, NEW n \\in Nat, ExistsSurjection(1..n,S)\n PROVE \\E m \\in Nat : ExistsBijection(1..m,S) /\\ m \\leq n\n\n\n(***************************************************************************)\n(* Simple corollary. *)\n(***************************************************************************)\nTHEOREM Fun_NatSurjEquivNatBij ==\n ASSUME NEW S\n PROVE (\\E n \\in Nat : ExistsSurjection(1..n,S))\n <=> (\\E m \\in Nat : ExistsBijection(1..m,S))\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* For any set S, given n, m \\in Nat such that bijections exist from 1..n *)\n(* to S and from 1..m to S, then it must be the case that n = m. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_NatBijSame ==\n ASSUME NEW S,\n NEW n \\in Nat, ExistsBijection(1..n,S),\n NEW m \\in Nat, ExistsBijection(1..m,S)\n PROVE n = m\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* S is empty iff there exists a bijection from 1..0 to S. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_NatBijEmpty ==\n ASSUME NEW S\n PROVE ExistsBijection(1..0,S) <=> S = {}\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* S is a singleton iff there exists a bijection from 1..1 to S. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_NatBijSingleton ==\n ASSUME NEW S\n PROVE ExistsBijection(1..1,S) <=> \\E s : S = {s}\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* If there exists a bijection from 1..m to S (for some m \\in Nat), then *)\n(* there exists a bijection from 1..n to T (for some n \\in Nat), where T *)\n(* is a subset of S. Furthermore n \\leq m. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_NatBijSubset ==\n ASSUME NEW S, NEW m \\in Nat, ExistsBijection(1..m,S),\n NEW T \\in SUBSET S\n PROVE \\E n \\in Nat : ExistsBijection(1..n,T) /\\ n \\leq m\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* If there exists a bijection from 1..m to S (for some m \\in Nat), then *)\n(* there exists a bijection from 1..(m+1) to S \\cup {x}, where x \\notin S. *)\n(* *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_NatBijAddElem ==\n ASSUME NEW S, NEW m \\in Nat, ExistsBijection(1..m,S),\n NEW x, x \\notin S\n PROVE ExistsBijection(1..(m+1), S \\cup {x})\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* If there exists a bijection from 1..m to S (for some m \\in Nat), then *)\n(* there exists a bijection from 1..(m-1) to S \\ {x}, where x \\in S. *)\n(* *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_NatBijSubElem ==\n ASSUME NEW S, NEW m \\in Nat, ExistsBijection(1..m,S),\n NEW x, x \\in S\n PROVE ExistsBijection(1..(m-1), S \\ {x})\n\n-----------------------------------------------------------------------------\n\n(***************************************************************************)\n(* Folding a function for an empty set of indices yields the base value. *)\n(***************************************************************************)\nTHEOREM FoldFunctionOnSetEmpty ==\n ASSUME NEW op(_,_), NEW base, NEW fun\n PROVE FoldFunctionOnSet(op, base, fun, {}) = base \n\n(***************************************************************************)\n(* Folding a function for a non-empty set of indices corresponds to *)\n(* applying the binary operator to f[i], for some i \\in indices, and the *)\n(* result of recursing for the set indices \\ {i}. *)\n(***************************************************************************)\nTHEOREM FoldFunctionOnSetNonempty ==\n ASSUME NEW op(_,_), NEW base, NEW fun, \n NEW indices, indices # {}, IsFiniteSet(indices)\n PROVE \\E i \\in indices : FoldFunctionOnSet(op, base, fun, indices) = \n op(fun[i], FoldFunctionOnSet(op, base, fun, indices \\ {i}))\n\n(***************************************************************************)\n(* The type of folding a function corresponds to the type associated with *)\n(* the binary operator that underlies the fold. *)\n(***************************************************************************)\nTHEOREM FoldFunctionOnSetType ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ,\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n NEW indices, IsFiniteSet(indices), NEW fun,\n \\A i \\in indices : fun[i] \\in Typ\n PROVE FoldFunctionOnSet(op, base, fun, indices) \\in Typ\n\n(***************************************************************************)\n(* Folding two functions that agree on every element of the set of indices *)\n(* yields the same value. *)\n(***************************************************************************)\nTHEOREM FoldFunctionOnSetEqual ==\n ASSUME NEW op(_,_), NEW base, NEW f, NEW g,\n NEW indices, IsFiniteSet(indices),\n \\A i \\in indices : f[i] = g[i]\n PROVE FoldFunctionOnSet(op, base, f, indices) = \n FoldFunctionOnSet(op, base, g, indices)\n\n(***************************************************************************)\n(* If the binary operator is associative and commutative, the result of *)\n(* folding a function over a non-empty set corresponds to applying the *)\n(* operator to an arbitrary element of the set and the result of folding *)\n(* the function over the remainder of the set. *)\n(***************************************************************************)\nTHEOREM FoldFunctionOnSetAC ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ,\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n NEW indices, IsFiniteSet(indices), NEW i \\in indices, NEW fun,\n \\A j \\in indices : fun[j] \\in Typ\n PROVE FoldFunctionOnSet(op, base, fun, indices) =\n op(fun[i], FoldFunctionOnSet(op, base, fun, indices \\ {i}))\n\n\\* reformulation in terms of adding an index to the set\nTHEOREM FoldFunctionOnSetACAddIndex ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ,\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n NEW indices, IsFiniteSet(indices), NEW i, i \\notin indices,\n NEW fun, \\A j \\in indices \\union {i} : fun[j] \\in Typ\n PROVE FoldFunctionOnSet(op, base, fun, indices \\union {i}) =\n op(fun[i], FoldFunctionOnSet(op, base, fun, indices))\n\n(*************************************************************************)\n(* For an AC operator, folding an EXCEPT construction can be reduced to *)\n(* folding the original function. *)\n(*************************************************************************)\nTHEOREM FoldFunctionOnSetACExcept ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ,\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n NEW fun, NEW i, NEW y \\in Typ,\n NEW indices \\in SUBSET (DOMAIN fun), IsFiniteSet(indices), \n \\A j \\in indices : fun[j] \\in Typ \n PROVE FoldFunctionOnSet(op, base, [fun EXCEPT ![i] = y], indices) =\n IF i \\in indices\n THEN op(y, FoldFunctionOnSet(op, base, fun, indices \\ {i}))\n ELSE FoldFunctionOnSet(op, base, fun, indices)\n\n(*************************************************************************)\n(* For an AC operator `op` for which `base` is the neutral element, *)\n(* folding a function commutes with set union, for disjoint sets. *)\n(*************************************************************************)\nTHEOREM FoldFunctionOnSetDisjointUnion ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ,\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n \\A t \\in Typ : op(base, t) = t,\n NEW S, IsFiniteSet(S), NEW T, IsFiniteSet(T), S \\cap T = {},\n NEW fun, \\A i \\in S \\union T : fun[i] \\in Typ\n PROVE FoldFunctionOnSet(op, base, fun, S \\union T) =\n op(FoldFunctionOnSet(op, base, fun, S), FoldFunctionOnSet(op, base, fun, T))\n\n(*************************************************************************)\n(* Analogous theorems for FoldFunction. Note that the theorems about *)\n(* adding an index and about disjoint union do not make sense here. *)\n(*************************************************************************)\nTHEOREM FoldFunctionEmpty ==\n ASSUME NEW op(_,_), NEW base, NEW fun, DOMAIN fun = {}\n PROVE FoldFunction(op, base, fun) = base \n\nTHEOREM FoldFunctionNonempty ==\n ASSUME NEW op(_,_), NEW base, NEW fun, \n DOMAIN fun # {}, IsFiniteSet(DOMAIN fun)\n PROVE \\E i \\in DOMAIN fun : FoldFunction(op, base, fun) = \n op(fun[i], FoldFunctionOnSet(op, base, fun, DOMAIN fun \\ {i}))\n\nTHEOREM FoldFunctionType ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ,\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n NEW fun, IsFiniteSet(DOMAIN fun),\n \\A i \\in DOMAIN fun : fun[i] \\in Typ\n PROVE FoldFunction(op, base, fun) \\in Typ\n\nTHEOREM FoldFunctionAC ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ,\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n NEW fun, IsFiniteSet(DOMAIN fun), NEW i \\in DOMAIN fun,\n \\A j \\in DOMAIN fun : fun[j] \\in Typ\n PROVE FoldFunction(op, base, fun) =\n op(fun[i], FoldFunctionOnSet(op, base, fun, (DOMAIN fun) \\ {i}))\n\nTHEOREM FoldFunctionACExcept ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ,\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n NEW fun, NEW i, NEW y \\in Typ,\n IsFiniteSet(DOMAIN fun), \n \\A j \\in DOMAIN fun : fun[j] \\in Typ \n PROVE FoldFunction(op, base, [fun EXCEPT ![i] = y]) =\n IF i \\in DOMAIN fun\n THEN op(y, FoldFunctionOnSet(op, base, fun, (DOMAIN fun) \\ {i}))\n ELSE FoldFunction(op, base, fun)\n\n(*************************************************************************)\n(* Summing a function that returns natural numbers, resp. integers, *)\n(* yields a natural number, resp. an integer. *)\n(*************************************************************************)\nTHEOREM SumFunctionOnSetNat ==\n ASSUME NEW fun, NEW indices, IsFiniteSet(indices),\n \\A i \\in indices : fun[i] \\in Nat \n PROVE SumFunctionOnSet(fun, indices) \\in Nat\n\nTHEOREM SumFunctionOnSetInt ==\n ASSUME NEW fun, NEW indices, IsFiniteSet(indices),\n \\A i \\in indices : fun[i] \\in Int\n PROVE SumFunctionOnSet(fun, indices) \\in Int\n\n(*************************************************************************)\n(* Summing two functions that agree on all relevant indices yields the *)\n(* same result. *)\n(*************************************************************************)\nTHEOREM SumFunctionOnSetEqual ==\n ASSUME NEW f, NEW g, NEW indices, IsFiniteSet(indices),\n \\A i \\in indices : f[i] = g[i]\n PROVE SumFunctionOnSet(f, indices) = SumFunctionOnSet(g, indices)\n\n(*************************************************************************)\n(* Summing a function over the empty set is 0. *)\n(*************************************************************************)\nTHEOREM SumFunctionOnSetEmpty ==\n ASSUME NEW fun \n PROVE SumFunctionOnSet(fun, {}) = 0\n\n(*************************************************************************)\n(* Summing a function over a non-empty set corresponds to the sum of *)\n(* element of the set and the sum of the function over the remainder. *)\n(*************************************************************************)\nTHEOREM SumFunctionOnSetNonempty ==\n ASSUME NEW fun, NEW indices, IsFiniteSet(indices), NEW i \\in indices,\n \\A j \\in indices : fun[j] \\in Int\n PROVE SumFunctionOnSet(fun, indices) = \n fun[i] + SumFunctionOnSet(fun, indices \\ {i})\n\n\\* reformulation in terms of adding an index\nTHEOREM SumFunctionOnSetAddIndex ==\n ASSUME NEW fun, NEW indices, IsFiniteSet(indices), \n NEW i, i \\notin indices,\n \\A j \\in indices \\union {i} : fun[j] \\in Int\n PROVE SumFunctionOnSet(fun, indices \\union {i}) = \n fun[i] + SumFunctionOnSet(fun, indices)\n\n\\* corresponding lemma for removing an index\nTHEOREM SumFunctionOnSetRemoveIndex ==\n ASSUME NEW fun, NEW indices, IsFiniteSet(indices), NEW i \\in indices,\n \\A j \\in indices : fun[j] \\in Int\n PROVE SumFunctionOnSet(fun, indices \\ {i}) = \n SumFunctionOnSet(fun, indices) - fun[i]\n\n(*************************************************************************)\n(* Reduce the sum of an EXCEPT to the sum of the original function. *)\n(*************************************************************************)\nTHEOREM SumFunctionOnSetExcept ==\n ASSUME NEW fun, NEW i, NEW y \\in Int,\n NEW indices \\in SUBSET (DOMAIN fun), IsFiniteSet(indices),\n \\A j \\in indices : fun[j] \\in Int \n PROVE SumFunctionOnSet([fun EXCEPT ![i] = y], indices) =\n IF i \\in indices\n THEN SumFunctionOnSet(fun, indices) + y - fun[i]\n ELSE SumFunctionOnSet(fun, indices)\n\n(*************************************************************************)\n(* Summing a function distributes over disjoint union. *)\n(*************************************************************************)\nTHEOREM SumFunctionOnSetDisjointUnion ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW T, IsFiniteSet(T), S \\cap T = {},\n NEW fun, \\A x \\in S \\union T : fun[x] \\in Int \n PROVE SumFunctionOnSet(fun, S \\union T) =\n SumFunctionOnSet(fun, S) + SumFunctionOnSet(fun, T)\n\n(*************************************************************************)\n(* Summing a Nat-valued function is monotonic in the subset relation. *)\n(*************************************************************************)\nTHEOREM SumFunctionNatOnSubset ==\n ASSUME NEW S, IsFiniteSet(S), NEW T \\in SUBSET S,\n NEW fun, \\A s \\in S : fun[s] \\in Nat\n PROVE SumFunctionOnSet(fun, T) <= SumFunctionOnSet(fun, S)\n\n(*************************************************************************)\n(* The sum of a Nat-valued function is zero iff all relevant function *)\n(* values are zero. *)\n(*************************************************************************)\nTHEOREM SumFunctionOnSetZero ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW fun, \\A x \\in S : fun[x] \\in Nat\n PROVE SumFunctionOnSet(fun, S) = 0 <=> \\A x \\in S : fun[x] = 0\n\n(*************************************************************************)\n(* Given any Int-valued function f and finite set S such that f[s] = c *)\n(* for all s \\in S, summing f over S yields c multiplied by the *)\n(* cardinality of S. For example, for *)\n(* f == [s \\in {\"a\", \"b\", \"c\"} |-> IF s = \"c\" THEN 42 ELSE 5], *)\n(* SumFunctionOnSet(f, {\"a\",\"b\"}) = 10. *)\n(*************************************************************************)\nTHEOREM SumFunctionOnSetConst ==\n ASSUME NEW D, NEW S \\in SUBSET D, IsFiniteSet(S),\n NEW f \\in [D -> Int], NEW c \\in Int, \\A x \\in S : f[x] = c \n PROVE SumFunctionOnSet(f, S) = c * Cardinality(S)\n\n(*************************************************************************)\n(* Summing a function is monotonic in the function argument. *)\n(*************************************************************************)\nTHEOREM SumFunctionOnSetMonotonic ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW f, \\A s \\in S : f[s] \\in Int,\n NEW g, \\A s \\in S : g[s] \\in Int,\n \\A s \\in S : f[s] <= g[s]\n PROVE SumFunctionOnSet(f, S) <= SumFunctionOnSet(g, S)\n\n(*************************************************************************)\n(* The sum over function f is strictly smaller than the sum over g if *)\n(* f[x] <= g[x] for all x \\in S and f[x] < g[s] holds for at least some *)\n(* element s \\in S. *)\n(*************************************************************************)\nTHEOREM SumFunctionOnSetStrictlyMonotonic ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW f, \\A s \\in S : f[s] \\in Int,\n NEW g, \\A s \\in S : g[s] \\in Int,\n \\A x \\in S : f[x] <= g[x],\n NEW s \\in S, f[s] < g[s]\n PROVE SumFunctionOnSet(f, S) < SumFunctionOnSet(g, S)\n\n(*************************************************************************)\n(* Similar theorems for SumFunction. *)\n(*************************************************************************)\nTHEOREM SumFunctionNat ==\n ASSUME NEW fun, IsFiniteSet(DOMAIN fun),\n \\A x \\in DOMAIN fun : fun[x] \\in Nat \n PROVE SumFunction(fun) \\in Nat\n\nTHEOREM SumFunctionInt ==\n ASSUME NEW fun, IsFiniteSet(DOMAIN fun),\n \\A x \\in DOMAIN fun : fun[x] \\in Int \n PROVE SumFunction(fun) \\in Int \n\nTHEOREM SumFunctionEmpty ==\n ASSUME NEW fun, DOMAIN fun = {}\n PROVE SumFunction(fun) = 0\n\nTHEOREM SumFunctionNonempty ==\n ASSUME NEW fun, IsFiniteSet(DOMAIN fun), NEW x \\in DOMAIN fun,\n \\A i \\in DOMAIN fun : fun[i] \\in Int\n PROVE SumFunction(fun) = fun[x] + SumFunctionOnSet(fun, (DOMAIN fun) \\ {x})\n\nTHEOREM SumFunctionExcept ==\n ASSUME NEW fun, NEW i, NEW y \\in Int, IsFiniteSet(DOMAIN fun),\n \\A x \\in DOMAIN fun : fun[x] \\in Int \n PROVE SumFunction([fun EXCEPT ![i] = y]) =\n IF i \\in DOMAIN fun \n THEN SumFunction(fun) + y - fun[i]\n ELSE SumFunction(fun)\n\nTHEOREM SumFunctionZero ==\n ASSUME NEW fun, IsFiniteSet(DOMAIN fun), \n \\A x \\in DOMAIN fun : fun[x] \\in Nat\n PROVE SumFunction(fun) = 0 <=> \\A x \\in DOMAIN fun : fun[x] = 0\n\n(*************************************************************************)\n(* Summing a constant function yields the constant multiplied by the *)\n(* cardinality of its domain. For example, *)\n(* SumFunction([s \\in {\"a\",\"b\",\"c\"} |-> 5]) = 15. *)\n(*************************************************************************)\nTHEOREM SumFunctionConst ==\n ASSUME NEW S, IsFiniteSet(S), NEW c \\in Int\n PROVE SumFunction([s \\in S |-> c]) = c * Cardinality(S)\n\nTHEOREM SumFunctionMonotonic ==\n ASSUME NEW f, IsFiniteSet(DOMAIN f), NEW g, DOMAIN g = DOMAIN f,\n \\A x \\in DOMAIN f : f[x] \\in Int,\n \\A x \\in DOMAIN f : g[x] \\in Int,\n \\A x \\in DOMAIN f : f[x] <= g[x]\n PROVE SumFunction(f) <= SumFunction(g)\n\nTHEOREM SumFunctionStrictlyMonotonic ==\n ASSUME NEW f, IsFiniteSet(DOMAIN f), NEW g, DOMAIN g = DOMAIN f,\n \\A x \\in DOMAIN f : f[x] \\in Int,\n \\A x \\in DOMAIN f : g[x] \\in Int,\n \\A x \\in DOMAIN f : f[x] <= g[x],\n NEW s \\in DOMAIN f, f[s] < g[s]\n PROVE SumFunction(f) < SumFunction(g)\n\n=============================================================================\n\\* Created Thu Apr 11 10:36:10 PDT 2013 by tomr\n", "FunctionTheorems_proofs": "--------------------- MODULE FunctionTheorems_proofs ------------------------\n(***************************************************************************)\n(* `^{\\large \\vspace{12pt} *)\n(* Proofs of facts about functions. *)\n(* Originally contributed by Tom Rodeheffer, MSR. *)\n(* \\vspace{12pt}}^' *)\n(***************************************************************************)\n\nEXTENDS Functions, Integers, NaturalsInduction, WellFoundedInduction, \n FiniteSetTheorems, FoldsTheorems, TLAPS\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Function restriction. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_RestrictProperties ==\n ASSUME NEW S, NEW T, NEW f \\in [S -> T], NEW A \\in SUBSET S\n PROVE /\\ Restrict(f,A) \\in [A -> T]\n /\\ \\A x \\in A : Restrict(f,A)[x] = f[x]\nBY DEF Restrict\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Range of a function. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_RangeProperties ==\n ASSUME NEW S, NEW T, NEW f \\in [S -> T]\n PROVE /\\ Range(f) \\subseteq T\n /\\ \\A y \\in Range(f) : \\E x \\in S : f[x] = y\n /\\ f \\in Surjection(S, Range(f))\nBY DEF Range, Surjection\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Inverse of a function. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_InverseProperties ==\n ASSUME NEW S, NEW T, NEW f \\in [S -> T]\n PROVE /\\ (S = {} => T = {}) => Inverse(f,S,T) \\in [T -> S]\n /\\ \\A y \\in Range(f) : f[Inverse(f,S,T)[y]] = y\nBY DEF Inverse, Range\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Introduction rules for injections, surjections, bijections. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_IsInj ==\n ASSUME NEW S, NEW T, NEW F \\in [S -> T],\n \\A a,b \\in S : F[a] = F[b] => a = b\n PROVE F \\in Injection(S,T)\nBY DEF Injection, IsInjective\n\n\nTHEOREM Fun_IsSurj ==\n ASSUME NEW S, NEW T, NEW F \\in [S -> T],\n \\A t \\in T : \\E s \\in S : F[s] = t\n PROVE F \\in Surjection(S,T)\nBY DEF Surjection\n\n\nTHEOREM Fun_IsBij ==\n ASSUME NEW S, NEW T, NEW F,\n \\/ F \\in Injection(S,T)\n \\/ (F \\in [S -> T] /\\ \\A a,b \\in S : F[a] = F[b] => a = b),\n\n \\/ F \\in Surjection(S,T)\n \\/ (F \\in [S -> T] /\\ \\A t \\in T : \\E s \\in S : F[s] = t)\n PROVE F \\in Bijection(S,T)\nBY DEF Bijection, Injection, IsInjective, Surjection\n\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Properties of injections, surjections, and bijections. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_InjectionProperties ==\n ASSUME NEW S, NEW T, NEW F \\in Injection(S,T)\n PROVE /\\ F \\in [S -> T]\n /\\ \\A a,b \\in S : F[a] = F[b] => a = b\nBY DEF Injection, IsInjective\n\n\nTHEOREM Fun_SurjectionProperties ==\n ASSUME NEW S, NEW T, NEW F \\in Surjection(S,T)\n PROVE /\\ F \\in [S -> T]\n /\\ \\A t \\in T : \\E s \\in S : F[s] = t\n /\\ Range(F) = T\nBY DEF Surjection, Range\n\n\nTHEOREM Fun_BijectionProperties ==\n ASSUME NEW S, NEW T, NEW F \\in Bijection(S,T)\n PROVE /\\ F \\in [S -> T]\n /\\ F \\in Injection(S,T)\n /\\ F \\in Surjection(S,T)\n /\\ \\A a,b \\in S : F[a] = F[b] => a = b\n /\\ \\A t \\in T : \\E s \\in S : F[s] = t\nBY DEF Bijection, Injection, IsInjective, Surjection\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* A surjection in [S -> T] such that there is no surjection from any *)\n(* subset of S to T is a bijection. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_SmallestSurjectionIsBijection ==\n ASSUME NEW S, NEW T, NEW f \\in Surjection(S,T),\n \\A U \\in SUBSET S : U # S => Surjection(U,T) = {}\n PROVE f \\in Bijection(S,T)\n<1>1. f \\in [S -> T]\n BY Fun_SurjectionProperties\n<1>2. SUFFICES ASSUME f \\notin Injection(S,T) PROVE FALSE\n BY Fun_IsBij\n<1>3. PICK a,b \\in S : a # b /\\ f[a] = f[b]\n BY <1>1, <1>2, Fun_IsInj\n<1>. DEFINE U == S \\ {b}\n<1>4. U \\in SUBSET S /\\ U # S\n OBVIOUS\n<1>. DEFINE g == [x \\in U |-> f[x]]\n<1>5. g \\in Surjection(U,T)\n <2>1. g \\in [U -> T] BY <1>1\n <2>2. ASSUME NEW t \\in T PROVE \\E u \\in U : g[u] = t\n <3>1. CASE t = f[b] BY <1>3, <3>1\n <3>2. CASE t # f[b]\n <4>1. PICK s \\in S : f[s] = t\n BY Fun_SurjectionProperties\n <4>2. s \\in U BY <3>2, <4>1\n <4>. QED BY <4>1, <4>2\n <3>3. QED BY <3>1, <3>2\n <2>3. QED BY <2>1, <2>2, Fun_IsSurj\n<1>. QED BY <1>4, <1>5\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Transitivity of injections, surjections, bijections. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_InjTransitive ==\n ASSUME NEW S, NEW T, NEW U,\n NEW F \\in Injection(S,T),\n NEW G \\in Injection(T,U)\n PROVE [s \\in S |-> G[F[s]]] \\in Injection(S,U)\nBY DEF Injection, IsInjective\n\n\nTHEOREM Fun_SurjTransitive ==\n ASSUME NEW S, NEW T, NEW U,\n NEW F \\in Surjection(S,T),\n NEW G \\in Surjection(T,U)\n PROVE [s \\in S |-> G[F[s]]] \\in Surjection(S,U)\nBY Zenon DEF Surjection\n\n\nTHEOREM Fun_BijTransitive ==\n ASSUME NEW S, NEW T, NEW U,\n NEW F \\in Bijection(S,T),\n NEW G \\in Bijection(T,U)\n PROVE [s \\in S |-> G[F[s]]] \\in Bijection(S,U)\nBY Fun_SurjTransitive, Fun_InjTransitive, Zenon DEF Bijection\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The inverse of a surjection is an injection and vice versa. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_SurjInverse ==\n ASSUME NEW S, NEW T, NEW f \\in Surjection(S,T)\n PROVE Inverse(f,S,T) \\in Injection(T,S)\nBY DEF Inverse, Surjection, Injection, IsInjective, Range\n\n\nTHEOREM Fun_InjInverse ==\n ASSUME NEW S, NEW T, NEW f \\in Injection(S,T), S = {} => T = {}\n PROVE Inverse(f,S,T) \\in Surjection(T,S)\n<1>. DEFINE g == Inverse(f,S,T)\n<1>1. f \\in [S -> T] BY DEF Injection, IsInjective\n<1>2. g \\in [T -> S] BY <1>1, Fun_InverseProperties, Zenon\n<1>3. ASSUME NEW s \\in S PROVE \\E t \\in T : g[t] = s\n <2>10. g[f[s]] = s BY DEF Inverse, Range, Injection, IsInjective\n <2>. QED BY <2>10, <1>1\n<1>. QED BY <1>2, <1>3 DEF Surjection\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Properties of the inverse of a bijection. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_BijInverse ==\n ASSUME NEW S, NEW T, NEW f \\in Bijection(S,T)\n PROVE /\\ Inverse(f,S,T) \\in Bijection(T,S)\n /\\ \\A s \\in S : Inverse(f,S,T)[f[s]] = s\n /\\ Inverse(Inverse(f,S,T), T,S) = f\n\n<1>. DEFINE g == Inverse(f,S,T)\n<1>1. f \\in [S -> T] BY DEF Bijection, Injection\n<1>2. f \\in Surjection(S,T) BY DEF Bijection\n<1>3. \\A a,b \\in S : f[a] = f[b] => a = b BY DEF Bijection, Injection, IsInjective\n<1>4. g \\in Injection(T,S) BY <1>2, Fun_SurjInverse\n\n<1>5. \\A t \\in T : f[g[t]] = t BY <1>2 DEF Surjection, Inverse, Range\n<1>6. \\A s \\in S : g[f[s]] = s BY <1>1, <1>3 DEF Inverse, Range\n\n<1>7. \\A a,b \\in T : g[a] = g[b] => a = b BY <1>5\n<1>8. \\A s \\in S : \\E t \\in T : g[t] = s BY <1>1, <1>6\n\n<1>9. g \\in Bijection(T,S) BY <1>4, <1>8 DEF Bijection, Injection, Surjection\n\n<1>10. Inverse(g,T,S) = f\n <2>1. ASSUME NEW s \\in S PROVE f[s] = CHOOSE t \\in T : s \\in Range(g) => g[t] = s\n <3>1. PICK a \\in T : g[a] = s BY <1>9 DEF Bijection, Surjection\n <3>2. \\A b \\in T : g[b] = s => a = b BY <3>1, <1>7\n <3>3. f[s] = a BY <3>1, <1>5\n <3>4. s \\in Range(g) BY <3>1, <1>4 DEF Injection, Range\n <3>. QED BY <3>1, <3>2, <3>3, <3>4\n <2>. QED BY <2>1, <1>1 DEF Inverse\n<1>. QED BY <1>9, <1>6, <1>10\n\n(***************************************************************************)\n(* We cannot conclude that a function f is a bijection if its inverse is *)\n(* a bijection, and the following is a counter-example. *)\n(* *)\n(* S = {1,2} T = {3,4} *)\n(* f = [x \\in S |-> 3] *)\n(* *)\n(* Then, Inverse(f,S,T) can very well be (3 :> 1) @@ (4 :> 2), which is a *)\n(* bijection although f is not. *)\n(***************************************************************************)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The restriction of a bijection is a bijection. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_BijRestrict ==\n ASSUME NEW S, NEW T, NEW F \\in Bijection(S,T),\n NEW R \\in SUBSET S\n PROVE Restrict(F, R) \\in Bijection(R, Range(Restrict(F, R)))\nBY DEF Bijection, Injection, IsInjective, Surjection, Range, Restrict\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Given F an injection from S to T, then F is a bijection from S to F(S). *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_InjMeansBijImage ==\n ASSUME NEW S, NEW T, NEW F \\in Injection(S,T)\n PROVE F \\in Bijection(S, Range(F))\nBY DEF Bijection, Injection, IsInjective, Surjection, Range\n\n\n\n\n-----------------------------------------------------------------------------\n(***************************************************************************)\n(* `^{\\large \\vspace{12pt} *)\n(* Facts about exists jections. *)\n(* \\vspace{12pt}}^' *)\n(***************************************************************************)\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Definitions restated as facts. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_ExistsInj ==\n \\A S,T : ExistsInjection(S,T) <=> Injection(S,T) # {}\nBY DEF ExistsInjection\n\n\nTHEOREM Fun_ExistsSurj ==\n \\A S,T : ExistsSurjection(S,T) <=> Surjection(S,T) # {}\nBY DEF ExistsSurjection\n\n\nTHEOREM Fun_ExistsBij ==\n \\A S,T : ExistsBijection(S,T) <=> Bijection(S,T) # {}\nBY DEF ExistsBijection\n\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* There is a surjection from any set S to any non-empty subset T of S. *)\n(* (Note that there cannot be a surjection to {} except if S is empty.) *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_ExistsSurjSubset ==\n ASSUME NEW S, NEW T \\in SUBSET S, T # {}\n PROVE ExistsSurjection(S,T)\n<1>. PICK x \\in T : TRUE OBVIOUS\n<1>. [s \\in S |-> IF s \\in T THEN s ELSE x] \\in Surjection(S,T)\n BY DEF Surjection\n<1>. QED BY DEF ExistsSurjection\n\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* If there is a surjection from S to T, then there is an injection from T *)\n(* to S. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_ExistsSurjMeansExistsRevInj ==\n ASSUME NEW S, NEW T, ExistsSurjection(S,T)\n PROVE ExistsInjection(T,S)\nBY Fun_SurjInverse DEF ExistsSurjection, ExistsInjection\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* ExistsBijection is reflexive, symmetric, and transitive. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_ExistsBijReflexive ==\n ASSUME NEW S\n PROVE ExistsBijection(S,S)\n<1>. [s \\in S |-> s] \\in Bijection(S,S) BY DEF Bijection, Injection, IsInjective, Surjection\n<1>. QED BY DEF ExistsBijection\n\n\nTHEOREM Fun_ExistsBijSymmetric ==\n ASSUME NEW S, NEW T, ExistsBijection(S,T)\n PROVE ExistsBijection(T,S)\nBY Fun_BijInverse DEF ExistsBijection\n\n\nTHEOREM Fun_ExistsBijTransitive ==\n ASSUME NEW S, NEW T, NEW U, ExistsBijection(S,T), ExistsBijection(T,U)\n PROVE ExistsBijection(S,U)\nBY Fun_BijTransitive, Zenon DEF ExistsBijection\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Existence of injections and surjections is reflexive and transitive. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_ExistsInjReflexive ==\n ASSUME NEW S\n PROVE ExistsInjection(S,S)\nBY Fun_ExistsBijReflexive DEF ExistsBijection, ExistsInjection, Bijection\n\n\nTHEOREM Fun_ExistsSurjReflexive ==\n ASSUME NEW S\n PROVE ExistsSurjection(S,S)\nBY Fun_ExistsBijReflexive DEF ExistsBijection, ExistsSurjection, Bijection\n\n\nTHEOREM Fun_ExistsInjTransitive ==\n ASSUME NEW S, NEW T, NEW U,\n ExistsInjection(S,T), ExistsInjection(T,U)\n PROVE ExistsInjection(S,U)\nBY Fun_InjTransitive, Zenon DEF ExistsInjection\n\n\nTHEOREM Fun_ExistsSurjTransitive ==\n ASSUME NEW S, NEW T, NEW U,\n ExistsSurjection(S,T), ExistsSurjection(T,U)\n PROVE ExistsSurjection(S,U)\nBY Fun_SurjTransitive, Zenon DEF ExistsSurjection\n\n\n-----------------------------------------------------------------------------\n(***************************************************************************)\n(* `^{\\large \\vspace{12pt} *)\n(* The Cantor-Bernstein-Schroeder theorem. *)\n(* \\vspace{12pt}}^' *)\n(***************************************************************************)\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* If there exists an injection from S to T, where T is a subset of S, *)\n(* then there exists a bijection from S to T. *)\n(* *)\n(* A lemma for the Cantor-Bernstein-Schroeder theorem. *)\n(* *)\n(* This proof is formalized from *)\n(* `^\\url{http://www.proofwiki.org/wiki/Cantor-Bernstein-Schroeder\\_Theorem/Lemma}^' *)\n(* retrieved April 29, 2013. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_CantorBernsteinSchroeder_Lemma ==\n ASSUME NEW S, NEW T, T \\subseteq S, ExistsInjection(S,T)\n PROVE ExistsBijection(S,T)\n<1> PICK F \\in Injection(S,T) : TRUE BY Fun_ExistsInj\n\n<1>1. /\\ F \\in [S -> T]\n /\\ \\A a,b \\in S : F[a] = F[b] => a = b\n BY Fun_InjectionProperties\n\n(*************************************************************************)\n(* Pick Y as S excluding T. *)\n(*************************************************************************)\n<1>2. PICK Y : Y = S \\ T BY Zenon\n\n(*************************************************************************)\n(* Define Ci[0] as Y, and Ci[i+1] as the image of Ci[i] under F. *)\n(*************************************************************************)\n<1> DEFINE Ci[i \\in Nat] ==\n IF i = 0 THEN Y ELSE {F[s] : s \\in Ci[i-1]}\n<1> HIDE DEF Ci\n\n<1>3. \\A i \\in Nat : Ci[i] = IF i = 0 THEN Y ELSE {F[s] : s \\in Ci[i-1]}\n (***********************************************************************)\n (* Use NatInductiveDef to prove that Ci equals its definition. *)\n (***********************************************************************)\n <2> DEFINE\n f0 == Y\n Def(v,i) == {F[s] : s \\in v}\n f == CHOOSE f : f = [i \\in Nat |-> IF i = 0 THEN f0 ELSE Def(f[i-1],i)]\n <2> SUFFICES \\A i \\in Nat : f[i] = IF i = 0 THEN f0 ELSE Def(f[i-1],i) BY DEF Ci\n <2> HIDE DEF f0, Def, f\n <2> SUFFICES NatInductiveDefConclusion(f,f0,Def) BY DEF NatInductiveDefConclusion\n <2> SUFFICES NatInductiveDefHypothesis(f,f0,Def) BY NatInductiveDef, Zenon\n <2> QED BY Zenon DEF NatInductiveDefHypothesis, f\n\n(*************************************************************************)\n(* Applying F to an element of Ci[i] produces an element of Ci[i+1]. *)\n(*************************************************************************)\n<1>4. ASSUME NEW i \\in Nat, NEW s \\in Ci[i]\n PROVE F[s] \\in Ci[i+1]\n BY <1>3\n\n(*************************************************************************)\n(* Each element of Ci[i+1] is the application of F to some element in *)\n(* Ci[i]. *)\n(*************************************************************************)\n<1>5. ASSUME NEW i \\in Nat, NEW t \\in Ci[i+1]\n PROVE \\E s \\in Ci[i] : F[s] = t\n <2>1. Ci[i+1] = {F[s] : s \\in Ci[i]}\n BY <1>3, i+1 \\in Nat \\ {0}, (i+1)-1 = i, Zenon\n <2>. QED BY <2>1\n\n(*************************************************************************)\n(* Each Ci[i] \\subseteq S. *)\n(*************************************************************************)\n<1>6. \\A i \\in Nat : Ci[i] \\subseteq S\n <2> DEFINE Prop(i) == Ci[i] \\subseteq S\n <2> SUFFICES \\A i \\in Nat : Prop(i) OBVIOUS\n <2>1. Prop(0) BY <1>2, <1>3, Zenon\n <2>2. ASSUME NEW i \\in Nat, Prop(i) PROVE Prop(i+1)\n <3> SUFFICES ASSUME NEW t \\in Ci[i+1] PROVE t \\in S OBVIOUS\n <3>1. PICK s \\in Ci[i] : F[s] = t BY <1>5\n <3>2. s \\in S BY <2>2\n <3> QED BY <3>1, <3>2, <1>1\n <2> HIDE DEF Prop\n <2> QED BY <2>1, <2>2, NatInduction, Isa\n\n(*************************************************************************)\n(* Pick C as the union of all Ci[i]. *)\n(*************************************************************************)\n<1>7. PICK C : C = UNION {Ci[i] : i \\in Nat} OBVIOUS\n<1>8. C \\subseteq S BY <1>6, <1>7\n\n(*************************************************************************)\n(* Pick FC as the image of C under F. *)\n(*************************************************************************)\n<1>9. PICK FC : FC = {F[c] : c \\in C} BY Zenon\n<1>10. FC \\subseteq T BY <1>1, <1>8, <1>9\n\n(*************************************************************************)\n(* C = Y \\cup FC because Ci[0] = Y and Ci[i+1] = image of Ci[i] under F. *)\n(*************************************************************************)\n<1>11. C = Y \\cup FC\n <2>1. ASSUME NEW c \\in C PROVE c \\in Y \\cup FC\n <3>1. PICK i \\in Nat : c \\in Ci[i] BY <1>7\n <3>2. CASE i = 0 BY <3>1, <3>2, <1>3, Zenon\n <3>3. CASE i > 0\n <4>0. i-1 \\in Nat\n BY <3>3\n <4>1. PICK s \\in Ci[i-1] : F[s] = c\n BY <4>0, <3>1, <1>5\n <4>2. s \\in C BY <3>3, <1>7\n <4> QED BY <4>1, <4>2, <1>9\n <3> QED BY <3>2, <3>3\n <2>2. ASSUME NEW c \\in Y \\cup FC PROVE c \\in C\n <3>1. CASE c \\in Y BY <3>1, <1>3, <1>7, Zenon\n <3>2. CASE c \\in FC\n <4>1. PICK s \\in C : F[s] = c BY <3>2, <1>9\n <4>2. PICK i \\in Nat : s \\in Ci[i] BY <4>1, <1>7\n <4>3. F[s] \\in Ci[i+1] BY <4>2, <1>4\n <4> QED BY <4>1, <4>3, <1>7\n <3> QED BY <3>1, <3>2\n <2> QED BY <2>1, <2>2\n\n(*************************************************************************)\n(* S \\ C is the same as T \\ FC. *)\n(*************************************************************************)\n<1>12. S \\ C = T \\ FC BY <1>2, <1>11\n\n(*************************************************************************)\n(* Pick H as F on C and the identity on S \\ C. Since F (restricted to *)\n(* C) is a bijection from C to FC and S \\ C = T \\ FC, this makes H a *)\n(* bijection from S to T. *)\n(*************************************************************************)\n<1>13. PICK H : H = [s \\in S |-> IF s \\in C THEN F[s] ELSE s] OBVIOUS\n<1>14. H \\in Bijection(S,T)\n (***********************************************************************)\n (* A useful lemma. If a \\in C and b \\notin C, then H[a] # H[b]. *)\n (***********************************************************************)\n <2>1. ASSUME NEW a \\in S, NEW b \\in S, a \\in C, b \\notin C PROVE H[a] # H[b]\n <3>1. H[a] \\in FC BY <2>1, <1>1, <1>9, <1>13\n <3>2. H[b] \\in T \\ FC BY <2>1, <1>12, <1>13\n <3> QED BY <3>1, <3>2\n\n <2>2. H \\in [S -> T]\n <3> SUFFICES ASSUME NEW s \\in S PROVE H[s] \\in T BY <1>13\n <3>1. CASE s \\in C BY <3>1, <1>1, <1>10, <1>13\n <3>2. CASE s \\notin C BY <3>2, <1>12, <1>13\n <3> QED BY <3>1, <3>2\n\n <2>3. ASSUME NEW a \\in S, NEW b \\in S, H[a] = H[b] PROVE a = b\n <3> H[a] = H[b] BY <2>3\n <3>1. CASE a \\in C /\\ b \\in C BY <3>1, <1>1, <1>13\n <3>2. CASE a \\in C /\\ b \\notin C BY <3>2, <2>1 (* impossible by lemma *)\n <3>3. CASE a \\notin C /\\ b \\in C BY <3>3, <2>1 (* impossible by lemma *)\n <3>4. CASE a \\notin C /\\ b \\notin C BY <3>4, <1>13\n <3> QED BY <3>1, <3>2, <3>3, <3>4\n\n <2>4. ASSUME NEW t \\in T PROVE \\E s \\in S : H[s] = t\n <3>1. CASE t \\in FC BY <3>1, <1>8, <1>9, <1>13\n <3>2. CASE t \\notin FC BY <3>2, <1>12, <1>13\n <3> QED BY <3>1, <3>2\n\n <2> QED BY <2>2, <2>3, <2>4, Fun_IsBij\n\n<1> QED BY <1>14, Fun_ExistsBij\n\n\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* If an injection exists from S to T and an injection exists from T to S, *)\n(* then there is a bijection from S to T. *)\n(* *)\n(* This is the Cantor-Bernstein-Schroeder theorem. *)\n(* *)\n(* This proof is formalized from *)\n(* `^\\url{http://www.proofwiki.org/wiki/Cantor-Bernstein-Schroeder_Theorem/Proof_5}^' *)\n(* retrieved April 29, 2013. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_CantorBernsteinSchroeder ==\n ASSUME NEW S, NEW T,\n ExistsInjection(S,T), ExistsInjection(T,S)\n PROVE ExistsBijection(S,T)\n\n<1>1. PICK F : F \\in Injection(S,T) BY DEF ExistsInjection\n<1>2. PICK G : G \\in Injection(T,S) BY DEF ExistsInjection\n<1>. DEFINE GF == [s \\in S |-> G[F[s]]]\n<1>3. Range(G) \\subseteq S BY <1>2, Fun_RangeProperties DEF Injection\n<1>4. GF \\in Injection(S, Range(G)) BY <1>1, <1>2 DEF Injection, IsInjective, Range\n<1>5. ExistsBijection(S, Range(G))\n BY <1>3, <1>4, Fun_CantorBernsteinSchroeder_Lemma DEF ExistsInjection\n<1>6. ExistsBijection(T, Range(G))\n BY <1>2, Fun_InjMeansBijImage DEF ExistsBijection\n<1>. QED BY <1>5, <1>6, Fun_ExistsBijSymmetric, Fun_ExistsBijTransitive\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Applications of the Cantor-Bernstein-Schroeder Theorem. *)\n(* If there exists an injection f: A->B and a surjection g: A->B, then *)\n(* there exists a bijection between A and B. *)\n(* Also, if there are surjections between A and B, then there is a *)\n(* bijection. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\n\nTHEOREM Fun_ExistInjAndSurjThenBij ==\n ASSUME NEW S, NEW T,\n ExistsInjection(S,T), ExistsSurjection(S,T)\n PROVE ExistsBijection(S,T)\n<1>. ExistsInjection(T,S) BY Fun_SurjInverse DEF ExistsInjection, ExistsSurjection\n<1>. QED BY Fun_CantorBernsteinSchroeder\n\n\n\nTHEOREM Fun_ExistSurjAndSurjThenBij ==\n ASSUME NEW S, NEW T,\n ExistsSurjection(S,T), ExistsSurjection(T,S)\n PROVE ExistsBijection(S,T)\n<1>. ExistsInjection(S,T) BY Fun_SurjInverse DEF ExistsInjection, ExistsSurjection\n<1>2. QED BY Fun_ExistInjAndSurjThenBij\n\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Equivalences for ExistsBijection. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_ExistsBijEquiv ==\n ASSUME NEW S, NEW T\n PROVE /\\ ExistsBijection(S,T) <=> ExistsBijection(T,S)\n /\\ ExistsBijection(S,T) <=> ExistsInjection(S,T) /\\ ExistsInjection(T,S)\n /\\ ExistsBijection(S,T) <=> ExistsInjection(S,T) /\\ ExistsSurjection(S,T)\n /\\ ExistsBijection(S,T) <=> ExistsInjection(T,S) /\\ ExistsSurjection(T,S)\n /\\ ExistsBijection(S,T) <=> ExistsSurjection(S,T) /\\ ExistsSurjection(T,S)\n\n<1>1. ExistsBijection(S,T) <=> ExistsBijection(T,S)\n BY Fun_ExistsBijSymmetric\n<1>2. ExistsInjection(S,T) /\\ ExistsInjection(T,S) => ExistsBijection(S,T)\n BY Fun_CantorBernsteinSchroeder\n<1>3. \\A S1, T1 : ExistsBijection(S1,T1) => ExistsSurjection(S1,T1)\n BY DEF ExistsBijection, ExistsSurjection, Bijection\n<1>4. \\A S1,T1 : ExistsSurjection(S1,T1) => ExistsInjection(T1,S1)\n BY Fun_ExistsSurjMeansExistsRevInj\n<1> QED BY <1>1, <1>2, <1>3, <1>4\n\n\n-----------------------------------------------------------------------------\n(***************************************************************************)\n(* `^{\\large \\vspace{12pt} *)\n(* Facts about jections involving 1..n. *)\n(* \\vspace{12pt}}^' *)\n(***************************************************************************)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* There is a bijection from 1..b-a+1 to a..b for integers a,b with a <= b.*)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_ExistsBijInterval ==\n ASSUME NEW a \\in Int, NEW b \\in Int, a <= b\n PROVE ExistsBijection(1 .. b-a+1, a .. b)\n\n<1>. DEFINE f == [i \\in 1 .. b-a+1 |-> i+a-1]\n<1>1. f \\in [1 .. b-a+1 -> a .. b] BY SMT\n<1>2. f \\in Injection(1 .. b-a+1, a .. b) BY DEF Injection, IsInjective\n<1>3. \\A t \\in a..b :\n /\\ t-a+1 \\in 1 .. (b-a+1)\n /\\ f[t-a+1] = t\n OBVIOUS\n<1>4. f \\in Surjection(1 .. b-a+1, a .. b)\n BY <1>3, <1>1, Zenon DEF Surjection\n<1>. QED BY <1>1, <1>2, <1>4 DEF ExistsBijection, Bijection\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* There is an injection from 1..n to 1..m iff n \\leq m. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_NatInjLeq ==\n ASSUME NEW n \\in Nat, NEW m \\in Nat\n PROVE ExistsInjection(1..n,1..m) <=> n \\leq m\n(*************************************************************************)\n(* n \\leq m means Injection exists. This part is easy. *)\n(*************************************************************************)\n<1>1. ASSUME n \\leq m PROVE [i \\in 1..n |-> i] \\in Injection(1..n, 1..m)\n BY <1>1 DEF Injection, IsInjective\n\n(*************************************************************************)\n(* Injection exists means n \\leq m. This part is harder. *)\n(*************************************************************************)\n<1>2. ASSUME ExistsInjection(1..n,1..m) PROVE n \\leq m\n <2>. DEFINE P(mm) == \\A nn \\in Nat : nn > mm => Injection(1..nn, 1..mm) = {}\n <2>1. SUFFICES \\A mm \\in Nat : P(mm)\n BY <1>2 DEF ExistsInjection\n <2>2. P(0)\n <3>. SUFFICES ASSUME NEW nn \\in Nat, nn > 0\n PROVE Injection(1..nn, 1..0) = {}\n OBVIOUS\n <3>1. /\\ 1 .. nn # {}\n /\\ 1 .. 0 = {}\n BY Isa\n <3>. QED BY <3>1 DEF Injection\n <2>3. ASSUME NEW mm \\in Nat, P(mm) PROVE P(mm+1)\n <3>1. SUFFICES ASSUME NEW nn \\in Nat, nn > mm+1,\n NEW f \\in Injection(1..nn, 1..mm+1)\n PROVE FALSE\n OBVIOUS\n <3>2. ASSUME NEW i \\in 1..nn, f[i] = mm+1 PROVE FALSE\n <4>. DEFINE g == [j \\in 1..nn-1 |-> IF j<i THEN f[j] ELSE f[j+1]]\n <4>1. nn-1 \\in Nat /\\ nn-1 > mm BY <3>1\n <4>2. g \\in Injection(1..nn-1, 1..mm) BY <3>2 DEF Injection, IsInjective\n <4>. QED BY <4>1, <4>2, P(mm), Zenon DEF Injection\n <3>3. ASSUME ~\\E i \\in 1..nn : f[i] = mm+1 PROVE FALSE\n <4>. f \\in Injection(1..nn, 1..mm) BY <3>3 DEF Injection\n <4>. QED BY <3>1, P(mm)\n <3>. QED BY <3>2, <3>3\n <2>. QED BY NatInduction, <2>2, <2>3, Isa\n\n<1> QED BY <1>1, <1>2 DEF ExistsInjection\n\n\n\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* If a surjection from 1..n to S exists (for some n \\in Nat) then a *)\n(* bijection from 1..m to S exists (for some m \\in Nat) and m \\leq n. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_NatSurjImpliesNatBij ==\n ASSUME NEW S, NEW n \\in Nat, ExistsSurjection(1..n,S)\n PROVE \\E m \\in Nat : ExistsBijection(1..m,S) /\\ m \\leq n\n\n (*************************************************************************)\n (* Pick the smallest m \\in Nat for which there is a surjection from *)\n (* 1..m to S. *)\n (*************************************************************************)\n<1>1. PICK m \\in Nat :\n /\\ ExistsSurjection(1..m, S)\n /\\ \\A k \\in Nat : k < m => ~ExistsSurjection(1..k, S)\n <2>. DEFINE NN == { m \\in Nat : ExistsSurjection(1..m, S) }\n <2>1. PICK m \\in NN : \\A k \\in NN : <<k, m>> \\notin OpToRel(<, Nat)\n BY WFMin, NatLessThanWellFounded, Zenon\n <2>. QED\n BY <2>1, Zenon DEF OpToRel\n\n<1>2. m <= n BY <1>1\n (*************************************************************************)\n (* Any surjection from 1..m to S is bijective. *)\n (*************************************************************************)\n<1>3. PICK f \\in Surjection(1..m, S) : TRUE BY <1>1 DEF ExistsSurjection\n<1>4. ASSUME f \\notin Injection(1..m, S) PROVE FALSE\n <2>1. f \\in [1..m -> S] BY <1>3 DEF Surjection\n <2>2. PICK i,j \\in 1..m : i < j /\\ f[i] = f[j]\n <3>1. PICK ii,jj \\in 1..m : ii # jj /\\ f[ii] = f[jj]\n BY <2>1, <1>4 DEF Injection, IsInjective\n <3>2. CASE ii < jj BY <3>1, <3>2\n <3>3. CASE jj < ii BY <3>1, <3>3\n <3>. QED BY SMT, <3>1, <3>2, <3>3\n <2>3. m-1 \\in Nat BY <2>2\n <2>. DEFINE g == [k \\in 1..m-1 |-> IF k=j THEN f[m] ELSE f[k]]\n <2>4. g \\in Surjection(1..m-1, S)\n <3>1. g \\in [1..m-1 -> S] BY <2>1\n <3>2. ASSUME NEW s \\in S PROVE \\E k \\in 1..m-1 : g[k] = s\n <4>. PICK l \\in 1..m : f[l] = s BY <1>3 DEF Surjection\n <4>1. CASE l = j\n BY <2>2, <4>1, i \\in 1 .. m-1\n <4>2. CASE l # j /\\ l = m\n BY <4>2, j \\in 1 .. m-1\n <4>3. CASE l # j /\\ l # m\n BY <4>3\n <4>. QED BY <4>1, <4>2, <4>3\n <3>. QED BY <3>1, <3>2, Zenon DEF Surjection\n <2>. QED BY SMT, <2>3, <2>4, <1>1 DEF ExistsSurjection\n\n<1>. QED BY <1>2, <1>3, <1>4 DEF ExistsBijection, Bijection\n\n\n(***************************************************************************)\n(* Simple corollary. *)\n(***************************************************************************)\nTHEOREM Fun_NatSurjEquivNatBij ==\n ASSUME NEW S\n PROVE (\\E n \\in Nat : ExistsSurjection(1..n,S))\n <=> (\\E m \\in Nat : ExistsBijection(1..m,S))\nBY Fun_NatSurjImpliesNatBij, Fun_ExistsBijEquiv\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* For any set S, given n, m \\in Nat such that bijections exist from 1..n *)\n(* to S and from 1..m to S, then it must be the case that n = m. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_NatBijSame ==\n ASSUME NEW S,\n NEW n \\in Nat, ExistsBijection(1..n,S),\n NEW m \\in Nat, ExistsBijection(1..m,S)\n PROVE n = m\nBY Fun_NatInjLeq, Fun_ExistsBijEquiv, Fun_ExistsBijTransitive\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* S is empty iff there exists a bijection from 1..0 to S. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_NatBijEmpty ==\n ASSUME NEW S\n PROVE ExistsBijection(1..0,S) <=> S = {}\n\n<1>1. ASSUME ExistsBijection(1..0, S), S # {} PROVE FALSE\n <2>. ExistsInjection(S, 1..0) BY <1>1, Fun_ExistsBijEquiv\n <2>. QED BY <1>1 DEF ExistsInjection, Injection\n<1>2. ASSUME S = {} PROVE ExistsBijection(1..0, S)\n BY <1>2, Fun_ExistsBijReflexive, Isa\n<1>3. QED BY <1>1, <1>2\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* S is a singleton iff there exists a bijection from 1..1 to S. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_NatBijSingleton ==\n ASSUME NEW S\n PROVE ExistsBijection(1..1,S) <=> \\E s : S = {s}\n<1>1. ASSUME NEW f \\in Bijection(1..1, S) PROVE \\E s : S = {s}\n <2>1. f \\in [1..1 -> S] /\\ \\A t \\in S : \\E i \\in 1..1 : f[i] = t\n BY DEF Bijection, Injection, Surjection\n <2>2. f[1] \\in S\n BY <2>1\n <2>3. \\A t \\in S : t = f[1]\n <3>1. TAKE t \\in S\n <3>2. PICK i \\in 1..1 : f[i] = t BY <2>1\n <3>. QED BY <3>2\n <2>. WITNESS f[1]\n <2>. QED BY <2>2, <2>3\n<1>2. ASSUME NEW s, S = {s} PROVE [i \\in 1..1 |-> s] \\in Bijection(1..1, S)\n BY <1>2 DEF Bijection, Injection, IsInjective, Surjection\n<1>. QED BY <1>1, <1>2 DEF ExistsBijection\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* If there exists a bijection from 1..m to S (for some m \\in Nat), then *)\n(* there exists a bijection from 1..n to T (for some n \\in Nat), where T *)\n(* is a subset of S. Furthermore n \\leq m. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_NatBijSubset ==\n ASSUME NEW S, NEW m \\in Nat, ExistsBijection(1..m,S),\n NEW T \\in SUBSET S\n PROVE \\E n \\in Nat : ExistsBijection(1..n,T) /\\ n \\leq m\n\n<1>1. CASE T = {} BY <1>1, Fun_NatBijEmpty\n<1>2. CASE T # {}\n <2>0. ExistsSurjection(1..m, S) BY Fun_ExistsBijEquiv\n <2>1. ExistsSurjection(S, T) BY <1>2, Fun_ExistsSurjSubset\n <2>2. ExistsSurjection(1..m, T) BY <2>0, <2>1, Fun_ExistsSurjTransitive\n <2>. QED BY <2>2, Fun_NatSurjImpliesNatBij\n<1> QED BY <1>1, <1>2\n\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* If there exists a bijection from 1..m to S (for some m \\in Nat), then *)\n(* there exists a bijection from 1..(m+1) to S \\cup {x}, where x \\notin S. *)\n(* *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_NatBijAddElem ==\n ASSUME NEW S, NEW m \\in Nat, ExistsBijection(1..m,S),\n NEW x, x \\notin S\n PROVE ExistsBijection(1..(m+1), S \\cup {x})\n\n<1>1. PICK F \\in Bijection(1..m, S) : TRUE BY DEF ExistsBijection\n<1>2. F \\in [1..m -> S] BY <1>1 DEF Bijection, Injection\n<1>3. \\A s \\in S : \\E i \\in 1..m : F[i] = s BY <1>1 DEF Bijection, Surjection\n<1>4. \\A i,j \\in 1..m : F[i] = F[j] => i = j BY <1>1 DEF Bijection, Injection, IsInjective\n\n<1>. DEFINE G == [i \\in 1..m+1 |-> IF i <= m THEN F[i] ELSE x]\n<1>10. G \\in [1..m+1 -> S \\cup {x}] BY SMT, <1>2\n<1>20. ASSUME NEW t \\in S \\cup {x} PROVE \\E i \\in 1..m+1 : G[i] = t\n <2>1. CASE t \\in S \n BY <1>3, <2>1, 1..m \\subseteq 1 .. m+1\n <2>2. CASE t = x\n BY <2>2, m+1 \\in 1 .. m+1\n <2>. QED BY <2>1, <2>2\n<1>30. ASSUME NEW i \\in 1..m+1, NEW j \\in 1..m+1, G[i] = G[j] PROVE i = j\n BY <1>2, <1>4, <1>30\n<1>40. G \\in Bijection(1..m+1, S \\cup {x})\n BY <1>10, <1>20, <1>30, Zenon DEF Bijection, Injection, IsInjective, Surjection\n<1>. QED BY <1>40, Zenon DEF ExistsBijection\n\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* If there exists a bijection from 1..m to S (for some m \\in Nat), then *)\n(* there exists a bijection from 1..(m-1) to S \\ {x}, where x \\in S. *)\n(* *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM Fun_NatBijSubElem ==\n ASSUME NEW S, NEW m \\in Nat, ExistsBijection(1..m,S),\n NEW x, x \\in S\n PROVE ExistsBijection(1..(m-1), S \\ {x})\n\n<1>1. PICK n \\in Nat : ExistsBijection(1..n, S \\ {x}) BY Fun_NatBijSubset, Zenon\n<1>2. ExistsBijection(1..n+1, (S \\ {x}) \\cup {x}) BY <1>1, Fun_NatBijAddElem, Zenon\n<1>3. ExistsBijection(1..n+1, S) BY <1>2, Zenon\n<1>4. n = m-1 BY <1>3, Fun_NatBijSame\n<1>. QED BY <1>1, <1>4\n\n(***************************************************************************)\n(* Folding a function for an empty set of indices yields the base value. *)\n(***************************************************************************)\nTHEOREM FoldFunctionOnSetEmpty ==\n ASSUME NEW op(_,_), NEW base, NEW fun\n PROVE FoldFunctionOnSet(op, base, fun, {}) = base \nBY MapThenFoldSetEmpty, Zenon DEF FoldFunctionOnSet\n\n(***************************************************************************)\n(* Folding a function for a non-empty set of indices corresponds to *)\n(* applying the binary operator to f[i], for some i \\in indices, and the *)\n(* result of recursing for the set indices \\ {i}. *)\n(***************************************************************************)\nTHEOREM FoldFunctionOnSetNonempty ==\n ASSUME NEW op(_,_), NEW base, NEW fun, \n NEW indices, indices # {}, IsFiniteSet(indices)\n PROVE \\E i \\in indices : FoldFunctionOnSet(op, base, fun, indices) = \n op(fun[i], FoldFunctionOnSet(op, base, fun, indices \\ {i}))\n<1>1. \\A T : T # {} => (CHOOSE t \\in T : TRUE) \\in T \n OBVIOUS\n<1>. QED BY <1>1, MapThenFoldSetNonempty, Isa DEF FoldFunctionOnSet\n\n(***************************************************************************)\n(* The type of folding a function corresponds to the type associated with *)\n(* the binary operator that underlies the fold. *)\n(***************************************************************************)\nTHEOREM FoldFunctionOnSetType ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ,\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n NEW indices, IsFiniteSet(indices), NEW fun,\n \\A i \\in indices : fun[i] \\in Typ\n PROVE FoldFunctionOnSet(op, base, fun, indices) \\in Typ\n<1>1. \\A T : T # {} => (CHOOSE t \\in T : TRUE) \\in T \n OBVIOUS\n<1>. QED BY <1>1, MapThenFoldSetType, Isa DEF FoldFunctionOnSet\n\n(***************************************************************************)\n(* Folding two functions that agree on every element of the set of indices *)\n(* yields the same value. *)\n(***************************************************************************)\nTHEOREM FoldFunctionOnSetEqual ==\n ASSUME NEW op(_,_), NEW base, NEW f, NEW g,\n NEW indices, IsFiniteSet(indices),\n \\A i \\in indices : f[i] = g[i]\n PROVE FoldFunctionOnSet(op, base, f, indices) = \n FoldFunctionOnSet(op, base, g, indices)\n<1>1. \\A T : T # {} => (CHOOSE t \\in T : TRUE) \\in T \n OBVIOUS\n<1>. QED BY <1>1, MapThenFoldSetEqual, Isa DEF FoldFunctionOnSet\n\n(***************************************************************************)\n(* If the binary operator is associative and commutative, the result of *)\n(* folding a function over a non-empty set corresponds to applying the *)\n(* operator to an arbitrary element of the set and the result of folding *)\n(* the function over the remainder of the set. *)\n(***************************************************************************)\nTHEOREM FoldFunctionOnSetAC ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ,\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n NEW indices, IsFiniteSet(indices), NEW i \\in indices, NEW fun,\n \\A j \\in indices : fun[j] \\in Typ\n PROVE FoldFunctionOnSet(op, base, fun, indices) =\n op(fun[i], FoldFunctionOnSet(op, base, fun, indices \\ {i}))\n<1>1. \\A T : T # {} => (CHOOSE t \\in T : TRUE) \\in T \n OBVIOUS\n<1>. QED BY <1>1, MapThenFoldSetAC, Isa DEF FoldFunctionOnSet\n\n\\* reformulation in terms of adding an index to the set\nTHEOREM FoldFunctionOnSetACAddIndex ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ,\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n NEW indices, IsFiniteSet(indices), NEW i, i \\notin indices,\n NEW fun, \\A j \\in indices \\union {i} : fun[j] \\in Typ\n PROVE FoldFunctionOnSet(op, base, fun, indices \\union {i}) =\n op(fun[i], FoldFunctionOnSet(op, base, fun, indices))\n<1>1. \\A T : T # {} => (CHOOSE t \\in T : TRUE) \\in T \n OBVIOUS\n<1>. QED BY <1>1, MapThenFoldSetACAddElement, Isa DEF FoldFunctionOnSet\n\n(*************************************************************************)\n(* For an AC operator, folding an EXCEPT construction can be reduced to *)\n(* folding the original function. *)\n(*************************************************************************)\nTHEOREM FoldFunctionOnSetACExcept ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ,\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n NEW fun, NEW i, NEW y \\in Typ,\n NEW indices \\in SUBSET (DOMAIN fun), IsFiniteSet(indices), \n \\A j \\in indices : fun[j] \\in Typ \n PROVE FoldFunctionOnSet(op, base, [fun EXCEPT ![i] = y], indices) =\n IF i \\in indices\n THEN op(y, FoldFunctionOnSet(op, base, fun, indices \\ {i}))\n ELSE FoldFunctionOnSet(op, base, fun, indices)\n<1>. DEFINE g == [fun EXCEPT ![i] = y]\n<1>1. \\A j \\in indices \\ {i} : g[j] = fun[j]\n OBVIOUS \n<1>2. IsFiniteSet(indices \\ {i})\n BY FS_RemoveElement\n<1>3. CASE i \\in indices\n <2>1. \\A j \\in indices : g[j] \\in Typ \n BY <1>3\n <2>2. FoldFunctionOnSet(op, base, g, indices) =\n op(g[i], FoldFunctionOnSet(op, base, g, indices \\ {i}))\n BY <1>3, <2>1, FoldFunctionOnSetAC, Isa \n <2>. QED BY <1>1, <1>2, <1>3, <2>2, FoldFunctionOnSetEqual, Isa\n<1>4. CASE i \\notin indices\n <2>. QED BY <1>1, <1>2, <1>4, indices = indices \\ {i}, FoldFunctionOnSetEqual, Isa\n<1>. QED BY <1>3, <1>4\n\n(*************************************************************************)\n(* For an AC operator `op` for which `base` is the neutral element, *)\n(* folding a function commutes with set union, for disjoint sets. *)\n(*************************************************************************)\nTHEOREM FoldFunctionOnSetDisjointUnion ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ,\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n \\A t \\in Typ : op(base, t) = t,\n NEW S, IsFiniteSet(S), NEW T, IsFiniteSet(T), S \\cap T = {},\n NEW fun, \\A i \\in S \\union T : fun[i] \\in Typ\n PROVE FoldFunctionOnSet(op, base, fun, S \\union T) =\n op(FoldFunctionOnSet(op, base, fun, S), FoldFunctionOnSet(op, base, fun, T))\n<1>1. \\A U : U # {} => (CHOOSE u \\in U : TRUE) \\in U \n OBVIOUS\n<1>. QED BY <1>1, MapThenFoldSetDisjointUnion, Isa DEF FoldFunctionOnSet\n\n(*************************************************************************)\n(* Analogous theorems for FoldFunction. Note that the theorems about *)\n(* adding an index and about disjoint union do not make sense here. *)\n(*************************************************************************)\nTHEOREM FoldFunctionEmpty ==\n ASSUME NEW op(_,_), NEW base, NEW fun, DOMAIN fun = {}\n PROVE FoldFunction(op, base, fun) = base \nBY FoldFunctionOnSetEmpty, Zenon DEF FoldFunction\n\nTHEOREM FoldFunctionNonempty ==\n ASSUME NEW op(_,_), NEW base, NEW fun, \n DOMAIN fun # {}, IsFiniteSet(DOMAIN fun)\n PROVE \\E i \\in DOMAIN fun : FoldFunction(op, base, fun) = \n op(fun[i], FoldFunctionOnSet(op, base, fun, DOMAIN fun \\ {i}))\nBY FoldFunctionOnSetNonempty, Isa DEF FoldFunction\n\nTHEOREM FoldFunctionType ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ,\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n NEW fun, IsFiniteSet(DOMAIN fun),\n \\A i \\in DOMAIN fun : fun[i] \\in Typ\n PROVE FoldFunction(op, base, fun) \\in Typ\nBY FoldFunctionOnSetType, Isa DEF FoldFunction \n\nTHEOREM FoldFunctionAC ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ,\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n NEW fun, IsFiniteSet(DOMAIN fun), NEW i \\in DOMAIN fun,\n \\A j \\in DOMAIN fun : fun[j] \\in Typ\n PROVE FoldFunction(op, base, fun) =\n op(fun[i], FoldFunctionOnSet(op, base, fun, (DOMAIN fun) \\ {i}))\nBY FoldFunctionOnSetAC, Isa DEF FoldFunction \n\nTHEOREM FoldFunctionACExcept ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ,\n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u \\in Typ : op(t,u) = op(u,t),\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u),v),\n NEW fun, NEW i, NEW y \\in Typ,\n IsFiniteSet(DOMAIN fun), \n \\A j \\in DOMAIN fun : fun[j] \\in Typ \n PROVE FoldFunction(op, base, [fun EXCEPT ![i] = y]) =\n IF i \\in DOMAIN fun\n THEN op(y, FoldFunctionOnSet(op, base, fun, (DOMAIN fun) \\ {i}))\n ELSE FoldFunction(op, base, fun)\nBY FoldFunctionOnSetACExcept, Isa DEF FoldFunction \n\n(*************************************************************************)\n(* Summing a function that returns natural numbers, resp. integers, *)\n(* yields a natural number, resp. an integer. *)\n(*************************************************************************)\nLEMMA IntegersAC ==\n /\\ 0 \\in Int \n /\\ \\A x,y \\in Int : x+y \\in Int \n /\\ \\A x,y \\in Int : x+y = y+x\n /\\ \\A x,y,z \\in Int : x + (y+z) = (x+y) + z\n /\\ \\A x \\in Int : 0 + x = x\nOBVIOUS\n\nTHEOREM SumFunctionOnSetNat ==\n ASSUME NEW fun, NEW indices, IsFiniteSet(indices),\n \\A i \\in indices : fun[i] \\in Nat \n PROVE SumFunctionOnSet(fun, indices) \\in Nat\n<1>. /\\ 0 \\in Nat \n /\\ \\A x,y \\in Nat : x + y \\in Nat \n OBVIOUS\n<1>. QED BY FoldFunctionOnSetType, IsaM(\"iprover\") DEF SumFunctionOnSet\n\nTHEOREM SumFunctionOnSetInt ==\n ASSUME NEW fun, NEW indices, IsFiniteSet(indices),\n \\A i \\in indices : fun[i] \\in Int\n PROVE SumFunctionOnSet(fun, indices) \\in Int\nBY FoldFunctionOnSetType, IntegersAC, IsaM(\"iprover\") DEF SumFunctionOnSet\n\n(*************************************************************************)\n(* Summing two functions that agree on all relevant indices yields the *)\n(* same result. *)\n(*************************************************************************)\nTHEOREM SumFunctionOnSetEqual ==\n ASSUME NEW f, NEW g, NEW indices, IsFiniteSet(indices),\n \\A i \\in indices : f[i] = g[i]\n PROVE SumFunctionOnSet(f, indices) = SumFunctionOnSet(g, indices)\nBY FoldFunctionOnSetEqual, Zenon DEF SumFunctionOnSet \n\n(*************************************************************************)\n(* Summing a function over the empty set is 0. *)\n(*************************************************************************)\nTHEOREM SumFunctionOnSetEmpty ==\n ASSUME NEW fun \n PROVE SumFunctionOnSet(fun, {}) = 0\nBY FoldFunctionOnSetEmpty, Zenon DEF SumFunctionOnSet\n\n(*************************************************************************)\n(* Summing a function over a non-empty set corresponds to the sum of *)\n(* element of the set and the sum of the function over the remainder. *)\n(*************************************************************************)\nTHEOREM SumFunctionOnSetNonempty ==\n ASSUME NEW fun, NEW indices, IsFiniteSet(indices), NEW i \\in indices,\n \\A j \\in indices : fun[j] \\in Int\n PROVE SumFunctionOnSet(fun, indices) = \n fun[i] + SumFunctionOnSet(fun, indices \\ {i})\n BY FoldFunctionOnSetAC, IntegersAC, IsaM(\"iprover\") DEF SumFunctionOnSet \n\n\\* reformulation in terms of adding an index\nTHEOREM SumFunctionOnSetAddIndex ==\n ASSUME NEW fun, NEW indices, IsFiniteSet(indices), \n NEW i, i \\notin indices,\n \\A j \\in indices \\union {i} : fun[j] \\in Int\n PROVE SumFunctionOnSet(fun, indices \\union {i}) = \n fun[i] + SumFunctionOnSet(fun, indices)\nBY FoldFunctionOnSetACAddIndex, IntegersAC, IsaM(\"iprover\") DEF SumFunctionOnSet \n\n\\* corresponding lemma for removing an index\nTHEOREM SumFunctionOnSetRemoveIndex ==\n ASSUME NEW fun, NEW indices, IsFiniteSet(indices), NEW i \\in indices,\n \\A j \\in indices : fun[j] \\in Int\n PROVE SumFunctionOnSet(fun, indices \\ {i}) = \n SumFunctionOnSet(fun, indices) - fun[i]\n<1>. IsFiniteSet(indices \\ {i})\n BY FS_RemoveElement\n<1>. QED BY SumFunctionOnSetNonempty, SumFunctionOnSetInt\n\n(*************************************************************************)\n(* Reduce the sum of an EXCEPT to the sum of the original function. *)\n(*************************************************************************)\nTHEOREM SumFunctionOnSetExcept ==\n ASSUME NEW fun, NEW i, NEW y \\in Int,\n NEW indices \\in SUBSET (DOMAIN fun), IsFiniteSet(indices),\n \\A j \\in indices : fun[j] \\in Int \n PROVE SumFunctionOnSet([fun EXCEPT ![i] = y], indices) =\n IF i \\in indices\n THEN SumFunctionOnSet(fun, indices) + y - fun[i]\n ELSE SumFunctionOnSet(fun, indices)\n<1>1. SumFunctionOnSet([fun EXCEPT ![i] = y], indices) =\n IF i \\in indices\n THEN y + SumFunctionOnSet(fun, indices \\ {i})\n ELSE SumFunctionOnSet(fun, indices)\n BY FoldFunctionOnSetACExcept, IntegersAC, IsaM(\"iprover\") DEF SumFunctionOnSet \n<1>2. i \\in indices => \n SumFunctionOnSet(fun, indices \\ {i}) = SumFunctionOnSet(fun, indices) - fun[i]\n BY SumFunctionOnSetRemoveIndex\n<1>3. SumFunctionOnSet(fun, indices) \\in Int \n BY SumFunctionOnSetInt\n<1>. QED BY <1>1, <1>2, <1>3\n\n(*************************************************************************)\n(* Summing a function distributes over disjoint union. *)\n(*************************************************************************)\nTHEOREM SumFunctionOnSetDisjointUnion ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW T, IsFiniteSet(T), S \\cap T = {},\n NEW fun, \\A x \\in S \\union T : fun[x] \\in Int \n PROVE SumFunctionOnSet(fun, S \\union T) =\n SumFunctionOnSet(fun, S) + SumFunctionOnSet(fun, T)\nBY FoldFunctionOnSetDisjointUnion, IntegersAC, IsaM(\"iprover\") DEF SumFunctionOnSet \n\n(*************************************************************************)\n(* Summing a Nat-valued function is monotonic in the subset relation. *)\n(*************************************************************************)\nTHEOREM SumFunctionNatOnSubset ==\n ASSUME NEW S, IsFiniteSet(S), NEW T \\in SUBSET S,\n NEW fun, \\A s \\in S : fun[s] \\in Nat\n PROVE SumFunctionOnSet(fun, T) <= SumFunctionOnSet(fun, S)\n<1>. DEFINE U == S \\ T \n<1>1. /\\ IsFiniteSet(T)\n /\\ IsFiniteSet(U)\n /\\ T \\cap U = {}\n /\\ S = T \\union U\n /\\ \\A x \\in T \\union U : fun[x] \\in Int \n BY FS_Subset\n<1>2. SumFunctionOnSet(fun, S) = SumFunctionOnSet(fun, T) + SumFunctionOnSet(fun, U)\n BY <1>1, SumFunctionOnSetDisjointUnion\n<1>3. /\\ SumFunctionOnSet(fun, T) \\in Nat \n /\\ SumFunctionOnSet(fun, U) \\in Nat \n BY <1>1, SumFunctionOnSetNat\n<1>. QED BY <1>2, <1>3\n\n(*************************************************************************)\n(* The sum of a Nat-valued function is zero iff all relevant function *)\n(* values are zero. *)\n(*************************************************************************)\nTHEOREM SumFunctionOnSetZero ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW fun, \\A x \\in S : fun[x] \\in Nat\n PROVE SumFunctionOnSet(fun, S) = 0 <=> \\A x \\in S : fun[x] = 0\n<1>. DEFINE P(T) == SumFunctionOnSet(fun, T) = 0 <=> \\A x \\in T : fun[x] = 0\n<1>1. P({})\n BY SumFunctionOnSetEmpty\n<1>2. ASSUME NEW T \\in SUBSET S, IsFiniteSet(T), P(T), NEW x \\in S \\ T \n PROVE P(T \\union {x})\n <2>1. SumFunctionOnSet(fun, T \\union {x}) = fun[x] + SumFunctionOnSet(fun, T)\n BY <1>2, SumFunctionOnSetAddIndex\n <2>2. SumFunctionOnSet(fun, T) \\in Nat\n BY <1>2, SumFunctionOnSetNat\n <2>. QED BY <1>2, <2>1, <2>2\n<1>. QED\n <2>. HIDE DEF P \n <2>. P(S) BY <1>1, <1>2, FS_Induction, IsaM(\"iprover\")\n <2>. QED BY DEF P\n\n(*************************************************************************)\n(* Given any Int-valued function f and finite set S such that f[s] = c *)\n(* for all s \\in S, summing f over S yields c multiplied by the *)\n(* cardinality of S. *)\n(*************************************************************************)\nTHEOREM SumFunctionOnSetConst ==\n ASSUME NEW D, NEW S \\in SUBSET D, IsFiniteSet(S),\n NEW f \\in [D -> Int], NEW c \\in Int, \\A x \\in S : f[x] = c \n PROVE SumFunctionOnSet(f, S) = c * Cardinality(S)\n<1>. DEFINE P(T) == SumFunctionOnSet(f, T) = c * Cardinality(T)\n<1>1. P({})\n <2>1. SumFunctionOnSet(f, {}) = 0\n BY SumFunctionOnSetEmpty\n <2>2. Cardinality({}) = 0\n BY FS_EmptySet\n <2>. QED BY <2>1, <2>2\n<1>2. ASSUME NEW T \\in SUBSET S, IsFiniteSet(T), P(T), NEW x \\in S \\ T\n PROVE P(T \\union {x})\n <2>1. SumFunctionOnSet(f, T \\union {x}) = c + SumFunctionOnSet(f, T)\n BY <1>2, SumFunctionOnSetAddIndex\n <2>2. Cardinality(T \\union {x}) = Cardinality(T) + 1\n BY <1>2, FS_AddElement\n <2>3. Cardinality(T) \\in Nat \n BY <1>2, FS_CardinalityType\n <2>. QED BY <1>2, <2>1, <2>2, <2>3\n<1>. QED BY <1>1, <1>2, FS_Induction, IsaM(\"iprover\")\n\n(*************************************************************************)\n(* Summing a function is monotonic in the function argument. *)\n(*************************************************************************)\nTHEOREM SumFunctionOnSetMonotonic ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW f, \\A s \\in S : f[s] \\in Int,\n NEW g, \\A s \\in S : g[s] \\in Int,\n \\A s \\in S : f[s] <= g[s]\n PROVE SumFunctionOnSet(f, S) <= SumFunctionOnSet(g, S)\n<1>. DEFINE P(T) == SumFunctionOnSet(f, T) <= SumFunctionOnSet(g, T)\n<1>1. P({})\n BY SumFunctionOnSetEmpty\n<1>2. ASSUME NEW T \\in SUBSET S, IsFiniteSet(T), P(T), NEW x \\in S \\ T \n PROVE P(T \\union {x})\n <2>1. /\\ SumFunctionOnSet(f, T \\union {x}) = f[x] + SumFunctionOnSet(f, T)\n /\\ SumFunctionOnSet(g, T \\union {x}) = g[x] + SumFunctionOnSet(g, T)\n BY <1>2, SumFunctionOnSetAddIndex\n <2>2. /\\ SumFunctionOnSet(f, T) \\in Int \n /\\ SumFunctionOnSet(g, T) \\in Int \n BY <1>2, SumFunctionOnSetInt\n <2>. QED BY <1>2, <2>1, <2>2\n<1>. QED\n <2>. HIDE DEF P \n <2>. P(S) BY <1>1, <1>2, FS_Induction, IsaM(\"iprover\")\n <2>. QED BY DEF P\n\n(*************************************************************************)\n(* The sum over function f is strictly smaller than the sum over g if *)\n(* f[x] <= g[x] for all x \\in S and f[x] < g[s] holds for at least some *)\n(* element s \\in S. *)\n(*************************************************************************)\nTHEOREM SumFunctionOnSetStrictlyMonotonic ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW f, \\A s \\in S : f[s] \\in Int,\n NEW g, \\A s \\in S : g[s] \\in Int,\n \\A x \\in S : f[x] <= g[x],\n NEW s \\in S, f[s] < g[s]\n PROVE SumFunctionOnSet(f, S) < SumFunctionOnSet(g, S)\n<1>1. /\\ SumFunctionOnSet(f, S) = f[s] + SumFunctionOnSet(f, S \\ {s})\n /\\ SumFunctionOnSet(g, S) = g[s] + SumFunctionOnSet(g, S \\ {s})\n BY SumFunctionOnSetNonempty\n<1>2. IsFiniteSet(S \\ {s})\n BY FS_RemoveElement\n<1>3. SumFunctionOnSet(f, S \\ {s}) <= SumFunctionOnSet(g, S \\ {s})\n BY <1>2, SumFunctionOnSetMonotonic\n<1>4. /\\ SumFunctionOnSet(f, S \\ {s}) \\in Int\n /\\ SumFunctionOnSet(g, S \\ {s}) \\in Int\n BY <1>2, SumFunctionOnSetInt\n<1>. QED BY <1>1, <1>3, <1>4\n\n(*************************************************************************)\n(* Similar theorems for SumFunction. *)\n(*************************************************************************)\nTHEOREM SumFunctionNat ==\n ASSUME NEW fun, IsFiniteSet(DOMAIN fun),\n \\A x \\in DOMAIN fun : fun[x] \\in Nat \n PROVE SumFunction(fun) \\in Nat\nBY SumFunctionOnSetNat DEF SumFunction \n\nTHEOREM SumFunctionInt ==\n ASSUME NEW fun, IsFiniteSet(DOMAIN fun),\n \\A x \\in DOMAIN fun : fun[x] \\in Int \n PROVE SumFunction(fun) \\in Int \nBY SumFunctionOnSetInt DEF SumFunction \n\nTHEOREM SumFunctionEmpty ==\n ASSUME NEW fun, DOMAIN fun = {}\n PROVE SumFunction(fun) = 0\nBY SumFunctionOnSetEmpty DEF SumFunction \n\nTHEOREM SumFunctionNonempty ==\n ASSUME NEW fun, IsFiniteSet(DOMAIN fun), NEW x \\in DOMAIN fun,\n \\A i \\in DOMAIN fun : fun[i] \\in Int\n PROVE SumFunction(fun) = fun[x] + SumFunctionOnSet(fun, (DOMAIN fun) \\ {x})\nBY SumFunctionOnSetNonempty DEF SumFunction\n\nTHEOREM SumFunctionExcept ==\n ASSUME NEW fun, NEW i, NEW y \\in Int, IsFiniteSet(DOMAIN fun),\n \\A x \\in DOMAIN fun : fun[x] \\in Int \n PROVE SumFunction([fun EXCEPT ![i] = y]) =\n IF i \\in DOMAIN fun \n THEN SumFunction(fun) + y - fun[i]\n ELSE SumFunction(fun)\nBY SumFunctionOnSetExcept DEF SumFunction \n\nTHEOREM SumFunctionZero ==\n ASSUME NEW fun, IsFiniteSet(DOMAIN fun), \n \\A x \\in DOMAIN fun : fun[x] \\in Nat\n PROVE SumFunction(fun) = 0 <=> \\A x \\in DOMAIN fun : fun[x] = 0\nBY SumFunctionOnSetZero DEF SumFunction \n\nTHEOREM SumFunctionConst ==\n ASSUME NEW S, IsFiniteSet(S), NEW c \\in Int\n PROVE SumFunction([s \\in S |-> c]) = c * Cardinality(S)\nBY SumFunctionOnSetConst DEF SumFunction\n\nTHEOREM SumFunctionMonotonic ==\n ASSUME NEW f, IsFiniteSet(DOMAIN f), NEW g, DOMAIN g = DOMAIN f,\n \\A x \\in DOMAIN f : f[x] \\in Int,\n \\A x \\in DOMAIN f : g[x] \\in Int,\n \\A x \\in DOMAIN f : f[x] <= g[x]\n PROVE SumFunction(f) <= SumFunction(g)\nBY SumFunctionOnSetMonotonic DEF SumFunction \n\nTHEOREM SumFunctionStrictlyMonotonic ==\n ASSUME NEW f, IsFiniteSet(DOMAIN f), NEW g, DOMAIN g = DOMAIN f,\n \\A x \\in DOMAIN f : f[x] \\in Int,\n \\A x \\in DOMAIN f : g[x] \\in Int,\n \\A x \\in DOMAIN f : f[x] <= g[x],\n NEW s \\in DOMAIN f, f[s] < g[s]\n PROVE SumFunction(f) < SumFunction(g)\nBY SumFunctionOnSetStrictlyMonotonic DEF SumFunction \n\n=============================================================================\n\\* Created Thu Apr 11 10:36:10 PDT 2013 by tomr\n", "Functions": "------------------------------ MODULE Functions -----------------------------\n(***************************************************************************)\n(* `^{\\large\\bf \\vspace{12pt} *)\n(* Notions about functions including injection, surjection, and bijection.*)\n(* Originally contributed by Tom Rodeheffer, MSR. *)\n(* \\vspace{12pt}}^' *)\n(***************************************************************************)\n\nEXTENDS Integers, Folds\n\n(***************************************************************************)\n(* Restriction of a function to a set (should be a subset of the domain). *)\n(***************************************************************************)\nRestrict(f,S) == [ x \\in S |-> f[x] ]\n\n(***************************************************************************)\n(* Restriction of a function to the subset of its domain satisfying a *)\n(* test predicate. *)\n(* *)\n(* Example: *)\n(* (LET f == (0 :> \"a\" @@ 1 :> \"b\" @@ 2 :> \"c\") *)\n(* IN RestrictDomain(f, LAMBDA x : x \\in {0,2})) *)\n(* = (0 :> \"a\" @@ 2 :> \"c\") *)\n(***************************************************************************)\nRestrictDomain(f, Test(_)) == Restrict(f, {x \\in DOMAIN f : Test(x)})\n\n(***************************************************************************)\n(* Restriction of a function to the subset of its domain for which the *)\n(* function values satisfy a test predicate. *)\n(* *)\n(* Example: *)\n(* (LET f == (\"a\" :> 0 @@ \"b\" :> 1 @@ \"c\" :> 2) *)\n(* IN RestrictValues(f, LAMBDA y : y \\in {0,2})) *)\n(* = (\"a\" :> 0 @@ \"b\" :> 2) *)\n(* *)\n(* This is similar to the operator SelectSeq from the standard Sequences *)\n(* module and related to standard \"filter\" functions in functional *)\n(* programming. However, SelectSeq produces sequences, whereas *)\n(* RestrictValues will in general not. For example, *)\n(* *)\n(* RestrictValues([0,1,2], LAMBDA y : y \\in {0,2}) *)\n(* = (1 :> 0 @@ 3 :> 2) *)\n(***************************************************************************)\nRestrictValues(f, Test(_)) ==\n LET S == {x \\in DOMAIN f : Test(f[x])}\n IN Restrict(f, S)\n\n(***************************************************************************)\n(* Check if a function narrow is a restriction of a function wide, i.e. *)\n(* Is the domain of narrow a subset of that of wide, and does the *)\n(* projection of wide on the domain of narrow have the same image as *)\n(* narrow does. *)\n(* *)\n(* Examples: *)\n(* IsRestriction([one |-> 1], [one |-> 1, two |-> 2]) *)\n(* IsRestriction([one |-> 1], [one |-> 1]) *)\n(* ~IsRestriction([one |-> 1, two |-> 2], [one |-> 1, two |-> 3]) *)\n(* ~IsRestriction([one |-> 1], [2 |-> two]) *)\n(* ~IsRestriction([one |-> 1, two |-> 2], [two |-> 2]) *)\n(***************************************************************************)\nIsRestriction(narrow, wide) ==\n /\\ DOMAIN narrow \\subseteq DOMAIN wide \n /\\ \\A x \\in DOMAIN narrow \\intersect DOMAIN wide: narrow[x] = wide[x]\n\n(***************************************************************************)\n(* Range of a function. *)\n(* Note: The image of a set under function f can be defined as *)\n(* Range(Restrict(f,S)). *)\n(***************************************************************************)\nRange(f) == { f[x] : x \\in DOMAIN f }\n\n(***************************************************************************)\n(* Assuming DOMAIN f \\subseteq DOMAIN g, apply the binary operation T to *)\n(* the corresponding elements of the two functions f and g. *)\n(* *)\n(* Example: *)\n(* LET f == (\"a\" :> 0 @@ \"b\" :> 1 @@ \"c\" :> 2) *)\n(* g == (\"a\" :> 1 @@ \"b\" :> 1 @@ \"c\" :> 3) *)\n(* IN Pointwise(f,g,+) = (\"a\" :> 1 @@ \"b\" :> 2 @@ \"c\" :> 5 ) *)\n(***************************************************************************)\nPointwise(f, g, T(_,_)) == [ e \\in DOMAIN f |-> T(f[e], g[e]) ]\n\n(***************************************************************************)\n(* The inverse of a function. *)\n(* Example: *)\n(* LET f == (\"a\" :> 0 @@ \"b\" :> 1 @@ \"c\" :> 2) *)\n(* IN Inverse(f, DOMAIN f, Range(f)) = *)\n(* (0 :> \"a\" @@ 1 :> \"b\" @@ 2 :> \"c\") *)\n(* Example: *)\n(* LET f == (\"a\" :> 0 @@ \"b\" :> 1 @@ \"c\" :> 2) *)\n(* IN Inverse(f, DOMAIN f, {1,3}) = *)\n(* 1 :> \"b\" @@ 3 :> \"a\") *)\n(***************************************************************************)\nInverse(f,S,T) == [t \\in T |-> CHOOSE s \\in S : t \\in Range(f) => f[s] = t]\n\n(***************************************************************************)\n(* The inverse of a function. *)\n(***************************************************************************)\nAntiFunction(f) == Inverse(f, DOMAIN f, Range(f))\n\n(***************************************************************************)\n(* A function is injective iff it maps each element in its domain to a *)\n(* distinct element. *)\n(* *)\n(* This definition is overridden by TLC in the Java class Functions.java *)\n(* The operator is overridden by the Java method with the same name. *)\n(***************************************************************************)\nIsInjective(f) == \\A a,b \\in DOMAIN f : f[a] = f[b] => a = b\n\n(***************************************************************************)\n(* Set of injections between two sets. *)\n(***************************************************************************)\nInjection(S,T) == { M \\in [S -> T] : IsInjective(M) }\n\n\n(***************************************************************************)\n(* A map is a surjection iff for each element in the range there is some *)\n(* element in the domain that maps to it. *)\n(***************************************************************************)\nSurjection(S,T) == { M \\in [S -> T] : \\A t \\in T : \\E s \\in S : M[s] = t }\n\n\n(***************************************************************************)\n(* A map is a bijection iff it is both an injection and a surjection. *)\n(***************************************************************************)\nBijection(S,T) == Injection(S,T) \\cap Surjection(S,T)\n\n\n(***************************************************************************)\n(* An injection, surjection, or bijection exists if the corresponding set *)\n(* is nonempty. *)\n(***************************************************************************)\nExistsInjection(S,T) == Injection(S,T) # {}\nExistsSurjection(S,T) == Surjection(S,T) # {}\nExistsBijection(S,T) == Bijection(S,T) # {}\n\n--------------------------------------------------------------------------------\n\n(***************************************************************************)\n(* Applies the binary operator op on the elements f[i] for i \\in indices, *)\n(* a subset of DOMAIN f, in an unspecified order, starting from value *)\n(* base. The result is *)\n(* op(f[i],op(f[j], ..., op(f[k],base) ...)) *)\n(* where i,j,k range over the sets indices. *)\n(* *)\n(* op should be associative and commutative, because we can not assume a *)\n(* particular ordering of i, j, and k *)\n(* *)\n(* Example: *)\n(* FoldFunctionOnSet(LAMBDA x,y: {x} \\cup y, {}, <<1,2>>, {}) = {} *)\n(***************************************************************************)\nFoldFunctionOnSet(op(_,_), base, fun, indices) ==\n MapThenFoldSet(op, base, LAMBDA i : fun[i], LAMBDA s: CHOOSE x \\in s : TRUE, indices)\n\n(***************************************************************************)\n(* Special case of the above where indices = DOMAIN fun. In other words, *)\n(* the values f[i] for all i \\in DOMAIN fun, are combined using the binary *)\n(* operator op, in an unspecified order. *)\n(* *)\n(* Example: *)\n(* FoldFunction(LAMBDA x,y: {x} \\cup y, {}, <<1,2,1>>) = {1,2} *)\n(***************************************************************************)\nFoldFunction(op(_,_), base, fun) ==\n FoldFunctionOnSet(op, base, fun, DOMAIN fun)\n\n(***************************************************************************)\n(* Sum the values f[i] for i \\in indices, a subset of DOMAIN f. *)\n(* *)\n(* Example: *)\n(* SumFunctionOnSet(<<4,5,6>>, {1,2}) = 9 *)\n(* SumFunction(<<4,5,6>>) = 15 *)\n(***************************************************************************)\nSumFunctionOnSet(fun, indices) == FoldFunctionOnSet(+, 0, fun, indices)\nSumFunction(fun) == SumFunctionOnSet(fun, DOMAIN fun)\n\n=============================================================================\n\\* Created Thu Apr 11 10:30:48 PDT 2013 by tomr\n", "GraphTheorems": "------------------------------ MODULE GraphTheorems --------------------------\nEXTENDS Graphs, Sequences\n\n(****************************************************************************)\n(* Lemmas about transposed graphs. *)\n(****************************************************************************)\nTHEOREM TransposeIsDirectedGraph ==\n ASSUME NEW G, IsDirectedGraph(G) \n PROVE IsDirectedGraph(Transpose(G))\n\n\\* Note that the reverse implication does not hold in general.\n\\* Consider G = [node |-> {1}, edge |-> {<<1,1,1>>}, root |-> {1}]\n\\* Then Transpose(G) = [node |-> {1}, edge |-> {<<1,1>>}],\n\\* which is a directed graph whereas G is not.\n\nTHEOREM TransposeTranspose == \n ASSUME NEW G, IsDirectedGraph(G)\n PROVE Transpose(Transpose(G)) = G \n\nTHEOREM PathTranspose ==\n ASSUME NEW G, NEW p \\in Path(G)\n PROVE Reverse(p) \\in Path(Transpose(G))\n\nTHEOREM TransposePath ==\n ASSUME NEW G, IsDirectedGraph(G), NEW p \\in Path(Transpose(G))\n PROVE Reverse(p) \\in Path(G)\n\nTHEOREM SimplePathTranspose ==\n ASSUME NEW G, NEW p \\in SimplePath(G)\n PROVE Reverse(p) \\in SimplePath(Transpose(G))\n\nTHEOREM TransposeSimplePath ==\n ASSUME NEW G, IsDirectedGraph(G), NEW p \\in SimplePath(Transpose(G))\n PROVE Reverse(p) \\in SimplePath(G)\n\nTHEOREM AreConnectedInTranspose ==\n ASSUME NEW G, NEW m, NEW n, AreConnectedIn(m, n, G)\n PROVE AreConnectedIn(n, m, Transpose(G))\n\nTHEOREM TransposeAreConnectedIn ==\n ASSUME NEW G, NEW m, NEW n, IsDirectedGraph(G), \n AreConnectedIn(m, n, Transpose(G))\n PROVE AreConnectedIn(n, m, G)\n\nTHEOREM IsStronglyConnectedTranspose ==\n ASSUME NEW G, IsStronglyConnected(G)\n PROVE IsStronglyConnected(Transpose(G))\n\nTHEOREM TransposeIsStronglyConnected ==\n ASSUME NEW G, IsDirectedGraph(G), IsStronglyConnected(Transpose(G))\n PROVE IsStronglyConnected(G)\n\n(****************************************************************************)\n(* The \"concatenation\" of two paths that share an endpoint is a path. *)\n(****************************************************************************)\nTHEOREM PathConcatenation ==\n ASSUME NEW G, NEW p \\in Path(G), NEW q \\in Path(G),\n p[Len(p)] = q[1] \n PROVE p \\o Tail(q) \\in Path(G)\n\n(****************************************************************************)\n(* Any non-empty subsequence of a path is a path. *)\n(****************************************************************************)\nTHEOREM SubPath ==\n ASSUME NEW G, NEW p \\in Path(G),\n NEW i \\in 1 .. Len(p), NEW j \\in i .. Len(p)\n PROVE SubSeq(p, i, j) \\in Path(G)\n\n(****************************************************************************)\n(* Two nodes are connected by a path if and only if they are connected by *)\n(* a simple path. *)\n(****************************************************************************)\nTHEOREM ExistsPathIffExistsSimplePath ==\n ASSUME NEW G, NEW m \\in G.node, NEW n \\in G.node\n PROVE (\\E p \\in Path(G) : p[1] = m /\\ p[Len(p)] = n)\n <=> (\\E p \\in SimplePath(G) : p[1] = m /\\ p[Len(p)] = n)\n\n(****************************************************************************)\n(* Connectedness is a pre-order. *)\n(****************************************************************************)\nTHEOREM ConnectedReflexive ==\n ASSUME NEW G, NEW a \\in G.node\n PROVE AreConnectedIn(a, a, G)\n\nTHEOREM ConnectedTransitive ==\n ASSUME NEW G, NEW a \\in G.node, NEW b \\in G.node, NEW c \\in G.node,\n AreConnectedIn(a, b, G), AreConnectedIn(b, c, G)\n PROVE AreConnectedIn(a, c, G)\n\n(****************************************************************************)\n(* A tree does not contain a cycle. One way to express this is that no *)\n(* non-empty set of nodes is closed under successors. *)\n(****************************************************************************)\nTHEOREM TreeAcyclic ==\n ASSUME NEW G, NEW r, IsTreeWithRoot(G,r), NEW S \\in SUBSET G.node, S # {}\n PROVE \\E q \\in S : \\A e \\in G.edge : e[1] = q => e[2] \\notin S \n\n(****************************************************************************)\n(* In particular, no node is its own successor in a tree, and all paths in *)\n(* a tree are simple paths. *)\n(****************************************************************************)\nTHEOREM TreeIrreflexive ==\n ASSUME NEW G, NEW r, IsTreeWithRoot(G,r), NEW n \\in G.node\n PROVE <<n,n>> \\notin G.edge\n\nTHEOREM TreeSimplePath ==\n ASSUME NEW G, NEW r, IsTreeWithRoot(G,r), NEW p \\in Path(G)\n PROVE p \\in SimplePath(G)\n\n(****************************************************************************)\n(* Connectedness is antisymmetric (and thus a partial order) in trees. *)\n(****************************************************************************)\nTHEOREM TreeConnectedAntisymmetric ==\n ASSUME NEW G, NEW r, IsTreeWithRoot(G,r), \n NEW a \\in G.node, NEW b \\in G.node,\n AreConnectedIn(a, b, G), AreConnectedIn(b, a, G)\n PROVE a = b \n\n(****************************************************************************)\n(* A graph consisting of only the root and no edges is a tree. *)\n(****************************************************************************)\nTHEOREM SingletonIsTreeWithRoot ==\n ASSUME NEW r\n PROVE IsTreeWithRoot([node |-> {r}, edge |-> {}],r)\n\n(****************************************************************************)\n(* A tree can be extended by attaching a disjoint tree to a node. *)\n(****************************************************************************)\nTHEOREM AttachTree ==\n ASSUME NEW G, NEW r, IsTreeWithRoot(G,r), NEW n \\in G.node,\n NEW H, NEW s, IsTreeWithRoot(H,s), G.node \\cap H.node = {} \n PROVE IsTreeWithRoot([node |-> G.node \\cup H.node,\n edge |-> G.edge \\cup H.edge \\cup {<<s,n>>}], r)\n\n(****************************************************************************)\n(* In particular, a tree can be extended by a new node that becomes a leaf. *)\n(****************************************************************************)\nTHEOREM AddLeafToTree == \n ASSUME NEW G, NEW r, NEW leaf, NEW parent,\n IsTreeWithRoot(G,r), leaf \\notin G.node, parent \\in G.node\n PROVE IsTreeWithRoot([node |-> G.node \\cup {leaf}, \n edge |-> G.edge \\cup {<<leaf, parent>>}], r)\n\n(****************************************************************************)\n(* Removing a subtree from a tree maintains the tree property, provided it *)\n(* is not the entire tree (trees must be non-empty.) *)\n(****************************************************************************)\nTHEOREM RemoveSubtree == \n ASSUME NEW G, NEW r, IsTreeWithRoot(G,r),\n NEW nd \\in G.node \\ {r}\n PROVE LET S == { n \\in G.node : AreConnectedIn(n, nd, G) }\n T == G.node \\ S \n E == G.edge \\cap (T \\X T)\n IN IsTreeWithRoot([node |-> T, edge |-> E], r)\n\n(****************************************************************************)\n(* In particular, removing a leaf node that is not the root from a tree *)\n(* results in a tree. *)\n(****************************************************************************)\nTHEOREM RemoveLeafFromTree == \n ASSUME NEW G, NEW r, IsTreeWithRoot(G,r),\n NEW leaf \\in G.node, Predecessors(G, leaf) = {},\n NEW parent \\in G.node, <<leaf, parent>> \\in G.edge\n PROVE IsTreeWithRoot([node |-> G.node \\ {leaf}, \n edge |-> G.edge \\ {<<leaf, parent>>}], r)\n\n(****************************************************************************)\n(* The operator IsTreeWithRoot is set up so that edges point towards the *)\n(* root of the tree. Sometimes it is more natural to consider trees with *)\n(* edges pointing towards the leaves. Such a graph G can be characterized *)\n(* by the predicate IsTreeWithRoot(Transpose(G), r). *)\n(* We prove lemmas analogous to the above ones about transposed trees. *)\n(****************************************************************************)\nTHEOREM SingletonIsTransposedTreeWithRoot ==\n ASSUME NEW r\n PROVE IsTreeWithRoot(Transpose([node |-> {r}, edge |-> {}]), r)\n\nTHEOREM AttachTransposedTree ==\n ASSUME NEW G, NEW r, IsDirectedGraph(G), IsTreeWithRoot(Transpose(G), r),\n NEW H, NEW s, IsDirectedGraph(H), IsTreeWithRoot(Transpose(H), s),\n G.node \\cap H.node = {}, NEW n \\in G.node\n PROVE IsTreeWithRoot(Transpose([node |-> G.node \\cup H.node,\n edge |-> G.edge \\cup H.edge \\cup {<<n,s>>}]), r)\n\nTHEOREM AddLeafToTransposedTree == \n ASSUME NEW G, NEW r, NEW leaf, NEW parent,\n IsDirectedGraph(G), IsTreeWithRoot(Transpose(G),r),\n leaf \\notin G.node, parent \\in G.node\n PROVE IsTreeWithRoot(Transpose([node |-> G.node \\cup {leaf}, \n edge |-> G.edge \\cup {<<parent, leaf>>}]), r)\n\nTHEOREM RemoveTransposedSubtree ==\n ASSUME NEW G, NEW r, IsDirectedGraph(G), IsTreeWithRoot(Transpose(G), r),\n NEW nd \\in G.node \\ {r}\n PROVE LET S == {n \\in G.node : AreConnectedIn(nd, n, G)}\n T == G.node \\ S \n E == G.edge \\cap (T \\X T)\n IN IsTreeWithRoot(Transpose([node |-> T, edge |-> E]), r)\n\nTHEOREM RemoveLeafFromTransposedTree ==\n ASSUME NEW G, IsDirectedGraph(G),\n NEW r, IsTreeWithRoot(Transpose(G),r),\n NEW leaf \\in G.node,\n Successors(G, leaf) = {},\n NEW parent \\in G.node, <<parent, leaf>> \\in G.edge\n PROVE IsTreeWithRoot(Transpose([node |-> G.node \\ {leaf}, \n edge |-> G.edge \\ {<<parent, leaf>>}]), r)\n\n==============================================================================\n", "GraphTheorems_proofs": "---------------------- MODULE GraphTheorems_proofs --------------------------\nEXTENDS Graphs, Integers, FunctionTheorems, FiniteSetTheorems, \n SequencesExtTheorems,\n FiniteSetsExt, FiniteSetsExtTheorems, TLAPS\n\n(****************************************************************************)\n(* Lemmas about transposed graphs. *)\n(****************************************************************************)\nTHEOREM TransposeIsDirectedGraph ==\n ASSUME NEW G, IsDirectedGraph(G) \n PROVE IsDirectedGraph(Transpose(G))\n<1>. DEFINE N == G.node \n E == G.edge\n<1>1. /\\ G = [node |-> N, edge |-> E]\n /\\ E \\subseteq N \\X N \n BY DEF IsDirectedGraph\n<1>. HIDE DEF N, E \n<1>. QED BY <1>1 DEF IsDirectedGraph, Transpose \n\nTHEOREM TransposeTranspose == \n ASSUME NEW G, IsDirectedGraph(G)\n PROVE Transpose(Transpose(G)) = G \n<1>. DEFINE N == G.node \n E == G.edge\n TE == { <<e[2], e[1]>> : e \\in E }\n TTE == { <<e[2], e[1]>> : e \\in TE }\n<1>1. /\\ G = [node |-> N, edge |-> E]\n /\\ E \\subseteq N \\X N \n /\\ TE \\subseteq N \\X N \n BY DEF IsDirectedGraph\n<1>. HIDE DEF N, E \n<1>2. Transpose(G) = [node |-> N, edge |-> TE]\n BY <1>1 DEF Transpose \n<1>3. TTE = E \n BY <1>1\n<1>4. Transpose(Transpose(G)) = [node |-> N, edge |-> TTE]\n BY <1>2 DEF Transpose \n<1>. HIDE DEF TTE\n<1>. QED BY <1>1, <1>3, <1>4\n\nTHEOREM PathTranspose ==\n ASSUME NEW G, NEW p \\in Path(G)\n PROVE Reverse(p) \\in Path(Transpose(G))\nBY DEF Path, Reverse, Transpose \n\nTHEOREM TransposePath ==\n ASSUME NEW G, IsDirectedGraph(G), NEW p \\in Path(Transpose(G))\n PROVE Reverse(p) \\in Path(G)\n<1>. DEFINE N == G.node \n E == G.edge \n TE == { <<e[2], e[1]>> : e \\in E }\n<1>1. /\\ G = [node |-> N, edge |-> E]\n /\\ E \\subseteq N \\X N\n BY DEF IsDirectedGraph \n<1>2. Transpose(G) = [node |-> N, edge |-> TE]\n BY DEF Transpose \n<1>. HIDE DEF N, E\n<1>3. /\\ p \\in Seq(N) \\ { <<>> }\n /\\ \\A i \\in 1 .. Len(p)-1 : <<p[i], p[i+1]>> \\in TE\n BY <1>2 DEF Path\n<1>. QED BY <1>1, <1>3 DEF Reverse, Path \n\nTHEOREM SimplePathTranspose ==\n ASSUME NEW G, NEW p \\in SimplePath(G)\n PROVE Reverse(p) \\in SimplePath(Transpose(G))\n<1>1. /\\ Reverse(p) \\in Path(Transpose(G))\n /\\ \\A i,j \\in 1 .. Len(p) : p[i] = p[j] => i = j\n BY PathTranspose DEF SimplePath \n<1>2. p \\in Seq(G.node)\n BY DEF SimplePath, Path \n<1>. QED BY <1>1, <1>2 DEF Reverse, SimplePath \n\nTHEOREM TransposeSimplePath ==\n ASSUME NEW G, IsDirectedGraph(G), NEW p \\in SimplePath(Transpose(G))\n PROVE Reverse(p) \\in SimplePath(G)\n<1>1. /\\ Reverse(p) \\in Path(G)\n /\\ \\A i,j \\in 1 .. Len(p) : p[i] = p[j] => i = j\n BY TransposePath DEF SimplePath \n<1>2. p \\in Seq(G.node)\n BY DEF SimplePath, Path, Transpose \n<1>. QED BY <1>1, <1>2 DEF Reverse, SimplePath \n\nTHEOREM AreConnectedInTranspose ==\n ASSUME NEW G, NEW m, NEW n, AreConnectedIn(m, n, G)\n PROVE AreConnectedIn(n, m, Transpose(G))\n<1>1. PICK p \\in Path(G) : p[1] = m /\\ p[Len(p)] = n \n BY DEF AreConnectedIn \n<1>. p \\in Seq(G.node) \\ { << >> }\n BY DEF Path \n<1>2. Reverse(p) \\in Path(Transpose(G))\n BY PathTranspose\n<1>3. Reverse(p)[1] = n /\\ Reverse(p)[Len(Reverse(p))] = m \n BY <1>1 DEF Reverse \n<1>. QED BY <1>2, <1>3 DEF AreConnectedIn \n\nTHEOREM TransposeAreConnectedIn ==\n ASSUME NEW G, NEW m, NEW n, IsDirectedGraph(G), AreConnectedIn(m, n, Transpose(G))\n PROVE AreConnectedIn(n, m, G)\n<1>1. PICK p \\in Path(Transpose(G)) : p[1] = m /\\ p[Len(p)] = n \n BY DEF AreConnectedIn \n<1>. p \\in Seq(G.node) \\ { << >> }\n BY DEF Path, Transpose \n<1>2. Reverse(p) \\in Path(G)\n BY TransposePath\n<1>3. Reverse(p)[1] = n /\\ Reverse(p)[Len(Reverse(p))] = m \n BY <1>1 DEF Reverse \n<1>. QED BY <1>2, <1>3 DEF AreConnectedIn \n\nTHEOREM IsStronglyConnectedTranspose ==\n ASSUME NEW G, IsStronglyConnected(G)\n PROVE IsStronglyConnected(Transpose(G))\n<1>. Transpose(G).node = G.node\n BY DEF Transpose \n<1>. QED BY AreConnectedInTranspose DEF IsStronglyConnected \n\nTHEOREM TransposeIsStronglyConnected ==\n ASSUME NEW G, IsDirectedGraph(G), IsStronglyConnected(Transpose(G))\n PROVE IsStronglyConnected(G)\n<1>. Transpose(G).node = G.node\n BY DEF Transpose \n<1>. QED BY TransposeAreConnectedIn DEF IsStronglyConnected \n\n(****************************************************************************)\n(* The \"concatenation\" of two paths that share an endpoint is a path. *)\n(****************************************************************************)\nTHEOREM PathConcatenation ==\n ASSUME NEW G, NEW p \\in Path(G), NEW q \\in Path(G),\n p[Len(p)] = q[1] \n PROVE p \\o Tail(q) \\in Path(G)\n<1>. DEFINE pq == p \\o Tail(q)\n<1>. p \\in Seq(G.node) \\ { <<>> } /\\ q \\in Seq(G.node) \\ { <<>> }\n BY DEF Path \n<1>2. ASSUME NEW i \\in 1 .. Len(pq)-1 PROVE <<pq[i], pq[i+1]>> \\in G.edge\n <2>1. CASE i \\in 1 .. Len(p)-1\n BY <2>1 DEF Path \n <2>2. CASE i \\in Len(p) .. Len(pq)-1\n BY <2>2, i-Len(p)+1 \\in 1 .. Len(q)-1 DEF Path \n <2>. QED BY <2>1, <2>2\n<1>. QED BY <1>2 DEF Path \n\n(****************************************************************************)\n(* Any non-empty subsequence of a path is a path. *)\n(****************************************************************************)\nTHEOREM SubPath ==\n ASSUME NEW G, NEW p \\in Path(G),\n NEW i \\in 1 .. Len(p), NEW j \\in i .. Len(p)\n PROVE SubSeq(p, i, j) \\in Path(G)\n<1>. DEFINE s == SubSeq(p, i, j)\n<1>. p \\in Seq(G.node)\n BY DEF Path \n<1>1. /\\ s \\in Seq(G.node) \\ { <<>> }\n /\\ Len(s) = j-i+1\n OBVIOUS \n<1>2. ASSUME NEW k \\in 1 .. (j-i) PROVE <<s[k], s[k+1]>> \\in G.edge\n BY i+k-1 \\in 1 .. Len(p)-1 DEF Path \n<1>. QED BY <1>1, <1>2 DEF Path \n\n(****************************************************************************)\n(* Two nodes are connected by a path if and only if they are connected by *)\n(* a simple path. *)\n(****************************************************************************)\nTHEOREM ExistsPathIffExistsSimplePath ==\n ASSUME NEW G, NEW m \\in G.node, NEW n \\in G.node\n PROVE (\\E p \\in Path(G) : p[1] = m /\\ p[Len(p)] = n)\n <=> (\\E p \\in SimplePath(G) : p[1] = m /\\ p[Len(p)] = n)\n<1>1. SUFFICES ASSUME NEW p \\in Path(G), p[1] = m, p[Len(p)] = n \n PROVE \\E q \\in SimplePath(G) : q[1] = m /\\ q[Len(q)] = n \n BY DEF SimplePath\n<1>. DEFINE Repeats(pp) == {i \\in 1 .. Len(pp) : \\E j \\in 1 .. Len(pp) : j > i /\\ pp[i] = pp[j]}\n CR(pp) == Cardinality(Repeats(pp))\n P(pp,k) == pp[1] = m /\\ pp[Len(pp)] = n /\\ CR(pp) = k \n => \\E q \\in SimplePath(G) : q[1] = m /\\ q[Len(q)] = n \n<1>2. ASSUME NEW pp \\in Path(G)\n PROVE IsFiniteSet(Repeats(pp))\n BY FS_Subset, FS_Interval DEF Path\n<1>3. \\E k \\in Nat : CR(p) = k\n BY <1>2, FS_CardinalityType, Zenon\n<1>4. \\A k \\in Nat : \\A pp \\in Path(G) : P(pp,k)\n <2>. SUFFICES \\A k \\in Nat : (\\A l \\in 0 .. k-1 : \\A pp \\in Path(G) : P(pp,l))\n => \\A pp \\in Path(G) : P(pp,k)\n <3>. HIDE DEF P \n <3>. QED BY GeneralNatInduction, IsaM(\"iprover\")\n <2>1. SUFFICES ASSUME NEW k \\in Nat,\n \\A l \\in 0 .. k-1 : \\A pp \\in Path(G) : P(pp,l),\n NEW pp \\in Path(G), pp[1] = m, pp[Len(pp)] = n, CR(pp) = k\n PROVE \\E q \\in SimplePath(G) : q[1] = m /\\ q[Len(q)] = n \n BY Zenon\n <2>. pp \\in Seq(G.node)\n BY DEF Path\n <2>2. CASE k = 0\n <3>. Repeats(pp) = {}\n BY <1>2, <2>1, <2>2, FS_EmptySet, Zenon\n <3>. QED BY <2>1 DEF SimplePath\n <2>3. CASE k # 0\n <3>1. PICK i \\in 1 .. Len(pp) : \\E j \\in 1 .. Len(pp) : j > i /\\ pp[i] = pp[j]\n BY <1>2, <2>1, <2>3, FS_EmptySet\n <3>2. PICK j \\in 1 .. Len(pp) : /\\ j > i /\\ pp[i] = pp[j] \n /\\ ~(\\E jj \\in 1 .. Len(pp) : jj > j /\\ pp[i] = pp[jj])\n <4>. DEFINE J == {jj \\in 1 .. Len(pp) : jj > i /\\ pp[i] = pp[jj]}\n <4>1. /\\ J \\in SUBSET Int \n /\\ J # {}\n /\\ Len(pp) \\in Int \n /\\ \\A jj \\in J : Len(pp) >= jj\n BY <3>1\n <4>2. /\\ Max(J) \\in J\n /\\ \\A jj \\in J : Max(J) >= jj\n BY <4>1, MaxIntBounded\n <4>. QED BY <4>2\n <3>. DEFINE pref == SubSeq(pp, 1, i)\n suff == SubSeq(pp, j+1, Len(pp))\n qq == pref \\o suff\n <3>3. qq \\in Path(G)\n <4>1. qq \\in Seq(G.node)\n OBVIOUS\n <4>2. qq # << >> \n BY <3>2 DEF Path\n <4>3. ASSUME NEW ii \\in 1 .. Len(qq)-1\n PROVE <<qq[ii], qq[ii+1]>> \\in G.edge\n <5>1. CASE ii \\in 1 .. i-1\n BY <5>1 DEF Path \n <5>2. CASE ii = i \n BY <3>2, <5>2 DEF Path\n <5>3. CASE ii > i\n <6>1. /\\ qq[ii] = pp[j-i+ii]\n /\\ qq[ii+1] = pp[j-i+ii+1]\n BY <3>2, <5>3\n <6>. QED BY <3>2, <6>1 DEF Path \n <5>. QED BY <5>1, <5>2, <5>3\n <4>. QED BY <4>1, <4>2, <4>3 DEF Path \n <3>4. CR(qq) \\in 0 .. k-1\n <4>. DEFINE inj == [ii \\in Repeats(qq) |-> IF ii <= i THEN ii ELSE j-i+ii]\n <4>1. ASSUME NEW ii \\in Repeats(qq)\n PROVE inj[ii] \\in Repeats(pp)\n BY <3>2\n <4>2. inj \\in [Repeats(qq) -> Repeats(pp)]\n BY <4>1, Zenon\n <4>3. \\A ii,jj \\in Repeats(qq) : inj[ii] = inj[jj] => ii = jj\n BY <3>2\n <4>4. inj \\in Injection(Repeats(qq), Repeats(pp))\n BY <4>2, <4>3, Fun_IsInj, Zenon\n <4>5. CR(qq) <= CR(pp)\n BY <1>2, <4>4, FS_Injection, Zenon\n <4>6. i \\in Repeats(pp)\n BY <3>1\n <4>7. i \\notin Repeats(qq)\n BY <3>2\n <4>8. inj \\notin Surjection(Repeats(qq), Repeats(pp))\n BY <3>2, <4>6, <4>7, SMTT(10) DEF Surjection\n <4>9. /\\ CR(qq) <= CR(pp)\n /\\ CR(qq) # CR(pp)\n <5>. HIDE DEF Repeats, inj, qq\n <5>. QED BY <1>2, <4>4, <4>8, FS_Injection, Zenon\n <4>. QED BY <1>2, <2>1, <3>3, <4>9, FS_CardinalityType, Isa\n <3>5. qq[1] = m \n BY <2>1 DEF Path\n <3>6. qq[Len(qq)] = n \n BY <2>1, <3>2 DEF Path\n <3>. HIDE DEF CR, qq\n <3>. QED BY <2>1, <3>3, <3>4, <3>5, <3>6\n <2>. HIDE DEF P\n <2>. QED BY <2>2, <2>3, NatInduction, Isa\n<1>. QED BY <1>1, <1>3, <1>4, Zenon\n\n(****************************************************************************)\n(* Connectedness is a pre-order. *)\n(****************************************************************************)\nTHEOREM ConnectedReflexive ==\n ASSUME NEW G, NEW a \\in G.node\n PROVE AreConnectedIn(a, a, G)\nBY <<a>> \\in Seq(G.node) DEF AreConnectedIn, Path \n\nTHEOREM ConnectedTransitive ==\n ASSUME NEW G, NEW a \\in G.node, NEW b \\in G.node, NEW c \\in G.node,\n AreConnectedIn(a, b, G), AreConnectedIn(b, c, G)\n PROVE AreConnectedIn(a, c, G)\n<1>1. PICK p \\in Path(G) : p[1] = a /\\ p[Len(p)] = b \n BY DEF AreConnectedIn \n<1>2. PICK q \\in Path(G) : q[1] = b /\\ q[Len(q)] = c\n BY DEF AreConnectedIn \n<1>. DEFINE pq == p \\o Tail(q)\n<1>3. pq \\in Path(G)\n BY <1>1, <1>2, PathConcatenation\n<1>4. pq[1] = a /\\ pq[Len(pq)] = c\n BY <1>1, <1>2 DEF Path \n<1>. QED BY <1>3, <1>4 DEF AreConnectedIn, Path \n\n(****************************************************************************)\n(* A tree does not contain a cycle. One way to express this is that no *)\n(* non-empty set of nodes is closed under successors. *)\n(****************************************************************************)\nTHEOREM TreeAcyclic ==\n ASSUME NEW G, NEW r, IsTreeWithRoot(G,r), NEW S \\in SUBSET G.node, S # {}\n PROVE \\E q \\in S : \\A e \\in G.edge : e[1] = q => e[2] \\notin S \n<1>. DEFINE RootPaths(n) == {p \\in Path(G) : p[1] = n /\\ p[Len(p)] = r}\n height(n) == Min({Len(p) : p \\in RootPaths(n)})\n<1>1. \\A n \\in G.node : RootPaths(n) # {}\n BY DEF IsTreeWithRoot, AreConnectedIn\n<1>2. \\A n \\in G.node : /\\ height(n) \\in Nat\n /\\ \\E p \\in RootPaths(n) : Len(p) = height(n) \n /\\ \\A p \\in RootPaths(n) : Len(p) >= height(n)\n <2>. TAKE n \\in G.node\n <2>1. {Len(p) : p \\in RootPaths(n)} \\in (SUBSET Nat) \\ {{}}\n BY <1>1 DEF Path \n <2>2. /\\ height(n) \\in {Len(p) : p \\in RootPaths(n)}\n /\\ \\A l \\in {Len(p) : p \\in RootPaths(n)} : height(n) <= l \n BY <2>1, MinNat\n <2>. QED BY <2>1, <2>2\n<1>3. ASSUME NEW e \\in G.edge\n PROVE height(e[2]) < height(e[1])\n <2>1. e[1] \\in G.node /\\ e[2] \\in G.node\n BY DEF IsTreeWithRoot, IsDirectedGraph\n <2>2. PICK p \\in RootPaths(e[1]) : Len(p) = height(e[1])\n BY <1>2, <2>1\n <2>. p \\in Seq(G.node) \\ {<< >>}\n BY DEF Path \n <2>3. p[1] = e[1] /\\ p[Len(p)] = r \n BY DEF Path \n <2>4. e[1] # r \n BY DEF IsTreeWithRoot \n <2>5. Len(p) >= 2\n BY <2>3, <2>4\n <2>6. <<p[1], p[2]>> \\in G.edge\n BY <2>5, 1 \\in 1 .. Len(p)-1 DEF Path \n <2>7. p[2] = e[2]\n BY <2>3, <2>6 DEF IsTreeWithRoot \n <2>. DEFINE q == Tail(p)\n <2>8. q \\in RootPaths(e[2])\n <3>1. q \\in Seq(G.node) \\ {<< >>}\n BY <2>5\n <3>2. ASSUME NEW i \\in 1 .. Len(q)-1\n PROVE <<q[i], q[i+1]>> \\in G.edge\n BY i+1 \\in 1 .. Len(p)-1 DEF Path\n <3>3. q[1] = e[2] /\\ q[Len(q)] = r\n BY <2>5, <2>7\n <3>. QED BY <3>1, <3>2, <3>3 DEF Path \n <2>. HIDE DEF height\n <2>. QED BY <1>2, <2>1, <2>2, <2>8\n<1>4. PICK q \\in S : \\A n \\in S : height(q) <= height(n)\n <2>. HIDE DEF height \n <2>. DEFINE HS == {height(n) : n \\in S} mh == Min(HS)\n <2>1. HS \\in (SUBSET Nat) \\ {{}}\n BY <1>2\n <2>2. /\\ mh \\in HS\n /\\ \\A h \\in HS : mh <= h\n BY <2>1, MinNat\n <2>. QED BY <2>2\n<1>. HIDE DEF RootPaths\n<1>. QED BY <1>2, <1>3, <1>4\n\n(****************************************************************************)\n(* In particular, no node is its own successor in a tree, and all paths in *)\n(* a tree are simple paths. *)\n(****************************************************************************)\nTHEOREM TreeIrreflexive ==\n ASSUME NEW G, NEW r, IsTreeWithRoot(G,r), NEW n \\in G.node\n PROVE <<n,n>> \\notin G.edge\n<1>1. PICK q \\in {n} : \\A e \\in G.edge : e[1] = q => e[2] \\notin {n}\n BY TreeAcyclic, Zenon\n<1>. QED BY <1>1\n\nTHEOREM TreeSimplePath ==\n ASSUME NEW G, NEW r, IsTreeWithRoot(G,r), NEW p \\in Path(G)\n PROVE p \\in SimplePath(G)\n<1>. SUFFICES ASSUME NEW i \\in 1 .. Len(p), NEW j \\in 1 .. Len(p), i < j, p[i] = p[j]\n PROVE FALSE \n BY DEF SimplePath \n<1>. p \\in Seq(G.node) \\ { << >> }\n BY DEF Path \n<1>. DEFINE S == {p[k] : k \\in i .. j}\n<1>1. PICK k \\in i..j : \\A e \\in G.edge : e[1] = p[k] => e[2] \\notin S \n BY TreeAcyclic, S \\subseteq G.node, S # {}\n<1>2. CASE k < j\n <2>1. /\\ <<p[k], p[k+1]>> \\in G.edge\n /\\ p[k+1] \\in S \n BY <1>2 DEF Path \n <2>. QED BY <1>1, <2>1\n<1>3. CASE k = j\n <2>1. /\\ <<p[i], p[i+1]>> \\in G.edge\n /\\ p[i+1] \\in S \n BY DEF Path \n <2>. QED BY <1>1, <1>3, <2>1\n<1>. QED BY <1>2, <1>3\n\n(****************************************************************************)\n(* Connectedness is antisymmetric (and thus a partial order) in trees. *)\n(****************************************************************************)\nTHEOREM TreeConnectedAntisymmetric ==\n ASSUME NEW G, NEW r, IsTreeWithRoot(G,r), \n NEW a \\in G.node, NEW b \\in G.node,\n AreConnectedIn(a, b, G), AreConnectedIn(b, a, G)\n PROVE a = b \n<1>1. PICK p \\in Path(G) : p[1] = a /\\ p[Len(p)] = b \n BY DEF AreConnectedIn \n<1>2. PICK q \\in Path(G) : q[1] = b /\\ q[Len(q)] = a \n BY DEF AreConnectedIn \n<1>. p \\in Seq(G.node) \\ { <<>> } /\\ q \\in Seq(G.node) \\ { <<>> }\n BY DEF Path \n<1>. DEFINE pq == p \\o Tail(q)\n<1>3. pq \\in Path(G)\n BY <1>1, <1>2, PathConcatenation\n<1>4. pq \\in SimplePath(G)\n BY <1>3, TreeSimplePath \n<1>5. pq[1] = a /\\ pq[Len(pq)] = a \n BY <1>1, <1>2\n<1>6. Len(pq) = 1\n BY <1>4, <1>5 DEF SimplePath \n<1>. QED BY <1>1, <1>6\n\n(****************************************************************************)\n(* A graph consisting of only the root and no edges is a tree. *)\n(****************************************************************************)\nTHEOREM SingletonIsTreeWithRoot ==\n ASSUME NEW r\n PROVE IsTreeWithRoot([node |-> {r}, edge |-> {}],r)\n<1>. DEFINE G == [node |-> {r}, edge |-> {}]\n<1>1. IsDirectedGraph(G)\n BY DEF IsDirectedGraph\n<1>2. \\A n \\in G.node : AreConnectedIn(n, r, G)\n <2>. <<r>> \\in Path(G)\n BY DEF Path\n <2>. QED BY DEF AreConnectedIn\n<1>. QED BY <1>1, <1>2 DEF IsTreeWithRoot, IsDirectedGraph\n\n(****************************************************************************)\n(* A tree can be extended by attaching a disjoint tree to a node. *)\n(****************************************************************************)\nTHEOREM AttachTree ==\n ASSUME NEW G, NEW r, IsTreeWithRoot(G,r), NEW n \\in G.node,\n NEW H, NEW s, IsTreeWithRoot(H,s), G.node \\cap H.node = {} \n PROVE IsTreeWithRoot([node |-> G.node \\cup H.node,\n edge |-> G.edge \\cup H.edge \\cup {<<s,n>>}], r)\n<1>. DEFINE T == [node |-> G.node \\cup H.node,\n edge |-> G.edge \\cup H.edge \\cup {<<s,n>>}]\n<1>1. IsDirectedGraph(T)\n BY DEF IsDirectedGraph, IsTreeWithRoot \n<1>2. r \\in T.node\n BY DEF IsTreeWithRoot\n<1>3. ASSUME NEW e \\in T.edge PROVE e[1] # r\n BY DEF IsTreeWithRoot, IsDirectedGraph\n<1>4. ASSUME NEW e \\in T.edge, NEW f \\in T.edge, e[1] = f[1]\n PROVE e = f\n BY <1>4 DEF IsTreeWithRoot, IsDirectedGraph \n<1>5. ASSUME NEW nd \\in T.node\n PROVE AreConnectedIn(nd, r, T)\n <2>1. CASE nd \\in G.node\n <3>1. PICK p \\in Path(G) : p[1] = nd /\\ p[Len(p)] = r \n BY <2>1 DEF IsTreeWithRoot, AreConnectedIn\n <3>2. p \\in Path(T)\n BY DEF Path \n <3>. QED BY <3>1, <3>2 DEF AreConnectedIn \n <2>2. CASE nd \\in H.node\n <3>1. PICK p \\in Path(H) : p[1] = nd /\\ p[Len(p)] = s\n BY <2>2 DEF IsTreeWithRoot, AreConnectedIn \n <3>2. PICK q \\in Path(G) : q[1] = n /\\ q[Len(q)] = r \n BY DEF IsTreeWithRoot, AreConnectedIn \n <3>. p \\in Seq(H.node) \\ { <<>> } /\\ q \\in Seq(G.node) \\ { <<>> }\n BY DEF Path \n <3>. DEFINE pq == p \\o q\n <3>3. pq \\in Seq(T.node) \\ { <<>> }\n BY <3>1, <3>2\n <3>4. ASSUME NEW i \\in 1 .. Len(pq)-1 \n PROVE << pq[i], pq[i+1] >> \\in T.edge \n <4>1. CASE i \\in 1 .. Len(p)-1\n BY <4>1 DEF Path \n <4>2. CASE i = Len(p)\n BY <3>1, <3>2, <4>2\n <4>3. CASE i \\in Len(p)+1 .. Len(pq)-1\n BY <4>3, i - Len(p) \\in 1 .. Len(q)-1 DEF Path \n <4>. QED BY <4>1, <4>2, <4>3 \n <3>5. pq[1] = nd /\\ pq[Len(pq)] = r\n BY <3>1, <3>2\n <3>. HIDE DEF pq, T\n <3>. QED BY <3>3, <3>4, <3>5 DEF AreConnectedIn, Path \n <2>. QED BY <2>1, <2>2\n<1>. QED BY <1>1, <1>2, <1>3, <1>4, <1>5 DEF IsTreeWithRoot \n\n(****************************************************************************)\n(* In particular, a tree can be extended by a new node that becomes a leaf. *)\n(****************************************************************************)\nTHEOREM AddLeafToTree == \n ASSUME NEW G, NEW r, NEW leaf, NEW parent,\n IsTreeWithRoot(G,r), leaf \\notin G.node, parent \\in G.node\n PROVE IsTreeWithRoot([node |-> G.node \\cup {leaf}, \n edge |-> G.edge \\cup {<<leaf, parent>>}], r)\n<1>. DEFINE H == [node |-> {leaf}, edge |-> {}]\n<1>1. IsTreeWithRoot(H, leaf)\n BY SingletonIsTreeWithRoot\n<1>2. G.node \\cap H.node = {}\n OBVIOUS \n<1>3. IsTreeWithRoot([node |-> G.node \\cup H.node,\n edge |-> G.edge \\cup H.edge \\cup {<<leaf, parent>>}], r)\n <2>. HIDE DEF H \n <2>. QED BY <1>1, <1>2, AttachTree \n<1>. QED BY <1>3, Zenon\n\n(****************************************************************************)\n(* Removing a subtree from a tree maintains the tree property, provided it *)\n(* is not the entire tree (trees must be non-empty.) *)\n(****************************************************************************)\nTHEOREM RemoveSubtree == \n ASSUME NEW G, NEW r, IsTreeWithRoot(G,r),\n NEW nd \\in G.node \\ {r}\n PROVE LET S == { n \\in G.node : AreConnectedIn(n, nd, G) }\n T == G.node \\ S \n E == G.edge \\cap (T \\X T)\n IN IsTreeWithRoot([node |-> T, edge |-> E], r)\n<1>. DEFINE S == { n \\in G.node : AreConnectedIn(n, nd, G) }\n T == G.node \\ S \n E == G.edge \\cap (T \\X T)\n SG == [node |-> T, edge |-> E]\n<1>1. IsDirectedGraph(SG)\n BY DEF IsDirectedGraph\n<1>2. r \\in SG.node\n <2>1. r \\in G.node /\\ AreConnectedIn(nd, r, G)\n BY DEF IsTreeWithRoot \n <2>2. ~ AreConnectedIn(r, nd, G)\n BY <2>1, TreeConnectedAntisymmetric\n <2>. QED BY <2>1, <2>2\n<1>3. ASSUME NEW e \\in SG.edge\n PROVE /\\ e[1] # r \n /\\ \\A f \\in SG.edge : e[1] = f[1] => e = f \n BY DEF IsTreeWithRoot \n<1>4. ASSUME NEW n \\in SG.node\n PROVE AreConnectedIn(n, r, SG)\n <2>1. PICK p \\in Path(G) : p[1] = n /\\ p[Len(p)] = r \n BY DEF IsTreeWithRoot, AreConnectedIn \n <2>. p \\in Seq(G.node) \\ { <<>> }\n BY DEF Path \n <2>2. p \\in Seq(T)\n <3>1. SUFFICES ASSUME NEW i \\in 1 .. Len(p), AreConnectedIn(p[i], nd, G)\n PROVE FALSE \n OBVIOUS \n <3>2. SubSeq(p, 1, i) \\in Path(G)\n BY SubPath\n <3>3. AreConnectedIn(n, p[i], G)\n BY <2>1, <3>2 DEF AreConnectedIn \n <3>. QED BY <3>1, <3>3, ConnectedTransitive\n <2>. HIDE DEF T\n <2>3. \\A i \\in 1 .. Len(p)-1 : <<p[i], p[i+1]>> \\in E \n BY <2>2 DEF Path \n <2>4. p \\in Path(SG)\n BY <2>2, <2>3 DEF Path \n <2>. QED BY <2>1, <2>4 DEF AreConnectedIn \n<1>. QED BY <1>1, <1>2, <1>3, <1>4 DEF IsTreeWithRoot \n\n(****************************************************************************)\n(* In particular, removing a leaf node that is not the root from a tree *)\n(* results in a tree. *)\n(****************************************************************************)\nTHEOREM RemoveLeafFromTree == \n ASSUME NEW G, NEW r, IsTreeWithRoot(G,r),\n NEW leaf \\in G.node, Predecessors(G, leaf) = {},\n NEW parent \\in G.node, <<leaf, parent>> \\in G.edge\n PROVE IsTreeWithRoot([node |-> G.node \\ {leaf}, \n edge |-> G.edge \\ {<<leaf, parent>>}], r)\n<1>1. leaf # r \n BY DEF IsTreeWithRoot \n<1>. DEFINE S == {n \\in G.node : AreConnectedIn(n, leaf, G)}\n T == G.node \\ S \n E == G.edge \\cap (T \\X T)\n<1>2. IsTreeWithRoot([node |-> T, edge |-> E], r)\n BY <1>1, RemoveSubtree, IsaM(\"blast\")\n<1>3. S = {leaf}\n <2>1. leaf \\in S \n BY ConnectedReflexive\n <2>2. ASSUME NEW n \\in G.node, AreConnectedIn(n, leaf, G)\n PROVE n = leaf \n <3>1. PICK p \\in Path(G) : p[1] = n /\\ p[Len(p)] = leaf \n BY <2>2 DEF AreConnectedIn \n <3>2. Len(p)-1 \\notin 1 .. Len(p)-1\n BY <3>1 DEF Path, Predecessors\n <3>. QED BY <3>1, <3>2 DEF Path\n <2>. QED BY <2>1, <2>2\n<1>4. E = G.edge \\ {<<leaf, parent>>}\n <2>1. E \\subseteq G.edge \\ {<<leaf, parent>>}\n BY <1>3\n <2>2. ASSUME NEW e \\in G.edge, e # <<leaf, parent>> \n PROVE e \\in E \n <3>1. PICK a \\in G.node, b \\in G.node : e = <<a,b>> \n BY DEF IsTreeWithRoot, IsDirectedGraph \n <3>2. CASE a \\in S \\* a must be `leaf', but it has a unique successor\n BY <1>3, <2>2, <3>1, <3>2 DEF IsTreeWithRoot \n <3>3. CASE b \\in S \\* b must be `leaf', but it has no predecessor\n BY <1>3, <3>1, <3>3 DEF Predecessors \n <3>. QED BY <3>1, <3>2, <3>3\n <2>. QED BY <2>1, <2>2 \n<1>. HIDE DEF E, S \n<1>. QED BY <1>2, <1>3, <1>4\n\n(****************************************************************************)\n(* The operator IsTreeWithRoot is set up so that edges point towards the *)\n(* root of the tree. Sometimes it is more natural to consider trees with *)\n(* edges pointing towards the leaves. Such a graph G can be characterized *)\n(* by the predicate IsTreeWithRoot(Transpose(G), r). *)\n(* We prove lemmas analogous to the above ones about transposed trees. *)\n(****************************************************************************)\nTHEOREM SingletonIsTransposedTreeWithRoot ==\n ASSUME NEW r\n PROVE IsTreeWithRoot(Transpose([node |-> {r}, edge |-> {}]), r)\nBY SingletonIsTreeWithRoot DEF Transpose \n\nTHEOREM AttachTransposedTree ==\n ASSUME NEW G, NEW r, IsDirectedGraph(G), IsTreeWithRoot(Transpose(G), r),\n NEW H, NEW s, IsDirectedGraph(H), IsTreeWithRoot(Transpose(H), s),\n G.node \\cap H.node = {}, NEW n \\in G.node\n PROVE IsTreeWithRoot(Transpose([node |-> G.node \\cup H.node,\n edge |-> G.edge \\cup H.edge \\cup {<<n,s>>}]), r)\n<1>. DEFINE GN == G.node GE == G.edge TGE == {<<e[2], e[1]>> : e \\in GE}\n HN == H.node HE == H.edge THE == {<<e[2], e[1]>> : e \\in HE}\n<1>1. /\\ G = [node |-> GN, edge |-> GE]\n /\\ GE \\subseteq GN \\X GN\n /\\ H = [node |-> HN, edge |-> HE]\n /\\ HE \\subseteq HN \\X HN\n BY DEF IsDirectedGraph \n<1>. HIDE DEF GN, GE, HN, HE \n<1>2. /\\ Transpose(G) = [node |-> GN, edge |-> TGE]\n /\\ Transpose(H) = [node |-> HN, edge |-> THE]\n BY <1>1 DEF Transpose \n<1>3. IsTreeWithRoot([node |-> GN \\cup HN,\n edge |-> TGE \\cup THE \\cup {<<s,n>>}], r)\n BY <1>1, <1>2, AttachTree \n<1>4. {<<e[2], e[1]>> : e \\in GE \\cup HE \\cup {<<n,s>>}} = TGE \\cup THE \\cup {<<s,n>>}\n BY <1>1\n<1>. QED BY <1>1, <1>3, <1>4 DEF Transpose \n\nTHEOREM AddLeafToTransposedTree == \n ASSUME NEW G, NEW r, NEW leaf, NEW parent,\n IsDirectedGraph(G), IsTreeWithRoot(Transpose(G),r),\n leaf \\notin G.node, parent \\in G.node\n PROVE IsTreeWithRoot(Transpose([node |-> G.node \\cup {leaf}, \n edge |-> G.edge \\cup {<<parent, leaf>>}]), r)\n<1>. DEFINE N == G.node\n E == G.edge\n TE == {<<e[2], e[1]>> : e \\in G.edge}\n<1>1. /\\ G = [node |-> N, edge |-> E]\n /\\ E \\subseteq N \\X N \n BY DEF IsDirectedGraph \n<1>. HIDE DEF N, E \n<1>2. Transpose(G) = [node |-> N, edge |-> TE]\n BY <1>1 DEF Transpose \n<1>3. IsTreeWithRoot([node |-> N \\cup {leaf},\n edge |-> TE \\cup {<<leaf, parent>>}], r)\n BY <1>1, <1>2, AddLeafToTree\n<1>4. {<<e[2], e[1]>> : e \\in E \\cup {<<parent, leaf>>}} = TE \\cup {<<leaf, parent>>}\n BY <1>1\n<1>. QED BY <1>1, <1>3, <1>4 DEF Transpose \n\nTHEOREM RemoveTransposedSubtree ==\n ASSUME NEW G, NEW r, IsDirectedGraph(G), IsTreeWithRoot(Transpose(G), r),\n NEW nd \\in G.node \\ {r}\n PROVE LET S == {n \\in G.node : AreConnectedIn(nd, n, G)}\n T == G.node \\ S \n E == G.edge \\cap (T \\X T)\n IN IsTreeWithRoot(Transpose([node |-> T, edge |-> E]), r)\n<1>. DEFINE GN == G.node GE == G.edge TGE == {<<e[2], e[1]>> : e \\in GE}\n S == {n \\in GN : AreConnectedIn(nd, n, G)}\n T == GN \\ S \n E == GE \\cap (T \\X T)\n TG == Transpose(G)\n TS == {n \\in TG.node : AreConnectedIn(n, nd, TG)}\n TT == TG.node \\ TS \n TE == TG.edge \\cap (TT \\X TT)\n<1>1. /\\ G = [node |-> GN, edge |-> GE]\n /\\ GE \\subseteq GN \\X GN\n BY DEF IsDirectedGraph \n<1>. HIDE DEF GN, GE \n<1>2. TG = [node |-> GN, edge |-> TGE]\n BY <1>1 DEF Transpose \n<1>3. /\\ IsTreeWithRoot(TG, r)\n /\\ nd \\in TG.node \\ {r}\n BY <1>1, <1>2\n<1>4. IsTreeWithRoot([node |-> TT, edge |-> TE], r)\n BY <1>3, RemoveSubtree, IsaM(\"blast\")\n<1>5. T = TT\n BY TransposeAreConnectedIn, AreConnectedInTranspose, <1>1, <1>2\n<1>6. {<<e[2], e[1]>> : e \\in E} = TE \n BY <1>1, <1>2, <1>5\n<1>7. IsTreeWithRoot(Transpose([node |-> T, edge |-> E]), r)\n <2>. HIDE DEF T, TT, E, TE \n <2>. QED BY <1>4, <1>5, <1>6 DEF Transpose \n<1>. QED BY <1>7 DEF GN, GE \n\nTHEOREM RemoveLeafFromTransposedTree ==\n ASSUME NEW G, IsDirectedGraph(G),\n NEW r, IsTreeWithRoot(Transpose(G),r),\n NEW leaf \\in G.node,\n Successors(G, leaf) = {},\n NEW parent \\in G.node, <<parent, leaf>> \\in G.edge\n PROVE IsTreeWithRoot(Transpose([node |-> G.node \\ {leaf}, \n edge |-> G.edge \\ {<<parent, leaf>>}]), r)\n<1>. DEFINE N == G.node\n E == G.edge\n TE == {<<e[2], e[1]>> : e \\in G.edge}\n<1>1. /\\ G = [node |-> N, edge |-> E]\n /\\ E \\subseteq N \\X N \n BY DEF IsDirectedGraph \n<1>. HIDE DEF N, E \n<1>2. /\\ Transpose(G) = [node |-> N, edge |-> TE]\n /\\ Predecessors(Transpose(G), leaf) = {}\n BY <1>1 DEF Transpose, Predecessors, Successors\n<1>3. IsTreeWithRoot([node |-> N \\ {leaf},\n edge |-> TE \\ {<<leaf, parent>>}], r)\n BY <1>1, <1>2, RemoveLeafFromTree \n<1>4. {<<e[2], e[1]>> : e \\in E \\ {<<parent, leaf>>}} = TE \\ {<<leaf, parent>>}\n BY <1>1\n<1>. QED BY <1>1, <1>3, <1>4 DEF Transpose \n\n==============================================================================\n", "GraphViz": "---------------------------- MODULE GraphViz -------------------------------\n\nDotDiGraph(G, VL(_), EL(_)) ==\n\tTRUE\n\n===============================================================================", "Graphs": "------------------------------- MODULE Graphs --------------------------------\r\n(****************************************************************************)\r\n(* Representation of (directed) graphs in TLA+. *)\r\n(* A graph is represented as a record with two fields: *)\r\n(* - node holds the set of nodes of the graph, *)\r\n(* - edge holds the set of edges, represented as pairs of nodes. *)\r\n(* The definitions of the operators SimplePath, AreConnectedIn, and *)\r\n(* IsStronglyConnected are overridden by TLC with methods defined in *)\r\n(* tlc2/overrides/Graphs.java. *)\r\n(****************************************************************************)\r\n\r\nEXTENDS Naturals, Sequences, FiniteSets, SequencesExt, Relation\r\n\r\n(* TLAPM does not play well with LOCAL INSTANCE. \r\n Reinstate the following when that issue is fixed.\r\nLOCAL INSTANCE Naturals\r\nLOCAL INSTANCE Sequences\r\nLOCAL INSTANCE FiniteSets\r\nLOCAL INSTANCE SequencesExt\r\nLOCAL INSTANCE Relation\r\n*)\r\n\r\n(***************************************************************************)\r\nIsDirectedGraph(G) ==\r\n /\\ G = [node |-> G.node, edge |-> G.edge]\r\n /\\ G.edge \\subseteq (G.node \\X G.node)\r\n\r\nDirectedSubgraph(G) == \r\n {H \\in [node : SUBSET G.node, edge : SUBSET (G.node \\X G.node)] :\r\n IsDirectedGraph(H) /\\ H.edge \\subseteq G.edge}\r\n\r\nTranspose(G) ==\r\n \\* https://en.wikipedia.org/wiki/Transpose_graph\r\n [ edge |-> { <<e[2], e[1]>> : e \\in G.edge }, \r\n node |-> G.node] \r\n\r\n-----------------------------------------------------------------------------\r\n(***************************************************************************)\r\n(* An undirected graph can be represented as a directed graph with a *)\r\n(* symmetric edge relation. However, see alternative definitions of *)\r\n(* undirected graphs in module UndirectedGraphs. *)\r\n(***************************************************************************)\r\nIsUndirectedGraph(G) ==\r\n /\\ IsDirectedGraph(G)\r\n /\\ \\A e \\in G.edge : <<e[2], e[1]>> \\in G.edge\r\n\r\nUndirectedSubgraph(G) == {H \\in DirectedSubgraph(G) : IsUndirectedGraph(H)}\r\n-----------------------------------------------------------------------------\r\n(***************************************************************************)\r\n(* A path in a graph is a non-empty sequence of nodes connected by edges. *)\r\n(* A simple path is a path that does not contain duplicate nodes. *)\r\n(* Two nodes m and n are connected if there exists a path from m to n. *)\r\n(* A graph is strongly connected if all of its nodes are connected. *)\r\n(***************************************************************************)\r\nPath(G) == {p \\in Seq(G.node) :\r\n /\\ p # << >>\r\n /\\ \\A i \\in 1..(Len(p)-1) : <<p[i], p[i+1]>> \\in G.edge}\r\n\r\nSimplePath(G) ==\r\n { p \\in Path(G) : \\A i,j \\in 1..Len(p) : p[i] = p[j] => i = j }\r\n\r\nAreConnectedIn(m, n, G) == \r\n \\E p \\in Path(G) : (p[1] = m) /\\ (p[Len(p)] = n)\r\n\r\nIsStronglyConnected(G) == \r\n \\* A graph is strongly connected if all pairs of nodes are connected.\r\n \\A m, n \\in G.node : AreConnectedIn(m, n, G) \r\n\r\nConnectionsIn(G) ==\r\n \\* Compute a Boolean matrix that indicates, for each pair of nodes,\r\n \\* if there exists a path that links the two nodes. The computation,\r\n \\* based on Warshall's algorithm, is much more efficient than the\r\n \\* definition used in AreConnectedIn, and the result can be cached\r\n \\* by TLC, avoiding recomputation.\r\n \\* Note that this is well-defined only for finite graphs.\r\n LET C[N \\in SUBSET G.node] ==\r\n \\* Matrix representing the existence of paths whose inner nodes\r\n \\* (i.e., except for the source and the target) are all in the\r\n \\* set of nodes N.\r\n IF N = {}\r\n THEN [m,n \\in G.node |-> m = n \\/ <<m,n>> \\in G.edge]\r\n ELSE LET u == CHOOSE u \\in N : TRUE\r\n Cu == C[N \\ {u}]\r\n IN [m,n \\in G.node |-> \\/ Cu[m,n]\r\n \\/ Cu[m,u] /\\ Cu[u,n]]\r\n IN C[G.node]\r\n\r\n-----------------------------------------------------------------------------\r\n(***************************************************************************)\r\n(* A tree is a directed graph (with edges pointing towards the root) *)\r\n(* such that: *)\r\n(* - the root is a node of the graph (in particular, a tree is non-empty), *)\r\n(* - every node has a single parent, and *)\r\n(* - every node is connected to the root. *)\r\n(* *)\r\n(* Note that a tree with edges pointing towards the leaves satisfies *)\r\n(* IsTreeWithRoot(Transpose(G), r). *)\r\n(***************************************************************************)\r\nIsTreeWithRoot(G, r) ==\r\n /\\ IsDirectedGraph(G)\r\n /\\ r \\in G.node\r\n /\\ \\A e \\in G.edge : /\\ e[1] # r\r\n /\\ \\A f \\in G.edge : (e[1] = f[1]) => (e = f)\r\n /\\ \\A n \\in G.node : AreConnectedIn(n, r, G)\r\n\r\n-----------------------------------------------------------------------------\r\n(*************************************************************)\r\n(* Returns the union of two graphs. *)\r\n(* *)\r\n(* Example: *)\r\n(* G = [node |-> {1, 2}, edge |-> {<<1, 2>>}] *)\r\n(* H = [node |-> {2, 3}, edge |-> {<<2, 3>>}] *)\r\n(* GraphUnion(G, H) *)\r\n(* = [node |-> {1, 2, 3}, edge |-> {<<1, 2>>, <<2, 3>>}] *)\r\n(*************************************************************)\r\nGraphUnion(G, H) ==\r\n [node |-> G.node \\union H.node, edge |-> G.edge \\union H.edge]\r\n\r\n(********************************************************************)\r\n(* Checks whether the graph G is bipartite with partitions U and V. *)\r\n(* *)\r\n(* Example: *)\r\n(* G = [node |-> {1, 2, 3, 4}, *)\r\n(* edge |-> {<<1, 2>>, <<2, 3>>, <<3, 4>>}] *)\r\n(* IsBipartiteWithPartitions(G, {1, 3}, {2, 4}) = TRUE *)\r\n(********************************************************************)\r\nIsBipartiteWithPartitions(G, U, V) ==\r\n /\\ U \\cap V = {}\r\n /\\ G.node \\subseteq (U \\cup V)\r\n /\\ \\A e \\in G.edge: \\/ e[1] \\in U /\\ e[2] \\in V\r\n \\/ e[2] \\in U /\\ e[1] \\in V\r\n\r\n(**************************************************************************)\r\n(* Checks whether the graph G contains a cycle. *)\r\n(* *)\r\n(* Note: Relies on the definition of ConnectionsIn. Please note that this *)\r\n(* operator is defined recursively. *)\r\n(**************************************************************************)\r\nHasCycle(G) ==\r\n \\/ \\E n \\in G.node: << n, n >> \\in G.edge\r\n \\/ \\E m, n \\in G.node:\r\n /\\ m # n\r\n /\\ ConnectionsIn(G)[m, n]\r\n /\\ ConnectionsIn(G)[n, m]\r\n\r\n(**************************************************************************)\r\n(* Checks whether the directed graph G is a directed Acyclic Graph (DAG). *)\r\n(**************************************************************************)\r\nIsDag(G) ==\r\n /\\ IsDirectedGraph(G)\r\n /\\ ~HasCycle(G)\r\n\r\n(**************************************************************************)\r\n(* Returns the set of nodes that are immediate successors of node n in G. *)\r\n(* *)\r\n(* Example: *)\r\n(* G = [node |-> {1, 2, 3}, edge |-> {<<1, 2>>, <<1, 3>>}] *)\r\n(* Successors(G, 1) = {2, 3} *)\r\n(**************************************************************************)\r\nSuccessors(G, n) == {m \\in G.node: << n, m >> \\in G.edge}\r\n\r\n(****************************************************************************)\r\n(* Returns the set of nodes that are immediate successors of any node in S. *)\r\n(* *)\r\n(* Example: *)\r\n(* G = [node |-> {1, 2, 3}, edge |-> {<<1, 2>>, <<1, 3>>}] *)\r\n(* AllSuccessors(G, {1, 2}) = {2, 3} *)\r\n(****************************************************************************)\r\nAllSuccessors(G, S) == UNION {Successors(G, n): n \\in S}\r\n\r\n(****************************************************************************)\r\n(* Returns the set of nodes that are immediate predecessors of node n in G. *)\r\n(* *)\r\n(* Example: *)\r\n(* G = [node |-> {1, 2, 3}, edge |-> {<<2, 1>>, <<3, 1>>}] *)\r\n(* Predecessors(G, 1) = {2, 3} *)\r\n(****************************************************************************)\r\nPredecessors(G, n) == {m \\in G.node: << m, n >> \\in G.edge}\r\n\r\n(******************************************************************************)\r\n(* Returns the set of nodes that are immediate predecessors of any node in S. *)\r\n(* *)\r\n(* Example: *)\r\n(* G = [node |-> {1, 2, 3}, edge |-> {<<2, 1>>, <<3, 1>>}] *)\r\n(* AllPredecessors(G, {1, 2}) = {2, 3} *)\r\n(******************************************************************************)\r\nAllPredecessors(G, S) == UNION {Predecessors(G, n): n \\in S}\r\n\r\n(***************************************************************************)\r\n(* Returns the set of all nodes in graph G that have a path to node n. *)\r\n(* *)\r\n(* Example: *)\r\n(* G == [node |-> {1, 2, 3, 4}, edge |-> {<<4, 2>>, <<2, 1>>, <<3, 1>>}] *)\r\n(* Ancestors(G, 1) = {2, 3, 4} *)\r\n(***************************************************************************)\r\nAncestors(G, n) ==\r\n LET EdgeRelation ==\r\n\\* revert to the following syntax once TLAPS uses SANY\r\n\\* (https://github.com/tlaplus/tlapm/issues/213)\r\n\\* [<<x, y>> \\in G.node \\X G.node |-> <<x, y>> \\in G.edge]\r\n [p \\in G.node \\X G.node |-> <<p[1], p[2]>> \\in G.edge]\r\n IN { m \\in G.node : TransitiveClosure(EdgeRelation, G.node)[m, n] }\r\n\r\n(***************************************************************************)\r\n(* Returns the set of all nodes in graph G that are reachable from node n *)\r\n(* *)\r\n(* Example: *)\r\n(* G == [node |-> {1, 2, 3, 4}, edge |-> {<<4, 2>>, <<2, 1>>, <<3, 1>>}] *)\r\n(* Descendants(G, 4) = {1, 2} *)\r\n(***************************************************************************)\r\nDescendants(G, n) ==\r\n LET EdgeRelation ==\r\n\\* revert to the following syntax once TLAPS uses SANY\r\n\\* (https://github.com/tlaplus/tlapm/issues/213)\r\n\\* [<<x, y>> \\in G.node \\X G.node |-> <<x, y>> \\in G.edge]\r\n [p \\in G.node \\X G.node |-> <<p[1], p[2]>> \\in G.edge]\r\n IN { m \\in G.node : TransitiveClosure(EdgeRelation, G.node)[n, m] }\r\n\r\n(*************************************************************)\r\n(* Returns the in-degree of node n in directed graph G. *)\r\n(* *)\r\n(* Example: *)\r\n(* G = [node |-> {1, 2, 3}, edge |-> {<<2, 1>>, <<3, 1>>}] *)\r\n(* InDegree(G, 1) = 2 *)\r\n(*************************************************************)\r\nInDegree(G, n) == Cardinality(Predecessors(G, n))\r\n\r\n(*************************************************************)\r\n(* Returns the out-degree of node n in directed graph G. *)\r\n(* *)\r\n(* Example: *)\r\n(* G = [node |-> {1, 2, 3}, edge |-> {<<1, 2>>, <<1, 3>>}] *)\r\n(* OutDegree(G, 1) = 2 *)\r\n(*************************************************************)\r\nOutDegree(G, n) == Cardinality(Successors(G, n))\r\n\r\n(***************************************)\r\n(* Returns the set of root nodes of G. *)\r\n(***************************************)\r\nRoots(G) == {n \\in G.node: Predecessors(G, n) = {}}\r\n\r\n(***************************************)\r\n(* Returns the set of leaf nodes of G. *)\r\n(***************************************)\r\nLeaves(G) == {n \\in G.node: Successors(G, n) = {}}\r\n-----------------------------------------------------------------------------\r\n(*****************************************)\r\n(* The graph with no nodes and no edges. *)\r\n(*****************************************)\r\nEmptyGraph == [node |-> {}, edge |-> {}]\r\n\r\n(************************************************************************)\r\n(* The set of all possible labeled directed graphs whose node set is S. *)\r\n(* *)\r\n(* Example: *)\r\n(* Graphs({1, 2}) = { *)\r\n(* [node |-> {1, 2}, edge |-> {}], *)\r\n(* [node |-> {1, 2}, edge |-> {<<1, 2>>}], *)\r\n(* [node |-> {1, 2}, edge |-> {<<1, 2>>, <<2, 1>>}], *)\r\n(* ... *)\r\n(* } *)\r\n(************************************************************************)\r\nGraphs(S) == [node: {S}, edge: SUBSET (S \\X S)]\r\n=============================================================================\r\n", "HTML": "-------------------------------- MODULE HTML --------------------------------\n\nLOCAL INSTANCE Sequences\nLOCAL INSTANCE SequencesExt\n\n\\***** Helpers\n\nHTMLFill(string, args) ==\n (***********************************************************************)\n (* Do the sequence of replaces in args to the string. *)\n (* The args is a sequence of tuples: <<substr,newsubstr>>. *)\n (* Example: *)\n (* HTMLFill(\"test %num%\", << <<\"%num%\", \"12\">> >>) = \"test 12\" *)\n (***********************************************************************)\n LET f(str, x) == ReplaceFirstSubSeq(x[2], x[1], str)\n IN FoldLeft(f, string, args)\n\nLOCAL StringSeqToString(string_seq, separator) ==\n (***********************************************************************)\n (* Concatenates sequence of strings into one string. *)\n (* The `seperator` is put between the strings in the sequence. *)\n (* Example: *)\n (* StringSeqToString(<<\"Test\",\"string!\">>, \"-\") = \"Test-string!\" *)\n (***********************************************************************)\n LET f(str1,str2) == IF str1 = \"\" THEN str2 ELSE str1 \\o separator \\o str2\n IN FoldLeft(f, \"\", string_seq)\n\n\\***** Main Document\n\nHTMLDocument(header, body, list_of_scripts) ==\n StringSeqToString(<<\n \"<!DOCTYPE html>\",\n \"<html>\",\n header,\n body,\n StringSeqToString(list_of_scripts,\"\\n\"),\n \"</html>\"\n >>, \"\\n\")\n\n\\***** Style Sheet\n\nHTMLClass(classname, attribute_list) ==\n StringSeqToString(<<\n ReplaceFirstSubSeq(classname, \"%classname%\", \"%classname% {\"),\n StringSeqToString(attribute_list,\"\\n\"),\n \"}\"\n >>, \"\\n\")\n\nHTMLAttribute(name, value) ==\n HTMLFill(\"%name%: %value%;\", <<\n <<\"%name%\", name>>,\n <<\"%value%\", value>>\n >>)\n\nHTMLDefaultStyle ==\n StringSeqToString(<<\n HTMLClass(\".default_grid\",<<\n HTMLAttribute(\"display\", \"grid\")\n >>),\n HTMLClass(\".default_box\",<<\n HTMLAttribute(\"border\", \"1px solid black\")\n >>),\n HTMLClass(\".default_circle\",<<\n HTMLAttribute(\"border-radius\", \"50%\")\n >>)\n >>, \"\\n\")\n\nHTMLStyleSheet(class_list) ==\n StringSeqToString(<<\n \"<style type=\\\"text/css\\\">\",\n HTMLDefaultStyle,\n StringSeqToString(class_list,\"\\n\"),\n \"</style>\"\n >>, \"\\n\")\n\n\\***** Script\n\nHTMLScriptFile(name) ==\n HTMLFill(\"<script src=\\\"%name%\\\"></script>\", << <<\"%name%\", name>>>> )\n\nHTMLScript(children) ==\n StringSeqToString(<<\n \"<script>\",\n StringSeqToString(children, \"\\n\"),\n \"</script>\"\n >>, \"\\n\")\n\n\\***** Header\n\nHTMLLink(name, type) ==\n HTMLFill(\"<link href=\\\"%name%\\\" rel=\\\"%type%\\\"/>\",\n << <<\"%name%\", name>>, <<\"%type%\", type>> >> )\n\nHTMLHeader(title, list_of_links, class_list) ==\n StringSeqToString(<<\n \"<head>\",\n \"<meta charset=\\\"UTF-8\\\">\",\n ReplaceFirstSubSeq(title, \"%title%\", \"<title>%title%</title>\"),\n StringSeqToString(list_of_links,\"\\n\"),\n HTMLStyleSheet(class_list),\n \"</head>\"\n >>, \"\\n\")\n\n\\***** Body\n\nHTMLBody(body) ==\n StringSeqToString(<<\n \"<body>\",\n body,\n \"</body>\"\n >>, \"\\n\")\n\nHTMLFrame(id, children) ==\n StringSeqToString(<<\n ReplaceFirstSubSeq(id, \"%id%\", \"<div id=\\\"%id%\\\">\"),\n StringSeqToString(children, \"\\n\"),\n \"</div>\"\n >>, \"\\n\")\n\nHTMLGridContainer(id, class_list, children) ==\n StringSeqToString(<<\n HTMLFill(\"<div id=\\\"%id%\\\" class=\\\"default_grid %class_list%\\\">\",<<\n <<\"%id%\", id>>,\n <<\"%class_list%\", StringSeqToString(class_list, \" \")>>\n >>),\n StringSeqToString(children, \"\\n\"),\n \"</div>\"\n >>, \"\\n\")\n\nHTMLBox(id, class_list, size, children) ==\n StringSeqToString(<<\n HTMLFill(\"<div id=\\\"%id%\\\" class=\\\"default_box %class_list%\\\" style=\\\"%size%\\\">\",<<\n <<\"%id%\", id>>,\n <<\"%class_list%\", StringSeqToString(class_list, \" \")>>,\n <<\"%size%\", size>>\n >>),\n StringSeqToString(children, \"\\n\"),\n \"</div>\"\n >>, \"\\n\")\n\nHTMLCircle(id, class_list, size, children) ==\n HTMLBox(id, <<\"default_circle\">> \\o class_list, size, children)\n\nHTMLText(id, text) ==\n StringSeqToString(<<\n ReplaceFirstSubSeq(id, \"%id%\", \"<span id=\\\"%id%\\\">\"),\n text,\n \"</span>\"\n >>, \"\\n\")\n\nHTMLSVG(id, viewBox, size, svgString) ==\n StringSeqToString(<<\n HTMLFill(\"<svg id=\\\"%id%\\\" viewBox=\\\"%viewBox%\\\" style=\\\"%size%\\\">\",<<\n <<\"%id%\", id>>,\n <<\"%viewBox%\", viewBox>>,\n <<\"%size%\", size>>\n >>),\n svgString,\n \"</svg>\"\n >>, \"\\n\")\n\n\\***** Pre built\n\nHTMLSize(width, height) ==\n StringSeqToString(<<\n HTMLAttribute(\"width\", width),\n HTMLAttribute(\"height\", height)\n >>, \" \")\n\nHTMLNewLine ==\n \"<br>\"\n\nHTMLFlexCenterAttributes ==\n StringSeqToString(<<\n HTMLAttribute(\"display\", \"flex\"),\n HTMLAttribute(\"align-items\", \"center\"),\n HTMLAttribute(\"justify-content\", \"center\")\n >>,\"\\n\")\n\nHTMLJSLineElems(fromID, destID) ==\n HTMLFill(\"document.getElementById(\\\"%id1%\\\"), document.getElementById(\\\"%id2%\\\")\",\n << <<\"%id1%\", fromID>>, <<\"%id2%\", destID>> >> )\n\nHTMLJSLine(fromID, destID, color, size) ==\n HTMLFill(\"new LeaderLine(%elems%, {color: '%color%', size: %size%})\",\n << <<\"%elems%\", HTMLJSLineElems(fromID, destID)>>,\n <<\"%color%\", color>>, <<\"%size%\", size>> >> )\n\nHTMLJSLineDash(fromID, destID, color, size) ==\n HTMLFill(\"new LeaderLine(%elems%, {color: '%color%', size: %size%, dash: true})\",\n << <<\"%elems%\", HTMLJSLineElems(fromID, destID)>>,\n <<\"%color%\", color>>, <<\"%size%\", size>> >> )\n\nHTMLJSKeyListener(events) ==\n StringSeqToString(<<\n \"window.onkeyup = function(event) {\",\n \"let key = event.key.toUpperCase();\",\n StringSeqToString(events,\"\\n\"),\n \"}\"\n >>, \"\\n\")\n\nHTMLJSNavigationKey(key, file) ==\n StringSeqToString(<<\n ReplaceFirstSubSeq(key, \"%key%\", \"if(key == '%key%'){\"),\n ReplaceFirstSubSeq(file, \"%file%\", \"window.location.replace(\\\"%file%\\\");\"),\n \"}\"\n >>,\"\\n\")\n=============================================================================\n", "IOUtils": "------------------------------ MODULE IOUtils ------------------------------\n(***************************************************************************)\n(* This module provides operators for input/output operations and *)\n(* external process execution. All operators are overridden by Java *)\n(* implementations that perform the actual I/O operations. *)\n(* *)\n(* The module enables TLA+ specifications to: *)\n(* - Execute external commands and capture their output *)\n(* - Serialize/deserialize TLA+ values to/from files *)\n(* - Access environment variables *)\n(* - Interface with external systems during model checking *)\n(* *)\n(* Note: Some operators in this module evaluate to TRUE in TLA+ but *)\n(* perform side effects when executed by TLC with the Java overrides. *)\n(***************************************************************************)\n\nLOCAL INSTANCE TLC\nLOCAL INSTANCE Integers\nLOCAL INSTANCE Sequences\nLOCAL INSTANCE SequencesExt\n (*************************************************************************)\n (* Imports the definitions from the modules, but doesn't export them. *)\n (*************************************************************************)\n\n(***************************************************************************)\n(* Serializes the given value to the file in Java's object serialization *)\n(* format. *)\n(* *)\n(* Parameters: *)\n(* - val: The TLA+ value to serialize *)\n(* - absoluteFilename: Absolute path to the output file *)\n(* - compress: Boolean indicating whether to compress the output *)\n(***************************************************************************)\nIOSerialize(val, absoluteFilename, compress) == TRUE\n\n(***************************************************************************)\n(* Deserializes a TLA+ value from a file in Java's object serialization *)\n(* format. *)\n(* *)\n(* Parameters: *)\n(* - absoluteFilename: Absolute path to the input file *)\n(* - compressed: Boolean indicating whether the file is compressed *)\n(* *)\n(* Returns the deserialized TLA+ value. *)\n(***************************************************************************)\nIODeserialize(absoluteFilename, compressed) == CHOOSE val : TRUE\n\n(*******************************************************************************)\n(* value: TLA+ value to be serialized. *)\n(* dest: Destination to serialize to such as a file or URL. *)\n(* options: Record of serializer-specific options with format mandatory to *)\n(* identify a serializer. Read a seriazlier's documentation for serializer- *)\n(* specific options. *)\n(*******************************************************************************)\nSerialize(value, dest, options) ==\n CHOOSE r \\in [exitValue : Int, stdout : STRING, stderr : STRING] : TRUE\n\n(*******************************************************************************)\n(* src: Destination to serialize to such as a file or URL. *)\n(* options: Record of serializer-specific options with format mandatory to *)\n(* identify a serializer. Read a seriazlier's documentation for serializer- *)\n(* specific options. *)\n(*******************************************************************************)\nDeserialize(src, options) ==\n CHOOSE val : TRUE\n\n----------------------------------------------------------------------------\n\n(*******************************************************************************)\n(* Spawns the given command as a sub-process of TLC. The sequence of sequence *)\n(* of strings `command' signifies the external program file to be invoked and *)\n(* its arguments: *)\n(* IOExec(<<\"ls\", \"-lah\", \"/tmp\">>) *)\n(* IOExec(<<\"bash\", \"-c\", \"echo \\\"Col1#Col2\\\" > /tmp/out.csv\">> *)\n(* see https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html *)\n(*******************************************************************************)\nIOExec(command) ==\n CHOOSE r \\in [exitValue : Int, stdout : STRING, stderr : STRING] : TRUE\n\n(*******************************************************************************)\n(* See IOExec *)\n(* LET ENV = {<<\"Var1\", \"SomeVal\">>, <<\"Var2\", 42>>} *)\n(* IN IOEnvExec(ENV, <<\"ls\", \"-lah\", \"/tmp\">>) *)\n(*******************************************************************************)\nIOEnvExec(env, command) ==\n CHOOSE r \\in [exitValue : Int, stdout : STRING, stderr : STRING] : TRUE\n\n(*************************************************************************)\n(* Spawns the given printf-style command as a sub-process of TLC. The *)\n(* n-th flag in `commandTemplate' is substituted with the n-th element *)\n(* of the sequence `parameters': *)\n(* IOExecTemplate(<<\"ls\", \"%s\" \"%s\">>, <<\"-lah\", \"/tmp\">>) *)\n(* IOExecTemplate(<<\"ls %1$s %2$s\">>, <<\"-lah\", \"/tmp\">>) *)\n(* see http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html *)\n(*************************************************************************)\nIOExecTemplate(commandTemplate, parameters) ==\n CHOOSE r \\in [exitValue : Int, stdout : STRING, stderr : STRING] : TRUE\n\n(*******************************************************************************)\n(* See IOExecTemplate *)\n(* LET ENV = {<<\"Var1\", \"SomeVal\">>, <<\"Var2\", 42>>} *)\n(* IN IOEnvExecTemplate(ENV, <<\"ls\", \"-lah\", \"/tmp\">>) *)\n(*******************************************************************************)\nIOEnvExecTemplate(env, commandTemplate, parameters) ==\n CHOOSE r \\in [exitValue : Int, stdout : STRING, stderr : STRING] : TRUE\n\n(*************************************************************************)\n(* The process' environment variables. *)\n(*************************************************************************)\nIOEnv ==\n CHOOSE r \\in [STRING -> STRING] : TRUE\n\n(***************************************************************************)\n(* Converts a string representation of a number to a number. *)\n(* *)\n(* This is useful for processing environment variables or command output *)\n(* that contains numeric values as strings. *)\n(* *)\n(* Examples: *)\n(* atoi(\"42\") = 42 *)\n(* atoi(\"-17\") = -17 *)\n(* atoi(\"0\") = 0 *)\n(* *)\n(* Usage with environment variables: *)\n(* Assuming environment variable PORT is set to \"8080\": *)\n(* atoi(IOEnv.PORT) = 8080 *)\n(***************************************************************************)\natoi(str) ==\n CHOOSE i \\in Int : ToString(i) = str\n\n(***************************************************************************)\n(* Pads a natural number with leading zeros to achieve a specified width. *)\n(* *)\n(* This function converts a natural number to a string and pads it with *)\n(* leading zeros if necessary to reach the specified width. If the *)\n(* number's string representation is already equal to or longer than the *)\n(* specified width, no padding is added. *)\n(* *)\n(* Parameters: *)\n(* - n: The natural number to be padded (must be >= 0) *)\n(* - width: The desired total width of the resulting string *)\n(* *)\n(* Examples: *)\n(* zeroPadN(42, 5) = \"00042\" *)\n(* zeroPadN(123, 3) = \"123\" *)\n(* zeroPadN(7, 1) = \"7\" *)\n(* zeroPadN(0, 4) = \"0000\" *)\n(* *)\n(* Note: Negative numbers are not supported and may produce unexpected *)\n(* results. *)\n(* *)\n(* Returns a sequence of characters representing the zero-padded number. *)\n(***************************************************************************)\nzeroPadN(n, width) ==\n LET s == ToString(n)\n padLen == width - Len(s)\n zeros == [i \\in 1..padLen |-> \"0\"]\n IN FoldLeft(LAMBDA acc, z: acc \\o z, \"\", zeros) \\o s\n============================================================================\n", "Json": "-------------------------------- MODULE Json --------------------------------\n\nLOCAL INSTANCE Sequences\nLOCAL INSTANCE FiniteSets\nLOCAL INSTANCE TLC\n (*************************************************************************)\n (* Imports the definitions from the modules, but doesn't export them. *)\n (*************************************************************************)\n\n-----------------------------------------------------------------------------\n\nLOCAL IsSequence(F, D) == TRUE \\/ FALSE\n\nLOCAL ToJsonKeyValue(F, d) ==\n ToString(ToString(d)) \\o \":\" \\o ToString(F[d])\n\nRECURSIVE ToJsonObjectString(_,_)\nLOCAL ToJsonObjectString(F, D) == \\* LOCAL just a hint for humans.\n LET d == CHOOSE d \\in D: TRUE\n IN IF D = DOMAIN F\n THEN \"{\" \\o ToJsonKeyValue(F, d) \\o IF D \\ {d} = {}\n THEN \"}\"\n ELSE ToJsonObjectString(F, D \\ {d})\n ELSE \", \" \\o ToJsonKeyValue(F, d) \\o IF D \\ {d} = {}\n THEN \"}\"\n ELSE ToJsonObjectString(F, D \\ {d})\n\nRECURSIVE ToJsonArrayString(_,_)\nLOCAL ToJsonArrayString(F, D) == \\* LOCAL just a hint for humans.\n LET d == CHOOSE d \\in D: TRUE\n IN IF D = DOMAIN F\n THEN \"[\" \\o ToString(F[d]) \\o IF D \\ {d} = {}\n THEN \"]\"\n ELSE ToJsonArrayString(F, D \\ {d})\n ELSE \", \" \\o ToString(F[d]) \\o IF D \\ {d} = {}\n THEN \"]\"\n ELSE ToJsonArrayString(F, D \\ {d})\n\nRECURSIVE ToJsonString(_,_)\nLOCAL ToJsonString(F, D) == \\* LOCAL just a hint for humans.\n IF IsFiniteSet(F) \\/ IsSequence(F, D) THEN\n ToJsonArrayString(F, D)\n ELSE\n ToJsonObjectString(F, D)\n\n(*************************************************************************)\n(* Converts the given value to a JSON string. Records are converted to *)\n(* JSON objects, tuples to JSON arrays, and scalar values as their JSON *)\n(* representation. *)\n(*************************************************************************)\nToJson(value) ==\n IF DOMAIN value = {} THEN\n \"{}\"\n ELSE\n ToJsonString(value, DOMAIN value)\n\n(*************************************************************************)\n(* Converts the given tuple value to a JSON array. *)\n(*************************************************************************)\nToJsonArray(value) ==\n IF DOMAIN value = {} THEN\n \"[]\"\n ELSE\n ToJsonArrayString(value, DOMAIN value)\n\n(*************************************************************************)\n(* Converts the given tuple value to a JSON object. *)\n(*************************************************************************)\nToJsonObject(value) ==\n IF DOMAIN value = {} THEN\n \"{}\"\n ELSE\n ToJsonObjectString(value, DOMAIN value)\n\n(*************************************************************************)\n(* Serializes a tuple of values to the given file as (plain) JSON. *)\n(* Records will be serialized as a JSON objects, and tuples as arrays. *)\n(*************************************************************************)\nJsonSerialize(absoluteFilename, value) ==\n TRUE\n\n(*************************************************************************)\n(* Deserializes JSON values from the given file. JSON objects will be *)\n(* deserialized to records, and arrays will be deserialized to tuples. *)\n(*************************************************************************)\nJsonDeserialize(absoluteFilename) ==\n CHOOSE val : TRUE\n\n(*************************************************************************)\n(* Serializes a tuple of values to the given file as newline delimited *)\n(* JSON. Records will be serialized as a JSON objects, and tuples as *)\n(* arrays. *)\n(*************************************************************************)\nndJsonSerialize(absoluteFilename, value) ==\n TRUE\n\n(*************************************************************************)\n(* Deserializes JSON values from the given file. JSON values must be *)\n(* separated by newlines. JSON objects will be deserialized to records, *)\n(* and arrays will be deserialized to tuples. *)\n(*************************************************************************)\nndJsonDeserialize(absoluteFilename) ==\n CHOOSE val : TRUE\n\n=============================================================================\n", "Quorum": "--------------------------------- MODULE Quorum -----------------------------\n\n(***************************************************************************)\n(* A quorum system for a set S is a non-empty collection of quorums, i.e. *)\n(* subsets of S such that any two quorums intersect. It is typically also *)\n(* assumed that a superset of a quorum is itself a quorum. *)\n(* *)\n(* For example, given a finite and non-empty set S of servers, the sets of *)\n(* strict majorities among servers form a quorum system. *)\n(***************************************************************************)\n\nQuorumSystem(S) ==\n { QS \\in SUBSET (SUBSET S) :\n /\\ QS # {}\n /\\ \\A Q1, Q2 \\in QS : Q1 \\cap Q2 # {}\n /\\ \\A Q1, Q2 \\in SUBSET S : Q1 \\in QS /\\ Q1 \\subseteq Q2 => Q2 \\in QS\n }\n\n=============================================================================\n", "QuorumTheorems": "----------------------------- MODULE QuorumTheorems -------------------------\nEXTENDS Quorum, Integers, FiniteSets\n\n(***************************************************************************)\n(* Direct consequences of the definition of a quorum system. *)\n(***************************************************************************)\n\nTHEOREM QuorumsIntersect ==\n ASSUME NEW S, NEW QS \\in QuorumSystem(S), NEW Q1 \\in QS, NEW Q2 \\in QS \n PROVE \\E s \\in S : s \\in Q1 \\cap Q2 \n\nTHEOREM QuorumSuperset ==\n ASSUME NEW S, NEW QS \\in QuorumSystem(S), \n NEW Q1 \\in QS, NEW Q2 \\in SUBSET S, Q1 \\subseteq Q2\n PROVE Q2 \\in QS \n\n(***************************************************************************)\n(* Strict majorities of a non-empty set S form a quorum system. *)\n(***************************************************************************)\n\nTHEOREM MajoritiesQuorumSystem ==\n ASSUME NEW S, IsFiniteSet(S), S # {}\n PROVE { Q \\in SUBSET S : 2 * Cardinality(Q) > Cardinality(S) }\n \\in QuorumSystem(S)\n\n=============================================================================\n", "QuorumTheorems_proofs": "------------------------- MODULE QuorumTheorems_proofs ----------------------\nEXTENDS Quorum, Integers, FiniteSetTheorems, FiniteSetsExtTheorems\n\n(***************************************************************************)\n(* Direct consequences of the definition of a quorum system. *)\n(***************************************************************************)\n\nTHEOREM QuorumsIntersect ==\n ASSUME NEW S, NEW QS \\in QuorumSystem(S), NEW Q1 \\in QS, NEW Q2 \\in QS \n PROVE \\E s \\in S : s \\in Q1 \\cap Q2 \nBY DEF QuorumSystem \n\nTHEOREM QuorumSuperset ==\n ASSUME NEW S, NEW QS \\in QuorumSystem(S), \n NEW Q1 \\in QS, NEW Q2 \\in SUBSET S, Q1 \\subseteq Q2\n PROVE Q2 \\in QS \nBY DEF QuorumSystem \n\n(***************************************************************************)\n(* Strict majorities of a non-empty set S form a quorum system. *)\n(***************************************************************************)\n\nTHEOREM MajoritiesQuorumSystem ==\n ASSUME NEW S, IsFiniteSet(S), S # {}\n PROVE { Q \\in SUBSET S : 2 * Cardinality(Q) > Cardinality(S) }\n \\in QuorumSystem(S)\nBY FS_CardinalityType, FS_EmptySet, FS_Subset, FS_Union DEF QuorumSystem\n\n=============================================================================\n", "Relation": "----------------------------- MODULE Relation ------------------------------\nEXTENDS Naturals, FiniteSets \n(* TLAPM does not play well with LOCAL INSTANCE.\n Reinstate the following when that issue is fixed.\nLOCAL INSTANCE Naturals\nLOCAL INSTANCE FiniteSets\n*)\n\n(***************************************************************************)\n(* This module provides some basic operations on relations, represented *)\n(* as binary Boolean functions over some set S. *)\n(* *)\n(* Relations are represented as functions R: S × S → BOOLEAN, where *)\n(* R[x,y] = TRUE means that x is related to y. *)\n(* *)\n(* The module includes: *)\n(* - Basic relation properties (reflexive, symmetric, transitive, etc.) *)\n(* - Ordering relations (partial orders, total orders) *)\n(* - Closure operations (transitive closure) *)\n(* - Connectivity analysis *)\n(* *)\n(* Each property has two variants: *)\n(* - Direct: Takes a relation R as a Boolean function *)\n(* - Under: Takes a binary operator and constructs the relation *)\n(***************************************************************************)\n\n(***************************************************************************)\n(* Is the relation R reflexive over S? *)\n(* *)\n(* A relation is reflexive if every element is related to itself. *)\n(* *)\n(* Examples: *)\n(* IsReflexive([<<x,y>> \\in {1,2}×{1,2} |-> x=y], {1,2}) = TRUE *)\n(* IsReflexiveUnder(=, {1,2,3}) = TRUE *)\n(* IsReflexiveUnder(<, {1,2,3}) = FALSE *)\n(***************************************************************************)\nIsReflexive(R, S) == \\A x \\in S : R[x,x]\n\nIsReflexiveUnder(op(_,_), S) ==\n\\* revert to the following syntax once TLAPS uses SANY\n\\* (https://github.com/tlaplus/tlapm/issues/213)\n\\* IsReflexive([<<x,y>> \\in S \\X S |-> op(x,y)], S)\n IsReflexive([p \\in S \\X S |-> op(p[1], p[2])], S)\n\n(***************************************************************************)\n(* Is the relation R irreflexive over set S? *)\n(* *)\n(* A relation is irreflexive if no element is related to itself. *)\n(* This is the opposite of reflexive. *)\n(* *)\n(* Examples: *)\n(* IsIrreflexiveUnder(<, {1,2,3}) = TRUE *)\n(* IsIrreflexiveUnder(<=, {1,2,3}) = FALSE *)\n(***************************************************************************)\nIsIrreflexive(R, S) == \\A x \\in S : ~ R[x,x]\n\nIsIrreflexiveUnder(op(_,_), S) ==\n\\* revert to the following syntax once TLAPS uses SANY\n\\* (https://github.com/tlaplus/tlapm/issues/213)\n\\* IsIrreflexive([<<x,y>> \\in S \\X S |-> op(x,y)], S)\n IsIrreflexive([p \\in S \\X S |-> op(p[1], p[2])], S)\n\n(***************************************************************************)\n(* Is the relation R symmetric over set S? *)\n(* *)\n(* A relation is symmetric if x related to y implies y related to x. *)\n(* *)\n(* Examples: *)\n(* IsSymmetricUnder(=, {1,2,3}) = TRUE *)\n(* IsSymmetricUnder(<, {1,2,3}) = FALSE *)\n(***************************************************************************)\nIsSymmetric(R, S) == \\A x,y \\in S : R[x,y] <=> R[y,x]\n\nIsSymmetricUnder(op(_,_), S) ==\n\\* revert to the following syntax once TLAPS uses SANY\n\\* (https://github.com/tlaplus/tlapm/issues/213)\n\\* IsSymmetric([<<x,y>> \\in S \\X S |-> op(x,y)], S)\n IsSymmetric([p \\in S \\X S |-> op(p[1], p[2])], S)\n\n(***************************************************************************)\n(* Is the relation R antisymmetric over set S? *)\n(* *)\n(* A relation is antisymmetric if x related to y and y related to x *)\n(* implies x equals y. *)\n(* *)\n(* Examples: *)\n(* IsAntiSymmetricUnder(<=, {1,2,3}) = TRUE *)\n(* IsAntiSymmetricUnder(=, {1,2,3}) = TRUE *)\n(***************************************************************************)\nIsAntiSymmetric(R, S) == \\A x,y \\in S : R[x,y] /\\ R[y,x] => x=y\n\nIsAntiSymmetricUnder(op(_,_), S) ==\n\\* revert to the following syntax once TLAPS uses SANY\n\\* (https://github.com/tlaplus/tlapm/issues/213)\n\\* IsAntiSymmetric([<<x,y>> \\in S \\X S |-> op(x,y)], S)\n IsAntiSymmetric([p \\in S \\X S |-> op(p[1], p[2])], S)\n\n(***************************************************************************)\n(* Is the relation R asymmetric over set S? *)\n(* *)\n(* A relation is asymmetric if it's never the case that both x is related *)\n(* to y and y is related to x (unless x=y). *)\n(* *)\n(* Examples: *)\n(* IsAsymmetricUnder(<, {1,2,3}) = TRUE *)\n(* IsAsymmetricUnder(<=, {1,2,3}) = FALSE (due to reflexivity) *)\n(***************************************************************************)\nIsAsymmetric(R, S) == \\A x,y \\in S : ~(R[x,y] /\\ R[y,x])\n\nIsAsymmetricUnder(op(_,_), S) ==\n\\* revert to the following syntax once TLAPS uses SANY\n\\* (https://github.com/tlaplus/tlapm/issues/213)\n\\* IsAsymmetric([<<x,y>> \\in S \\X S |-> op(x,y)], S)\n IsAsymmetric([p \\in S \\X S |-> op(p[1], p[2])], S)\n\n(***************************************************************************)\n(* Is the relation R transitive over set S? *)\n(* *)\n(* A relation is transitive if x related to y and y related to z implies *)\n(* x related to z. *)\n(* *)\n(* Examples: *)\n(* IsTransitiveUnder(<, {1,2,3}) = TRUE *)\n(* IsTransitiveUnder(=, {1,2,3}) = TRUE *)\n(***************************************************************************)\nIsTransitive(R, S) == \\A x,y,z \\in S : R[x,y] /\\ R[y,z] => R[x,z]\n\nIsTransitiveUnder(op(_,_), S) ==\n\\* revert to the following syntax once TLAPS uses SANY\n\\* (https://github.com/tlaplus/tlapm/issues/213)\n\\* IsTransitive([<<x,y>> \\in S \\X S |-> op(x,y)], S)\n IsTransitive([p \\in S \\X S |-> op(p[1], p[2])], S)\n\n(***************************************************************************)\n(* Is the set S strictly partially ordered under the (binary) relation R? *)\n(* *)\n(* A strict partial order combines irreflexivity, antisymmetry, and *)\n(* transitivity. *)\n(* *)\n(* Examples: *)\n(* IsStrictlyPartiallyOrderedUnder(<, {1,2,3}) = TRUE *)\n(* IsStrictlyPartiallyOrderedUnder(<=, {1,2,3}) = FALSE (not irreflexive)*)\n(***************************************************************************)\nIsStrictlyPartiallyOrdered(R, S) ==\n /\\ IsIrreflexive(R, S)\n /\\ IsAntiSymmetric(R, S)\n /\\ IsTransitive(R, S)\n\nIsStrictlyPartiallyOrderedUnder(op(_,_), S) ==\n\\* revert to the following syntax once TLAPS uses SANY\n\\* (https://github.com/tlaplus/tlapm/issues/213)\n\\* IsStrictlyPartiallyOrdered([<<x,y>> \\in S \\X S |-> op(x,y)], S)\n IsStrictlyPartiallyOrdered([p \\in S \\X S |-> op(p[1], p[2])], S)\n\n(***************************************************************************)\n(* Is the set S (weakly) partially ordered under the (binary) relation R? *)\n(* *)\n(* A partial order (poset) combines reflexivity, antisymmetry, and *)\n(* transitivity. *)\n(* *)\n(* Examples: *)\n(* IsPartiallyOrderedUnder(<=, {1,2,3}) = TRUE *)\n(* IsPartiallyOrderedUnder(<, {1,2,3}) = FALSE (not reflexive) *)\n(***************************************************************************)\nIsPartiallyOrdered(R, S) ==\n /\\ IsReflexive(R, S)\n /\\ IsAntiSymmetric(R, S)\n /\\ IsTransitive(R, S)\n\nIsPartiallyOrderedUnder(op(_,_), S) ==\n\\* revert to the following syntax once TLAPS uses SANY\n\\* (https://github.com/tlaplus/tlapm/issues/213)\n\\* IsPartiallyOrdered([<<x,y>> \\in S \\X S |-> op(x,y)], S)\n IsPartiallyOrdered([p \\in S \\X S |-> op(p[1], p[2])], S)\n\n(***************************************************************************)\n(* Is the set S strictly totally ordered under the (binary) relation R? *)\n(* *)\n(* A strict total order is a strict partial order where any two distinct *)\n(* elements are comparable (trichotomy property). *)\n(* *)\n(* Examples: *)\n(* IsStrictlyTotallyOrderedUnder(<, {1,2,3}) = TRUE *)\n(***************************************************************************)\nIsStrictlyTotallyOrdered(R, S) ==\n /\\ IsStrictlyPartiallyOrdered(R, S)\n \\* Trichotomy (R is irreflexive)\n /\\ \\A x,y \\in S : x # y => R[x,y] \\/ R[y,x]\n\nIsStrictlyTotallyOrderedUnder(op(_,_), S) ==\n\\* revert to the following syntax once TLAPS uses SANY\n\\* (https://github.com/tlaplus/tlapm/issues/213)\n\\* IsStrictlyTotallyOrdered([<<x,y>> \\in S \\X S |-> op(x,y)], S)\n IsStrictlyTotallyOrdered([p \\in S \\X S |-> op(p[1], p[2])], S)\n\n(***************************************************************************)\n(* Is the set S totally ordered under the (binary) relation R? *)\n(* *)\n(* A total order is a partial order where any two elements are comparable. *)\n(* *)\n(* Examples: *)\n(* IsTotallyOrderedUnder(<=, {1,2,3}) = TRUE *)\n(***************************************************************************)\nIsTotallyOrdered(R, S) ==\n /\\ IsPartiallyOrdered(R, S)\n /\\ \\A x,y \\in S : R[x,y] \\/ R[y,x]\n\nIsTotallyOrderedUnder(op(_,_), S) ==\n\\* revert to the following syntax once TLAPS uses SANY\n\\* (https://github.com/tlaplus/tlapm/issues/213)\n\\* IsTotallyOrdered([<<x,y>> \\in S \\X S |-> op(x,y)], S)\n IsTotallyOrdered([p \\in S \\X S |-> op(p[1], p[2])], S)\n\n(***************************************************************************)\n(* Compute the transitive closure of relation R over set S. *)\n(***************************************************************************)\nTransitiveClosure(R, S) ==\n LET N == Cardinality(S)\n trcl[n \\in Nat] == \n [x,y \\in S |-> IF n=0 THEN R[x,y]\n ELSE \\/ trcl[n-1][x,y]\n \\/ \\E z \\in S : trcl[n-1][x,z] /\\ trcl[n-1][z,y]]\n IN trcl[N]\n\n(***************************************************************************)\n(* Compute the reflexive transitive closure of relation R over set S. *)\n(***************************************************************************)\nReflexiveTransitiveClosure(R, S) ==\n LET trcl == TransitiveClosure(R,S)\n IN [x,y \\in S |-> x=y \\/ trcl[x,y]]\n\n(***************************************************************************)\n(* Is the relation R connected over set S, i.e. does there exist a path *)\n(* between two arbitrary elements of S? *)\n(***************************************************************************)\nIsConnected(R, S) ==\n LET rtrcl == ReflexiveTransitiveClosure(R,S)\n IN \\A x,y \\in S : rtrcl[x,y]\n\n=============================================================================\n", "SVG": "----------------------------- MODULE SVG -----------------------------\nLOCAL INSTANCE Naturals\nLOCAL INSTANCE Sequences\nLOCAL INSTANCE Integers\nLOCAL INSTANCE TLC\nLOCAL INSTANCE FiniteSets\n\n(******************************************************************************)\n(* Helper Operators *)\n(******************************************************************************)\n\nLOCAL Merge(r1, r2) == \n (**************************************************************************)\n (* Merge two records. *)\n (* *)\n (* If a field appears in both records, then the value from 'r1' is kept. *)\n (**************************************************************************)\n LET D1 == DOMAIN r1 \n D2 == DOMAIN r2 IN\n [k \\in (D1 \\cup D2) |-> IF k \\in D1 THEN r1[k] ELSE r2[k]]\n\n(**************************************************************************)\n(* SVG element constructor. *)\n(* *)\n(* An SVG element like: *)\n(* *)\n(* [name |-> \"elem\", attrs |-> [k1 |-> \"0\", k2 |-> \"1\"], children |-> *)\n(* <<>>, innerText |-> \"\"] *)\n(* *)\n(* would represent the SVG element '<elem k1=\"0\" k2=\"1\"></elem>' *)\n(**************************************************************************)\nSVGElem(_name, _attrs, _children, _innerText) == \n [name |-> _name, \n attrs |-> _attrs, \n children |-> _children,\n innerText |-> _innerText ]\n\n(******************************************************************************)\n(* *)\n(* Core Graphic Elements. *)\n(* *)\n(* These primitives are TLA+ wrappers around SVG elements. We base them on *)\n(* SVG since it is a widely used format and provides good features for *)\n(* representing and laying out vector graphics. Each primitive below should *)\n(* roughly correspond to a standard SVG element type. We represent a graphic *)\n(* element as a record with the fields 'name', 'attrs', 'children', and *)\n(* 'innerText'. 'name' is the name of the SVG element, 'attrs' is a record *)\n(* mapping SVG attribute names to values, 'children' is a tuple containing *)\n(* any children elements, and 'innerText' is a string that represents any raw *)\n(* text that is contained directly inside the SVG element. The 'innerText' *)\n(* field is only meaningful for text elements. *)\n(* *)\n(* All elements accept an 'attrs' argument, which must be a function mapping *)\n(* string keys to string values. These key-value pairs describe any *)\n(* additional SVG attributes of the element. To pass no attributes, just *)\n(* pass attrs=<<>>. *)\n(* *)\n(******************************************************************************)\n\n(**************************************************************************)\n(* Line element. 'x1', 'y1', 'x2', and 'y2' should be given as integers. *)\n(**************************************************************************)\nLine(x1, y1, x2, y2, attrs) == \n LET svgAttrs == [x1 |-> ToString(x1), \n y1 |-> ToString(y1), \n x2 |-> ToString(x2),\n y2 |-> ToString(y2)] IN\n SVGElem(\"line\", Merge(svgAttrs, attrs), <<>>, \"\")\n\n(**************************************************************************)\n(* Circle element. 'cx', 'cy', and 'r' should be given as integers. *)\n(**************************************************************************)\nCircle(cx, cy, r, attrs) == \n LET svgAttrs == [cx |-> ToString(cx), \n cy |-> ToString(cy), \n r |-> ToString(r)] IN\n SVGElem(\"circle\", Merge(svgAttrs, attrs), <<>>, \"\")\n\n(**************************************************************************)\n(* Rectangle element. 'x', 'y', 'w', and 'h' should be given as *)\n(* integers. *)\n(**************************************************************************)\nRect(x, y, w, h, attrs) == \n LET svgAttrs == [x |-> ToString(x), \n y |-> ToString(y), \n width |-> ToString(w), \n height |-> ToString(h)] IN\n SVGElem(\"rect\", Merge(svgAttrs, attrs), <<>>, \"\")\n\n(**************************************************************************)\n(* Text element.'x' and 'y' should be given as integers, and 'text' given *)\n(* as a string. *)\n(**************************************************************************)\nText(x, y, text, attrs) == \n LET svgAttrs == [x |-> ToString(x), \n y |-> ToString(y)] IN\n SVGElem(\"text\", Merge(svgAttrs, attrs), <<>>, text) \n\n(**************************************************************************)\n(* Image element. 'x', 'y', 'width', and 'height' should be given as *)\n(* integers, and 'href' given as a string specifying the image source. *)\n(**************************************************************************)\nImage(x, y, width, height, href, attrs) == \n LET svgAttrs == (\"xlink:href\" :> href @@\n \"x\" :> ToString(x) @@\n \"y\" :> ToString(y) @@\n \"width\" :> ToString(width) @@\n \"height\" :> ToString(height)) IN\n SVGElem(\"image\", Merge(svgAttrs, attrs), <<>>, \"\")\n\n(**************************************************************************)\n(* Group element. 'children' is a sequence of elements that will be *)\n(* contained in this group. *)\n(**************************************************************************)\nGroup(children, attrs) == \n SVGElem(\"g\", attrs, children, \"\")\n\n(**************************************************************************)\n(* Svg container. 'children' is a sequence of elements that will be *)\n(* contained in this svg container. *)\n(**************************************************************************)\nSvg(children, attrs) == \n SVGElem(\"svg\", attrs, children, \"\")\n\n(**************************************************************************)\n(* Creates a complete SVG document with viewBox and additional attributes.*)\n(* *)\n(* Parameters: *)\n(* children: A sequence of SVG elements to include in the document *)\n(* vbX, vbY: The x,y coordinates of the top-left corner of the viewBox *)\n(* vbW, vbH: The width and height of the viewBox *)\n(* attrs: Additional attributes to merge with the SVG root element *)\n(* *)\n(* The viewBox defines the coordinate system and viewport dimensions for *)\n(* the SVG content. This is essential for proper scaling and positioning *)\n(* of elements within the document. *)\n(**************************************************************************)\nSVGDoc(children, vbX, vbY, vbW, vbH, attrs) ==\n LET svgAttrs == (\"xmlns:xlink\" :> \"http://www.w3.org/1999/xlink\" @@\n \"xmlns\" :> \"http://www.w3.org/2000/svg\" @@\n \"viewBox\" :> ToString(vbX) \\o \" \" \\o\n ToString(vbY) \\o \" \" \\o\n ToString(vbW) \\o \" \" \\o\n ToString(vbH)) IN\n Svg(<<children>>, Merge(svgAttrs, attrs))\n\n(**************************************************************************)\n(* Convert an SVG element record into its string representation. *)\n(* *)\n(* This operator is expected to be replaced by a Java module override. *)\n(* We do not implement it in pure TLA+ because doing non-trivial string *)\n(* manipulation in TLA+ is tedious. Also, the performance of *)\n(* implementing this in TLA+ has been demonstrated to be prohibitively *)\n(* slow. *)\n(**************************************************************************)\nSVGElemToString(elem) == \n TRUE \n\n-------------------------------------------------------------------------------\n\n(**************************************************************************)\n(* Equals the Cartesian coordinates of the n-th of m nodes in a ring *)\n(* (https://en.wikipedia.org/wiki/Ring_network), such that the circle *)\n(* on which the nodes are placed has radius r and is centered at the *)\n(* coordinates cx, cy. *)\n(* *)\n(* ASSUME /\\ n \\in 0..360 /\\ m \\in 0..360 /\\ n <= m *)\n(* /\\ cx \\in Nat /\\ cy \\in Nat /\\ r \\in Nat *)\n(* *)\n(* Example to create a ring network of M nodes (Rects with dimension 15) *)\n(* with center (10,10) and radius 5: *)\n(* *)\n(* ASSUME M \\in Nat *)\n(* *)\n(* RN[ n \\in 1..M ] == *)\n(* LET c == NodeOfRingNetwork(10, 10, 5, n, M) *)\n(* node == Rect(c.x, c.y, 15, 15, <<>>) *)\n(* IN Group(<<node>>, <<>>) *)\n(* *)\n(* Note: It would have been more elegant to provide the basic geometric *)\n(* operators, such as polar to Cartesian conversion. However, *)\n(* TLC's lack of floats makes this impractical due to intermediate *)\n(* rounding errors. *)\n(* *)\n(* For this operator's actual definition, please consult the Java module *)\n(* override at: *)\n(* modules/tlc2/overrides/SVG.java#ringNetwork *)\n(**************************************************************************)\nNodeOfRingNetwork(cx, cy, r, n, m) ==\n [ x |-> 0, y |-> 0 ]\n\n(**************************************************************************)\n(* Example to layout a graph with the given Nodes and Edges: *)\n(* *)\n(* Nodes == {\"v1\", \"v2\", \"v3\"} \\* v3 is not connected *)\n(* Edges == {<<\"v1\", \"v2\">>, <<\"v2\", \"v1\">>} *)\n(* Graph == NodesOfDirectedMultiGraph(Nodes, Edges, [algo |-> ...]) *)\n(* *)\n(* RN[ n \\in Nodes ] == *)\n(* LET c == Graph[n] *)\n(* node == Rect(c.x, c.y, 23, 42, <<>>) *)\n(* IN Group(<<node>>, <<>>) *)\n(* *)\n(* For this operator's actual definition, please consult the Java module *)\n(* override at: *)\n(* modules/tlc2/overrides/SVG.java#directedMultiGraph *)\n(**************************************************************************)\nNodesOfDirectedMultiGraph(nodes, edges, options) ==\n CHOOSE f \\in [ nodes -> [x: Int, y: Int] ]: TRUE\n\nPointOnLine(from, to, segment) ==\n [x |-> from.x + ((to.x - from.x) \\div segment), \n y |-> from.y + ((to.y - from.y) \\div segment)]\n\n-------------------------------------------------------------------------------\n\n(**************************************************************************)\n(* Serializes an SVG element to a file on disk. *)\n(* *)\n(* Parameters: *)\n(* svg: The SVG element/document to serialize *)\n(* frameNamePrefix: String prefix for the output filename *)\n(* frameNumber: Numeric identifier appended to create unique files *)\n(* *)\n(* Creates a file named \"<frameNamePrefix><frameNumber>.svg\" containing *)\n(* the serialized SVG content. This is useful for generating animation *)\n(* frames or saving visualization snapshots. *)\n(* *)\n(* Example usage: *)\n(* SVGSerialize(SVGDoc(myElements, 0, 0, 800, 600, <<>>), *)\n(* \"svg_frame_\", TLCGet(\"level\")) *)\n(* *)\n(* This creates files like: svg_frame_001.svg, *)\n(* svg_frame_002.svg, etc. *)\n(**************************************************************************)\nSVGSerialize(svg, frameNamePrefix, frameNumber) ==\n LET IO == INSTANCE IOUtils IN\n IO!Serialize(\n SVGElemToString(svg),\n \\* Construct the filename using the prefix and a zero-padded frame\n \\* number so that files sort correctly lexicographically.\n frameNamePrefix \\o IO!zeroPadN(frameNumber, 3) \\o \".svg\",\n [format |-> \"TXT\", charset |-> \"UTF-8\", \n openOptions |-> <<\"WRITE\", \"CREATE\", \"TRUNCATE_EXISTING\">>])\n\n=============================================================================\n", "SequencesExt": "---------------------------- MODULE SequencesExt ----------------------------\nEXTENDS Sequences, Naturals, FiniteSets, FiniteSetsExt, Folds, Functions, Bags, TLC\n\\* TLAPM does not play well with LOCAL INSTANCE, reinstate the following\n\\* when that issue is fixed.\n\\* LOCAL INSTANCE Sequences\n\\* LOCAL INSTANCE Naturals\n\\* LOCAL INSTANCE FiniteSets\n\\* LOCAL INSTANCE FiniteSetsExt\n\\* LOCAL INSTANCE Folds\n\\* LOCAL INSTANCE Functions\n\\* LOCAL INSTANCE Bags\n\\* LOCAL INSTANCE TLC\n (*************************************************************************)\n (* Imports the definitions from the modules, but doesn't export them. *)\n (*************************************************************************)\n\n-----------------------------------------------------------------------------\n\n(*************************************************************************) \n(* Convert a sequence to the set of its elements (sequence image). *)\n(* *)\n(* Returns the set of all elements that appear in the sequence, *)\n(* effectively removing duplicates and order. The cardinality of the *)\n(* result is at most the length of the sequence. *)\n(* *)\n(* Examples: *)\n(* ToSet(<<>>) = {} *)\n(* ToSet(<<1>>) = {1} *)\n(* ToSet(<<1,2,3>>) = {1,2,3} *)\n(* ToSet(<<1,1,2>>) = {1,2} *)\n(* *)\n(* See: https://en.wikipedia.org/wiki/Image_(mathematics) *)\n(*************************************************************************)\nToSet(s) ==\n { s[i] : i \\in DOMAIN s }\n\n(**************************************************************************)\n(* Convert a sequence to the bag (i.e. multi-set) of its elements. *)\n(* Cf. standard module Bags.tla and community module BagsExt.tla. *)\n(**************************************************************************)\nToBag(s) ==\n [x \\in Range(s) |-> Cardinality({i \\in DOMAIN s : s[i] = x})]\n\n(**************************************************************************)\n(* Convert a set to some sequence that contains all the elements of the *)\n(* set exactly once, and contains no other elements. *)\n(**************************************************************************)\nSetToSeq(S) == \n CHOOSE f \\in [1..Cardinality(S) -> S] : IsInjective(f)\n\n(**************************************************************************)\n(* Convert the set S to a set containing all sequences containing the *)\n(* elements of S exactly once and no other elements. *)\n(* Example: *)\n(* SetToSeqs({}), {<<>>} *)\n(* SetToSeqs({\"t\",\"l\"}) = {<<\"t\",\"l\">>, <<\"l\",\"t\">>} *) \n(**************************************************************************)\nSetToSeqs(S) == \n LET D == 1..Cardinality(S)\n IN { f \\in [D -> S] : \\A i,j \\in D : i # j => f[i] # f[j] }\n\n(**************************************************************************)\n(* Convert a set to a sorted sequence that contains all the elements of *)\n(* the set exactly once, and contains no other elements. *)\n(**************************************************************************)\nSetToSortSeq(S, op(_,_)) ==\n SortSeq(SetToSeq(S), op)\n\n(**************************************************************************)\n(* Convert the set S to a set containing all k-permutations of elements *)\n(* of S for k \\in 0..Cardinality(S). *)\n(* Example: *)\n(* SetToAllKPermutations({}) = {<<>>} *)\n(* SetToAllKPermutations({\"a\"}) = {<<>>, <<\"a\">>} *)\n(* SetToAllKPermutations({\"a\",\"b\"}) = *)\n(* {<<>>, <<\"a\">>, <<\"b\">>,<<\"a\",\"b\">>, <<\"b\",\"a\">>} *)\n(**************************************************************************)\nSetToAllKPermutations(S) ==\n UNION { SetToSeqs(s) : s \\in SUBSET S }\n\n(***************************************************************************)\n(* TupleOf(s, 3) = s \\X s \\X s *)\n(***************************************************************************)\nTupleOf(set, n) == \n [1..n -> set]\n\n(***************************************************************************)\n(* All sequences up to length n with all elements in set. Includes empty *)\n(* sequence. *)\n(***************************************************************************)\nSeqOf(set, n) == \n UNION {[1..m -> set] : m \\in 0..n}\n\n(***************************************************************************)\n(* An alias for SeqOf to make the connection to Sequences!Seq, which is *)\n(* the unbounded version of BoundedSeq. *)\n(***************************************************************************)\nBoundedSeq(S, n) ==\n SeqOf(S, n)\n\n-----------------------------------------------------------------------------\n\n(**************************************************************************)\n(* TRUE iff the element e \\in ToSet(s). *)\n(**************************************************************************)\nContains(s, e) ==\n \\E i \\in 1..Len(s) : s[i] = e\n\n(**************************************************************************)\n(* TRUE iff the sequence s is sorted with respect to the binary *)\n(* relation op, i.e. op(s[i], s[j]) holds for every pair of positions *)\n(* i, j in the domain of s with i < j. *)\n(* *)\n(* No assumptions are made about op; its meaning is fixed by the *)\n(* caller. When op is a strict order (irreflexive and transitive), *)\n(* s is strictly increasing and therefore duplicate-free; when op is *)\n(* a reflexive relation such as <=, equal elements may be adjacent. *)\n(* Instantiating op with the converse relation sorts s in the *)\n(* opposite direction. The empty sequence and every singleton are *)\n(* vacuously sorted under any op. *)\n(* *)\n(* Examples: *)\n(* IsSorted(<<>>, <) = TRUE *)\n(* IsSorted(<<5>>, <) = TRUE *)\n(* IsSorted(<<1, 2, 3>>, <) = TRUE *)\n(* IsSorted(<<1, 2, 2>>, <) = FALSE (not strictly increasing) *)\n(* IsSorted(<<1, 2, 2>>, <=) = TRUE *)\n(* IsSorted(<<3, 2, 1>>, <) = FALSE *)\n(* IsSorted(<<3, 2, 1>>, >) = TRUE *)\n(**************************************************************************)\nIsSorted(s, op(_, _)) ==\n \\A i, j \\in 1..Len(s) : i < j => op(s[i], s[j])\n\n(**************************************************************************)\n(* Reverse the given sequence s: Let l be Len(s) (length of s). *)\n(* Equals a sequence s.t. << S[l], S[l-1], ..., S[1]>> *)\n(**************************************************************************)\nReverse(s) ==\n [ i \\in 1..Len(s) |-> s[(Len(s) - i) + 1] ]\n\n(************************************************************************)\n(* The sequence s with e removed or s iff e \\notin Range(s) *)\n(************************************************************************)\nRemove(s, e) ==\n SelectSeq(s, LAMBDA t: t # e)\n\n(*************************************************************************)\n(* Equals the sequence s except that all occurrences of element old are *)\n(* replaced with the element new. *)\n(*************************************************************************)\nReplaceAll(s, old, new) ==\n [i \\in 1 .. Len(s) |-> IF s[i] = old THEN new ELSE s[i]]\n\n-----------------------------------------------------------------------------\n\n(*************************************************************************)\n(* Selects the index of the first element such that Test(seq[i]) is true *)\n(* Equals 0 if Test(seq[i]) is FALSE for all elements. *)\n(*************************************************************************)\nSelectInSeq(seq, Test(_)) ==\n LET I == { i \\in 1..Len(seq) : Test(seq[i]) }\n IN IF I # {} THEN Min(I) ELSE 0\n\n(*************************************************************************)\n(* Selects the index of the first element in seq such that Test(seq[i]) *)\n(* is TRUE for this elements in from..to. Equals 0 if Test(seq[i]) is *)\n(* FALSE for all elements. *)\n(*************************************************************************)\nSelectInSubSeq(seq, from, to, Test(_)) ==\n LET I == { i \\in from..to : Test(seq[i]) }\n IN IF I # {} THEN Min(I) ELSE 0\n\n(*************************************************************************)\n(* Selects the index of the last element such that Test(seq[i]) is true *)\n(* Equals 0 if Test(seq[i]) is FALSE for all elements. *)\n(*************************************************************************)\nSelectLastInSeq(seq, Test(_)) ==\n LET I == { i \\in 1..Len(seq) : Test(seq[i]) }\n IN IF I # {} THEN Max(I) ELSE 0\n\n(*************************************************************************)\n(* Selects the index of the last element in seq such that Test(seq[i]) *)\n(* is TRUE for this elements in from..to. Equals 0 if Test(seq[i]) is *)\n(* FALSE for all elements. *)\n(*************************************************************************)\nSelectLastInSubSeq(seq, from, to, Test(_)) ==\n LET I == { i \\in from..to : Test(seq[i]) }\n IN IF I # {} THEN Max(I) ELSE 0\n\n-----------------------------------------------------------------------------\n\n\\* The operators below up to including IsStrictSuffix have been extracted \n\\* from the TLAPS module SequencesTheorems.tla as of 10/14/2019. The original\n\\* comments have been partially rewritten.\n\n(**************************************************************************)\n(* Inserts element e at the position i moving the original element to i+1 *)\n(* and so on. In other words, a sequence t s.t.: *)\n(* /\\ Len(t) = Len(s) + 1 *)\n(* /\\ t[i] = e *)\n(* /\\ \\A j \\in 1..(i - 1): t[j] = s[j] *)\n(* /\\ \\A k \\in (i + 1)..Len(s): t[k + 1] = s[k] *)\n(**************************************************************************)\nInsertAt(s, i, e) ==\n SubSeq(s, 1, i-1) \\o <<e>> \\o SubSeq(s, i, Len(s))\n\n(**************************************************************************)\n(* Replaces the element at position i with the element e. *)\n(**************************************************************************)\nReplaceAt(s, i, e) ==\n [s EXCEPT ![i] = e]\n \n(**************************************************************************)\n(* Replaces the element at position i shortening the length of s by one. *)\n(**************************************************************************)\nRemoveAt(s, i) == \n SubSeq(s, 1, i-1) \\o SubSeq(s, i+1, Len(s))\n\n(************************************************************************)\n(* The sequence s with the first occurrence of e removed or s *)\n(* iff e \\notin Range(s) *)\n(************************************************************************)\nRemoveFirst(s, e) ==\n IF \\E i \\in 1..Len(s): s[i] = e\n THEN RemoveAt(s, SelectInSeq(s, LAMBDA v: v = e))\n ELSE s\n\n(************************************************************************)\n(* The sequence s with the first element removed s.t. Test(e) or s *)\n(* iff e \\notin Range(s) *)\n(************************************************************************)\nRemoveFirstMatch(s, Test(_)) ==\n IF \\E i \\in 1..Len(s): Test(s[i])\n THEN RemoveAt(s, SelectInSeq(s, Test))\n ELSE s\n\n-----------------------------------------------------------------------------\n\n(************************************************************************)\n(* Cons prepends an element at the beginning of a sequence. *)\n(************************************************************************)\nCons(elt, seq) == \n <<elt>> \\o seq\n\n(************************************************************************)\n(* Reverses the operands of Sequences!Append for better compatibility *)\n(* with FoldFunction below. *)\n(* Example: *)\n(* FoldSeq(LAMBDA x,y: {x} \\cup y, {}, <<1,2,1>>) = Range(<<1,2,1>>) *)\n(************************************************************************)\nSnoc(elt, seq) == \n Append(seq, elt)\n\n(**************************************************************************)\n(* The sequence formed by removing its last element. *)\n(**************************************************************************)\nFront(s) == \n SubSeq(s, 1, Len(s)-1)\n\n(**************************************************************************)\n(* The last element of the sequence. *)\n(**************************************************************************)\nLast(s) ==\n s[Len(s)]\n\n-----------------------------------------------------------------------------\n\n(**************************************************************************)\n(* TRUE iff the sequence s is a prefix of the sequence t, s.t. *)\n(* \\E u \\in Seq(Range(t)) : t = s \\o u. In other words, there exists *)\n(* a suffix u that with s prepended equals t. *)\n(**************************************************************************)\nIsPrefix(s, t) ==\n Len(s) <= Len(t) /\\ SubSeq(s, 1, Len(s)) = SubSeq(t, 1, Len(s))\n\n(**************************************************************************)\n(* TRUE iff the sequence s is a prefix of the sequence t and s # t *)\n(**************************************************************************)\nIsStrictPrefix(s,t) ==\n IsPrefix(s, t) /\\ s # t\n\n(**************************************************************************)\n(* TRUE iff the sequence s is a suffix of the sequence t, s.t. *)\n(* \\E u \\in Seq(Range(t)) : t = u \\o s. In other words, there exists a *)\n(* prefix that with s appended equals t. *)\n(**************************************************************************)\nIsSuffix(s, t) ==\n IsPrefix(Reverse(s), Reverse(t))\n\n(**************************************************************************)\n(* TRUE iff the sequence s is a suffix of the sequence t and s # t *)\n(**************************************************************************)\nIsStrictSuffix(s, t) ==\n IsSuffix(s,t) /\\ s # t\n \n-----------------------------------------------------------------------------\n\n(**************************************************************************)\n(* The set of prefixes of the sequence s, including the empty sequence. *)\n(**************************************************************************)\nPrefixes(s) ==\n \\* 0.. for <<>>\n { SubSeq(s, 1, l) : l \\in 0..Len(s) }\n\n(**************************************************************************)\n(* The set of all sequences that are prefixes of the set of sequences S. *)\n(**************************************************************************)\nCommonPrefixes(S) ==\n LET P == UNION { Prefixes(seq) : seq \\in S }\n IN { prefix \\in P : \\A t \\in S: IsPrefix(prefix, t) }\n\n(**************************************************************************)\n(* The longest common prefix of the sequences in the set S. *)\n(**************************************************************************)\nLongestCommonPrefix(S) ==\n CHOOSE longest \\in CommonPrefixes(S): \\* there can only be one LCP => CHOOSE\n \\A other \\in CommonPrefixes(S):\n Len(other) <= Len(longest)\n\n(**************************************************************************)\n(* The set of suffixes of the sequence s, including the empty sequence. *)\n(**************************************************************************)\nSuffixes(s) ==\n { SubSeq(s, l, Len(s)) : l \\in 1..Len(s) } \\cup {<<>>}\n\n-----------------------------------------------------------------------------\n\n(***************************************************************************)\n(* Range(a % b) = 0..b-1, but DOMAIN seq = 1..Len(seq). *)\n(* So to do modular arithmetic on sequences we need to *)\n(* map 0 to b. *)\n(***************************************************************************)\nSeqMod(a, b) == \n IF a % b = 0 THEN b ELSE a % b\n\n\n(***************************************************************************)\n(* An alias of FoldFunction that combines all elements of seq using op, *)\n(* in an unspecified order. The result is *)\n(* op(seq[i],op(seq[j], ..., op(seq[k],base) ...)) *)\n(* *)\n(* op should be associative and commutative, because we can not assume a *)\n(* particular ordering of i, j, and k *)\n(* *)\n(* Example: *)\n(* FoldSeq(LAMBDA x,y: {x} \\cup y, {}, <<1,2,1>>) = Range(<<1,2,1>>) *)\n(***************************************************************************)\nFoldSeq(op(_, _), base, seq) == \n FoldFunction(op, base, seq)\n\n(***************************************************************************)\n(* FoldLeft folds op on all elements of seq from left to right, starting *)\n(* with the first element and base. The resulting function is: *)\n(* op(op(...op(base,f[0]), ...f[n-1]), f[n]) *)\n(* *)\n(* *)\n(* Example: *)\n(* LET cons(x,y) == <<x,y>> *)\n(* IN FoldLeft(cons, 0, <<3,1,2>>) = << << <<0,3>>, 1>>, 2>> *)\n(***************************************************************************)\nFoldLeft(op(_, _), base, seq) == \n MapThenFoldSet(LAMBDA x,y : op(y,x), base,\n LAMBDA i : seq[i],\n LAMBDA S : Max(S),\n DOMAIN seq)\n\n(***************************************************************************)\n(* FoldRight folds op on all elements of seq from right to left, starting *)\n(* with the last element and base. The resulting function is: *)\n(* op(f[0],op(f[1], ..., op(f[n],base) ...)) *)\n(* *)\n(* *)\n(* Example: *)\n(* LET cons(x,y) == <<x,y>> *)\n(* IN FoldRight(cons, <<3,1,2>>, 0 ) = << 3, << 1, <<2,0>> >> >> *)\n(***************************************************************************)\nFoldRight(op(_, _), seq, base) == \n MapThenFoldSet(op, base,\n LAMBDA i : seq[i],\n LAMBDA S : Min(S),\n DOMAIN seq)\n\n(***************************************************************************)\n(* FoldLeftDomain folds op on the domain of seq, i.e., the seq's indices, *)\n(* starting at the lowest index. *) \n(***************************************************************************)\nFoldLeftDomain(op(_, _), base, seq) == \n FoldLeft(op, base, [i \\in DOMAIN seq |-> i])\n\n(***************************************************************************)\n(* FoldRightDomain folds op on the domain of seq, i.e., the seq's indices, *)\n(* starting at the highest index. *)\n(***************************************************************************)\nFoldRightDomain(op(_, _), seq, base) == \n FoldRight(op, [i \\in DOMAIN seq |-> i], base)\n\n-----------------------------------------------------------------------------\n\n(**************************************************************************)\n(* A sequence of all elements from all sequences in the sequence seqs . *)\n(* *)\n(* Examples: *)\n(* *)\n(* FlattenSeq(<< <<1,2>>, <<1>> >>) = << 1, 2, 1 >> *)\n(* FlattenSeq(<< <<\"a\">>, <<\"b\">> >>) = <<\"a\", \"b\">> *)\n(* FlattenSeq(<< \"a\", \"b\" >>) = \"ab\" *)\n(**************************************************************************)\nFlattenSeq(seqs) ==\n IF Len(seqs) = 0 THEN seqs ELSE\n \\* Not via FoldSeq(\\o, <<>>, seqs) here to support strings with TLC.\n LET flatten[i \\in 1..Len(seqs)] ==\n IF i = 1 THEN seqs[i] ELSE flatten[i-1] \\o seqs[i]\n IN flatten[Len(seqs)]\n\n(**************************************************************************)\n(* A sequence of pairs where the i-th pair is formed from the i-th *)\n(* element of s and the i-th element of t. The length of the result *)\n(* sequence is the minimum of the lengths of s and t. *)\n(* *)\n(* Examples: *)\n(* *)\n(* Zip(<< >>, << >>) = << >> *)\n(* Zip(<<\"a\">>, <<\"b\">>) = << <<\"a\", \"b\">> >> *)\n(* Zip(<<1, 3>>, <<2, 4>>) = <<<<1, 2>>, <<3, 4>>>> *)\n(* FlattenSeq(Zip(<<1, 3>>, <<2, 4>>)) = <<1, 2, 3, 4>>>> *)\n(* Zip(<< >>, <<1, 2, 3>>) = << >> *)\n(* Zip(<<\"a\", \"b\", \"c\">>, <<1>>) = << <<\"a\", 1>> >> *)\n(**************************************************************************)\nZip(s, t) ==\n LET l == IF Len(s) <= Len(t) THEN Len(s) ELSE Len(t)\n IN [ i \\in 1 .. l |-> <<s[i], t[i] >> ]\n\n(**************************************************************************)\n(* A sequence where the i-th tuple contains the i-th element of s and *)\n(* t in this order. Not defined for Len(s) # Len(t) *)\n(* *)\n(* Examples: *)\n(* *)\n(* Interleave(<< >>, << >>) = << >> *)\n(* Interleave(<<\"a\">>, <<\"b\">>) = << <<\"a\">>, <<\"b\">> >> *)\n(* Interleave(<<1,3>>, <<2,4>>) = <<<<1>>, <<2>>, <<3>>, <<4>>>> *)\n(* FlattenSeq(Interleave(<<1,3>>,<<2,4>>)) = <<1, 2, 3, 4>> *)\n(**************************************************************************)\nInterleave(s, t) ==\n CASE Len(s) = Len(t) /\\ Len(s) > 0 ->\n LET u[ i \\in 1..Len(s) ] == \n IF i = 1 THEN << <<s[i]>> >> \\o << <<t[i]>> >>\n ELSE u[i-1] \\o << <<s[i]>> >> \\o << <<t[i]>> >>\n IN Last(u)\n \\* error \"Interleave: sequences must have same length\"\n \\* <<>> is the identity of \\o, which is what the arm above builds the\n \\* result with, so Len(Interleave(s, t)) = 2 * Len(s) holds for the empty\n \\* sequence as well. It also matches the other operators of this module\n \\* on an empty argument, such as Zip, FlattenSeq, and Reverse.\n [] Len(s) = Len(t) /\\ Len(s) = 0 -> << >>\n\n(**************************************************************************)\n(* The set of all subsequences of the sequence s . Note that the empty *)\n(* sequence <<>> is defined to be a subsequence of any sequence. *)\n(**************************************************************************)\nSubSeqs(s) ==\n { SubSeq(s, i+1, j) : i, j \\in 0..Len(s) }\n\n(**************************************************************************)\n(* SubSeqs(s) *)\n(* \\cup *)\n(* { subsequences of s, with arbitrarily many elts removed } *)\n(* *)\n(* Example: *)\n(* AllSubSeqs(<<1,2,3,4>>) = *)\n(* {<<>>, <<1>>, <<2>>, <<3>>, <<4>>, *)\n(* <<1, 2>>, <<1, 3>>, <<1, 4>>, *)\n(* <<2, 3>>, <<2, 4>>, <<3, 4>>, *)\n(* <<1, 2, 3>>, <<1, 2, 4>>, <<1, 3, 4>>, <<2, 3, 4>>, *)\n(* <<1, 2, 3, 4>>} *)\n(**************************************************************************)\nAllSubSeqs(s) ==\n { FoldFunction(Snoc, <<>>, [ i \\in D |-> s[i] ]) : D \\in SUBSET DOMAIN s }\n\n(**************************************************************************)\n(* The (1-based) index of the beginning of the subsequence s of the *)\n(* sequence t . If s appears in t multiple times, this equals the *)\n(* lowest index. *)\n(* For example: IndexFirstSubSeq(<<1>>, <<1,1,1>>) = 1 *)\n(**************************************************************************)\nIndexFirstSubSeq(s, t) ==\n LET last == CHOOSE i \\in 0..Len(t) :\n /\\ s \\in SubSeqs(SubSeq(t, 1, i))\n /\\ \\A j \\in 0..i-1 : s \\notin SubSeqs(SubSeq(t, 1, j))\n IN last - (Len(s) - 1)\n\n(**************************************************************************)\n(* The sequence t with its subsequence s at position i replaced by *)\n(* the sequence r . *)\n(**************************************************************************)\nReplaceSubSeqAt(i, r, s, t) ==\n LET prefix == SubSeq(t, 1, i - 1)\n suffix == SubSeq(t, i + Len(s), Len(t))\n IN prefix \\o r \\o suffix \n\n(**************************************************************************)\n(* The sequence t with its subsequence s replaced by the sequence r *)\n(**************************************************************************)\nReplaceFirstSubSeq(r, s, t) ==\n IF s \\in SubSeqs(t)\n THEN ReplaceSubSeqAt(IndexFirstSubSeq(s, t), r, s, t)\n ELSE t\n\n(**************************************************************************)\n(* The sequence t with all subsequences s replaced by the sequence r *)\n(* Overlapping replacements are disambiguated by choosing the occurrence *)\n(* closer to the beginning of the sequence. *)\n(* *)\n(* Examples: *)\n(* *)\n(* ReplaceAllSubSeqs(<<>>,<<>>,<<>>) = <<>> *)\n(* ReplaceAllSubSeqs(<<4>>,<<>>,<<>>) = <<4>> *)\n(* ReplaceAllSubSeqs(<<2>>,<<3>>,<<1,3>>) = <<1,2>> *)\n(* ReplaceAllSubSeqs(<<2,2>>,<<1,1>>,<<1,1,1>>) = <<2,2,1>> *)\n(**************************************************************************)\nReplaceAllSubSeqs(r, s, t) ==\n CASE s = t -> r\n [] r = s -> t \\* TLC optimization\n [] s # t /\\ Len(s) = 0 ->\n LET z == Interleave([i \\in 1..Len(t) |-> r], [i \\in 1..Len(t) |-> <<t[i]>>])\n IN FlattenSeq(FlattenSeq(z))\n [] s # t /\\ Len(s) > 0 /\\ s \\in SubSeqs(t) ->\n \\* Not defined recursively to avoid infinite loops.\n LET match(f) == { i \\in 1..Len(f) : s = f[i] }\n comp(p, q) == \\A i \\in 1..Len(p) : p[i] <= q[i]\n \\* TODO: Replace with Seq(Seq(Range(t))) once *total* Java module\n \\* override in place. The current override handles only the\n \\* case where the parameters are strings (hence Range(\"abc\") \n \\* not a problem with TLC).\n R == BoundedSeq(BoundedSeq(Range(t), Len(t)), Len(t))\n \\* A) Matches the input t.\n S == { f \\in R : FlattenSeq(f) = t }\n \\* B) Has the max number of matches...\n T == { f \\in S : \\A g \\in S : \n Cardinality(match(g)) <= Cardinality(match(f)) }\n \\* C) ...of min (leftmost) matches.\n u == CHOOSE f \\in T : \n \\A g \\in T : comp(\n SetToSortSeq(match(f), <), SetToSortSeq(match(g), <))\n IN FlattenSeq([i \\in 1..Len(u) |-> IF s = u[i] THEN r ELSE u[i]])\n [] OTHER -> t\n\n=============================================================================\n", "SequencesExtTheorems": "----------------------- MODULE SequencesExtTheorems ------------------------\n(**************************************************************************)\n(* This module contains theorems about the operators on sequences defined *)\n(* in module SequencesExt. *)\n(* The proofs are given in module SequencesExtTheorems_proofs. *)\n(**************************************************************************)\nEXTENDS Sequences, SequencesExt, Functions, Integers, WellFoundedInduction\n\n(***************************************************************************)\n(* SeqOf(S,n) is the set of sequences over S whose length is at most n. *)\n(***************************************************************************)\nTHEOREM SeqOfSeq ==\n ASSUME NEW S, NEW n \\in Int \n PROVE SeqOf(S,n) = {s \\in Seq(S) : Len(s) <= n}\n\n(***************************************************************************)\n(* Theorems about Cons. *)\n(* Cons(elt, seq) == <<elt>> \\o seq *)\n(***************************************************************************)\n\nTHEOREM ConsProperties ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW elt \\in S\n PROVE /\\ Cons(elt, seq) \\in Seq(S)\n /\\ Cons(elt, seq) # <<>>\n /\\ Len(Cons(elt, seq)) = Len(seq)+1\n /\\ Head(Cons(elt, seq)) = elt\n /\\ Tail(Cons(elt, seq)) = seq\n /\\ Cons(elt, seq)[1] = elt\n /\\ \\A i \\in 1 .. Len(seq) : Cons(elt, seq)[i+1] = seq[i]\n /\\ \\A i \\in 2 .. Len(seq)+1 : Cons(elt, seq)[i] = seq[i-1]\n\nTHEOREM ConsEmpty ==\n \\A x : Cons(x, << >>) = << x >>\n\nTHEOREM ConsHeadTail ==\n ASSUME NEW S, NEW seq \\in Seq(S), seq # << >>\n PROVE Cons(Head(seq), Tail(seq)) = seq\n\nTHEOREM ConsAppend ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW x \\in S, NEW y \\in S\n PROVE Cons(x, Append(seq, y)) = Append(Cons(x,seq), y)\n\nTHEOREM ConsConcat ==\n ASSUME NEW S, NEW e \\in S, NEW ls \\in Seq(S), NEW rs \\in Seq(S)\n PROVE Cons(e, ls) \\o rs = Cons(e, ls \\o rs)\n\nTHEOREM ConsInjective ==\n ASSUME NEW S, NEW e \\in S, NEW s \\in Seq(S), NEW f \\in S, NEW t \\in Seq(S)\n PROVE Cons(e,s) = Cons(f,t) <=> e = f /\\ s = t\n\nTHEOREM SequencesInductionCons ==\n ASSUME NEW P(_), NEW S,\n P(<< >>),\n \\A s \\in Seq(S), e \\in S : P(s) => P(Cons(e,s))\n PROVE \\A seq \\in Seq(S) : P(seq)\n\n(***************************************************************************)\n(* Theorems about Contains. *)\n(* Contains(s, e) == \\E i \\in 1 .. Len(s) : s[i] = e *)\n(***************************************************************************)\n\n(* Membership in a sequence coincides with membership in its image set. *)\nTHEOREM ContainsRange ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW e\n PROVE Contains(s, e) <=> e \\in Range(s)\n\nTHEOREM ContainsEmpty ==\n ASSUME NEW e\n PROVE ~ Contains(<< >>, e)\n\nTHEOREM ContainsAppend ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW x, NEW e\n PROVE Contains(Append(s, x), e) <=> (Contains(s, e) \\/ e = x)\n\nTHEOREM ContainsSingleton ==\n ASSUME NEW S, NEW x\n PROVE \\A e : Contains(<< x >>, e) <=> e = x\n\nTHEOREM ContainsConcat ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW T, NEW t \\in Seq(T), NEW e\n PROVE Contains(s \\o t, e) <=> (Contains(s, e) \\/ Contains(t, e))\n\nTHEOREM ContainsCons ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW x, NEW e\n PROVE Contains(Cons(x, s), e) <=> (e = x \\/ Contains(s, e))\n\nTHEOREM ContainsTail ==\n ASSUME NEW S, NEW s \\in Seq(S), s # << >>, NEW e\n PROVE Contains(Tail(s), e) => Contains(s, e)\n\n(* An element other than the head of a non-empty sequence that occurs in *)\n(* the sequence also occurs in its tail. *)\nTHEOREM ContainsTailExceptHead ==\n ASSUME NEW S, NEW s \\in Seq(S), s # << >>, NEW e,\n Contains(s, e), e # Head(s)\n PROVE Contains(Tail(s), e)\n\n(***************************************************************************)\n(* Theorems about IsSorted (see SequencesExt). *)\n(* *)\n(* The relevant order properties of op (transitivity, irreflexivity) *)\n(* are stated locally as hypotheses of each theorem, so that the *)\n(* theorems apply to an arbitrary binary relation op rather than only *)\n(* to relations globally known to be orders. *)\n(***************************************************************************)\n\nTHEOREM SortedEmpty ==\n ASSUME NEW op(_,_)\n PROVE IsSorted(<< >>, op)\n\nTHEOREM SortedSingleton ==\n ASSUME NEW S, NEW x \\in S, NEW op(_,_)\n PROVE IsSorted(<< x >>, op)\n\n(* Appending an element that dominates the last one keeps the sequence *)\n(* sorted, provided the ordering relation is transitive. *)\nTHEOREM SortedAppend ==\n ASSUME NEW S, NEW op(_,_),\n \\A x,y,z \\in S : op(x,y) /\\ op(y,z) => op(x,z),\n NEW s \\in Seq(S), IsSorted(s, op),\n NEW e \\in S, s # << >> => op(s[Len(s)], e)\n PROVE IsSorted(Append(s, e), op)\n\n(* Concatenating two sorted sequences whose boundary elements are ordered *)\n(* yields a sorted sequence (for a transitive ordering relation). *)\nTHEOREM SortedConcat ==\n ASSUME NEW S, NEW op(_,_),\n \\A x,y,z \\in S : op(x,y) /\\ op(y,z) => op(x,z),\n NEW s \\in Seq(S), NEW t \\in Seq(S),\n IsSorted(s, op), IsSorted(t, op),\n (Len(s) > 0 /\\ Len(t) > 0) => op(s[Len(s)], t[1])\n PROVE IsSorted(s \\o t, op)\n\n(* A sequence sorted by an irreflexive relation has no repeated elements. *)\nTHEOREM SortedInjective ==\n ASSUME NEW S, NEW op(_,_),\n \\A x \\in S : ~ op(x, x),\n NEW s \\in Seq(S), IsSorted(s, op)\n PROVE IsInjective(s)\n\n(* Any contiguous subsequence of a sorted sequence is sorted. *)\nTHEOREM SortedSubSeq ==\n ASSUME NEW S, NEW op(_,_),\n NEW s \\in Seq(S), IsSorted(s, op),\n NEW m \\in 1..Len(s)+1, NEW n \\in 0..Len(s)\n PROVE IsSorted(SubSeq(s, m, n), op)\n\n(***************************************************************************)\n(* Theorems about InsertAt and RemoveAt. *)\n(* InsertAt(seq,i,elt) == *)\n(* SubSeq(seq, 1, i-1) \\o <<elt>> \\o SubSeq(seq, i, Len(seq)) *)\n(* RemoveAt(seq, i) == SubSeq(seq, 1, i-1) \\o SubSeq(seq, i+1, Len(seq)) *)\n(***************************************************************************)\n\nTHEOREM InsertAtProperties ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW i \\in 1 .. Len(seq)+1, NEW elt \\in S\n PROVE /\\ InsertAt(seq,i,elt) \\in Seq(S)\n /\\ Len(InsertAt(seq,i,elt)) = Len(seq)+1\n /\\ \\A j \\in 1 .. Len(seq)+1 : InsertAt(seq,i,elt)[j] =\n IF j<i THEN seq[j]\n ELSE IF j=i THEN elt\n ELSE seq[j-1]\n\nTHEOREM RemoveAtProperties ==\n ASSUME NEW S, NEW seq \\in Seq(S),\n NEW i \\in 1..Len(seq)\n PROVE /\\ RemoveAt(seq,i) \\in Seq(S)\n /\\ Len(RemoveAt(seq,i)) = Len(seq) - 1\n /\\ \\A j \\in 1 .. Len(seq)-1 : RemoveAt(seq,i)[j] = IF j<i THEN seq[j] ELSE seq[j+1]\n\n(***************************************************************************)\n(* Theorems about Front and Last. *)\n(* Front(seq) == SubSeq(seq, 1, Len(seq)-1) *)\n(* Last(seq) == seq[Len(seq)] *)\n(***************************************************************************)\n\nTHEOREM FrontProperties ==\n ASSUME NEW S, NEW seq \\in Seq(S)\n PROVE /\\ Front(seq) \\in Seq(S)\n /\\ Len(Front(seq)) = IF seq = << >> THEN 0 ELSE Len(seq)-1\n /\\ \\A i \\in 1 .. Len(seq)-1 : Front(seq)[i] = seq[i]\n\nTHEOREM FrontOfEmpty == Front(<< >>) = << >>\n\nTHEOREM LastProperties ==\n ASSUME NEW S, NEW seq \\in Seq(S), seq # << >>\n PROVE /\\ Last(seq) \\in S\n /\\ Append(Front(seq), Last(seq)) = seq\n\nTHEOREM FrontLastOfSubSeq ==\n ASSUME NEW S, NEW seq \\in Seq(S),\n NEW m \\in 1 .. Len(seq), NEW n \\in m .. Len(seq)\n PROVE /\\ Front(SubSeq(seq,m,n)) = SubSeq(seq, m, n-1)\n /\\ Last(SubSeq(seq,m,n)) = seq[n]\n\nTHEOREM FrontLastAppend ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW e \\in S\n PROVE /\\ Front(Append(seq, e)) = seq\n /\\ Last(Append(seq, e)) = e\n\nLEMMA FrontInjectiveSeq ==\n ASSUME NEW S, NEW seq \\in Seq(S), IsInjective(seq), seq # << >>\n PROVE /\\ IsInjective(Front(seq))\n /\\ Range(Front(seq)) = Range(seq) \\ {Last(seq)}\n\nTHEOREM SequencesInductionFront ==\n ASSUME NEW S, NEW P(_),\n P(<< >>),\n \\A s \\in Seq(S) : (s # << >>) /\\ P(Front(s)) => P(s)\n PROVE \\A s \\in Seq(S) : P(s)\n\n(***************************************************************************)\n(* Theorems about Reverse. *)\n(* Reverse(seq) == [j \\in 1 .. Len(seq) |-> seq[Len(seq)-j+1] ] *)\n(***************************************************************************)\n\nTHEOREM ReverseProperties ==\n ASSUME NEW S, NEW seq \\in Seq(S)\n PROVE /\\ Reverse(seq) \\in Seq(S)\n /\\ Len(Reverse(seq)) = Len(seq)\n /\\ Reverse(Reverse(seq)) = seq\n\nTHEOREM ReverseEmpty == Reverse(<< >>) = << >>\n\nTHEOREM ReverseEqual ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S), Reverse(s) = Reverse(t)\n PROVE s = t\n\nTHEOREM ReverseEmptyIffEmpty ==\n ASSUME NEW S, NEW seq \\in Seq(S), Reverse(seq) = <<>>\n PROVE seq = <<>>\n\nTHEOREM ReverseConcat ==\n ASSUME NEW S, NEW s1 \\in Seq(S), NEW s2 \\in Seq(S)\n PROVE Reverse(s1 \\o s2) = Reverse(s2) \\o Reverse(s1)\n\nTHEOREM ReverseAppend ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW e \\in S\n PROVE Reverse(Append(seq,e)) = Cons(e, Reverse(seq))\n\nTHEOREM ReverseCons ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW e \\in S\n PROVE Reverse(Cons(e,seq)) = Append(Reverse(seq), e)\n\nTHEOREM ReverseSingleton == \\A x : Reverse(<< x >>) = << x >>\n\nTHEOREM ReverseSubSeq ==\n ASSUME NEW S, NEW seq \\in Seq(S),\n NEW m \\in 1..Len(seq), NEW n \\in 0..Len(seq)\n PROVE Reverse(SubSeq(seq, m , n)) = SubSeq(Reverse(seq), Len(seq)-n+1, Len(seq)-m+1)\n\nTHEOREM ReversePalindrome ==\n ASSUME NEW S, NEW seq \\in Seq(S),\n Reverse(seq) = seq\n PROVE Reverse(seq \\o seq) = seq \\o seq\n\nTHEOREM LastEqualsHeadReverse ==\n ASSUME NEW S, NEW seq \\in Seq(S), seq # << >>\n PROVE Last(seq) = Head(Reverse(seq))\n\nTHEOREM ReverseFrontEqualsTailReverse ==\n ASSUME NEW S, NEW seq \\in Seq(S), seq # << >>\n PROVE Reverse(Front(seq)) = Tail(Reverse(seq))\n\n(* The range of the reverse sequence equals that of the original one. *)\nTHEOREM RangeReverse ==\n ASSUME NEW S, NEW seq \\in Seq(S)\n PROVE /\\ DOMAIN Reverse(seq) = DOMAIN seq\n /\\ Range(Reverse(seq)) = Range(seq)\n\n(***************************************************************************)\n(* The following theorem gives three alternative characterizations of *)\n(* prefixes. It also implies that any prefix of a sequence t is at most *)\n(* as long as t. *)\n(***************************************************************************)\nTHEOREM IsPrefixProperties ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S)\n PROVE /\\ IsPrefix(s,t) <=> \\E u \\in Seq(S) : t = s \\o u\n /\\ IsPrefix(s,t) <=> Len(s) <= Len(t) /\\ s = SubSeq(t, 1, Len(s))\n /\\ IsPrefix(s,t) <=> Len(s) <= Len(t) /\\ s = Restrict(t, DOMAIN s)\n\nTHEOREM IsStrictPrefixProperties ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S)\n PROVE /\\ IsStrictPrefix(s,t) <=> \\E u \\in Seq(S) : u # << >> /\\ t = s \\o u\n /\\ IsStrictPrefix(s,t) <=> Len(s) < Len(t) /\\ s = SubSeq(t, 1, Len(s))\n /\\ IsStrictPrefix(s,t) <=> Len(s) < Len(t) /\\ s = Restrict(t, DOMAIN s)\n /\\ IsStrictPrefix(s,t) <=> IsPrefix(s,t) /\\ Len(s) < Len(t)\n\nTHEOREM IsPrefixElts ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S), NEW i \\in 1 .. Len(s),\n IsPrefix(s,t)\n PROVE s[i] = t[i]\n\nTHEOREM EmptyIsPrefix ==\n ASSUME NEW S, NEW s \\in Seq(S)\n PROVE /\\ IsPrefix(<<>>, s)\n /\\ IsPrefix(s, <<>>) <=> s = <<>>\n /\\ IsStrictPrefix(<<>>, s) <=> s # <<>>\n /\\ ~ IsStrictPrefix(s, <<>>)\n\nTHEOREM IsPrefixConcat ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S)\n PROVE IsPrefix(s, s \\o t)\n\nTHEOREM IsStrictPrefixAppend ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW e \\in S\n PROVE IsStrictPrefix(s, Append(s,e))\n\nTHEOREM FrontIsPrefix ==\n ASSUME NEW S, NEW s \\in Seq(S)\n PROVE /\\ IsPrefix(Front(s), s)\n /\\ s # <<>> => IsStrictPrefix(Front(s), s)\n\n(***************************************************************************)\n(* (Strict) prefixes on sequences form a (strict) partial order, and *)\n(* the strict ordering is well-founded. *)\n(***************************************************************************)\nTHEOREM IsPrefixPartialOrder ==\n ASSUME NEW S\n PROVE /\\ \\A s \\in Seq(S) : IsPrefix(s,s)\n /\\ \\A s,t \\in Seq(S) : IsPrefix(s,t) /\\ IsPrefix(t,s) => s = t\n /\\ \\A s,t,u \\in Seq(S) : IsPrefix(s,t) /\\ IsPrefix(t,u) => IsPrefix(s,u)\n\nTHEOREM ConcatIsPrefix ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S), NEW u \\in Seq(S),\n IsPrefix(s \\o t, u)\n PROVE IsPrefix(s, u)\n\nTHEOREM ConcatIsPrefixCancel ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S), NEW u \\in Seq(S)\n PROVE /\\ IsPrefix(s \\o t, s \\o u) <=> IsPrefix(t, u)\n\nTHEOREM ConsIsPrefixCancel ==\n ASSUME NEW S, NEW e \\in S, NEW s \\in Seq(S), NEW t \\in Seq(S)\n PROVE IsPrefix(Cons(e,s), Cons(e,t)) <=> IsPrefix(s,t)\n\nTHEOREM ConsIsPrefix ==\n ASSUME NEW S, NEW e \\in S, NEW s \\in Seq(S), NEW u \\in Seq(S),\n IsPrefix(Cons(e,s), u)\n PROVE /\\ e = Head(u)\n /\\ IsPrefix(s, Tail(u))\n\nTHEOREM IsStrictPrefixStrictPartialOrder ==\n ASSUME NEW S\n PROVE /\\ \\A s \\in Seq(S) : ~ IsStrictPrefix(s,s)\n /\\ \\A s,t \\in Seq(S) : IsStrictPrefix(s,t) => ~ IsStrictPrefix(t,s)\n /\\ \\A s,t,u \\in Seq(S) : IsStrictPrefix(s,t) /\\ IsStrictPrefix(t,u) => IsStrictPrefix(s,u)\n\nTHEOREM IsStrictPrefixWellFounded ==\n ASSUME NEW S\n PROVE IsWellFoundedOn(OpToRel(IsStrictPrefix, Seq(S)), Seq(S))\n\nTHEOREM SeqStrictPrefixInduction ==\n ASSUME NEW P(_), NEW S,\n \\A t \\in Seq(S) : (\\A s \\in Seq(S) : IsStrictPrefix(s,t) => P(s)) => P(t)\n PROVE \\A s \\in Seq(S) : P(s)\n\n(***************************************************************************)\n(* Similar theorems about suffixes. *)\n(***************************************************************************)\n\nTHEOREM IsSuffixProperties ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S)\n PROVE /\\ IsSuffix(s,t) <=> \\E u \\in Seq(S) : t = u \\o s\n /\\ IsSuffix(s,t) <=> Len(s) <= Len(t) /\\ s = SubSeq(t, Len(t)-Len(s)+1, Len(t))\n\nTHEOREM IsStrictSuffixProperties ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S)\n PROVE /\\ IsStrictSuffix(s,t) <=> \\E u \\in Seq(S) : u # << >> /\\ t = u \\o s\n /\\ IsStrictSuffix(s,t) <=> Len(s) < Len(t) /\\ IsSuffix(s,t)\n /\\ IsStrictSuffix(s,t) <=> Len(s) < Len(t) /\\ s = SubSeq(t, Len(t)-Len(s)+1, Len(t))\n /\\ IsStrictSuffix(s,t) <=> IsStrictPrefix(Reverse(s), Reverse(t))\n\nTHEOREM IsSuffixElts ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S), NEW i \\in 1 .. Len(s),\n IsSuffix(s,t)\n PROVE s[i] = t[Len(t) - Len(s) + i]\n\nTHEOREM EmptyIsSuffix ==\n ASSUME NEW S, NEW s \\in Seq(S)\n PROVE /\\ IsSuffix(<<>>, s)\n /\\ IsSuffix(s, <<>>) <=> s = <<>>\n /\\ IsStrictSuffix(<<>>, s) <=> s # <<>>\n /\\ ~ IsStrictSuffix(s, <<>>)\n\nTHEOREM IsSuffixConcat ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S)\n PROVE IsSuffix(s, t \\o s)\n\nTHEOREM IsStrictSuffixCons ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW e \\in S\n PROVE IsStrictSuffix(s, Cons(e,s))\n\nTHEOREM TailIsSuffix ==\n ASSUME NEW S, NEW s \\in Seq(S), s # << >>\n PROVE IsStrictSuffix(Tail(s), s)\n\nTHEOREM IsSuffixPartialOrder ==\n ASSUME NEW S\n PROVE /\\ \\A s \\in Seq(S) : IsSuffix(s,s)\n /\\ \\A s,t \\in Seq(S) : IsSuffix(s,t) /\\ IsSuffix(t,s) => s = t\n /\\ \\A s,t,u \\in Seq(S) : IsSuffix(s,t) /\\ IsSuffix(t,u) => IsSuffix(s,u)\n\nTHEOREM ConcatIsSuffix ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S), NEW u \\in Seq(S),\n IsSuffix(s \\o t, u)\n PROVE IsSuffix(t, u)\n\nTHEOREM ConcatIsSuffixCancel ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S), NEW u \\in Seq(S)\n PROVE IsSuffix(s \\o t, u \\o t) <=> IsSuffix(s, u)\n\nTHEOREM AppendIsSuffixCancel ==\n ASSUME NEW S, NEW e \\in S, NEW s \\in Seq(S), NEW t \\in Seq(S)\n PROVE IsSuffix(Append(s,e), Append(t,e)) <=> IsSuffix(s,t)\n\nTHEOREM AppendIsSuffix ==\n ASSUME NEW S, NEW e \\in S, NEW s \\in Seq(S), NEW u \\in Seq(S),\n IsSuffix(Append(s,e), u)\n PROVE /\\ e = Last(u)\n /\\ IsSuffix(s, Front(u))\n\nTHEOREM IsStrictSuffixStrictPartialOrder ==\n ASSUME NEW S\n PROVE /\\ \\A s \\in Seq(S) : ~ IsStrictSuffix(s,s)\n /\\ \\A s,t \\in Seq(S) : IsStrictSuffix(s,t) => ~ IsStrictSuffix(t,s)\n /\\ \\A s,t,u \\in Seq(S) : IsStrictSuffix(s,t) /\\ IsStrictSuffix(t,u) => IsStrictSuffix(s,u)\n\nTHEOREM IsStrictSuffixWellFounded ==\n ASSUME NEW S\n PROVE IsWellFoundedOn(OpToRel(IsStrictSuffix, Seq(S)), Seq(S))\n\nTHEOREM SeqStrictSuffixInduction ==\n ASSUME NEW P(_), NEW S,\n \\A t \\in Seq(S) : (\\A s \\in Seq(S) : IsStrictSuffix(s,t) => P(s)) => P(t)\n PROVE \\A s \\in Seq(S) : P(s)\n\n(***************************************************************************)\n(* Since the (strict) prefix and suffix orderings on sequences are *)\n(* well-founded, they can be used for defining recursive functions. *)\n(* The operators OpDefinesFcn, WFInductiveDefines, and WFInductiveUnique *)\n(* are defined in module WellFoundedInduction. *)\n(***************************************************************************)\n\nStrictPrefixesDetermineDef(S, Def(_,_)) ==\n \\A g,h : \\A seq \\in Seq(S) :\n (\\A pre \\in Seq(S) : IsStrictPrefix(pre,seq) => g[pre] = h[pre])\n => Def(g, seq) = Def(h, seq)\n\nLEMMA StrictPrefixesDetermineDef_WFDefOn ==\n ASSUME NEW S, NEW Def(_,_), StrictPrefixesDetermineDef(S, Def)\n PROVE WFDefOn(OpToRel(IsStrictPrefix, Seq(S)), Seq(S), Def)\n\nTHEOREM PrefixRecursiveSequenceFunctionUnique ==\n ASSUME NEW S, NEW Def(_,_), StrictPrefixesDetermineDef(S, Def)\n PROVE WFInductiveUnique(Seq(S), Def)\n\nTHEOREM PrefixRecursiveSequenceFunctionDef ==\n ASSUME NEW S, NEW Def(_,_), NEW f,\n StrictPrefixesDetermineDef(S, Def),\n OpDefinesFcn(f, Seq(S), Def)\n PROVE WFInductiveDefines(f, Seq(S), Def)\n\nTHEOREM PrefixRecursiveSequenceFunctionType ==\n ASSUME NEW S, NEW T, NEW Def(_,_), NEW f,\n T # {},\n StrictPrefixesDetermineDef(S, Def),\n WFInductiveDefines(f, Seq(S), Def),\n \\A g \\in [Seq(S) -> T], s \\in Seq(S) : Def(g,s) \\in T\n PROVE f \\in [Seq(S) -> T]\n\nStrictSuffixesDetermineDef(S, Def(_,_)) ==\n \\A g,h : \\A seq \\in Seq(S) :\n (\\A suf \\in Seq(S) : IsStrictSuffix(suf,seq) => g[suf] = h[suf])\n => Def(g, seq) = Def(h, seq)\n\nLEMMA StrictSuffixesDetermineDef_WFDefOn ==\n ASSUME NEW S, NEW Def(_,_), StrictSuffixesDetermineDef(S, Def)\n PROVE WFDefOn(OpToRel(IsStrictSuffix, Seq(S)), Seq(S), Def)\n\nTHEOREM SuffixRecursiveSequenceFunctionUnique ==\n ASSUME NEW S, NEW Def(_,_), StrictSuffixesDetermineDef(S, Def)\n PROVE WFInductiveUnique(Seq(S), Def)\n\nTHEOREM SuffixRecursiveSequenceFunctionDef ==\n ASSUME NEW S, NEW Def(_,_), NEW f,\n StrictSuffixesDetermineDef(S, Def),\n OpDefinesFcn(f, Seq(S), Def)\n PROVE WFInductiveDefines(f, Seq(S), Def)\n\nTHEOREM SuffixRecursiveSequenceFunctionType ==\n ASSUME NEW S, NEW T, NEW Def(_,_), NEW f,\n T # {},\n StrictSuffixesDetermineDef(S, Def),\n WFInductiveDefines(f, Seq(S), Def),\n \\A g \\in [Seq(S) -> T], s \\in Seq(S) : Def(g,s) \\in T\n PROVE f \\in [Seq(S) -> T]\n\n(***************************************************************************)\n(* The following theorems justify ``primitive recursive'' functions over *)\n(* sequences, with a base case for the empty sequence and recursion along *)\n(* either the Tail or the Front of a non-empty sequence. *)\n(***************************************************************************)\n\nTailInductiveDefHypothesis(f, S, f0, Def(_,_)) ==\n f = CHOOSE g : g = [s \\in Seq(S) |-> IF s = <<>> THEN f0 ELSE Def(g[Tail(s)], s)]\n\nTailInductiveDefConclusion(f, S, f0, Def(_,_)) ==\n f = [s \\in Seq(S) |-> IF s = <<>> THEN f0 ELSE Def(f[Tail(s)], s)]\n\nTHEOREM TailInductiveDef ==\n ASSUME NEW S, NEW Def(_,_), NEW f, NEW f0,\n TailInductiveDefHypothesis(f, S, f0, Def)\n PROVE TailInductiveDefConclusion(f, S, f0, Def)\n\nTHEOREM TailInductiveDefType ==\n ASSUME NEW S, NEW Def(_,_), NEW f, NEW f0, NEW T,\n TailInductiveDefConclusion(f, S, f0, Def),\n f0 \\in T,\n \\A v \\in T, s \\in Seq(S) : s # <<>> => Def(v,s) \\in T\n PROVE f \\in [Seq(S) -> T]\n\nFrontInductiveDefHypothesis(f, S, f0, Def(_,_)) ==\n f = CHOOSE g : g = [s \\in Seq(S) |-> IF s = <<>> THEN f0 ELSE Def(g[Front(s)], s)]\n\nFrontInductiveDefConclusion(f, S, f0, Def(_,_)) ==\n f = [s \\in Seq(S) |-> IF s = <<>> THEN f0 ELSE Def(f[Front(s)], s)]\n\nTHEOREM FrontInductiveDef ==\n ASSUME NEW S, NEW Def(_,_), NEW f, NEW f0,\n FrontInductiveDefHypothesis(f, S, f0, Def)\n PROVE FrontInductiveDefConclusion(f, S, f0, Def)\n\nTHEOREM FrontInductiveDefType ==\n ASSUME NEW S, NEW Def(_,_), NEW f, NEW f0, NEW T,\n FrontInductiveDefConclusion(f, S, f0, Def),\n f0 \\in T,\n \\A v \\in T, s \\in Seq(S) : s # <<>> => Def(v,s) \\in T\n PROVE f \\in [Seq(S) -> T]\n\n----------------------------------------------------------------------------\n\n(**************************************************************************)\n(* Recursive characterization of FoldLeft. *)\n(**************************************************************************)\nTHEOREM FoldLeftRecursion ==\n ASSUME NEW op(_,_), NEW base, NEW S, NEW seq \\in Seq(S)\n PROVE FoldLeft(op, base, seq) = \n IF seq = << >> THEN base \n ELSE op(FoldLeft(op, base, Front(seq)), Last(seq))\n\nTHEOREM FoldLeftEmpty ==\n ASSUME NEW op(_,_), NEW base\n PROVE FoldLeft(op, base, <<>>) = base\n\nTHEOREM FoldLeftNonempty ==\n ASSUME NEW op(_,_), NEW base, NEW S, NEW seq \\in Seq(S), seq # << >>\n PROVE FoldLeft(op, base, seq) = op(FoldLeft(op, base, Front(seq)), Last(seq))\n\n(**************************************************************************)\n(* Interaction of FoldLeft and Append. *)\n(**************************************************************************)\nTHEOREM FoldLeftAppend ==\n ASSUME NEW op(_,_), NEW base, NEW S, NEW seq \\in Seq(S), NEW s \\in S \n PROVE FoldLeft(op, base, Append(seq,s)) = op(FoldLeft(op, base, seq), s)\n\n(**************************************************************************)\n(* Type of FoldLeft. *)\n(**************************************************************************)\nTHEOREM FoldLeftType ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ, NEW seq \\in Seq(Typ),\n \\A t,u \\in Typ : op(t,u) \\in Typ \n PROVE FoldLeft(op, base, seq) \\in Typ \n\n(**************************************************************************)\n(* Interaction of FoldLeft and concatenation when op is associative and *)\n(* base is a right identity. *)\n(**************************************************************************)\nTHEOREM FoldLeftConcat ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ, \n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u), v),\n \\A t \\in Typ : op(t, base) = t,\n NEW ls \\in Seq(Typ), NEW rs \\in Seq(Typ)\n PROVE FoldLeft(op, base, ls \\o rs) = \n op(FoldLeft(op, base, ls), FoldLeft(op, base, rs))\n\n(**************************************************************************)\n(* Recursive characterization of FoldRight. *)\n(**************************************************************************)\nTHEOREM FoldRightRecursion ==\n ASSUME NEW op(_,_), NEW base, NEW S, NEW seq \\in Seq(S)\n PROVE FoldRight(op, seq, base) = \n IF seq = << >> THEN base \n ELSE op(Head(seq), FoldRight(op, Tail(seq), base))\n\nTHEOREM FoldRightEmpty ==\n ASSUME NEW op(_,_), NEW base\n PROVE FoldRight(op, <<>>, base) = base\n\nTHEOREM FoldRightNonempty ==\n ASSUME NEW op(_,_), NEW base, NEW S, NEW seq \\in Seq(S), seq # << >>\n PROVE FoldRight(op, seq, base) = op(Head(seq), FoldRight(op, Tail(seq), base))\n\n(**************************************************************************)\n(* Interaction of FoldRight and Cons. *)\n(**************************************************************************)\nTHEOREM FoldRightCons ==\n ASSUME NEW op(_,_), NEW base, NEW S, NEW seq \\in Seq(S), NEW s \\in S \n PROVE FoldRight(op, Cons(s, seq), base) = op(s, FoldRight(op, seq, base))\n\n(**************************************************************************)\n(* Type of FoldRight. *)\n(**************************************************************************)\nTHEOREM FoldRightType ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ, NEW seq \\in Seq(Typ),\n \\A t,u \\in Typ : op(t,u) \\in Typ \n PROVE FoldRight(op, seq, base) \\in Typ \n\n(**************************************************************************)\n(* Interaction of FoldRight and concatenation when op is associative and *)\n(* base is a left identity. *)\n(**************************************************************************)\nTHEOREM FoldRightConcat ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ, \n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u), v),\n \\A t \\in Typ : op(base, t) = t,\n NEW ls \\in Seq(Typ), NEW rs \\in Seq(Typ)\n PROVE FoldRight(op, ls \\o rs, base) = \n op(FoldRight(op, ls, base), FoldRight(op, rs, base))\n\n(**************************************************************************)\n(* FoldLeftDomain and FoldRightDomain cannot be characterized recursively *)\n(* in terms of the same operators, we reduce them to MapThenFoldSet. *)\n(**************************************************************************)\nTHEOREM FoldLeftDomainIsMFS ==\n ASSUME NEW op(_,_), NEW base, NEW S, NEW seq \\in Seq(S)\n PROVE FoldLeftDomain(op, base, seq) =\n MapThenFoldSet(LAMBDA x,y : op(y,x), base, \n LAMBDA i : i, LAMBDA T : Max(T), 1 .. Len(seq))\n\nTHEOREM FoldRightDomainIsMFS ==\n ASSUME NEW op(_,_), NEW base, NEW S, NEW seq \\in Seq(S)\n PROVE FoldRightDomain(op, seq, base) =\n MapThenFoldSet(op, base, \n LAMBDA i : i, LAMBDA T : Min(T), 1 .. Len(seq))\n\n============================================================================\n", "SequencesExtTheorems_proofs": "-------------------- MODULE SequencesExtTheorems_proofs --------------------\n(**************************************************************************)\n(* This module contains theorems about the operators on sequences defined *)\n(* in module SequencesExt. *)\n(**************************************************************************)\nEXTENDS Sequences, SequencesExt, FiniteSetsExt, Functions, Integers, \n SequenceTheorems, NaturalsInduction, WellFoundedInduction, \n FiniteSetTheorems, FiniteSetsExtTheorems, FoldsTheorems, TLAPS\n\n(***************************************************************************)\n(* SeqOf(S,n) is the set of sequences over S whose length is at most n. *)\n(***************************************************************************)\nTHEOREM SeqOfSeq ==\n ASSUME NEW S, NEW n \\in Int \n PROVE SeqOf(S,n) = {s \\in Seq(S) : Len(s) <= n}\n<1>1. ASSUME NEW s \\in SeqOf(S,n)\n PROVE s \\in Seq(S) /\\ Len(s) <= n \n BY DEF SeqOf \n<1>2. ASSUME NEW s \\in Seq(S), Len(s) <= n \n PROVE s \\in SeqOf(S,n)\n BY <1>2 DEF SeqOf \n<1>. QED BY <1>1, <1>2\n\n(***************************************************************************)\n(* Theorems about Cons. *)\n(* Cons(elt, seq) == <<elt>> \\o seq *)\n(***************************************************************************)\n\nTHEOREM ConsProperties ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW elt \\in S\n PROVE /\\ Cons(elt, seq) \\in Seq(S)\n /\\ Cons(elt, seq) # <<>>\n /\\ Len(Cons(elt, seq)) = Len(seq)+1\n /\\ Head(Cons(elt, seq)) = elt\n /\\ Tail(Cons(elt, seq)) = seq\n /\\ Cons(elt, seq)[1] = elt\n /\\ \\A i \\in 1 .. Len(seq) : Cons(elt, seq)[i+1] = seq[i]\n /\\ \\A i \\in 2 .. Len(seq)+1 : Cons(elt, seq)[i] = seq[i-1]\nBY DEF Cons\n\nTHEOREM ConsEmpty ==\n ASSUME NEW x\n PROVE Cons(x, << >>) = << x >>\nBY <<x>> \\in Seq({x}) DEF Cons\n\nTHEOREM ConsHeadTail ==\n ASSUME NEW S, NEW seq \\in Seq(S), seq # << >>\n PROVE Cons(Head(seq), Tail(seq)) = seq\nBY DEF Cons\n\nTHEOREM ConsAppend ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW x \\in S, NEW y \\in S\n PROVE Cons(x, Append(seq, y)) = Append(Cons(x,seq), y)\nBY DEF Cons\n\nTHEOREM ConsConcat ==\n ASSUME NEW S, NEW e \\in S, NEW ls \\in Seq(S), NEW rs \\in Seq(S)\n PROVE Cons(e, ls) \\o rs = Cons(e, ls \\o rs)\nBY DEF Cons\n\nTHEOREM ConsInjective ==\n ASSUME NEW S, NEW e \\in S, NEW s \\in Seq(S), NEW f \\in S, NEW t \\in Seq(S)\n PROVE Cons(e,s) = Cons(f,t) <=> e = f /\\ s = t\n<1>1. ASSUME Cons(e,s) = Cons(f,t) PROVE e = f /\\ s = t\n <2>1. /\\ Head(Cons(e,s)) = e /\\ Tail(Cons(e,s)) = s\n /\\ Head(Cons(f,t)) = f /\\ Tail(Cons(f,t)) = t\n BY DEF Cons\n <2>. QED BY <1>1, <2>1\n<1>. QED BY <1>1\n\nTHEOREM SequencesInductionCons ==\n ASSUME NEW P(_), NEW S,\n P(<< >>),\n \\A s \\in Seq(S), e \\in S : P(s) => P(Cons(e,s))\n PROVE \\A seq \\in Seq(S) : P(seq)\n<1>. DEFINE Q(n) == \\A seq \\in Seq(S) : Len(seq) = n => P(seq)\n<1>1. SUFFICES \\A k \\in Nat : Q(k)\n OBVIOUS\n<1>2. Q(0)\n OBVIOUS\n<1>3. ASSUME NEW n \\in Nat, Q(n)\n PROVE Q(n+1)\n <2>1. ASSUME NEW s \\in Seq(S), Len(s) = n+1\n PROVE P(s)\n <3>1. /\\ Tail(s) \\in Seq(S)\n /\\ Head(s) \\in S\n /\\ Len(Tail(s)) = n\n /\\ Cons(Head(s), Tail(s)) = s\n BY <2>1, ConsHeadTail\n <3>2. P(Tail(s))\n BY <1>3, <3>1, Zenon\n <3>. QED BY <3>1, <3>2, Zenon\n <2>. QED BY <2>1\n<1>. QED BY <1>2, <1>3, NatInduction, Isa\n\n(***************************************************************************)\n(* Theorems about Contains. *)\n(* Contains(s, e) == \\E i \\in 1 .. Len(s) : s[i] = e *)\n(***************************************************************************)\n\nTHEOREM ContainsRange ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW e\n PROVE Contains(s, e) <=> e \\in Range(s)\nBY DEF Contains, Range \n\nTHEOREM ContainsEmpty ==\n ASSUME NEW e\n PROVE ~ Contains(<< >>, e)\nBY DEF Contains\n\nTHEOREM ContainsAppend ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW x, NEW e\n PROVE Contains(Append(s, x), e) <=> (Contains(s, e) \\/ e = x)\nBY DEF Contains \n\nTHEOREM ContainsSingleton ==\n ASSUME NEW x\n PROVE \\A e : Contains(<< x >>, e) <=> e = x\nBY Isa DEF Contains \n\nTHEOREM ContainsConcat ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW T, NEW t \\in Seq(T), NEW e\n PROVE Contains(s \\o t, e) <=> (Contains(s, e) \\/ Contains(t, e))\n<1>1. ASSUME Contains(s \\o t, e), ~ Contains(t, e) PROVE Contains(s, e)\n BY <1>1 DEF Contains \n<1>2. ASSUME Contains(s, e) PROVE Contains(s \\o t, e)\n <2>. PICK i \\in 1 .. Len(s) : s[i] = e \n BY <1>2 DEF Contains \n <2>. QED BY i \\in 1 .. Len(s \\o t) DEF Contains \n<1>3. ASSUME Contains(t, e) PROVE Contains(s \\o t, e)\n <2>. PICK i \\in 1 .. Len(t) : t[i] = e \n BY <1>3 DEF Contains \n <2>. QED BY Len(s)+i \\in 1 .. Len(s \\o t) DEF Contains \n<1>. QED BY <1>1, <1>2, <1>3\n\nTHEOREM ContainsCons ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW x, NEW e\n PROVE Contains(Cons(x, s), e) <=> (e = x \\/ Contains(s, e))\nBY ContainsConcat, ContainsSingleton, <<x>> \\in Seq({x}) DEF Cons \n\nTHEOREM ContainsTail ==\n ASSUME NEW S, NEW s \\in Seq(S), s # << >>, NEW e\n PROVE Contains(Tail(s), e) => Contains(s, e)\nBY DEF Contains \n\nTHEOREM ContainsTailExceptHead ==\n ASSUME NEW S, NEW s \\in Seq(S), s # << >>, NEW e,\n Contains(s, e), e # Head(s)\n PROVE Contains(Tail(s), e)\nBY DEF Contains \n\n(***************************************************************************)\n(* Theorems about IsSorted. *)\n(* IsSorted(s, op) == \\A i,j \\in 1..Len(s) : i<j => op(s[i],s[j]) *)\n(***************************************************************************)\n\nTHEOREM SortedEmpty ==\n ASSUME NEW op(_,_)\n PROVE IsSorted(<< >>, op)\nBY DEF IsSorted\n\nTHEOREM SortedSingleton ==\n ASSUME NEW S, NEW x \\in S, NEW op(_,_)\n PROVE IsSorted(<< x >>, op)\nBY DEF IsSorted \n\nTHEOREM SortedAppend ==\n ASSUME NEW S, NEW op(_,_),\n \\A x,y,z \\in S : op(x,y) /\\ op(y,z) => op(x,z),\n NEW s \\in Seq(S), IsSorted(s, op),\n NEW e \\in S, s # << >> => op(s[Len(s)], e)\n PROVE IsSorted(Append(s, e), op)\nBY DEF IsSorted \n\nTHEOREM SortedConcat ==\n ASSUME NEW S, NEW op(_,_),\n \\A x,y,z \\in S : op(x,y) /\\ op(y,z) => op(x,z),\n NEW s \\in Seq(S), NEW t \\in Seq(S),\n IsSorted(s, op), IsSorted(t, op),\n (Len(s) > 0 /\\ Len(t) > 0) => op(s[Len(s)], t[1])\n PROVE IsSorted(s \\o t, op)\nBY SMTT(20) DEF IsSorted \n\nTHEOREM SortedInjective ==\n ASSUME NEW S, NEW op(_,_),\n \\A x \\in S : ~ op(x, x),\n NEW s \\in Seq(S), IsSorted(s, op)\n PROVE IsInjective(s)\nBY DEF IsSorted, IsInjective \n\nTHEOREM SortedSubSeq ==\n ASSUME NEW S, NEW op(_,_),\n NEW s \\in Seq(S), IsSorted(s, op),\n NEW m \\in 1..Len(s)+1, NEW n \\in 0..Len(s)\n PROVE IsSorted(SubSeq(s, m, n), op)\nBY DEF IsSorted \n\n(***************************************************************************)\n(* Theorems about InsertAt and RemoveAt. *)\n(* InsertAt(seq,i,elt) == *)\n(* SubSeq(seq, 1, i-1) \\o <<elt>> \\o SubSeq(seq, i, Len(seq)) *)\n(* RemoveAt(seq, i) == SubSeq(seq, 1, i-1) \\o SubSeq(seq, i+1, Len(seq)) *)\n(***************************************************************************)\n\nTHEOREM InsertAtProperties ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW i \\in 1 .. Len(seq)+1, NEW elt \\in S\n PROVE /\\ InsertAt(seq,i,elt) \\in Seq(S)\n /\\ Len(InsertAt(seq,i,elt)) = Len(seq)+1\n /\\ \\A j \\in 1 .. Len(seq)+1 : InsertAt(seq,i,elt)[j] =\n IF j<i THEN seq[j]\n ELSE IF j=i THEN elt\n ELSE seq[j-1]\nBY DEF InsertAt\n\nTHEOREM RemoveAtProperties ==\n ASSUME NEW S, NEW seq \\in Seq(S),\n NEW i \\in 1..Len(seq)\n PROVE /\\ RemoveAt(seq,i) \\in Seq(S)\n /\\ Len(RemoveAt(seq,i)) = Len(seq) - 1\n /\\ \\A j \\in 1 .. Len(seq)-1 : RemoveAt(seq,i)[j] = IF j<i THEN seq[j] ELSE seq[j+1]\nBY DEF RemoveAt\n\n(***************************************************************************)\n(* Theorems about Front and Last. *)\n(* Front(seq) == SubSeq(seq, 1, Len(seq)-1) *)\n(* Last(seq) == seq[Len(seq)] *)\n(***************************************************************************)\n\nTHEOREM FrontProperties ==\n ASSUME NEW S, NEW seq \\in Seq(S)\n PROVE /\\ Front(seq) \\in Seq(S)\n /\\ Len(Front(seq)) = IF seq = << >> THEN 0 ELSE Len(seq)-1\n /\\ \\A i \\in 1 .. Len(seq)-1 : Front(seq)[i] = seq[i]\nBY DEF Front\n\nTHEOREM FrontOfEmpty == Front(<< >>) = << >>\nBY DEF Front\n\nTHEOREM LastProperties ==\n ASSUME NEW S, NEW seq \\in Seq(S), seq # << >>\n PROVE /\\ Last(seq) \\in S\n /\\ Append(Front(seq), Last(seq)) = seq\nBY DEF Front, Last\n\nTHEOREM FrontLastOfSubSeq ==\n ASSUME NEW S, NEW seq \\in Seq(S),\n NEW m \\in 1 .. Len(seq), NEW n \\in m .. Len(seq)\n PROVE /\\ Front(SubSeq(seq,m,n)) = SubSeq(seq, m, n-1)\n /\\ Last(SubSeq(seq,m,n)) = seq[n]\nBY DEF Front, Last\n\nTHEOREM FrontLastAppend ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW e \\in S\n PROVE /\\ Front(Append(seq, e)) = seq\n /\\ Last(Append(seq, e)) = e\nBY DEF Front, Last\n\nLEMMA FrontInjectiveSeq ==\n ASSUME NEW S, NEW seq \\in Seq(S), IsInjective(seq), seq # << >>\n PROVE /\\ IsInjective(Front(seq))\n /\\ Range(Front(seq)) = Range(seq) \\ {Last(seq)}\n<1>. DEFINE ft == Front(seq)\n<1>1. IsInjective(ft)\n BY DEF IsInjective, Front\n<1>2. ASSUME NEW e \\in Range(ft)\n PROVE e \\in Range(seq) \\ {Last(seq)}\n BY DEF Range, IsInjective, Front, Last\n<1>3. ASSUME NEW e \\in Range(seq) \\ {Last(seq)}\n PROVE e \\in Range(ft)\n <2>1. PICK i \\in 1 .. Len(seq)-1 : e = seq[i]\n BY DEF Range, Last\n <2>. QED BY <2>1 DEF Range, Front\n<1>. QED BY <1>1, <1>2, <1>3\n\nTHEOREM SequencesInductionFront ==\n ASSUME NEW S, NEW P(_),\n P(<< >>),\n \\A s \\in Seq(S) : (s # << >>) /\\ P(Front(s)) => P(s)\n PROVE \\A s \\in Seq(S) : P(s)\n<1>. DEFINE Q(n) == \\A s \\in Seq(S) : Len(s) = n => P(s)\n<1>. SUFFICES \\A k \\in Nat : Q(k)\n OBVIOUS\n<1>1. Q(0)\n OBVIOUS\n<1>2. ASSUME NEW n \\in Nat, Q(n)\n PROVE Q(n+1)\n <2>. SUFFICES ASSUME NEW s \\in Seq(S), Len(s) = n+1\n PROVE P(s)\n OBVIOUS\n <2>1. /\\ Front(s) \\in Seq(S)\n /\\ Len(Front(s)) = n\n BY FrontProperties\n <2>. QED BY <2>1, <1>2\n<1>3. QED BY <1>1, <1>2, NatInduction, Isa\n\n(***************************************************************************)\n(* Theorems about Reverse. *)\n(* Reverse(seq) == [j \\in 1 .. Len(seq) |-> seq[Len(seq)-j+1] ] *)\n(***************************************************************************)\n\nTHEOREM ReverseProperties ==\n ASSUME NEW S, NEW seq \\in Seq(S)\n PROVE /\\ Reverse(seq) \\in Seq(S)\n /\\ Len(Reverse(seq)) = Len(seq)\n /\\ Reverse(Reverse(seq)) = seq\nBY DEF Reverse\n\nTHEOREM ReverseEmpty == Reverse(<< >>) = << >>\nBY DEF Reverse\n\nTHEOREM ReverseSingleton == \\A x : Reverse(<< x >>) = << x >>\nBY DEF Reverse\n\nTHEOREM ReverseEqual ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S), Reverse(s) = Reverse(t)\n PROVE s = t\n<1>1. Len(s) = Len(t) BY DEF Reverse\n<1>2. ASSUME NEW i \\in 1 .. Len(s)\n PROVE s[i] = t[i]\n <2>. /\\ s[i] = Reverse(s)[Len(s)-i+1]\n /\\ t[i] = Reverse(t)[Len(s)-i+1]\n BY DEF Reverse\n <2>. QED OBVIOUS\n<1>. QED BY <1>1, <1>2, SeqEqual\n\nTHEOREM ReverseEmptyIffEmpty ==\n ASSUME NEW S, NEW seq \\in Seq(S), Reverse(seq) = <<>>\n PROVE seq = <<>>\nBY DEF Reverse\n\nTHEOREM ReverseConcat ==\n ASSUME NEW S, NEW s1 \\in Seq(S), NEW s2 \\in Seq(S)\n PROVE Reverse(s1 \\o s2) = Reverse(s2) \\o Reverse(s1)\n<1>1. /\\ Reverse(s1) \\in Seq(S) /\\ Reverse(s2) \\in Seq(S)\n /\\ Reverse(s1 \\o s2) \\in Seq(S)\n /\\ Len(Reverse(s1 \\o s2)) = Len(Reverse(s2) \\o Reverse(s1))\n BY ReverseProperties\n<1>2. ASSUME NEW i \\in 1 .. Len(Reverse(s1 \\o s2))\n PROVE Reverse(s1 \\o s2)[i] = (Reverse(s2) \\o Reverse(s1))[i]\n BY DEF Reverse\n<1>. QED BY <1>1, <1>2\n\nTHEOREM ReverseAppend ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW e \\in S\n PROVE Reverse(Append(seq,e)) = Cons(e, Reverse(seq))\nBY DEF Reverse, Cons\n\nTHEOREM ReverseCons ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW e \\in S\n PROVE Reverse(Cons(e,seq)) = Append(Reverse(seq), e)\n<1>1. Reverse(seq) \\in Seq(S)\n BY ReverseProperties\n<1>2. Reverse(Cons(e, seq)) = Reverse(seq) \\o <<e>>\n BY ReverseConcat, ReverseSingleton DEF Cons\n<1>3. @ = Append(Reverse(seq), e)\n BY <1>1\n<1>. QED BY <1>2, <1>3\n\n\nTHEOREM ReverseSubSeq ==\n ASSUME NEW S, NEW seq \\in Seq(S),\n NEW m \\in 1..Len(seq), NEW n \\in 0..Len(seq)\n PROVE Reverse(SubSeq(seq, m , n)) = SubSeq(Reverse(seq), Len(seq)-n+1, Len(seq)-m+1)\n<1>. /\\ Reverse(seq) \\in Seq(S)\n /\\ Len(seq)-n+1 \\in 1 .. Len(Reverse(seq))+1\n /\\ Len(seq)-m+1 \\in 1 .. Len(Reverse(seq))\n BY ReverseProperties\n<1>. QED BY DEF Reverse\n\nTHEOREM ReversePalindrome ==\n ASSUME NEW S, NEW seq \\in Seq(S),\n Reverse(seq) = seq\n PROVE Reverse(seq \\o seq) = seq \\o seq\nBY ReverseConcat\n\nTHEOREM LastEqualsHeadReverse ==\n ASSUME NEW S, NEW seq \\in Seq(S), seq # << >>\n PROVE Last(seq) = Head(Reverse(seq))\nBY DEF Last, Reverse\n\nTHEOREM ReverseFrontEqualsTailReverse ==\n ASSUME NEW S, NEW seq \\in Seq(S), seq # << >>\n PROVE Reverse(Front(seq)) = Tail(Reverse(seq))\n<1>1. Reverse(seq) \\in Seq(S) /\\ Len(Reverse(seq)) = Len(seq)\n BY ReverseProperties\n<1>2. Reverse(Front(seq)) = SubSeq(Reverse(seq), 2, Len(seq))\n BY ReverseSubSeq DEF Front\n<1>. QED BY <1>1, <1>2\n\n(* The range of the reverse sequence equals that of the original one. *)\nTHEOREM RangeReverse ==\n ASSUME NEW S, NEW seq \\in Seq(S)\n PROVE /\\ DOMAIN Reverse(seq) = DOMAIN seq\n /\\ Range(Reverse(seq)) = Range(seq)\n<1>1. /\\ DOMAIN seq = 1 .. Len(seq)\n /\\ DOMAIN Reverse(seq) = 1 .. Len(seq)\n /\\ Reverse(seq) \\in Seq(S)\n BY DEF Reverse\n<1>2. ASSUME NEW i \\in 1 .. Len(seq)\n PROVE seq[i] = Reverse(seq)[Len(seq)-i+1]\n BY DEF Reverse\n<1>. QED BY <1>1, <1>2 DEF Range\n\n(***************************************************************************)\n(* Theorems about prefixes and suffixes of sequences. *)\n(* IsPrefix(s,t) == \\E u \\in Seq(Range(t)) : t = s \\o u *)\n(* IsStrictPrefix(s,t) == IsPrefix(s,t) /\\ s # t *)\n(* IsSuffix(s,t) == \\E u \\in Seq(Range(t)) : t = u \\o s *)\n(* IsStrictSuffix(s,t) == IsSuffix(s,t) /\\ s # t *)\n(***************************************************************************)\n\n(***************************************************************************)\n(* The following theorem gives three alternative characterizations of *)\n(* prefixes. It also implies that any prefix of a sequence t is at most *)\n(* as long as t. *)\n(***************************************************************************)\nTHEOREM IsPrefixProperties ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S)\n PROVE /\\ IsPrefix(s,t) <=> \\E u \\in Seq(S) : t = s \\o u\n /\\ IsPrefix(s,t) <=> Len(s) <= Len(t) /\\ s = SubSeq(t, 1, Len(s))\n /\\ IsPrefix(s,t) <=> Len(s) <= Len(t) /\\ s = Restrict(t, DOMAIN s)\n<1>1. IsPrefix(s,t) <=> (Len(s) <= Len(t) /\\ s = SubSeq(t, 1, Len(s)))\n <2>1. IsPrefix(s,t) => Len(s) =< Len(t) BY DEF IsPrefix\n <2>2. IsPrefix(s,t) => s = SubSeq(t, 1, Len(s)) BY <2>1 DEF IsPrefix\n <2>3. (Len(s) <= Len(t) /\\ s = SubSeq(t, 1, Len(s))) => IsPrefix(s, t) BY DEF IsPrefix\n <2>q. QED BY <2>1, <2>2, <2>3\n<1>2. IsPrefix(s,t) <=> \\E u \\in Seq(S) : t = s \\o u\n <2>1. ASSUME IsPrefix(s,t) PROVE \\E u \\in Seq(S) : t = s \\o u\n <3>1. Len(s) <= Len(t) /\\ s = SubSeq(t, 1, Len(s))\n BY <1>1, <2>1\n <3>2. /\\ SubSeq(t, Len(s)+1, Len(t)) \\in Seq(S)\n /\\ t = s \\o SubSeq(t, Len(s)+1, Len(t))\n BY <3>1\n <3>. QED BY <3>2\n <2>2. ASSUME NEW u \\in Seq(S), t = s \\o u PROVE IsPrefix(s,t)\n BY <2>2 DEF IsPrefix\n <2>. QED BY <2>1, <2>2\n<1>3. IsPrefix(s,t) <=> Len(s) <= Len(t) /\\ s = Restrict(t, DOMAIN s)\n BY DEF IsPrefix, Restrict\n<1>. QED BY <1>1, <1>2, <1>3\n\nTHEOREM IsStrictPrefixProperties ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S)\n PROVE /\\ IsStrictPrefix(s,t) <=> \\E u \\in Seq(S) : u # << >> /\\ t = s \\o u\n /\\ IsStrictPrefix(s,t) <=> Len(s) < Len(t) /\\ s = SubSeq(t, 1, Len(s))\n /\\ IsStrictPrefix(s,t) <=> Len(s) < Len(t) /\\ s = Restrict(t, DOMAIN s)\n /\\ IsStrictPrefix(s,t) <=> IsPrefix(s,t) /\\ Len(s) < Len(t)\n<1>1. IsStrictPrefix(s,t) => Len(s) < Len(t)\n BY IsPrefixProperties DEF IsStrictPrefix\n<1>2. IsStrictPrefix(s,t) <=> \\E u \\in Seq(S) : u # << >> /\\ t = s \\o u\n <2>1. ASSUME IsStrictPrefix(s,t)\n PROVE \\E u \\in Seq(S) : u # << >> /\\ t = s \\o u\n \\* the following doesn't seem to work everywhere\n \\* BY <2>1, IsPrefixProperties DEF IsStrictPrefix\n <3>1. IsPrefix(s,t) /\\ s # t\n BY <2>1 DEF IsStrictPrefix\n <3>2. PICK u \\in Seq(S): t = s \\o u\n BY <3>1, IsPrefixProperties\n <3>. QED BY <3>1, <3>2\n\n <2>2. ASSUME NEW u \\in Seq(S), u # << >>, t = s \\o u\n PROVE IsStrictPrefix(s,t)\n <3>1. IsPrefix(s,t)\n BY <2>2, IsPrefixProperties, Zenon\n <3>2. s # t\n BY <2>2\n <3>. QED BY <3>1, <3>2 DEF IsStrictPrefix\n <2>. QED BY <2>1, <2>2, Zenon\n<1>3. /\\ IsStrictPrefix(s,t) <=> Len(s) < Len(t) /\\ s = SubSeq(t, 1, Len(s))\n /\\ IsStrictPrefix(s,t) <=> Len(s) < Len(t) /\\ s = Restrict(t, DOMAIN s)\n /\\ IsStrictPrefix(s,t) <=> IsPrefix(s,t) /\\ Len(s) < Len(t)\n BY <1>1, IsPrefixProperties DEF IsStrictPrefix\n<1>. QED BY <1>1, <1>2, <1>3\n\nTHEOREM IsPrefixElts ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S), NEW i \\in 1 .. Len(s),\n IsPrefix(s,t)\n PROVE s[i] = t[i]\nBY IsPrefixProperties\n\nTHEOREM EmptyIsPrefix ==\n ASSUME NEW S, NEW s \\in Seq(S)\n PROVE /\\ IsPrefix(<<>>, s)\n /\\ IsPrefix(s, <<>>) <=> s = <<>>\n /\\ IsStrictPrefix(<<>>, s) <=> s # <<>>\n /\\ ~ IsStrictPrefix(s, <<>>)\nBY IsPrefixProperties, IsStrictPrefixProperties\n\nTHEOREM IsPrefixConcat ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S)\n PROVE IsPrefix(s, s \\o t)\nBY IsPrefixProperties, ConcatProperties\n\nTHEOREM IsStrictPrefixAppend ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW e \\in S\n PROVE IsStrictPrefix(s, Append(s,e))\n<1>1. /\\ Append(s,e) = s \\o <<e>>\n /\\ Append(s,e) \\in Seq(S)\n /\\ <<e>> \\in Seq(S)\n /\\ <<e>> # << >>\n OBVIOUS\n<1>. QED BY <1>1, IsStrictPrefixProperties\n\nTHEOREM FrontIsPrefix ==\n ASSUME NEW S, NEW s \\in Seq(S)\n PROVE /\\ IsPrefix(Front(s), s)\n /\\ s # <<>> => IsStrictPrefix(Front(s), s)\n<1>1. /\\ Front(s) \\in Seq(S)\n /\\ Len(Front(s)) <= Len(s)\n /\\ Front(s) = SubSeq(s, 1, Len(Front(s)))\n /\\ s # << >> => Len(Front(s)) < Len(s)\n BY DEF Front\n<1>. QED BY <1>1, IsPrefixProperties, IsStrictPrefixProperties\n\n(***************************************************************************)\n(* (Strict) prefixes on sequences form a (strict) partial order, and *)\n(* the strict ordering is well-founded. *)\n(***************************************************************************)\nTHEOREM IsPrefixPartialOrder ==\n ASSUME NEW S\n PROVE /\\ \\A s \\in Seq(S) : IsPrefix(s,s)\n /\\ \\A s,t \\in Seq(S) : IsPrefix(s,t) /\\ IsPrefix(t,s) => s = t\n /\\ \\A s,t,u \\in Seq(S) : IsPrefix(s,t) /\\ IsPrefix(t,u) => IsPrefix(s,u)\nBY DEF IsPrefix\n\nTHEOREM ConcatIsPrefix ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S), NEW u \\in Seq(S),\n IsPrefix(s \\o t, u)\n PROVE IsPrefix(s, u)\nBY DEF IsPrefix\n\nTHEOREM ConcatIsPrefixCancel ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S), NEW u \\in Seq(S)\n PROVE /\\ IsPrefix(s \\o t, s \\o u) <=> IsPrefix(t, u)\n<1>1. ASSUME IsPrefix(t,u) PROVE IsPrefix(s \\o t, s \\o u)\n <2>1. PICK v \\in Seq(S) : u = t \\o v BY <1>1, IsPrefixProperties\n <2>2. s \\o u = (s \\o t) \\o v BY <2>1\n <2>. QED BY <2>2, IsPrefixProperties, s \\o u \\in Seq(S), s \\o t \\in Seq(S)\n<1>2. ASSUME IsPrefix(s \\o t, s \\o u) PROVE IsPrefix(t,u)\n <2>1. PICK v \\in Seq(S) : s \\o u = (s \\o t) \\o v\n BY <1>2, s \\o t \\in Seq(S), s \\o u \\in Seq(S), IsPrefixProperties\n <2>2. s \\o u = s \\o (t \\o v)\n BY <2>1\n <2>3. u = t \\o v\n BY t \\o v \\in Seq(S), <2>2, ConcatSimplifications\n <2>. QED BY <2>3, IsPrefixProperties\n<1>. QED BY <1>1, <1>2\n\nTHEOREM ConsIsPrefixCancel ==\n ASSUME NEW S, NEW e \\in S, NEW s \\in Seq(S), NEW t \\in Seq(S)\n PROVE IsPrefix(Cons(e,s), Cons(e,t)) <=> IsPrefix(s,t)\nBY <<e>> \\in Seq(S), ConcatIsPrefixCancel, Zenon DEF Cons\n\nTHEOREM ConsIsPrefix ==\n ASSUME NEW S, NEW e \\in S, NEW s \\in Seq(S), NEW u \\in Seq(S),\n IsPrefix(Cons(e,s), u)\n PROVE /\\ e = Head(u)\n /\\ IsPrefix(s, Tail(u))\n<1>. <<e>> \\in Seq(S)\n OBVIOUS\n<1>1. IsPrefix(<<e>>, u)\n BY ConcatIsPrefix, Zenon DEF Cons\n<1>2. PICK v \\in Seq(S) : u = Cons(e, v)\n BY <1>1, IsPrefixProperties DEF Cons\n<1>3. /\\ e = Head(u)\n /\\ v = Tail(u)\n /\\ IsPrefix(Cons(e,s), Cons(e, Tail(u)))\n BY <1>2, ConsProperties\n<1>. QED\n BY <1>3, ConsIsPrefixCancel\n\nTHEOREM IsStrictPrefixStrictPartialOrder ==\n ASSUME NEW S\n PROVE /\\ \\A s \\in Seq(S) : ~ IsStrictPrefix(s,s)\n /\\ \\A s,t \\in Seq(S) : IsStrictPrefix(s,t) => ~ IsStrictPrefix(t,s)\n /\\ \\A s,t,u \\in Seq(S) : IsStrictPrefix(s,t) /\\ IsStrictPrefix(t,u) => IsStrictPrefix(s,u)\n<1>1. ASSUME NEW s \\in Seq(S)\n PROVE ~ IsStrictPrefix(s,s)\n BY <1>1, IsStrictPrefixProperties\n<1>2. ASSUME NEW s \\in Seq(S), NEW t \\in Seq(S), IsStrictPrefix(s,t), IsStrictPrefix(t,s)\n PROVE FALSE\n BY <1>2, IsStrictPrefixProperties\n<1>3. ASSUME NEW s \\in Seq(S), NEW t \\in Seq(S), NEW u \\in Seq(S),\n IsStrictPrefix(s,t), IsStrictPrefix(t,u)\n PROVE IsStrictPrefix(s,u)\n <2>1. PICK st \\in Seq(S) \\ {<<>>} : t = s \\o st\n BY <1>3, IsStrictPrefixProperties\n <2>2. PICK tu \\in Seq(S) \\ {<<>>} : u = t \\o tu\n BY <1>3, IsStrictPrefixProperties\n <2>3. u = s \\o (st \\o tu)\n BY <2>1, <2>2\n <2>. QED BY <2>3, IsStrictPrefixProperties\n<1> QED BY <1>1, <1>2, <1>3\n\n\nTHEOREM IsStrictPrefixWellFounded ==\n ASSUME NEW S\n PROVE IsWellFoundedOn(OpToRel(IsStrictPrefix, Seq(S)), Seq(S))\n<1>1. IsWellFoundedOn(PreImage(Len, Seq(S), OpToRel(<, Nat)), Seq(S))\n BY NatLessThanWellFounded, PreImageWellFounded, IsaM(\"blast\")\n<1>2. OpToRel(IsStrictPrefix, Seq(S)) \\subseteq PreImage(Len, Seq(S), OpToRel(<, Nat))\n BY IsStrictPrefixProperties DEF PreImage, OpToRel\n<1>. QED\n BY <1>1, <1>2, IsWellFoundedOnSubrelation, Zenon\n\nTHEOREM SeqStrictPrefixInduction ==\n ASSUME NEW P(_), NEW S,\n \\A t \\in Seq(S) : (\\A s \\in Seq(S) : IsStrictPrefix(s,t) => P(s)) => P(t)\n PROVE \\A s \\in Seq(S) : P(s)\n<1>1. \\A t \\in Seq(S) :\n (\\A s \\in SetLessThan(t, OpToRel(IsStrictPrefix, Seq(S)), Seq(S)) : P(s))\n => P(t)\n BY Zenon DEF SetLessThan, OpToRel\n<1>. QED BY WFInduction, IsStrictPrefixWellFounded, <1>1, IsaM(\"blast\")\n\n(***************************************************************************)\n(* Similar theorems about suffixes. *)\n(***************************************************************************)\n\nTHEOREM IsSuffixProperties ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S)\n PROVE /\\ IsSuffix(s,t) <=> \\E u \\in Seq(S) : t = u \\o s\n /\\ IsSuffix(s,t) <=> Len(s) <= Len(t) /\\ s = SubSeq(t, Len(t)-Len(s)+1, Len(t))\n<1>. Reverse(s) \\in Seq(S) /\\ Reverse(t) \\in Seq(S)\n BY DEF Reverse\n<1>1. IsSuffix(s,t) <=> \\E u \\in Seq(S) : t = u \\o s\n <2>1. ASSUME IsSuffix(s,t)\n PROVE \\E u \\in Seq(S) : t = u \\o s\n <3>1. PICK u \\in Seq(S) : Reverse(t) = Reverse(s) \\o u\n BY <2>1, IsPrefixProperties, Isa DEF IsSuffix\n <3>2. Reverse(u) \\in Seq(S)\n BY DEF Reverse\n <3>3. Reverse(Reverse(t)) = Reverse(u) \\o Reverse(Reverse(s))\n BY <3>1, ReverseConcat\n <3>4. t = Reverse(u) \\o s\n BY <3>3, ReverseProperties\n <3>. QED BY <3>2, <3>4 DEF Reverse\n <2>2. ASSUME NEW u \\in Seq(S), t = u \\o s\n PROVE IsSuffix(s,t)\n <3>. Reverse(u) \\in Seq(S)\n BY ReverseProperties\n <3>1. Reverse(t) = Reverse(s) \\o Reverse(u)\n BY <2>2, ReverseConcat\n <3>2. IsPrefix(Reverse(s), Reverse(t))\n BY <3>1, IsPrefixProperties, Zenon\n <3>. QED BY <3>2 DEF IsSuffix\n <2>. QED BY <2>1, <2>2, Zenon\n<1>2. IsSuffix(s,t) <=> Len(s) <= Len(t) /\\ s = SubSeq(t, Len(t)-Len(s)+1, Len(t))\n <2>1. ASSUME IsSuffix(s,t)\n PROVE Len(s) <= Len(t) /\\ s = SubSeq(t, Len(t)-Len(s)+1, Len(t))\n <3>1. PICK u \\in Seq(S) : t = u \\o s\n BY <1>1, <2>1\n <3>. QED BY <3>1\n <2>2. ASSUME Len(s) <= Len(t), s = SubSeq(t, Len(t)-Len(s)+1, Len(t))\n PROVE IsSuffix(s,t)\n <3>1. t = SubSeq(t, 1, Len(t) - Len(s)) \\o s\n BY <2>2\n <3>2. SubSeq(t, 1, Len(t) - Len(s)) \\in Seq(S)\n OBVIOUS\n <3>. QED BY <3>1, <3>2, <1>1, Zenon\n <2>. QED BY <2>1, <2>2\n<1>. QED BY <1>1, <1>2, Zenon\n\nTHEOREM IsStrictSuffixProperties ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S)\n PROVE /\\ IsStrictSuffix(s,t) <=> \\E u \\in Seq(S) : u # << >> /\\ t = u \\o s\n /\\ IsStrictSuffix(s,t) <=> Len(s) < Len(t) /\\ IsSuffix(s,t)\n /\\ IsStrictSuffix(s,t) <=> Len(s) < Len(t) /\\ s = SubSeq(t, Len(t)-Len(s)+1, Len(t))\n /\\ IsStrictSuffix(s,t) <=> IsStrictPrefix(Reverse(s), Reverse(t))\n<1>1. IsStrictSuffix(s,t) <=> IsStrictPrefix(Reverse(s), Reverse(t))\n BY ReverseEqual DEF IsStrictSuffix, IsSuffix, IsStrictPrefix\n<1>2. IsStrictSuffix(s,t) => Len(s) < Len(t)\n BY <1>1, IsStrictPrefixProperties, ReverseProperties, Zenon\n<1>3. IsStrictSuffix(s,t) <=> \\E u \\in Seq(S) : u # << >> /\\ t = u \\o s\n <2>1. ASSUME IsStrictSuffix(s,t)\n PROVE \\E u \\in Seq(S) : u # << >> /\\ t = u \\o s\n <3>1. PICK u \\in Seq(S) : t = u \\o s\n BY <2>1, IsSuffixProperties DEF IsStrictSuffix\n <3>2. u # << >>\n BY <2>1, <3>1 DEF IsStrictSuffix\n <3>. QED BY <3>1, <3>2\n <2>2. ASSUME NEW u \\in Seq(S), u # << >> /\\ t = u \\o s\n PROVE IsStrictSuffix(s,t)\n <3>1. IsSuffix(s,t)\n BY <2>2, IsSuffixProperties, Zenon\n <3>2. s # t\n BY <2>2\n <3>. QED BY <3>1, <3>2 DEF IsStrictSuffix\n <2>. QED BY <2>1, <2>2, Zenon\n<1>4. IsStrictSuffix(s,t) <=> Len(s) < Len(t) /\\ IsSuffix(s,t)\n BY <1>2, IsSuffixProperties DEF IsStrictSuffix\n<1>5. IsStrictSuffix(s,t) <=> Len(s) < Len(t) /\\ s = SubSeq(t, Len(t)-Len(s)+1, Len(t))\n BY <1>2, IsSuffixProperties DEF IsStrictSuffix\n<1>. QED BY <1>1, <1>3, <1>4, <1>5, Zenon\n\nTHEOREM IsSuffixElts ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S), NEW i \\in 1 .. Len(s),\n IsSuffix(s,t)\n PROVE s[i] = t[Len(t) - Len(s) + i]\nBY IsSuffixProperties\n\nTHEOREM EmptyIsSuffix ==\n ASSUME NEW S, NEW s \\in Seq(S)\n PROVE /\\ IsSuffix(<<>>, s)\n /\\ IsSuffix(s, <<>>) <=> s = <<>>\n /\\ IsStrictSuffix(<<>>, s) <=> s # <<>>\n /\\ ~ IsStrictSuffix(s, <<>>)\nBY IsSuffixProperties, IsStrictSuffixProperties\n\nTHEOREM IsSuffixConcat ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S)\n PROVE IsSuffix(s, t \\o s)\nBY IsSuffixProperties, ConcatProperties, Zenon\n\nTHEOREM IsStrictSuffixCons ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW e \\in S\n PROVE IsStrictSuffix(s, Cons(e,s))\n<1>. /\\ <<e>> \\in Seq(S)\n /\\ <<e>> \\o s \\in Seq(S)\n OBVIOUS\n<1>. QED BY IsStrictSuffixProperties, Zenon DEF Cons\n\nTHEOREM TailIsSuffix ==\n ASSUME NEW S, NEW s \\in Seq(S), s # << >>\n PROVE IsStrictSuffix(Tail(s), s)\nBY ConsHeadTail, IsStrictSuffixCons, Head(s) \\in S, Tail(s) \\in Seq(S), Zenon\n\nTHEOREM IsSuffixPartialOrder ==\n ASSUME NEW S\n PROVE /\\ \\A s \\in Seq(S) : IsSuffix(s,s)\n /\\ \\A s,t \\in Seq(S) : IsSuffix(s,t) /\\ IsSuffix(t,s) => s = t\n /\\ \\A s,t,u \\in Seq(S) : IsSuffix(s,t) /\\ IsSuffix(t,u) => IsSuffix(s,u)\n<1>1. ASSUME NEW s \\in Seq(S) PROVE IsSuffix(s,s)\n BY s = s \\o <<>>, IsSuffixProperties\n<1>2. ASSUME NEW s \\in Seq(S), NEW t \\in Seq(S), IsSuffix(s,t), IsSuffix(t,s)\n PROVE s = t\n BY <1>2, IsPrefixPartialOrder, ReverseProperties, ReverseEqual DEF IsSuffix\n<1>3. ASSUME NEW s \\in Seq(S), NEW t \\in Seq(S), NEW u \\in Seq(S),\n IsSuffix(s,t), IsSuffix(t,u)\n PROVE IsSuffix(s,u)\n BY <1>3, IsPrefixPartialOrder, ReverseProperties DEF IsSuffix\n<1>. QED BY <1>1, <1>2, <1>3\n\nTHEOREM ConcatIsSuffix ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S), NEW u \\in Seq(S),\n IsSuffix(s \\o t, u)\n PROVE IsSuffix(t, u)\n<1>1. /\\ s \\o t \\in Seq(S)\n /\\ IsSuffix(t, s \\o t)\n BY IsSuffixConcat\n<1>. QED BY <1>1, IsSuffixPartialOrder\n\nTHEOREM ConcatIsSuffixCancel ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S), NEW u \\in Seq(S)\n PROVE IsSuffix(s \\o t, u \\o t) <=> IsSuffix(s, u)\n<1>1. ASSUME IsSuffix(s, u) PROVE IsSuffix(s \\o t, u \\o t)\n <2>1. PICK v \\in Seq(S) : u = v \\o s BY <1>1, IsSuffixProperties\n <2>2. u \\o t = v \\o (s \\o t) BY <2>1\n <2>. QED BY s \\o t \\in Seq(S), u \\o t \\in Seq(S), <2>2, IsSuffixProperties\n<1>2. ASSUME IsSuffix(s \\o t, u \\o t) PROVE IsSuffix(s, u)\n <2>1. PICK v \\in Seq(S) : u \\o t = v \\o (s \\o t)\n BY <1>2, s \\o t \\in Seq(S), u \\o t \\in Seq(S), IsSuffixProperties\n <2>2. u \\o t = (v \\o s) \\o t\n BY <2>1\n <2>3. u = v \\o s\n BY <2>2, ConcatSimplifications(*!cancelRight*), v \\o s \\in Seq(S)\n <2>. QED BY <2>3, IsSuffixProperties\n<1>. QED BY <1>1, <1>2\n\nTHEOREM AppendIsSuffixCancel ==\n ASSUME NEW S, NEW e \\in S, NEW s \\in Seq(S), NEW t \\in Seq(S)\n PROVE IsSuffix(Append(s,e), Append(t,e)) <=> IsSuffix(s,t)\n<1>1. IsSuffix(Append(s,e), Append(t,e)) <=> IsSuffix(s \\o <<e>>, t \\o <<e>>)\n BY AppendIsConcat\n<1>2. @ <=> IsSuffix(s,t)\n BY <<e>> \\in Seq(S), ConcatIsSuffixCancel, Zenon\n<1>. QED BY <1>1, <1>2\n\nTHEOREM AppendIsSuffix ==\n ASSUME NEW S, NEW e \\in S, NEW s \\in Seq(S), NEW u \\in Seq(S),\n IsSuffix(Append(s,e), u)\n PROVE /\\ e = Last(u)\n /\\ IsSuffix(s, Front(u))\n<1>. <<e>> \\in Seq(S)\n OBVIOUS\n<1>1. IsSuffix(<<e>>, u)\n BY ConcatIsSuffix, AppendIsConcat, Zenon\n<1>2. PICK v \\in Seq(S) : u = Append(v,e)\n BY <1>1, IsSuffixProperties, AppendIsConcat\n<1>3. /\\ e = Last(u)\n /\\ v = Front(u)\n /\\ IsSuffix(Append(s,e), Append(Front(u),e))\n BY <1>2, FrontLastAppend\n<1>. QED\n BY <1>3, AppendIsSuffixCancel, Zenon\n\nTHEOREM IsStrictSuffixStrictPartialOrder ==\n ASSUME NEW S\n PROVE /\\ \\A s \\in Seq(S) : ~ IsStrictSuffix(s,s)\n /\\ \\A s,t \\in Seq(S) : IsStrictSuffix(s,t) => ~ IsStrictSuffix(t,s)\n /\\ \\A s,t,u \\in Seq(S) : IsStrictSuffix(s,t) /\\ IsStrictSuffix(t,u) => IsStrictSuffix(s,u)\n<1>1. ASSUME NEW s \\in Seq(S)\n PROVE ~ IsStrictSuffix(s,s)\n BY <1>1, IsStrictSuffixProperties\n<1>2. ASSUME NEW s \\in Seq(S), NEW t \\in Seq(S), IsStrictSuffix(s,t), IsStrictSuffix(t,s)\n PROVE FALSE\n BY <1>2, IsStrictSuffixProperties\n<1>3. ASSUME NEW s \\in Seq(S), NEW t \\in Seq(S), NEW u \\in Seq(S),\n IsStrictSuffix(s,t), IsStrictSuffix(t,u)\n PROVE IsStrictSuffix(s,u)\n <2>1. PICK st \\in Seq(S) \\ {<<>>} : t = st \\o s\n BY <1>3, IsStrictSuffixProperties\n <2>2. PICK tu \\in Seq(S) \\ {<<>>} : u = tu \\o t\n BY <1>3, IsStrictSuffixProperties\n <2>3. u = (tu \\o st) \\o s\n BY <2>1, <2>2\n <2>. QED BY <2>3, tu \\o st # <<>>, IsStrictSuffixProperties\n<1> QED BY <1>1, <1>2, <1>3\n\nTHEOREM IsStrictSuffixWellFounded ==\n ASSUME NEW S\n PROVE IsWellFoundedOn(OpToRel(IsStrictSuffix, Seq(S)), Seq(S))\n<1>1. IsWellFoundedOn(PreImage(Len, Seq(S), OpToRel(<, Nat)), Seq(S))\n BY NatLessThanWellFounded, PreImageWellFounded, IsaM(\"blast\")\n<1>2. OpToRel(IsStrictSuffix, Seq(S)) \\subseteq PreImage(Len, Seq(S), OpToRel(<, Nat))\n BY IsStrictSuffixProperties DEF PreImage, OpToRel\n<1>. QED\n BY <1>1, <1>2, IsWellFoundedOnSubrelation, Zenon\n\nTHEOREM SeqStrictSuffixInduction ==\n ASSUME NEW P(_), NEW S,\n \\A t \\in Seq(S) : (\\A s \\in Seq(S) : IsStrictSuffix(s,t) => P(s)) => P(t)\n PROVE \\A s \\in Seq(S) : P(s)\n<1>1. \\A t \\in Seq(S) :\n (\\A s \\in SetLessThan(t, OpToRel(IsStrictSuffix, Seq(S)), Seq(S)) : P(s))\n => P(t)\n BY Zenon DEF SetLessThan, OpToRel\n<1>. QED BY WFInduction, IsStrictSuffixWellFounded, <1>1, IsaM(\"blast\")\n\n(***************************************************************************)\n(* Since the (strict) prefix and suffix orderings on sequences are *)\n(* well-founded, they can be used for defining recursive functions. *)\n(* The operators OpDefinesFcn, WFInductiveDefines, and WFInductiveUnique *)\n(* are defined in module WellFoundedInduction. *)\n(***************************************************************************)\n\nStrictPrefixesDetermineDef(S, Def(_,_)) ==\n \\A g,h : \\A seq \\in Seq(S) :\n (\\A pre \\in Seq(S) : IsStrictPrefix(pre,seq) => g[pre] = h[pre])\n => Def(g, seq) = Def(h, seq)\n\nLEMMA StrictPrefixesDetermineDef_WFDefOn ==\n ASSUME NEW S, NEW Def(_,_), StrictPrefixesDetermineDef(S, Def)\n PROVE WFDefOn(OpToRel(IsStrictPrefix, Seq(S)), Seq(S), Def)\nBY Isa DEF StrictPrefixesDetermineDef, WFDefOn, OpToRel, SetLessThan\n\nTHEOREM PrefixRecursiveSequenceFunctionUnique ==\n ASSUME NEW S, NEW Def(_,_), StrictPrefixesDetermineDef(S, Def)\n PROVE WFInductiveUnique(Seq(S), Def)\nBY StrictPrefixesDetermineDef_WFDefOn, IsStrictPrefixWellFounded, WFDefOnUnique, Zenon\n\nTHEOREM PrefixRecursiveSequenceFunctionDef ==\n ASSUME NEW S, NEW Def(_,_), NEW f,\n StrictPrefixesDetermineDef(S, Def),\n OpDefinesFcn(f, Seq(S), Def)\n PROVE WFInductiveDefines(f, Seq(S), Def)\nBY StrictPrefixesDetermineDef_WFDefOn, IsStrictPrefixWellFounded, WFInductiveDef, Zenon\n\nTHEOREM PrefixRecursiveSequenceFunctionType ==\n ASSUME NEW S, NEW T, NEW Def(_,_), NEW f,\n T # {},\n StrictPrefixesDetermineDef(S, Def),\n WFInductiveDefines(f, Seq(S), Def),\n \\A g \\in [Seq(S) -> T], s \\in Seq(S) : Def(g,s) \\in T\n PROVE f \\in [Seq(S) -> T]\n<1>1. IsWellFoundedOn(OpToRel(IsStrictPrefix, Seq(S)), Seq(S))\n BY IsStrictPrefixWellFounded, Zenon\n<1>2. WFDefOn(OpToRel(IsStrictPrefix, Seq(S)), Seq(S), Def)\n BY StrictPrefixesDetermineDef_WFDefOn, Zenon\n<1>. QED\n BY <1>1, <1>2, WFInductiveDefType, Isa\n\nStrictSuffixesDetermineDef(S, Def(_,_)) ==\n \\A g,h : \\A seq \\in Seq(S) :\n (\\A suf \\in Seq(S) : IsStrictSuffix(suf,seq) => g[suf] = h[suf])\n => Def(g, seq) = Def(h, seq)\n\nLEMMA StrictSuffixesDetermineDef_WFDefOn ==\n ASSUME NEW S, NEW Def(_,_), StrictSuffixesDetermineDef(S, Def)\n PROVE WFDefOn(OpToRel(IsStrictSuffix, Seq(S)), Seq(S), Def)\nBY Isa DEF StrictSuffixesDetermineDef, WFDefOn, OpToRel, SetLessThan\n\nTHEOREM SuffixRecursiveSequenceFunctionUnique ==\n ASSUME NEW S, NEW Def(_,_), StrictSuffixesDetermineDef(S, Def)\n PROVE WFInductiveUnique(Seq(S), Def)\nBY StrictSuffixesDetermineDef_WFDefOn, IsStrictSuffixWellFounded, WFDefOnUnique, Zenon\n\nTHEOREM SuffixRecursiveSequenceFunctionDef ==\n ASSUME NEW S, NEW Def(_,_), NEW f,\n StrictSuffixesDetermineDef(S, Def),\n OpDefinesFcn(f, Seq(S), Def)\n PROVE WFInductiveDefines(f, Seq(S), Def)\nBY StrictSuffixesDetermineDef_WFDefOn, IsStrictSuffixWellFounded, WFInductiveDef, Zenon\n\nTHEOREM SuffixRecursiveSequenceFunctionType ==\n ASSUME NEW S, NEW T, NEW Def(_,_), NEW f,\n T # {},\n StrictSuffixesDetermineDef(S, Def),\n WFInductiveDefines(f, Seq(S), Def),\n \\A g \\in [Seq(S) -> T], s \\in Seq(S) : Def(g,s) \\in T\n PROVE f \\in [Seq(S) -> T]\n<1>1. IsWellFoundedOn(OpToRel(IsStrictSuffix, Seq(S)), Seq(S))\n BY IsStrictSuffixWellFounded, Zenon\n<1>2. WFDefOn(OpToRel(IsStrictSuffix, Seq(S)), Seq(S), Def)\n BY StrictSuffixesDetermineDef_WFDefOn, Zenon\n<1>. QED\n BY <1>1, <1>2, WFInductiveDefType, Isa\n\n(***************************************************************************)\n(* The following theorems justify ``primitive recursive'' functions over *)\n(* sequences, with a base case for the empty sequence and recursion along *)\n(* either the Tail or the Front of a non-empty sequence. *)\n(***************************************************************************)\n\nTailInductiveDefHypothesis(f, S, f0, Def(_,_)) ==\n f = CHOOSE g : g = [s \\in Seq(S) |-> IF s = <<>> THEN f0 ELSE Def(g[Tail(s)], s)]\n\nTailInductiveDefConclusion(f, S, f0, Def(_,_)) ==\n f = [s \\in Seq(S) |-> IF s = <<>> THEN f0 ELSE Def(f[Tail(s)], s)]\n\nTHEOREM TailInductiveDef ==\n ASSUME NEW S, NEW Def(_,_), NEW f, NEW f0,\n TailInductiveDefHypothesis(f, S, f0, Def)\n PROVE TailInductiveDefConclusion(f, S, f0, Def)\n<1>. DEFINE Op(h,s) == IF s = <<>> THEN f0 ELSE Def(h[Tail(s)], s)\n<1>1. StrictSuffixesDetermineDef(S, Op)\n <2>. SUFFICES ASSUME NEW g, NEW h, NEW seq \\in Seq(S),\n \\A suf \\in Seq(S) : IsStrictSuffix(suf, seq) => g[suf] = h[suf]\n PROVE Op(g, seq) = Op(h, seq)\n BY Zenon DEF StrictSuffixesDetermineDef\n <2>1. CASE seq = <<>>\n BY <2>1\n <2>2. CASE seq # <<>>\n BY <2>2, TailIsSuffix, Tail(seq) \\in Seq(S), Zenon\n <2>. QED BY <2>1, <2>2, Zenon\n<1>2. OpDefinesFcn(f, Seq(S), Op)\n BY Zenon DEF OpDefinesFcn, TailInductiveDefHypothesis\n<1>3. WFInductiveDefines(f, Seq(S), Op)\n BY <1>1, <1>2, SuffixRecursiveSequenceFunctionDef, Zenon\n<1>. QED BY <1>3, Zenon DEF WFInductiveDefines, TailInductiveDefConclusion\n\nTHEOREM TailInductiveDefType ==\n ASSUME NEW S, NEW Def(_,_), NEW f, NEW f0, NEW T,\n TailInductiveDefConclusion(f, S, f0, Def),\n f0 \\in T,\n \\A v \\in T, s \\in Seq(S) : s # <<>> => Def(v,s) \\in T\n PROVE f \\in [Seq(S) -> T]\n<1>. SUFFICES \\A s \\in Seq(S) : f[s] \\in T\n BY DEF TailInductiveDefConclusion\n<1>1. f[<<>>] \\in T\n BY <<>> \\in Seq(S), Zenon DEF TailInductiveDefConclusion\n<1>2. ASSUME NEW seq \\in Seq(S), NEW e \\in S, f[seq] \\in T\n PROVE f[Cons(e, seq)] \\in T\n <2>1. /\\ Cons(e, seq) \\in Seq(S)\n /\\ Cons(e, seq) # <<>>\n /\\ Tail(Cons(e, seq)) = seq\n BY ConsProperties, Zenon\n <2>2. f[Cons(e, seq)] = Def(f[seq], Cons(e,seq))\n BY <2>1, Zenon DEF TailInductiveDefConclusion\n <2>. QED BY <1>2, <2>1, <2>2, Zenon\n<1>. QED BY <1>1, <1>2, SequencesInductionCons, Isa\n\nFrontInductiveDefHypothesis(f, S, f0, Def(_,_)) ==\n f = CHOOSE g : g = [s \\in Seq(S) |-> IF s = <<>> THEN f0 ELSE Def(g[Front(s)], s)]\n\nFrontInductiveDefConclusion(f, S, f0, Def(_,_)) ==\n f = [s \\in Seq(S) |-> IF s = <<>> THEN f0 ELSE Def(f[Front(s)], s)]\n\nTHEOREM FrontInductiveDef ==\n ASSUME NEW S, NEW Def(_,_), NEW f, NEW f0,\n FrontInductiveDefHypothesis(f, S, f0, Def)\n PROVE FrontInductiveDefConclusion(f, S, f0, Def)\n<1>. DEFINE Op(h,s) == IF s = <<>> THEN f0 ELSE Def(h[Front(s)], s)\n<1>1. StrictPrefixesDetermineDef(S, Op)\n <2>. SUFFICES ASSUME NEW g, NEW h, NEW seq \\in Seq(S),\n \\A pre \\in Seq(S) : IsStrictPrefix(pre, seq) => g[pre] = h[pre]\n PROVE Op(g, seq) = Op(h, seq)\n BY Zenon DEF StrictPrefixesDetermineDef, Zenon\n <2>1. CASE seq = <<>>\n BY <2>1\n <2>2. CASE seq # <<>>\n BY <2>2, FrontIsPrefix, FrontProperties\n <2>. QED BY <2>1, <2>2, Zenon\n<1>2. OpDefinesFcn(f, Seq(S), Op)\n BY Zenon DEF OpDefinesFcn, FrontInductiveDefHypothesis\n<1>3. WFInductiveDefines(f, Seq(S), Op)\n BY <1>1, <1>2, PrefixRecursiveSequenceFunctionDef, Zenon\n<1>. QED BY <1>3, Zenon DEF WFInductiveDefines, FrontInductiveDefConclusion\n\nTHEOREM FrontInductiveDefType ==\n ASSUME NEW S, NEW Def(_,_), NEW f, NEW f0, NEW T,\n FrontInductiveDefConclusion(f, S, f0, Def),\n f0 \\in T,\n \\A v \\in T, s \\in Seq(S) : s # <<>> => Def(v,s) \\in T\n PROVE f \\in [Seq(S) -> T]\n<1>. SUFFICES \\A s \\in Seq(S) : f[s] \\in T\n BY DEF FrontInductiveDefConclusion\n<1>1. f[<<>>] \\in T\n BY <<>> \\in Seq(S), Zenon DEF FrontInductiveDefConclusion\n<1>2. ASSUME NEW seq \\in Seq(S), NEW e \\in S, f[seq] \\in T\n PROVE f[Append(seq, e)] \\in T\n <2>1. /\\ Append(seq, e) \\in Seq(S)\n /\\ Append(seq, e) # <<>>\n /\\ Front(Append(seq, e)) = seq\n BY AppendProperties, FrontLastAppend\n <2>2. f[Append(seq, e)] = Def(f[seq], Append(seq, e))\n BY <2>1, Zenon DEF FrontInductiveDefConclusion\n <2>. QED BY <1>2, <2>1, <2>2, Zenon\n<1>. QED BY <1>1, <1>2, SequencesInductionAppend, Isa\n\n(**************************************************************************)\n(* Recursive characterization of FoldLeft. *)\n(**************************************************************************)\nTHEOREM FoldLeftRecursion ==\n ASSUME NEW op(_,_), NEW base, NEW S, NEW seq \\in Seq(S)\n PROVE FoldLeft(op, base, seq) = \n IF seq = << >> THEN base \n ELSE op(FoldLeft(op, base, Front(seq)), Last(seq))\n<1>1. CASE seq = << >>\n BY <1>1, DOMAIN << >> = {}, MapThenFoldSetEmpty, Zenon DEF FoldLeft\n<1>2. CASE seq # << >>\n <2>. DEFINE f(i) == seq[i] g(i) == Front(seq)[i]\n I == 1 .. Len(seq)-1 choose(T) == Max(T)\n <2>1. ASSUME NEW T \\in SUBSET (DOMAIN seq), T # {}\n PROVE choose(T) \\in T\n BY <2>1, FS_Interval, FS_Subset, MaxIntFinite\n <2>2. /\\ DOMAIN seq = 1 .. Len(seq)\n /\\ 1 <= Len(seq)\n /\\ IsFiniteSet(DOMAIN seq)\n BY <1>2, FS_Interval\n <2>a. /\\ 1 .. Len(seq) # {}\n /\\ choose(DOMAIN seq) = Len(seq)\n /\\ (DOMAIN seq) \\ {choose(DOMAIN seq)} = I\n BY <2>2, 1 \\in 1 .. Len(seq), MaxInterval\n <2>3. FoldLeft(op, base, seq) =\n op(MapThenFoldSet(LAMBDA x,y : op(y,x), base, f, choose, I),\n seq[Len(seq)])\n BY <2>1, <2>2, <2>a, MapThenFoldSetNonempty, Isa DEF FoldLeft\n <2>4. (DOMAIN Front(seq)) = I\n BY <1>2 DEF Front\n <2>5. FoldLeft(op, base, Front(seq)) =\n MapThenFoldSet(LAMBDA x,y : op(y,x), base, g, choose, I)\n BY <2>4 DEF FoldLeft\n <2>6. IsFiniteSet(I)\n BY FS_Interval\n <2>7. \\A T \\in SUBSET I : T # {} => choose(T) \\in T\n BY <2>6, FS_Subset, MaxIntFinite\n <2>8. \\A i \\in I : f(i) = g(i)\n BY <2>7 DEF Front\n <2>. HIDE DEF f, g, choose, I\n <2>. QED BY <1>2, <2>3, <2>5, <2>6, <2>7, <2>8, MapThenFoldSetEqual, Isa DEF Last\n<1>. QED BY <1>1, <1>2\n\n(**************************************************************************)\n(* Left folding the empty sequence yields the base value. *)\n(**************************************************************************)\nTHEOREM FoldLeftEmpty ==\n ASSUME NEW op(_,_), NEW base\n PROVE FoldLeft(op, base, <<>>) = base\nBY (DOMAIN << >>) = {}, MapThenFoldSetEmpty, Zenon DEF FoldLeft\n\n(**************************************************************************)\n(* Left folding a non-empty sequence recurses on the front, then combines *)\n(* the last element of the sequence. *)\n(**************************************************************************)\nTHEOREM FoldLeftNonempty ==\n ASSUME NEW op(_,_), NEW base, NEW S, NEW seq \\in Seq(S), seq # << >>\n PROVE FoldLeft(op, base, seq) = op(FoldLeft(op, base, Front(seq)), Last(seq))\nBY FoldLeftRecursion, Isa\n\n(**************************************************************************)\n(* Interaction of FoldLeft and Append. *)\n(**************************************************************************)\nTHEOREM FoldLeftAppend ==\n ASSUME NEW op(_,_), NEW base, NEW S, NEW seq \\in Seq(S), NEW s \\in S \n PROVE FoldLeft(op, base, Append(seq,s)) = op(FoldLeft(op, base, seq), s)\n<1>. DEFINE app == Append(seq, s)\n<1>1. /\\ app \\in Seq(S)\n /\\ app # << >>\n /\\ Front(app) = seq \n /\\ Last(app) = s \n BY DEF Front, Last \n<1>. HIDE DEF app \n<1>2. FoldLeft(op, base, app) = op(FoldLeft(op, base, seq), s)\n BY <1>1, FoldLeftNonempty, Isa\n<1>. QED BY <1>2 DEF app\n\n(**************************************************************************)\n(* Type of FoldLeft. *)\n(**************************************************************************)\nTHEOREM FoldLeftType ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ, NEW seq \\in Seq(Typ),\n \\A t,u \\in Typ : op(t,u) \\in Typ \n PROVE FoldLeft(op, base, seq) \\in Typ \n<1>. DEFINE P(sq) == FoldLeft(op, base, sq) \\in Typ \n<1>1. P(<< >>)\n BY FoldLeftEmpty, Zenon\n<1>2. \\A sq \\in Seq(Typ), e \\in Typ : P(sq) => P(Append(sq, e))\n BY FoldLeftAppend, Isa\n<1>. HIDE DEF P \n<1>3. \\A sq \\in Seq(Typ) : P(seq)\n BY <1>1, <1>2, SequencesInductionAppend, IsaM(\"blast\")\n<1>. QED BY <1>3 DEF P \n\n(**************************************************************************)\n(* Interaction of FoldLeft and concatenation when op is associative and *)\n(* base is a right identity. *)\n(**************************************************************************)\nTHEOREM FoldLeftConcat ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ, \n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u), v),\n \\A t \\in Typ : op(t, base) = t,\n NEW ls \\in Seq(Typ), NEW rs \\in Seq(Typ)\n PROVE FoldLeft(op, base, ls \\o rs) = \n op(FoldLeft(op, base, ls), FoldLeft(op, base, rs))\n<1>. DEFINE P(r) == \\A s \\in Seq(Typ) :\n FoldLeft(op, base, s \\o r) =\n op(FoldLeft(op, base, s), FoldLeft(op, base, r))\n<1>1. \\A s \\in Seq(Typ) : FoldLeft(op, base, s) \\in Typ\n BY FoldLeftType, Isa\n<1>2. P(<< >>)\n <2>. TAKE s \\in Seq(Typ)\n <2>. QED \n BY <1>1, FoldLeftEmpty, s \\o << >> = s, Isa\n<1>3. ASSUME NEW r \\in Seq(Typ), NEW e \\in Typ, P(r)\n PROVE P(Append(r,e))\n <2>. TAKE s \\in Seq(Typ)\n <2>1. FoldLeft(op, base, s \\o Append(r,e)) = \n FoldLeft(op, base, Append(s \\o r, e))\n OBVIOUS\n <2>2. @ = op(FoldLeft(op, base, s \\o r), e)\n BY FoldLeftAppend, s \\o r \\in Seq(Typ), Isa\n <2>3. @ = op(op(FoldLeft(op, base, s), FoldLeft(op, base, r)), e)\n BY <1>3\n <2>4. @ = op(FoldLeft(op, base, s), op(FoldLeft(op, base, r), e))\n BY <1>1\n <2>5. op(FoldLeft(op, base, r), e) = FoldLeft(op, base, Append(r, e))\n BY FoldLeftAppend, Isa\n <2>. QED BY <2>1, <2>2, <2>3, <2>4, <2>5\n<1>4. \\A s \\in Seq(Typ) : P(s)\n <2>. HIDE DEF P \n <2>. QED BY <1>2, <1>3, SequencesInductionAppend, Isa\n<1>. QED BY <1>4\n\n(**************************************************************************)\n(* Recursive characterization of FoldRight. *)\n(**************************************************************************)\nTHEOREM FoldRightRecursion ==\n ASSUME NEW op(_,_), NEW base, NEW S, NEW seq \\in Seq(S)\n PROVE FoldRight(op, seq, base) = \n IF seq = << >> THEN base \n ELSE op(Head(seq), FoldRight(op, Tail(seq), base))\n<1>1. CASE seq = << >>\n BY <1>1, DOMAIN << >> = {}, MapThenFoldSetEmpty, Zenon DEF FoldRight\n<1>2. CASE seq # << >>\n <2>. DEFINE f(i) == seq[i] g(i) == Tail(seq)[i]\n I == 2 .. Len(seq) J == 1 .. Len(seq)-1\n choose(T) == Min(T) bij(j) == j+1\n <2>1. ASSUME NEW T \\in SUBSET (DOMAIN seq), T # {}\n PROVE choose(T) \\in T\n BY <2>1, FS_Interval, FS_Subset, MinIntFinite\n <2>2. /\\ DOMAIN seq = 1 .. Len(seq)\n /\\ 1 <= Len(seq)\n /\\ IsFiniteSet(DOMAIN seq)\n BY <1>2, FS_Interval\n <2>a. /\\ 1 .. Len(seq) # {}\n /\\ choose(DOMAIN seq) = 1\n /\\ (DOMAIN seq) \\ {choose(DOMAIN seq)} = I\n BY <2>2, 1 \\in 1 .. Len(seq), MinInterval\n <2>3. FoldRight(op, seq, base) =\n op(seq[1], MapThenFoldSet(op, base, f, choose, I))\n BY <2>1, <2>2, <2>a, MapThenFoldSetNonempty, Isa DEF FoldRight\n <2>4. (DOMAIN Tail(seq)) = J\n BY <1>2\n <2>5. FoldRight(op, Tail(seq), base) =\n MapThenFoldSet(op, base, g, choose, J)\n BY <2>4 DEF FoldRight \n <2>6. /\\ IsFiniteSet(J)\n /\\ IsFiniteSet(I)\n BY FS_Interval\n <2>7. /\\ \\A T \\in SUBSET J : T # {} => choose(T) \\in T\n /\\ \\A T \\in SUBSET I : T # {} => choose(T) \\in T \n BY <2>6, FS_Subset, MinIntFinite\n <2>8. /\\ \\A j \\in J : bij(j) \\in I \n /\\ \\A j1, j2 \\in J : bij(j1) = bij(j2) => j1 = j2\n /\\ \\A j \\in J : g(j) = f(bij(j))\n BY <1>2\n <2>9. ASSUME NEW i \\in I\n PROVE \\E j \\in J : bij(j) = i\n BY <1>2, i-1 \\in J\n <2>10. ASSUME NEW U \\in SUBSET J, U # {}\n PROVE bij(choose(U)) = choose({bij(j) : j \\in U})\n <3>. DEFINE BU == {bij(j) : j \\in U}\n <3>1. /\\ choose(U) \\in U \n /\\ \\A u \\in U : choose(U) <= u \n BY <2>6, <2>10, FS_Subset, MinIntFinite\n <3>2. /\\ bij(choose(U)) \\in BU\n /\\ \\A u \\in BU : bij(choose(U)) <= u \n BY <3>1\n <3>. QED BY <3>2, MinInt\n <2>. HIDE DEF f, g, choose, I, J, bij\n <2>11. MapThenFoldSet(op, base, g, choose, J) =\n MapThenFoldSet(op, base, f, choose, I)\n BY <2>6, <2>7, <2>8, <2>9, <2>10, MapThenFoldSetBijection, Isa \n <2>. QED BY <1>2, <2>3, <2>5, <2>11\n<1>. QED BY <1>1, <1>2\n\n(**************************************************************************)\n(* Right folding the empty sequence yields the base value. *)\n(**************************************************************************)\nTHEOREM FoldRightEmpty ==\n ASSUME NEW op(_,_), NEW base\n PROVE FoldRight(op, <<>>, base) = base\nBY (DOMAIN << >>) = {}, MapThenFoldSetEmpty, Zenon DEF FoldRight \n\n(**************************************************************************)\n(* Right folding a non-empty sequence combines the head of the sequence *)\n(* with the result of folding the tail. *)\n(**************************************************************************)\nTHEOREM FoldRightNonempty ==\n ASSUME NEW op(_,_), NEW base, NEW S, NEW seq \\in Seq(S), seq # << >>\n PROVE FoldRight(op, seq, base) = op(Head(seq), FoldRight(op, Tail(seq), base))\nBY FoldRightRecursion, Isa\n\n(**************************************************************************)\n(* Interaction of FoldRight and Cons. *)\n(**************************************************************************)\nTHEOREM FoldRightCons ==\n ASSUME NEW op(_,_), NEW base, NEW S, NEW seq \\in Seq(S), NEW s \\in S \n PROVE FoldRight(op, Cons(s, seq), base) = op(s, FoldRight(op, seq, base))\n<1>. DEFINE cns == Cons(s, seq)\n<1>1. /\\ cns \\in Seq(S)\n /\\ cns # << >>\n /\\ Tail(cns) = seq \n /\\ Head(cns) = s \n BY DEF Cons\n<1>. HIDE DEF cns\n<1>2. FoldRight(op, cns, base) = op(s, FoldRight(op, seq, base))\n BY <1>1, FoldRightNonempty, Isa\n<1>. QED BY <1>2 DEF cns\n\n(**************************************************************************)\n(* Type of FoldRight. *)\n(**************************************************************************)\nTHEOREM FoldRightType ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ, NEW seq \\in Seq(Typ),\n \\A t,u \\in Typ : op(t,u) \\in Typ \n PROVE FoldRight(op, seq, base) \\in Typ \n<1>. DEFINE P(sq) == FoldRight(op, sq, base) \\in Typ \n<1>1. P(<< >>)\n BY FoldRightEmpty, Zenon\n<1>2. \\A sq \\in Seq(Typ), e \\in Typ : P(sq) => P(Cons(e, sq))\n BY FoldRightCons, Isa\n<1>. HIDE DEF P \n<1>3. \\A sq \\in Seq(Typ) : P(seq)\n BY <1>1, <1>2, SequencesInductionCons, IsaM(\"blast\")\n<1>. QED BY <1>3 DEF P \n\n(**************************************************************************)\n(* Interaction of FoldRight and concatenation when op is associative and *)\n(* base is a left identity. *)\n(**************************************************************************)\nTHEOREM FoldRightConcat ==\n ASSUME NEW Typ, NEW op(_,_), NEW base \\in Typ, \n \\A t,u \\in Typ : op(t,u) \\in Typ,\n \\A t,u,v \\in Typ : op(t, op(u,v)) = op(op(t,u), v),\n \\A t \\in Typ : op(base, t) = t,\n NEW ls \\in Seq(Typ), NEW rs \\in Seq(Typ)\n PROVE FoldRight(op, ls \\o rs, base) = \n op(FoldRight(op, ls, base), FoldRight(op, rs, base))\n<1>. DEFINE P(s) == \\A r \\in Seq(Typ) :\n FoldRight(op, s \\o r, base) =\n op(FoldRight(op, s, base), FoldRight(op, r, base))\n<1>1. \\A r \\in Seq(Typ) : FoldRight(op, r, base) \\in Typ\n BY FoldRightType, Isa\n<1>2. P(<< >>)\n <2>. TAKE r \\in Seq(Typ)\n <2>. QED \n BY <1>1, FoldRightEmpty, << >> \\o r = r, Isa\n<1>3. ASSUME NEW s \\in Seq(Typ), NEW e \\in Typ, P(s)\n PROVE P(Cons(e,s))\n <2>. TAKE r \\in Seq(Typ)\n <2>1. FoldRight(op, Cons(e,s) \\o r, base) = \n FoldRight(op, Cons(e, s \\o r), base)\n BY ConsConcat\n <2>2. @ = op(e, FoldRight(op, s \\o r, base))\n BY FoldRightCons, s \\o r \\in Seq(Typ), Isa\n <2>3. @ = op(e, op(FoldRight(op, s, base), FoldRight(op, r, base)))\n BY <1>3\n <2>4. @ = op(op(e, FoldRight(op, s, base)), FoldRight(op, r, base))\n BY <1>1\n <2>5. op(e, FoldRight(op, s, base)) = FoldRight(op, Cons(e,s), base)\n BY FoldRightCons, Isa\n <2>. QED BY <2>1, <2>2, <2>3, <2>4, <2>5\n<1>4. \\A s \\in Seq(Typ) : P(s)\n <2>. HIDE DEF P \n <2>. QED BY <1>2, <1>3, SequencesInductionCons, Isa\n<1>. QED BY <1>4\n\n(**************************************************************************)\n(* FoldLeftDomain and FoldRightDomain cannot be characterized recursively *)\n(* in terms of the same operators, we reduce them to MapThenFoldSet. *)\n(**************************************************************************)\nTHEOREM FoldLeftDomainIsMFS ==\n ASSUME NEW op(_,_), NEW base, NEW S, NEW seq \\in Seq(S)\n PROVE FoldLeftDomain(op, base, seq) =\n MapThenFoldSet(LAMBDA x,y : op(y,x), base, \n LAMBDA i : i, LAMBDA T : Max(T), 1 .. Len(seq))\n<1>. DEFINE f(i) == [j \\in DOMAIN seq |-> j][i] g(i) == i\n choose(T) == Max(T) rop(x,y) == op(y,x)\n<1>1. /\\ (DOMAIN [i \\in DOMAIN seq |-> i]) = 1 .. Len(seq)\n /\\ \\A i \\in 1 .. Len(seq) : f(i) = g(i)\n OBVIOUS\n<1>2. IsFiniteSet(1 .. Len(seq))\n BY FS_Interval\n<1>3. \\A T \\in SUBSET (1 .. Len(seq)) : T # {} => choose(T) \\in T \n BY <1>2, FS_Subset, MaxIntFinite\n<1>. HIDE DEF f, g\n<1>4. MapThenFoldSet(rop, base, f, choose, 1 .. Len(seq)) =\n MapThenFoldSet(rop, base, g, choose, 1 .. Len(seq))\n BY <1>1, <1>2, <1>3, MapThenFoldSetEqual, Isa \n<1>. QED BY <1>1, <1>4 DEF FoldLeftDomain, FoldLeft, f, g\n\nTHEOREM FoldRightDomainIsMFS ==\n ASSUME NEW op(_,_), NEW base, NEW S, NEW seq \\in Seq(S)\n PROVE FoldRightDomain(op, seq, base) =\n MapThenFoldSet(op, base, \n LAMBDA i : i, LAMBDA T : Min(T), 1 .. Len(seq))\n<1>. DEFINE f(i) == [j \\in DOMAIN seq |-> j][i] g(i) == i\n choose(T) == Min(T)\n<1>1. /\\ (DOMAIN [i \\in DOMAIN seq |-> i]) = 1 .. Len(seq)\n /\\ \\A i \\in 1 .. Len(seq) : f(i) = g(i)\n OBVIOUS\n<1>2. IsFiniteSet(1 .. Len(seq))\n BY FS_Interval\n<1>3. \\A T \\in SUBSET (1 .. Len(seq)) : T # {} => choose(T) \\in T \n BY <1>2, FS_Subset, MinIntFinite\n<1>. HIDE DEF f, g\n<1>4. MapThenFoldSet(op, base, f, choose, 1 .. Len(seq)) =\n MapThenFoldSet(op, base, g, choose, 1 .. Len(seq))\n BY <1>1, <1>2, <1>3, MapThenFoldSetEqual, Isa \n<1>. QED BY <1>1, <1>4 DEF FoldRightDomain, FoldRight, f, g\n\n============================================================================\n", "ShiViz": "------------------------------- MODULE ShiViz -------------------------------\nLOCAL INSTANCE Integers\nLOCAL INSTANCE Json\nLOCAL INSTANCE TLC\n\n\\* Declaring instances local causes definition overrides to be hidden. In the\n\\* case of Toolbox, this causes the definition override of `_TETrace` to be\n\\* invisible. In turn, TLC will then try to evaluate the TLA+ definition of\n\\*\n\\* `_TETrace` as defined in Tooblox.tla:\n\\* Attempted to enumerate S \\ T when S:\n\\* Nat\n\\* is not enumerable.\n\\*\n\\* See: https://github.com/tlaplus/CommunityModules/issues/37\nINSTANCE Toolbox\n\n-----------------------------------------------------------------------------\n\n\\* Host below is different to the action predicate \n\\* CHOOSE p \\in DOMAIN pc : pc[p] # pc'[p]\n\\* since Host evaluates for states n and n - 1\n\\* whereas the CHOOSE evaluates for states n and n + 1.\n\\* This difference is most easily observed by looking\n\\* at off-by-one difference of the initial state.\nLOCAL FnHost ==\n LET host[i \\in DOMAIN _TETrace] ==\n (*************************************************************************)\n (* The pc value that has been modified in (the current) state n compared *)\n (* to the predecessor state n-1. *)\n (*************************************************************************)\n IF i = 1 THEN \"--\"\n ELSE IF _TETrace[i-1].pc = _TETrace[i].pc\n THEN host[i-1] \\* pc variable has not changed.\n ELSE CHOOSE p \\in DOMAIN _TETrace[i-1].pc :\n _TETrace[i-1].pc[p] # _TETrace[i].pc[p]\n IN TLCEval(host)\n\nHost == FnHost[_TEPosition]\n \n-----------------------------------------------------------------------------\n\nLOCAL clock(n) == \n IF n = 1 THEN [p \\in DOMAIN _TETrace[n].pc |-> 0] \\* In the init state, all clocks are 0.\n ELSE [p \\in DOMAIN _TETrace[n].pc |-> IF p = FnHost[n] \n THEN 1\n ELSE 0]\n\nLOCAL sum(F, G) == \n [d \\in DOMAIN F |-> F[d] + G[d]]\n\nLOCAL FnClock == \n LET vclock[i \\in DOMAIN _TETrace] == \n IF i = 1\n THEN clock(i)\n ELSE sum(TLCEval(vclock[i-1]), clock(i))\n IN TLCEval(vclock)\n\nClock ==\n (*************************************************************************)\n (* The pc variable formatted as a Json object as required by ShiViz. *)\n (*************************************************************************)\n ToJsonObject(FnClock[_TEPosition])\n\n=============================================================================\n\\* Modification History\n\\* Last modified Tue Sep 25 17:20:37 PDT 2019 by markus\n\\* Created Tue Aug 27 13:04:16 PDT 2019 by markus\n", "Statistics": "------------------------- MODULE Statistics -------------------------\nEXTENDS Naturals\n\n(***************************************************************************)\n(* Performs a Chi-square goodness of fit test evaluating the null *)\n(* hypothesis that the observed counts conform to the frequency *)\n(* distribution described by the expected counts, with significance level *)\n(* alpha. *)\n(* From: *)\n(* https://commons.apache.org/proper/commons-math/javadocs/api-3.6.1/ *)\n(* org/apache/commons/math3/stat/inference/ChiSquareTest.html *)\n(***************************************************************************)\nChiSquare(expected, actual, alpha) ==\n \\* TODO: specify the ChiSquare test\n TRUE\n\n=========================================================================\n ", "UndirectedGraphs": "------------------------- MODULE UndirectedGraphs ----------------------------\r\n(****************************************************************************)\r\n(* Representation of undirected graphs in TLA+. In contrast to module *)\r\n(* Graphs, edges are represented as unordered pairs {a,b} of nodes, thus *)\r\n(* enforcing symmetry. *)\r\n(* The definitions of the operators SimplePath, AreConnectedIn, and *)\r\n(* ConnectedComponents are overridden by TLC with methods defined in *)\r\n(* tlc2/overrides/UndirectedGraphs.java. *)\r\n(****************************************************************************)\r\nLOCAL INSTANCE Naturals\r\nLOCAL INSTANCE Sequences\r\nLOCAL INSTANCE SequencesExt\r\nLOCAL INSTANCE FiniteSets\r\nLOCAL INSTANCE Folds\r\nLOCAL INSTANCE Functions\r\n\r\nIsUndirectedGraph(G) ==\r\n /\\ G = [node |-> G.node, edge |-> G.edge]\r\n /\\ \\A e \\in G.edge : \\E a,b \\in G.node : e = {a,b}\r\n\r\nIsLoopFreeUndirectedGraph(G) ==\r\n /\\ G = [node |-> G.node, edge |-> G.edge]\r\n /\\ \\A e \\in G.edge : \\E a,b \\in G.node : a # b /\\ e = {a,b}\r\n\r\nUndirectedSubgraph(G) ==\r\n {H \\in [node : SUBSET G.node, edge : SUBSET G.edge] : IsUndirectedGraph(H)}\r\n\r\n-----------------------------------------------------------------------------\r\n(****************************************************************************)\r\n(* A path in a graph is a non-empty sequence of nodes connected by edges. *)\r\n(* A simple path is a path that does not contain duplicate nodes. *)\r\n(* Two nodes m and n are connected if there exists a path from m to n. *)\r\n(* A graph is strongly connected if all of its nodes are connected. *)\r\n(****************************************************************************)\r\nPath(G) == {p \\in Seq(G.node) :\r\n /\\ p # << >>\r\n /\\ \\A i \\in 1..(Len(p)-1) : {p[i], p[i+1]} \\in G.edge}\r\n\r\nSimplePath(G) ==\r\n { p \\in Path(G) : \\A i,j \\in 1..Len(p) : p[i] = p[j] => i = j }\r\n\r\nAreConnectedIn(m, n, G) ==\r\n \\E p \\in Path(G) : (p[1] = m) /\\ (p[Len(p)] = n)\r\n\r\n-----------------------------------------------------------------------------\r\n(****************************************************************************)\r\n(* The (maximal) connected components are the maximal non-empty subsets S *)\r\n(* of nodes such that any two nodes in the set are connected by a path that *)\r\n(* only visits nodes in S. A graph is strongly connected if and only if it *)\r\n(* has precisely one connected component containing all nodes. *)\r\n(****************************************************************************)\r\nConnectedComponents(G) == \r\n LET IsCC(S) == /\\ S # {}\r\n /\\ \\A m,n \\in S : \\E p \\in Seq(S) : \r\n /\\ p # << >> \r\n /\\ p[1] = m /\\ p[Len(p)] = n \r\n /\\ \\A i \\in 1 .. Len(p)-1 : {p[i], p[i+1]} \\in G.edge\r\n IN { S \\in SUBSET G.node : \r\n /\\ IsCC(S)\r\n /\\ \\A T \\in SUBSET G.node : S \\subseteq T /\\ S # T => ~ IsCC(T)\r\n }\r\n\r\n\\* Observe that the possible alternative definition\r\n\\* \"Cardinality(ConnectedComponents(G)) = 1\" makes sense only if the\r\n\\* set of connected components is finite, and this need not be the case\r\n\\* in general.\r\nIsStronglyConnected(G) == ConnectedComponents(G) = {G.node}\r\n\r\n-----------------------------------------------------------------------------\r\n(****************************************************************************)\r\n(* The set of all possible undirected graphs whose node set is S. *)\r\n(* *)\r\n(* Example: *)\r\n(* UndirectedGraphs({1, 2}) = { *)\r\n(* [node |-> {1, 2}, edge |-> {}], *)\r\n(* [node |-> {1, 2}, edge |-> {{1}}], *)\r\n(* [node |-> {1, 2}, edge |-> {{2}}], *)\r\n(* [node |-> {1, 2}, edge |-> {{1,2}}], *)\r\n(* [node |-> {1, 2}, edge |-> {{1}, {1,2}}], *)\r\n(* [node |-> {1, 2}, edge |-> {{2}, {1,2}}], *)\r\n(* [node |-> {1, 2}, edge |-> {{1}, {2}, {1,2}}], *)\r\n(* } *)\r\n(****************************************************************************)\r\nUndirectedGraphs(S) == [node: {S}, edge: SUBSET { {s, t} : <<s,t>> \\in S \\X S }]\r\n=============================================================================\r\n", "VectorClocks": "---------------------------- MODULE VectorClocks -------------------------------\nEXTENDS Naturals, Sequences, Functions\n\nLOCAL concurrent(v1, v2) ==\n \\E i,j \\in DOMAIN v1: i # j /\\ v1[i] < v2[i] /\\ v1[j] > v2[j]\n\nLOCAL happenedBefore(v1, v2) ==\n \\* A vector clock v1 is said to happen before v2 if for all nodes, \n \\* x[n] <= y[n] , and there exists at least one node such that\n \\* x[h] < y[h].\n /\\ \\A i \\in DOMAIN v1: v1[i] <= v2[i]\n /\\ \\E i \\in DOMAIN v1: v1[i] < v2[i]\n\nIsCausalOrder(log, clock(_)) ==\n \\A i \\in 1..Len(log) :\n \\A j \\in 1..(i - 1) :\n \\* Align the vector clocks to the same domain (mapping\n \\* missing values to 0). A node gradually learns about\n \\* other nodes, so its clock domain expands over time.\n LET Fill(c, D) == \n [ d \\in D |-> IF d \\in DOMAIN c THEN c[d] ELSE 0 ]\n D == DOMAIN clock(log[i]) \\cup DOMAIN clock(log[j])\n vci == Fill(clock(log[i]), D)\n vcj == Fill(clock(log[j]), D)\n IN happenedBefore(vcj, vci) \\/ concurrent(vcj, vci)\n\nCausalOrder(log, clock(_), node(_), domain(_)) ==\n (*\n Sort the provided log by the vector clock values indicated on each line\n of the log. This operator cannot accommodate \"hidden\" events, meaning\n events that are excluded from the log. The vector clocks must be\n continuous without any gaps.\n \n The predicates clock, node, and domain equals the vector clock from\n a log entry, the node's clock value, and the clock's domain, i.e., the\n nodes for which the clock has values.\n \n Imagine a log containing lines such as:\n \n [pkt |-> \n [vc |-> \n [1 |-> 20, \n 0 |-> 10,\n 3 |-> 16, \n 7 |-> 21, \n 4 |-> 10, \n 6 |-> 21]],\n node |-> 5,\n ...\n ]\n \n CausalOrder(log, \n LAMBDA line: line.pkt.vc,\n LAMBDA line: line.node,\n LAMBDA vc : DOMAIN vc)\n *)\n CHOOSE newlog \\in \n { f \\in \n [ 1..Len(log) -> Range(log)] : \n Range(f) = Range(log) } : \n IsCausalOrder(newlog, clock)\n\n===============================================================================\n", "RealTime": "----------------------------- MODULE RealTime -------------------------------\n(***************************************************************************)\n(* This standard module is described in Chapter 9 of the book \"Specifying *)\n(* Systems\". The type of specification described there cannot be handled *)\n(* by the TLC model checker. *)\n(***************************************************************************)\nEXTENDS Reals \nVARIABLE now\n\nRTBound(A, v, D, E) == \n LET TNext(t) == t' = IF <<A>>_v \\/ ~(ENABLED <<A>>_v)'\n THEN 0 \n ELSE t + (now'-now)\n\n Timer(t) == (t=0) /\\ [][TNext(t)]_<<t, v, now>>\n\n MaxTime(t) == [](t \\leq E) \n\n MinTime(t) == [][A => t \\geq D]_v \n IN \\EE t : Timer(t) /\\ MaxTime(t) /\\ MinTime(t)\n-----------------------------------------------------------------------------\nRTnow(v) == LET NowNext == /\\ now' \\in {r \\in Real : r > now} \n /\\ UNCHANGED v\n IN /\\ now \\in Real \n /\\ [][NowNext]_now\n /\\ \\A r \\in Real : WF_now(NowNext /\\ (now'>r))\n=============================================================================\n", "Toolbox": "----------------------------- MODULE Toolbox --------------------------------\n(***************************************************************************)\n(* *)\n(***************************************************************************)\nLOCAL INSTANCE Naturals \\* a + b\nLOCAL INSTANCE FiniteSets \\* Cardinality\nLOCAL INSTANCE TLC \\* TLCGet\n\n-----------------------------------------------------------------------------\n\n(* Trace Exploration *)\n\n_TETrace ==\n CHOOSE f \\in [ (Nat \\ {0}) -> STRING ] : TRUE\n\nLOCAL _TETraceLength == Cardinality(DOMAIN _TETrace)\n\n\\* When evaluated as part of a TLC ALIAS expression, `_TEPosition` should be\n\\* redefined as `TLCGet(\"level\"), or replace with `TLCGet(level)` right away.\n\\* The definition below accounts for several \"peculiarities\" of the\n\\* Toolbox's trace exploration.\n_TEPosition == \n IF TLCGet(\"level\") >= _TETraceLength\n THEN _TETraceLength\n ELSE IF TLCGet(\"level\") = 0 THEN 1 ELSE TLCGet(\"level\")\n\n=============================================================================\n", "BagsTheorems": "---------------------------- MODULE BagsTheorems ---------------------------\n(**************************************************************************)\n(* A bag, also called a multiset, is a set that can contain multiple *)\n(* copies of the same element. A bag can have infinitely many elements, *)\n(* but only finitely many copies of any single element. *)\n(* *)\n(* We represent a bag in the usual way as a function whose range is a *)\n(* subset of the positive integers. An element e belongs to bag B iff e *)\n(* is in the domain of B, in which case bag B contains B[e] copies of e. *)\n(**************************************************************************)\nEXTENDS Bags\n\nLOCAL INSTANCE Naturals\n\nCopiesIn(e, B) ==\n (************************************************************************)\n (* If B is a bag, then CopiesIn(e, B) is the number of copies of e in *)\n (* B. If ~BagIn(e, B), then CopiesIn(e, B) = 0. *)\n (************************************************************************)\n IF BagIn(e, B) THEN B[e] ELSE 0\n\nScaling(n, B) ==\n (************************************************************************)\n (* If B is a bag, then Scaling(e, B) is the Bag containing the same *)\n (* elements of B with n times their copies *)\n (************************************************************************)\n IF n>0 THEN [i \\in DOMAIN B |-> n*B[i] ] ELSE EmptyBag\n\n\n(***************************************************************************)\n(* Converts a sequence into a bag *)\n(***************************************************************************)\n\nSeqToBag(seq) == [ x \\in Range(seq) |-> Cardinality({i \\in DOMAIN seq: seq[i]=x}) ]\n\n\n(***************************************************************************)\n(* \\sqsubseteq is a PARTIAL ORDER relattion *)\n(***************************************************************************)\n\n(*Reflexivity*)\nTHEOREM Bags_SqsubsetPO_Reflexivity ==\n ASSUME NEW B, IsABag(B)\n PROVE B \\sqsubseteq B\n\n(*Antisymmetry*)\nTHEOREM Bags_SqsubseteqPO_AntiSymmetry ==\n ASSUME NEW A, NEW B, IsABag(A), IsABag(B),\n A \\sqsubseteq B, B \\sqsubseteq A\n PROVE A = B\n\n(*Transitivity*)\nTHEOREM Bags_SqsubseteqPO_Transitivity ==\n ASSUME NEW A, NEW B, NEW C, IsABag(A), IsABag(B), IsABag(C),\n A \\sqsubseteq B, B \\sqsubseteq C\n PROVE A \\sqsubseteq C\n\n(***************************************************************************)\n(* Lemmas on EmptyBags *)\n(***************************************************************************)\n\nTHEOREM Bags_EmptyBag ==\n ASSUME NEW B, IsABag(B)\n PROVE /\\ IsABag(EmptyBag)\n /\\ B=EmptyBag <=> DOMAIN B ={}\n /\\ DOMAIN EmptyBag ={}\n /\\ EmptyBag \\sqsubseteq B\n /\\ \\A e: ~BagIn(e, EmptyBag)\n\n(***************************************************************************)\n(* Lemmas on Scaling Operator for Bags *)\n(***************************************************************************)\n\nTHEOREM Bags_Scaling ==\n ASSUME NEW B, IsABag(B), NEW n \\in Nat, NEW m \\in Nat\n PROVE /\\ IsABag(Scaling(n, B))\n /\\ Scaling(n, EmptyBag)=EmptyBag\n /\\ Scaling(0, B)=EmptyBag\n /\\ Scaling(1, B)= B\n /\\ Scaling(n*m, B) = Scaling(n, Scaling(m, B))\n /\\ n>0 => DOMAIN(Scaling(n, B)) = DOMAIN B\n\n(***************************************************************************)\n(* SetToBag and BagToSet are inverse of each other *)\n(***************************************************************************)\n\nTHEOREM Bags_Inverse ==\n ASSUME NEW S\n PROVE BagToSet(SetToBag(S))=S\n\nTHEOREM Bags_Inverse1 ==\n ASSUME NEW B, IsABag(B)\n PROVE SetToBag(BagToSet(B)) \\sqsubseteq B\n\n(***************************************************************************)\n(* SetToBag Preserves Equality *)\n(***************************************************************************)\n\nTHEOREM Bags_SetToBagEquality ==\n ASSUME NEW A, NEW B\n PROVE A=B <=> SetToBag(A)=SetToBag(B)\n\n(***************************************************************************)\n(* Union of Bags *)\n(***************************************************************************)\n\nTHEOREM Bags_Union ==\n ASSUME NEW B1, NEW B2, IsABag(B1), IsABag(B2)\n PROVE /\\ IsABag(B1 (+) B2)\n /\\ DOMAIN(B1 (+) B2) = DOMAIN B1 \\cup DOMAIN B2\n /\\ \\A e : CopiesIn(e, B1(+)B2) = CopiesIn(e,B1) + CopiesIn(e,B2)\n\n(***************************************************************************)\n(* Difference of Bags *)\n(***************************************************************************)\n\nTHEOREM Bags_Difference ==\n ASSUME NEW B1, NEW B2, IsABag(B1), IsABag(B2)\n PROVE /\\ IsABag(B1 (-) B2)\n /\\ DOMAIN (B1 (-) B2) =\n {e \\in DOMAIN B1 : e \\notin DOMAIN B2 \\/ B1[e] > B2[e]}\n /\\ \\A e : CopiesIn(e, B1 (-) B2) =\n IF BagIn(e, B1(-)B2) THEN CopiesIn(e,B1) - CopiesIn(e,B2)\n ELSE 0\n\n(***************************************************************************)\n(* Union is commutative. *)\n(***************************************************************************)\n\nTHEOREM Bags_UnionCommutative ==\n ASSUME NEW B1, NEW B2, IsABag(B1), IsABag(B2)\n PROVE B1(+)B2 = B2(+)B1\n\n(***************************************************************************)\n(* Unon is associative. *)\n(***************************************************************************)\nTHEOREM Bags_UnionAssociative ==\n ASSUME NEW B1, NEW B2, NEW B3, IsABag(B1), IsABag(B2), IsABag(B3)\n PROVE (B1(+)B2) (+) B3 = B1 (+) (B2(+)B3)\n\n(***************************************************************************)\n(* Given Bags B1, B2 then B1 \\sqsubseteq B1(+)B2 *)\n(***************************************************************************)\n\nTHEOREM Bags_UnionSqSubset ==\n ASSUME NEW B1, NEW B2, IsABag(B1), IsABag(B2)\n PROVE B1 \\sqsubseteq B1(+)B2\n\n(***************************************************************************)\n(* Scaling is monotonic in the scaling factor. *)\n(***************************************************************************)\n\nTHEOREM Bags_ScalingMonotonic ==\n ASSUME NEW B, IsABag(B), NEW n \\in Nat, NEW m \\in Nat, m <= n\n PROVE Scaling(m, B) \\sqsubseteq Scaling(n, B)\n\n(***************************************************************************)\n(* Given Bags A and B, A(-)B \\sqsubseteq A *)\n(***************************************************************************)\n\nTHEOREM Bags_DifferenceSqsubset ==\n ASSUME NEW A, NEW B, IsABag(A), IsABag(B)\n PROVE A(-)B \\sqsubseteq A\n\n(***************************************************************************)\n(* EmptyBag is the additive identity. *)\n(***************************************************************************)\n\nTHEOREM Bags_EmptyBagOperations ==\n ASSUME NEW B, IsABag(B)\n PROVE /\\ B (+) EmptyBag = B\n /\\ B (-) EmptyBag = B\n\n(***************************************************************************)\n(* SetToBag of a set is a bag. *)\n(***************************************************************************)\n\nTHEOREM Bags_SetToBagIsABag ==\n ASSUME NEW S\n PROVE IsABag(SetToBag(S))\n\n(***************************************************************************)\n(* CopiesIn is monotone w.r.t \\sqsubseteq *)\n(***************************************************************************)\n\nTHEOREM Bags_CopiesInBagsInMonotone ==\n ASSUME NEW B1, NEW B2, NEW e, IsABag(B1), IsABag(B2), B1 \\sqsubseteq B2\n PROVE /\\ BagIn(e, B1) => BagIn(e, B2)\n /\\ CopiesIn(e, B1) <= CopiesIn(e, B2)\n\n(***************************************************************************)\n(* Given Bag B and Natural n, CopiesIn(e, Scaling(n, B))=n*CopiesIn(e, B) *)\n(***************************************************************************)\n\nTHEOREM Bags_CopiesInScaling ==\n ASSUME NEW B, IsABag(B), NEW n \\in Nat, NEW e\n PROVE CopiesIn(e, Scaling(n, B)) = n*CopiesIn(e, B)\n\n(***************************************************************************)\n(* Given set S, CopiesIn(e, SetToBag(S))=IF e \\in B THEN 1 ELSE 0 *)\n(***************************************************************************)\n\nTHEOREM Bags_CopiesInSetToBag ==\n ASSUME NEW B, NEW e\n PROVE CopiesIn(e, SetToBag(B))=IF e \\in B THEN 1 ELSE 0\n\n(***************************************************************************)\n(* Given sequence seq, SeqToBag(seq) is a Bag *)\n(***************************************************************************)\n\nTHEOREM Bags_IsABagSeqToBag ==\n ASSUME NEW S, NEW seq \\in Seq(S)\n PROVE IsABag(SeqToBag(seq))\n\n=============================================================================\n", "BagsTheorems_proofs": "------------------------- MODULE BagsTheorems_proofs -------------------------------\n(**************************************************************************)\n(* A bag, also called a multiset, is a set that can contain multiple *)\n(* copies of the same element. A bag can have infinitely many elements, *)\n(* but only finitely many copies of any single element. *)\n(* *)\n(* We represent a bag in the usual way as a function whose range is a *)\n(* subset of the positive integers. An element e belongs to bag B iff e *)\n(* is in the domain of B, in which case bag B contains B[e] copies of e. *)\n(**************************************************************************)\nEXTENDS Bags,\n FiniteSetTheorems,\n TLAPS\n\nLOCAL INSTANCE Naturals\n\nCopiesIn(e, B) ==\n (************************************************************************)\n (* If B is a bag, then CopiesIn(e, B) is the number of copies of e in *)\n (* B. If ~BagIn(e, B), then CopiesIn(e, B) = 0. *)\n (************************************************************************)\n IF BagIn(e, B) THEN B[e] ELSE 0\n\nScaling(n, B) ==\n (************************************************************************)\n (* If B is a bag, then Scaling(e, B) is the Bag containing the same *)\n (* elements of B with n times their copies *)\n (************************************************************************)\n IF n>0 THEN [i \\in DOMAIN B |-> n*B[i] ] ELSE EmptyBag\n\n\n(***************************************************************************)\n(* Converts a sequence into a bag *)\n(***************************************************************************)\n\nSeqToBag(seq) == [ x \\in Range(seq) |-> Cardinality({i \\in DOMAIN seq: seq[i]=x}) ]\n\n\n(***************************************************************************)\n(* Two bags are equal if they agree on all numbers of copies. *)\n(***************************************************************************)\nTHEOREM BagEquality ==\n ASSUME NEW B1, NEW B2, IsABag(B1), IsABag(B2),\n \\A e : CopiesIn(e, B1) = CopiesIn(e, B2)\n PROVE B1 = B2\n<1>1. \\A e : e \\in DOMAIN B1 <=> e \\in DOMAIN B2\n BY DEF IsABag, CopiesIn, BagIn, BagToSet\n<1>2. DOMAIN B1 = DOMAIN B2\n BY <1>1\n<1>. QED BY <1>2 DEF IsABag, CopiesIn, BagIn, BagToSet\n \n(***************************************************************************)\n(* \\sqsubseteq is a PARTIAL ORDER relattion *)\n(***************************************************************************)\n\n(*Reflexivity*)\nTHEOREM Bags_SqsubsetPO_Reflexivity ==\n ASSUME NEW B, IsABag(B)\n PROVE B \\sqsubseteq B\nBY DEF \\sqsubseteq, IsABag\n\n(*Antisymmetry*)\nTHEOREM Bags_SqsubseteqPO_AntiSymmetry ==\n ASSUME NEW A, NEW B, IsABag(A), IsABag(B),\n A \\sqsubseteq B, B \\sqsubseteq A\n PROVE A = B\nBY DEF IsABag, \\sqsubseteq\n\n(*Transitivity*)\nTHEOREM Bags_SqsubseteqPO_Transitivity ==\n ASSUME NEW A, NEW B, NEW C, IsABag(A), IsABag(B), IsABag(C),\n A \\sqsubseteq B, B \\sqsubseteq C\n PROVE A \\sqsubseteq C\nBY DEF IsABag, \\sqsubseteq\n\n(***************************************************************************)\n(* Lemmas on EmptyBags *)\n(***************************************************************************)\n\nTHEOREM Bags_EmptyBag ==\n ASSUME NEW B, IsABag(B)\n PROVE /\\ IsABag(EmptyBag)\n /\\ B=EmptyBag <=> DOMAIN B ={}\n /\\ DOMAIN EmptyBag ={}\n /\\ EmptyBag \\sqsubseteq B\n /\\ \\A e: ~BagIn(e, EmptyBag)\nBY DEF EmptyBag, SetToBag, IsABag, \\sqsubseteq, BagIn, BagToSet\n\n(***************************************************************************)\n(* Lemmas on Scaling Operator for Bags *)\n(***************************************************************************)\n\nTHEOREM Bags_Scaling ==\n ASSUME NEW B, IsABag(B), NEW n \\in Nat, NEW m \\in Nat\n PROVE /\\ IsABag(Scaling(n, B))\n /\\ Scaling(n, EmptyBag)=EmptyBag\n /\\ Scaling(0, B)=EmptyBag\n /\\ Scaling(1, B)= B\n /\\ Scaling(n*m, B) = Scaling(n, Scaling(m, B))\n /\\ n>0 => DOMAIN(Scaling(n, B)) = DOMAIN B\n<1>1. IsABag(Scaling(n, B))\n BY DEF IsABag, Scaling, EmptyBag, SetToBag\n<1>2. Scaling(n, EmptyBag)=EmptyBag\n BY DEF IsABag, Scaling, EmptyBag, SetToBag\n<1>3. Scaling(0, B)=EmptyBag\n BY DEF Scaling\n<1>4. Scaling(1, B)= B\n BY DEF Scaling, IsABag\n<1>5. Scaling(n*m, B) = Scaling(n, Scaling(m, B))\n <2>1. CASE m>0 /\\ n>0\n <3>. \\A i \\in DOMAIN B : n * m * B[i] = n * (m * B[i])\n BY DEF IsABag\n <3>. QED BY <2>1 DEF Scaling\n <2>2. CASE m=0\n BY <2>2, <1>2, <1>3 DEF Scaling\n <2>3. CASE n=0\n BY <2>3 DEF Scaling\n <2>. QED BY <2>1, <2>2, <2>3\n<1>6. n>0 => DOMAIN Scaling(n, B)=DOMAIN B\n BY DEF Scaling\n<1> QED BY <1>1, <1>2, <1>3, <1>4, <1>5, <1>6\n\n(***************************************************************************)\n(* SetToBag and BagToSet are inverse of each other *)\n(***************************************************************************)\n\nTHEOREM Bags_Inverse ==\n ASSUME NEW S\n PROVE BagToSet(SetToBag(S))=S\nBY DEF SetToBag, BagToSet\n\nTHEOREM Bags_Inverse1 ==\n ASSUME NEW B, IsABag(B)\n PROVE SetToBag(BagToSet(B)) \\sqsubseteq B\nBY DEF SetToBag, BagToSet, \\sqsubseteq, IsABag\n\n(***************************************************************************)\n(* SetToBag Preserves Equality *)\n(***************************************************************************)\n\nTHEOREM Bags_SetToBagEquality ==\n ASSUME NEW A, NEW B\n PROVE A=B <=> SetToBag(A)=SetToBag(B)\nBY DEF SetToBag\n\n(***************************************************************************)\n(* Union of Bags *)\n(***************************************************************************)\n\nTHEOREM Bags_Union ==\n ASSUME NEW B1, NEW B2, IsABag(B1), IsABag(B2)\n PROVE /\\ IsABag(B1 (+) B2)\n /\\ DOMAIN(B1 (+) B2) = DOMAIN B1 \\cup DOMAIN B2\n /\\ \\A e : CopiesIn(e, B1(+)B2) = CopiesIn(e,B1) + CopiesIn(e,B2)\nBY DEF IsABag, (+), CopiesIn, BagIn, BagToSet\n\n(***************************************************************************)\n(* Difference of Bags *)\n(***************************************************************************)\n\nTHEOREM Bags_Difference ==\n ASSUME NEW B1, NEW B2, IsABag(B1), IsABag(B2)\n PROVE /\\ IsABag(B1 (-) B2)\n /\\ DOMAIN (B1 (-) B2) =\n {e \\in DOMAIN B1 : e \\notin DOMAIN B2 \\/ B1[e] > B2[e]}\n /\\ \\A e : CopiesIn(e, B1 (-) B2) =\n IF BagIn(e, B1(-)B2) THEN CopiesIn(e,B1) - CopiesIn(e,B2)\n ELSE 0\n<1>. DEFINE B == [e \\in DOMAIN B1 |-> IF e \\in DOMAIN B2 THEN B1[e] - B2[e]\n ELSE B1[e]]\n D == {e \\in DOMAIN B1 : B[e] > 0}\n R == [e \\in D |-> B[e]]\n<1>1. R = B1 (-) B2\n BY Zenon DEF (-)\n<1>2. \\A e \\in DOMAIN R : R[e] \\in Nat /\\ R[e] > 0\n BY DEF IsABag\n<1>3. DOMAIN R = {e \\in DOMAIN B1 : e \\notin DOMAIN B2 \\/ B1[e] > B2[e]}\n BY DEF IsABag\n<1>4. \\A e : CopiesIn(e, R) =\n IF BagIn(e, R) THEN CopiesIn(e,B1) - CopiesIn(e,B2)\n ELSE 0\n BY DEF IsABag, BagIn, BagToSet, CopiesIn\n<1>. HIDE DEF D\n<1>5. IsABag(R)\n BY <1>2, Zenon DEF IsABag\n<1>. QED BY <1>1, <1>2, <1>3, <1>4, <1>5\n\n(***************************************************************************)\n(* Union is commutative. *)\n(***************************************************************************)\n\nTHEOREM Bags_UnionCommutative ==\n ASSUME NEW B1, NEW B2, IsABag(B1), IsABag(B2)\n PROVE B1(+)B2 = B2(+)B1\n<1>. DEFINE D1 == DOMAIN B1 D2 == DOMAIN B2\n C1(e) == IF e \\in D1 THEN B1[e] ELSE 0\n C2(e) == IF e \\in D2 THEN B2[e] ELSE 0\n<1>1. \\A e : C1(e) \\in Nat /\\ C2(e) \\in Nat\n BY DEF IsABag\n<1>2. /\\ B1 (+) B2 = [e \\in D1 \\union D2 |-> C1(e) + C2(e)]\n /\\ B2 (+) B1 = [e \\in D2 \\union D1 |-> C2(e) + C1(e)]\n BY DEF (+)\n<1>. HIDE DEF D1, D2, C1, C2\n<1>. QED BY <1>1, <1>2\n\n(***************************************************************************)\n(* Unon is associative. *)\n(***************************************************************************)\n\nTHEOREM Bags_UnionAssociative ==\n ASSUME NEW B1, NEW B2, NEW B3, IsABag(B1), IsABag(B2), IsABag(B3)\n PROVE (B1(+)B2) (+) B3 = B1 (+) (B2(+)B3)\n<1>. DEFINE D1 == DOMAIN B1 D2 == DOMAIN B2 D3 == DOMAIN B3\n C1(e) == IF e \\in D1 THEN B1[e] ELSE 0\n C2(e) == IF e \\in D2 THEN B2[e] ELSE 0\n C3(e) == IF e \\in D3 THEN B3[e] ELSE 0\n<1>1. \\A e : C1(e) \\in Nat /\\ C2(e) \\in Nat /\\ C3(e) \\in Nat\n BY DEF IsABag\n<1>2. /\\ (B1 (+) B2) (+) B3 = \n [e \\in (D1 \\union D2) \\union D3 |-> (C1(e) + C2(e)) + C3(e)]\n /\\ B1 (+) (B2 (+) B3) = \n [e \\in D1 \\union (D2 \\union D3) |-> C1(e) + (C2(e) + C3(e))]\n BY DEF (+)\n<1>. HIDE DEF D1, D2, D3, C1, C2, C3\n<1>3. \\A e : (C1(e) + C2(e)) + C3(e) = C1(e) + (C2(e) + C3(e))\n BY <1>1\n<1>. QED BY <1>2, <1>3, Zenon\n\n\n(***************************************************************************)\n(* Given Bags B1, B2 then B1 \\sqsubseteq B1(+)B2 *)\n(***************************************************************************)\n\nTHEOREM Bags_UnionSqSubset ==\n ASSUME NEW B1, NEW B2, IsABag(B1), IsABag(B2)\n PROVE B1 \\sqsubseteq B1(+)B2\nBY DEF IsABag, (+), \\sqsubseteq\n\n(***************************************************************************)\n(* Scaling is monotonic in the scaling factor. *)\n(***************************************************************************)\n\nTHEOREM Bags_ScalingMonotonic ==\n ASSUME NEW B, IsABag(B), NEW n \\in Nat, NEW m \\in Nat, m <= n\n PROVE Scaling(m, B) \\sqsubseteq Scaling(n, B)\nBY DEF IsABag, Scaling, EmptyBag, SetToBag, \\sqsubseteq\n\n(***************************************************************************)\n(* Given Bags A and B, A(-)B \\sqsubseteq A *)\n(***************************************************************************)\n\nTHEOREM Bags_DifferenceSqsubset ==\n ASSUME NEW A, NEW B, IsABag(A), IsABag(B)\n PROVE A(-)B \\sqsubseteq A\nBY DEF IsABag, (-), \\sqsubseteq\n\n(***************************************************************************)\n(* EmptyBag is the additive identity. *)\n(***************************************************************************)\n\nTHEOREM Bags_EmptyBagOperations ==\n ASSUME NEW B, IsABag(B)\n PROVE /\\ B (+) EmptyBag = B\n /\\ B (-) EmptyBag = B\n<1>. DEFINE D == DOMAIN B C(e) == IF e \\in D THEN B[e] ELSE 0\n<1>1. /\\ \\A e \\in D : C(e) \\in Nat /\\ C(e) > 0\n /\\ \\A e : e \\notin D => C(e) = 0\n BY DEF IsABag\n<1>2. /\\ B = [e \\in D |-> C(e)]\n /\\ EmptyBag = [e \\in {} |-> 0]\n BY DEF IsABag, EmptyBag, SetToBag\n<1>. HIDE DEF D, C\n<1>3. B (+) EmptyBag = B\n BY <1>1, <1>2 DEF (+)\n<1>4. B (-) EmptyBag = B\n BY <1>1, <1>2 DEF (-)\n<1>. QED BY <1>3, <1>4\n\n(***************************************************************************)\n(* SetToBag of a set is a bag. *)\n(***************************************************************************)\n\nTHEOREM Bags_SetToBagIsABag ==\n ASSUME NEW S\n PROVE IsABag(SetToBag(S))\nBY DEF IsABag, SetToBag\n\n(***************************************************************************)\n(* CopiesIn is monotone w.r.t \\sqsubseteq *)\n(***************************************************************************)\n\nTHEOREM Bags_CopiesInBagsInMonotone ==\n ASSUME NEW B1, NEW B2, NEW e, IsABag(B1), IsABag(B2), B1 \\sqsubseteq B2\n PROVE /\\ BagIn(e, B1) => BagIn(e, B2)\n /\\ CopiesIn(e, B1) <= CopiesIn(e, B2)\nBY DEF IsABag, BagIn, \\sqsubseteq, BagToSet, CopiesIn\n\n(***************************************************************************)\n(* Given Bag B and Natural n, CopiesIn(e, Scaling(n, B))=n*CopiesIn(e, B) *)\n(***************************************************************************)\n\nTHEOREM Bags_CopiesInScaling ==\n ASSUME NEW B, IsABag(B), NEW n \\in Nat, NEW e\n PROVE CopiesIn(e, Scaling(n, B)) = n*CopiesIn(e, B)\nBY DEF IsABag, Scaling, CopiesIn, BagIn, EmptyBag, SetToBag, BagToSet\n\n(***************************************************************************)\n(* Given set S, CopiesIn(e, SetToBag(S))=IF e \\in B THEN 1 ELSE 0 *)\n(***************************************************************************)\n\nTHEOREM Bags_CopiesInSetToBag ==\n ASSUME NEW B, NEW e\n PROVE CopiesIn(e, SetToBag(B))=IF e \\in B THEN 1 ELSE 0\nBY DEF CopiesIn, SetToBag, BagToSet, BagIn\n\n(***************************************************************************)\n(* Given sequence seq, SeqToBag(seq) is a Bag *)\n(***************************************************************************)\n\nTHEOREM Bags_IsABagSeqToBag ==\n ASSUME NEW S, NEW seq \\in Seq(S)\n PROVE IsABag(SeqToBag(seq))\n<1>. DEFINE occs(x) == { i \\in DOMAIN seq : seq[i] = x }\n<1>1. ASSUME NEW x \\in Range(seq)\n PROVE Cardinality(occs(x)) \\in Nat \\ {0}\n <2>1. occs(x) # {}\n BY DEF Range\n <2>2. IsFiniteSet(occs(x))\n BY FS_Interval, FS_Subset\n <2>. QED BY <2>1, <2>2, FS_CardinalityType, FS_EmptySet, Zenon\n<1>. QED BY <1>1 DEF IsABag, SeqToBag\n\n=============================================================================\n", "FiniteSetTheorems": "------------------------- MODULE FiniteSetTheorems --------------------------\n(***************************************************************************)\n(* `^{\\large\\bf \\vspace{12pt} *)\n(* Facts about finite sets and their cardinality. *)\n(* Originally contributed by Tom Rodeheffer, MSR. *)\n(* \\vspace{12pt}}^' *)\n(***************************************************************************)\n\nEXTENDS FiniteSets, Integers, Functions, WellFoundedInduction\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* A set S is finite iff there exists a natural number n such that there *)\n(* exist a surjection (or a bijection) from 1..n to S. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\n\nLEMMA FS_NatSurjection ==\n ASSUME NEW S\n PROVE IsFiniteSet(S) <=> \\E n \\in Nat : ExistsSurjection(1..n,S)\n\nLEMMA FS_NatBijection ==\n ASSUME NEW S\n PROVE IsFiniteSet(S) <=> \\E n \\in Nat : ExistsBijection(1..n,S)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* If there exists n \\in Nat such that a bijection exists from 1..n to S, *)\n(* then Cardinality(S) = n. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\n\nLEMMA FS_CountingElements ==\n ASSUME NEW S, NEW n \\in Nat, ExistsBijection(1..n,S)\n PROVE Cardinality(S) = n\n\n\n(***************************************************************************)\n(* Corollary: a surjection from 1..n to S provides a cardinality bound. *)\n(***************************************************************************)\nTHEOREM FS_SurjCardinalityBound ==\n ASSUME NEW S, NEW n \\in Nat, ExistsSurjection(1..n, S)\n PROVE Cardinality(S) <= n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* For any finite set S, Cardinality(S) \\in Nat. Moreover, there is a *)\n(* bijection from 1 .. Cardinality(S) to S. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_CardinalityType ==\n ASSUME NEW S, IsFiniteSet(S)\n PROVE /\\ Cardinality(S) \\in Nat\n /\\ ExistsBijection(1..Cardinality(S), S)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The image of a finite set under a bijection is finite. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_Bijection ==\n ASSUME NEW S, NEW T, IsFiniteSet(S), ExistsBijection(S,T)\n PROVE /\\ IsFiniteSet(T)\n /\\ Cardinality(T) = Cardinality(S)\n\nTHEOREM FS_SameCardinalityBij ==\n ASSUME NEW S, NEW T, IsFiniteSet(S), IsFiniteSet(T),\n Cardinality(S) = Cardinality(T)\n PROVE ExistsBijection(S,T)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Any surjection between two finite sets of equal cardinality is *)\n(* an injection. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_SurjSameCardinalityImpliesInj ==\n ASSUME NEW S, NEW T, IsFiniteSet(S), Cardinality(S) = Cardinality(T),\n NEW f \\in Surjection(S,T)\n PROVE f \\in Injection(S,T)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The image of a finite set is finite. *)\n(* NB: Note that any function is a surjection on its range by theorem *)\n(* Fun_RangeProperties. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_Surjection ==\n ASSUME NEW S, NEW T, NEW f \\in Surjection(S,T), IsFiniteSet(S)\n PROVE /\\ IsFiniteSet(T)\n /\\ Cardinality(T) <= Cardinality(S)\n /\\ Cardinality(T) = Cardinality(S) <=> f \\in Injection(S,T)\n\nTHEOREM FS_Image ==\n ASSUME NEW S, IsFiniteSet(S), NEW Op(_)\n PROVE LET T == {Op(x) : x \\in S}\n IN /\\ IsFiniteSet(T)\n /\\ Cardinality(T) <= Cardinality(S)\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Symmetric theorem for injections and the pigeonhole principle. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_Injection ==\n ASSUME NEW S, NEW T, NEW f \\in Injection(S,T), IsFiniteSet(T)\n PROVE /\\ IsFiniteSet(S)\n /\\ Cardinality(S) <= Cardinality(T)\n /\\ Cardinality(S) = Cardinality(T) <=> f \\in Surjection(S,T)\n\nTHEOREM FS_PigeonHole ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW T, IsFiniteSet(T), Cardinality(T) < Cardinality(S),\n NEW f \\in [S -> T]\n PROVE \\E x,y \\in S : x # y /\\ f[x] = f[y]\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The cardinality of a finite set S is 0 iff S is empty. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_EmptySet ==\n /\\ IsFiniteSet({})\n /\\ Cardinality({}) = 0\n /\\ \\A S : IsFiniteSet(S) => (Cardinality(S) = 0 <=> S = {})\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* If S is finite, so are S \\cup {x} and S \\ {x}. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_AddElement ==\n ASSUME NEW S, NEW x, IsFiniteSet(S)\n PROVE /\\ IsFiniteSet(S \\cup {x})\n /\\ Cardinality(S \\cup {x}) =\n IF x \\in S THEN Cardinality(S) ELSE Cardinality(S)+1\n\nTHEOREM FS_RemoveElement ==\n ASSUME NEW S, NEW x, IsFiniteSet(S)\n PROVE /\\ IsFiniteSet(S \\ {x})\n /\\ Cardinality(S \\ {x}) =\n IF x \\in S THEN Cardinality(S)-1 ELSE Cardinality(S)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* In particular, a singleton set is finite. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_Singleton ==\n /\\ \\A x : IsFiniteSet({x}) /\\ Cardinality({x}) = 1\n /\\ \\A S : IsFiniteSet(S) => (Cardinality(S) = 1 <=> \\E x: S = {x})\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Any subset of a finite set is finite. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_Subset ==\n ASSUME NEW S, IsFiniteSet(S), NEW T \\in SUBSET S\n PROVE /\\ IsFiniteSet(T)\n /\\ Cardinality(T) <= Cardinality(S)\n /\\ Cardinality(S) = Cardinality(T) => S = T\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* a..b is a finite set for any a,b \\in Int. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_Interval ==\n ASSUME NEW a \\in Int, NEW b \\in Int\n PROVE /\\ IsFiniteSet(a..b)\n /\\ Cardinality(a..b) = IF a > b THEN 0 ELSE b-a+1\n\nTHEOREM FS_BoundedSetOfNaturals ==\n ASSUME NEW S \\in SUBSET Nat, NEW n \\in Nat,\n \\A s \\in S : s <= n\n PROVE /\\ IsFiniteSet(S)\n /\\ Cardinality(S) \\leq n+1\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Induction for finite sets. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\n\nTHEOREM FS_Induction ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW P(_), P({}),\n ASSUME NEW T \\in SUBSET S, IsFiniteSet(T), P(T), NEW x \\in S \\ T\n PROVE P(T \\cup {x})\n PROVE P(S)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The finite subsets form a well-founded ordering with respect to strict *)\n(* set inclusion. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\n\nFiniteSubsetsOf(S) == { T \\in SUBSET S : IsFiniteSet(T) }\nStrictSubsetOrdering(S) == { ss \\in (SUBSET S) \\X (SUBSET S) :\n ss[1] \\subseteq ss[2] /\\ ss[1] # ss[2] }\n\nLEMMA FS_FiniteSubsetsOfFinite ==\n ASSUME NEW S, IsFiniteSet(S)\n PROVE FiniteSubsetsOf(S) = SUBSET S\n\n\n(*****************************************************************************)\n(* The formulation of the following theorem doesn't require S being finite. *)\n(* If S is finite, it implies *)\n(* IsWellFoundedOn(StrictSubsetOrdering(S), SUBSET S) *)\n(* using lemma FS_FiniteSubsetsOfFinite. *)\n(*****************************************************************************)\nTHEOREM FS_StrictSubsetOrderingWellFounded ==\n ASSUME NEW S\n PROVE IsWellFoundedOn(StrictSubsetOrdering(S), FiniteSubsetsOf(S))\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Well-founded induction for finite subsets. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\n\nTHEOREM FS_WFInduction ==\n ASSUME NEW P(_), NEW S, IsFiniteSet(S),\n ASSUME NEW T \\in SUBSET S,\n \\A U \\in (SUBSET T) \\ {T} : P(U)\n PROVE P(T)\n PROVE P(S)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The union of two finite sets is finite. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_Union ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW T, IsFiniteSet(T)\n PROVE /\\ IsFiniteSet(S \\cup T)\n /\\ Cardinality(S \\cup T) =\n Cardinality(S) + Cardinality(T) - Cardinality(S \\cap T)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Corollary: two majorities intersect. More precisely, any two subsets *)\n(* of a finite set U such that the sum of cardinalities of the subsets *)\n(* exceeds that of U must have non-empty intersection. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_MajoritiesIntersect ==\n ASSUME NEW U, NEW S, NEW T, IsFiniteSet(U),\n S \\subseteq U, T \\subseteq U,\n Cardinality(S) + Cardinality(T) > Cardinality(U)\n PROVE S \\cap T # {}\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The intersection of a finite set with an arbitrary set is finite. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\n\n\nTHEOREM FS_Intersection ==\n ASSUME NEW S, IsFiniteSet(S), NEW T\n PROVE /\\ IsFiniteSet(S \\cap T)\n /\\ IsFiniteSet(T \\cap S)\n /\\ Cardinality(S \\cap T) <= Cardinality(S)\n /\\ Cardinality(T \\cap S) <= Cardinality(S)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The difference between a finite set and an arbitrary set is finite. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_Difference ==\n ASSUME NEW S, NEW T, IsFiniteSet(S)\n PROVE /\\ IsFiniteSet(S \\ T)\n /\\ Cardinality(S \\ T) = Cardinality(S) - Cardinality(S \\cap T)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The union of a finite number of finite sets is finite. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_UNION ==\n ASSUME NEW S, IsFiniteSet(S), \\A T \\in S : IsFiniteSet(T)\n PROVE IsFiniteSet(UNION S)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The product of two finite sets is finite. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_Product ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW T, IsFiniteSet(T)\n PROVE /\\ IsFiniteSet(S \\X T)\n /\\ Cardinality(S \\X T) = Cardinality(S) * Cardinality(T)\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The powerset of a finite set is finite. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_SUBSET ==\n ASSUME NEW S, IsFiniteSet(S)\n PROVE /\\ IsFiniteSet(SUBSET S)\n /\\ Cardinality(SUBSET S) = 2^Cardinality(S)\n\n=============================================================================\n\\* Modification History\n\\* Last modified Wed Jan 08 17:43:30 CET 2020 by merz\n\\* Last modified Thu Jul 04 15:15:07 CEST 2013 by bhargav\n\\* Last modified Tue Jun 04 11:44:51 CEST 2013 by bhargav\n\\* Last modified Fri May 03 12:02:51 PDT 2013 by tomr\n\\* Created Fri Oct 05 15:04:18 PDT 2012 by tomr\n", "FiniteSetTheorems_proofs": "---------------------- MODULE FiniteSetTheorems_proofs ----------------------\n(***************************************************************************)\n(* `^{\\large\\bf \\vspace{12pt} *)\n(* Facts about finite sets and their cardinality. *)\n(* Originally contributed by Tom Rodeheffer, MSR. *)\n(* \\vspace{12pt}}^' *)\n(***************************************************************************)\n\nEXTENDS\n FiniteSets,\n Sequences,\n FunctionTheorems,\n WellFoundedInduction,\n TLAPS\n\n(***************************************************************************)\n(* Arithmetic lemma that is currently not proved. *)\n(***************************************************************************)\nLEMMA TwoExpLemma ==\n ASSUME NEW n \\in Nat\n PROVE /\\ 2^0 = 1\n /\\ 2^(n+1) = 2^n + 2^n\nPROOF OMITTED\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* A set S is finite iff there exists a natural number n such that there *)\n(* exist a surjection (or a bijection) from 1..n to S. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\n\nLEMMA FS_NatSurjection ==\n ASSUME NEW S\n PROVE IsFiniteSet(S) <=> \\E n \\in Nat : ExistsSurjection(1..n,S)\n\n<1>1. ASSUME IsFiniteSet(S) PROVE \\E n \\in Nat : ExistsSurjection(1..n,S)\n <2>1. PICK Q \\in Seq(S) : \\A s \\in S : \\E i \\in 1..Len(Q) : Q[i] = s\n BY <1>1 DEF IsFiniteSet\n <2>2. /\\ Len(Q) \\in Nat\n /\\ Q \\in Surjection(1..Len(Q),S)\n BY <2>1 DEF Surjection\n <2> QED BY <2>2 DEF ExistsSurjection\n\n<1>2. ASSUME NEW n \\in Nat, ExistsSurjection(1..n,S) PROVE IsFiniteSet(S)\n <2>1. PICK M \\in [1..n -> S] : \\A t \\in S : \\E s \\in 1..n : M[s] = t\n BY <1>2 DEF ExistsSurjection, Surjection\n <2>. QED BY <2>1, M \\in Seq(S) DEF IsFiniteSet\n\n<1> QED BY <1>1, <1>2\n\n\nLEMMA FS_NatBijection ==\n ASSUME NEW S\n PROVE IsFiniteSet(S) <=> \\E n \\in Nat : ExistsBijection(1..n,S)\nBY FS_NatSurjection, Fun_NatSurjEquivNatBij, Zenon\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* If there exists n \\in Nat such that a bijection exists from 1..n to S, *)\n(* then Cardinality(S) = n. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\n\nLEMMA FS_CountingElements ==\n ASSUME NEW S, NEW n \\in Nat, ExistsBijection(1..n,S)\n PROVE Cardinality(S) = n\nPROOF\n <1> DEFINE\n (***********************************************************************)\n (* Size of set T. *)\n (***********************************************************************)\n Size(T) == CHOOSE i \\in Nat : ExistsBijection(1..i,T)\n\n (***********************************************************************)\n (* Size function for subsets of S. *)\n (***********************************************************************)\n SZ == [ T \\in SUBSET S |-> Size(T) ]\n\n (***********************************************************************)\n (* Formula part of the CS property for element T. *)\n (***********************************************************************)\n fn(CS,T) == IF T = {} THEN 0 ELSE 1 + CS[T \\ {CHOOSE x : x \\in T}]\n\n (***********************************************************************)\n (* The CS property. *)\n (***********************************************************************)\n IsCS(CS) == CS = [T \\in SUBSET S |-> fn(CS,T)]\n\n (***********************************************************************)\n (* CS function for subsets of S. Since this is defined as CHOOSE *)\n (* something that satisfies the CS property, we do not know that the *)\n (* CS function actually satisfies the CS property until we know that *)\n (* there exists something that satisfies the CS property. *)\n (***********************************************************************)\n CS == CHOOSE CS : IsCS(CS)\n\n <1> HIDE DEF SZ, CS, fn\n\n\n (*************************************************************************)\n (* The SZ function satisfies the CS property. *)\n (*************************************************************************)\n <1>1. IsCS(SZ)\n (***********************************************************************)\n (* Use induction on the size of T to show that the values match at *)\n (* each T \\in SUBSET S. *)\n (***********************************************************************)\n <2> DEFINE\n Prop(i) == \\A T \\in SUBSET S : ExistsBijection(1..i,T) => SZ[T] = fn(SZ,T)\n\n <2>1. \\A i \\in Nat : Prop(i)\n <3>1. Prop(0)\n (*******************************************************************)\n (* Base step. *)\n (*******************************************************************)\n <4>1. SUFFICES ASSUME NEW T \\in SUBSET S, ExistsBijection(1..0,T)\n PROVE SZ[T] = fn(SZ,T)\n OBVIOUS\n <4>2. Size(T) = 0 BY <4>1, Fun_NatBijSame\n <4>3. T = {} BY <4>1, Fun_NatBijEmpty\n <4>4. SZ[T] = 0 BY <4>2, Zenon DEF SZ\n <4>5. fn(SZ,T) = 0 BY <4>3 DEF fn\n <4> QED BY <4>4, <4>5\n\n <3>2. ASSUME NEW i \\in Nat, Prop(i) PROVE Prop(i+1)\n (*******************************************************************)\n (* Inductive step. *)\n (*******************************************************************)\n <4>. SUFFICES ASSUME NEW T \\in SUBSET S, ExistsBijection(1 .. i+1,T)\n PROVE SZ[T] = fn(SZ,T)\n OBVIOUS\n <4>1. ~ExistsBijection(1..0,T) BY Fun_NatBijSame, Z3\n <4>2. T # {} BY <4>1, Fun_NatBijEmpty\n <4>3. Size(T) = i+1 BY Fun_NatBijSame\n <4>4. PICK t \\in T : t = CHOOSE x : x \\in T BY <4>2\n <4>. DEFINE U == T \\ {t}\n <4>5. ExistsBijection(1..i,U) BY Fun_NatBijSubElem, Z3\n <4>6. SZ[U] = fn(SZ,U) BY <4>5, <3>2\n <4>7. SZ[U] = i BY <4>5, Fun_NatBijSame DEF SZ\n <4>8. fn(SZ,T) = 1 + SZ[U] BY <4>2, <4>4 DEF fn\n <4>9. fn(SZ,T) = i+1 BY <4>7, <4>8\n <4>10. SZ[T] = i+1 BY <4>3, Zenon DEF SZ\n <4>. QED BY <4>9, <4>10\n <3> HIDE DEF Prop\n <3> QED BY Isa, <3>1, <3>2, NatInduction\n\n <2> SUFFICES ASSUME NEW T \\in SUBSET S PROVE SZ[T] = fn(SZ,T) BY Zenon DEF SZ\n <2>2. PICK i \\in Nat : ExistsBijection(1..i,T) BY Fun_NatBijSubset\n <2> QED BY <2>1, <2>2\n\n\n (*************************************************************************)\n (* Any two things that satisfy the CS property must be equal. *)\n (*************************************************************************)\n <1>2. ASSUME\n NEW CS1, IsCS(CS1),\n NEW CS2, IsCS(CS2)\n PROVE CS1 = CS2\n (***********************************************************************)\n (* Use induction on the size of T to show that the values match at *)\n (* each T \\in SUBSET S. *)\n (***********************************************************************)\n <2> DEFINE\n Prop(i) == \\A T \\in SUBSET S : ExistsBijection(1..i,T) => CS1[T] = CS2[T]\n\n <2>1. \\A i \\in Nat : Prop(i)\n <3>1. Prop(0)\n (*******************************************************************)\n (* Base step. *)\n (*******************************************************************)\n <4>1. SUFFICES ASSUME NEW T \\in SUBSET S, ExistsBijection(1..0,T)\n PROVE CS1[T] = CS2[T]\n OBVIOUS\n <4>2. T = {} BY <4>1, Fun_NatBijEmpty\n <4>3. fn(CS1,T) = 0 BY <4>2 DEF fn\n <4>4. fn(CS2,T) = 0 BY <4>2 DEF fn\n <4> QED BY <4>3, <4>4, <1>2\n\n <3>2. ASSUME NEW i \\in Nat, Prop(i) PROVE Prop(i+1)\n (*******************************************************************)\n (* Inductive step. *)\n (*******************************************************************)\n <4>. SUFFICES ASSUME NEW T \\in SUBSET S, ExistsBijection(1 .. i+1,T)\n PROVE CS1[T] = CS2[T]\n OBVIOUS\n <4>5. ~ExistsBijection(1..0,T) BY Fun_NatBijSame, Z3\n <4>6. T # {} BY <4>5, Fun_NatBijEmpty\n <4>7. PICK t \\in T : t = CHOOSE x : x \\in T BY <4>6\n <4>. DEFINE U == T \\ {t}\n <4>9. ExistsBijection(1..i,U) BY Fun_NatBijSubElem, Z3\n <4>10. CS1[U] = CS2[U] BY <4>9, <3>2\n <4>11. CS1[T] = 1 + CS1[U] BY <4>6, <4>7, <1>2, Zenon DEF fn\n <4>12. CS2[T] = 1 + CS2[U] BY <4>6, <4>7, <1>2, Zenon DEF fn\n <4> QED BY <4>10, <4>11, <4>12\n\n <3> HIDE DEF Prop\n <3> QED BY Isa, <3>1, <3>2, NatInduction\n\n <2> SUFFICES ASSUME NEW T \\in SUBSET S PROVE CS1[T] = CS2[T] BY <1>2, Zenon\n <2>2. PICK i \\in Nat : ExistsBijection(1..i,T) BY Fun_NatBijSubset, Zenon\n <2> QED BY <2>1, <2>2\n\n\n (*************************************************************************)\n (* Since SZ satisfies the CS property, the CS function must satisfy the *)\n (* CS property. And it must be the same as SZ. *)\n (*************************************************************************)\n <1>3. IsCS(CS) BY <1>1 DEF CS\n <1>4. CS = SZ BY <1>1, <1>2, <1>3, Zenon\n\n\n <1>5. Cardinality(S) = CS[S] BY Zenon DEF Cardinality, CS, fn\n <1>7. SZ[S] = Size(S) BY Zenon DEF SZ\n <1>8. Size(S) = n BY Fun_NatBijSame\n <1> QED BY <1>4, <1>5, <1>7, <1>8\n\n\n(***************************************************************************)\n(* Corollary: a surjection from 1..n to S provides a cardinality bound. *)\n(***************************************************************************)\nTHEOREM FS_SurjCardinalityBound ==\n ASSUME NEW S, NEW n \\in Nat, ExistsSurjection(1..n, S)\n PROVE Cardinality(S) <= n\nBY Fun_NatSurjImpliesNatBij, FS_CountingElements\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* For any finite set S, Cardinality(S) \\in Nat. Moreover, there is a *)\n(* bijection from 1 .. Cardinality(S) to S. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_CardinalityType ==\n ASSUME NEW S, IsFiniteSet(S)\n PROVE /\\ Cardinality(S) \\in Nat\n /\\ ExistsBijection(1..Cardinality(S), S)\nBY FS_NatBijection, FS_CountingElements\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The image of a finite set under a bijection is finite. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_Bijection ==\n ASSUME NEW S, NEW T, IsFiniteSet(S), ExistsBijection(S,T)\n PROVE /\\ IsFiniteSet(T)\n /\\ Cardinality(T) = Cardinality(S)\nBY FS_CardinalityType, Fun_ExistsBijTransitive, FS_CountingElements,\n FS_NatBijection\n\n\nTHEOREM FS_SameCardinalityBij ==\n ASSUME NEW S, NEW T, IsFiniteSet(S), IsFiniteSet(T),\n Cardinality(S) = Cardinality(T)\n PROVE ExistsBijection(S,T)\nBY FS_CardinalityType, Fun_ExistsBijSymmetric, Fun_ExistsBijTransitive\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Any surjection between two finite sets of equal cardinality is *)\n(* an injection. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_SurjSameCardinalityImpliesInj ==\n ASSUME NEW S, NEW T, IsFiniteSet(S), Cardinality(S) = Cardinality(T),\n NEW f \\in Surjection(S,T)\n PROVE f \\in Injection(S,T)\n<1>1. SUFFICES ASSUME NEW a \\in S, NEW b \\in S, a # b, f[a] = f[b]\n PROVE FALSE\n BY DEF Injection, Surjection, IsInjective\n<1>. DEFINE n == Cardinality(S)\n<1>. n \\in Nat BY FS_CardinalityType\n<1>. PICK g \\in Bijection(1..n, S) : TRUE\n BY FS_CardinalityType, Zenon DEF ExistsBijection\n<1>2. PICK i,j \\in 1 .. n :\n /\\ i < j\n /\\ \\/ g[i] = a /\\ g[j] = b\n \\/ g[i] = b /\\ g[j] = a\n <2>1. PICK i,j \\in 1 .. n : i # j /\\ g[i] = a /\\ g[j] = b\n BY <1>1 DEF Bijection, Surjection\n <2>2. CASE i < j BY <2>1, <2>2\n <2>3. CASE i > j BY <2>1, <2>3\n <2>. QED BY <2>1, <2>2, <2>3\n<1>. n-1 \\in Nat BY <1>2\n<1>. DEFINE h == [ k \\in 1 .. n-1 |-> IF k=j THEN f[g[n]] ELSE f[g[k]] ]\n<1>3. h \\in Surjection(1..n-1, T)\n <2>1. h \\in [1..n-1 -> T] BY DEF Bijection, Surjection\n <2>2. ASSUME NEW t \\in T PROVE \\E k \\in 1..n-1 : h[k] = t\n <3>1. PICK s \\in S : f[s] = t BY DEF Surjection\n <3>2. PICK l \\in 1..n : g[l] = s BY DEF Bijection, Surjection\n <3>. HIDE DEF n\n <3>3. CASE l = j\n <4>. /\\ i \\in 1 .. n-1\n /\\ i # j\n /\\ h[i] = t\n BY <1>1, <1>2, <3>1, <3>2, <3>3\n <4>. QED OBVIOUS\n <3>4. CASE l # j /\\ l = n\n <4>. /\\ j \\in 1 .. n-1\n /\\ h[j] = t\n BY <3>1, <3>2, <3>4\n <4>. QED OBVIOUS\n <3>5. CASE l # j /\\ l # n\n BY <3>1, <3>2, <3>5, l \\in 1 .. n-1\n <3>. QED BY <3>3, <3>4, <3>5\n <2>. QED BY <2>1, <2>2 DEF Surjection\n<1>4. Cardinality(T) <= n-1 BY <1>3, FS_SurjCardinalityBound DEF ExistsSurjection\n<1>. QED BY <1>4\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The cardinality of a finite set S is 0 iff S is empty. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_EmptySet ==\n /\\ IsFiniteSet({})\n /\\ Cardinality({}) = 0\n /\\ \\A S : IsFiniteSet(S) => (Cardinality(S) = 0 <=> S = {})\n<1>1. IsFiniteSet({}) /\\ Cardinality({}) = 0\n BY Fun_NatBijEmpty, FS_NatBijection, FS_CountingElements, Zenon\n<1>2. ASSUME NEW S, IsFiniteSet(S), Cardinality(S) = 0\n PROVE S = {}\n BY <1>2, FS_CardinalityType, Fun_NatBijEmpty\n<1>. QED BY <1>1, <1>2\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* If S is finite, so are S \\cup {x} and S \\ {x}. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_AddElement ==\n ASSUME NEW S, NEW x, IsFiniteSet(S)\n PROVE /\\ IsFiniteSet(S \\cup {x})\n /\\ Cardinality(S \\cup {x}) =\n IF x \\in S THEN Cardinality(S) ELSE Cardinality(S)+1\n<1>1. CASE x \\notin S\n BY <1>1, FS_CardinalityType, Fun_NatBijAddElem, FS_NatBijection,\n FS_CountingElements\n<1>. QED BY <1>1, Zenon \\* the case \"x \\in S\" is trivial\n\n\nTHEOREM FS_RemoveElement ==\n ASSUME NEW S, NEW x, IsFiniteSet(S)\n PROVE /\\ IsFiniteSet(S \\ {x})\n /\\ Cardinality(S \\ {x}) =\n IF x \\in S THEN Cardinality(S)-1 ELSE Cardinality(S)\n<1>1. CASE x \\in S\n BY <1>1, FS_CardinalityType, Fun_NatBijSubElem, FS_NatBijection,\n FS_CountingElements, FS_EmptySet\n<1>. QED BY <1>1, Zenon \\* the case \"x \\notin S\" is trivial\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* In particular, a singleton set is finite. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_Singleton ==\n /\\ \\A x : IsFiniteSet({x}) /\\ Cardinality({x}) = 1\n /\\ \\A S : IsFiniteSet(S) => (Cardinality(S) = 1 <=> \\E x: S = {x})\n\n<1>1. ASSUME NEW x PROVE IsFiniteSet({x}) /\\ Cardinality({x}) = 1\n <2>1. /\\ IsFiniteSet({} \\cup {x})\n /\\ Cardinality({} \\cup {x}) = 0 + 1\n BY FS_EmptySet, FS_AddElement, Zenon\n <2>. QED BY <2>1, {x} = {} \\cup {x}\n<1>2. ASSUME NEW S, IsFiniteSet(S), Cardinality(S) = 1\n PROVE \\E x : S = {x}\n BY <1>2, FS_CardinalityType, Fun_NatBijSingleton, Zenon\n<1>. QED BY <1>1, <1>2, Zenon\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Any subset of a finite set is finite. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_Subset ==\n ASSUME NEW S, IsFiniteSet(S), NEW T \\in SUBSET S\n PROVE /\\ IsFiniteSet(T)\n /\\ Cardinality(T) <= Cardinality(S)\n /\\ Cardinality(S) = Cardinality(T) => S = T\n\\* NB: Changing the last implication to an equivalence breaks proofs\n\n<1>1. /\\ Cardinality(S) \\in Nat\n /\\ ExistsBijection(1 .. Cardinality(S), S)\n BY FS_CardinalityType\n<1>2. PICK n \\in Nat : ExistsBijection(1..n, T) /\\ n <= Cardinality(S)\n BY <1>1, Fun_NatBijSubset, Zenon\n<1>3. ASSUME Cardinality(S) = Cardinality(T), S # T\n PROVE FALSE\n <2>1. PICK x \\in S \\ T : TRUE BY <1>3, Zenon\n <2>2. /\\ IsFiniteSet(S \\ {x})\n /\\ Cardinality(S \\ {x}) = Cardinality(S) - 1\n BY <2>1, FS_RemoveElement\n <2>3. T \\subseteq S \\ {x} BY <2>1\n <2>4. PICK m \\in Nat : ExistsBijection(1..m, T) /\\ m <= Cardinality(S)-1\n BY <2>2, <2>3, FS_CardinalityType, Fun_NatBijSubset, Zenon\n <2>. QED BY <2>4, <1>3, FS_CountingElements\n<1>. QED BY <1>2, <1>3, FS_NatBijection, FS_CountingElements\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The image of a finite set is finite. *)\n(* NB: Note that any function is a surjection on its range by theorem *)\n(* Fun_RangeProperties. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_Surjection ==\n ASSUME NEW S, NEW T, NEW f \\in Surjection(S,T), IsFiniteSet(S)\n PROVE /\\ IsFiniteSet(T)\n /\\ Cardinality(T) <= Cardinality(S)\n /\\ Cardinality(T) = Cardinality(S) <=> f \\in Injection(S,T)\n<1>1. /\\ Cardinality(S) \\in Nat\n /\\ ExistsBijection(1 .. Cardinality(S), S)\n BY FS_CardinalityType\n<1>2. ExistsSurjection(1 .. Cardinality(S), T)\n BY <1>1, Fun_ExistsBijEquiv, Fun_ExistsSurjTransitive DEF ExistsSurjection\n<1>4. IsFiniteSet(T) /\\ Cardinality(T) <= Cardinality(S)\n BY <1>1, <1>2, FS_NatSurjection, FS_SurjCardinalityBound\n<1>5. ASSUME Cardinality(T) = Cardinality(S) PROVE f \\in Injection(S,T)\n BY <1>5, FS_SurjSameCardinalityImpliesInj\n<1>6. ASSUME f \\in Injection(S,T) PROVE Cardinality(T) = Cardinality(S)\n <2>1. ExistsBijection(S, T) BY <1>6 DEF Bijection, ExistsBijection\n <2>2. ExistsBijection(1..Cardinality(S), T)\n BY <1>1, <2>1, Fun_ExistsBijTransitive\n <2>. QED BY <1>1, <2>2, FS_CountingElements\n<1>. QED BY <1>4, <1>5, <1>6\n\nTHEOREM FS_Image ==\n ASSUME NEW S, IsFiniteSet(S), NEW Op(_)\n PROVE LET T == {Op(x) : x \\in S}\n IN /\\ IsFiniteSet(T)\n /\\ Cardinality(T) <= Cardinality(S)\n<1>. DEFINE f == [x \\in S |-> Op(x)]\n<1>. f \\in Surjection(S, {Op(x) : x \\in S}) BY DEF Surjection\n<1>. QED BY FS_Surjection\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Symmetric theorem for injections and the pigeonhole principle. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_Injection ==\n ASSUME NEW S, NEW T, NEW f \\in Injection(S,T), IsFiniteSet(T)\n PROVE /\\ IsFiniteSet(S)\n /\\ Cardinality(S) <= Cardinality(T)\n /\\ Cardinality(S) = Cardinality(T) <=> f \\in Surjection(S,T)\n<1>1. CASE S = {}\n <2>1. IsFiniteSet(S) /\\ Cardinality(S) <= Cardinality(T)\n BY <1>1, FS_CardinalityType, FS_EmptySet\n <2>2. Cardinality(S) = Cardinality(T) <=> f \\in Surjection(S,T)\n BY <1>1, FS_CardinalityType, FS_EmptySet, Fun_IsSurj, Fun_SurjectionProperties, Zenon\n DEF Injection\n <2>. QED BY <2>1, <2>2\n<1>2. CASE S # {}\n <2>1. Inverse(f,S,T) \\in Surjection(T,S)\n BY <1>2, Fun_InjInverse\n <2>2. /\\ IsFiniteSet(S)\n /\\ Cardinality(S) <= Cardinality(T)\n /\\ Cardinality(S) = Cardinality(T) <=> Inverse(f,S,T) \\in Injection(T,S)\n BY <2>1, FS_Surjection\n <2>3. ASSUME Cardinality(S) = Cardinality(T)\n PROVE f \\in Surjection(S,T)\n <3>. SUFFICES ASSUME NEW t \\in T, \\A s \\in S : f[s] # t\n PROVE FALSE\n BY DEF Surjection, Injection\n <3>1. ExistsBijection(T,S)\n BY <2>1, <2>2, <2>3 DEF ExistsBijection, Bijection\n <3>2. f \\in Bijection(S, Range(f))\n BY Fun_InjMeansBijImage\n <3>3. ExistsBijection(T, Range(f))\n BY <3>1, <3>2, Fun_ExistsBijTransitive DEF ExistsBijection\n <3>4. Cardinality(T) = Cardinality(Range(f))\n BY <3>3, FS_Bijection\n <3>5. Range(f) \\subseteq T\n BY DEF Injection, Range\n <3>6. Range(f) = T\n BY <3>4, <3>5, FS_Subset, Zenon\n <3>. QED BY <3>6 DEF Range, Injection\n <2>4. ASSUME f \\in Surjection(S,T)\n PROVE Cardinality(S) = Cardinality(T)\n BY <2>2, <2>4, FS_Surjection, FS_CardinalityType\n <2>. QED BY <2>2, <2>3, <2>4\n<1>. QED BY <1>1, <1>2\n\nTHEOREM FS_PigeonHole ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW T, IsFiniteSet(T), Cardinality(T) < Cardinality(S),\n NEW f \\in [S -> T]\n PROVE \\E x,y \\in S : x # y /\\ f[x] = f[y]\n<1>. SUFFICES ASSUME f \\in Injection(S,T)\n PROVE FALSE\n BY DEF Injection, IsInjective\n<1>. QED BY FS_Injection, FS_CardinalityType\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* a..b is a finite set for any a,b \\in Int. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_Interval ==\n ASSUME NEW a \\in Int, NEW b \\in Int\n PROVE /\\ IsFiniteSet(a..b)\n /\\ Cardinality(a..b) = IF a > b THEN 0 ELSE b-a+1\n<1>1. CASE a <= b\n BY <1>1, Fun_ExistsBijInterval, FS_NatBijection, FS_CountingElements\n<1>2. CASE a > b\n BY <1>2, FS_EmptySet\n<1>. QED BY <1>1, <1>2\n\n\nTHEOREM FS_BoundedSetOfNaturals ==\n ASSUME NEW S \\in SUBSET Nat, NEW n \\in Nat,\n \\A s \\in S : s <= n\n PROVE /\\ IsFiniteSet(S)\n /\\ Cardinality(S) \\leq n+1\n<1>1. S \\subseteq 0 .. n\n OBVIOUS\n<1>2. IsFiniteSet(0..n) /\\ Cardinality(0..n) = n+1 BY FS_Interval\n<1>. QED BY <1>1, <1>2, FS_Subset, Zenon\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Induction for finite sets. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\n\nTHEOREM FS_Induction ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW P(_), P({}),\n ASSUME NEW T \\in SUBSET S, IsFiniteSet(T), P(T), NEW x \\in S \\ T\n PROVE P(T \\cup {x})\n PROVE P(S)\n<1>. DEFINE Q(n) == \\A T \\in SUBSET S : Cardinality(T) = n => P(T)\n<1>1. SUFFICES \\A n \\in Nat : Q(n) BY FS_CardinalityType\n<1>2. Q(0) BY FS_EmptySet, FS_Subset\n<1>3. ASSUME NEW n \\in Nat, Q(n),\n NEW T \\in SUBSET S, Cardinality(T) = n+1\n PROVE P(T)\n <2>1. PICK x \\in T : TRUE BY <1>3, FS_EmptySet, FS_Subset\n <2>2. /\\ T \\ {x} \\in SUBSET S\n /\\ IsFiniteSet(T \\ {x})\n /\\ Cardinality(T \\ {x}) = n\n BY <1>3, FS_RemoveElement, FS_Subset\n <2>3. P(T \\ {x}) BY <2>2, Q(n)\n <2>. QED BY <2>2, <2>3, Zenon\n<1>4. QED BY <1>2, <1>3, NatInduction, Isa\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The finite subsets form a well-founded ordering with respect to strict *)\n(* set inclusion. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\n\nFiniteSubsetsOf(S) == { T \\in SUBSET S : IsFiniteSet(T) }\nStrictSubsetOrdering(S) == { ss \\in (SUBSET S) \\X (SUBSET S) :\n ss[1] \\subseteq ss[2] /\\ ss[1] # ss[2] }\n\nLEMMA FS_FiniteSubsetsOfFinite ==\n ASSUME NEW S, IsFiniteSet(S)\n PROVE FiniteSubsetsOf(S) = SUBSET S\nBY FS_Subset DEF FiniteSubsetsOf\n\n\n(*****************************************************************************)\n(* The formulation of the following theorem doesn't require S being finite. *)\n(* If S is finite, it implies *)\n(* IsWellFoundedOn(StrictSubsetOrdering(S), SUBSET S) *)\n(* using lemma FS_FiniteSubsetsOfFinite. *)\n(*****************************************************************************)\nTHEOREM FS_StrictSubsetOrderingWellFounded ==\n ASSUME NEW S\n PROVE IsWellFoundedOn(StrictSubsetOrdering(S), FiniteSubsetsOf(S))\n\n<1>1. \\A T \\in FiniteSubsetsOf(S) : Cardinality(T) \\in Nat\n BY FS_CardinalityType, FS_Subset DEF FiniteSubsetsOf\n<1>2. IsWellFoundedOn(PreImage(Cardinality, FiniteSubsetsOf(S), OpToRel(<,Nat)),\n FiniteSubsetsOf(S))\n BY <1>1, PreImageWellFounded, NatLessThanWellFounded, IsaM(\"blast\")\n<1>3. StrictSubsetOrdering(S) \\cap (FiniteSubsetsOf(S) \\X FiniteSubsetsOf(S))\n \\subseteq PreImage(Cardinality, FiniteSubsetsOf(S), OpToRel(<, Nat))\n BY FS_Subset, <1>1\n DEF StrictSubsetOrdering, FiniteSubsetsOf, PreImage, OpToRel\n<1>. QED BY <1>2, <1>3, IsWellFoundedOnSubrelation, Zenon\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Well-founded induction for finite subsets. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\n\nTHEOREM FS_WFInduction ==\n ASSUME NEW P(_), NEW S, IsFiniteSet(S),\n ASSUME NEW T \\in SUBSET S,\n \\A U \\in (SUBSET T) \\ {T} : P(U)\n PROVE P(T)\n PROVE P(S)\n<1>. DEFINE SubS == SUBSET S\n<1>1. IsWellFoundedOn(StrictSubsetOrdering(S), SubS)\n BY FS_FiniteSubsetsOfFinite, FS_StrictSubsetOrderingWellFounded, Zenon\n<1>2. \\A T \\in SubS :\n (\\A U \\in SetLessThan(T, StrictSubsetOrdering(S), SubS) : P(U))\n => P(T)\n BY Zenon DEF SetLessThan, StrictSubsetOrdering\n<1>. HIDE DEF SubS\n<1>3. \\A T \\in SubS : P(T) BY ONLY <1>1, <1>2, WFInduction, IsaM(\"blast\")\n<1>. QED BY <1>3 DEF SubS\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The union of two finite sets is finite. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_Union ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW T, IsFiniteSet(T)\n PROVE /\\ IsFiniteSet(S \\cup T)\n /\\ Cardinality(S \\cup T) =\n Cardinality(S) + Cardinality(T) - Cardinality(S \\cap T)\n\n<1>. DEFINE P(A) == /\\ IsFiniteSet(S \\cup A)\n /\\ Cardinality(S \\cup A) =\n Cardinality(S) + Cardinality(A) - Cardinality(S \\cap A)\n<1>1. P({}) BY FS_EmptySet, FS_CardinalityType\n<1>2. ASSUME NEW A \\in SUBSET T, IsFiniteSet(A), P(A), NEW x \\in T \\ A\n PROVE P(A \\cup {x})\n <2>1. IsFiniteSet(S \\cup (A \\cup {x})) BY P(A), FS_AddElement, Isa\n <2>. /\\ IsFiniteSet(S \\cup A)\n /\\ IsFiniteSet(S \\cap A)\n /\\ Cardinality(S) \\in Nat\n /\\ Cardinality(A) \\in Nat\n /\\ Cardinality(S \\cap A) \\in Nat\n BY P(A), FS_Subset, FS_CardinalityType, Zenon\n <2>2. Cardinality(A \\cup {x}) = Cardinality(A) + 1\n BY <1>2, FS_AddElement\n <2>3. CASE x \\in S\n <3>1. Cardinality(S \\cup (A \\cup {x})) = Cardinality(S \\cup A) BY <2>3, Zenon\n <3>2. Cardinality(S \\cap (A \\cup {x})) = Cardinality((S \\cap A) \\cup {x}) BY <2>3, Zenon\n <3>3. Cardinality(S \\cap (A \\cup {x})) = Cardinality(S \\cap A) + 1\n BY <3>2, <1>2, FS_AddElement, Zenon\n <3>. QED BY <3>1, <3>3, <2>2, <2>1, P(A)\n <2>4. CASE x \\notin S\n <3>1. Cardinality((S \\cup A) \\cup {x}) = Cardinality(S \\cup A) + 1\n BY <1>2, <2>4, FS_AddElement, Zenon\n <3>1a. Cardinality(S \\cup (A \\cup {x})) = Cardinality(S \\cup A) + 1 BY <3>1, Zenon\n <3>2. Cardinality(S \\cap (A \\cup {x})) = Cardinality(S \\cap A) BY <2>4, Zenon\n <3>. QED BY <3>1a, <3>2, <2>2, <2>1, P(A)\n <2>. QED BY <2>3, <2>4\n<1>. HIDE DEF P\n<1>. P(T) BY <1>1, <1>2, FS_Induction, IsaM(\"blast\")\n<1>. QED BY DEF P\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* Corollary: two majorities intersect. More precisely, any two subsets *)\n(* of a finite set U such that the sum of cardinalities of the subsets *)\n(* exceeds that of U must have non-empty intersection. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_MajoritiesIntersect ==\n ASSUME NEW U, NEW S, NEW T, IsFiniteSet(U),\n S \\subseteq U, T \\subseteq U,\n Cardinality(S) + Cardinality(T) > Cardinality(U)\n PROVE S \\cap T # {}\n\n<1>. /\\ IsFiniteSet(S)\n /\\ IsFiniteSet(T)\n /\\ Cardinality(S) \\in Nat\n /\\ Cardinality(T) \\in Nat\n /\\ Cardinality(U) \\in Nat\n /\\ Cardinality(S \\cap T) \\in Nat\n /\\ Cardinality(S \\cup T) <= Cardinality(U)\n BY FS_Subset, FS_CardinalityType, Zenon\n<1>1. Cardinality(S \\cup T) =\n Cardinality(S) + Cardinality(T) - Cardinality(S \\cap T)\n BY FS_Union, Zenon\n<1>2. Cardinality(S \\cap T) # 0 BY <1>1\n<1>3. QED BY <1>2, FS_EmptySet, Zenon\n\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The intersection of a finite set with an arbitrary set is finite. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\n\n\nTHEOREM FS_Intersection ==\n ASSUME NEW S, IsFiniteSet(S), NEW T\n PROVE /\\ IsFiniteSet(S \\cap T)\n /\\ IsFiniteSet(T \\cap S)\n /\\ Cardinality(S \\cap T) <= Cardinality(S)\n /\\ Cardinality(T \\cap S) <= Cardinality(S)\nBY FS_Subset\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The difference between a finite set and an arbitrary set is finite. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_Difference ==\n ASSUME NEW S, NEW T, IsFiniteSet(S)\n PROVE /\\ IsFiniteSet(S \\ T)\n /\\ Cardinality(S \\ T) = Cardinality(S) - Cardinality(S \\cap T)\n\n<1>. /\\ IsFiniteSet(S \\ T)\n /\\ IsFiniteSet(S \\cap T)\n /\\ Cardinality(S \\ T) \\in Nat\n /\\ Cardinality(S \\cap T) \\in Nat\n BY FS_Subset, FS_CardinalityType, Zenon\n<1>2. Cardinality(S \\ T) = Cardinality(S) - Cardinality(S \\cap T)\n <2>1. Cardinality(S) = Cardinality((S \\cap T) \\cup (S \\ T)) BY Zenon\n <2>2. Cardinality((S \\cap T) \\cup (S \\ T)) =\n Cardinality(S \\cap T) + Cardinality(S \\ T) - Cardinality((S \\cap T) \\cap (S \\ T))\n BY FS_Union, Zenon\n <2>3. Cardinality((S \\cap T) \\cap (S \\ T)) = 0 BY FS_EmptySet, Zenon\n <2>. QED BY <2>1, <2>2, <2>3\n<1>3. QED BY <1>2\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The union of a finite number of finite sets is finite. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_UNION ==\n ASSUME NEW S, IsFiniteSet(S), \\A T \\in S : IsFiniteSet(T)\n PROVE IsFiniteSet(UNION S)\n\n<1>. DEFINE P(U) == (\\A T \\in U : IsFiniteSet(T)) => IsFiniteSet(UNION U)\n<1>1. P({}) BY FS_EmptySet, Zenon\n<1>2. ASSUME NEW U \\in SUBSET S, P(U), NEW x \\in S \\ U\n PROVE P(U \\cup {x})\n BY <1>2, FS_Union, Isa\n<1>. HIDE DEF P\n<1>. P(S) BY <1>1, <1>2, FS_Induction, IsaM(\"blast\")\n<1>. QED BY DEF P\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The product of two finite sets is finite. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_Product ==\n ASSUME NEW S, IsFiniteSet(S),\n NEW T, IsFiniteSet(T)\n PROVE /\\ IsFiniteSet(S \\X T)\n /\\ Cardinality(S \\X T) = Cardinality(S) * Cardinality(T)\n\n<1>. DEFINE P(A) == /\\ IsFiniteSet(S \\X A)\n /\\ Cardinality(S \\X A) = Cardinality(S) * Cardinality(A)\n<1>1. P({})\n <2>1. /\\ S \\X {} = {}\n /\\ IsFiniteSet(S \\X {})\n /\\ Cardinality(S \\X {}) = 0\n /\\ Cardinality({}) = 0\n /\\ Cardinality(S) \\in Nat\n BY FS_EmptySet, FS_CardinalityType, Zenon\n <2>. QED BY <2>1\n<1>2. ASSUME NEW A \\in SUBSET T, IsFiniteSet(A), P(A), NEW x \\in T \\ A\n PROVE P(A \\cup {x})\n <2>. /\\ Cardinality(A) \\in Nat\n /\\ Cardinality(S) \\in Nat\n BY <1>2, FS_CardinalityType, FS_Subset\n <2>. DEFINE SX == { <<s,x>> : s \\in S }\n <2>1. /\\ IsFiniteSet(A \\cup {x})\n /\\ Cardinality(A \\cup {x}) = Cardinality(A) + 1\n BY <1>2, FS_AddElement, Zenon\n <2>2. S \\X (A \\cup {x}) = (S \\X A) \\cup SX\n BY <1>2\n <2>3. ExistsBijection(S, SX)\n <3>. DEFINE f == [s \\in S |-> <<s,x>>]\n <3>. f \\in Bijection(S, SX)\n BY DEF Bijection, Injection, IsInjective, Surjection\n <3>. QED BY DEF ExistsBijection\n <2>4. /\\ IsFiniteSet(SX)\n /\\ Cardinality(SX) = Cardinality(S)\n BY <2>3, FS_Bijection\n <2>5. /\\ IsFiniteSet(S \\X (A \\cup {x}))\n /\\ Cardinality(S \\X (A \\cup {x})) =\n Cardinality(S \\X A) + Cardinality(SX) - Cardinality((S \\X A) \\cap SX)\n BY <2>2, <2>4, P(A), FS_Union\n <2>6. (S \\X A) \\cap SX = {} BY <1>2\n <2>7. Cardinality((S \\X A) \\cap SX) = 0 BY <2>6, FS_EmptySet\n <2>. QED BY <2>1, <2>5, <2>4, <2>7, P(A)\n<1>. HIDE DEF P\n<1>. P(T) BY <1>1, <1>2, FS_Induction, IsaM(\"blast\")\n<1>. QED BY DEF P\n\n\n\n(***************************************************************************)\n(* `. .' *)\n(* *)\n(* The powerset of a finite set is finite. *)\n(* *)\n(* `. .' *)\n(***************************************************************************)\nTHEOREM FS_SUBSET ==\n ASSUME NEW S, IsFiniteSet(S)\n PROVE /\\ IsFiniteSet(SUBSET S)\n /\\ Cardinality(SUBSET S) = 2^Cardinality(S)\n\n<1>. DEFINE P(A) == /\\ IsFiniteSet(SUBSET A)\n /\\ Cardinality(SUBSET A) = 2^Cardinality(A)\n<1>1. P({})\n <2>. /\\ IsFiniteSet({{}})\n /\\ Cardinality({{}}) = 1\n BY FS_Singleton, Zenon\n <2>. QED BY FS_EmptySet, TwoExpLemma, Zenon\n<1>2. ASSUME NEW A \\in SUBSET S, IsFiniteSet(A), P(A), NEW x \\in S \\ A\n PROVE P(A \\cup {x})\n <2>. DEFINE Ax == {B \\cup {x} : B \\in SUBSET A}\n <2>1. Cardinality(A \\cup {x}) = Cardinality(A) + 1 BY <1>2, FS_AddElement, Zenon\n <2>2. 2^Cardinality(A \\cup {x}) = 2^Cardinality(A) + 2^Cardinality(A)\n BY <2>1, <1>2, FS_CardinalityType, TwoExpLemma, Zenon\n <2>3. SUBSET (A \\cup {x}) = (SUBSET A) \\cup Ax BY <1>2, Isa\n <2>4. ExistsBijection(SUBSET A, Ax)\n <3>. DEFINE f == [B \\in SUBSET A |-> B \\cup {x}]\n <3>1. f \\in Injection(SUBSET A, Ax)\n <4>1. f \\in [SUBSET A -> Ax]\n BY Zenon\n <4>2. ASSUME NEW B \\in SUBSET A, NEW C \\in SUBSET A, f[B] = f[C]\n PROVE B = C\n BY <4>2, <1>2, Zenon\n <4>. QED BY <4>1, <4>2, Fun_IsInj, Zenon\n <3>2. f \\in Surjection(SUBSET A, Ax) BY Zenon DEF Surjection\n <3>3. f \\in Bijection(SUBSET A, Ax)\n BY <3>1, <3>2 DEF Bijection\n <3>. QED BY <3>3, Zenon DEF ExistsBijection\n <2>5. /\\ IsFiniteSet(Ax)\n /\\ Cardinality(Ax) = Cardinality(SUBSET A)\n BY <2>4, P(A), FS_Bijection\n <2>6. /\\ IsFiniteSet(SUBSET (A \\cup {x}))\n /\\ Cardinality(SUBSET (A \\cup {x})) =\n Cardinality(SUBSET A) + Cardinality(Ax) - Cardinality((SUBSET A) \\cap Ax)\n BY <2>3, <2>5, P(A), FS_Union\n <2>7. (SUBSET A) \\cap Ax = {} BY <1>2\n <2>8. Cardinality((SUBSET A) \\cap Ax) = 0 BY <2>7, FS_EmptySet\n <2>. QED BY <2>2, <2>5, <2>6, <2>8, P(A), FS_CardinalityType\n<1>. HIDE DEF P\n<1>. P(S) BY <1>1, <1>2, FS_Induction, IsaM(\"blast\")\n<1>. QED BY DEF P\n\n\n=============================================================================\n\\* Created Fri Oct 05 15:04:18 PDT 2012 by tomr\n", "NaturalsInduction": "------------------------- MODULE NaturalsInduction --------------------------\n(***************************************************************************)\n(* This module contains useful theorems for inductive proofs and recursive *)\n(* definitions over the naturals. *)\n(* *)\n(* Some of the statements of the theorems are decomposed in terms of *)\n(* definitions. This is done for two reasons: *)\n(* *)\n(* - It makes it easier for the backends to instantiate the theorems *)\n(* when those definitions are not expanded. *)\n(* *)\n(* - It can be convenient when writing proofs to use those definitions *)\n(* rather than having to write out their expansions. *)\n(***************************************************************************)\nEXTENDS Integers\n\n(***************************************************************************)\n(* The following is the simple statement of inductions over the naturals. *)\n(* For predicates P defined by a moderately complex operator, it is often *)\n(* useful to hide the operator definition before using this theorem. That *)\n(* is, you first define a suitable operator P (not necessarily by that *)\n(* name), prove the two hypotheses of the theorem, and then hide the *)\n(* definition of P when using the theorem. *)\n(***************************************************************************)\nTHEOREM NatInduction ==\n ASSUME NEW P(_),\n P(0),\n \\A n \\in Nat : P(n) => P(n+1)\n PROVE \\A n \\in Nat : P(n)\n\n(***************************************************************************)\n(* A useful corollary of NatInduction *)\n(***************************************************************************)\nTHEOREM DownwardNatInduction ==\n ASSUME NEW P(_), NEW m \\in Nat, P(m),\n \\A n \\in 1 .. m : P(n) => P(n-1)\n PROVE P(0)\n\n(***************************************************************************)\n(* The following theorem expresses a stronger induction principle, *)\n(* also known as course-of-values induction, where the induction *)\n(* hypothesis is available for all strictly smaller natural numbers. *)\n(***************************************************************************)\nTHEOREM GeneralNatInduction ==\n ASSUME NEW P(_),\n \\A n \\in Nat : (\\A m \\in 0..(n-1) : P(m)) => P(n)\n PROVE \\A n \\in Nat : P(n)\n\n(***************************************************************************)\n(* The following theorem expresses the ``least-number principle'': *)\n(* if P(n) is true for some natural number n then there is a *)\n(* smallest natural number for which P is true. It could be derived in *)\n(* module WellFoundedInduction as a corollary of the fact that the natural *)\n(* numbers are well ordered, but we give a direct proof. *)\n(***************************************************************************)\nTHEOREM SmallestNatural ==\n ASSUME NEW P(_), NEW n \\in Nat, P(n)\n PROVE \\E m \\in Nat : /\\ P(m)\n /\\ \\A k \\in 0 .. m-1 : ~ P(k)\n\n(***************************************************************************)\n(* The following theorem says that a recursively defined function f over *)\n(* the natural numbers is well-defined if for every n \\in Nat the *)\n(* definition of f[n] depends only on arguments smaller than n. *)\n(***************************************************************************)\nTHEOREM RecursiveFcnOfNat ==\n ASSUME NEW Def(_,_),\n ASSUME NEW n \\in Nat, NEW g, NEW h,\n \\A i \\in 0..(n-1) : g[i] = h[i]\n PROVE Def(g, n) = Def(h, n)\n PROVE LET f[n \\in Nat] == Def(f, n)\n IN f = [n \\in Nat |-> Def(f, n)]\n\n(***************************************************************************)\n(* The following theorem NatInductiveDef is what you use to justify a *)\n(* function defined by primitive recursion over the naturals. *)\n(***************************************************************************)\nNatInductiveDefHypothesis(f, f0, Def(_,_)) ==\n (f = CHOOSE g : g = [i \\in Nat |-> IF i = 0 THEN f0 ELSE Def(g[i-1], i)])\nNatInductiveDefConclusion(f, f0, Def(_,_)) ==\n f = [i \\in Nat |-> IF i = 0 THEN f0 ELSE Def(f[i-1], i)]\n\nTHEOREM NatInductiveDef ==\n ASSUME NEW Def(_,_), NEW f, NEW f0,\n NatInductiveDefHypothesis(f, f0, Def)\n PROVE NatInductiveDefConclusion(f, f0, Def)\n\n(***************************************************************************)\n(* The following two theorems allow you to prove the type of a recursively *)\n(* defined function over the natural numbers. *)\n(***************************************************************************)\nTHEOREM RecursiveFcnOfNatType ==\n ASSUME NEW f, NEW S, NEW Def(_,_), f = [n \\in Nat |-> Def(f,n)],\n ASSUME NEW n \\in Nat, NEW g, \\A i \\in 0 .. n-1 : g[i] \\in S\n PROVE Def(g,n) \\in S\n PROVE f \\in [Nat -> S]\n\nTHEOREM NatInductiveDefType ==\n ASSUME NEW Def(_,_), NEW S, NEW f, NEW f0 \\in S,\n NatInductiveDefConclusion(f, f0, Def),\n f0 \\in S,\n \\A v \\in S, n \\in Nat \\ {0} : Def(v, n) \\in S\n PROVE f \\in [Nat -> S]\n\n(***************************************************************************)\n(* The following theorems show uniqueness of functions recursively defined *)\n(* over Nat. *)\n(***************************************************************************)\nTHEOREM RecursiveFcnOfNatUnique ==\n ASSUME NEW Def(_,_), NEW f, NEW g,\n f = [n \\in Nat |-> Def(f,n)],\n g = [n \\in Nat |-> Def(g,n)],\n ASSUME NEW n \\in Nat, NEW ff, NEW gg,\n \\A i \\in 0..(n-1) : ff[i] = gg[i]\n PROVE Def(ff, n) = Def(gg, n)\n PROVE f = g\n\nTHEOREM NatInductiveUnique ==\n ASSUME NEW Def(_,_), NEW f, NEW g, NEW f0,\n NatInductiveDefConclusion(f, f0, Def),\n NatInductiveDefConclusion(g, f0, Def)\n PROVE f = g\n\n(***************************************************************************)\n(* The following theorems are analogous to the preceding ones but for *)\n(* functions defined over intervals of natural numbers. *)\n(***************************************************************************)\n\nFiniteNatInductiveDefHypothesis(f, c, Def(_,_), m, n) ==\n (f = CHOOSE g : g = [i \\in m..n |-> IF i = m THEN c ELSE Def(g[i-1], i)])\nFiniteNatInductiveDefConclusion(f, c, Def(_,_), m, n) ==\n f = [i \\in m..n |-> IF i = m THEN c ELSE Def(f[i-1], i)]\n\nTHEOREM FiniteNatInductiveDef ==\n ASSUME NEW Def(_,_), NEW f, NEW c, NEW m \\in Nat, NEW n \\in Nat,\n FiniteNatInductiveDefHypothesis(f, c, Def, m, n)\n PROVE FiniteNatInductiveDefConclusion(f, c, Def, m, n)\n\nTHEOREM FiniteNatInductiveDefType ==\n ASSUME NEW S, NEW Def(_,_), NEW f, NEW c \\in S, NEW m \\in Nat, NEW n \\in Nat,\n FiniteNatInductiveDefConclusion(f, c, Def, m, n),\n \\A v \\in S, i \\in (m+1) .. n : Def(v,i) \\in S\n PROVE f \\in [m..n -> S]\n\nTHEOREM FiniteNatInductiveUnique ==\n ASSUME NEW Def(_,_), NEW f, NEW g, NEW c, NEW m \\in Nat, NEW n \\in Nat,\n FiniteNatInductiveDefConclusion(f, c, Def, m, n),\n FiniteNatInductiveDefConclusion(g, c, Def, m, n)\n PROVE f = g\n\n=============================================================================\n\n(***************************************************************************)\n(* The following example shows how this module is used. *)\n(***************************************************************************)\n\nfactorial[n \\in Nat] == IF n = 0 THEN 1 ELSE n * factorial[n-1]\n\nTHEOREM FactorialDefConclusion == NatInductiveDefConclusion(factorial, 1, LAMBDA v,n : n*v)\n<1>1. NatInductiveDefHypothesis(factorial, 1, LAMBDA v,n : n*v)\n BY DEF NatInductiveDefHypothesis, factorial\n<1>2. QED\n BY <1>1, NatInductiveDef\n\nTHEOREM FactorialDef == \\A n \\in Nat : factorial[n] = IF n = 0 THEN 1 ELSE n * factorial[n-1]\nBY FactorialDefConclusion DEF NatInductiveDefConclusion\n\nTHEOREM FactorialType == factorial \\in [Nat -> Nat]\n<1>1. \\A v \\in Nat, n \\in Nat \\ {0} : n * v \\in Nat\n OBVIOUS\n<1>2. QED\n BY <1>1, NatInductiveDefType, FactorialDefConclusion, Isa\n\n\n\\* Modification History\n\\* Last modified Thu Aug 22 15:29:20 CEST 2019 by merz\n\\* Last modified Tue Oct 15 12:06:48 CEST 2013 by shaolin\n\\* Last modified Sat Nov 26 08:49:59 CET 2011 by merz\n\\* Last modified Mon Nov 07 08:58:05 PST 2011 by lamport\n\\* Created Mon Oct 31 02:52:05 PDT 2011 by lamport\n", "NaturalsInduction_proofs": "---------------------- MODULE NaturalsInduction_proofs ----------------------\n(***************************************************************************)\n(* This module contains useful theorems for inductive proofs and recursive *)\n(* definitions over the naturals. *)\n(* *)\n(* Some of the statements of the theorems are decomposed in terms of *)\n(* definitions. This is done for two reasons: *)\n(* *)\n(* - It makes it easier for the backends to instantiate the theorems *)\n(* when those definitions are not expanded. *)\n(* *)\n(* - It can be convenient when writing proofs to use those definitions *)\n(* rather than having to write out their expansions. *)\n(***************************************************************************)\nEXTENDS Integers, TLAPS\n\n(***************************************************************************)\n(* The following is the simple statement of inductions over the naturals. *)\n(* For predicates P defined by a moderately complex operator, it is often *)\n(* useful to hide the operator definition before using this theorem. That *)\n(* is, you first define a suitable operator P (not necessarily by that *)\n(* name), prove the two hypotheses of the theorem, and then hide the *)\n(* definition of P when using the theorem. *)\n(***************************************************************************)\nTHEOREM NatInduction ==\n ASSUME NEW P(_),\n P(0),\n \\A n \\in Nat : P(n) => P(n+1)\n PROVE \\A n \\in Nat : P(n)\nBY IsaM(\"(intro natInduct, auto)\")\n\n(***************************************************************************)\n(* A useful corollary of NatInduction *)\n(***************************************************************************)\nTHEOREM DownwardNatInduction ==\n ASSUME NEW P(_), NEW m \\in Nat, P(m),\n \\A n \\in 1 .. m : P(n) => P(n-1)\n PROVE P(0)\n<1>. DEFINE Q(i) == i \\leq m => P(m-i)\n<1>1. Q(0) OBVIOUS\n<1>2. ASSUME NEW n \\in Nat, Q(n)\n PROVE Q(n+1)\n BY <1>2, n+1 <= m => m-n \\in 1 .. m\n<1>3. \\A n \\in Nat : Q(n) BY <1>1, <1>2, NatInduction, Isa\n<1>. QED BY <1>3, Isa\n\n(***************************************************************************)\n(* The following theorem expresses a stronger induction principle, *)\n(* also known as course-of-values induction, where the induction *)\n(* hypothesis is available for all strictly smaller natural numbers. *)\n(***************************************************************************)\nTHEOREM GeneralNatInduction ==\n ASSUME NEW P(_),\n \\A n \\in Nat : (\\A m \\in 0..(n-1) : P(m)) => P(n)\n PROVE \\A n \\in Nat : P(n)\n<1> DEFINE Q(n) == \\A m \\in 0..n : P(m)\n<1>1. Q(0) OBVIOUS\n<1>2. \\A n \\in Nat : Q(n) => Q(n+1) OBVIOUS\n<1>3. \\A n \\in Nat : Q(n) BY <1>1, <1>2, NatInduction, Isa\n<1>4. QED BY <1>3\n\n(***************************************************************************)\n(* The following theorem expresses the ``least-number principle'': *)\n(* if P(n) is true for some natural number n then there is a *)\n(* smallest natural number for which P is true. It could be derived in *)\n(* module WellFoundedInduction as a corollary of the fact that the natural *)\n(* numbers are well ordered, but we give a direct proof. *)\n(***************************************************************************)\nTHEOREM SmallestNatural ==\n ASSUME NEW P(_), NEW n \\in Nat, P(n)\n PROVE \\E m \\in Nat : /\\ P(m)\n /\\ \\A k \\in 0 .. m-1 : ~ P(k)\n<1>. DEFINE Q(k) == ~ P(k)\n<1>. SUFFICES ASSUME \\A m \\in Nat : P(m) => \\E k \\in 0 .. m-1 : P(k)\n PROVE \\A m \\in Nat : Q(m)\n OBVIOUS\n<1>1. ASSUME NEW l \\in Nat, \\A k \\in 0 .. l-1 : Q(k)\n PROVE Q(l)\n BY <1>1\n<1>. HIDE DEF Q\n<1>. QED BY ONLY <1>1, GeneralNatInduction, Isa\n\n(***************************************************************************)\n(* The following theorem says that a recursively defined function f over *)\n(* the natural numbers is well-defined if for every n \\in Nat the *)\n(* definition of f[n] depends only on arguments smaller than n. *)\n(***************************************************************************)\nTHEOREM RecursiveFcnOfNat ==\n ASSUME NEW Def(_,_),\n ASSUME NEW n \\in Nat, NEW g, NEW h,\n \\A i \\in 0..(n-1) : g[i] = h[i]\n PROVE Def(g, n) = Def(h, n)\n PROVE LET f[n \\in Nat] == Def(f, n)\n IN f = [n \\in Nat |-> Def(f, n)]\n<1>. SUFFICES \\E ff : ff = [n \\in Nat |-> Def(ff, n)]\n OBVIOUS\n (*************************************************************************)\n (* The strategy of the proof is to define a sequence F of approximations *)\n (* such that F[n] is a function with domain 0 .. n-1 that computes *)\n (* F[n][i] by applying the definition to the preceding approximation *)\n (* function F[n-1]. *)\n (*************************************************************************)\n<1>. DEFINE F[n \\in Nat] == [i \\in 0 .. n-1 |-> Def(F[n-1], i)]\n f[n \\in Nat] == F[n+1][n]\n\n (*************************************************************************)\n (* We first show that F itself is well-defined by diagonalization *)\n (* over functions that are defined over finite intervals of integers. *)\n (*************************************************************************)\n<1>1. F = [n \\in Nat |-> [i \\in 0 .. n-1 |-> Def(F[n-1], i)]]\n <2>. SUFFICES \\E FF : FF = [n \\in Nat |-> [i \\in 0 .. n-1 |-> Def(FF[n-1], i)]]\n BY Zenon\n <2>. DEFINE P(g,k) == g = [n \\in 0 .. k |-> [i \\in 0 .. n-1 |-> Def(g[n-1], i)]]\n G(k) == CHOOSE g : P(g,k)\n FF == [n \\in Nat |-> [i \\in 0 .. n-1 |-> G(n)[n][i] ]]\n <2>0. ASSUME NEW g, NEW k \\in Nat, P(g,k),\n NEW n \\in 0 .. k, NEW i \\in 0 .. n-1\n PROVE g[n][i] = Def(g[n-1], i)\n <3>. DEFINE gg == [m \\in 0 .. k |-> [j \\in 0 .. m-1 |-> Def(g[m-1], j)]]\n <3>1. gg[n][i] = Def(g[n-1],i) OBVIOUS\n <3>2. g = gg BY <2>0, Zenon\n <3>. QED BY <3>1, <3>2, Zenon\n <2>1. \\A k \\in Nat : \\E g : P(g,k)\n <3>. DEFINE Q(k) == \\E g : P(g,k)\n <3>. SUFFICES \\A k \\in Nat : Q(k) BY Zenon\n <3>1. Q(0)\n <4>. DEFINE g0 == [n \\in {0} |-> [i \\in {} |-> {}]]\n <4>1. P(g0, 0) BY Isa\n <4>. QED BY <4>1, Zenon\n <3>2. ASSUME NEW k \\in Nat, Q(k)\n PROVE Q(k+1)\n <4>1. PICK g : P(g,k) BY <3>2, Zenon\n <4>1a. ASSUME NEW n \\in 0 .. k, NEW i \\in 0 .. n-1\n PROVE g[n][i] = Def(g[n-1], i)\n BY <4>1, <2>0, Zenon\n <4>. DEFINE h == [n \\in 0 .. k+1 |-> [i \\in 0 .. n-1 |-> Def(g[n-1], i) ]]\n <4>2. h = [n \\in 0 .. k+1 |-> [i \\in 0 .. n-1 |-> Def(h[n-1], i)]]\n <5>. SUFFICES ASSUME NEW n \\in 0 .. k+1, NEW i \\in 0 .. n-1\n PROVE h[n][i] = Def(h[n-1], i)\n BY Zenon\n <5>1. h[n][i] = Def(g[n-1], i) OBVIOUS\n <5>2. ASSUME NEW j \\in 0 .. i-1\n PROVE g[n-1][j] = h[n-1][j]\n BY <4>1a\n <5>. HIDE DEF h\n <5>3. Def(g[n-1],i) = Def(h[n-1],i) BY <5>2\n <5>. QED BY <5>1, <5>3\n <4>. HIDE DEF h\n <4>. QED BY <4>2, Zenon\n <3>. HIDE DEF Q\n <3>. QED BY <3>1, <3>2, NatInduction, IsaM(\"blast\")\n <2>2. \\A k \\in Nat : P(G(k), k) BY <2>1, Zenon\n <2>3. \\A k \\in Nat : \\A l \\in 0 .. k : \\A i \\in 0 .. l-1 : \\A g,h :\n P(g,k) /\\ P(h,l) => g[l][i] = h[l][i]\n <3>. DEFINE Q(k) == \\A l \\in 0 .. k : \\A i \\in 0 .. l-1 : \\A g,h :\n P(g,k) /\\ P(h,l) => g[l][i] = h[l][i]\n <3>. SUFFICES \\A k \\in Nat : Q(k) BY Zenon\n <3>0. Q(0) OBVIOUS\n <3>1. ASSUME NEW k \\in Nat, Q(k)\n PROVE Q(k+1)\n <4>. HIDE DEF P\n <4>. SUFFICES ASSUME NEW l \\in 1 .. k+1, NEW i \\in 0 .. l-1, NEW g, NEW h,\n P(g,k+1), P(h,l)\n PROVE g[l][i] = h[l][i]\n OBVIOUS\n <4>1. /\\ g[l][i] = Def(g[l-1],i)\n /\\ h[l][i] = Def(h[l-1],i)\n BY <2>0\n <4>. DEFINE gg == [nn \\in 0 .. k |-> [ii \\in 0 .. nn-1 |-> Def(g[nn-1],ii)]]\n hh == [nn \\in 0 .. l-1 |-> [ii \\in 0 .. nn-1 |-> Def(h[nn-1],ii)]]\n <4>2. P(gg,k)\n <5>1. ASSUME NEW nn \\in 0 .. k, NEW j \\in 0 .. nn-1\n PROVE gg[nn-1] = g[nn-1]\n <6>1. gg[nn-1] = [ii \\in 0 .. nn-2 |-> Def(g[nn-2],ii)]\n OBVIOUS\n <6>2. g[nn-1] = [ii \\in 0 .. (nn-1)-1 |-> Def(g[(nn-1)-1],ii)]\n BY nn-1 \\in 0 .. k, nn-1 \\in 0 .. k+1, Zenon DEF P\n <6>. QED BY <6>1, <6>2\n <5>. QED BY <5>1, Zenon DEF P\n <4>3. P(hh,l-1)\n <5>1. ASSUME NEW nn \\in 0 .. l-1, NEW j \\in 0 .. nn-1\n PROVE hh[nn-1] = h[nn-1]\n <6>1. hh[nn-1] = [ii \\in 0 .. nn-2 |-> Def(h[nn-2],ii)]\n OBVIOUS\n <6>2. h[nn-1] = [ii \\in 0 .. (nn-1)-1 |-> Def(h[(nn-1)-1],ii)]\n BY nn-1 \\in 0 .. l-1, nn-1 \\in 0 .. l, Zenon DEF P\n <6>. QED BY <6>1, <6>2\n <5>. QED BY <5>1, Zenon DEF P\n <4>4. ASSUME NEW m \\in 0 .. i-1\n PROVE gg[l-1][m] = hh[l-1][m]\n <5>. HIDE DEF gg, hh\n <5>. QED BY <3>1, <4>2, <4>3, l-1 \\in 0 .. k, m \\in 0 .. (l-1)-1\n <4>5. \\A m \\in 0 .. i-1 : g[l-1][m] = gg[l-1][m] BY <2>0\n <4>6. \\A m \\in 0 .. i-1 : h[l-1][m] = hh[l-1][m] BY <2>0\n <4>7. \\A m \\in 0 .. i-1 : g[l-1][m] = h[l-1][m] BY <4>4, <4>5, <4>6, Zenon\n <4>8. Def(g[l-1],i) = Def(h[l-1],i) BY <4>7\n <4>. QED BY <4>8, <2>0\n <3>. HIDE DEF Q\n <3>. QED BY <3>0, <3>1, NatInduction, IsaM(\"blast\")\n <2>4. FF = [n \\in Nat |-> [i \\in 0 .. n-1 |-> Def(FF[n-1], i)]]\n <3>. HIDE DEF G\n <3>. SUFFICES ASSUME NEW k \\in Nat, NEW i \\in 0 .. k-1\n PROVE FF[k][i] = Def(FF[k-1], i)\n BY Zenon\n <3>1. FF[k][i] = G(k)[k][i] OBVIOUS\n <3>2. G(k)[k][i] = Def(G(k)[k-1], i) BY <2>2\n <3>. HIDE DEF P\n <3>3. \\A j \\in 0 .. i-1 : G(k)[k-1][j] = FF[k-1][j] BY <2>2, <2>3 DEF G\n <3>. HIDE DEF FF\n <3>4. Def(G(k)[k-1], i) = Def(FF[k-1], i) BY <3>3\n <3>. QED BY <3>1, <3>2, <3>4\n <2>. QED BY <2>4, Zenon\n\n<1>. HIDE DEF F \\* from now on, use step <1>1 rather than the definition\n\n (*************************************************************************)\n (* The following step is a trivial consequence of <1>1 but the backend *)\n (* provers are currently unable to prove it directly. *)\n (*************************************************************************)\n<1>2. ASSUME NEW n \\in Nat, NEW i \\in 0 .. n-1\n PROVE F[n][i] = Def(F[n-1], i)\n <2>. DEFINE G == [m \\in Nat |-> [j \\in 0 .. m-1 |-> Def(F[m-1],j)]]\n <2>1. G[n][i] = Def(F[n-1],i) OBVIOUS\n <2>2. F = G BY <1>1, Zenon\n <2>. QED BY <2>1, <2>2, Zenon\n\n (*************************************************************************)\n (* Any two approximations F[n] and F[m] agree for arguments where they *)\n (* are both defined. *)\n (*************************************************************************)\n<1>. DEFINE P(n) == \\A m \\in 0 .. n : \\A i \\in 0 .. m-1 : F[n][i] = F[m][i]\n<1>3. \\A n \\in Nat : P(n)\n <2>1. ASSUME NEW n \\in Nat, \\A k \\in 0 .. n-1 : P(k)\n PROVE P(n)\n <3>. SUFFICES ASSUME NEW m \\in 0 .. n, NEW i \\in 0 .. m-1\n PROVE F[n][i] = F[m][i]\n OBVIOUS\n <3>2. CASE m = n BY <3>2\n <3>3. CASE n = 0 BY <3>3, SMT\n <3>4. CASE 0 < n /\\ m \\in 0 .. n-1\n <4>1. F[n][i] = Def(F[n-1],i) BY <1>2\n <4>2. \\A j \\in 0 .. i-1 : F[n-1][j] = F[m-1][j] BY <2>1, <3>4\n <4>3. Def(F[n-1],i) = Def(F[m-1],i) BY <4>2\n <4>4. Def(F[m-1],i) = F[m][i] BY <1>2\n <4>. QED BY <4>1, <4>3, <4>4\n <3>. QED BY <3>2, <3>3, <3>4, SMT\n <2>. HIDE DEF P\n <2>. QED BY <2>1, GeneralNatInduction, Blast\n\n (*************************************************************************)\n (* The assertion follows immediately from the two preceding steps. *)\n (*************************************************************************)\n<1>4. f = [n \\in Nat |-> Def(f,n)]\n <2>. SUFFICES ASSUME NEW n \\in Nat\n PROVE f[n] = Def(f,n)\n BY Zenon\n <2>1. f[n] = Def(F[n], n) BY <1>2\n <2>2. \\A i \\in 0 .. n-1 : F[n][i] = f[i] BY <1>3\n <2>3. Def(F[n],n) = Def(f,n) BY <2>2, Zenon\n <2>. QED BY <2>1, <2>3\n\n<1>. QED BY <1>4, Zenon\n\n\n(***************************************************************************)\n(* The following theorem NatInductiveDef is what you use to justify a *)\n(* function defined by primitive recursion over the naturals. *)\n(***************************************************************************)\nNatInductiveDefHypothesis(f, f0, Def(_,_)) ==\n (f = CHOOSE g : g = [i \\in Nat |-> IF i = 0 THEN f0 ELSE Def(g[i-1], i)])\nNatInductiveDefConclusion(f, f0, Def(_,_)) ==\n f = [i \\in Nat |-> IF i = 0 THEN f0 ELSE Def(f[i-1], i)]\n\nTHEOREM NatInductiveDef ==\n ASSUME NEW Def(_,_), NEW f, NEW f0,\n NatInductiveDefHypothesis(f, f0, Def)\n PROVE NatInductiveDefConclusion(f, f0, Def)\n<1>. DEFINE PRDef(g,n) == IF n = 0 THEN f0 ELSE Def(g[n-1], n)\n ff[n \\in Nat] == PRDef(ff,n)\n<1>1. ASSUME NEW n \\in Nat, NEW g, NEW h,\n \\A i \\in 0 .. n-1 : g[i] = h[i]\n PROVE PRDef(g,n) = PRDef(h,n)\n BY <1>1, Z3\n<1>. HIDE DEF PRDef\n<1>2. ff = [n \\in Nat |-> PRDef(ff,n)] BY <1>1, RecursiveFcnOfNat, Isa\n<1>. USE DEF PRDef\n<1>3. ff = f BY DEF NatInductiveDefHypothesis, Zenon\n<1>. HIDE DEF ff\n<1>. QED BY <1>2, <1>3 DEF NatInductiveDefConclusion\n\n(***************************************************************************)\n(* The following two theorems allow you to prove the type of a recursively *)\n(* defined function over the natural numbers. *)\n(***************************************************************************)\nTHEOREM RecursiveFcnOfNatType ==\n ASSUME NEW f, NEW S, NEW Def(_,_), f = [n \\in Nat |-> Def(f,n)],\n ASSUME NEW n \\in Nat, NEW g, \\A i \\in 0 .. n-1 : g[i] \\in S\n PROVE Def(g,n) \\in S\n PROVE f \\in [Nat -> S]\n<1>1. SUFFICES \\A n \\in Nat : f[n] \\in S\n OBVIOUS\n<1>2. ASSUME NEW n \\in Nat, \\A i \\in 0 .. n-1 : f[i] \\in S\n PROVE f[n] \\in S\n BY <1>2, Zenon\n<1>. QED BY <1>2, GeneralNatInduction, Isa\n\nTHEOREM NatInductiveDefType ==\n ASSUME NEW Def(_,_), NEW S, NEW f, NEW f0 \\in S,\n NatInductiveDefConclusion(f, f0, Def),\n f0 \\in S,\n \\A v \\in S, n \\in Nat \\ {0} : Def(v, n) \\in S\n PROVE f \\in [Nat -> S]\n<1>. USE DEF NatInductiveDefConclusion\n<1> SUFFICES \\A n \\in Nat : f[n] \\in S\n OBVIOUS\n<1>1. f[0] \\in S OBVIOUS\n<1>2. ASSUME NEW n \\in Nat, f[n] \\in S\n PROVE f[n+1] \\in S\n <2>1. /\\ n+1 \\in Nat \\ {0}\n /\\ (n+1)-1 = n\n OBVIOUS\n <2>. QED BY <2>1, <1>2\n<1>. QED BY <1>1, <1>2, NatInduction, Isa\n\n(***************************************************************************)\n(* The following theorems show uniqueness of functions recursively defined *)\n(* over Nat. *)\n(***************************************************************************)\nTHEOREM RecursiveFcnOfNatUnique ==\n ASSUME NEW Def(_,_), NEW f, NEW g,\n f = [n \\in Nat |-> Def(f,n)],\n g = [n \\in Nat |-> Def(g,n)],\n ASSUME NEW n \\in Nat, NEW ff, NEW gg,\n \\A i \\in 0..(n-1) : ff[i] = gg[i]\n PROVE Def(ff, n) = Def(gg, n)\n PROVE f = g\n<1>1. SUFFICES \\A n \\in Nat : f[n] = g[n]\n OBVIOUS\n<1>2. ASSUME NEW n \\in Nat, \\A i \\in 0 .. n-1 : f[i] = g[i]\n PROVE f[n] = g[n]\n <2>1. Def(f,n) = Def(g,n) BY <1>2\n <2>. QED BY <2>1, Zenon\n<1>. QED\n BY <1>2, GeneralNatInduction, Isa\n\nTHEOREM NatInductiveUnique ==\n ASSUME NEW Def(_,_), NEW f, NEW g, NEW f0,\n NatInductiveDefConclusion(f, f0, Def),\n NatInductiveDefConclusion(g, f0, Def)\n PROVE f = g\n<1>. USE DEF NatInductiveDefConclusion\n<1>1. SUFFICES \\A n \\in Nat : f[n] = g[n]\n OBVIOUS\n<1>2. f[0] = g[0] OBVIOUS\n<1>3. ASSUME NEW n \\in Nat, f[n] = g[n]\n PROVE f[n+1] = g[n+1]\n BY <1>3\n<1>. QED\n BY <1>2, <1>3, NatInduction, Isa\n\n(***************************************************************************)\n(* The following theorems are analogous to the preceding ones but for *)\n(* functions defined over intervals of natural numbers. *)\n(***************************************************************************)\n\nFiniteNatInductiveDefHypothesis(f, c, Def(_,_), m, n) ==\n (f = CHOOSE g : g = [i \\in m..n |-> IF i = m THEN c ELSE Def(g[i-1], i)])\nFiniteNatInductiveDefConclusion(f, c, Def(_,_), m, n) ==\n f = [i \\in m..n |-> IF i = m THEN c ELSE Def(f[i-1], i)]\n\nTHEOREM FiniteNatInductiveDef ==\n ASSUME NEW Def(_,_), NEW f, NEW c, NEW m \\in Nat, NEW n \\in Nat,\n FiniteNatInductiveDefHypothesis(f, c, Def, m, n)\n PROVE FiniteNatInductiveDefConclusion(f, c, Def, m, n)\n<1>. DEFINE PRDef(g,i) == IF i <= m THEN c ELSE Def(g[i-1], i)\n ff[i \\in Nat] == PRDef(ff,i)\n gg == [i \\in m..n |-> ff[i]]\n<1>1. ASSUME NEW i \\in Nat, NEW g, NEW h,\n \\A j \\in 0 .. i-1 : g[j] = h[j]\n PROVE PRDef(g,i) = PRDef(h,i)\n BY <1>1, Z3\n<1>. HIDE DEF PRDef\n<1>2. ff = [i \\in Nat |-> PRDef(ff,i)]\n BY <1>1, RecursiveFcnOfNat, Isa\n<1>. HIDE DEF ff\n<1>. USE DEF PRDef\n<1>3. gg = [i \\in m..n |-> IF i=m THEN c ELSE Def(gg[i-1],i)]\n BY <1>2, Z3\n<1>. HIDE DEF gg\n<1>. QED\n BY <1>3, Zenon DEF FiniteNatInductiveDefHypothesis, FiniteNatInductiveDefConclusion\n\nTHEOREM FiniteNatInductiveDefType ==\n ASSUME NEW S, NEW Def(_,_), NEW f, NEW c \\in S, NEW m \\in Nat, NEW n \\in Nat,\n FiniteNatInductiveDefConclusion(f, c, Def, m, n),\n \\A v \\in S, i \\in (m+1) .. n : Def(v,i) \\in S\n PROVE f \\in [m..n -> S]\n<1>. USE DEF FiniteNatInductiveDefConclusion\n<1>. DEFINE P(i) == i \\in m..n => f[i] \\in S\n<1>1. SUFFICES \\A i \\in Nat : P(i)\n OBVIOUS\n<1>2. P(0)\n OBVIOUS\n<1>3. ASSUME NEW i \\in Nat, P(i)\n PROVE P(i+1)\n BY <1>3\n<1>. QED\n BY <1>2, <1>3, NatInduction, Isa\n\nTHEOREM FiniteNatInductiveUnique ==\n ASSUME NEW Def(_,_), NEW f, NEW g, NEW c, NEW m \\in Nat, NEW n \\in Nat,\n FiniteNatInductiveDefConclusion(f, c, Def, m, n),\n FiniteNatInductiveDefConclusion(g, c, Def, m, n)\n PROVE f = g\n<1>. USE DEF FiniteNatInductiveDefConclusion\n<1>. DEFINE P(i) == i \\in m..n => f[i] = g[i]\n<1>1. SUFFICES \\A i \\in Nat : P(i)\n BY m..n \\subseteq Nat\n<1>2. P(0)\n OBVIOUS\n<1>3. ASSUME NEW i \\in Nat, P(i)\n PROVE P(i+1)\n BY <1>3\n<1>. QED\n BY <1>2, <1>3, NatInduction, Isa\n\n=============================================================================\n\n(***************************************************************************)\n(* The following example shows how this module is used. *)\n(***************************************************************************)\n\nfactorial[n \\in Nat] == IF n = 0 THEN 1 ELSE n * factorial[n-1]\n\nTHEOREM FactorialDefConclusion == NatInductiveDefConclusion(factorial, 1, LAMBDA v,n : n*v)\n<1>1. NatInductiveDefHypothesis(factorial, 1, LAMBDA v,n : n*v)\n BY Zenon DEF NatInductiveDefHypothesis, factorial\n<1>2. QED\n BY <1>1, NatInductiveDef, Zenon\n\nTHEOREM FactorialDef == \\A n \\in Nat : factorial[n] = IF n = 0 THEN 1 ELSE n * factorial[n-1]\nBY FactorialDefConclusion DEF NatInductiveDefConclusion\n\nTHEOREM FactorialType == factorial \\in [Nat -> Nat]\n<1>1. \\A v \\in Nat, n \\in Nat \\ {0} : n * v \\in Nat\n OBVIOUS\n<1>2. QED\n BY <1>1, NatInductiveDefType, FactorialDefConclusion, Isa\n\n\\* Modification History\n\\* Last modified Fri Jun 13 13:59:36 CEST 2025 by merz\n\\* Last modified Tue Oct 15 12:06:48 CEST 2013 by shaolin\n\\* Last modified Sat Nov 26 08:49:59 CET 2011 by merz\n\\* Last modified Mon Nov 07 08:58:05 PST 2011 by lamport\n\\* Created Mon Oct 31 02:52:05 PDT 2011 by lamport\n", "SequenceTheorems": "-------------------------- MODULE SequenceTheorems --------------------------\n(***************************************************************************)\n(* This module contains theorems about operations on sequences. *)\n(***************************************************************************)\nEXTENDS Sequences, Functions\nLOCAL INSTANCE NaturalsInduction\nLOCAL INSTANCE TLAPS\n\n\n(***************************************************************************)\n(* Elementary properties about Seq(S) *)\n(***************************************************************************)\n\nLEMMA SeqDef == \\A S : Seq(S) = UNION {[1..n -> S] : n \\in Nat}\n\nTHEOREM SeqMonotonic ==\n ASSUME NEW S, NEW T, NEW seq \\in Seq(S),\n S \\subseteq T\n PROVE seq \\in Seq(T)\n\nTHEOREM ElementOfSeq ==\n ASSUME NEW S, NEW seq \\in Seq(S),\n NEW n \\in 1..Len(seq)\n PROVE seq[n] \\in S\n\nTHEOREM EmptySeq ==\n ASSUME NEW S\n PROVE /\\ << >> \\in Seq(S)\n /\\ Len(<< >>) = 0\n /\\ \\A seq \\in Seq(S) : (seq # << >>) => (Len(seq) \\in Nat \\ {0})\n\nTHEOREM LenProperties ==\n ASSUME NEW S, NEW seq \\in Seq(S)\n PROVE /\\ Len(seq) \\in Nat\n /\\ seq \\in [1..Len(seq) -> S]\n /\\ DOMAIN seq = 1 .. Len(seq)\n\nTHEOREM IsASeq ==\n ASSUME NEW n \\in Nat, NEW e(_), NEW S,\n \\A i \\in 1..n : e(i) \\in S\n PROVE [i \\in 1..n |-> e(i)] \\in Seq(S)\n\nTHEOREM ExceptSeq ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW i \\in 1 .. Len(seq), NEW e \\in S\n PROVE /\\ [seq EXCEPT ![i] = e] \\in Seq(S)\n /\\ Len([seq EXCEPT ![i] = e]) = Len(seq)\n /\\ \\A j \\in 1 .. Len(seq) : [seq EXCEPT ![i] = e][j] = IF j=i THEN e ELSE seq[j]\n\nTHEOREM SeqEqual ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S),\n Len(s) = Len(t), \\A i \\in 1 .. Len(s) : s[i] = t[i]\n PROVE s = t\n\n(**************************************************************************)\n(* Properties of concatenation (\\o) *)\n(**************************************************************************)\n\nTHEOREM ConcatProperties ==\n ASSUME NEW S, NEW s1 \\in Seq(S), NEW s2 \\in Seq(S)\n PROVE /\\ s1 \\o s2 \\in Seq(S)\n /\\ Len(s1 \\o s2) = Len(s1) + Len(s2)\n /\\ \\A i \\in 1 .. Len(s1) + Len(s2) : (s1 \\o s2)[i] =\n IF i <= Len(s1) THEN s1[i] ELSE s2[i - Len(s1)]\n\nTHEOREM ConcatEmptySeq ==\n ASSUME NEW S, NEW seq \\in Seq(S)\n PROVE /\\ seq \\o << >> = seq\n /\\ << >> \\o seq = seq\n\nTHEOREM ConcatEqualIffEmpty ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S)\n PROVE /\\ s \\o t = s <=> t = << >>\n /\\ s \\o t = t <=> s = << >>\n /\\ s \\o t = << >> <=> s = << >> /\\ t = << >>\n\nTHEOREM ConcatAssociative ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S), NEW u \\in Seq(S)\n PROVE (s \\o t) \\o u = s \\o (t \\o u)\n\n(****************************************************************************)\n(* Intentionally not written as an ASSUME ... PROVE such that the conjuncts *)\n(* can be named individually. *)\n(****************************************************************************)\nTHEOREM ConcatSimplifications ==\n /\\ idRight:: \\A S : \\A s,t \\in Seq(S) : s \\o t = s => t = <<>>\n /\\ idLeft:: \\A S : \\A s,t \\in Seq(S) : s \\o t = t => s = <<>>\n /\\ concIsEmpty:: \\A S : \\A s,t \\in Seq(S) : s \\o t = <<>> => s = <<>> /\\ t = <<>>\n /\\ cancelLeft:: \\A S : \\A s,t,v \\in Seq(S) : s \\o t = s \\o v => t = v\n /\\ cancelRight:: \\A S : \\A s,t,v \\in Seq(S) : s \\o v = t \\o v => s = t\n\n(***************************************************************************)\n(* SubSeq, Head and Tail *)\n(***************************************************************************)\n\nTHEOREM SubSeqProperties ==\n ASSUME NEW S, NEW s, NEW m \\in Int, NEW n \\in Int,\n \\A i \\in m .. n : s[i] \\in S\n PROVE /\\ SubSeq(s,m,n) \\in Seq(S)\n /\\ Len(SubSeq(s,m,n)) = IF m<=n THEN n-m+1 ELSE 0\n /\\ \\A i \\in 1 .. n-m+1 : SubSeq(s,m,n)[i] = s[m+i-1]\n\nTHEOREM SubSeqEmpty ==\n ASSUME NEW s, NEW m \\in Int, NEW n \\in Int, n < m\n PROVE SubSeq(s,m,n) = << >>\n\nTHEOREM SubSeqRange ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW m \\in 1 .. Len(s), NEW n \\in 1 .. Len(s)\n PROVE Range(SubSeq(s,m,n)) \\subseteq Range(s)\n\nTHEOREM HeadTailProperties ==\n ASSUME NEW S,\n NEW s \\in Seq(S), s # << >>\n PROVE /\\ Head(s) \\in S\n /\\ Head(s) \\in Range(s)\n /\\ Tail(s) \\in Seq(S)\n /\\ Len(Tail(s)) = Len(s)-1\n /\\ \\A i \\in 1 .. Len(Tail(s)) : Tail(s)[i] = s[i+1]\n /\\ Range(Tail(s)) \\subseteq Range(s)\n\nTHEOREM TailIsSubSeq ==\n ASSUME NEW S,\n NEW s \\in Seq(S), s # << >>\n PROVE Tail(s) = SubSeq(s, 2, Len(s))\n\nTHEOREM SubSeqRestrict ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW n \\in 0 .. Len(seq)\n PROVE SubSeq(seq, 1, n) = Restrict(seq, 1 .. n)\n\nTHEOREM HeadTailOfSubSeq ==\n ASSUME NEW S, NEW seq \\in Seq(S),\n NEW m \\in 1 .. Len(seq), NEW n \\in m .. Len(seq)\n PROVE /\\ Head(SubSeq(seq,m,n)) = seq[m]\n /\\ Tail(SubSeq(seq,m,n)) = SubSeq(seq, m+1, n)\n\nTHEOREM SubSeqRecursive ==\n ASSUME NEW S, NEW seq \\in Seq(S),\n NEW m \\in 1 .. Len(seq), NEW n \\in m .. Len(seq)\n PROVE /\\ SubSeq(seq, m, n) = << seq[m] >> \\o SubSeq(seq, m+1, n)\n /\\ SubSeq(seq, m, n) = SubSeq(seq, m, n-1) \\o << seq[n] >>\n\n(***************************************************************************)\n(* Subsequences of subsequences. *)\n(***************************************************************************)\nTHEOREM SubSeqOfSubSeq ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S),\n NEW i \\in 1 .. Len(s), NEW j \\in 1 .. Len(s),\n NEW m \\in 1 .. j-i+1, NEW n \\in 1 .. j-i+1\n PROVE SubSeq(SubSeq(s,i,j),m,n) = SubSeq(s, i+m-1, i+n-1)\n\n(*****************************************************************************)\n(* Adjacent subsequences can be concatenated to obtain a longer subsequence. *)\n(*****************************************************************************)\nTHEOREM ConcatAdjacentSubSeq ==\n ASSUME NEW S, NEW seq \\in Seq(S),\n NEW m \\in 1 .. Len(seq)+1,\n NEW k \\in m-1 .. Len(seq),\n NEW n \\in k .. Len(seq)\n PROVE SubSeq(seq, m, k) \\o SubSeq(seq, k+1, n) = SubSeq(seq, m, n)\n\n(***************************************************************************)\n(* Special cases of subsequences of concatenations. *)\n(***************************************************************************)\nTHEOREM SubSeqOfConcat1 ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S),\n NEW m \\in 1 .. Len(s), NEW n \\in 1 .. Len(s)\n PROVE SubSeq(s \\o t, m, n) = SubSeq(s, m, n)\n\nTHEOREM SubSeqOfConcat2 ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S),\n NEW m \\in Len(s)+1 .. Len(s)+Len(t), NEW n \\in Len(s)+1 .. Len(s)+Len(t)\n PROVE SubSeq(s \\o t, m, n) = SubSeq(t, m - Len(s), n - Len(s))\n\n(***************************************************************************)\n(* Theorems about Append *)\n(***************************************************************************)\n\nTHEOREM AppendProperties ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW elt \\in S\n PROVE /\\ Append(seq, elt) \\in Seq(S)\n /\\ Append(seq, elt) # << >>\n /\\ Len(Append(seq, elt)) = Len(seq)+1\n /\\ \\A i \\in 1.. Len(seq) : Append(seq, elt)[i] = seq[i]\n /\\ Append(seq, elt)[Len(seq)+1] = elt\n /\\ Range(Append(seq, elt)) = Range(seq) \\cup {elt}\n\nTHEOREM AppendIsConcat ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW elt \\in S\n PROVE Append(seq, elt) = seq \\o <<elt>>\n\nTHEOREM HeadTailAppend ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW elt\n PROVE /\\ Head(Append(seq, elt)) = IF seq = <<>> THEN elt ELSE Head(seq)\n /\\ Tail(Append(seq, elt)) = IF seq = <<>> THEN <<>> ELSE Append(Tail(seq), elt)\n\nTHEOREM AppendInjective ==\n ASSUME NEW S, NEW e \\in S, NEW s \\in Seq(S), NEW f \\in S, NEW t \\in Seq(S)\n PROVE Append(s,e) = Append(t,f) <=> s = t /\\ e = f\n\nTHEOREM SequenceEmptyOrAppend ==\n ASSUME NEW S, NEW seq \\in Seq(S), seq # << >>\n PROVE \\E s \\in Seq(S), elt \\in S : seq = Append(s, elt)\n\n(***************************************************************************)\n(* Inductive reasoning for sequences *)\n(***************************************************************************)\n\nTHEOREM SequencesInductionAppend ==\n ASSUME NEW P(_), NEW S,\n P(<< >>),\n \\A s \\in Seq(S), e \\in S : P(s) => P(Append(s,e))\n PROVE \\A seq \\in Seq(S) : P(seq)\n\nTHEOREM SequencesInductionTail ==\n ASSUME NEW S, NEW P(_),\n P(<< >>),\n \\A s \\in Seq(S) : (s # << >>) /\\ P(Tail(s)) => P(s)\n PROVE \\A s \\in Seq(S) : P(s)\n\n\n(***************************************************************************)\n(* RANGE OF SEQUENCE *)\n(***************************************************************************)\n\nTHEOREM RangeOfSeq ==\n ASSUME NEW S, NEW seq \\in Seq(S)\n PROVE Range(seq) \\in SUBSET S\n\nTHEOREM RangeEquality ==\n ASSUME NEW S, NEW seq \\in Seq(S)\n PROVE Range(seq) = { seq[i] : i \\in 1 .. Len(seq) }\n\nTHEOREM SeqOfRange ==\n ASSUME NEW S, NEW seq \\in Seq(S)\n PROVE seq \\in Seq(Range(seq))\n\n(* The range of the concatenation of two sequences is the union of the ranges *)\nTHEOREM RangeConcatenation ==\n ASSUME NEW S, NEW s1 \\in Seq(S), NEW s2 \\in Seq(S)\n PROVE Range(s1 \\o s2) = Range(s1) \\cup Range(s2)\n\n(***************************************************************************)\n(* Theorems about injective sequences, i.e. sequences without duplicates. *)\n(***************************************************************************)\n\nLEMMA EmptySingletonIsInjective ==\n /\\ IsInjective(<< >>)\n /\\ \\A e : IsInjective(<< e >>)\n\nLEMMA ConcatInjectiveSeq ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S)\n PROVE IsInjective(s \\o t) <=>\n IsInjective(s) /\\ IsInjective(t) /\\ Range(s) \\cap Range(t) = {}\n\nLEMMA AppendInjectiveSeq ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW e \\in S\n PROVE IsInjective(Append(seq,e)) <=> IsInjective(seq) /\\ e \\notin Range(seq)\n\nLEMMA TailInjectiveSeq ==\n ASSUME NEW S, NEW seq \\in Seq(S), IsInjective(seq), seq # << >>\n PROVE /\\ IsInjective(Tail(seq))\n /\\ Range(Tail(seq)) = Range(seq) \\ {Head(seq)}\n\n=============================================================================\n", "SequenceTheorems_proofs": "----------------------- MODULE SequenceTheorems_proofs ----------------------\n(***************************************************************************)\n(* This module contains the proofs for theorems about sequences and the *)\n(* corresponding operations. *)\n(***************************************************************************)\nEXTENDS Sequences, Functions\nLOCAL INSTANCE NaturalsInduction\nLOCAL INSTANCE TLAPS\n\n\n(***************************************************************************)\n(* Elementary properties about Seq(S) *)\n(***************************************************************************)\n\nLEMMA SeqDef == \\A S : Seq(S) = UNION {[1..n -> S] : n \\in Nat}\nOBVIOUS\n\nTHEOREM SeqMonotonic ==\n ASSUME NEW S, NEW T, NEW seq \\in Seq(S),\n S \\subseteq T\n PROVE seq \\in Seq(T)\nOBVIOUS\n\nTHEOREM ElementOfSeq ==\n ASSUME NEW S, NEW seq \\in Seq(S),\n NEW n \\in 1..Len(seq)\n PROVE seq[n] \\in S\nOBVIOUS\n\nTHEOREM EmptySeq ==\n ASSUME NEW S\n PROVE /\\ << >> \\in Seq(S)\n /\\ Len(<< >>) = 0\n /\\ \\A seq \\in Seq(S) : (seq # << >>) => (Len(seq) \\in Nat \\ {0})\nOBVIOUS\n\nTHEOREM LenProperties ==\n ASSUME NEW S, NEW seq \\in Seq(S)\n PROVE /\\ Len(seq) \\in Nat\n /\\ seq \\in [1..Len(seq) -> S]\n /\\ DOMAIN seq = 1 .. Len(seq)\nOBVIOUS\n\nTHEOREM IsASeq ==\n ASSUME NEW n \\in Nat, NEW e(_), NEW S,\n \\A i \\in 1..n : e(i) \\in S\n PROVE [i \\in 1..n |-> e(i)] \\in Seq(S)\nOBVIOUS\n\nTHEOREM ExceptSeq ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW i \\in 1 .. Len(seq), NEW e \\in S\n PROVE /\\ [seq EXCEPT ![i] = e] \\in Seq(S)\n /\\ Len([seq EXCEPT ![i] = e]) = Len(seq)\n /\\ \\A j \\in 1 .. Len(seq) : [seq EXCEPT ![i] = e][j] = IF j=i THEN e ELSE seq[j]\nOBVIOUS\n\nTHEOREM SeqEqual ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S),\n Len(s) = Len(t), \\A i \\in 1 .. Len(s) : s[i] = t[i]\n PROVE s = t\nOBVIOUS\n\n(**************************************************************************)\n(* Properties of concatenation (\\o) *)\n(**************************************************************************)\n\nTHEOREM ConcatProperties ==\n ASSUME NEW S, NEW s1 \\in Seq(S), NEW s2 \\in Seq(S)\n PROVE /\\ s1 \\o s2 \\in Seq(S)\n /\\ Len(s1 \\o s2) = Len(s1) + Len(s2)\n /\\ \\A i \\in 1 .. Len(s1) + Len(s2) : (s1 \\o s2)[i] =\n IF i <= Len(s1) THEN s1[i] ELSE s2[i - Len(s1)]\nOBVIOUS\n\nTHEOREM ConcatEmptySeq ==\n ASSUME NEW S, NEW seq \\in Seq(S)\n PROVE /\\ seq \\o << >> = seq\n /\\ << >> \\o seq = seq\nOBVIOUS\n\nTHEOREM ConcatEqualIffEmpty ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S)\n PROVE /\\ s \\o t = s <=> t = << >>\n /\\ s \\o t = t <=> s = << >>\n /\\ s \\o t = << >> <=> s = << >> /\\ t = << >>\nOBVIOUS\n\nTHEOREM ConcatAssociative ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S), NEW u \\in Seq(S)\n PROVE (s \\o t) \\o u = s \\o (t \\o u)\nOBVIOUS\n\n(****************************************************************************)\n(* Intentionally not written as an ASSUME ... PROVE such that the conjuncts *)\n(* can be named individually. *)\n(****************************************************************************)\nTHEOREM ConcatSimplifications ==\n /\\ idRight:: \\A S : \\A s,t \\in Seq(S) : s \\o t = s => t = <<>>\n /\\ idLeft:: \\A S : \\A s,t \\in Seq(S) : s \\o t = t => s = <<>>\n /\\ concIsEmpty:: \\A S : \\A s,t \\in Seq(S) : s \\o t = <<>> => s = <<>> /\\ t = <<>>\n /\\ cancelLeft:: \\A S : \\A s,t,v \\in Seq(S) : s \\o t = s \\o v => t = v\n /\\ cancelRight:: \\A S : \\A s,t,v \\in Seq(S) : s \\o v = t \\o v => s = t\n<1>1. ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S), NEW v \\in Seq(S),\n s \\o t = s \\o v\n PROVE t = v\n <2>1. Len(t) = Len(v)\n BY <1>1\n <2>2. ASSUME NEW i \\in 1 .. Len(t) PROVE t[i] = v[i]\n <3>1. t[i] = (s \\o t)[Len(s)+i]\n OBVIOUS\n <3>2. v[i] = (s \\o v)[Len(s)+i]\n BY <2>1\n <3>. QED BY <1>1, <3>1, <3>2\n <2>. QED BY <2>1, <2>2\n<1>2. ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S), NEW v \\in Seq(S),\n s \\o v = t \\o v\n PROVE s = t\n <2>1. Len(s) = Len(t)\n BY <1>2\n <2>2. ASSUME NEW i \\in 1 .. Len(s) PROVE s[i] = t[i]\n BY <1>2, <2>1\n <2>. QED BY <2>1, <2>2\n<1>. QED BY <1>1, <1>2, ConcatEqualIffEmpty\n\n(***************************************************************************)\n(* SubSeq, Head and Tail *)\n(***************************************************************************)\n\nTHEOREM SubSeqProperties ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW m \\in Int, NEW n \\in Int,\n m > n \\/ (1 <= m /\\ n <= Len(s)),\n \\A i \\in m .. n : s[i] \\in S\n PROVE /\\ SubSeq(s,m,n) \\in Seq(S)\n /\\ Len(SubSeq(s,m,n)) = IF m<=n THEN n-m+1 ELSE 0\n /\\ \\A i \\in 1 .. n-m+1 : SubSeq(s,m,n)[i] = s[m+i-1]\nOBVIOUS\n\nTHEOREM SubSeqEmpty ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW m \\in Int, NEW n \\in Int, n < m\n PROVE SubSeq(s,m,n) = << >>\nOBVIOUS\n\nTHEOREM SubSeqRange ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW m \\in 1 .. Len(s), NEW n \\in 1 .. Len(s)\n PROVE Range(SubSeq(s,m,n)) \\subseteq Range(s)\nBY DEF Range\n\nTHEOREM HeadTailProperties ==\n ASSUME NEW S,\n NEW s \\in Seq(S), s # << >>\n PROVE /\\ Head(s) \\in S\n /\\ Head(s) \\in Range(s)\n /\\ Tail(s) \\in Seq(S)\n /\\ Len(Tail(s)) = Len(s)-1\n /\\ \\A i \\in 1 .. Len(Tail(s)) : Tail(s)[i] = s[i+1]\n /\\ Range(Tail(s)) \\subseteq Range(s)\nBY DEF Range\n\nTHEOREM TailIsSubSeq ==\n ASSUME NEW S,\n NEW s \\in Seq(S), s # << >>\n PROVE Tail(s) = SubSeq(s, 2, Len(s))\nOBVIOUS\n\nTHEOREM SubSeqRestrict ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW n \\in 0 .. Len(seq)\n PROVE SubSeq(seq, 1, n) = Restrict(seq, 1 .. n)\nBY DEF Restrict\n\nTHEOREM HeadTailOfSubSeq ==\n ASSUME NEW S, NEW seq \\in Seq(S),\n NEW m \\in 1 .. Len(seq), NEW n \\in m .. Len(seq)\n PROVE /\\ Head(SubSeq(seq,m,n)) = seq[m]\n /\\ Tail(SubSeq(seq,m,n)) = SubSeq(seq, m+1, n)\nOBVIOUS\n\nTHEOREM SubSeqRecursive ==\n ASSUME NEW S, NEW seq \\in Seq(S),\n NEW m \\in 1 .. Len(seq), NEW n \\in m .. Len(seq)\n PROVE /\\ SubSeq(seq, m, n) = << seq[m] >> \\o SubSeq(seq, m+1, n)\n /\\ SubSeq(seq, m, n) = SubSeq(seq, m, n-1) \\o << seq[n] >>\nOBVIOUS\n\n(***************************************************************************)\n(* Subsequences of subsequences. *)\n(***************************************************************************)\nTHEOREM SubSeqOfSubSeq ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S),\n NEW i \\in 1 .. Len(s), NEW j \\in 1 .. Len(s),\n NEW m \\in 1 .. j-i+1, NEW n \\in 1 .. j-i+1\n PROVE SubSeq(SubSeq(s,i,j),m,n) = SubSeq(s, i+m-1, i+n-1)\nOBVIOUS\n\n(*****************************************************************************)\n(* Adjacent subsequences can be concatenated to obtain a longer subsequence. *)\n(*****************************************************************************)\nTHEOREM ConcatAdjacentSubSeq ==\n ASSUME NEW S, NEW seq \\in Seq(S),\n NEW m \\in 1 .. Len(seq)+1,\n NEW k \\in m-1 .. Len(seq),\n NEW n \\in k .. Len(seq)\n PROVE SubSeq(seq, m, k) \\o SubSeq(seq, k+1, n) = SubSeq(seq, m, n)\nOBVIOUS\n\n(***************************************************************************)\n(* Special cases of subsequences of concatenations. *)\n(***************************************************************************)\nTHEOREM SubSeqOfConcat1 ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S),\n NEW m \\in 1 .. Len(s), NEW n \\in 1 .. Len(s)\n PROVE SubSeq(s \\o t, m, n) = SubSeq(s, m, n)\nOBVIOUS\n\nTHEOREM SubSeqOfConcat2 ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S),\n NEW m \\in Len(s)+1 .. Len(s)+Len(t), NEW n \\in Len(s)+1 .. Len(s)+Len(t)\n PROVE SubSeq(s \\o t, m, n) = SubSeq(t, m - Len(s), n - Len(s))\nOBVIOUS\n\n(***************************************************************************)\n(* Theorems about Append *)\n(***************************************************************************)\n\nTHEOREM AppendProperties ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW elt \\in S\n PROVE /\\ Append(seq, elt) \\in Seq(S)\n /\\ Append(seq, elt) # << >>\n /\\ Len(Append(seq, elt)) = Len(seq)+1\n /\\ \\A i \\in 1.. Len(seq) : Append(seq, elt)[i] = seq[i]\n /\\ Append(seq, elt)[Len(seq)+1] = elt\n /\\ Range(Append(seq, elt)) = Range(seq) \\cup {elt}\nBY DEF Range\n\nTHEOREM AppendIsConcat ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW elt \\in S\n PROVE Append(seq, elt) = seq \\o <<elt>>\nOBVIOUS\n\nTHEOREM HeadTailAppend ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW elt\n PROVE /\\ Head(Append(seq, elt)) = IF seq = <<>> THEN elt ELSE Head(seq)\n /\\ Tail(Append(seq, elt)) = IF seq = <<>> THEN <<>> ELSE Append(Tail(seq), elt)\nBY seq \\in Seq(S \\union {elt})\n\nTHEOREM AppendInjective ==\n ASSUME NEW S, NEW e \\in S, NEW s \\in Seq(S), NEW f \\in S, NEW t \\in Seq(S)\n PROVE Append(s,e) = Append(t,f) <=> s = t /\\ e = f\nOBVIOUS\n\nTHEOREM SequenceEmptyOrAppend ==\n ASSUME NEW S, NEW seq \\in Seq(S), seq # << >>\n PROVE \\E s \\in Seq(S), elt \\in S : seq = Append(s, elt)\n<1>. DEFINE front == [i \\in 1 .. Len(seq)-1 |-> seq[i]]\n last == seq[Len(seq)]\n<1>1. /\\ front \\in Seq(S)\n /\\ Len(front) = Len(seq)-1\n /\\ last \\in S\n OBVIOUS\n<1>2. Len(seq) = Len(Append(front, last))\n OBVIOUS\n<1>3. ASSUME NEW i \\in 1 .. Len(seq) \n PROVE seq[i] = Append(front, last)[i]\n OBVIOUS\n<1>4. seq = Append(front, last)\n BY <1>1, <1>2, <1>3, SeqEqual\n<1>. QED BY <1>1, <1>4\n\n(***************************************************************************)\n(* Inductive reasoning for sequences *)\n(***************************************************************************)\n\nTHEOREM SequencesInductionAppend ==\n ASSUME NEW P(_), NEW S,\n P(<< >>),\n \\A s \\in Seq(S), e \\in S : P(s) => P(Append(s,e))\n PROVE \\A seq \\in Seq(S) : P(seq)\n<1>. DEFINE Q(n) == \\A seq \\in Seq(S) : Len(seq) = n => P(seq)\n<1>. SUFFICES \\A k \\in Nat : Q(k)\n OBVIOUS\n<1>1. Q(0)\n OBVIOUS\n<1>2. ASSUME NEW n \\in Nat, Q(n)\n PROVE Q(n+1)\n <2>. SUFFICES ASSUME NEW s \\in Seq(S), Len(s) = n+1\n PROVE P(s)\n OBVIOUS\n <2>. PICK seq \\in Seq(S), e \\in S : s = Append(seq,e) /\\ Len(seq) = n\n BY SequenceEmptyOrAppend\n <2>. QED BY <1>2\n<1>3. QED BY <1>1, <1>2, NatInduction, Isa\n\nTHEOREM SequencesInductionTail ==\n ASSUME NEW S, NEW P(_),\n P(<< >>),\n \\A s \\in Seq(S) : (s # << >>) /\\ P(Tail(s)) => P(s)\n PROVE \\A s \\in Seq(S) : P(s)\n<1>. DEFINE Q(n) == \\A s \\in Seq(S) : Len(s) = n => P(s)\n<1>. SUFFICES \\A k \\in Nat : Q(k)\n OBVIOUS\n<1>1. Q(0)\n OBVIOUS\n<1>2. ASSUME NEW n \\in Nat, Q(n)\n PROVE Q(n+1)\n <2>. SUFFICES ASSUME NEW s \\in Seq(S), Len(s) = n+1\n PROVE P(s)\n OBVIOUS\n <2>1. /\\ Tail(s) \\in Seq(S)\n /\\ Len(Tail(s)) = n\n OBVIOUS\n <2>. QED BY <2>1, <1>2, Zenon\n<1>3. QED BY <1>1, <1>2, NatInduction, Isa\n\n\n(***************************************************************************)\n(* RANGE OF SEQUENCE *)\n(***************************************************************************)\n\nTHEOREM RangeOfSeq ==\n ASSUME NEW S, NEW seq \\in Seq(S)\n PROVE Range(seq) \\in SUBSET S\nBY DEF Range\n\nTHEOREM RangeEquality ==\n ASSUME NEW S, NEW seq \\in Seq(S)\n PROVE Range(seq) = { seq[i] : i \\in 1 .. Len(seq) }\nBY DEF Range\n\nTHEOREM SeqOfRange ==\n ASSUME NEW S, NEW seq \\in Seq(S)\n PROVE seq \\in Seq(Range(seq))\nBY DEF Range\n\n(* The range of the concatenation of two sequences is the union of the ranges *)\nTHEOREM RangeConcatenation ==\n ASSUME NEW S, NEW s1 \\in Seq(S), NEW s2 \\in Seq(S)\n PROVE Range(s1 \\o s2) = Range(s1) \\cup Range(s2)\n<1>1. Range(s1) \\subseteq Range(s1 \\o s2)\n BY DEF Range\n<1>2. Range(s2) \\subseteq Range(s1 \\o s2)\n <2>1. SUFFICES ASSUME NEW i \\in 1 .. Len(s2)\n PROVE s2[i] \\in Range(s1 \\o s2)\n BY RangeEquality\n <2>2. /\\ Len(s1)+i \\in 1 .. Len(s1 \\o s2)\n /\\ (s1 \\o s2)[Len(s1)+i] = s2[i]\n OBVIOUS\n <2>. QED BY <2>2 DEF Range\n<1>3. Range(s1 \\o s2) \\subseteq Range(s1) \\cup Range(s2)\n BY DEF Range\n<1>. QED BY <1>1, <1>2, <1>3\n\n(***************************************************************************)\n(* Theorems about injective sequences, i.e. sequences without duplicates. *)\n(***************************************************************************)\n\nLEMMA EmptySingletonIsInjective ==\n /\\ IsInjective(<< >>)\n /\\ \\A e : IsInjective(<< e >>)\nBY DEF IsInjective\n\nLEMMA ConcatInjectiveSeq ==\n ASSUME NEW S, NEW s \\in Seq(S), NEW t \\in Seq(S)\n PROVE IsInjective(s \\o t) <=>\n IsInjective(s) /\\ IsInjective(t) /\\ Range(s) \\cap Range(t) = {}\n<1>. DEFINE st == s \\o t\n<1>1. ASSUME IsInjective(st) PROVE IsInjective(s)\n BY <1>1 DEF IsInjective\n<1>2. ASSUME IsInjective(st) PROVE IsInjective(t)\n <2>1. SUFFICES ASSUME NEW i \\in DOMAIN t, NEW j \\in DOMAIN t, t[i] = t[j]\n PROVE i = j\n BY DEF IsInjective\n <2>2. /\\ i + Len(s) \\in DOMAIN st\n /\\ j + Len(s) \\in DOMAIN st\n /\\ st[i+Len(s)] = st[j + Len(s)]\n BY <2>1\n <2>. QED BY <1>2, <2>2 DEF IsInjective\n<1>3. ASSUME IsInjective(st), NEW e \\in Range(s) \\cap Range(t)\n PROVE FALSE\n <2>1. PICK i \\in DOMAIN s : s[i] = e\n BY <1>3 DEF Range\n <2>2. PICK j \\in DOMAIN t : t[j] = e\n BY <1>3 DEF Range\n <2>3. /\\ i \\in DOMAIN st /\\ st[i] = e\n /\\ j+Len(s) \\in DOMAIN st /\\ st[j+Len(s)] = e\n BY <2>1, <2>2\n <2>. QED BY <1>3, <2>3 DEF IsInjective\n<1>4. ASSUME IsInjective(s), IsInjective(t), Range(s) \\cap Range(t) = {}\n PROVE IsInjective(st)\n BY <1>4 DEF IsInjective, Range\n<1>. QED BY <1>1, <1>2, <1>3, <1>4\n\nLEMMA AppendInjectiveSeq ==\n ASSUME NEW S, NEW seq \\in Seq(S), NEW e \\in S\n PROVE IsInjective(Append(seq,e)) <=> IsInjective(seq) /\\ e \\notin Range(seq)\n<1>. DEFINE app == Append(seq,e)\n<1>1. ASSUME IsInjective(app)\n PROVE IsInjective(seq)\n BY <1>1 DEF IsInjective\n<1>2. ASSUME IsInjective(app), e \\in Range(seq)\n PROVE FALSE\n <2>1. PICK i \\in DOMAIN seq : seq[i] = e\n BY <1>2 DEF Range\n <2>2. app[i] = e /\\ app[Len(seq)+1] = e\n BY <2>1\n <2>. QED\n BY <1>2, <2>2 DEF IsInjective\n<1>3. ASSUME IsInjective(seq), e \\notin Range(seq)\n PROVE IsInjective(app)\n BY <1>3 DEF IsInjective, Range\n<1>. QED BY <1>1, <1>2, <1>3\n\nLEMMA TailInjectiveSeq ==\n ASSUME NEW S, NEW seq \\in Seq(S), IsInjective(seq), seq # << >>\n PROVE /\\ IsInjective(Tail(seq))\n /\\ Range(Tail(seq)) = Range(seq) \\ {Head(seq)}\n<1>. DEFINE tl == Tail(seq)\n<1>1. IsInjective(tl)\n BY DEF IsInjective\n<1>2. ASSUME NEW e \\in Range(tl)\n PROVE e \\in Range(seq) \\ {Head(seq)}\n BY DEF Range, IsInjective\n<1>3. ASSUME NEW e \\in Range(seq) \\ {Head(seq)}\n PROVE e \\in Range(tl)\n <2>1. PICK i \\in 2 .. Len(seq) : seq[i] = e\n BY DEF Range\n <2>2. i-1 \\in DOMAIN tl /\\ tl[i-1] = e\n BY <2>1\n <2>. QED BY <2>2, Zenon DEF Range\n<1>. QED BY <1>1, <1>2, <1>3\n\n=============================================================================\n", "TLAPS": "------------------------------- MODULE TLAPS --------------------------------\n\n(* Backend pragmas. *)\n\n\n(***************************************************************************)\n(* Each of these pragmas can be cited with a BY or a USE. The pragma that *)\n(* is added to the context of an obligation most recently is the one whose *)\n(* effects are triggered. *)\n(***************************************************************************)\n\n(***************************************************************************)\n(* The following pragmas should be used only as a last resource. They are *)\n(* dependent upon the particular backend provers, and are unlikely to have *)\n(* any effect if the set of backend provers changes. Moreover, they are *)\n(* meaningless to a reader of the proof. *)\n(***************************************************************************)\n\n\n(**************************************************************************)\n(* Backend pragma: use the SMT solver for arithmetic. *)\n(* *)\n(* This method exists under this name for historical reasons. *)\n(**************************************************************************)\n\nSimpleArithmetic == TRUE (*{ by (prover:\"smt3\") }*)\n\n\n(**************************************************************************)\n(* Backend pragma: SMT solver *)\n(* *)\n(* This method translates the proof obligation to SMTLIB2. The supported *)\n(* fragment includes first-order logic, set theory, functions and *)\n(* records. *)\n(* SMT calls the smt-solver with the default timeout of 5 seconds *)\n(* while SMTT(n) calls the smt-solver with a timeout of n seconds. *)\n(* *)\n(* SMTT also accepts a string argument of the form \"rN\" to bound the *)\n(* underlying Z3 solver by a deterministic `rlimit` budget instead of a *)\n(* wall-clock timeout, e.g. SMTT(\"r5\"). N is a multiple of a fixed base *)\n(* resource count, so a small readable budget like \"r5\" is meaningful. *)\n(* Unlike a wall-clock timeout, an `rlimit` budget does not depend on CPU *)\n(* speed or load, so the proof's pass/fail outcome reproduces on any *)\n(* machine and every rerun (for a fixed Z3 build); how long it takes to *)\n(* consume the budget still varies by machine. This is Z3-specific. *)\n(**************************************************************************)\n\nSMT == TRUE (*{ by (prover:\"smt3\") }*)\nSMTT(X) == TRUE (*{ by (prover:\"smt3\"; timeout:@) }*)\n\n\n(**************************************************************************)\n(* Backend pragma: CVC4 SMT solver *)\n(* *)\n(* These methods translate the proof obligation to SMTLIB2 and call CVC4. *)\n(**************************************************************************)\n\n(* The CVC3* methods are here for backward compatibility. They call CVC4. *)\nCVC3 == TRUE (*{ by (prover: \"cvc33\") }*)\nCVC3T(X) == TRUE (*{ by (prover:\"cvc33\"; timeout:@) }*)\n\nCVC4 == TRUE (*{ by (prover: \"cvc33\") }*)\nCVC4T(X) == TRUE (*{ by (prover:\"cvc33\"; timeout:@) }*)\n\n\n(**************************************************************************)\n(* Backend pragma: Yices SMT solver *)\n(* *)\n(* This method translates the proof obligation to Yices native language. *)\n(**************************************************************************)\n\nYices == TRUE (*{ by (prover: \"yices3\") }*)\nYicesT(X) == TRUE (*{ by (prover:\"yices3\"; timeout:@) }*)\n\n(**************************************************************************)\n(* Backend pragma: veriT SMT solver *)\n(* *)\n(* This method translates the proof obligation to SMTLIB2 and calls veriT.*)\n(**************************************************************************)\n\nveriT == TRUE (*{ by (prover: \"verit\") }*)\nveriTT(X) == TRUE (*{ by (prover:\"verit\"; timeout:@) }*)\n\n(**************************************************************************)\n(* Backend pragma: Zipperposition solver *)\n(* *)\n(* This method translates the proof obligation to TPTP and *)\n(* calls Zipperposition. *)\n(**************************************************************************)\n\nZipper == TRUE (*{ by (prover: \"zipper\") }*)\nZipperT(X) == TRUE (*{ by (prover:\"zipper\"; timeout:@) }*)\n\n(**************************************************************************)\n(* Backend pragma: Z3 SMT solver *)\n(* *)\n(* This method translates the proof obligation to SMTLIB2 and calls Z3. *)\n(* Z3 is used by default but you can also explicitly call it. *)\n(* Z3T(n) bounds Z3 by a wall-clock timeout of n seconds, while Z3T(\"rN\") *)\n(* bounds it by a deterministic `rlimit` budget of N base units, which *)\n(* reproduces the same outcome on any machine (see SMTT). *)\n(**************************************************************************)\n\nZ3 == TRUE (*{ by (prover: \"z33\") }*)\nZ3T(X) == TRUE (*{ by (prover:\"z33\"; timeout:@) }*)\n\n(**************************************************************************)\n(* Backend pragma: SPASS superposition prover *)\n(* *)\n(* This method translates the proof obligation to the DFG format language *)\n(* supported by the ATP SPASS. The translation is based on the SMT one. *)\n(**************************************************************************)\n\nSpass == TRUE (*{ by (prover: \"spass\") }*)\nSpassT(X) == TRUE (*{ by (prover:\"spass\"; timeout:@) }*)\n\n(**************************************************************************)\n(* Backend pragma: The PTL propositional linear time temporal logic *)\n(* prover. It currently is the LS4 backend. *)\n(* *)\n(* This method translates the negetation of the proof obligation to *)\n(* Seperated Normal Form (TRP++ format) and checks for unsatisfiability *)\n(**************************************************************************)\n\nLS4 == TRUE (*{ by (prover: \"ls4\") }*)\nLS4T(X) == TRUE (*{ by (prover: \"ls4\"; timeout:@) }*)\nPTL == TRUE (*{ by (prover: \"ls4\") }*)\n\n(**************************************************************************)\n(* Backend pragma: Zenon with different timeouts (default is 10 seconds) *)\n(* *)\n(**************************************************************************)\n\nZenon == TRUE (*{ by (prover:\"zenon\") }*)\nZenonT(X) == TRUE (*{ by (prover:\"zenon\"; timeout:@) }*)\n\n(********************************************************************)\n(* Backend pragma: Isabelle with different timeouts and tactics *)\n(* (default is 30 seconds/auto) *)\n(********************************************************************)\n\nIsa == TRUE (*{ by (prover:\"isabelle\") }*)\nIsaT(X) == TRUE (*{ by (prover:\"isabelle\"; timeout:@) }*)\nIsaM(X) == TRUE (*{ by (prover:\"isabelle\"; tactic:@) }*)\nIsaMT(X,Y) == TRUE (*{ by (prover:\"isabelle\"; tactic:@; timeout:@) }*)\n\n(***************************************************************************)\n(* The following theorem expresses the (useful implication of the) law of *)\n(* set extensionality, which can be written as *)\n(* *)\n(* THEOREM \\A S, T : (S = T) <=> (\\A x : (x \\in S) <=> (x \\in T)) *)\n(* *)\n(* Theorem SetExtensionality is sometimes required by the SMT backend for *)\n(* reasoning about sets. It is usually counterproductive to include *)\n(* theorem SetExtensionality in a BY clause for the Zenon or Isabelle *)\n(* backends. Instead, use the pragma IsaWithSetExtensionality to instruct *)\n(* the Isabelle backend to use the rule of set extensionality. *)\n(***************************************************************************)\nIsaWithSetExtensionality == TRUE\n (*{ by (prover:\"isabelle\"; tactic:\"(auto intro: setEqualI)\")}*)\n\nTHEOREM SetExtensionality == \\A S,T : (\\A x : x \\in S <=> x \\in T) => S = T\nOBVIOUS\n\n(***************************************************************************)\n(* The following theorem is needed to deduce NotInSetS \\notin SetS from *)\n(* the definition *)\n(* *)\n(* NotInSetS == CHOOSE v : v \\notin SetS *)\n(***************************************************************************)\nTHEOREM NoSetContainsEverything == \\A S : \\E x : x \\notin S\nOBVIOUS (*{by (isabelle \"(auto intro: inIrrefl)\")}*)\n-----------------------------------------------------------------------------\n\n\n\n(********************************************************************)\n(********************************************************************)\n(********************************************************************)\n\n\n(********************************************************************)\n(* Old versions of Zenon and Isabelle pragmas below *)\n(* (kept for compatibility) *)\n(********************************************************************)\n\n\n(**************************************************************************)\n(* Backend pragma: Zenon with different timeouts (default is 10 seconds) *)\n(* *)\n(**************************************************************************)\n\nSlowZenon == TRUE (*{ by (prover:\"zenon\"; timeout:20) }*)\nSlowerZenon == TRUE (*{ by (prover:\"zenon\"; timeout:40) }*)\nVerySlowZenon == TRUE (*{ by (prover:\"zenon\"; timeout:80) }*)\nSlowestZenon == TRUE (*{ by (prover:\"zenon\"; timeout:160) }*)\n\n\n\n(********************************************************************)\n(* Backend pragma: Isabelle's automatic search (\"auto\") *)\n(* *)\n(* This pragma bypasses Zenon. It is useful in situations involving *)\n(* essentially simplification and equational reasoning. *)\n(* Default imeout for all isabelle tactics is 30 seconds. *)\n(********************************************************************)\nAuto == TRUE (*{ by (prover:\"isabelle\"; tactic:\"auto\") }*)\nSlowAuto == TRUE (*{ by (prover:\"isabelle\"; tactic:\"auto\"; timeout:120) }*)\nSlowerAuto == TRUE (*{ by (prover:\"isabelle\"; tactic:\"auto\"; timeout:480) }*)\nSlowestAuto == TRUE (*{ by (prover:\"isabelle\"; tactic:\"auto\"; timeout:960) }*)\n\n(********************************************************************)\n(* Backend pragma: Isabelle's \"force\" tactic *)\n(* *)\n(* This pragma bypasses Zenon. It is useful in situations involving *)\n(* quantifier reasoning. *)\n(********************************************************************)\nForce == TRUE (*{ by (prover:\"isabelle\"; tactic:\"force\") }*)\nSlowForce == TRUE (*{ by (prover:\"isabelle\"; tactic:\"force\"; timeout:120) }*)\nSlowerForce == TRUE (*{ by (prover:\"isabelle\"; tactic:\"force\"; timeout:480) }*)\nSlowestForce == TRUE (*{ by (prover:\"isabelle\"; tactic:\"force\"; timeout:960) }*)\n\n(***********************************************************************)\n(* Backend pragma: Isabelle's \"simplification\" tactics *)\n(* *)\n(* These tactics simplify the goal before running one of the automated *)\n(* tactics. They are often necessary for obligations involving record *)\n(* or tuple projections. Use the SimplfyAndSolve tactic unless you're *)\n(* sure you can get away with just Simplification *)\n(***********************************************************************)\nSimplifyAndSolve == TRUE\n (*{ by (prover:\"isabelle\"; tactic:\"clarsimp auto?\") }*)\nSlowSimplifyAndSolve == TRUE\n (*{ by (prover:\"isabelle\"; tactic:\"clarsimp auto?\"; timeout:120) }*)\nSlowerSimplifyAndSolve == TRUE\n (*{ by (prover:\"isabelle\"; tactic:\"clarsimp auto?\"; timeout:480) }*)\nSlowestSimplifyAndSolve == TRUE\n (*{ by (prover:\"isabelle\"; tactic:\"clarsimp auto?\"; timeout:960) }*)\n\nSimplification == TRUE (*{ by (prover:\"isabelle\"; tactic:\"clarsimp\") }*)\nSlowSimplification == TRUE\n (*{ by (prover:\"isabelle\"; tactic:\"clarsimp\"; timeout:120) }*)\nSlowerSimplification == TRUE\n (*{ by (prover:\"isabelle\"; tactic:\"clarsimp\"; timeout:480) }*)\nSlowestSimplification == TRUE\n (*{ by (prover:\"isabelle\"; tactic:\"clarsimp\"; timeout:960) }*)\n\n(**************************************************************************)\n(* Backend pragma: Isabelle's tableau prover (\"blast\") *)\n(* *)\n(* This pragma bypasses Zenon and uses Isabelle's built-in theorem *)\n(* prover, Blast. It is almost never better than Zenon by itself, but *)\n(* becomes very useful in combination with the Auto pragma above. The *)\n(* AutoBlast pragma first attempts Auto and then uses Blast to prove what *)\n(* Auto could not prove. (There is currently no way to use Zenon on the *)\n(* results left over from Auto.) *)\n(**************************************************************************)\nBlast == TRUE (*{ by (prover:\"isabelle\"; tactic:\"blast\") }*)\nSlowBlast == TRUE (*{ by (prover:\"isabelle\"; tactic:\"blast\"; timeout:120) }*)\nSlowerBlast == TRUE (*{ by (prover:\"isabelle\"; tactic:\"blast\"; timeout:480) }*)\nSlowestBlast == TRUE (*{ by (prover:\"isabelle\"; tactic:\"blast\"; timeout:960) }*)\n\nAutoBlast == TRUE (*{ by (prover:\"isabelle\"; tactic:\"auto, blast\") }*)\n\n\n(**************************************************************************)\n(* Backend pragmas: multi-back-ends *)\n(* *)\n(* These pragmas just run a bunch of back-ends one after the other in the *)\n(* hope that one will succeed. This saves time and effort for the user at *)\n(* the expense of computation time. *)\n(**************************************************************************)\n\n(* CVC3 goes first because it's bundled with TLAPS, then the other SMT\n solvers are unlikely to succeed if CVC3 fails, so we run zenon and\n Isabelle before them. *)\nAllProvers == TRUE (*{\n by (prover:\"cvc33\")\n by (prover:\"zenon\")\n by (prover:\"isabelle\"; tactic:\"auto\")\n by (prover:\"spass\")\n by (prover:\"smt3\")\n by (prover:\"yices3\")\n by (prover:\"verit\")\n by (prover:\"z33\")\n by (prover:\"isabelle\"; tactic:\"force\")\n by (prover:\"isabelle\"; tactic:\"(auto intro: setEqualI)\")\n by (prover:\"isabelle\"; tactic:\"clarsimp auto?\")\n by (prover:\"isabelle\"; tactic:\"clarsimp\")\n by (prover:\"isabelle\"; tactic:\"auto, blast\")\n }*)\nAllProversT(X) == TRUE (*{\n by (prover:\"cvc33\"; timeout:@)\n by (prover:\"zenon\"; timeout:@)\n by (prover:\"isabelle\"; tactic:\"auto\"; timeout:@)\n by (prover:\"spass\"; timeout:@)\n by (prover:\"smt3\"; timeout:@)\n by (prover:\"yices3\"; timeout:@)\n by (prover:\"verit\"; timeout:@)\n by (prover:\"z33\"; timeout:@)\n by (prover:\"isabelle\"; tactic:\"force\"; timeout:@)\n by (prover:\"isabelle\"; tactic:\"(auto intro: setEqualI)\"; timeout:@)\n by (prover:\"isabelle\"; tactic:\"clarsimp auto?\"; timeout:@)\n by (prover:\"isabelle\"; tactic:\"clarsimp\"; timeout:@)\n by (prover:\"isabelle\"; tactic:\"auto, blast\"; timeout:@)\n }*)\n\nAllSMT == TRUE (*{\n by (prover:\"cvc33\")\n by (prover:\"smt3\")\n by (prover:\"yices3\")\n by (prover:\"verit\")\n by (prover:\"z33\")\n }*)\nAllSMTT(X) == TRUE (*{\n by (prover:\"cvc33\"; timeout:@)\n by (prover:\"smt3\"; timeout:@)\n by (prover:\"yices3\"; timeout:@)\n by (prover:\"verit\"; timeout:@)\n by (prover:\"z33\"; timeout:@)\n }*)\n\nAllIsa == TRUE (*{\n by (prover:\"isabelle\"; tactic:\"auto\")\n by (prover:\"isabelle\"; tactic:\"force\")\n by (prover:\"isabelle\"; tactic:\"(auto intro: setEqualI)\")\n by (prover:\"isabelle\"; tactic:\"clarsimp auto?\")\n by (prover:\"isabelle\"; tactic:\"clarsimp\")\n by (prover:\"isabelle\"; tactic:\"auto, blast\")\n }*)\nAllIsaT(X) == TRUE (*{\n by (prover:\"isabelle\"; tactic:\"auto\"; timeout:@)\n by (prover:\"isabelle\"; tactic:\"force\"; timeout:@)\n by (prover:\"isabelle\"; tactic:\"(auto intro: setEqualI)\"; timeout:@)\n by (prover:\"isabelle\"; tactic:\"clarsimp auto?\"; timeout:@)\n by (prover:\"isabelle\"; tactic:\"clarsimp\"; timeout:@)\n by (prover:\"isabelle\"; tactic:\"auto, blast\"; timeout:@)\n }*)\n\n\n(**************************************************************************)\n(* The pragma ExpandEnabled invokes expansion of the operator ENABLED. *)\n(* *)\n(* The pragma ExpandCdot invokes expansion of the operator \\cdot. *)\n(* *)\n(* The pragma AutoUSE invokes automated expansion of definitions, *)\n(* for both of ExpandEnabled and ExpandCdot, when each is present. *)\n(* *)\n(* The pragma Lambdify invokes expansion of the operators *)\n(* ENABLED and \\cdot to an intermediate form with bound VARIABLES, *)\n(* which is a form before introducing rigid quantifiers. *)\n(* The pragma Lambdify is sound for occurrences of ENABLED and \\cdot *)\n(* that are not nested. *)\n(**************************************************************************)\nExpandENABLED == TRUE (*{ by (prover:\"expandenabled\") }*)\nExpandCdot == TRUE (*{ by (prover:\"expandcdot\") }*)\nAutoUSE == TRUE (*{ by (prover:\"autouse\") }*)\nLambdify == TRUE (*{ by (prover:\"lambdify\") }*)\nENABLEDaxioms == TRUE (*{ by (prover:\"enabledaxioms\") }*)\nLevelComparison == TRUE (*{ by (prover:\"levelcomparison\") }*)\n\n(* The operators EnabledWrapper and CdotWrapper occur in an intermediate *)\n(* representation within TLAPM. *)\nEnabledWrapper(Op(_)) == FALSE\nCdotWrapper(Op(_)) == FALSE\n\n(***************************************************************************)\n(* The following may be used in a `BY ONLY ThmName` for unit testing the *)\n(* triviality checks in TLAPM. *)\n(***************************************************************************)\nTrivial == TRUE (*{ by (prover:\"trivial\") }*)\n\n\n=============================================================================\n\nThe material below is obsolete: the TLA proof rules below are superseded by\nthe PTL decision procedure, and their formulation is unsound for the semantics\nof temporal reasoning that TLAPS adopts.\n\n----------------------------------------------------------------------------\n(***************************************************************************)\n(* TEMPORAL LOGIC *)\n(* *)\n(* The following rules are intended to be used when TLAPS handles temporal *)\n(* logic. They will not work now. Moreover when temporal reasoning is *)\n(* implemented, these rules may be changed or omitted, and additional *)\n(* rules will probably be added. However, they are included mainly so *)\n(* their names will be defined, preventing the use of identifiers that are *)\n(* likely to produce name clashes with future versions of this module. *)\n(***************************************************************************)\n\n\n(***************************************************************************)\n(* The following proof rules (and their names) are from the paper \"The *)\n(* Temporal Logic of Actions\". *)\n(***************************************************************************)\nTHEOREM RuleTLA1 == ASSUME STATE P, STATE f,\n P /\\ (f' = f) => P'\n PROVE []P <=> P /\\ [][P => P']_f\n\nTHEOREM RuleTLA2 == ASSUME STATE P, STATE Q, STATE f, STATE g,\n ACTION A, ACTION B,\n P /\\ [A]_f => Q /\\ [B]_g\n PROVE []P /\\ [][A]_f => []Q /\\ [][B]_g\n\nTHEOREM RuleINV1 == ASSUME STATE I, STATE F, ACTION N,\n I /\\ [N]_F => I'\n PROVE I /\\ [][N]_F => []I\n\nTHEOREM RuleINV2 == ASSUME STATE I, STATE f, ACTION N\n PROVE []I => ([][N]_f <=> [][N /\\ I /\\ I']_f)\n\nTHEOREM RuleWF1 == ASSUME STATE P, STATE Q, STATE f, ACTION N, ACTION A,\n P /\\ [N]_f => (P' \\/ Q'),\n P /\\ <<N /\\ A>>_f => Q',\n P => ENABLED <<A>>_f\n PROVE [][N]_f /\\ WF_f(A) => (P ~> Q)\n\nTHEOREM RuleSF1 == ASSUME STATE P, STATE Q, STATE f,\n ACTION N, ACTION A, TEMPORAL F,\n P /\\ [N]_f => (P' \\/ Q'),\n P /\\ <<N /\\ A>>_f => Q',\n []P /\\ [][N]_f /\\ []F => <> ENABLED <<A>>_f\n PROVE [][N]_f /\\ SF_f(A) /\\ []F => (P ~> Q)\n\n(***************************************************************************)\n(* The rules WF2 and SF2 in \"The Temporal Logic of Actions\" are obtained *)\n(* from the following two rules by the following substitutions: `. *)\n(* *)\n(* ___ ___ _______________ *)\n(* M <- M , g <- g , EM <- ENABLED <<M>>_g .' *)\n(***************************************************************************)\nTHEOREM RuleWF2 == ASSUME STATE P, STATE f, STATE g, STATE EM,\n ACTION A, ACTION B, ACTION N, ACTION M,\n TEMPORAL F,\n <<N /\\ B>>_f => <<M>>_g,\n P /\\ P' /\\ <<N /\\ A>>_f /\\ EM => B,\n P /\\ EM => ENABLED A,\n [][N /\\ ~B]_f /\\ WF_f(A) /\\ []F /\\ <>[]EM => <>[]P\n PROVE [][N]_f /\\ WF_f(A) /\\ []F => []<><<M>>_g \\/ []<>(~EM)\n\nTHEOREM RuleSF2 == ASSUME STATE P, STATE f, STATE g, STATE EM,\n ACTION A, ACTION B, ACTION N, ACTION M,\n TEMPORAL F,\n <<N /\\ B>>_f => <<M>>_g,\n P /\\ P' /\\ <<N /\\ A>>_f /\\ EM => B,\n P /\\ EM => ENABLED A,\n [][N /\\ ~B]_f /\\ SF_f(A) /\\ []F /\\ []<>EM => <>[]P\n PROVE [][N]_f /\\ SF_f(A) /\\ []F => []<><<M>>_g \\/ <>[](~EM)\n\n\n(***************************************************************************)\n(* The following rule is a special case of the general temporal logic *)\n(* proof rule STL4 from the paper \"The Temporal Logic of Actions\". The *)\n(* general rule is for arbitrary temporal formulas F and G, but it cannot *)\n(* yet be handled by TLAPS. *)\n(***************************************************************************)\nTHEOREM RuleInvImplication ==\n ASSUME STATE F, STATE G,\n F => G\n PROVE []F => []G\nPROOF OMITTED\n\n(***************************************************************************)\n(* The following rule is a special case of rule TLA2 from the paper \"The *)\n(* Temporal Logic of Actions\". *)\n(***************************************************************************)\nTHEOREM RuleStepSimulation ==\n ASSUME STATE I, STATE f, STATE g,\n ACTION M, ACTION N,\n I /\\ I' /\\ [M]_f => [N]_g\n PROVE []I /\\ [][M]_f => [][N]_g\nPROOF OMITTED\n\n(***************************************************************************)\n(* The following may be used to invoke a decision procedure for *)\n(* propositional temporal logic. *)\n(***************************************************************************)\nPropositionalTemporalLogic == TRUE\n=============================================================================\n", "WellFoundedInduction": "------------------------ MODULE WellFoundedInduction ------------------------\n(***************************************************************************)\n(* This module contains useful theorems for inductive proofs and recursive *)\n(* definitions based on a well-founded ordering. *)\n(* *)\n(* Most of the statements of the theorems are decomposed in terms of *)\n(* definitions. This is done for two reasons: *)\n(* *)\n(* - It makes it easier for the backends to instantiate the theorems *)\n(* when those definitions are not expanded. In fact, at the moment *)\n(* the provers can't prove any of those theorems from the theorem *)\n(* itself if the definitions are made usable. *)\n(* *)\n(* - It can be convenient when writing proofs to use those definitions *)\n(* rather than having to write out their expansions. *)\n(* *)\n(* A relation is represented as a set of ordered pairs, where we write *)\n(* <<x, y>> \\in R instead of x R y. It is more convenient to represent *)\n(* relations this way rather than as operators such as < . *)\n(***************************************************************************)\nEXTENDS NaturalsInduction\n\n(***************************************************************************)\n(* The following defines what it means for a relation R to be transitively *)\n(* closed on a set S. In this and other definitions, we think of R as a *)\n(* relation on S, meaning that it is a subset of S \\X S. However, this is *)\n(* not necessary. Our results do not require this as a hypothesis, and it *)\n(* is often convenient to apply them when R is a relation on a set *)\n(* containing S as a subset. They're even true (though uninteresting) if *)\n(* R and S \\X S are disjoint sets. *)\n(***************************************************************************)\nIsTransitivelyClosedOn(R, S) ==\n \\A i, j, k \\in S : (<<i, j>> \\in R) /\\ (<<j, k>> \\in R)\n => (<<i, k>> \\in R)\n(***************************************************************************)\n(* If we think of R as a less-than relation, then R is well founded on S *)\n(* iff there is no \"infinitely descending\" sequence of elements of S. The *)\n(* canonical example of a well founded relation is the ordinary less-than *)\n(* relation on the natural numbers. *)\n(* *)\n(* A S with a well-founded ordering is often called well-ordered. *)\n(***************************************************************************)\nIsWellFoundedOn(R, S) ==\n ~ \\E f \\in [Nat -> S] : \\A n \\in Nat : <<f[n+1], f[n]>> \\in R\n\nLEMMA EmptyIsWellFounded == \\A S : IsWellFoundedOn({}, S)\n\n\nLEMMA IsWellFoundedOnSubset ==\n ASSUME NEW R, NEW S, NEW T \\in SUBSET S,\n IsWellFoundedOn(R,S)\n PROVE IsWellFoundedOn(R,T)\n\n\nLEMMA IsWellFoundedOnSubrelation ==\n ASSUME NEW S, NEW R, NEW RR, RR \\cap (S \\X S) \\subseteq R,\n IsWellFoundedOn(R,S)\n PROVE IsWellFoundedOn(RR,S)\n\n(***************************************************************************)\n(* If we think of R as a less-than relation on S, then the following is *)\n(* the set of elements of S that are less than x. *)\n(***************************************************************************)\nSetLessThan(x, R, S) == {y \\in S : <<y, x>> \\in R}\n\n(***************************************************************************)\n(* If we think of R as a less-than relation on S, then R is well-founded *)\n(* iff every non-empty subset of S has a minimal element. *)\n(***************************************************************************)\n\nTHEOREM WFMin ==\n ASSUME NEW R, NEW S,\n IsWellFoundedOn(R, S),\n NEW T, T \\subseteq S, T # {}\n PROVE \\E x \\in T : \\A y \\in T : ~ (<<y, x>> \\in R)\n\n\nTHEOREM MinWF ==\n ASSUME NEW R, NEW S,\n \\A T \\in SUBSET S : T # {} => \\E x \\in T : \\A y \\in T : ~ (<<y, x>> \\in R)\n PROVE IsWellFoundedOn(R,S)\n\n(***************************************************************************)\n(* The two following lemmas are simple consequences of theorem WFMin. *)\n(***************************************************************************)\nLEMMA WellFoundedIsIrreflexive ==\n ASSUME NEW R, NEW S, NEW x \\in S,\n IsWellFoundedOn(R, S)\n PROVE <<x, x>> \\notin R\n\nLEMMA WellFoundedIsAsymmetric ==\n ASSUME NEW R, NEW S, NEW x \\in S, NEW y \\in S,\n IsWellFoundedOn(R,S),\n <<x,y>> \\in R, <<y,x>> \\in R\n PROVE FALSE\n\n(***************************************************************************)\n(* The following lemmas are simple facts about operator SetLessThan. *)\n(***************************************************************************)\nLEMMA WFSetLessThanIrreflexive ==\n ASSUME NEW R, NEW S, NEW x \\in S,\n IsWellFoundedOn(R,S)\n PROVE x \\notin SetLessThan(x,R,S)\n\nLEMMA SetLessTransitive ==\n ASSUME NEW R, NEW S, NEW x \\in S, NEW y \\in SetLessThan(x,R,S),\n IsTransitivelyClosedOn(R, S)\n PROVE SetLessThan(y, R, S) \\subseteq SetLessThan(x, R, S)\n\n----------------------------------------------------------------------------\n(***************************************************************************)\n(* The following theorem is the basis for proof by induction over a *)\n(* well-founded set. It generalizes theorem GeneralNatInduction of module *)\n(* NaturalsInduction. *)\n(***************************************************************************)\nTHEOREM WFInduction ==\n ASSUME NEW P(_), NEW R, NEW S,\n IsWellFoundedOn(R, S),\n \\A x \\in S : (\\A y \\in SetLessThan(x, R, S) : P(y))\n => P(x)\n PROVE \\A x \\in S : P(x)\n\n(***************************************************************************)\n(* Theorem WFInductiveDef below justifies recursive definitions based on a *)\n(* well-founded ordering. We first prove it with the hypothesis that the *)\n(* ordering is transitively closed. We prove the theorem for an arbitrary *)\n(* well-founded relation by applying the special case to its transitive *)\n(* closure. *)\n(***************************************************************************)\nWFDefOn(R, S, Def(_,_)) ==\n \\A g, h :\n \\A x \\in S :\n (\\A y \\in SetLessThan(x, R, S) : g[y] = h[y])\n => (Def(g,x) = Def(h,x))\n\nOpDefinesFcn(f, S, Def(_,_)) ==\n f = CHOOSE g : g = [x \\in S |-> Def(g, x)]\n\nWFInductiveDefines(f, S, Def(_,_)) ==\n f = [x \\in S |-> Def(f, x)]\n\nWFInductiveUnique(S, Def(_,_)) ==\n \\A g, h : /\\ WFInductiveDefines(g, S, Def)\n /\\ WFInductiveDefines(h, S, Def)\n => (g = h)\n\nTHEOREM WFDefOnUnique ==\n ASSUME NEW Def(_,_), NEW R, NEW S,\n IsWellFoundedOn(R, S), WFDefOn(R, S, Def)\n PROVE WFInductiveUnique(S, Def)\n\nLEMMA WFInductiveDefLemma ==\n ASSUME NEW Def(_,_), NEW R, NEW S, NEW f,\n IsWellFoundedOn(R, S),\n IsTransitivelyClosedOn(R, S),\n WFDefOn(R, S, Def),\n OpDefinesFcn(f, S, Def)\n PROVE WFInductiveDefines(f, S, Def)\n\n(***************************************************************************)\n(* The following defines the transitive closure of the relation R on S. *)\n(* More precisely, it is the transitive closure of the restriction of R *)\n(* to S. We give an abstract definition of transitive closure as the *)\n(* smallest relation that contains R (restricted to S \\X S) and that is *)\n(* transitively closed, then prove some relevant properties. *)\n(***************************************************************************)\nTransitiveClosureOn(R,S) ==\n { ss \\in S \\X S :\n \\A U \\in SUBSET (S \\X S) :\n /\\ R \\cap S \\X S \\subseteq U\n /\\ IsTransitivelyClosedOn(U, S)\n => ss \\in U }\n\nLEMMA TransitiveClosureThm ==\n \\A R, S :\n /\\ R \\cap S \\X S \\subseteq TransitiveClosureOn(R, S)\n /\\ IsTransitivelyClosedOn(TransitiveClosureOn(R, S), S)\n\nLEMMA TransitiveClosureMinimal ==\n ASSUME NEW R, NEW S, NEW U \\in SUBSET (S \\X S),\n R \\cap S \\X S \\subseteq U,\n IsTransitivelyClosedOn(U,S)\n PROVE TransitiveClosureOn(R,S) \\subseteq U\n\n(***************************************************************************)\n(* The following lemmas are consequences of the two previous ones. The *)\n(* first three state closure properties of transitive closure, the fourth *)\n(* lemma allows one to chop off a step in the underlying relation for any *)\n(* pair in the transitive closure. *)\n(***************************************************************************)\n\nLEMMA TCTCTC ==\n ASSUME NEW R, NEW S, NEW i \\in S, NEW j \\in S, NEW k \\in S,\n <<i,j>> \\in TransitiveClosureOn(R,S),\n <<j,k>> \\in TransitiveClosureOn(R,S)\n PROVE <<i,k>> \\in TransitiveClosureOn(R,S)\n\nLEMMA TCRTC ==\n ASSUME NEW R, NEW S, NEW i \\in S, NEW j \\in S, NEW k \\in S,\n <<i,j>> \\in TransitiveClosureOn(R,S), <<j,k>> \\in R\n PROVE <<i,k>> \\in TransitiveClosureOn(R,S)\n\nLEMMA RTCTC ==\n ASSUME NEW R, NEW S, NEW i \\in S, NEW j \\in S, NEW k \\in S,\n <<i,j>> \\in R, <<j,k>> \\in TransitiveClosureOn(R,S)\n PROVE <<i,k>> \\in TransitiveClosureOn(R,S)\n\nLEMMA TransitiveClosureChopLast ==\n ASSUME NEW R, NEW S, NEW i \\in S, NEW k \\in S, <<i,k>> \\in TransitiveClosureOn(R,S)\n PROVE \\E j \\in S : /\\ <<j,k>> \\in R\n /\\ i = j \\/ <<i,j>> \\in TransitiveClosureOn(R,S)\n\n(***************************************************************************)\n(* NB: In a similar way to the preceding lemma, one could prove *)\n(* ASSUME NEW R, NEW S, NEW x \\in S, NEW y \\in S, *)\n(* <<x,y>> \\in TransitiveClosureOn(R,S) *)\n(* PROVE \\E n \\in Nat : \\E f \\in [0..(n+1) -> S] : *)\n(* /\\ \\A i \\in 0..n : <<f[i], f[i+1]>> \\in R *)\n(* /\\ x = f[0] /\\ y = f[n+1] *)\n(* which provides a more constructive characterization of transitive *)\n(* closure. The converse theorem would be proved by induction on n, *)\n(* using the above closure properties. *)\n(***************************************************************************)\n\nTHEOREM TransitiveClosureWF ==\n ASSUME NEW R, NEW S, IsWellFoundedOn(R,S)\n PROVE IsWellFoundedOn(TransitiveClosureOn(R, S), S)\n\nTHEOREM WFInductiveDef ==\n ASSUME NEW Def(_,_), NEW R, NEW S, NEW f,\n IsWellFoundedOn(R, S),\n WFDefOn(R, S, Def),\n OpDefinesFcn(f, S, Def)\n PROVE WFInductiveDefines(f, S, Def)\n\n(***************************************************************************)\n(* Theorem WFInductiveDef allows us to conclude that a recursively defined *)\n(* function satisfies its recursion equation. The following result allows *)\n(* us to deduce the range of this function. *)\n(***************************************************************************)\nTHEOREM WFInductiveDefType ==\n ASSUME NEW Def(_,_), NEW f, NEW R, NEW S, NEW T,\n T # {},\n IsWellFoundedOn(R, S),\n WFDefOn(R, S, Def),\n WFInductiveDefines(f, S, Def),\n \\A g \\in [S -> T], s \\in S : Def(g, s) \\in T\n PROVE f \\in [S -> T]\n\n ----------------------------------------------------------------------------\n(***************************************************************************)\n(* Below are some theorems that allow us to derive some useful *)\n(* well-founded relations from a given well-founded relation. First, we *)\n(* define the operator OpToRel that constructs a relation (a set of *)\n(* ordered pairs) from a relation expressed as an operator. *)\n(***************************************************************************)\nOpToRel(_\\prec_, S) == {ss \\in S \\X S : ss[1] \\prec ss[2]}\n\n(***************************************************************************)\n(* To construct well-founded relations from the less-than relation on the *)\n(* natural numbers, we first prove that it is well-founded. *)\n(***************************************************************************)\nTHEOREM NatLessThanWellFounded == IsWellFoundedOn(OpToRel(<,Nat), Nat)\n\n(***************************************************************************)\n(* The next definition would be easier to read if we used the TLA+ *)\n(* construct {<<x, y>> \\in T : ... }. However, TLAPS does not suport *)\n(* that notation. (It's meaning is rather complicated in the general case *)\n(* when T is not a Cartesian product of sets.) *)\n(***************************************************************************)\nPreImage(f(_), S, R) == {ss \\in S \\X S : <<f(ss[1]), f(ss[2])>> \\in R}\n\nTHEOREM PreImageWellFounded ==\n ASSUME NEW S, NEW T, NEW R, NEW f(_),\n \\A s \\in S : f(s) \\in T,\n IsWellFoundedOn(R, T)\n PROVE IsWellFoundedOn(PreImage(f, S, R), S)\n\n(***************************************************************************)\n(* We now prove that the lexicographical ordering on the Cartesian product *)\n(* of two well-ordered sets is well-ordered. *)\n(***************************************************************************)\nLexPairOrdering(R1, R2, S1, S2) ==\n {ss \\in (S1 \\X S2) \\X (S1 \\X S2) :\n \\/ <<ss[1][1], ss[2][1]>> \\in R1\n \\/ /\\ ss[1][1] = ss[2][1]\n /\\ <<ss[1][2], ss[2][2]>> \\in R2}\n\nTHEOREM WFLexPairOrdering ==\n ASSUME NEW R1, NEW R2, NEW S1, NEW S2,\n IsWellFoundedOn(R1, S1),\n IsWellFoundedOn(R2, S2)\n PROVE IsWellFoundedOn(LexPairOrdering(R1, R2, S1, S2), S1 \\X S2)\n\n(***************************************************************************)\n(* The preceding theorem generalizes in the obvious way to the Cartesian *)\n(* product of a finite number of well-ordered sets. However, the *)\n(* statement of the general theorem is rather complicated, so we state it *)\n(* for the most useful case: the Cartesian product of n copies of the same *)\n(* set. *)\n(***************************************************************************)\nLexProductOrdering(R, S, n) ==\n { ff \\in [1..n -> S] \\X [1..n -> S] :\n \\E j \\in 1..n :\n /\\ \\A i \\in 1..(j-1) : ff[1][i] = ff[2][i]\n /\\ <<ff[1][j], ff[2][j]>> \\in R }\n\nTHEOREM WFLexProductOrdering ==\n ASSUME NEW R, NEW S, NEW n \\in Nat,\n IsWellFoundedOn(R, S)\n PROVE IsWellFoundedOn(LexProductOrdering(R, S, n), [1..n -> S])\n\n=============================================================================\n\\* Modification History\n\\* Last modified Thu Aug 22 18:31:26 CEST 2019 by merz\n\\* Last modified Sun Jan 01 18:39:23 CET 2012 by merz\n\\* Last modified Wed Nov 23 10:13:18 PST 2011 by lamport\n", "WellFoundedInduction_proofs": "--------------------- MODULE WellFoundedInduction_proofs --------------------\n(***************************************************************************)\n(* This module contains useful theorems for inductive proofs and recursive *)\n(* definitions based on a well-founded ordering. *)\n(* *)\n(* Most of the statements of the theorems are decomposed in terms of *)\n(* definitions. This is done for two reasons: *)\n(* *)\n(* - It makes it easier for the backends to instantiate the theorems *)\n(* when those definitions are not expanded. In fact, at the moment *)\n(* the provers can't prove any of those theorems from the theorem *)\n(* itself if the definitions are made usable. *)\n(* *)\n(* - It can be convenient when writing proofs to use those definitions *)\n(* rather than having to write out their expansions. *)\n(* *)\n(* A relation is represented as a set of ordered pairs, where we write *)\n(* <<x, y>> \\in R instead of x R y. It is more convenient to represent *)\n(* relations this way rather than as operators such as < . *)\n(***************************************************************************)\nEXTENDS NaturalsInduction, TLAPS\n\n(***************************************************************************)\n(* The following defines what it means for a relation R to be transitively *)\n(* closed on a set S. In this and other definitions, we think of R as a *)\n(* relation on S, meaning that it is a subset of S \\X S. However, this is *)\n(* not necessary. Our results do not require this as a hypothesis, and it *)\n(* is often convenient to apply them when R is a relation on a set *)\n(* containing S as a subset. They're even true (though uninteresting) if *)\n(* R and S \\X S are disjoint sets. *)\n(***************************************************************************)\nIsTransitivelyClosedOn(R, S) ==\n \\A i, j, k \\in S : (<<i, j>> \\in R) /\\ (<<j, k>> \\in R)\n => (<<i, k>> \\in R)\n(***************************************************************************)\n(* If we think of R as a less-than relation, then R is well founded on S *)\n(* iff there is no \"infinitely descending\" sequence of elements of S. The *)\n(* canonical example of a well founded relation is the ordinary less-than *)\n(* relation on the natural numbers. *)\n(* *)\n(* A S with a well-founded ordering is often called well-ordered. *)\n(***************************************************************************)\nIsWellFoundedOn(R, S) ==\n ~ \\E f \\in [Nat -> S] : \\A n \\in Nat : <<f[n+1], f[n]>> \\in R\n\nLEMMA EmptyIsWellFounded == \\A S : IsWellFoundedOn({}, S)\nBY DEF IsWellFoundedOn\n\n\nLEMMA IsWellFoundedOnSubset ==\n ASSUME NEW R, NEW S, NEW T \\in SUBSET S,\n IsWellFoundedOn(R,S)\n PROVE IsWellFoundedOn(R,T)\nBY Zenon DEF IsWellFoundedOn\n\n\nLEMMA IsWellFoundedOnSubrelation ==\n ASSUME NEW S, NEW R, NEW RR, RR \\cap (S \\X S) \\subseteq R,\n IsWellFoundedOn(R,S)\n PROVE IsWellFoundedOn(RR,S)\n<1>1. SUFFICES ASSUME NEW f \\in [Nat -> S],\n \\A n \\in Nat : <<f[n+1], f[n]>> \\in RR\n PROVE FALSE\n BY DEF IsWellFoundedOn\n<1>2. \\A n \\in Nat : <<f[n+1], f[n]>> \\in RR \\cap (S \\X S)\n BY <1>1\n<1>. QED BY <1>2 DEF IsWellFoundedOn\n\n(***************************************************************************)\n(* If we think of R as a less-than relation on S, then the following is *)\n(* the set of elements of S that are less than x. *)\n(***************************************************************************)\nSetLessThan(x, R, S) == {y \\in S : <<y, x>> \\in R}\n\n(***************************************************************************)\n(* If we think of R as a less-than relation on S, then R is well-founded *)\n(* iff every non-empty subset of S has a minimal element. *)\n(***************************************************************************)\n\nTHEOREM WFMin ==\n ASSUME NEW R, NEW S,\n IsWellFoundedOn(R, S),\n NEW T, T \\subseteq S, T # {}\n PROVE \\E x \\in T : \\A y \\in T : ~ (<<y, x>> \\in R)\n<1> SUFFICES ASSUME \\A x \\in T : \\E y \\in T : <<y, x>> \\in R\n PROVE FALSE\n BY Zenon\n<1> DEFINE f0 == CHOOSE x \\in T : TRUE\n Def(v, n) == CHOOSE x \\in T : <<x, v>> \\in R\n f[n \\in Nat] == IF n = 0 THEN f0 ELSE Def(f[n-1], n)\n<1>1. NatInductiveDefConclusion(f, f0, Def)\n <2>1. NatInductiveDefHypothesis(f, f0, Def)\n BY Zenon DEF NatInductiveDefHypothesis\n <2>. QED\n BY <2>1, NatInductiveDef, Zenon\n<1>2. f \\in [Nat -> T]\n <2>1. f0 \\in T\n OBVIOUS\n <2>2. \\A v \\in T, n \\in Nat \\ {0} : Def(v, n) \\in T\n OBVIOUS\n <2>. QED\n BY <1>1, <2>1, <2>2, NatInductiveDefType, Isa\n<1>3. ASSUME NEW n \\in Nat\n PROVE <<f[n+1], f[n]>> \\in R\n BY <1>1, <1>2, n+1 \\in Nat \\ {0}, (n+1)-1 = n, Zenon DEF NatInductiveDefConclusion\n<1>. QED\n BY <1>2, <1>3, Zenon DEF IsWellFoundedOn\n\n\nTHEOREM MinWF ==\n ASSUME NEW R, NEW S,\n \\A T \\in SUBSET S : T # {} => \\E x \\in T : \\A y \\in T : ~ (<<y, x>> \\in R)\n PROVE IsWellFoundedOn(R,S)\n<1> SUFFICES ASSUME NEW f \\in [Nat -> S],\n \\A n \\in Nat : <<f[n+1], f[n]>> \\in R\n PROVE FALSE\n BY Zenon DEF IsWellFoundedOn\n<1> DEFINE T == { f[n] : n \\in Nat }\n<1>1. T \\subseteq S\n OBVIOUS\n<1>2. \\A x \\in T : \\E y \\in T : <<y,x>> \\in R\n BY Isa\n<1> QED\n BY <1>1, <1>2, Zenon\n\n(***************************************************************************)\n(* The two following lemmas are simple consequences of theorem WFMin. *)\n(***************************************************************************)\nLEMMA WellFoundedIsIrreflexive ==\n ASSUME NEW R, NEW S, NEW x \\in S,\n IsWellFoundedOn(R, S)\n PROVE <<x, x>> \\notin R\n<1>1. \\E z \\in {x} : \\A y \\in {x} : <<y,z>> \\notin R\n BY WFMin, IsaM(\"blast\")\n<1>2. QED\n BY <1>1\n\n\nLEMMA WellFoundedIsAsymmetric ==\n ASSUME NEW R, NEW S, NEW x \\in S, NEW y \\in S,\n IsWellFoundedOn(R,S),\n <<x,y>> \\in R, <<y,x>> \\in R\n PROVE FALSE\n<1>1. \\E u \\in {x,y} : \\A v \\in {x,y} : <<v,u>> \\notin R\n BY WFMin, IsaM(\"blast\")\n<1>2. QED\n BY <1>1\n\n(***************************************************************************)\n(* The following lemmas are simple facts about operator SetLessThan. *)\n(***************************************************************************)\nLEMMA WFSetLessThanIrreflexive ==\n ASSUME NEW R, NEW S, NEW x \\in S,\n IsWellFoundedOn(R,S)\n PROVE x \\notin SetLessThan(x,R,S)\nBY WellFoundedIsIrreflexive DEF SetLessThan\n\n\nLEMMA SetLessTransitive ==\n ASSUME NEW R, NEW S, NEW x \\in S, NEW y \\in SetLessThan(x,R,S),\n IsTransitivelyClosedOn(R, S)\n PROVE SetLessThan(y, R, S) \\subseteq SetLessThan(x, R, S)\nBY DEF SetLessThan, IsTransitivelyClosedOn\n\n----------------------------------------------------------------------------\n(***************************************************************************)\n(* The following theorem is the basis for proof by induction over a *)\n(* well-founded set. It generalizes theorem GeneralNatInduction of module *)\n(* NaturalsInduction. *)\n(***************************************************************************)\nTHEOREM WFInduction ==\n ASSUME NEW P(_), NEW R, NEW S,\n IsWellFoundedOn(R, S),\n \\A x \\in S : (\\A y \\in SetLessThan(x, R, S) : P(y))\n => P(x)\n PROVE \\A x \\in S : P(x)\n<1> DEFINE T == {x \\in S : ~P(x)}\n<1>1. SUFFICES ASSUME T # {}\n PROVE FALSE\n OBVIOUS\n<1>2. PICK x \\in T : \\A y \\in T : ~ (<<y, x>> \\in R)\n BY <1>1, WFMin, Zenon\n<1>3. QED\n BY <1>2 DEF SetLessThan\n\n(***************************************************************************)\n(* Theorem WFInductiveDef below justifies recursive definitions based on a *)\n(* well-founded ordering. We first prove it with the hypothesis that the *)\n(* ordering is transitively closed. We prove the theorem for an arbitrary *)\n(* well-founded relation by applying the special case to its transitive *)\n(* closure. *)\n(***************************************************************************)\nWFDefOn(R, S, Def(_,_)) ==\n \\A g, h :\n \\A x \\in S :\n (\\A y \\in SetLessThan(x, R, S) : g[y] = h[y])\n => (Def(g,x) = Def(h,x))\n\nOpDefinesFcn(f, S, Def(_,_)) ==\n f = CHOOSE g : g = [x \\in S |-> Def(g, x)]\n\nWFInductiveDefines(f, S, Def(_,_)) ==\n f = [x \\in S |-> Def(f, x)]\n\nWFInductiveUnique(S, Def(_,_)) ==\n \\A g, h : /\\ WFInductiveDefines(g, S, Def)\n /\\ WFInductiveDefines(h, S, Def)\n => (g = h)\n\nTHEOREM WFDefOnUnique ==\n ASSUME NEW Def(_,_), NEW R, NEW S,\n IsWellFoundedOn(R, S), WFDefOn(R, S, Def)\n PROVE WFInductiveUnique(S, Def)\n<1>1. SUFFICES ASSUME NEW g, NEW h,\n WFInductiveDefines(g, S, Def),\n WFInductiveDefines(h, S, Def)\n PROVE g = h\n BY DEF WFInductiveUnique\n<1> SUFFICES \\A x \\in S : g[x] = h[x]\n BY <1>1 DEF WFInductiveDefines\n<1>2. ASSUME NEW x \\in S,\n \\A y \\in SetLessThan(x, R, S) : g[y] = h[y]\n PROVE g[x] = h[x]\n <2>1. Def(g,x) = Def(h,x)\n BY <1>2 DEF WFDefOn\n <2>2. QED\n BY <1>1, <2>1 DEF WFInductiveDefines\n<1>3. QED\n BY <1>2, WFInduction, Isa\n\nLEMMA WFInductiveDefLemma ==\n ASSUME NEW Def(_,_), NEW R, NEW S, NEW f,\n IsWellFoundedOn(R, S),\n IsTransitivelyClosedOn(R, S),\n WFDefOn(R, S, Def),\n OpDefinesFcn(f, S, Def)\n PROVE WFInductiveDefines(f, S, Def)\n<1> DEFINE LT(x) == {x} \\cup SetLessThan(x, R, S)\n<1>1. ASSUME NEW x \\in S\n PROVE /\\ LT(x) = {x} \\cup UNION {LT(y) : y \\in SetLessThan(x, R, S)}\n /\\ (x \\in LT(x)) /\\ (SetLessThan(x, R, S) \\subseteq LT(x))\n /\\ \\A y \\in LT(x) : SetLessThan(y, R, S) \\subseteq LT(x)\n /\\ \\A y \\in LT(x) : LT(y) \\subseteq LT(x)\n /\\ LT(x) \\subseteq S\n BY Isa DEF SetLessThan, IsTransitivelyClosedOn\n<1> HIDE DEF LT \\** from now on, (mostly) use properties in step <1>1 rather than the definition\n\n<1> DEFINE F(x) == CHOOSE g : g = [y \\in LT(x) |-> Def(g, y)]\n ff == [x \\in S |-> F(x)[x]]\n<1> HIDE DEF ff\n\n<1>2. \\A x \\in S : ff[x] = Def(ff,x)\n <2>1. SUFFICES ASSUME NEW x \\in S,\n \\A y \\in SetLessThan(x, R, S) : ff[y] = Def(ff,y)\n PROVE ff[x] = Def(ff,x)\n BY WFInduction, Isa\n <2>2. WFInductiveUnique(LT(x), Def)\n <3>1. LT(x) \\subseteq S\n BY <1>1\n <3>2. IsWellFoundedOn(R, LT(x))\n BY <3>1, IsWellFoundedOnSubset\n <3>3. \\A z \\in LT(x) : SetLessThan(z, R, LT(x)) = SetLessThan(z, R, S)\n BY DEF LT, SetLessThan, IsTransitivelyClosedOn\n <3>4. WFDefOn(R, LT(x), Def)\n BY <3>1, <3>3 DEF WFDefOn\n <3>. QED\n BY <3>2, <3>4, WFDefOnUnique, Zenon\n <2> DEFINE g == [y \\in LT(x) |-> Def(ff, y)]\n <2>3. Def(ff,x) = Def(g,x)\n BY <1>1 (* x \\in LT(x) *), <2>1, Zenon DEF WFDefOn\n <2>4. ASSUME NEW y \\in SetLessThan(x, R, S)\n PROVE Def(ff,y) = Def(g,y)\n <3>1. y \\in S\n BY DEF SetLessThan\n <3>2. \\A z \\in SetLessThan(y, R, S) : ff[z] = g[z]\n BY <2>1, SetLessTransitive DEF LT\n <3>3. QED\n BY <3>1, <3>2, Zenon DEF WFDefOn\n <2>5. WFInductiveDefines(g, LT(x), Def)\n BY <2>3, <2>4 DEF WFInductiveDefines, LT\n <2>6. WFInductiveDefines(F(x), LT(x), Def)\n BY <2>5 DEF WFInductiveDefines\n <2>7. g = F(x)\n BY <2>5, <2>6, <2>2, Zenon DEF WFInductiveUnique\n <2>. QED\n BY <1>1, <2>7, Zenon DEF ff\n\n<1>3. QED\n <2>1. WFInductiveDefines(ff, S, Def)\n BY <1>2 DEF WFInductiveDefines, ff\n <2>2. QED\n BY <2>1 DEF WFInductiveDefines, OpDefinesFcn\n\n(***************************************************************************)\n(* The following defines the transitive closure of the relation R on S. *)\n(* More precisely, it is the transitive closure of the restriction of R *)\n(* to S. We give an abstract definition of transitive closure as the *)\n(* smallest relation that contains R (restricted to S \\X S) and that is *)\n(* transitively closed, then prove some relevant properties. *)\n(***************************************************************************)\nTransitiveClosureOn(R,S) ==\n { ss \\in S \\X S :\n \\A U \\in SUBSET (S \\X S) :\n /\\ R \\cap S \\X S \\subseteq U\n /\\ IsTransitivelyClosedOn(U, S)\n => ss \\in U }\n\nLEMMA TransitiveClosureThm ==\n \\A R, S :\n /\\ R \\cap S \\X S \\subseteq TransitiveClosureOn(R, S)\n /\\ IsTransitivelyClosedOn(TransitiveClosureOn(R, S), S)\n<1> TAKE R, S\n<1>1. R \\cap S \\X S \\subseteq TransitiveClosureOn(R, S)\n BY DEF TransitiveClosureOn\n<1>2. IsTransitivelyClosedOn(TransitiveClosureOn(R, S), S)\n BY Zenon DEF TransitiveClosureOn, IsTransitivelyClosedOn\n<1>. QED BY <1>1, <1>2\n\nLEMMA TransitiveClosureMinimal ==\n ASSUME NEW R, NEW S, NEW U \\in SUBSET (S \\X S),\n R \\cap S \\X S \\subseteq U,\n IsTransitivelyClosedOn(U,S)\n PROVE TransitiveClosureOn(R,S) \\subseteq U\nBY DEF TransitiveClosureOn\n\n(***************************************************************************)\n(* The following lemmas are consequences of the two previous ones. The *)\n(* first three state closure properties of transitive closure, the fourth *)\n(* lemma allows one to chop off a step in the underlying relation for any *)\n(* pair in the transitive closure. *)\n(***************************************************************************)\n\nLEMMA TCTCTC ==\n ASSUME NEW R, NEW S, NEW i \\in S, NEW j \\in S, NEW k \\in S,\n <<i,j>> \\in TransitiveClosureOn(R,S),\n <<j,k>> \\in TransitiveClosureOn(R,S)\n PROVE <<i,k>> \\in TransitiveClosureOn(R,S)\nBY TransitiveClosureThm DEF IsTransitivelyClosedOn\n\nLEMMA TCRTC ==\n ASSUME NEW R, NEW S, NEW i \\in S, NEW j \\in S, NEW k \\in S,\n <<i,j>> \\in TransitiveClosureOn(R,S), <<j,k>> \\in R\n PROVE <<i,k>> \\in TransitiveClosureOn(R,S)\nBY TransitiveClosureThm, TCTCTC, Zenon\n\nLEMMA RTCTC ==\n ASSUME NEW R, NEW S, NEW i \\in S, NEW j \\in S, NEW k \\in S,\n <<i,j>> \\in R, <<j,k>> \\in TransitiveClosureOn(R,S)\n PROVE <<i,k>> \\in TransitiveClosureOn(R,S)\nBY TransitiveClosureThm, TCTCTC, Zenon\n\nLEMMA TransitiveClosureChopLast ==\n ASSUME NEW R, NEW S, NEW i \\in S, NEW k \\in S, <<i,k>> \\in TransitiveClosureOn(R,S)\n PROVE \\E j \\in S : /\\ <<j,k>> \\in R\n /\\ i = j \\/ <<i,j>> \\in TransitiveClosureOn(R,S)\n<1> DEFINE U == { ss \\in S \\X S : \\E s \\in S : /\\ <<s, ss[2]>> \\in R\n /\\ ss[1] = s \\/ <<ss[1], s>> \\in TransitiveClosureOn(R,S) }\n<1>1. R \\cap S \\X S \\subseteq U\n <2> SUFFICES ASSUME NEW x \\in S, NEW y \\in S, <<x,y>> \\in R\n PROVE <<x,y>> \\in U\n BY IsaM(\"blast\")\n <2> QED\n OBVIOUS\n<1>2. U \\subseteq TransitiveClosureOn(R,S)\n <2> SUFFICES ASSUME NEW x \\in S, NEW y \\in S, <<x,y>> \\in U\n PROVE <<x,y>> \\in TransitiveClosureOn(R,S)\n BY IsaM(\"blast\")\n <2> QED\n BY TransitiveClosureThm, Zenon DEF IsTransitivelyClosedOn\n<1>3. IsTransitivelyClosedOn(U,S)\n <2>1. SUFFICES ASSUME NEW x \\in S, NEW y \\in S, NEW z \\in S,\n <<x,y>> \\in U, <<y,z>> \\in U\n PROVE <<x,z>> \\in U\n BY Zenon DEF IsTransitivelyClosedOn\n <2>2. <<x,y>> \\in TransitiveClosureOn(R,S)\n BY <2>1, <1>2, Zenon\n <2>3. PICK s \\in S : /\\ <<s,z>> \\in R\n /\\ y=s \\/ <<y,s>> \\in TransitiveClosureOn(R,S)\n BY <2>1, Zenon\n <2>4. <<x,s>> \\in TransitiveClosureOn(R,S)\n BY <2>2, <2>3, TransitiveClosureThm DEF IsTransitivelyClosedOn\n <2> QED\n BY <2>3, <2>4\n<1>4. QED\n <2>1. TransitiveClosureOn(R,S) \\subseteq U\n BY <1>1, <1>3, TransitiveClosureMinimal, Zenon\n <2>2. QED\n BY <2>1, Zenon\n\n(***************************************************************************)\n(* NB: In a similar way to the preceding lemma, one could prove *)\n(* ASSUME NEW R, NEW S, NEW x \\in S, NEW y \\in S, *)\n(* <<x,y>> \\in TransitiveClosureOn(R,S) *)\n(* PROVE \\E n \\in Nat : \\E f \\in [0..(n+1) -> S] : *)\n(* /\\ \\A i \\in 0..n : <<f[i], f[i+1]>> \\in R *)\n(* /\\ x = f[0] /\\ y = f[n+1] *)\n(* which provides a more constructive characterization of transitive *)\n(* closure. The converse theorem would be proved by induction on n, *)\n(* using the above closure properties. *)\n(***************************************************************************)\n\nTHEOREM TransitiveClosureWF ==\n ASSUME NEW R, NEW S, IsWellFoundedOn(R,S)\n PROVE IsWellFoundedOn(TransitiveClosureOn(R, S), S)\n<1> SUFFICES ASSUME NEW T \\in SUBSET S, T # {}\n PROVE \\E x \\in T : \\A y \\in T : ~(<<y,x>> \\in TransitiveClosureOn(R, S))\n BY MinWF\n(* It is tempting to simply pick a minimal element x in T w.r.t. relation R as the witness,\n but that wouldn't work in general because there may be elements below x in the transitive\n closure of R. So we complete T w.r.t. the transitive closure in an appropriate way and\n pick a minimal element in that larger set. *)\n<1> DEFINE TT == T \\cup { j \\in S : \\E i,k \\in T : /\\ <<i,j>> \\in TransitiveClosureOn(R,S)\n /\\ <<j,k>> \\in TransitiveClosureOn(R,S) }\n<1>1. PICK x \\in TT : \\A y \\in TT : ~(<<y,x>> \\in R)\n BY WFMin, Zenon\n<1>2. x \\in T\n <2>1. ASSUME NEW i \\in T, NEW k \\in T,\n <<i,x>> \\in TransitiveClosureOn(R,S),\n <<x,k>> \\in TransitiveClosureOn(R,S)\n PROVE FALSE\n <3>1. PICK j \\in S : /\\ <<j,x>> \\in R\n /\\ i=j \\/ <<i,j>> \\in TransitiveClosureOn(R,S)\n BY <2>1, TransitiveClosureChopLast, Zenon\n <3>2. j \\in TT\n <4>1. CASE <<i,j>> \\in TransitiveClosureOn(R,S)\n BY <3>1, <4>1, <2>1, RTCTC, Zenon\n <4>2. QED\n BY <3>1, <4>1\n <3>3. QED\n BY <3>1, <3>2, <1>1, Zenon\n <2>2. QED\n BY <2>1, Zenon\n<1>3. ASSUME NEW y \\in T, <<y,x>> \\in TransitiveClosureOn(R, S)\n PROVE FALSE\n <2>1. PICK j \\in S : /\\ <<j,x>> \\in R\n /\\ y=j \\/ <<y,j>> \\in TransitiveClosureOn(R,S)\n BY <1>3, TransitiveClosureChopLast\n <2>2. j \\in TT\n <3>1. CASE <<y,j>> \\in TransitiveClosureOn(R,S)\n BY <1>2, <3>1, <2>1, TransitiveClosureThm\n <3>2. QED\n BY <2>1, <3>1\n <2>3. QED\n BY <2>1, <2>2, <1>1, Zenon\n<1> QED\n BY <1>2, <1>3\n\nTHEOREM WFInductiveDef ==\n ASSUME NEW Def(_,_), NEW R, NEW S, NEW f,\n IsWellFoundedOn(R, S),\n WFDefOn(R, S, Def),\n OpDefinesFcn(f, S, Def)\n PROVE WFInductiveDefines(f, S, Def)\n<1> DEFINE TC == TransitiveClosureOn(R, S)\n<1>1. IsTransitivelyClosedOn(TC, S)\n BY TransitiveClosureThm\n<1>2. IsWellFoundedOn(TC, S)\n BY TransitiveClosureWF\n<1>3. WFDefOn(TC, S, Def)\n <2>1. \\A x \\in S : SetLessThan(x, R, S) \\subseteq SetLessThan(x, TC, S)\n BY TransitiveClosureThm DEF SetLessThan\n <2>2. QED\n BY <2>1, Zenon DEF WFDefOn\n<1>4. QED\n BY <1>1, <1>2, <1>3, WFInductiveDefLemma\n\n(***************************************************************************)\n(* Theorem WFInductiveDef allows us to conclude that a recursively defined *)\n(* function satisfies its recursion equation. The following result allows *)\n(* us to deduce the range of this function. *)\n(***************************************************************************)\nTHEOREM WFInductiveDefType ==\n ASSUME NEW Def(_,_), NEW f, NEW R, NEW S, NEW T,\n T # {},\n IsWellFoundedOn(R, S),\n WFDefOn(R, S, Def),\n WFInductiveDefines(f, S, Def),\n \\A g \\in [S -> T], s \\in S : Def(g, s) \\in T\n PROVE f \\in [S -> T]\n<1>1. \\A s \\in S : f[s] \\in T\n <2>1. SUFFICES ASSUME NEW s \\in S,\n \\A x \\in SetLessThan(s, R, S) : f[x] \\in T\n PROVE f[s] \\in T\n BY ONLY <2>1, IsWellFoundedOn(R, S), WFInduction, IsaM(\"auto\")\n <2>2. PICK t0 : t0 \\in T\n OBVIOUS\n <2> DEFINE g == [x \\in S |-> IF x \\in SetLessThan(s, R, S) THEN f[x] ELSE t0]\n <2>3. /\\ g \\in [S -> T]\n /\\ \\A x \\in SetLessThan(s, R, S) : g[x] = f[x]\n <3> SetLessThan(s, R, S) \\subseteq S\n BY DEF SetLessThan\n <3> QED\n BY <2>1, <2>2\n <2>4. Def(f,s) = Def(g,s)\n BY <2>3 DEF WFDefOn\n <2>5. QED\n BY <2>3, <2>4 DEF WFInductiveDefines, WFDefOn\n<1>2. QED\n BY <1>1 DEF WFInductiveDefines\n\n ----------------------------------------------------------------------------\n(***************************************************************************)\n(* Below are some theorems that allow us to derive some useful *)\n(* well-founded relations from a given well-founded relation. First, we *)\n(* define the operator OpToRel that constructs a relation (a set of *)\n(* ordered pairs) from a relation expressed as an operator. *)\n(***************************************************************************)\nOpToRel(_\\prec_, S) == {ss \\in S \\X S : ss[1] \\prec ss[2]}\n\n(***************************************************************************)\n(* To construct well-founded relations from the less-than relation on the *)\n(* natural numbers, we first prove that it is well-founded. *)\n(***************************************************************************)\nTHEOREM NatLessThanWellFounded == IsWellFoundedOn(OpToRel(<,Nat), Nat)\n<1> DEFINE R == OpToRel(<,Nat)\n<1>1. SUFFICES ASSUME NEW ff \\in [Nat -> Nat],\n \\A n \\in Nat : ff[n+1] < ff[n]\n PROVE FALSE\n BY Zenon DEF IsWellFoundedOn, OpToRel\n\n<1> DEFINE P(n) == \\E f \\in [Nat -> Nat] :\n /\\ \\A m \\in Nat : <<f[m+1], f[m]>> \\in R\n /\\ f[0] = n\n<1>2. P(ff[0])\n BY <1>1, Isa DEF OpToRel\n<1>3. ASSUME NEW n \\in Nat,\n \\A m \\in 0..(n-1) : ~ P(m)\n PROVE ~ P(n)\n <2> SUFFICES ASSUME NEW f \\in [Nat -> Nat],\n \\A m \\in Nat : <<f[m+1], f[m]>> \\in R ,\n f[0] = n\n PROVE FALSE\n OBVIOUS\n <2> DEFINE g[i \\in Nat] == f[i+1]\n <2>1. g \\in [Nat -> Nat]\n OBVIOUS\n <2>2. \\A i \\in Nat : <<g[i+1], g[i]>> \\in R\n BY Isa\n <2>3. g[0] \\in 0..(n-1)\n <3>. f[1] < f[0] BY Isa DEF OpToRel\n <3>. QED BY Isa\n <2>4 QED\n BY <2>1, <2>2, <2>3, <1>3\n<1>4. ~ P(ff[0])\n <2> HIDE DEF P\n <2> \\A n \\in Nat : ~ P(n)\n BY ONLY <1>3, GeneralNatInduction, Isa\n <2> QED\n BY DEF P\n<1>5. QED\n BY <1>2, <1>4\n\n(***************************************************************************)\n(* The next definition would be easier to read if we used the TLA+ *)\n(* construct {<<x, y>> \\in T : ... }. However, TLAPS does not suport *)\n(* that notation. (It's meaning is rather complicated in the general case *)\n(* when T is not a Cartesian product of sets.) *)\n(***************************************************************************)\nPreImage(f(_), S, R) == {ss \\in S \\X S : <<f(ss[1]), f(ss[2])>> \\in R}\n\nTHEOREM PreImageWellFounded ==\n ASSUME NEW S, NEW T, NEW R, NEW f(_),\n \\A s \\in S : f(s) \\in T,\n IsWellFoundedOn(R, T)\n PROVE IsWellFoundedOn(PreImage(f, S, R), S)\n<1> SUFFICES ASSUME NEW g \\in [Nat -> S],\n \\A n \\in Nat : <<g[n+1], g[n]>> \\in PreImage(f, S, R)\n PROVE FALSE\n BY DEF IsWellFoundedOn\n<1> DEFINE gg[n \\in Nat] == f(g[n])\n<1>1. ASSUME NEW n \\in Nat\n PROVE <<gg[n+1], gg[n]>> \\in R\n BY Isa DEF PreImage\n<1> QED\n BY <1>1, Zenon DEF IsWellFoundedOn\n\n(***************************************************************************)\n(* We now prove that the lexicographical ordering on the Cartesian product *)\n(* of two well-ordered sets is well-ordered. *)\n(***************************************************************************)\nLexPairOrdering(R1, R2, S1, S2) ==\n {ss \\in (S1 \\X S2) \\X (S1 \\X S2) :\n \\/ <<ss[1][1], ss[2][1]>> \\in R1\n \\/ /\\ ss[1][1] = ss[2][1]\n /\\ <<ss[1][2], ss[2][2]>> \\in R2}\n\nTHEOREM WFLexPairOrdering ==\n ASSUME NEW R1, NEW R2, NEW S1, NEW S2,\n IsWellFoundedOn(R1, S1),\n IsWellFoundedOn(R2, S2)\n PROVE IsWellFoundedOn(LexPairOrdering(R1, R2, S1, S2), S1 \\X S2)\n<1> SUFFICES ASSUME NEW T \\in SUBSET (S1 \\X S2), T # {}\n PROVE \\E x \\in T : \\A y \\in T : <<y,x>> \\notin LexPairOrdering(R1, R2, S1, S2)\n BY MinWF, Zenon\n<1> DEFINE T1 == { tt[1] : tt \\in T }\n<1>1. PICK x1 \\in T1 : \\A y1 \\in T1 : <<y1,x1>> \\notin R1\n <2>1. T1 \\subseteq S1 /\\ T1 # {}\n OBVIOUS\n <2>2. QED\n BY <2>1, WFMin, Zenon\n<1> DEFINE T2 == { tt[2] : tt \\in { uu \\in T : uu[1] = x1 } }\n<1>2. PICK x2 \\in T2 : \\A y2 \\in T2 : <<y2,x2>> \\notin R2\n <2>1. T2 \\subseteq S2 /\\ T2 # {}\n OBVIOUS\n <2>2. QED\n BY <2>1, WFMin, Zenon\n<1>3. ASSUME NEW t \\in T,\n << t, <<x1,x2>> >> \\in LexPairOrdering(R1, R2, S1, S2)\n PROVE FALSE\n <2>1. CASE << t[1], x1 >> \\in R1\n BY <1>1, <2>1\n <2>2. CASE t[1] = x1 /\\ << t[2], x2 >> \\in R2\n BY <1>2, <2>2\n <2>3. QED\n BY <2>1, <2>2, <1>3 DEF LexPairOrdering\n<1> QED\n BY <1>3\n\n(***************************************************************************)\n(* The preceding theorem generalizes in the obvious way to the Cartesian *)\n(* product of a finite number of well-ordered sets. However, the *)\n(* statement of the general theorem is rather complicated, so we state it *)\n(* for the most useful case: the Cartesian product of n copies of the same *)\n(* set. *)\n(***************************************************************************)\nLexProductOrdering(R, S, n) ==\n { ff \\in [1..n -> S] \\X [1..n -> S] :\n \\E j \\in 1..n :\n /\\ \\A i \\in 1..(j-1) : ff[1][i] = ff[2][i]\n /\\ <<ff[1][j], ff[2][j]>> \\in R }\n\nTHEOREM WFLexProductOrdering ==\n ASSUME NEW R, NEW S, NEW n \\in Nat,\n IsWellFoundedOn(R, S)\n PROVE IsWellFoundedOn(LexProductOrdering(R, S, n), [1..n -> S])\n<1> DEFINE LPO(m) == LexProductOrdering(R, S, m)\n<1> DEFINE P(m) == IsWellFoundedOn(LPO(m), [1..m -> S])\n<1>1. P(0)\n BY EmptyIsWellFounded, 1..0 = {}, Zenon DEF LexProductOrdering\n<1>2. ASSUME NEW m \\in Nat, P(m)\n PROVE P(m+1)\n <2>1. IsWellFoundedOn(LexPairOrdering(LPO(m), R, [1..m -> S], S), [1..m -> S] \\X S)\n BY <1>2, WFLexPairOrdering\n (*************************************************************************)\n (* Pairs of m-tuples over S in [1..m ->S] and an element of S are *)\n (* isomorphic to (m+1)-tuples over S, and the following function *)\n (* establishes this isomorphism. We will then apply the theorem about *)\n (* preimages to prove the desired result. *)\n (*************************************************************************)\n <2> DEFINE g(ss) == << [i \\in 1..m |-> ss[i]], ss[m+1] >>\n <2>2. IsWellFoundedOn(PreImage(g, [1..m+1 -> S], LexPairOrdering(LPO(m), R, [1..m -> S], S)),\n [1..m+1 -> S])\n <3>1. \\A ss \\in [1..m+1 -> S] : g(ss) \\in [1..m -> S] \\X S\n OBVIOUS\n <3> HIDE DEF g\n <3>2. QED\n BY <2>1, <3>1, PreImageWellFounded, Isa\n <2>3. LPO(m+1) = PreImage(g, [1..m+1 -> S], LexPairOrdering(LPO(m), R, [1..m -> S], S))\n <3>0. 1 .. m+1 = 1..m \\union {m+1}\n OBVIOUS\n <3>1. LPO(m+1) \\subseteq PreImage(g, [1..m+1 -> S], LexPairOrdering(LPO(m), R, [1..m -> S], S))\n <4> SUFFICES ASSUME NEW x \\in [1..m+1 -> S], NEW y \\in [1..m+1 -> S],\n NEW j \\in 1 .. m+1,\n \\A i \\in 1..j-1 : x[i] = y[i],\n <<x[j], y[j]>> \\in R\n PROVE <<x,y>> \\in PreImage(g, [1..m+1 -> S], LexPairOrdering(LPO(m), R, [1..m -> S], S))\n BY Isa DEF LexProductOrdering\n <4>1. \\A i \\in 1 .. j-1 : i \\in 1 .. m\n OBVIOUS\n <4>2. << g(x), g(y) >> \\in LexPairOrdering(LPO(m), R, [1..m -> S], S)\n <5>1. CASE j \\in 1..m\n <6>1. << g(x)[1], g(y)[1] >> \\in LPO(m)\n BY <3>0, <4>1, <5>1, Zenon DEF LexProductOrdering\n <6>2. QED\n BY <6>1 DEF LexPairOrdering\n <5>2. CASE j = m+1\n <6>1. /\\ g(x)[1] = g(y)[1]\n /\\ << g(x)[2], g(y)[2] >> \\in R\n BY <3>0, <5>2\n <6>2. QED\n BY <6>1 DEF LexPairOrdering\n <5>3. QED\n BY <5>1, <5>2\n <4>3. QED\n BY <4>2, Zenon DEF PreImage\n <3>2. PreImage(g, [1..m+1 -> S], LexPairOrdering(LPO(m), R, [1..m -> S], S)) \\subseteq LPO(m+1)\n <4> SUFFICES ASSUME NEW x \\in [1..m+1 -> S], NEW y \\in [1..m+1 -> S],\n << g(x), g(y) >> \\in LexPairOrdering(LPO(m), R, [1..m -> S], S)\n PROVE <<x,y>> \\in LPO(m+1)\n BY Isa DEF PreImage\n <4>1. CASE << g(x)[1], g(y)[1] >> \\in LPO(m)\n <5> HIDE DEF g\n <5>1. PICK j \\in 1..m : /\\ \\A i \\in 1..j-1 : g(x)[1][i] = g(y)[1][i]\n /\\ << g(x)[1][j], g(y)[1][j] >> \\in R\n BY <4>1, Zenon DEF LexProductOrdering\n <5>3. /\\ g(x)[1][j] = x[j]\n /\\ \\A i \\in 1..j-1 : g(x)[1][i] = x[i]\n /\\ g(y)[1][j] = y[j]\n /\\ \\A i \\in 1..j-1 : g(y)[1][i] = y[i]\n BY DEF g\n <5> QED\n BY <5>1, <5>3, <3>0, Zenon DEF LexProductOrdering\n <4>2. CASE g(x)[1] = g(y)[1] /\\ << g(x)[2], g(y)[2] >> \\in R\n <5>1. <<x[m+1], y[m+1]>> \\in R\n BY <4>2\n <5>2. \\A i \\in 1..m : /\\ g(x)[1][i] = x[i]\n /\\ g(y)[1][i] = y[i]\n OBVIOUS\n <5>3. \\A i \\in 1..(m+1)-1 : x[i] = y[i]\n BY <4>2, <5>2, Isa\n <5> QED\n BY <5>1, <5>3 DEF LexProductOrdering\n <4> QED\n BY <4>1, <4>2, Zenon DEF LexPairOrdering\n <3>3. QED\n BY <3>1, <3>2\n <2>. QED\n BY <2>2, <2>3\n<1>3. \\A m \\in Nat : P(m)\n BY <1>1, <1>2, NatInduction, Isa\n<1>4. QED\n BY <1>3\n\n=============================================================================\n\\* Modification History\n\\* Last modified Fri Jun 13 15:08:07 CEST 2025 by merz\n\\* Last modified Sun Jan 01 18:39:23 CET 2012 by merz\n\\* Last modified Wed Nov 23 10:13:18 PST 2011 by lamport\n" };
|