<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Georgii</title>
    <description>The latest articles on DEV Community by Georgii (@0xgeorgii).</description>
    <link>https://dev.to/0xgeorgii</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1420745%2F73951022-9a00-4845-9d78-b8cb22391069.jpg</url>
      <title>DEV Community: Georgii</title>
      <link>https://dev.to/0xgeorgii</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/0xgeorgii"/>
    <language>en</language>
    <item>
      <title>Commit to marriage with TLA+ pt.2</title>
      <dc:creator>Georgii</dc:creator>
      <pubDate>Mon, 24 Feb 2025 01:28:43 +0000</pubDate>
      <link>https://dev.to/0xgeorgii/commit-to-marriage-with-tla-pt2-2n08</link>
      <guid>https://dev.to/0xgeorgii/commit-to-marriage-with-tla-pt2-2n08</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This is the second blog post (here is the first one [&lt;a href="https://www.inferara.com/en/blog/do-not-die-hard-with-tla-plus-1/" rel="noopener noreferrer"&gt;1&lt;/a&gt;]) in the series of the conspectus of the "Introduction to TLA+" course by Leslie Lamport. As usual, all credits are to Leslie Lamport and his course that can be found on his &lt;a href="https://lamport.azurewebsites.net/tla/learning.html" rel="noopener noreferrer"&gt;website&lt;/a&gt;. In this part, we will consider the &lt;em&gt;Transaction Commit&lt;/em&gt; and &lt;em&gt;Two-Phase Commit&lt;/em&gt; algorithms.&lt;/p&gt;

&lt;h2&gt;
  
  
  Transaction Commit
&lt;/h2&gt;

&lt;p&gt;In the first part we consider the &lt;em&gt;Transaction Commit&lt;/em&gt; algorithm [&lt;a href="https://lamport.azurewebsites.net/video/consensus-on-transaction-commit.pdf" rel="noopener noreferrer"&gt;3&lt;/a&gt;], pp.2-4. In a distributed system, a transaction commit involves multiple resource managers (RMs) across different nodes that must unanimously decide to either commit or abort a transaction. The protocol ensures that all RMs either reach a committed state or an aborted state, supported by the conditions of stability and consistency, which mandate that once an RM reaches one of these states, it cannot revert, and no RM can be in the opposite state.&lt;/p&gt;

&lt;p&gt;A database transaction is performed by a collection of processes called &lt;em&gt;resource managers&lt;/em&gt;. A transaction can either &lt;em&gt;commit&lt;/em&gt; or &lt;em&gt;abort&lt;/em&gt;. It can commit only iif all resource managers are prepared to commit and must abort if any resource manager wants to abort.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;All resource managers must agree on whether a transaction is committed or aborted.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Unfortunately, this platform does not fully support the formatting we have on our website, so if you are interested, please read the &lt;a href="https://www.inferara.com/en/blog/commit-to-marriage-with-tla-plus-2/" rel="noopener noreferrer"&gt;original blog post&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>tlc</category>
      <category>tlaplus</category>
    </item>
    <item>
      <title>Do not die hard with TLA+ pt.1</title>
      <dc:creator>Georgii</dc:creator>
      <pubDate>Sun, 16 Feb 2025 04:12:19 +0000</pubDate>
      <link>https://dev.to/0xgeorgii/do-not-die-hard-with-tla-pt1-5d4b</link>
      <guid>https://dev.to/0xgeorgii/do-not-die-hard-with-tla-pt1-5d4b</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This is the first blog post in the series of the conspectus of the “Introduction to TLA+” course by Leslie Lamport. It would directly follow the course structure and would be a good reference for those who are taking the course because it adds some additional information and explanations to the course material. So all credits are to Leslie Lamport and his course, which can be found on his &lt;a href="https://lamport.azurewebsites.net/tla/learning.html" rel="noopener noreferrer"&gt;website&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;TLA + is based on temporal logic so you may read about it in our &lt;a href="https://www.inferara.com/en/blog/ltl-ctl-for-smart-contract-security/" rel="noopener noreferrer"&gt;LTL and CTL Applications for Smart Contracts Security&lt;/a&gt; blog post.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inroduction to TLA+
&lt;/h2&gt;

&lt;p&gt;TLA+ is a language for &lt;strong&gt;high-level&lt;/strong&gt; (design level, above the code) systems (modules, algorithms, etc.) modelling and consists of the following components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TLC — the model checker;&lt;/li&gt;
&lt;li&gt;TLAPS — the TLA+ proof system;&lt;/li&gt;
&lt;li&gt;TLA+ Toolbox — the IDE.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;TLA+ system is used to model &lt;strong&gt;critical parts&lt;/strong&gt; of digital systems, abstracting away less-critical parts and lower-level implementation details. TLA+ was designed for designing concurrent and &lt;strong&gt;distributed systems&lt;/strong&gt; in order to help find and correct &lt;strong&gt;design errors&lt;/strong&gt; that are hard to find by testing and &lt;strong&gt;before&lt;/strong&gt; writing any single line of code.&lt;/p&gt;

&lt;p&gt;OpenComRTOS is a commercial network-centric, real-time operating system [&lt;a href="https://en.wikipedia.org/wiki/OpenComRTOS" rel="noopener noreferrer"&gt;3&lt;/a&gt;] that heavily used TLA+ during the design and development process and shared their experience in the freely available book [&lt;a href="https://www.researchgate.net/publication/315385340_Formal_Development_of_a_Network-Centric_RTOS" rel="noopener noreferrer"&gt;4&lt;/a&gt;]. And showed that using design gratefully reduces the code base size and number of errors and boosts the engineering view overall.&lt;/p&gt;

&lt;p&gt;Consequently, TLA+ provides programmers and engineers &lt;strong&gt;a new way of thinking&lt;/strong&gt; that &lt;strong&gt;makes them better programmers and engineers&lt;/strong&gt; even when TLA+ are not useful. TLA+ forces engineers to think more abstractly.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Abstraction — the process of removing irrelevant details and the most important part of engineering. Without them, we cannot design and understand small systems.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;An example of using TLA+ in a huge company for verifying a system that many of us use daily is Amazon Web Services. They use TLA+ to verify the correctness of their distributed algorithms and AWS system design [&lt;a href="https://www.amazon.science/publications/how-amazon-web-services-uses-formal-methods" rel="noopener noreferrer"&gt;5&lt;/a&gt;]. The problem of algorithms and communication in distributed systems is well described in Leslie Lamport's paper "Time, Clocks, and the Ordering of Events in a Distributed System" [&lt;a href="https://amturing.acm.org/p558-lamport.pdf" rel="noopener noreferrer"&gt;6&lt;/a&gt;].&lt;/p&gt;

&lt;p&gt;A system design is expressed in a formal way called &lt;em&gt;specification&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Specification — the precise high-level model.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;TLA+ defines the specification, but it cannot produce the code. But it helps come with much clearer architecture and write more precise, accurate, in some cases compact code. It is able to check properties that express conditions on an individual execution (a system satisfies a property if and only if every single execution satisfies it).&lt;/p&gt;

&lt;p&gt;The underlying abstraction of TLA+ is as follows: an execution of a system is represented as a sequence of discrete steps, where a step is the change from one state to the next one:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;discrete — continuous evolution is a sequence of discrete events (computer is a discrete events-based system);&lt;/li&gt;
&lt;li&gt;sequence — a concurrent system can be simulated with a sequential program;&lt;/li&gt;
&lt;li&gt;step — a state change;&lt;/li&gt;
&lt;li&gt;state — an assignment of values to variables.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Behavior — a sequence of states.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A state machine in the context of TLA+ system is described by:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;all possible initial states – [what the variables are] and [their possible initial values];&lt;/li&gt;
&lt;li&gt;what next states can follow any given state – a relation between their values in the current state and their possible values in the next state;&lt;/li&gt;
&lt;li&gt;halts if there is no possible next state.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;Control state — the next to be executed statement.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;State machines eliminate low-level implementation details, and TLA+ is a language to describe state machines.&lt;/p&gt;

&lt;h2&gt;
  
  
  State Machines in TLA+
&lt;/h2&gt;

&lt;p&gt;TLA+ uses ordinary, simple math. Consider a defined state machine example for the following &lt;code&gt;C&lt;/code&gt; code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;someNumber&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to turn this code into the TLA+ state machine definition we need to pack the execution flow of this code into states (sets of variables). For the given example, it is obvious how to define &lt;code&gt;i&lt;/code&gt; variable. But we also need to instantiate the control state. We call it a &lt;code&gt;pc&lt;/code&gt; such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;pc = "start"&lt;/code&gt; = &lt;code&gt;i = someNumber();&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pc = "middle"&lt;/code&gt; = &lt;code&gt;i = i + 1;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pc = "done"&lt;/code&gt; = execution is finished.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Assume for this example &lt;code&gt;someNumber()&lt;/code&gt; returns an integer from the &lt;code&gt;[0:1000]&lt;/code&gt; interval.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To define the system we need to define the &lt;em&gt;initial&lt;/em&gt; state of the system the the &lt;em&gt;next&lt;/em&gt; possible system state, expressed as a formula, that can be reached from the current state.&lt;/p&gt;

&lt;p&gt;Here is the &lt;strong&gt;formula&lt;/strong&gt; of the &lt;code&gt;C&lt;/code&gt; code above, not the sequence of execution.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initial-state formula: &lt;code&gt;(i = 0) /\ (pc = "start")&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Next-state formula:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\/    /\ pc = "start"
      /\ i' \in 0..1000
      /\ pc' = "middle"
\/    /\ pc = "middle"
      /\ i' = i + 1
      /\ pc' = "done"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since this is the formula, it respects such formulas properties as commutativity, associativity, etc.&lt;/p&gt;

&lt;p&gt;Sub-formulas can also be extracted into their own definitions to make a spec more compact.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A == /\ pc = "start"
     /\ i' \in 0..1000
     /\ pc' = "middle"

B == /\ pc = "middle"
     /\ i' = i + 1
     /\ pc' = "done"

Next == A \/ B
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From this spec, we see that there are two possible &lt;em&gt;next states&lt;/em&gt; that can be reached beginning from the &lt;em&gt;initial&lt;/em&gt; state. &lt;code&gt;A&lt;/code&gt; states the beginning of the execution, assigning a number to &lt;code&gt;i&lt;/code&gt; and moving to the next &lt;code&gt;pc&lt;/code&gt; state equals &lt;code&gt;p'&lt;/code&gt;, &lt;code&gt;B&lt;/code&gt; states the increment of &lt;code&gt;i&lt;/code&gt; and moving to the final state.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources and tools
&lt;/h2&gt;

&lt;p&gt;There are not many learning resources of TLA+; however, there are some that we need to mention:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://lamport.azurewebsites.net/tla/learning.html" rel="noopener noreferrer"&gt;Learning TLA+&lt;/a&gt; — a portal with useful links;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/tlaplus/tlaplus/releases" rel="noopener noreferrer"&gt;TLA+ toolbox binaries&lt;/a&gt; — a &lt;code&gt;Java&lt;/code&gt; based TLA+ IDE;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://gist.github.com/rain1024/98dd5e2c6c8c28f9ea9d" rel="noopener noreferrer"&gt;pdflatex&lt;/a&gt; — a required component to render &lt;code&gt;pdf&lt;/code&gt;;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Model Checking
&lt;/h2&gt;

