replica nfl jerseysreplica nfl jerseyssoccer jerseyreplica nfl jerseys forums.silverfrost.com :: View topic - Fortran 2003 and 2008 features
forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Fortran 2003 and 2008 features
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8, 9, 10  Next
 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Suggestions
View previous topic :: View next topic  
Author Message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2615
Location: Sydney

PostPosted: Wed Aug 24, 2022 4:36 pm    Post subject: Re: Reply with quote

PaulLaidler wrote:
Dan

Optional and named/keyword arguments were introduced at the "beginning" i.e. in the Fortran 90 standard.
and they do require an interface definition, either explicitly or via contains, as Ken has outlined.

A lot happened to F95 after F90 !

it is not "call sub ( a==b )" which would be a different spin,
Back to top
View user's profile Send private message
DanRRight



Joined: 10 Mar 2008
Posts: 2924
Location: South Pole, Antarctica

PostPosted: Wed Aug 24, 2022 6:24 pm    Post subject: Reply with quote

Paul,
Seems no one since 1990th used such a=b option and those who used did not ask the question why interface was needed when it is clear that sometimes it is not needed. Was interface required because Fortran compilers back then did not check subroutine arguments for consistency?

Great that Clearwin functions do not require interfaces, that would be a disaster

Eddie,
And there also exist linked lists also introduced hell lot ago which will crush not only the human brains but also the brains and cache coherency of pipelines of all predictive branches of processors https://en.wikipedia.org/wiki/Linked_list

John,
The question is why they need interface definition? I gave definition for a and b enough for Fortran to run the code unambiguously.
Back to top
View user's profile Send private message
Kenneth_Smith



Joined: 18 May 2012
Posts: 815
Location: Hamilton, Lanarkshire, Scotland.

PostPosted: Wed Aug 24, 2022 9:10 pm    Post subject: Reply with quote

Dan,
Code:
b=1
a=b
call sub1(a)

Is not the same as
Code:
b=1
call sub1(a=b)

as in the second case (a=b) is not an assignment in the "usual" way. It is showing that argument A is present with value B.

Example:
Code:
Program test
implicit none
real b              ! No need to define a as real
b=1
call sub1 (a=b)
call sub1

contains

  subroutine sub1(a)
  real, optional :: a
  if (present(a))        print*,a
  if ( .not. present(a)) print*, 'no a input'
  end subroutine

end program


But the program runs OK, if you omit the optional declaration for A in SUB1 and don't use the PRESENT inquiry function.
Back to top
View user's profile Send private message
DanRRight



Joined: 10 Mar 2008
Posts: 2924
Location: South Pole, Antarctica

PostPosted: Wed Aug 24, 2022 9:32 pm    Post subject: Reply with quote

Ken,
As valid by common sense code my example should work. I think it does not work just because possibly not all options were taken into account by FTN95 - no one in 20 years used this super rare twist besides geeks of 80th level, those who were paid by the number of lines of source code adding millions interfaces or those who wanted to make their codes unreadable to 99% fortraneers Smile. I am not insisting to fix this as it looks like there is no any benefits for the codes with this (currently) cryptic syntax.

Or may be writing variables like this
Code:
call sub1(a=b)
is a good idea and will save us one line of code if it will not require any interfaces/modules/anything and will work like in my demo example which currently does not work.

But something related to better checking of arguments of subroutines still needs a fix as this code prints the result 6.6e-37 while has to write "no a input"

Code:
Program test

call sub1

end program
!----------------------
  subroutine sub1(a)
  real, optional :: a
  if (present(a))        print*,a
  if ( .not. present(a)) print*, 'no a input'
  end subroutine


Definitely by not working and requiring interface (or as you have shown by placing into the module or into CONTAINS) my demo example will confuse the programmer who will not understand why with interface or in module it worked and without did not.

If there will be more detailed error message explaining that the Standard by unknown reasons always requires interface or be in the module for such cases even if all variables are available and declared - hell knows why - then this might help not to lose time trying to find the ends.

Last 6 months i was literally losing time trying to get through similar twists of "modern" Fortran and so far did not find any rationale for using most of them. More and more i find they are more bad than good for the code readabilty, maintenance and speed.
Back to top
View user's profile Send private message
Kenneth_Smith



Joined: 18 May 2012
Posts: 815
Location: Hamilton, Lanarkshire, Scotland.

PostPosted: Thu Aug 25, 2022 9:00 am    Post subject: Reply with quote

Dan, That's an interesting twist to my last example. Another compiler does issue an ERROR at the call to sub1, "Explicit interface required for 'sub1': optional argument.

FTN95 with Checkmate does generate a run-time message "Attempt to call a routine with zero arguments when one is required". Which I agree does not help to pinpoint the need for an explicit interface.

One for the folks at Silverfrost to consider at some point.
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 8213
Location: Salford, UK

