-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A shorthand for immutability #221
Comments
In addition to |
So this proposal I think is equivalent to C++'s auto (#129):
is equivalent to:
And so all my objections from #129 apply here also. Mainly that the code is actually less readable because it is not clear what type |
Some languages use
|
The syntax that you propose is ambiguous with assignment-stmt in fixed form. |
I suppose that's true, but as fixed-form is obsolescent (IMO) it would be reasonable for that feature not to be available in fixed form. Of course there are plenty of other opinions on that front. |
@everythingfunctional writes Aug. 25, 2021, 11:22 AM EDT:
To make "end associate be made optional" means, in my mind, to introduce an My observation remains Fortran has a long history of statement-construct pairs, from I firmly believe I personally will be firmly opposed to introducing something like |
@FortranFan if you wouldn't mind, to keep our conversations focused, in the future can you please post more general comments into issues like #59 (and/or create new ones), and then just provide a quick link here. That way we can separate general discussion about "vision" from a technical discussion about a Update: Thank you @FortranFan for opening up the other issues and linking them here! |
It would also be reasonable to delete an obsolescent feature that has had a better alternative (free form) for nearly the entire lifetime of some of the younger people in this dialogue. If a generation of Fortran programmers has gone from kindergarten to service on the language standard body in the time since a better alternative entered the language, it has to be time to jettison the obsolescent feature that conflicts with a compelling new proposal. Compiler developers who must support the obsolescent feature can require a flag to enable the support for the obsolescent feature and let users know that the flag precludes the use of the newer conflicting feature. |
I like this idea and agree that it nicely mirrors other features.
I agree with everything you wrote except the last sentence. I like your suggestion of the |
I'm tempted to agree with this, and that it probably would be the more "Fortranique" way, but I don't like that it is effectively two separate features masquerading as one: create an alias, and create an immutable variable. I'd prefer if we're adding a new feature to make it clear that they are two separate things by having two different keywords. I could live with an |
Please see #225. If the obsolescent category were defined with some "teeth" in the standard, or an outmoded feature at issue (like |
You should perhaps consider the option of limiting the scope of the immutable captured expression value to be something other than just "from here to the end of the BLOCK or executable-part". Otherwise, you'll have to come up with semantics for things like
and
If you make the name a construct entity (19.1, 19.4) owned by the enclosing construct, if any, and finesse the scoping a little to isolate THEN parts from ELSE parts (and various cases from each other in SELECT, &c., you get the idea), you would dodge these concerns, which I think you have to worry about with your original statement of the scope having essentially an implied |
In order to achieve effective |
Exactly the kind of insight I look forward to seeing from this community. I like the idea.
I don't think these are convenient. a) does not work for values that are not constant expressions It's not that I don't think an |
To use an old expression we don’t hear often anymore, Brad just took the
words right out of my mouth. For the past ~24 hours, I’ve been thinking of
expressing precisely the same sentiment. I probably would have done so
already if my computer hadn’t died yesterday.
I want to be clear that what we are discussing is a much bigger point than
many might realize. Working with immutable state is central to the
functional programming paradigm. As Fortran has evolved to support multiple
programming paradigms, some have gotten a lot more attention and explicit
support than others. Object-oriented, parallel, and array programming have
extensive feature sets in Fortran. Hopefully generic programming will too
in Fortran 202Y.
Functional programming support is limited and little-used. Think about
how rare it is to see pure procedures. And before this exchange, how many
people even knew that associating with an expression defines immutable
state? Those who work closely with me know that this practice has now
become ubiquitous in my Fortran programming. I rarely declare local
variables anymore and only when I have a specific need such as a loop
index, but I also eschew loops in favor of array statements and elemental
procedures whenever possible. I rather like the idea of calling out
immutability in a new and more prominent way than hiding it as a special
case of a feature that most people probably don’t use often.
Damian
…On Wed, Aug 25, 2021 at 16:37 Brad Richardson ***@***.***> wrote:
To make "end associate be made optional" means, in my mind, to introduce
an ASSOCIATE statement form to complement the ASSOCIATE block construct.
My observation remains Fortran has a long history of statement-construct
pairs, from IF to WHERE.
I'm tempted to agree with this, and that it probably would be the more
"Fortranique" way, but I don't like that it is effectively two separate
features masquerading as one: create an alias, and create an immutable
variable. I'd prefer if we're adding a new feature to make it clear that
they are two separate things by having two different keywords. I could live
with an ASSOCIATE statement, but it's not my first choice.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#221 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADEANBG3G7I4P4LSF7KXCWLT6V5DHANCNFSM5CZKUNRA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email>
.
|
Thank you everybody for the discussion, this is very helpful! I think this should be prototyped in a compiler, and let's simply use it and see how it looks and feels. |
@rouson writes Aug. 27, 2021 8:21 PM EDT:
@rouson , perhaps you can open up a new issue thread toward better support toward the functional programming paradigm in Fortran? And perhaps also drive a comprehensive review of the needs and benefits with this paradigm in a set of domains (are they still in scientific and technical computing) with use cases and examples, even if in abstract algorithmic terms and/or with other languages? And what Fortran offers currently (there is of course It will be really good if attempts are not made instead to |
Good point @FortranFan. I agree that we should have a broader vision for Fortran with regards to functional programming, if we want to go that way, and then ensure that |
@rouson writes Aug. 27, 2021 8:01 PM EDT:
From what I have seen, the notion of As to not using local variables and its connection with functional programming, it surely is aspirational when it comes to good coding practices. But it's up to the library authors to aspire to it. The question then becomes what tools they require to achieve it in Fortran. However as I write above, that is a separate and broader discussion and separate from considerations of immutability. A simple example I usually give is the Euclidean algorithm to find the greatest common divisor of two positive integers: apart from any outstanding bugs (it only had limited testing) and any aspects of a generic algorithm (any kind integer e.g., 64-bit) and style considerations, the following is a perfectly reasonable function in Fortran:
The above with the simple driver program use euclid_m
integer :: a, b
a = 46332 ; b = 71162
print *, "a = ", a, "; b = ", b
print *, "gcd(a,b) = ", gcd(a, b), "; expected result is 26"
end with any current standard-conforming processor should yield:
Now, keeping in mind everything around Fortran with its strong legacy of Fortran of nearly 75 years and all the great effort in its standard development with facilities toward better functional programming and the accompanying constraints imply:
Then consider a "textbook" case of an equivalent code in Rust that touts its drive for functional programming and which too may be the basis for the fn gcd(mut n: u64, mut m: u64) -> u64 {
assert!(n !=0 && m != 0);
while m != 0 {
if m < n {
let t = m;
m = n;
n = t;
}
m = m % n;
}
n
} Going strictly by the number of typed characters above, sure the code in Fortran is more verbose than in Rust. However the verbosity in Fortran is inherent to its design and it's also beneficial; regardless, it's divorced from any immutability aspects here. Looking at the Rust code though, there are instead far bigger issues of significant concern:
Going by this, one can even argue there is hardly much of anything to be "imported" into Fortran from Rust such as This is why I suggested above to take a broader view of the functional paradigm with fully worked out use cases, even if they are in abstract algorithmic terms or minimal working examples from other languages. To take some isolated code snippets and start building solutions around them is detrimental, in my opinion. It's something which has been quite problematic with several feature introductions in recent Fortran standard revisions also and which has led to suboptimal facilities and the approach is better avoided from now on. |
@FortranFan that is where I am coming from also, thanks for writing it up. This is a really good discussion. I think
I think you are onto something here, but I didn't understand your argument. Can you elaborate on this? My understanding is that Rust does not have exceptions: https://doc.rust-lang.org/book/ch09-00-error-handling.html, you have to return the error as a result, and Rust has some syntax that helps making this smooth. The Zig language follows a very similar approach I think. |
@FortranFan thanks for the example. I agree that local variables can be indispensable. The original issue description by @everythingfunctional did not mention functional programming so I took us off-course by bringing it up. Apologies. I think the original use cases suffice for what was being proposed. If we can communicate more intention to the reader and to the compiler and thereby clarify code and prevent mistakes (.e.g., mistakenly modifying data), then I think those are good things in their own right, independent of the programming paradigm that inspired them. Just in case it's not clear from earlier comments, |
Besides making
It would resemble the syntax |
As an alternative to a bunch of |
If |
If verbosity from repeated statements is the issue why not just:
or (with the alignment/spacing of personal preference):
instead? Assuming there is not some syntax ambiguities to associate I overlook here or there not being anything else I have misunderstood. I do not like the asymmetry between the 3 opening statements and the single close, and I really would not consider having many not too fond of short semi-cryptic (possibly inline) declarations like |
Oh right, I see now that the second associate depends on the value of the first so that combining them would not work too well, sorry. I still stand by what I said about asymmetry though. |
This would be an extremely important addition to the language, not only for functional programming, but also for enabling safer implementation of certain object-oriented design patterns, that are required for writing object-oriented code that is both efficient and loosely coupled. Thumbs up for this one! |
@everythingfunctional was a paper on this submitted to J3? It would be great to get some form of your proposal onto the Fortran 202Y work list at the June meeting. |
@rouson , not that I know of. I could be convinced to write a paper, but it's not particularly high on my list of priorities. |
There is already a method of specifying immutable local values (i.e. calculate once, reference multiple times), but often results in some inconvenience/verbosity. For example
I have seen it requested in a few places that the
end associate
be made optional, but I think that deserves a different keyword. I would proposelet
. Thuslet val1 = some_expr
would be exactly equivalent to
associate(val1 => (some_expr)) ! following code end associate end ! of enclosing scope
which would simplify the example above to
This has the benefit of making it clear that
associate
is not being used to alias, and thus any following assignment to the value is prohibited (e.g. the variable is immutable). It also enables automatic types for local variables, albeit with the side-effect (IMO benefit) that they can only be assigned to once.The text was updated successfully, but these errors were encountered: