{"id":13472914,"url":"https://github.com/lorin/tla-linearizability","last_synced_at":"2026-01-28T01:08:42.967Z","repository":{"id":41885084,"uuid":"153871211","full_name":"lorin/tla-linearizability","owner":"lorin","description":"Reading the linearizability paper with TLA+","archived":false,"fork":false,"pushed_at":"2022-04-24T23:28:22.000Z","size":229,"stargazers_count":49,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-30T03:37:45.658Z","etag":null,"topics":["linearizability","tlaplus"],"latest_commit_sha":null,"homepage":null,"language":"TLA","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lorin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-10-20T04:58:49.000Z","updated_at":"2025-04-09T21:28:45.000Z","dependencies_parsed_at":"2022-08-11T20:10:29.246Z","dependency_job_id":null,"html_url":"https://github.com/lorin/tla-linearizability","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/lorin/tla-linearizability","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorin%2Ftla-linearizability","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorin%2Ftla-linearizability/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorin%2Ftla-linearizability/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorin%2Ftla-linearizability/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lorin","download_url":"https://codeload.github.com/lorin/tla-linearizability/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorin%2Ftla-linearizability/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28831233,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-27T23:29:49.665Z","status":"ssl_error","status_checked_at":"2026-01-27T23:25:58.379Z","response_time":168,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["linearizability","tlaplus"],"created_at":"2024-07-31T16:00:59.054Z","updated_at":"2026-01-28T01:08:42.952Z","avatar_url":"https://github.com/lorin.png","language":"TLA","funding_links":[],"categories":["TLA"],"sub_categories":[],"readme":"# Reading the Herlihy \u0026 Wing Linearizability paper with TLA+\n\nThis repository contains a TLA+ model for checking if an object history is\nlinearizable.\n\nSee also: [Reading the Herlihy \u0026 Wing Linearizability paper with TLA+, part 2: prophecy variables](https://github.com/lorin/tla-prophecy)\n\nThe [Herlihy \u0026 Wing 1990](http://dx.doi.org/10.1145/78969.78972) paper entitled\n*Linearizability: a correctness condition for concurrent objects*\nintroduced *linearizability* as a correctness condition for reasoning about the\nbehavior of concurrent data structures.\n\nPeter Bailis's blog entry [Linearizability versus Serializability][bailis-lin]\nhas a good definition:\n\n\u003e Linearizability is a guarantee about single operations on single objects. It\n\u003e provides a real-time (i.e., wall-clock) guarantee on the behavior of a set of\n\u003e single operations (often reads and writes) on a single object (e.g.,\n\u003e distributed register or data item).\n\n\u003e In plain English, under linearizability, writes should appear to be\n\u003e instantaneous. Imprecisely, once a write completes, all later reads (where\n\u003e “later” is defined by wall-clock start time) should return the value of that\n\u003e write or the value of a later write. Once a read returns a particular value,\n\u003e all later reads should return that value or the value of a later write.\n\n[bailis-lin]: http://www.bailis.org/blog/linearizability-versus-serializability/\n\nThere are several linearizable data stores whose behaviors have been specified\nwith [TLA+]:\n\n* Lamport's book [Specifying Systems][specifying-systems] uses an example of a linearizable memory in\nSection 5.3.\n* The [Raft][raft] Consensus algorithm supports linearizable semantics and has a\n  [TLA+ specification][raft-tla] written by [Diego Ongaro].\n* [Azure Cosmos DB][cosmosdb] supports a consistency model with linearizable reads and has \n  [high-level TLA+ specifications][cosmosdb-tla] written by [Murat Demirbas].\n  \n[Diego Ongaro]: https://ongardie.net/diego/\n[Murat Demirbas]: http://muratbuffalo.blogspot.com/\n\n[TLA+]: https://lamport.azurewebsites.net/tla/tla.html\n[specifying-systems]: https://lamport.azurewebsites.net/tla/book.html\n[raft]: https://raft.github.io/\n[raft-tla]: https://github.com/ongardie/raft.tla\n[cosmosdb]: http://cosmosdb.com/\n[cosmosdb-tla]: https://github.com/Azure/azure-cosmos-tla\n\nHowever, none of these models use the definition of linearizability outlined in\nthe original paper by Herlihy \u0026 Wing.\n\nIndeed, the definition in the original paper is awkward to use with TLC (the\nTLA+ model checker), because it involves reordering of event histories, which\nleads to state space explosion.\n\nHowever, I found it useful to work directly with the definition of\nlinearizability as an exercise for practicing with TLA+, as well as to gain a\nbetter understanding of how linearizability is defined.\n\n## Files in this repository\n\n* [Linearizability.tla](Linearizability.tla) contains a definition of\n  linearizability. In particular, the `IsLinearizableHistory` operator\n  returns true if an event history is linearizable.\n* [LinQueue.tla](LinQueue.tla) instantiates the Linearizability module for\n  a queue (FIFO) object. It contains an `IsLinearizableHistory` operator that returns true\n  if an event history for a queue is linearizable.\n* [LinQueuePlusCal.tla](LinQueuePlusCal.tla) is a PlusCal version. If a\n  history is linearizable, using TLC with this module makes it easy to see\n  a valid linearization.\n* [Utilities.tla](Utilities.tla) conatins some general-purpose operators.\n\n## Histories\n\nOn p469, the paper defines a *linearizable object* as an object whose concurrent\nhistories are linearizable with respect to some sequential specification.\n\nTo understand linearizability, we need to understand what a concurrent history\nis.\n\nAs a motivating example, figure one from the paper shows several possible\nhistories for a concurrently accessed queue.  Figures 1(a) and 1(c) are\nlinearizable, and Figures 1(b) and 1(d) are not.\n\n![Figure 1](fig1.png)\n\nEach interval represents an operation. There are two types of operations: {E,\nD} for enqueue and dequeue. There are three processes: {A, B, C}. There are three\nitems that can be added to the queue: {x, y, z}.\n\nHere's how I modeled these four histories in TLA+:\n\n```\nH1 == \u003c\u003c\n        [op|-\u003e\"E\", val|-\u003e\"x\", proc|-\u003e\"A\"],\n        [op|-\u003e\"E\", val|-\u003e\"y\", proc|-\u003e\"B\"],\n        [op|-\u003e\"Ok\", proc|-\u003e\"B\"],\n        [op|-\u003e\"Ok\", proc|-\u003e\"A\"],\n        [op|-\u003e\"D\",  proc|-\u003e\"B\"],\n        [op|-\u003e\"Ok\", val|-\u003e\"x\", proc|-\u003e\"B\"],\n        [op|-\u003e\"D\",  proc|-\u003e\"A\"],\n        [op|-\u003e\"Ok\", val|-\u003e\"y\", proc|-\u003e\"A\"],\n        [op|-\u003e\"E\", val|-\u003e\"z\", proc|-\u003e\"A\"]\u003e\u003e\n\nH2 == \u003c\u003c\n        [op|-\u003e\"E\", val|-\u003e\"x\", proc|-\u003e\"A\"],\n        [op|-\u003e\"Ok\", proc|-\u003e\"A\"],\n        [op|-\u003e\"E\", val|-\u003e\"y\", proc|-\u003e\"B\"],\n        [op|-\u003e\"D\",  proc|-\u003e\"A\"],\n        [op|-\u003e\"Ok\", proc|-\u003e\"B\"],\n        [op|-\u003e\"Ok\", val|-\u003e\"y\", proc|-\u003e\"A\"]\n\u003e\u003e\n\nH3 == \u003c\u003c\n        [op|-\u003e\"E\", val|-\u003e\"x\", proc|-\u003e\"A\"],\n        [op|-\u003e\"D\", proc|-\u003e \"B\"],\n        [op|-\u003e\"Ok\", val|-\u003e\"x\", proc|-\u003e\"B\"]\u003e\u003e\n\n\nH4 == \u003c\u003c\n        [op|-\u003e\"E\", val|-\u003e\"x\", proc|-\u003e\"A\"],\n        [op|-\u003e\"E\", val|-\u003e\"y\", proc|-\u003e\"B\"],\n        [op|-\u003e\"Ok\", proc|-\u003e\"A\"],\n        [op|-\u003e\"Ok\", proc|-\u003e\"B\"],\n        [op|-\u003e\"D\", proc|-\u003e \"A\"],\n        [op|-\u003e\"D\", proc|-\u003e \"C\"],\n        [op|-\u003e\"Ok\", val|-\u003e\"y\", proc|-\u003e\"A\"],\n        [op|-\u003e\"Ok\", val|-\u003e\"y\", proc|-\u003e\"C\"]\n\u003e\u003e\n```\n\nWe can use the `IsLinearizableHistory` operator from LinQueue.tla to verify that\nH2 is not linearizable.\n\n![Evaluating IsLinearizable(H2)](h2.png)\n\nFor H3, we can use the `FindLinearization` algorithm from LinQueuePlusCal.tla to\nfind a linearization. Specify `linearizable = FALSE` as the invariant and\nrun the model checker. The variable `S` contains the linearization:\n\n![H3 linearization](h3.png)\n\nHere's the value for `S`:\n\n```\n\u003c\u003c\n    [op|-\u003e\"E\", val|-\u003e\"x\", proc|-\u003e\"A\"],\n    [op|-\u003e\"Ok\", proc|-\u003e\"A\"],\n    [op|-\u003e\"D\", proc|-\u003e \"B\"],\n    [op|-\u003e\"Ok\", val|-\u003e\"x\", proc|-\u003e\"B\"]\n\u003e\u003e\n```\n\n## Some excerpts of the model\n\nI endeavored to make the TLA+ representation as close as possible to how the\ndefinitions were written in the paper, rather than trying to optimize for\nreducing the state space of the TLC model checker.\n\n### Linearizable history\n\np469:\n\nA history H is linearizable if it can be extended (by appending zero or more\nresponse events) to some history H’ such that:\n\n* L1: complete(H’) is equivalent to some legal sequential history S, and\n* L2: \u003c\u003csub\u003eH\u003c/sub\u003e ⊆ \u003c\u003csub\u003eS\u003c/sub\u003e\n\n\nHere's how I modeled this in TLA+:\n\n```\nIsLinearizableHistory(H) == \n    \\E Hp \\in ExtendedHistories(H) : \n       LET completeHp == Complete(Hp)\n       IN \\E f \\in Orderings(Len(completeHp)) :\n            LET S == completeHp ** f            \n            IN /\\ IsSequential(S)               \\* L1\n               /\\ IsLegal(S)                    \\* L1\n               /\\ AreEquivalent(S, completeHp)  \\* L1\n               /\\ RespectsPrecedenceOrdering(H, S) \\* L2\n```\n\n### complete(H)\n\nFrom p467:\n\nIf H is a history, complete(H) is the maximal subsequence of H consisting only of invocations and matching responses.\n\nA subsequence is a sequence that can be derived from another sequence by\ndeleting some or no elements without changing the order of the remaining\nelements (source: [Wikipedia](https://en.wikipedia.org/wiki/Subsequence))\n\n```\nComplete(H) ==\n    LET subseqs == Subsequences(H)\n    IN CHOOSE CH \\in subseqs :\n        /\\ OnlyInvAndMatchingResponses(CH) \n        /\\ \\A s \\in subseqs : OnlyInvAndMatchingResponses(s) =\u003e Len(s) \u003c= Len(CH) \\* maximal\n```\n\n### Sequential\n\np467\n\nA history H is sequential if:\n1. The first event of H is an invocation.\n2. Each invocation, except possibly the last, is immediately followed by a\n   matching response. Each response is immediately followed by a matching\n   invocation.\n\n```\nIsSequential(H) ==\n    LET IsLastInvocation(h,i) == \\A j \\in 1..Len(h) : IsInvocation(h[j]) =\u003e j\u003c=i\n    IN /\\ Len(H)\u003e0 =\u003e IsInvocation(H[1])\n       /\\ \\A i \\in 1..Len(H) : IsInvocation(H[i]) =\u003e (IsLastInvocation(H,i) \\/ Matches(H, i, i+1))\n       /\\ \\A i \\in 1..Len(H) : IsResponse(H[i]) =\u003e Matches(H,i-1,i)\n```\n\n### Legal sequential history\n\nThe specificiation for a legal sequential history varies based on the kind of\nobject whose behavior you are trying to model. The paper uses a queue (FIFO) as the example object being\nmodeled.\n\nMost of the work is done by the recursive `LegalQueue` function.\n\n```\nRECURSIVE LegalQueue(_, _)\n\n\\* Check if a history h is legal given an initial queue state q\nLegalQueue(h, q) == \\/ h = \u003c\u003c \u003e\u003e\n                    \\/ LET first == Head(h)\n                           rest == Tail(h)\n                       IN \\/ /\\ first.op = \"E\" \n                             /\\ LegalQueue(rest, Append(q, first.val))\n                          \\/ /\\ first.op = \"D\"\n                             /\\ Len(q)\u003e0\n                             /\\ first.val = Head(q)\n                             /\\ LegalQueue(rest, Tail(q))\n\nIsLegalQueueHistory(h) == LegalQueue(h, \u003c\u003c \u003e\u003e)\n\n\\* Given a sequential history H, true if it represents a legal queue history\nIsLegal(H) == \n    LET RECURSIVE Helper(_, _)\n        Helper(h, s) == IF h = \u003c\u003c \u003e\u003e THEN IsLegalQueueHistory(s)\n                        ELSE LET hr == Tail(Tail(h))\n                                 inv == h[1]\n                                 res == h[2]\n                                 e == [op|-\u003einv.op, val|-\u003e IF inv.op = \"E\" THEN inv.val ELSE res.val]\n                             IN Helper(hr, Append(s, e))\n    IN Helper(H, \u003c\u003c\u003e\u003e)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Florin%2Ftla-linearizability","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Florin%2Ftla-linearizability","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Florin%2Ftla-linearizability/lists"}