Back to skills
SKILL.md
2524 Language Patterns B94de45b
ASecurity**C Code:** ```c int *ptr; int array[10]; ptr = &array[5]; *ptr = 42; ```
- 9 stars
- 0 votes
- 0 copies
- 0 views
- Added October 11, 2026
Works with
Security analysis
100/100npx -y skills add tools-only/X-Skills --skill 2524-language_patterns_b94de45b --agent claude-codeAre you the author of 2524 Language Patterns B94de45b?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/tools-only-2524-language-patterns-b94de45b)# Language-Specific Patterns for TLA+ Generation
## C/C++ to TLA+
### Pointer Handling
**C Code:**
```c
int *ptr;
int array[10];
ptr = &array[5];
*ptr = 42;
```
**TLA+ Model:**
```tla
VARIABLES memory, ptr
\* Model memory as function from addresses to values
Init ==
/\ memory = [i \in 1..10 |-> 0]
/\ ptr = 5
Write ==
/\ memory' = [memory EXCEPT ![ptr] = 42]
/\ UNCHANGED ptr
```
### Mutex and Locks
**C Code:**
```c
pthread_mutex_t lock;
pthread_mutex_lock(&lock);
// critical section
pthread_mutex_unlock(&lock);
```
**TLA+ Model:**
```tla
VARIABLES lock, inCriticalSection
AcquireLock ==
/\ lock = FALSE
/\ lock' = TRUE
/\ inCriticalSection' = TRUE
ReleaseLock ==
/\ lock = TRUE
/\ lock' = FALSE
/\ inCriticalSection' = FALSE
```
### Condition Variables
**C Code:**
```c
pthread_cond_wait(&cond, &mutex);
pthread_cond_signal(&cond);
```
**TLA+ Model:**
```tla
VARIABLES waiting, signaled
Wait ==
/\ ~signaled
/\ waiting' = TRUE
/\ UNCHANGED signaled
Signal ==
/\ waiting
/\ signaled' = TRUE
/\ UNCHANGED waiting
Wakeup ==
/\ signaled
/\ waiting' = FALSE
/\ signaled' = FALSE
```
## Java to TLA+
### Synchronized Methods
**Java Code:**
```java
class Counter {
private int count = 0;
synchronized void increment() {
count++;
}
synchronized int getCount() {
return count;
}
}
```
**TLA+ Model:**
```tla
VARIABLES count, lock
Increment ==
/\ lock = FALSE
/\ lock' = TRUE
/\ count' = count + 1
/\ lock' = FALSE \* Release after operation
GetCount ==
/\ lock = FALSE
/\ lock' = TRUE
/\ \* Read count (no state change)
/\ UNCHANGED count
/\ lock' = FALSE
```
### Thread Execution
**Java Code:**
```java
class Worker extends Thread {
public void run() {
while (!done) {
process();
}
}
}
```
**TLA+ Model:**
```tla
VARIABLES pc, done
Process ==
/\ pc = "running"
/\ ~done
/\ \* Process logic
/\ UNCHANGED <<pc, done>>
Terminate ==
/\ done
/\ pc' = "terminated"
/\ UNCHANGED done
```
### Wait/Notify
**Java Code:**
```java
synchronized void produce() {
while (buffer.isFull()) {
wait();
}
buffer.add(item);
notifyAll();
}
```
**TLA+ Model:**
```tla
VARIABLES buffer, waiting
Produce ==
/\ Len(buffer) < MaxSize
/\ buffer' = Append(buffer, item)
/\ waiting' = {} \* Notify all
ProduceWait ==
/\ Len(buffer) = MaxSize
/\ waiting' = waiting \cup {producer}
/\ UNCHANGED buffer
```
## Go to TLA+
### Goroutines
**Go Code:**
```go
func worker(id int, jobs <-chan int, results chan<- int) {
for j := range jobs {
results <- process(j)
}
}
go worker(1, jobs, results)
```
**TLA+ Model:**
```tla
VARIABLES jobs, results, workerState
Worker(id) ==
/\ workerState[id] = "ready"
/\ jobs # <<>>
/\ LET job == Head(jobs)
IN /\ jobs' = Tail(jobs)
/\ results' = Append(results, Process(job))
/\ UNCHANGED workerState
```
### Channels
**Go Code:**
```go
ch := make(chan int, 10)
ch <- 42 // Send
x := <-ch // Receive
```
**TLA+ Model:**
```tla
VARIABLES channel
Send(value) ==
/\ Len(channel) < 10
/\ channel' = Append(channel, value)
Receive ==
/\ channel # <<>>
/\ LET value == Head(channel)
IN /\ channel' = Tail(channel)
/\ \* Use value
```
### Select Statement
**Go Code:**
```go
select {
case msg := <-ch1:
handle1(msg)
case msg := <-ch2:
handle2(msg)
default:
handleDefault()
}
```
**TLA+ Model:**
```tla
SelectReceive ==
\/ /\ ch1 # <<>>
/\ LET msg == Head(ch1)
IN /\ ch1' = Tail(ch1)
/\ Handle1(msg)
/\ UNCHANGED ch2
\/ /\ ch2 # <<>>
/\ LET msg == Head(ch2)
IN /\ ch2' = Tail(ch2)
/\ Handle2(msg)
/\ UNCHANGED ch1
\/ /\ ch1 = <<>>
/\ ch2 = <<>>
/\ HandleDefault()
/\ UNCHANGED <<ch1, ch2>>
```
## Python to TLA+
### Threading
**Python Code:**
```python
import threading
lock = threading.Lock()
counter = 0
def increment():
global counter
with lock:
counter += 1
```
**TLA+ Model:**
```tla
VARIABLES lock, counter
Increment ==
/\ lock = FALSE
/\ lock' = TRUE
/\ counter' = counter + 1
/\ lock' = FALSE
```
### Asyncio
**Python Code:**
```python
async def fetch_data(url):
response = await http_get(url)
return process(response)
await asyncio.gather(
fetch_data(url1),
fetch_data(url2)
)
```
**TLA+ Model:**
```tla
VARIABLES pending, completed
StartFetch(url) ==
/\ pending' = pending \cup {url}
/\ UNCHANGED completed
CompleteFetch(url) ==
/\ url \in pending
/\ pending' = pending \ {url}
/\ completed' = completed \cup {url}
```
### Queue
**Python Code:**
```python
from queue import Queue
q = Queue(maxsize=10)
q.put(item)
item = q.get()
```
**TLA+ Model:**
```tla
VARIABLES queue
Put(item) ==
/\ Len(queue) < 10
/\ queue' = Append(queue, item)
Get ==
/\ queue # <<>>
/\ LET item == Head(queue)
IN queue' = Tail(queue)
```
## Rust to TLA+
### Ownership and Borrowing
**Rust Code:**
```rust
fn process(data: Vec<i32>) -> Vec<i32> {
// data is moved here
data.iter().map(|x| x * 2).collect()
}
let v = vec![1, 2, 3];
let result = process(v);
// v is no longer accessible
```
**TLA+ Model:**
```tla
VARIABLES owner, data
Transfer ==
/\ owner = "caller"
/\ owner' = "callee"
/\ data' = [i \in DOMAIN data |-> data[i] * 2]
\* Model ownership as exclusive access
TypeInvariant ==
owner \in {"caller", "callee"}
```
### Mutex and Arc
**Rust Code:**
```rust
use std::sync::{Arc, Mutex};
let counter = Arc::new(Mutex::new(0));
let mut num = counter.lock().unwrap();
*num += 1;
```
**TLA+ Model:**
```tla
VARIABLES counter, lock, refCount
Increment ==
/\ lock = FALSE
/\ lock' = TRUE
/\ counter' = counter + 1
/\ lock' = FALSE
/\ UNCHANGED refCount
```
### Channels (mpsc)
**Rust Code:**
```rust
use std::sync::mpsc;
let (tx, rx) = mpsc::channel();
tx.send(42).unwrap();
let value = rx.recv().unwrap();
```
**TLA+ Model:**
```tla
VARIABLES channel
Send(value) ==
/\ channel' = Append(channel, value)
Receive ==
/\ channel # <<>>
/\ LET value == Head(channel)
IN channel' = Tail(channel)
```
## Common Abstraction Patterns
### Loop Abstraction
**Program:**
```
for (int i = 0; i < n; i++) {
process(i);
}
```
**TLA+ (Atomic):**
```tla
ProcessAll ==
/\ state = "ready"
/\ state' = "done"
/\ result' = [i \in 1..n |-> Process(i)]
```
**TLA+ (Iterative):**
```tla
VARIABLES i, state
ProcessNext ==
/\ i < n
/\ Process(i)
/\ i' = i + 1
/\ UNCHANGED state
Complete ==
/\ i = n
/\ state' = "done"
/\ UNCHANGED i
```
### Error Handling
**Program:**
```
try {
operation();
} catch (Exception e) {
handleError(e);
}
```
**TLA+ Model:**
```tla
Operation ==
\/ /\ CanSucceed
/\ DoOperation
/\ error' = NULL
\/ /\ ~CanSucceed
/\ HandleError
/\ error' = "OperationFailed"
```
### Callback/Event Handler
**Program:**
```
button.onClick(() => {
updateState();
});
```
**TLA+ Model:**
```tla
OnClick ==
/\ buttonPressed
/\ UpdateState
/\ buttonPressed' = FALSE
```
## Type Mapping Reference
| Program Type | TLA+ Type | Example |
|--------------|-----------|---------|
| `bool` | `BOOLEAN` | `flag \in BOOLEAN` |
| `int` | `Int` or `Nat` | `count \in Nat` |
| `int[n]` | `Seq(Int)` | `array \in Seq(Int)` |
| `List<T>` | `Seq(T)` | `list \in Seq(Data)` |
| `Set<T>` | `SUBSET T` | `set \in SUBSET Data` |
| `Map<K,V>` | `[K -> V]` | `map \in [Keys -> Values]` |
| `struct/class` | Record | `[field1: T1, field2: T2]` |
| `enum` | Set | `state \in {"A", "B", "C"}` |
| `Optional<T>` | `T \cup {NULL}` | `value \in Data \cup {NULL}` |
| `pointer` | Index/Ref | `ptr \in 1..Size` |
Attribution
Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.