&lt;p&gt;Now, let us talk about TLC and model-checking-related topics.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;TLC computes all possible behaviors allowed by the spec. More precisely, TLC checks a &lt;em&gt;model&lt;/em&gt; of the specification.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;TLC reports &lt;strong&gt;deadlock&lt;/strong&gt; if execution stopped when it was not supposed to;&lt;/li&gt;
&lt;li&gt;TLC reports &lt;strong&gt;termination&lt;/strong&gt; if execution stopped when it was supposed to.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;TLA+ allows writing theorems and formal proofs of those theorems.&lt;br&gt;
TLAPS (&lt;code&gt;TLA&lt;/code&gt; proof system) is a tool for checking those proofs, it can check proofs of correctness of algorithms.&lt;/p&gt;

&lt;p&gt;Practically, the term &lt;em&gt;spec&lt;/em&gt; (a specification) means:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the set of modules, including imported modules consists of &lt;code&gt;.tla&lt;/code&gt; files;&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;TLA&lt;/code&gt; formula specifies the set of allowed behaviors of the system or algorithm being specified.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A specification may contain multiple models. The model tells TLC what it should do. Here are the parts of the model that must be explicitly chosen:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;what the behavior spec is (The behavior spec is the formula or pair of formulas that describe the possible behaviors of the system or algorithm you want to check);&lt;/li&gt;
&lt;li&gt;what TLC should check;&lt;/li&gt;
&lt;li&gt;what values to substitute for constant parameters.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Behavior spec
&lt;/h3&gt;

&lt;p&gt;There are two ways to write the behavior spec:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Init and Next&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;A pair of formulas that specify the initial state and the next-state relation, respectively.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Single formula&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;A single temporal formula of the form  &lt;code&gt;Init and [][Next]_{vars} and F&lt;/code&gt;,  where

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Init&lt;/code&gt; is the initial predicate;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Next&lt;/code&gt; is the next-state relation;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;vars&lt;/code&gt; is the tuple of variables;&lt;/li&gt;
&lt;li&gt;and &lt;code&gt;F&lt;/code&gt; is an optional fairness formula.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The only way to write a behavior spec that includes fairness is with a temporal formula, otherwise, a spec would not have variables and in this case, TLC will check assumptions and evaluate a constant expression.&lt;/p&gt;

&lt;p&gt;There are three kinds of properties of the behavior spec that TLC can check:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Deadlock&lt;/strong&gt; — A &lt;em&gt;deadlock&lt;/em&gt; is said to occur in a state for which the next-state relation allows no successor states;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Invariants&lt;/strong&gt; — a state predicate that is true of all reachable states--that is, states that can occur in a behavior allowed by the behavior spec;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Properties&lt;/strong&gt; — TLC can check if the behavior spec satisfies (implies) a temporal property, which is expressed as a temporal-logic formula.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Model
&lt;/h3&gt;

&lt;p&gt;The most basic part of a model is a set of assignments of values to declared constants.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ordinary assignment&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It is possible to set the value of the constant to any constant TLA+ expression that contains only symbols defined in the spec.  The expression can even include declared constants, as long as the value assigned to a constant does not depend on that constant (escape circular dependencies). A model must specify the values of all declared constants.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Model value&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A model value is an unspecified value that TLC considers to be unequal to any value that you can express in TLA+.  You can substitute the set  &lt;code&gt;{p1, p2, p3\}&lt;/code&gt;  of three model values for  &lt;code&gt;Proc&lt;/code&gt;.  If by mistake you write an expression like  &lt;code&gt;p+1&lt;/code&gt; where the value of  &lt;code&gt;p&lt;/code&gt; is a process, TLC will report an error when it tries to evaluate that expression because it knows that a process is a model value and thus not a number.  An important reason for substituting a set of model values for  &lt;code&gt;Proc&lt;/code&gt;  is to let TLC take advantage of symmetry.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Example: &lt;code&gt;NotANat == CHOOSE n : n \notin Nat&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It defines  &lt;code&gt;NotANat&lt;/code&gt;  to be an arbitrary value that is not a natural number.  TLC cannot evaluate this definition because it cannot evaluate the unbounded  &lt;code&gt;CHOOSE&lt;/code&gt;  expression.  To allow TLC to handle the spec, you need to substitute a model value for  &lt;code&gt;NotANat&lt;/code&gt;.  The best model value to substitute for it is one named  &lt;code&gt;NotANat&lt;/code&gt;.  This is done by the Definition Override.  The TLA+ Toolbox creates the appropriate entry in that section when it creates a model if it finds a definition having the precise syntax above or the syntax:&lt;br&gt;
&lt;code&gt;NotANat == CHOOSE n: ~(n \in Nat)&lt;/code&gt;, where &lt;code&gt;Nat&lt;/code&gt; can be any expression, and &lt;code&gt;NotANat&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt; can be any identifiers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Model values can be typed as follows: a model value has type  &lt;code&gt;T&lt;/code&gt;  if and only if its name begins with the two characters  &lt;code&gt;T_&lt;/code&gt; .&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A model value declared in the model can be used as an ordinary value in any expression that is part of the model's specification.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Symmetry&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Consider a specification of a memory system containing a declared constant &lt;code&gt;Val&lt;/code&gt; that represents the set of possible values of a memory register. The set &lt;code&gt;Val&lt;/code&gt; of values is probably a &lt;em&gt;symmetry set&lt;/em&gt; for the memory system's behavior specification, meaning that permuting the elements in the set of values does not change whether or not a behavior satisfies that behavior spec. TLC can take advantage of this to speed up its checking.  Suppose we substitute a set &lt;code&gt;{v1, v2, v3}&lt;/code&gt; of model values for &lt;code&gt;Val&lt;/code&gt;. We can use the &lt;em&gt;Symmetry set&lt;/em&gt; option to declare this set of model values to be a symmetry set of the behavior spec. This will reduce the number of reachable states that TLC has to examine by up to &lt;code&gt;3!&lt;/code&gt;, or &lt;code&gt;6&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can declare more than one set of model values to be a symmetry set. However, the union of all the symmetry sets cannot contain two typed model values with different types.&lt;/p&gt;

