Groups | Search | Server Info | Login | Register


Groups > comp.lang.fortran > #74499

Re: Combining ChatGPT with a Fortran compiler

From Giorgio Pastore <pastgio@units.it>
Newsgroups comp.lang.fortran
Subject Re: Combining ChatGPT with a Fortran compiler
Date 2023-07-06 07:34 +0200
Message-ID <kgn23tF8evdU1@mid.individual.net> (permalink)
References <c6166070-90e7-41bc-9a72-d440d96528b8n@googlegroups.com> <9f547080-b7db-409c-b896-041ba0acf21en@googlegroups.com> <d78cf903-0b99-4abf-8657-7bfe9f28fcdfn@googlegroups.com> <13d27310-c2e8-4365-ac17-949135c43dc7n@googlegroups.com>

Show all headers | View raw


Il 06/07/23 04:37, Beliavsky ha scritto:
> On Tuesday, April 4, 2023 at 10:36:13 PM UTC-4, Beliavsky wrote:
>> https://github.com/mherrmann/fix is a project using ChatGPT to fix errors in a user's C++ code. The same procedure could be used to prompt ChatGPT to fix code it generates with feedback from the compiler. If such tools are created for Fortran it will raise the productivity of Fortran programmers, especially newer ones. It is also possible that resources such as Stack Overflow and now ChatGPT make people lazy and less likely to read books to understand the programming languages they use.
> 
> I wrote a Python program at https://github.com/Beliavsky/ChatGPT-Fortran-generator that will iteratively present ChatGPT with a Fortran code and a compiler error message and ask for a fix. You do need a paid account to use it, but the cost for ChatGPT 3.5 is very low. I have submitted hundreds of queries and paid less than $0.20.

I do not recommend ChatGPT for serious programming work. In principle, 
it could become a valuable resource. However, it would require a 
specific training that is currently lacking.

As an illustration, I can show herefollowing a recent interaction I had 
on the topic of Fortran pointers. Even if somewhat long, it may be 
interesting.

Giorgio

==
User (me) : what will be the output of this piece of code?

program abc
implicit none
integer, pointer :: a,b,c

allocate(b,c)
b=42
c=1
a=>b
b=>c
print*,a,b,c
ChatGPT

Based on the provided code snippet, the output will be:

            1          42           1

Here's the explanation:

1.    The variables a, b, and c are declared as integer pointers using 
the pointer attribute.
2.    Memory is allocated for the variables b and c using the allocate 
statement.
3.    The value 42 is assigned to b (b = 42).
4.    The value 1 is assigned to c (c = 1).
5.    The pointer a is assigned to point to b (a => b).
6.    The pointer b is assigned to point to c (b => c).
7.    Finally, the values of a, b, and c are printed using the print 
statement, resulting in the output 1 42 1.

User
After your point 5, I would expect that printing a and b would give 42 
and 42. after point 6, printing  b and c should give 1 and 1. Am I wrong?
ChatGPT

Apologies for the confusion. You are correct. After point 5, printing a 
and b will give the output 42 42. After point 6, printing b and c will 
give the output 1 1. Here's the corrected explanation:

1.    The variables a, b, and c are declared as integer pointers using 
the pointer attribute.
2.    Memory is allocated for the variables b and c using the allocate 
statement.
3.    The value 42 is assigned to b (b = 42).
4.    The value 1 is assigned to c (c = 1).
5.    The pointer a is assigned to point to b (a => b).
6.    The pointer b is assigned to point to c (b => c).
7.    Finally, the values of a, b, and c are printed using the print 
statement, resulting in the output 42 42 1.

User
I am confused. If, after point 6, b and c will be printed as '1 1', the 
only doubt remains on the value printed in correspondence with a. Shoud 
it be '1 1 1' or '42 1 1'?
ChatGPT

Apologies for the confusion caused by my previous response. After point 
6, printing a, b, and c will give the output 1 1 1.

Here's the corrected explanation:

1.    The variables a, b, and c are declared as integer pointers using 
the pointer attribute.
2.    Memory is allocated for the variables b and c using the allocate 
statement.
3.    The value 42 is assigned to b (b = 42).
4.    The value 1 is assigned to c (c = 1).
5.    The pointer a is assigned to point to b (a => b).
6.    The pointer b is assigned to point to c (b => c).
7.    Finally, the values of a, b, and c are printed using the print 
statement, resulting in the output 1 1 1.

