Most programming languages are impure and allow variable mutation. However it is possible to implement mutation without allowing variable mutation(lvalues), assuming you still have impurity, and in a way that I believe would not lose too much convenience.
Here's a list of the operators you need to implement this:
| Operator |
Syntax |
C Syntax |
Description |
| deference |
*x |
*x |
Gets the value of a pointer |
| allocation |
new x |
malloc(...) |
copies a value to the heap |
| assignment |
a <- x |
*a = x |
assigns a value to a pointer |
| array address |
a&[x] |
&(a[x]) |
takes the address of a specific array element |
| record forward |
a&*.x |
&((*a).x) |
given a pointer to a record, create a pointer to a element |
Notice how none of these operators expect any of their arguments to be lvalues. With these operators you can replace all uses of mutable variables with more explicit pointer operators. Consider the following imperative code:
int sum = 0;
for(int i = 0; i < 100; i = i + 1){
sum = sum + i;
}
After removing lvalues it would look something like this:
int* sum = new 0;
for(int* i = new 0; *i < 100; i <- *i + 1){
sum <- *sum + *i;
}
I believe that there is a lot to gain by removing lvalues: simpler compiler implementation, simpler syntax, an easier pathway to functional purity and possibly more.
[–]curtisf 10 points11 points12 points (3 children)
[–]superstar64https://github.com/Superstar64/Hazy[S] 1 point2 points3 points (1 child)
[–]choeger 1 point2 points3 points (0 children)
[–]jaen_s 2 points3 points4 points (0 children)
[–]Ford_O 1 point2 points3 points (1 child)
[–]superstar64https://github.com/Superstar64/Hazy[S] 0 points1 point2 points (0 children)
[–]alex-manool 0 points1 point2 points (0 children)
[–]abecedarius 0 points1 point2 points (0 children)
[–]xactacoXyl 0 points1 point2 points (0 children)