&lt;p&gt;TLC does not check if a set you declare to be a symmetry set really is one. If you declare a set to be a symmetry set and it isn't, then TLC can fail to find an error that it otherwise would find. An expression is &lt;em&gt;symmetric&lt;/em&gt; for a set &lt;code&gt;S&lt;/code&gt; if and only if interchanging any two values of &lt;code&gt;S&lt;/code&gt; does not change the value of the expression. The expression &lt;code&gt;{{v1, v2}, {v1, v3}, {v2, v3}}&lt;/code&gt; is symmetric for the set &lt;code&gt;{v1, v2, v3}&lt;/code&gt; — for example, interchanging &lt;code&gt;v1&lt;/code&gt; and &lt;code&gt;v3&lt;/code&gt; in this expression produces &lt;code&gt;{{v3, v2}, {v3, v1}, {v2, v1}}&lt;/code&gt;, which is equal to the original expression. You should declare a set &lt;code&gt;S&lt;/code&gt; of model values to be a symmetry set only if the specification and all properties you are checking are symmetric for &lt;code&gt;S&lt;/code&gt; after the substitutions for constants and defined operators specified by the model are made. For example, you should not declare &lt;code&gt;{v1, v2, v3}&lt;/code&gt; to be a symmetry set if the model substitutes &lt;code&gt;v1&lt;/code&gt; for some constant. The only TLA+ operator that can produce a non-symmetric expression when applied to a symmetric expression is &lt;code&gt;CHOOSE&lt;/code&gt;. For example, the expression &lt;code&gt;CHOOSE x \in {v1, v2, v3} : TRUE&lt;/code&gt; is not symmetric for &lt;code&gt;{v1, v2, v3}&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Symmetry sets should not be used when checking liveness properties. Doing so can make TLC fail to find errors, or to report nonexistent errors.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Die Hard
&lt;/h2&gt;

&lt;p&gt;Die Hard is an action movie from 1988. In this movie, there is a scene where our heroes need to solve a problem with two jugs in order to disable a bomb. The problem is to measure 4 gallons of water using 3 and 5-gallon jugs.&lt;/p&gt;

&lt;p&gt;For the plot, search: "Die Hard Jugs problem" on YouTube or simply click here 🙂. We will solve this problem using TLA+.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/2vdF6NASMiE"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;First, we need to write the behavior. Let values of &lt;code&gt;small&lt;/code&gt; and &lt;code&gt;big&lt;/code&gt; represent a number of gallons in each jug.&lt;/p&gt;

&lt;p&gt;

&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;[small0 big0 ]→[small3 big0 ]→[small0 big3 ]→[small3 big3 ]→...
  \begin{bmatrix}
    small &amp;amp; 0 \
    big &amp;amp; 0 \
  \end{bmatrix} \rightarrow   \begin{bmatrix}
                                   small &amp;amp; 3 \
                                   big &amp;amp; 0 \
                              \end{bmatrix} \rightarrow     \begin{bmatrix}
                                                                 small &amp;amp; 0 \
                                                                 big &amp;amp; 3 \
                                                              \end{bmatrix} \rightarrow     \begin{bmatrix}
                                                                                                 small &amp;amp; 3 \
                                                                                                 big &amp;amp; 3 \
                                                                                              \end{bmatrix} \rightarrow ...

