For Programmers: Free Programming Magazines  


Home > Archive > Fortran > August 2005 > Which function should be called?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Which function should be called?
Thomas Koenig

2005-08-12, 5:03 pm


What should the following code print? I think it should be 1
(gfortran agrees; this is from the gfortran testsuite), but
ifort 8.1 disagrees and prints 2.


module M1

INTEGER p

CONTAINS
subroutine AA ()
implicit NONE
p = BB ()
CONTAINS
subroutine AA_1 ()
implicit NONE
integer :: i
i = BB ()
end subroutine

function BB()
integer :: BB
BB = 1
end function
end subroutine

function BB()
implicit NONE
integer :: BB
BB = 2
end function
end module

program P1
USE M1
implicit none
p = 0
call AA ()
print *,p
end
Richard E Maine

2005-08-12, 10:32 pm

In article <ddj4b5$knr$1@meiner.onlinehome.de>,
Thomas Koenig <Thomas.Koenig@online.de> wrote:

> What should the following code print? I think it should be 1
> (gfortran agrees; this is from the gfortran testsuite), but
> ifort 8.1 disagrees and prints 2.

[code elided]

If this ever comes up in any real code, what should happen is that
the programmer should be taken out and shot. :-) But I understand
that you are intentionally stressing the compiler to test conformance
on even horrible code.

It should print 1 as you think. I'll quote from f2003 because it
is most handy; plus I'm sure that it incorporates any f95 corrigenda,
so I don't have to go find if there was one in this tricky area.
I'm also sure that the f95 answer is the same because otherwise there
would have been an incompatibility, and there isn't one mentioned in
this area.

At first, I looked in the host association stuff (16.4.1.3), expecting
to find it there, but it isn't. I guess it doesn't quite fit in the
long list there because the internal subroutine statement is not in
the scoping unit of its host (it is in its own). Eventually, I thought
to look in 12.4.4, a section that I truly hate pawing through. :-(
I can't explain the "logic" of that section (though I've known people
who claim to understand it); all I can do is mechanistically follow it
through line by line. So, here we go. In the scoping unit of AA,
(which is what matters here)... considering the procedure name BB.

1. The procedure name is not established to be generic, because it
doesn't fit any of the conditions in (1). Well, that part makes
sense anyway, as there isn't anything generic in sight.

2. By (2)(a), the procedure name is established to be specific because
AA contains an internal subprogram that defines a procedure of that
name. By the first sentence of (2), it follows that the procedure
name is established to be only specific. (For those about
the "only" but, some names can be both specific and generic, but
this one isn't).

So we go to 12.4.4.2 ("established to be only specific").

(1) and (2) don't apply, but (3) does. Therefore, by (3), the
reference is to the procedure defined by the internal subprogram
of AA. That would b the version of BB that prints 1.

As aside, I note that subroutine AA_1 and its reference to BB play no
part at all in this, though I could imagine compilers getting messed up
by the presence of AA_1 (that would be a bug). You didn't ask about the
reference in AA_1, but if you did, then the trail of deduction would be
a bit longer. You'd end up going through 12.4.4(2)(e), which sends you
up to the host scoping unit of AA_1 to deduce that BB is established to
be specific. Then in 12.4.4.2, you'd similarly get sent to the host
via item (6). Thus the reference to BB in AA_1 is also to the internal
procedure BB instead of the module procedure, but that extra level of
logic might be what throws of some compilers.

In conclusion, I agree with the reported result of gfortran.

Have I mentioned that I hate reading that section? Do you see why? :-)

--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain | experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
Steven G. Kargl

2005-08-12, 10:32 pm

In article <nospam-2BBA49.15192512082005@news.supernews.com>,
Richard E Maine <nospam@see.signature> writes:
> In article <ddj4b5$knr$1@meiner.onlinehome.de>,
> Thomas Koenig <Thomas.Koenig@online.de> wrote:
>
> [code elided]
>
> If this ever comes up in any real code, what should happen is that
> the programmer should be taken out and shot. :-)


Thanks, Richard! With the w I've had, I needed a good laugh.

--
Steve
http://troutmask.apl.washington.edu/~kargl/
Abdul Qat

2005-08-13, 4:00 am


"Steven G. Kargl" <kargl@troutmask.apl.washington.edu> wrote in message
news:ddj8fm$9c$1@gnus01.u.washington.edu...
> In article <nospam-2BBA49.15192512082005@news.supernews.com>,
> Richard E Maine <nospam@see.signature> writes:
>
> Thanks, Richard! With the w I've had, I needed a good laugh.


So volunteer for Iraq, and take Maine with you.

Chicken,
Abdul Qat


Steve Lionel

2005-08-15, 5:02 pm

On Fri, 12 Aug 2005 23:29:09 +0200 (CEST), Thomas Koenig
<Thomas.Koenig@online.de> wrote:

>What should the following code print? I think it should be 1
>(gfortran agrees; this is from the gfortran testsuite), but
>ifort 8.1 disagrees and prints 2.


I agree and thank Richard for explaining the logic. I've sent this on to
development for a fix in ifort.

Steve Lionel
Software Products Division
Intel Corporation
Nashua, NH

User communities for Intel Software Development Products
http://softwareforums.intel.com/
Intel Fortran Support
http://developer.intel.com/software/products/support/
Ken Fairfield

2005-08-15, 5:02 pm

Steve Lionel wrote:
> On Fri, 12 Aug 2005 23:29:09 +0200 (CEST), Thomas Koenig
> <Thomas.Koenig@online.de> wrote:
>
>
>
>
> I agree and thank Richard for explaining the logic. I've sent this on to
> development for a fix in ifort.


If you can, please feed this back to the VMS Fortran folks as well
since it (version: HP Fortran V7.6-3276-48D52) suffers from the same
error.

Regards, Ken
--
I don't speak for Intel, Intel doesn't speak for me...

Ken Fairfield
D1C Automation VMS System Support
who: kenneth dot h dot fairfield
where: intel dot com
Steve Lionel

2005-08-15, 5:02 pm

On Mon, 15 Aug 2005 10:35:45 -0700, Ken Fairfield <my.full.name@intel.com>
wrote:

>
>If you can, please feed this back to the VMS Fortran folks as well
>since it (version: HP Fortran V7.6-3276-48D52) suffers from the same
>error.


The fix will be made available to HP automatically. It's up to them whether
they pick it up. If it's important to you, I recommend that you file a
support request with HP about it. You can reference Intel issue T57896 so
they know what to look for.

Steve Lionel
Software Products Division
Intel Corporation
Nashua, NH

User communities for Intel Software Development Products
http://softwareforums.intel.com/
Intel Fortran Support
http://developer.intel.com/software/products/support/
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com