PostPosted: Thu Aug 25, 2022 10:32 am    Post subject: Reply with quote

gFortran does do better in this respect in that it generates a compile time error.

However, sub1 and the sub-program that calls it could be in separate files. In which case the caller knows nothing about the arguments at compile time and the compiler is unable to do any argument checking.

FTN95 deals with this via the runtime checking of CHECKMATE which may be unique.

As a general rule, sub-programs are separately compiled. If they reside in the same file then the compiler may do some cross referencing and indeed FTN95 does do this in most contexts but apparently not in this case.

For separate compilation and in the absence of an interface, the best that a compiler can do is to check that different calls to a sub-program are consistent in the number and in the type of the arguments.
Back to top
View user's profile Send private message AIM Address
DanRRight



Joined: 10 Mar 2008
Posts: 2924
Location: South Pole, Antarctica

PostPosted: Fri Aug 26, 2022 2:30 am    Post subject: Reply with quote

Paul,
The gfortran gave diagnostics
Code:
    4 | call sub1

      |         1

Error: Explicit interface required for �sub1� at (1): optional argument


exactly opposite to what i claim. It just formally follows the Standard which for safety always (as if there were two separate files while there were actually single file with two programs enough to think a bit further) requires interface but my point was that in some cases interface is not needed and smart compiler can deal with this rising its artificial intelligence to the next level and resolving the problem by itself. Smart compiler will be more verbose and will issues just the warning where tell the user all these suggestions Ken have shown above (place arguments in the MODULE or CONTAIN) for the user to be aware and decide what's to do. For my demo example where all a and b were defined it also should just issue a warning and go without a bug.

The FTN95 was as always smarter but just had a bug which needs fixing. Compared to FTN95 the gFortran just got more 2003/2008 features and has little bit better C integration and Intel Fortran compatibility (plus it supports OpenMP and MPI and could be sometimes faster) but as a bug crusher it is a disaster. It can sometimes swallow without hiccups such horrible mistakes in the source code that you can not believe. I will find such examples when time permits
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 8213
Location: Salford, UK

PostPosted: Fri Aug 26, 2022 6:47 am    Post subject: Reply with quote

Dan

I don't understand what you are saying.

1) Interfaces were introduced in Fortran 90.
2) Fortran compilers support Fortran 77.
3) For some features of Fortran 90, compilation requires an interface and both logic and the Standard naturally insist on this.

The sample program illustrates a case where FTN95 could give useful feedback but the bug is in the code not in the compiler. FTN95 is usually what you call "smart" in situations like this but (at the moment) not in this particular case.
Back to top
View user's profile Send private message AIM Address
LitusSaxonicum



Joined: 23 Aug 2005
Posts: 2402
Location: Yateley, Hants, UK

PostPosted: Fri Aug 26, 2022 10:07 am    Post subject: Reply with quote

Paul,

So that's "smartish".

Smart enough for me, though.

Eddie
Back to top
View user's profile Send private message
DanRRight



Joined: 10 Mar 2008
Posts: 2924
Location: South Pole, Antarctica

PostPosted: Fri Aug 26, 2022 11:01 pm    Post subject: Reply with quote

Paul,
I'm trying to say that code does not complain if i define a and b in the module, it does not complain when i define a and b in CONTAINS like Ken revealed, but when i define a and b directly just one line before calling sub1(a=b) the compiler abruptly remembers the Standard and tells me that i forgot damn interface.
Sounds kind of unusual, right?
And the resolution i proposed is that despite asking for the interface is formally correct according to Standard but by the common sense compiler has to violate Standard and do not ask for interface just warning that in other circumstances in similar situations here could be an error (when two programs are in separate files). In my case with sub1(a=b) exactly there is no error in any circumstances as a and b are fully defined.

That how smart compiler should do. And FTN95 in many cases doing like that checking arguments of subroutines on match and mismatch. Can it do that at compile time instead of run time with /checkmate? Ideally it should.
Computers are not yet perfect but the humans are even worse. Analyzing not only my mistakes in my own codes but even driving mistakes recently made on 3000 miles trip i am sure Elon Masks self-driving software would not make any of my mistakes when i barely escaped collision or wrong turns Smile. It would do its own mistakes if i'll fall asleep but this is different story
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 8213
Location: Salford, UK

PostPosted: Sat Aug 27, 2022 8:06 am    Post subject: Reply with quote

Dan

I think that I now understand what you are asking for.

But perhaps you are not understanding the idea of an interface.

An interface can be explicit via an INTERFACE statement. It can also be implicit when provided automatically via USE and a MODULE or by a CONTAINS in a program or sub-program.

The idea that your code should provide a warning and not an error at compile time is interesting. It would be an extension to the Standard and might have its downside.
Back to top
View user's profile Send private message AIM Address
JohnCampbell



Joined: 16 Feb 2006
Posts: 2615
Location: Sydney