&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="minner"&gt;&lt;span class="mopen delimcenter"&gt;&lt;span class="delimsizing size1"&gt;[&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mtable"&gt;&lt;span class="col-align-c"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;s&lt;/span&gt;&lt;span class="mord mathnormal"&gt;ma&lt;/span&gt;&lt;span class="mord mathnormal"&gt;ll&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="arraycolsep"&gt;&lt;/span&gt;&lt;span class="arraycolsep"&gt;&lt;/span&gt;&lt;span class="col-align-c"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mspace"&gt; &lt;/span&gt;&lt;span class="mord mathnormal"&gt;bi&lt;/span&gt;&lt;span class="mord mathnormal"&gt;g&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="arraycolsep"&gt;&lt;/span&gt;&lt;span class="arraycolsep"&gt;&lt;/span&gt;&lt;span class="col-align-c"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mspace"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose delimcenter"&gt;&lt;span class="delimsizing size1"&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;→&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="minner"&gt;&lt;span class="mopen delimcenter"&gt;&lt;span class="delimsizing size1"&gt;[&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mtable"&gt;&lt;span class="col-align-c"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;s&lt;/span&gt;&lt;span class="mord mathnormal"&gt;ma&lt;/span&gt;&lt;span class="mord mathnormal"&gt;ll&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="arraycolsep"&gt;&lt;/span&gt;&lt;span class="arraycolsep"&gt;&lt;/span&gt;&lt;span class="col-align-c"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;3&lt;/span&gt;&lt;span class="mspace"&gt; &lt;/span&gt;&lt;span class="mord mathnormal"&gt;bi&lt;/span&gt;&lt;span class="mord mathnormal"&gt;g&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="arraycolsep"&gt;&lt;/span&gt;&lt;span class="arraycolsep"&gt;&lt;/span&gt;&lt;span class="col-align-c"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mspace"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose delimcenter"&gt;&lt;span class="delimsizing size1"&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;→&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="minner"&gt;&lt;span class="mopen delimcenter"&gt;&lt;span class="delimsizing size1"&gt;[&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mtable"&gt;&lt;span class="col-align-c"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;s&lt;/span&gt;&lt;span class="mord mathnormal"&gt;ma&lt;/span&gt;&lt;span class="mord mathnormal"&gt;ll&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="arraycolsep"&gt;&lt;/span&gt;&lt;span class="arraycolsep"&gt;&lt;/span&gt;&lt;span class="col-align-c"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mspace"&gt; &lt;/span&gt;&lt;span class="mord mathnormal"&gt;bi&lt;/span&gt;&lt;span class="mord mathnormal"&gt;g&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="arraycolsep"&gt;&lt;/span&gt;&lt;span class="arraycolsep"&gt;&lt;/span&gt;&lt;span class="col-align-c"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;3&lt;/span&gt;&lt;span class="mspace"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose delimcenter"&gt;&lt;span class="delimsizing size1"&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;→&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="minner"&gt;&lt;span class="mopen delimcenter"&gt;&lt;span class="delimsizing size1"&gt;[&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mtable"&gt;&lt;span class="col-align-c"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;s&lt;/span&gt;&lt;span class="mord mathnormal"&gt;ma&lt;/span&gt;&lt;span class="mord mathnormal"&gt;ll&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="arraycolsep"&gt;&lt;/span&gt;&lt;span class="arraycolsep"&gt;&lt;/span&gt;&lt;span class="col-align-c"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;3&lt;/span&gt;&lt;span class="mspace"&gt; &lt;/span&gt;&lt;span class="mord mathnormal"&gt;bi&lt;/span&gt;&lt;span class="mord mathnormal"&gt;g&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="arraycolsep"&gt;&lt;/span&gt;&lt;span class="arraycolsep"&gt;&lt;/span&gt;&lt;span class="col-align-c"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;3&lt;/span&gt;&lt;span class="mspace"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose delimcenter"&gt;&lt;span class="delimsizing size1"&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;→&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;...&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/p&gt;

&lt;p&gt;Filling a jug is a single step; there are no intermediate steps.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Real specifications are written to eliminate some kinds of errors.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;TLA+ has no type declarations; however, it is important to define a formula that asserts type correctness. It helps to understand the spec and TLC can check types by checking if such a formula is always &lt;code&gt;true&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TypeOK == /\ small \in 0..3
          /\ big   \in 0..5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we define that &lt;code&gt;small&lt;/code&gt; is an integer in the range &lt;code&gt;[0:3]&lt;/code&gt; and &lt;code&gt;big&lt;/code&gt; is an integer in the range &lt;code&gt;[0:5]&lt;/code&gt;. But this definition is not a part of the spec.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Initial-State Formula&lt;/strong&gt; &lt;code&gt;Init == small = 0 /\ big = 0&lt;/code&gt; defines the initial state of the system.&lt;br&gt;
The &lt;strong&gt;Next-State Formula&lt;/strong&gt; defines possible transfers from state to state and is usually written as &lt;code&gt;F_1 or F_2 or ... or F_n&lt;/code&gt;, where each formula &lt;code&gt;F_i&lt;/code&gt; allows a different kind of step.&lt;/p&gt;

&lt;p&gt;Our problem has 3 kinds of steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;fill a jug;&lt;/li&gt;
&lt;li&gt;empty a jug;&lt;/li&gt;
&lt;li&gt;pour from one jug into the other.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We define the spec as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Next == \/ FillSmall  \* fill the small jug
        \/ FillBig    \* fill the big jug
        \/ EmptySmall \* empty the small jug
        \/ EmptyBig   \* empty the big jug
        \/ SmallToBig \* pour water from small jug in the big jug
        \/ BigToSmall \* pour water from the big jug in the small jug
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The names of definitions (like &lt;code&gt;FillSmall&lt;/code&gt;, etc.) must be defined before the usage (precede the definition of &lt;code&gt;Next&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FillSmall == /\ small' = 3
             /\ big' = big
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When defining formulas we need to keep in mind thinking of the system as a whole and about steps as a transition from one state to another. In our case, it means that we cannot define &lt;code&gt;FillSmall&lt;/code&gt; as &lt;code&gt;FillSmall == small' = 3&lt;/code&gt; because this formula doesn't have a part defining the second part of the program state (&lt;code&gt;big&lt;/code&gt;). In other words, this formula turns &lt;code&gt;true&lt;/code&gt; if &lt;code&gt;small'&lt;/code&gt; equals &lt;code&gt;3&lt;/code&gt; and &lt;code&gt;big'&lt;/code&gt; equals whatever. But this is not correct. In fact, if we fill the small jug, we keep the big jug in the state it is without changes.&lt;/p&gt;

&lt;p&gt;Now, we define &lt;code&gt;SmallToBig&lt;/code&gt;. There are two possible cases we need to consider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SmallToBig == /\ IF big + small &amp;lt;= 5
                  THEN /* There is room -&amp;gt; empty small.
                  ELSE /* There is no room -&amp;gt; fill big.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SmallToBig == /\ IF big + small &amp;lt;= 5
                  THEN /\ big' = big + small
                       /\ small' = 0
                  ELSE /\ big' = 5
                       /\ small' = small - (5 - big)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;------------------------------ MODULE DieHard ------------------------------
EXTENDS Integers

VARIABLES small, big

TypeOK == /\ small \in 0..3
          /\ big \in 0..5

Init == /\ big = 0
        /\ small = 0

FillSmall == /\ small' = 3
             /\ big' = big

FillBig == /\ big' = 5
           /\ small' = small

EmptySmall == /\ small' = 0
              /\ big' = big

EmptyBig == /\ big' = 0
            /\ small' = small

SmallToBig == IF big + small =&amp;lt; 5
                THEN /\ big' = big + small
                     /\ small' = 0
                ELSE /\ big' = 5
                     /\ small' = small - (5 - big)

BigToSmall == IF big + small =&amp;lt; 3
               THEN /\ big' = 0
                    /\ small' = big + small
               ELSE /\ big' = big - (3 - small)
                    /\ small' = 3

Next == \/ FillSmall
        \/ FillBig
        \/ EmptySmall
        \/ EmptyBig
        \/ SmallToBig
        \/ BigToSmall

=============================================================================

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;p&gt;If we create and run a model for this spec we will see no errors and this is fine; however, it doesn't check any particular invariant of our spec.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Invariant is a formula that is &lt;code&gt;true&lt;/code&gt; in every reachable state.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We have defined a &lt;code&gt;TypeOK&lt;/code&gt; as a type definition for &lt;code&gt;small&lt;/code&gt; and &lt;code&gt;big&lt;/code&gt;, so we can add this formula as an invariant to check that this invariant is not broken.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff9q3r0f9b4joy9icqvad.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff9q3r0f9b4joy9icqvad.png" alt="Add typeok invariant" width="301" height="123"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we run it now, we still see no errors meaning &lt;code&gt;small&lt;/code&gt; and &lt;code&gt;big&lt;/code&gt; respecting their types in every reachable state.&lt;/p&gt;

&lt;p&gt;Now we can solve the &lt;em&gt;die-hard&lt;/em&gt; problem of pouring &lt;code&gt;big&lt;/code&gt; with exactly 4 gallons of water. To do it, we add a new invariant &lt;code&gt;big /= 4&lt;/code&gt; into the invariants section.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1tkjtm8p1uzw13n5cbue.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1tkjtm8p1uzw13n5cbue.png" alt="Add bigneg 4 invariant" width="301" height="148"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, this invariant works as a counterexample. An invariant is a formula that turns to &lt;code&gt;true&lt;/code&gt; in &lt;strong&gt;every&lt;/strong&gt; reachable state. We need to find a state (actually a state sequence) where &lt;code&gt;big = 4&lt;/code&gt;, so we negate this by the &lt;code&gt;/=&lt;/code&gt; symbol that equals &lt;code&gt;neq&lt;/code&gt;. With this new formula, if we run the model, it finds an error (a state where an invariant is broken) and shows a sequence of states that led to this state.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbcghqmn5jmr2j6f3aj4e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbcghqmn5jmr2j6f3aj4e.png" alt="Run result" width="619" height="709"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, we can see the exact steps that are required to be done to solve the problem and our heroes can move on.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.inferara.com/en/blog/do-not-die-hard-with-tla-plus-1/" rel="noopener noreferrer"&gt;Original publication&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://lamport.azurewebsites.net/tla/learning.html" rel="noopener noreferrer"&gt;Leslie Lamport. Learning TLA+&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.inferara.com/en/blog/ltl-ctl-for-smart-contract-security/" rel="noopener noreferrer"&gt;LTL and CTL Applications for Smart Contracts Security&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/OpenComRTOS" rel="noopener noreferrer"&gt;OpenComRTOS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.researchgate.net/publication/315385340_Formal_Development_of_a_Network-Centric_RTOS" rel="noopener noreferrer"&gt;Eric Verhulst, Raymond T. Boute, José Miguel Faria, Bernhard H C Sputh, Vitaliy Mezhuyev. Formal Development of a Network-Centric RTOS. January 2011. DOI:10.1007/978-1-4419-9736-4. ISBN: 978-1-4419-9735-7&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.amazon.science/publications/how-amazon-web-services-uses-formal-methods" rel="noopener noreferrer"&gt;Chris Newcombe, Tim Rath, Fan Zhang, Bogdan Munteanu, Marc Brooker and Michael Deardeuff.How Amazon Web Services uses formal methods. 2015, Communications of the ACM&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://amturing.acm.org/p558-lamport.pdf" rel="noopener noreferrer"&gt;Leslie Lamport. Time, Clocks, and the Ordering of Events in a Distributed System. 1978. Massachusetts Computer Associates, Inc.&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>tla</category>
      <category>modelchecking</category>
      <category>temporallogic</category>
    </item>
    <item>
      <title>Small and Big Step Semantics</title>
      <dc:creator>Georgii</dc:creator>
      <pubDate>Sat, 08 Feb 2025 04:18:02 +0000</pubDate>
      <link>https://dev.to/0xgeorgii/small-and-big-step-semantics-4m0d</link>
      <guid>https://dev.to/0xgeorgii/small-and-big-step-semantics-4m0d</guid>
      <description>&lt;p&gt;Small-step semantics and big-step semantics are two approaches used in the field of formal semantics within computer science, particularly in the study of programming languages and formal verification. These semantic models provide formal ways to describe how programs execute and are used to reason about the behavior of programs in a deductive way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Small-Step Semantics (Operational Semantics)
&lt;/h2&gt;

&lt;p&gt;Small-step semantics, also known as structural operational semantics, describes the execution of programs as a sequence of individual computation steps. Each step represents a single, atomic action, such as the evaluation of an expression, the execution of a statement, or a change in the program’s state.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Granularity: It focuses on the fine-grained, step-by-step execution process.&lt;/li&gt;
&lt;li&gt;Transition System: The execution is modelled as a transition system where each transition corresponds to a small step. The system is described by a set of rules that define how one program state transitions to another.&lt;/li&gt;
&lt;li&gt;Expressions and Commands: Both expressions and commands are evaluated using rules that specify how a single computational step proceeds.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Small-step semantics is particularly useful for understanding the dynamics of program execution, analyzing properties like termination, and reasoning about concurrent or interactive systems where the interleaving of actions matters.&lt;/p&gt;

&lt;h2&gt;
  
  
  Big-Step Semantics (Natural Semantics)
&lt;/h2&gt;

&lt;p&gt;Big-step semantics, also known as natural semantics, describes the execution of programs as a relation between an initial program state and its final state after the program has completed execution. Instead of focusing on the individual steps of execution, it captures the overall effect of executing a piece of code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Completeness: It emphasizes the result of executing an expression or a command, usually ignoring the intermediate steps.&lt;/li&gt;
&lt;li&gt;Evaluation Relations: The semantics are given in terms of evaluation relations that directly relate an initial state and an expression or command to their final state and result.&lt;/li&gt;
&lt;li&gt;Suitability: Big-step semantics is well-suited for reasoning about the final outcomes of program execution, making it useful for proving properties like correctness and equivalence of programs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Small-step semantics offer a detailed view of execution, allowing for the analysis of intermediate states, which is essential for understanding how specific computations unfold over time. Big-step semantics provides a more abstract, high-level view, focusing on the initial and final states without detailing how those states are reached.&lt;/p&gt;

&lt;p&gt;Small-step semantics is often preferred in scenarios where the process of computation is as important as the result, such as in interactive systems, concurrent programming, and step-wise debugging tools. Big-step semantics is typically used when the interest lies in the outcome of computation, such as verifying the correctness of algorithms or functional equivalences between programs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.inferara.com/en/blog/small-and-big-step-semantics/" rel="noopener noreferrer"&gt;Original publication&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>LTL and CTL Applications for Smart Contracts Security</title>
      <dc:creator>Georgii</dc:creator>
      <pubDate>Sat, 01 Feb 2025 02:54:51 +0000</pubDate>
      <link>https://dev.to/0xgeorgii/ltl-and-ctl-applications-for-smart-contracts-security-32im</link>
      <guid>https://dev.to/0xgeorgii/ltl-and-ctl-applications-for-smart-contracts-security-32im</guid>
      <description>&lt;p&gt;Both &lt;code&gt;LTL&lt;/code&gt; and &lt;code&gt;CTL&lt;/code&gt; are fragments of propositional logic and parts of &lt;code&gt;CTL&lt;/code&gt;*.&lt;/p&gt;

&lt;h2&gt;
  
  
  Linear Temporal Logic (LTL)
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;LTL&lt;/code&gt; is a modal temporal logic with modalities referring to time [&lt;a href="https://en.wikipedia.org/wiki/Linear_temporal_logic" rel="noopener noreferrer"&gt;1&lt;/a&gt;]. In the context of smart contracts, &lt;code&gt;LTL&lt;/code&gt; can be used to specify and verify properties that must hold over the sequences of states that a contract might pass through.&lt;/p&gt;

&lt;p&gt;For example, it allows developers to make assertions like, "If condition 

&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;XX&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;X&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 is 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;truetrue&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;t&lt;/span&gt;&lt;span class="mord mathnormal"&gt;r&lt;/span&gt;&lt;span class="mord mathnormal"&gt;u&lt;/span&gt;&lt;span class="mord mathnormal"&gt;e&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, then condition 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;YY&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;Y&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 will eventually be 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;truetrue&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;t&lt;/span&gt;&lt;span class="mord mathnormal"&gt;r&lt;/span&gt;&lt;span class="mord mathnormal"&gt;u&lt;/span&gt;&lt;span class="mord mathnormal"&gt;e&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
" (expressed as 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;X  ⟹  ◊YX \implies \lozenge Y&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;X&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;⟹&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord amsrm"&gt;◊&lt;/span&gt;&lt;span class="mord mathnormal"&gt;Y&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 in &lt;code&gt;LTL&lt;/code&gt;). This is particularly useful for smart contracts, which may need to guarantee certain outcomes after a sequence of events.&lt;/p&gt;

&lt;p&gt;Imagine a smart contract that has a critical function to ensure that once a user deposits a certain amount of assets, they will eventually receive interest payments. With &lt;code&gt;LTL&lt;/code&gt;, we could formally verify this property by expressing it as:&lt;/p&gt;

&lt;p&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;G(deposit  ⟹  F interest payment)
G(\text{deposit} \implies F\text{ interest payment})
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;G&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord text"&gt;&lt;span class="mord"&gt;deposit&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;⟹&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;F&lt;/span&gt;&lt;span class="mord text"&gt;&lt;span class="mord"&gt; interest payment&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/p&gt;

&lt;p&gt;This &lt;code&gt;LTL&lt;/code&gt; formula states that it is globally 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;truetrue&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;t&lt;/span&gt;&lt;span class="mord mathnormal"&gt;r&lt;/span&gt;&lt;span class="mord mathnormal"&gt;u&lt;/span&gt;&lt;span class="mord mathnormal"&gt;e&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 (
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;GG&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;G&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
) that if a deposit action occurs, it will be followed eventually (
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;FF&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;F&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
) by an interest payment. This kind of temporal logic allows developers and security analysts to prove that the smart contract will behave as intended over time.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;LTL&lt;/code&gt; uses temporal operators to express logical statements about the future, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;GG&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;G&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 (Globally): A condition must hold in all future states.&lt;/li&gt;
&lt;li&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;FF&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;F&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 (Finally): A condition will eventually hold in some future state.&lt;/li&gt;
&lt;li&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;XX&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;X&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 (neXt): A condition will hold in the next state.&lt;/li&gt;
&lt;li&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;UU&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;U&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 (Until): A condition will hold until another condition holds.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Computation Tree Logic (CTL)
&lt;/h2&gt;

&lt;p&gt;While &lt;code&gt;LTL&lt;/code&gt; is linear and considers the flow of time as a single path, &lt;code&gt;CTL&lt;/code&gt; allows for branching time, where multiple future possibilities can be considered from any given point [&lt;a href="https://en.wikipedia.org/wiki/Computation_tree_logic" rel="noopener noreferrer"&gt;2&lt;/a&gt;]. &lt;code&gt;CTL&lt;/code&gt; introduces path quantifiers to specify properties of computation trees — structures representing all possible executions of a system from any given state.&lt;/p&gt;

&lt;p&gt;The two path quantifiers are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;AA&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 (for All paths): Specifies that a property must hold on all possible future paths.&lt;/li&gt;
&lt;li&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;EE&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;E&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 (there Exists a path): Specifies that there is at least one possible future path where a property holds.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;CTL&lt;/code&gt; uses the same temporal operators as &lt;code&gt;LTL&lt;/code&gt;, but prefixed with path quantifiers. So, you might see formulas like 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;AGAG&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;span class="mord mathnormal"&gt;G&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 (on all paths, globally) or 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;EFEF&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;EF&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 (there exists a path where finally).&lt;/p&gt;

&lt;p&gt;For instance, a smart contract might have a function that only allows funds to be withdrawn by a specific party after a certain date. &lt;code&gt;CTL&lt;/code&gt; can be used to validate this by expressing properties like "For all paths, if the withdrawal function is called, then for every possible continuation of that path, either the caller is the authorized party, and the date is correct, or the withdrawal fails."&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;CTL&lt;/code&gt;, this could be expressed as:&lt;/p&gt;

&lt;p&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;A[withdraw  ⟹  (E[caller=authorized∧date≥specified]∨E[transaction reverted])]
A[\text{withdraw} \implies (E[\text{caller} = \text{authorized} \land \text{date} \geq \text{specified}] \lor E[\text{transaction reverted}])]
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;span class="mopen"&gt;[&lt;/span&gt;&lt;span class="mord text"&gt;&lt;span class="mord"&gt;withdraw&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;⟹&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;E&lt;/span&gt;&lt;span class="mopen"&gt;[&lt;/span&gt;&lt;span class="mord text"&gt;&lt;span class="mord"&gt;caller&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord text"&gt;&lt;span class="mord"&gt;authorized&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;∧&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord text"&gt;&lt;span class="mord"&gt;date&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;≥&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord text"&gt;&lt;span class="mord"&gt;specified&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;]&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;∨&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;E&lt;/span&gt;&lt;span class="mopen"&gt;[&lt;/span&gt;&lt;span class="mord text"&gt;&lt;span class="mord"&gt;transaction reverted&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;])]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/p&gt;

&lt;p&gt;This states that along all paths (
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;AA&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
), if a withdrawal attempt occurs, there exists a path (
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;EE&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;E&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
) where either the caller is authorized and the date is on or after the specified date, or there exists a path where the transaction is reverted, ensuring funds can't be incorrectly withdrawn.&lt;/p&gt;

&lt;p&gt;Keeping in mind: &lt;code&gt;LTL&lt;/code&gt; synthesis and the problem of verification of games against an &lt;code&gt;LTL&lt;/code&gt; winning condition is 2EXPTIME-complete [&lt;a href="https://en.wikipedia.org/wiki/2-EXPTIME" rel="noopener noreferrer"&gt;3&lt;/a&gt;].&lt;/p&gt;

&lt;p&gt;We think these logics can be used to verify; hence, cover high-level logical scenarios for smart-contract as within a single transaction as in a sequence of blocks.&lt;/p&gt;

&lt;p&gt;Of course, for such digital systems as smart contracts, design and modelling based on temporal logic should be done before the actual implementation. [The same is true of any other considered digital system]. This is a part of the &lt;strong&gt;Verification-Driven Development&lt;/strong&gt; process [&lt;a href="https://www.inferara.com/en/papers/verification-driven-development/" rel="noopener noreferrer"&gt;4&lt;/a&gt;] and to reduce the number of possible errors, it must be a predecessor of code.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Linear_temporal_logic" rel="noopener noreferrer"&gt;Linear Temporal Logic&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Computation_tree_logic" rel="noopener noreferrer"&gt;Computation Tree Logic&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/2-EXPTIME" rel="noopener noreferrer"&gt;2EXPTIME&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.inferara.com/en/papers/verification-driven-development/" rel="noopener noreferrer"&gt;Verification-Driven Development&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ltl</category>
      <category>ctl</category>
      <category>smartcontract</category>
      <category>security</category>
    </item>
    <item>
      <title>Why use formal specification</title>
      <dc:creator>Georgii</dc:creator>
      <pubDate>Sat, 25 Jan 2025 04:25:31 +0000</pubDate>
      <link>https://dev.to/0xgeorgii/why-use-formal-specification-mg5</link>
      <guid>https://dev.to/0xgeorgii/why-use-formal-specification-mg5</guid>
      <description>&lt;h2&gt;
  
  
  Why use formal specification
&lt;/h2&gt;

&lt;p&gt;Imagine a programmer tasked with a challenge: to bring a Dutch auction to life through code. With an arsenal of skill and determination, they approach this journey, piecing together logic and functionality, guided by what feels right and what their experience dictates.&lt;/p&gt;

&lt;p&gt;Once they reach a point where the digital auction seems ready to face the world, they step back, inviting testers into this creation to scrutinize his work, to ensure it behaves as intended. In this dance between creation and evaluation, both programmers and testers lean on a set of guidelines that are more akin to unwritten rules than formal commandments. This ambiguity can also open a door to endless interpretations, a breeding ground for misunderstandings.&lt;/p&gt;

&lt;p&gt;Testers navigate through scenarios they believe will prove the auction’s readiness. Each discovery of a flaw sends the project into a loop of corrections, where solving one problem might unwittingly birth another, trapping them in a potentially endless cycle of tweaking.&lt;/p&gt;

&lt;p&gt;Contrast this with a world where every stroke of the programmer’s keyboard is directed by a formal specification, a map that outlines the operational semantics of the program with precision. In this realm, the programmer’s role transforms into one of diligent adherence, ensuring each line of code aligns with the predefined semantics, sculpting the auction into its ideal form.&lt;/p&gt;

&lt;p&gt;The tester’s role, too, shifts focus — now, they must verify that the real-world behaviour of the code, shaped by the interplay between compiler, operating system, and beyond, truly matches those expectations laid out in the specification. This narrative isn’t just about building and testing software; it’s about navigating the accurate balance between creativity and conformity, between the freedom of interpretation and the clarity of specification.&lt;/p&gt;

&lt;p&gt;It’s a journey through the complexities of bringing a concept to life, where each decision can lead to success or endless revision, and the path chosen can make all the difference.&lt;/p&gt;

&lt;p&gt;You can read about the software development based on the specification/verification, or as we call it Verification Driven Development presented as a mathematical formalism in our &lt;a href="https://www.inferara.com/en/papers/verification-driven-development/" rel="noopener noreferrer"&gt;post&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>formalmethods</category>
      <category>coq</category>
      <category>formalspecification</category>
      <category>inference</category>
    </item>
  </channel>
</rss>