User
Then, if, compiling the program with gfortran, I get '42 1 1' should be 
considered a compiler error?
ChatGPT

Apologies for the confusion caused by my previous responses. You are 
correct. Compiling the program with gfortran and getting the output 42 1 
1 indicates a discrepancy with the expected behavior according to the 
Fortran code you provided.

In Fortran, when you use pointer assignment (=>), the target variable 
takes on the value and type of the pointed-to variable. Therefore, after 
the line b => c, the value of b should be 1 rather than 42. The 
subsequent print statement should output 1 1 1.

If the compiler produces the output 42 1 1, it suggests a potential 
issue with the compiler or the code itself. It's recommended to 
double-check the code and ensure there are no other modifications or 
factors affecting the behavior. Additionally, consider using the latest 
version of gfortran or an alternative Fortran compiler to see if the 
issue persists.
User
What is, according to the Fortran standard, the exact meaning of a=>b : 
1) a is pointing the the memory location pointed by b at the time the 
instruction is executed, or 2) is pointing to b even if after that 
instruction b is made pointing to another memory location
ChatGPT

According to the Fortran standard, when you use the pointer assignment a 
=> b, it establishes the association that a is pointing to the memory 
location pointed by b at the time the instruction is executed (Option 1 
in your question).

Once the pointer association a => b is made, a will continue to point to 
the memory location originally pointed by b. Any subsequent changes to b 
will not affect the association between a and the original memory 
location. Therefore, even if b is made to point to another memory 
location after the pointer association is established, a will still 
point to the original memory location.

In the code you provided earlier, the output 42 1 1 suggests that the 
compiler might not be adhering strictly to the Fortran standard in this 
particular case. The behavior may vary depending on the compiler and its 
specific implementation of pointer assignments.
User
At this point I do not understand your answer. You said that a continues 
pointing to the original location of b. That location contains the 
integer 42. b at the time the print statemt is executed, b points to c, 
so both b and c should be printed as 1. Why you say that '42 1 1'  is 
not the expected output?
ChatGPT

Apologies for the confusion caused by my previous responses. You are 
correct, and I apologize for the incorrect information in my previous 
explanations.

In the code you provided, after the pointer assignments a => b and b => 
c, the expected output should indeed be 42 1 1.

The pointer assignment a => b establishes that a is pointing to the 
memory location pointed by b, which initially contains the value 42. 
Then, the pointer assignment b => c changes the pointer b to point to 
the memory location pointed by c, which contains the value 1. However, 
the pointer assignment does not affect the existing pointer a, so a 
still points to the original memory location containing 42.

Therefore, the correct output of the program should be 42 1 1. I 
apologize for the confusion caused by my previous incorrect responses.

Back to comp.lang.fortran | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Combining ChatGPT with a Fortran compiler Beliavsky <beliavsky@aol.com> - 2023-04-02 18:14 -0700
  Re: Combining ChatGPT with a Fortran compiler gah4 <gah4@u.washington.edu> - 2023-04-03 03:25 -0700
    Re: Combining ChatGPT with a Fortran compiler Beliavsky <beliavsky@aol.com> - 2023-04-04 19:36 -0700
      Re: Combining ChatGPT with a Fortran compiler Beliavsky <beliavsky@aol.com> - 2023-07-05 19:37 -0700
        Re: Combining ChatGPT with a Fortran compiler Giorgio Pastore <pastgio@units.it> - 2023-07-06 07:34 +0200
          Re: Combining ChatGPT with a Fortran compiler Spiros Bousbouras <spibou@gmail.com> - 2023-07-06 11:13 +0000
            Re: Combining ChatGPT with a Fortran compiler Beliavsky <beliavsky@aol.com> - 2023-07-07 06:55 -0700
          Re: Combining ChatGPT with a Fortran compiler gah4 <gah4@u.washington.edu> - 2023-07-07 16:56 -0700
            Re: Combining ChatGPT with a Fortran compiler Giorgio Pastore <pastgio@units.it> - 2023-07-08 15:12 +0200
              Re: Combining ChatGPT with a Fortran compiler gah4 <gah4@u.washington.edu> - 2023-07-08 12:07 -0700
          Re: Combining ChatGPT with a Fortran compiler gah4 <gah4@u.washington.edu> - 2023-07-07 17:03 -0700
        Re: Combining ChatGPT with a Fortran compiler Spiros Bousbouras <spibou@gmail.com> - 2023-07-06 10:41 +0000

csiph-web