PostPosted: Sun Aug 28, 2022 2:08 pm    Post subject: Reply with quote

My understanding of this latest thread is that �a� is an optional argument for the examples to work.
So in the calling routine, �a� doesn�t need to be declared, only �b�.
Should �a� be declared optional for this to work properly?
Back to top
View user's profile Send private message
mecej4



Joined: 31 Oct 2006
Posts: 1899

PostPosted: Mon Aug 29, 2022 2:23 am    Post subject: Reply with quote

It seems to me that Dan is confused by two features of subroutine argument lists that were added in Fortran 90 (and were not present in Fortran 77). One feature is that some dummy arguments may be given the attribute OPTIONAL. The other feature is that argument association may be controlled by position in the argument list, as in Fortran 77, OR by keyword. I think that Dan is unaware of association by keyword.

Dan is free to avoid these "new" features of Fortran 90 when he writes Fortran code. When, however, he has to collaborate with others who use these new features, he will have to understand the properties of these new features.

Here is a small program to shed some light on these features.

Code:
Program test

   Real a, b
   a = 1.23
   b = 2.34
   Call Sub1(a)        ! optional argument not passed
   call Sub1(a, b)     ! pass both arguments
   Call Sub1(b=a, a=b) ! pass both arguments, using keywords
   Call Sub1(a=a, b=b)

contains

   subroutine sub1(A, B)
      Real, Optional, Intent(in) :: B
      Real, Intent(in) :: A
      Real :: C

      C = -1.7              ! default value
      if (present(B)) C = B
      Print '(2F8.4)', A, C

   End Subroutine

End Program
Back to top
View user's profile Send private message
Kenneth_Smith



Joined: 18 May 2012
Posts: 815
Location: Hamilton, Lanarkshire, Scotland.

PostPosted: Mon Aug 29, 2022 1:45 pm    Post subject: Reply with quote

I found this via google which explains the logic, particularly when mixing optional and non-optional arguments.

Quote:
Dummy arguments can be made optional if they are declared with the OPTIONAL attribute. In this case, an actual argument does not have to be supplied for it in a procedure reference.

If argument keywords are not used, argument association is positional. The first dummy argument becomes associated with the first actual argument, and so on. If argument keywords are used, arguments are associated by the keyword name, so actual arguments can be in a different order than dummy arguments. A keyword is required for an argument only if a preceding optional argument is omitted or if the argument sequence is changed.

Positional arguments (if any) must appear first in an actual argument list, followed by keyword arguments (if any). If an optional argument is the last positional argument, it can simply be omitted if desired.

However, if the optional argument is to be omitted but it is not the last positional argument, keyword arguments must be used for any subsequent arguments in the list.

Optional arguments must have explicit procedure interfaces so that appropriate argument associations can be made.

The PRESENT intrinsic function can be used to determine if an actual argument is associated with an optional dummy argument in a particular reference.
Back to top
View user's profile Send private message
DanRRight



Joined: 10 Mar 2008
Posts: 2924
Location: South Pole, Antarctica

PostPosted: Mon Sep 05, 2022 9:06 am    Post subject: Reply with quote

We are discussing new or rare features to find why people use them and are they worth using. If someone uses them my immediate question is why? Even if this is just the personal programming style then there still could be reason for that. I observed couple programs recently which heavily use all new features. When the author is present and supports these programs are looks kind of ok but the fun is - rarely who to no one else uses them even though they are open source.

When i asked authors why they use this or that new features they can not explain that. Meantime i see if they would not use them the code would be much more clean and easy. I also see other much more dangerous consequences for that when adding some key things to the code which was easy in previous "not so modern" versions of the same code become difficult to add (or they are even absent in the new and "modern" versions and the authors admit "adding that would be difficult...". Of course difficult, when you added 100 Linked Lists, derived types and pointers pointing on pointers Smile.

Another observation - it takes weeks and months even for authors to do elementary things with such "modern" codes.

As to this last example - it was almost nothing to argue about here besides that as soon as you use keywords the subroutine needs interface or be in the module or CONTASINS because of "All shut up, I am the Fortran Standard and I say so" while my example used just one single argument and it does not matter what keyword you will use there all arguments and variables when calling SUB1 (a) or SUB1(a=b) is A keyword or an actual argument all were 100% defined there and asking for interface (which could be reasonable in other complex cases of many optional arguments could be reasonable or obligatory) in this simplest example was not so needed. I pointed out that the future compilers with AI will just issue a warning.

By the way, I have not heard from anyone that they use keyword arguments and claim it worth programming like that. But this thread is exactly to push everyone to use them and find how useful they are.


Last edited by DanRRight on Mon Sep 05, 2022 9:43 am; edited 2 times in total
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Suggestions All times are GMT + 1 Hour
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8, 9, 10  Next
Page 7 of 10

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group