Programming Paradigm project
Programming Paradigm project
Data Structures
Arrays
byte arr[5]; // Array of 5 bytes
arr[0] = 10;
Channels
chan ch = [2] of { int }; // Channel storing int values
ch!5; // Sending data to channel
ch?x; // Receiving data from channel
Array
Structure
Conditional Expressions
Break
If
Switch
Loops
Recursive Functions
Functions
Pointers
Pointers Assignment & Reference
A proctype defines a concurrent process in Promela. It encapsulates a sequence of actions that can be
executed in parallel with other processes.
Uses:
Features:
proctype exampleProcess() {
byte x = 0;
do
:: x < 5 -> x++;
:: else -> break;
od;
}
init {
run exampleProcess();
}
inline
The inline construct is used to define reusable code blocks similar to macros in C. It helps to avoid code
repetition and improve readability.
Uses:
Features:
init {
int num = 5;
square(num);
printf("Square: %d\n", num);
}
// OUTPUT:
typedef
The typedef construct is used to define complex data structures, similar to structs in C.
Uses:
Features:
typedef Node {
int value;
int next;
}
Node node_mem[2];
int head = 0;
init {
atomic {
node_mem[0].value = 10;
node_mem[0].next = 1;
node_mem[1].value = 20;
node_mem[1].next = -1;
4 Loops
C Promela
byte i = 0;
for (int i = 0; i < 5; i++) {
printf("%d\n", i); do
} :: (i < 5) ->
printf("%d\n", i);
i++;
:: else -> break; /* Stops when i >= 5 */
od;
How It Works:
● The do...od loop contains multiple choices (::).
● One of the choices is randomly selected during execution.
● The loop breaks immediately after one selection to simulate a one-time random choice.
● If we remove break;, the process can keep making random choices indefinitely.
Alternative Using if...fi
● if...fi behaves like switch-case in C but picks one of the choices non-deterministically.
byte x;
proctype randomChoice() {
if
:: x = 1; printf("Randomly chose: %d\n", x);
:: x = 2; printf("Randomly chose: %d\n", x);
:: x = 3; printf("Randomly chose: %d\n", x);
fi;
}
init {
run randomChoice();
}
//OUTPUT:
6 Channels in Promela
Channels in Promela are used for process communication. They allow message passing between
processes, either synchronously (no buffer) or asynchronously (buffered).
Declaring Channels
Syntax:
chan channel_name = [buffer_size] of { type1, type2, ... };
● buffer_size: Defines the capacity (0 for synchronous, >0 for asynchronous).
● { type1, type2, ... }: Specifies the data types sent through the channel.
Example Declarations
chan ch1 = [0] of { byte }; // Synchronous channel (no buffer)
chan ch2 = [3] of { int }; // Asynchronous channel (buffer size = 3)
chan ch3 = [2] of { int, bool }; // Channel storing (int, bool) pairs
Sending & Receiving Messages
Syntax
● Send Message: channel ! value1, value2, ...
● Receive Message: channel ? var1, var2, ...
Example
han ch = [2] of { byte };
proctype Sender() {
ch!5; // Sending value 5
}
proctype Receiver() {
byte x;
ch?x; // Receiving into x
printf("Received: %d\n", x);
}
init {
run Sender();
run Receiver();
}
● The Sender process sends 5 to ch.
● The Receiver process receives 5 and prints it.
How It Works
1. Step1 cannot proceed until Step2 picks up the message.
2. This ensures strict sequential execution (Step1 → Step2).
7 Atomic in Promela
The atomic block ensures that all enclosed statements execute without interruption by other processes.
byte x = 0; byte x = 0;
proctype Example() { proctype Example() {
x = x + 1; atomic {
printf("Without atomic: x = %d\n", x); x = x + 1;
x = x * 2; printf("With atomic: x = %d\n", x);
printf("Without atomic (after multiplication): x = %d\n", x); x = x * 2;
} printf("With atomic (after multiplication): x = %d\n", x);
init { }
run Example(); }
run Example(); init {
} run Example();
run Example();
}
Key Difference:
● Without atomic: Different interleavings may cause inconsistent values.
● With atomic: Execution is sequential, preventing interference.
Best Practice:
C to Promela
If else
int x = 10; #include <stdio.h>
Loop
int i = 0; #include <stdio.h>
Array
int arr[5]; #include <stdio.h>
proctype initialize() {
arr[0] = 10; int arr[5];
arr[1] = 20;
arr[2] = 30; void initialize() {
arr[3] = 40; arr[0] = 10;
arr[4] = 50; arr[1] = 20;
} arr[2] = 30;
arr[3] = 40;
proctype print_array() { arr[4] = 50;
int i = 0; }
do
:: (i < 5) -> printf("arr[%d] = %d\n", i, arr[i]); i = i + 1; void print_array() {
:: (i >= 5) -> break; int i;
od; for (i = 0; i < 5; i++) {
} printf("arr[%d] = %d\n", i, arr[i]);
}
init { }
run initialize();
run print_array(); int main() {
} initialize();
print_array();
return 0;
}
Switch case
Break
int i = 0;
proctype break_example() {
do
:: (i >= 5) -> printf("Breaking at i = %d\n", i); break;
:: printf("i = %d\n", i); i++;
od;
}
init {
run break_example();
}
#include <iostream>
using namespace std;
void break_example() {
for (int i = 0; i < 10; i++) {
if (i >= 5) {
cout << "Breaking at i = " << i << endl;
break;
}
cout << "i = " << i << endl;
}
}
int main() {
break_example();
return 0;
}
Continue
int i = 0;
proctype continue_example() {
do
:: (i == 2) -> i++; printf("Skipping i = 2\n");
:: (i >= 5) -> break;
:: printf("i = %d\n", i); i++;
od;
}
init {
run continue_example();
}
#include <iostream>
using namespace std;
void continue_example() {
for (int i = 0; i < 10; i++) {
if (i == 2) {
cout << "Skipping i = 2" << endl;
continue;
}
cout << "i = " << i << endl;
}
}
int main() {
continue_example();
return 0;
}
proctype func1() {
proctype func2() {
printf("Step 6: Executing function 2, statement 1\n");
printf("Step 7: Executing function 2, statement 2\n");
printf("Step 8: Executing function 2, statement 3\n");
printf("Step 9: Executing function 2, statement 4\n");
printf("Step 10: Executing function 2, statement 5\n");
}
proctype func3() {
printf("Step 11: Executing function 3, statement 1\n");
printf("Step 12: Executing function 3, statement 2\n");
printf("Step 13: Executing function 3, statement 3\n");
printf("Step 14: Executing function 3, statement 4\n");
printf("Step 15: Executing function 3, statement 5\n");
}
proctype func4() {
printf("Step 16: Executing function 4, statement 1\n");
printf("Step 17: Executing function 4, statement 2\n");
printf("Step 18: Executing function 4, statement 3\n");
printf("Step 19: Executing function 4, statement 4\n");
printf("Step 20: Executing function 4, statement 5\n");
}
proctype func5() {
printf("Step 21: Executing function 5, statement 1\n");
printf("Step 22: Executing function 5, statement 2\n");
printf("Step 23: Executing function 5, statement 3\n");
printf("Step 24: Executing function 5, statement 4\n");
printf("Step 25: Executing function 5, statement 5\n");
}
init {
run func1();
run func2();
run func3();
run func4();
run func5();
}
#include <stdio.h>
void func1() {
printf("Step 1: Executing function 1, statement 1\n");
printf("Step 2: Executing function 1, statement 2\n");
printf("Step 3: Executing function 1, statement 3\n");
printf("Step 4: Executing function 1, statement 4\n");
printf("Step 5: Executing function 1, statement 5\n");
}
void func2() {
printf("Step 6: Executing function 2, statement 1\n");
printf("Step 7: Executing function 2, statement 2\n");
printf("Step 8: Executing function 2, statement 3\n");
printf("Step 9: Executing function 2, statement 4\n");
printf("Step 10: Executing function 2, statement 5\n");
}
void func3() {
printf("Step 11: Executing function 3, statement 1\n");
printf("Step 12: Executing function 3, statement 2\n");
printf("Step 13: Executing function 3, statement 3\n");
printf("Step 14: Executing function 3, statement 4\n");
printf("Step 15: Executing function 3, statement 5\n");
}
void func4() {
printf("Step 16: Executing function 4, statement 1\n");
printf("Step 17: Executing function 4, statement 2\n");
printf("Step 18: Executing function 4, statement 3\n");
printf("Step 19: Executing function 4, statement 4\n");
printf("Step 20: Executing function 4, statement 5\n");
}
void func5() {
printf("Step 21: Executing function 5, statement 1\n");
printf("Step 22: Executing function 5, statement 2\n");
printf("Step 23: Executing function 5, statement 3\n");
printf("Step 24: Executing function 5, statement 4\n");
printf("Step 25: Executing function 5, statement 5\n");
}
int main() {
func1();
func2();
func3();
func4();
func5();
return 0;
}
References:
1. https://www.diva-portal.org/smash/get/diva2:235718/FULLTEXT01.pdf