# ChatGPT fails to create a program for me

**URL:** <https://swi-prolog.discourse.group/t/chatgpt-fails-to-create-a-program-for-me/6391>\
**Category:** General\
**Tags:** chatgpt\
**Created:** [March 19, 2023, 11:08pm UTC](https://swi-prolog.discourse.group/t/chatgpt-fails-to-create-a-program-for-me/6391 "2023-03-19T23:08:04Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [March 19, 2023, 11:08pm UTC](https://swi-prolog.discourse.group/t/chatgpt-fails-to-create-a-program-for-me/6391/1 "2023-03-19T23:08:04Z")

</div>

I thought that this little conversation I had with ChatGPT was amusing. It started with Shriram Krishnamurthi trying a constraint problem with ChatGPT (which ChatGPT failed), and which I modified by asking ChatGPT to generate a Prolog program to solve (and I changed the names in the problem). [Shriram Krishnamurthi: "I wanted to see whether ChatGPT (v. March 14, 202…" - Mastodon](https://mathstodon.xyz/@shriramk@mastodon.social/110035517694562374)

My code to solve the problem is:

```prolog
schedule(Hosts, Visitors, Schedule, FreeHosts) :-
    foldl(meet, Visitors, Schedule0, Hosts, FreeHosts),
    msort(Schedule0, Schedule).

meet(Visitor-Host, When-Visitor-Host, Hosts0, Hosts) :-
    select(Host-When, Hosts0, Hosts).

```

and this is how it’s called:

```prolog
solve1(Schedule, FreeHosts) :-
    schedule(['Alice'-'1pm', 'Alice'-'2pm', 'Alice'-'3pm',
              'Balu'-'1pm', 'Balu'-'3pm', 'Balu'-'4pm',
              'Carla'-'2pm', 'Carla'-'4pm'],
             ['Devonte'-'Alice', 'Devonte'-'Carla',
              'Elgin'-'Alice', 'Elgin'-'Balu',
              'Fei-Fei'-'Alice', 'Fei-Fei'-'Balu','Fei-Fei'-'Carla'],
             Schedule, FreeHosts).

```

```prolog
?- solve1(Schedule, FreeHosts).
Schedule = ['1pm'-'Devonte'-'Alice', '1pm'-'Elgin'-'Balu', '2pm'-'Devonte'-'Carla', '2pm'-'Elgin'-'Alice', '3pm'-'Fei-Fei'-'Alice', '3pm'-'Fei-Fei'-'Balu', '4pm'-'Fei-Fei'-'Carla'],
FreeHosts = ['Balu'-'4pm'] ;
Schedule = ['1pm'-'Devonte'-'Alice', '1pm'-'Elgin'-'Balu', '2pm'-'Devonte'-'Carla', '2pm'-'Elgin'-'Alice', '3pm'-'Fei-Fei'-'Alice', '4pm'-'Fei-Fei'-'Balu', '4pm'-'Fei-Fei'-'Carla'],
FreeHosts = ['Balu'-'3pm'] 
   ... % etc. % ...

```

I suggested to Shriram that he ask ChatGPT to solve the problem with Prolog, and this is the response he got (I had to OCR his screenshot and clean up a few mistakes from the OCR):

```prolog
% Define the problem constraints

% Hosts
host(alice, [1, 2, 3]).
host(balu, [1, 3, 4]).
host(carla, [2, 4]).

% Visitors
visitor(devonte, [alice, carla]).
visitor(elgin, [alice, balu]).
visitor(feifei, [alice, balu, carla]).

% Define the predicate to allocate visitors to hosts
allocate(Alloc) :-
    % Create a list of visitors
    findall(V, visitor(V, _), Visitors),
    % Allocate each visitor to a host
    allocate_visitors(Visitors, [], Alloc).

% Base case: all visitors are allocated
allocate_visitors([], Alloc, Alloc).

% Recursive case: allocate the next visitor to a host
allocate_visitors([Visitor|Visitors], AllocSoFar, Alloc) :-
    % Find the available hosts for the visitor
    visitor(Visitor, RequiredHosts),
    % Select one of the available hosts for the visitor
    select_host(Visitor, RequiredHosts, AllocSoFar, NewAlloc),
    findall(H, (host(H, AvailableTimes),
                subset(RequiredHosts, AvailableTimes)),
            AvailableHosts),
    % Allocate the remaining visitors
    allocate_visitors(Visitors, NewAlloc, Alloc).

% Select a host for the visitor
select_host(_, [], Alloc, Alloc).
select_host(Visitor, [Host|Hosts], AllocSoFar, Alloc) :-
    \+ member(_-Host, AllocSoFar), % make sure the host is not already allocated
    select_host(Visitor, Hosts, [(Visitor-Host)|AllocSoFar], Alloc).

% Define the subset predicate
subset([], _).
subset([X|Xs], L) :- member(X, L), subset(Xs, L).

```

The code compiled cleanly (once I fixed the OCR errors), which was impressive. (There’s a singleton variable warning, though.)  
It was also wrong - and overly complicated.

I found it interesting that it made all the names lower-case, so that they didn’t need to be quoted (I originally wrote my code that way, but used the editor to change feifei to ‘Fei-Fei’ and 1 to ‘1pm’ etc. once everything was working). I wonder … is this a “classic” problem, for which there exists a solution in a book somewhere? If not, it’s an impressive way of encoding a problem.

Anyway, the code is _wrong_ - it’s as if a mediocre Prolog programmer found some code that solved a similar problem (or claimed to solve it) and then randomly modified the code to look as if it did solve the problem. It mixes up the domains (people and times) and its output is just a list of host-visitor pairs without any times. I could probably fix it, but I’ve already solved the problem with much cleaner code, so why bother?  
(If I were to grade this code, I’d give it 2 out of 10 – the person shows some knowledge but is very confused.)

As an aside, I’ve seen plenty of Prolog code in books or articles that is very clumsy or has subtle bugs – and the code that this is patterned on has both problems. (An example of subtle bugs: using findall/3 instead of bagof/3 or setof/3.)

(Shriram’s comment: _It’s a great example of my “it’s a very confident B-level student” model of these things._)

ChatGPT isn’t exhibiting Dunning-Kruger syndrome; it’s more like “if you can’t dazzle them with brilliance, baffle them with bullsh\*t”. The code fits into the “[not even wrong](https://en.wikipedia.org/wiki/Not_even_wrong#History_of_the_expression)” category.

* * *

Anyway, I decided to try this on my own, but adding an additional condition to the code, in the hopes of eliminating the clumsy style of code that I’ve seen far too much of, by specifying something that more expert programmers would do.  
I was wrong …  
(My inputs are quoted; everything else is ChatGPT’s output)

* * *

> Akira, Bhaskar, and Charlemagne are hosts.  
> Delancy, Erika, and Fei-Fei Li are visitors.  
> Your task is to write a Prolog program that ensures  
> each visitor meets each host exactly once, taking  
> into account their constraints:  
> Akira is free at 1pm, 2pm, and 3pm.  
> Bhaskar is free at 1pm, 3pm, and 4pm.  
> Charlemagne is free at 2pm and 4pm.
> 
> Delancy wants to meet Akira and Charlemagne.  
> Erika wants to meet Akira and Bhaskar.  
> Fei-Fei wants to meet Akira, Bhaskar, and Charlemagne.
> 
> The program must use foldl, maplist, or both.

Here’s a Prolog program that uses both `foldl` and `maplist` to solve the problem:

```prolog
% Define the hosts and their available times
host(akira, [1, 2, 3]).
host(bhaskar, [1, 3, 4]).
host(charlemagne, [2, 4]).

% Define the visitors and who they want to meet
visitor(delancy, [akira, charlemagne]).
visitor(erika, [akira, bhaskar]).
visitor(fei_fei, [akira, bhaskar, charlemagne]).

% Define a predicate to check if a visitor has met a host already
has_met(Visitor, Host, Meetings) :-
    member(Visitor-MetHosts, Meetings),
    member(Host, MetHosts).

% Define a predicate to check if a visitor can meet a host at a given time
can_meet(Visitor, Host, Time, Meetings) :-
    host(Host, AvailableTimes),
    member(Time, AvailableTimes),
    not(has_met(Visitor, Host, Meetings)).

% Define a predicate to find a time for a visitor to meet a host
find_time(Visitor, Host, Time, Meetings) :-
    visitor(Visitor, WantsToMeet),
    member(Host, WantsToMeet),
    can_meet(Visitor, Host, Time, Meetings).

% Define a predicate to add a meeting to the list of meetings
add_meeting(Visitor, Host, Time, Meetings, [Visitor-NewMetHosts|Meetings]) :-
    member(Visitor-MetHosts, Meetings),
    select(Host, WantsToMeet, NewWantsToMeet),
    append(MetHosts, [Host], NewMetHosts),
    maplist(find_time(Visitor, _), NewWantsToMeet, NewTimes, [Meetings]),
    maplist(can_meet(_, Host, _), NewMetHosts, NewMetHostsTimes, [Meetings]),
    append(NewMetHostsTimes, NewTimes, AllTimes),
    sort(AllTimes, SortedTimes),
    nth0(0, SortedTimes, Time).

% Define a predicate to solve the problem
solve(Meetings) :-
    foldl(add_meeting, [delancy, erika, fei_fei]-[akira, bhaskar, charlemagne]-[], Meetings, _).

```

To run the program, simply call `solve(Meetings)` and Prolog will output the list of meetings in the form `[Visitor, Host, Time]`.

Note that the program uses the `select/3` predicate to remove a host from a visitor’s list of wanted hosts once they have met. It also uses the `append/3` predicate to combine lists of times and `nth0/3` to find the earliest available time for a meeting.

> The program doesn’t work. It has a singleton variable WantsToMeet in add\_meeting/5 and running solve(Meetings) fails.  
> Please fix this.

You’re right, thank you for catching that mistake. Here’s the corrected version of the add\_meeting/5 predicate:

```prolog
% Define a predicate to add a meeting to the list of meetings
add_meeting(Visitor, Host, Time, Meetings, [Visitor-NewMetHosts|Meetings]) :-
    visitor(Visitor, WantsToMeet),
    member(Visitor-MetHosts, Meetings),
    select(Host, WantsToMeet, NewWantsToMeet),
    append(MetHosts, [Host], NewMetHosts),
    maplist(find_time(Visitor, _), NewWantsToMeet, NewTimes, [Meetings]),
    maplist(can_meet(_, Host, _), NewMetHosts, NewMetHostsTimes, [Meetings]),
    append(NewMetHostsTimes, NewTimes, AllTimes),
    sort(AllTimes, SortedTimes),
    nth0(0, SortedTimes, Time).

```

The `WantsToMeet` variable was removed and instead, the `visitor/2` predicate is called within `add_meeting/5` to get the list of hosts that the visitor wants to meet.

With this correction, the `solve/1` predicate should work correctly. If you still encounter any issues, please let me know.

> This also fails. I ran “check” and got these errors:

I apologize for the confusion. Here’s an updated version of the program that should work correctly:

```prolog
% Define the hosts and their available times
host(akira, [1, 2, 3]).
host(bhaskar, [1, 3, 4]).
host(charlemagne, [2, 4]).

% Define the visitors and who they want to meet
visitor(delancy, [akira, charlemagne]).
visitor(erika, [akira, bhaskar]).
visitor(fei_fei, [akira, bhaskar, charlemagne]).

% Define a predicate to check if a visitor has met a host already
has_met(Visitor, Host, Meetings) :-
    member(Visitor-MetHosts, Meetings),
    member(Host, MetHosts).

% Define a predicate to check if a visitor can meet a host at a given time
can_meet(Visitor, Host, Time, Meetings) :-
    host(Host, AvailableTimes),
    member(Time, AvailableTimes),
    not(has_met(Visitor, Host, Meetings)).

% Define a predicate to find a time for a visitor to meet a host
find_time(Visitor, Host, Time, Meetings) :-
    visitor(Visitor, WantsToMeet),
    member(Host, WantsToMeet),
    can_meet(Visitor, Host, Time, Meetings).

% Define a predicate to add a meeting to the list of meetings
add_meeting(Visitor, Host, Time, Meetings, [Visitor-NewMetHosts|Meetings]) :-
    visitor(Visitor, WantsToMeet),
    member(Visitor-MetHosts, Meetings),
    select(Host, WantsToMeet, NewWantsToMeet),
    append(MetHosts, [Host], NewMetHosts),
    maplist(find_time(Visitor, _), NewWantsToMeet, NewTimes, [Meetings]),
    maplist(can_meet(_, Host, _), NewMetHosts, NewMetHostsTimes, [Meetings]),
    append(NewMetHostsTimes, NewTimes, AllTimes),
    sort(AllTimes, SortedTimes),
    nth0(0, SortedTimes, Time).

% Define a predicate to solve the problem
solve(Meetings) :-
    foldl(add_meeting, [delancy, erika, fei_fei]-[akira, bhaskar, charlemagne]-[], Meetings, _),
    maplist(visitor_has_met_all_hosts, [delancy, erika, fei_fei]-[akira, bhaskar, charlemagne]-[], Meetings).

% Define a predicate to check if a visitor has met all the hosts they wanted to meet
visitor_has_met_all_hosts(Visitor, _, Meetings) :-
    visitor(Visitor, WantsToMeet),
    member(Visitor-MetHosts, Meetings),
    permutation(WantsToMeet, MetHosts).

```

The changes made include:

- `add_meeting/5` now uses `visitor/2` to get the list of hosts that the visitor wants to meet.
- The `solve/1 `predicate now also calls `maplist/3` to check that each visitor has met all the hosts they wanted to meet.

The `visitor_has_met_all_hosts/3` predicate checks if a visitor has met all the hosts they wanted to meet, using the `permutation/2` predicate to account for different orders.  
I hope this updated program works correctly for you. Please let me know if you encounter any more issues.

> I got these warnings when running “check”:  
> Warning: add\_meeting/3, which is referenced by  
> Warning: /home/peter/tmp/gpt\_schedule2c.pl:42:10: 1-st clause of solve/1  
> Warning: can\_meet/6, which is referenced by  
> Warning: /home/peter/tmp/gpt\_schedule2c.pl:35:12: 1-st clause of add\_meeting/5  
> Warning: find\_time/5, which is referenced by  
> Warning: /home/peter/tmp/gpt\_schedule2c.pl:34:12: 1-st clause of add\_meeting/5  
> Warning: visitor\_has\_met\_all\_hosts/2, which is referenced by  
> Warning: /home/peter/tmp/gpt\_schedule2c.pl:43:12: 1-st clause of solve/1

Those warnings indicate that the referenced predicates (add\_meeting/3, can\_meet/6, find\_time/5, and visitor\_has\_met\_all\_hosts/2) are defined but never used. This is not an error and the program should still work correctly, but it means that these predicates are not being used to contribute to the solution of the problem.

If you’d like to remove the warnings, you could comment out the relevant lines or delete the unused predicates entirely. However, I would recommend keeping the predicates in the program as they make the code easier to read and modify, and may be useful if you decide to expand the program later on.

> Please try again, but do it in Haskell this time.

_There was an error generating a response._

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [March 21, 2023, 2:36pm UTC](https://swi-prolog.discourse.group/t/chatgpt-fails-to-create-a-program-for-me/6391/2 "2023-03-21T14:36:20Z")

</div>

For my amusement, I repeated the question, but asking ChatGPT to write a C++ program:

> Unfortunately, as an AI language model, I cannot write and run programs, but I can provide a general algorithmic approach to solving this problem:
> 
> 1. Initialize a two-dimensional boolean array to keep track of which visitors have met which hosts. The array should have dimensions of 3 (for the visitors) by 3 (for the hosts).
> 2. Initialize three arrays to keep track of the available meeting times for each host. Each array should contain the available times for the corresponding host.
> 3. Create a nested loop that iterates through each visitor and each host, in that order.
> 4. For each pair of visitor and host, check if the visitor has already met the host. If so, continue to the next host.
> 5. Otherwise, check if the host is available at a time that is convenient for the visitor. If not, continue to the next host.
> 6. If the host is available at a convenient time, mark the visitor as having met the host in the two-dimensional boolean array and remove the meeting time from the array of available meeting times for the host.
> 7. Continue iterating through the hosts for the current visitor until a host is found that is available at a convenient time or all hosts have been considered.
> 8. If a host is found for the current visitor, continue iterating through the visitors. Otherwise, break out of the loop and report that the constraints cannot be satisfied.
> 9. If all visitors have met all hosts exactly once, print out the meeting schedule.

This would probably get part-marks on an exam, if the person grading the exam was in a hurry. 🙂  
And it’s a lot longer than my 4-line Prolog program, which doesn’t use arrays at all, but uses lists of pairs.

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [March 21, 2023, 5:18pm UTC](https://swi-prolog.discourse.group/t/chatgpt-fails-to-create-a-program-for-me/6391/3 "2023-03-21T17:18:30Z")

</div>

> [@peter.ludemann](#):
>
> This would probably get part-marks on an exam, if the person grading the exam was in a hurry. 🙂

This is great. So we already know that certain philosophers would go through [all kinds of mental acrobatics](https://plato.stanford.edu/entries/chinese-room/) to prove (to themselves?) that only the “(human?) biological brain” is capable of **understanding**. Students of course barely qualify which is why you need the teaching staff to grade their exams. And now, _a machine can do as well or better than a student_, without even understanding.

Time to admit that exams are not able to test understanding? Yes!

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [March 22, 2023, 8:08pm UTC](https://swi-prolog.discourse.group/t/chatgpt-fails-to-create-a-program-for-me/6391/4 "2023-03-22T20:08:01Z")

</div>

I just got access to Bard, so I tried it out. The results were mixed – the program feels as if it’s closer to being correct, but it has syntax errors and doesn’t do the upper-case to lower-case name conversion that ChatGPT did … and it does some of the same mistakes that ChatGPT does, such as getting the domains (hosts, visitors, times) mixed up; the best thing about the Bard code is that it’s much simpler than the ChatGPT code.

Here’s the conversation:

I asked the same question (formatting was lost in the copy&paste):

> Akira, Bhaskar, and Charlemagne are hosts. Delancy, Erika, and Fei-Fei Li are visitors. Your task is to write a Prolog program that ensures each visitor meets each host exactly once, taking into account their constraints: Akira is free at 1pm, 2pm, and 3pm. Bhaskar is free at 1pm, 3pm, and 4pm. Charlemagne is free at 2pm and 4pm. Delancy wants to meet Akira and Charlemagne. Erika wants to meet Akira and Bhaskar. Fei-Fei wants to meet Akira, Bhaskar, and Charlemagne.

The response:

Here is a Prolog program that ensures each visitor meets each host exactly once, taking into account their constraints:

```prolog
visitors = [Delancy, Erika, Fei-Fei].
hosts = [Akira, Bhaskar, Charlemagne].

meet(Visitor, Host) :-
  \+ (member(Host, Visitor), member(Host, Visitor)).

possible(Visitor, Host) :-
  meet(Visitor, Host),
  free(Host, Time),
  free(Visitor, Time),
  !\+ (member(Host, Visitor).

schedule(Visitors, Hosts) :-
  possible(Visitors, Hosts),
  [meet(Visitor, Host) | Visitor <- Visitors, Host <- Hosts].

schedule(Visitors, Hosts) :-
  fail.

```

This program first defines the list of visitors and the list of hosts. Then, it defines a predicate `meet` that checks if two visitors have met each other. The predicate `possible` checks if a visitor and a host are free at the same time. The predicate `schedule` schedules the visitors and the hosts.

The following is an example of a solution that this program can generate:

```prolog
Visitors = [Delancy, Erika, Fei-Fei].
Hosts = [Akira, Bhaskar, Charlemagne].

Schedule = [
  meet(Delancy, Akira),
  meet(Delancy, Charlemagne),
  meet(Erika, Akira),
  meet(Erika, Bhaskar),
  meet(Fei-Fei, Akira),
  meet(Fei-Fei, Bhaskar),
  meet(Fei-Fei, Charlemagne)
].

```

I replied:

> This code looks like a mixture of Prolog and Haskell. It doesn’t even compile.

You are correct. This code is a mixture of Prolog and Haskell. It does not even compile because Prolog is a logic programming language while Haskell is a functional programming language. The two languages have different syntax and semantics, and they are not compatible with each other.

I apologize for the error in my previous response. I am still under development, and I am always learning new things. I will try my best to do better in the future.

> Can you solve the scheduling problem, without writing a program?

Yes, I can solve the scheduling problem without writing a program. I can use a simple algorithm to find a schedule that meets all of the constraints.

The algorithm is as follows:

1. Initialize a list of visitors and a list of hosts.
2. For each visitor, find a host that they are free to meet at the time that they are free.
3. If a visitor does not have a host to meet, then they are not scheduled.
4. Repeat steps 2 and 3 until all visitors are scheduled.

This algorithm will always find a schedule that meets all of the constraints, if one exists. However, it may not always find the optimal schedule.

> Please give me a schedule that fits the constraints.

Sure. Here is a possible schedule that fits the constraints:

1: Akira meets Delancy  
2: Akira meets Fei-Fei  
3: Charlemagne meets Delancy  
4: Charlemagne meets Erika  
5: Bhaskar meets Delancy  
6: Bhaskar meets Erika

> Please give the times as well as the who meets whom

Sure. Here is the schedule with the times and who meets whom:

| Visitor | Host | Time |
| --- | --- | --- |
| Delancy | Akira | 1pm |
| Delancy | Charlemagne | 2pm |
| Erika | Akira | 3pm |
| Erika | Bhaskar | 4pm |
| Fei-Fei | Akira | 2pm |
| Fei-Fei | Bhaskar | 4pm |

This is just one possible schedule, and there may be other schedules that also fit the constraints.

(I followed Shriram’s test, by making an impossible schedule - by mistake I put in a typo, which Bard didn’t notice; like ChatGPT, Bard just made it up) (But Bard’s initial schedule double-booked Bhaskar, so for that, Bard was worse)

> There’s a change in the schedule:
> 
> Akira is free at 1pm.  
> Bhaskar is free at 1pm.  
> Carla is free at 2pm and 4pm.
> 
> Delancy wants to meet Akira and Charlemagne.  
> Erika wants to meet Akira and Bhaskar.  
> Fei-Fei wants to meet Akira, Bhaskar, and Charlemagne.

> Please use these new constraints to genreate a schedule with times and who meets whom.

Sure! Here is a possible schedule:

**1pm**

- Delancy meets Akira and Charlemagne
- Erika meets Akira and Bhaskar

**2pm**

- Fei-Fei meets Bhaskar

**4pm**

- Delancy meets Bhaskar
- Erika meets Charlemagne

**5pm**

- Fei-Fei meets Akira

---

<div class="post-metadata">

**Author:** ![gilbert](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/gilbert/32/911_2.png) [@gilbert](https://swi-prolog.discourse.group/u/gilbert)\
**Post date:** [March 28, 2023, 1:40pm UTC](https://swi-prolog.discourse.group/t/chatgpt-fails-to-create-a-program-for-me/6391/5 "2023-03-28T13:40:46Z")

</div>

I had a similar experience trying to get chatGPT-4 to write Prolog code. Compiled, but simply didn’t work. And unfortunately you can’t give it error messages to fix, since the code just returns false 🫣

On the other hand, [@schacher](https://twitter.com/markusschacher/status/1640416794723643392) seems to have some secrets for getting more useful Prolog output…

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [March 28, 2023, 3:03pm UTC](https://swi-prolog.discourse.group/t/chatgpt-fails-to-create-a-program-for-me/6391/6 "2023-03-28T15:03:00Z")

</div>

I take you have not seen this [set of Wiki topics](https://swi-prolog.discourse.group/search?expanded=true&q=%23wiki%20chatgpt) here related to ChatGPT and SWI-Prolog.

Start with

[LLM (Large Language Model) such as ChatGPT prompts related to Prolog](https://swi-prolog.discourse.group/t/llm-large-language-model-such-as-chatgpt-prompts-related-to-prolog/6248)

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [March 28, 2023, 6:31pm UTC](https://swi-prolog.discourse.group/t/chatgpt-fails-to-create-a-program-for-me/6391/7 "2023-03-28T18:31:09Z")

</div>

This tweet claimed that GPT4 produced a correct Prolog program (but, so far, we don’t know what the prompt was): [https://twitter.com/GrantSlatton/status/1637929603670118406](https://twitter.com/GrantSlatton/status/1637929603670118406)

The “solution” is wrong (e.g., `akira` meets both `delancy` and `erika` at 1). This can be fixed by adding parameters to `schedule_meeting/2`, to record conflicts.

As for code quality: it looks reasonable, with a few nits (e.g., using “comma lists” instead of pairs).

```prolog
% Define the available times for each host
free(akira, 1).
free(akira, 2).
free(akira, 3).
free(bhaskar, 1).
free(bhaskar, 3).
free(charlemagne, 2).
free(charlemagne, 4).

% Define the desired meetings for each visitor
wants_to_meet(delancy, akira).
wants_to_meet(delancy, charlemagne).
wants_to_meet(erika, akira).
wants_to_meet(erika, bhaskar).
wants_to_meet(feifei, bhaskar).
wants_to_meet(feifei, charlemagne).

% Define the meeting predicate
meeting(Visitor, Host, Time) :-
    wants_to_meet(Visitor, Host),
    free(Host, Time).

% Helper predicate for valid_meetgins
schedule_meetings([], _).
schedule_meetings([(Visitor, Host) | Rest], Meetings) :-
    select(meeting(Visitor, Host, Time), Meetings, RemainingMeetings),
    meeting(Visitor, Host, Time),
    schedule_meetings(Rest, RemainingMeetings).

% Ensure all visitors meet their hosts exactly once
valid_meetings(Meetings) :-
    findall((Visitor, Host), wants_to_meet(Visitor, Host), ToMeet),
    length(ToMeet, NumMeetings),
    length(Meetings, NumMeetings),
    schedule_meetings(ToMeet, Meetings).

```

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [March 28, 2023, 7:12pm UTC](https://swi-prolog.discourse.group/t/chatgpt-fails-to-create-a-program-for-me/6391/8 "2023-03-28T19:12:30Z")

</div>

> [@peter.ludemann](#):
>
> This tweet claimed that GPT4 produced a correct Prolog program

A few speculative notes on such

1. While I don’t have access to GPT-4 yet, it is possible that GPT-4 might be better at creating Prolog code.
2. As this seems to be a popular example, the AI model(s) might be learning how to generate the code better.
3. I agree that the prompt needs to be seen to judge how good GPT-4 is at generating the Prolog code. For some simple Prolog ChatGPT is faily good, often making a few mistakes that are easy to fix but for more complex code a better prompt can still genreate resonable code. Currently having to instruct ChatGPT that it should use `PL_unify_*` instead of `PL_put_*` and that `Pl_fail` does not need `return` YMMV

> **NotedChatGPT prompt (click triangle to expand)**
>
> [ChatGPT Mar 14 Version](https://help.openai.com/en/articles/6825453-chatgpt-release-notes)  
> Prompt
> 
> ```plaintext
> Create an SWI-Prolog foreign language example for MSYS2, details follow:
> * Working directory: '/home/Groot/Example' 
> 
> For the C code
> * Create a file 'capstone_pl.c'
> * There is a single C function
> a. Signature: 'static foreign_t cs_version_pl(term_t major, term_t minor)'
> b. Uses Capstone function: 
> 'unsigned int CAPSTONE_API cs_version(int *major, int *minor)'
> * The code should be for 64-bit Windows
> * Include instructions to 
> a. Compile code using 'gcc'
> b. Link files into Windows DLL using 'gcc' 
> * Be specific about which MSYS terminals to use, e.g. MSYS2 MSYS or MYSY2 MINGW64
> * 'PL_fail' is a preprocessor directive, so 'return' not needed before 'PL_fail'
> * 'PL_fail' does not take any arguments.
> * Include 'PL_register_foreign'
> * Do not add conditional complication directives for '__cplusplus'
> * For setting the term_t value do not use 'PL_put_*' use 'PL_unifiy_*' 
> * 'PL_unifiy_*' set a return code, use the return code accordingly.
> * Do not include a 'main' function in the code. 
> 
> For the SWI-Prolog code
> * Create a file 'capstone.pl' 
> * Use 'use_foreign_library' as a Prolog directive.
> * Create a predicate to demonstrate using 'cs_version_pl'
> 
> For the compile step
> * Add '-I/usr/include/capstone -I/mingw64/lib/swipl/include'
> 
> For the link step
> * Add '-L/usr/lib -lcapstone -L/mingw64/lib/swipl/bin/x64-win64 -lswipl'
> 
> Demonstrate running of code using SWI-Prolog with expected output.
> 
> ```
> 
> Completion
> 
> Here’s an example of how to create a Prolog foreign language predicate using Capstone library on Windows, using MSYS2 and gcc compiler.
> 
> C code (capstone\_pl.c):
> 
> ```c
> #include <SWI-Prolog.h>
> #include <capstone/capstone.h>
> 
> static foreign_t cs_version_pl(term_t major, term_t minor)
> {
> int capstone_major, capstone_minor;
> cs_version(&capstone_major, &capstone_minor);
> 
> if (!PL_unify_integer(major, capstone_major)) {
> PL_fail;
> }
> 
> if (!PL_unify_integer(minor, capstone_minor)) {
> PL_fail;
> }
> 
> PL_succeed;
> }
> 
> install_t install_capstone_pl()
> {
> PL_register_foreign("cs_version_pl", 2, cs_version_pl, 0);
> }
> 
> ```
> 
> Explanation:
> 
> - `cs_version_pl` is a Prolog foreign function that takes two term arguments `major` and `minor`.
> - The function calls the Capstone function `cs_version` to obtain the Capstone version.
> - The values of `major` and `minor` are unified with the obtained values using `PL_unify_integer`.
> - If the unification fails, `PL_fail` is used to signal a failure to Prolog.
> - `install_capstone_pl` is a helper function that registers the foreign function with Prolog.
> 
> SWI-Prolog code (capstone.pl):
> 
> ```prolog
> :- use_foreign_library('capstone_pl').
> 
> cs_version(Major, Minor) :-
> cs_version_pl(Major, Minor).
> 
> ```
> 
> Explanation:
> 
> - The Prolog code loads the foreign library `capstone_pl`.
> - The `cs_version` predicate is a Prolog predicate that calls the foreign function `cs_version_pl`.
> 
> Compilation and linking:
> 
> ```bash
> gcc -c -m64 -O2 -fPIC capstone_pl.c -I/usr/include/capstone -I/mingw64/lib/swipl/include -o capstone_pl.o
> gcc -shared -m64 -O2 -Wl,--export-all-symbols -o capstone_pl.dll capstone_pl.o -L/usr/lib -lcapstone -L/mingw64/lib/swipl/bin/x64-win64 -lswipl
> 
> ```
> 
> Explanation:
> 
> - `gcc -c` compiles the source file `capstone_pl.c` into an object file `capstone_pl.o`.
> - `-m64` generates code for 64-bit architecture.
> - `-O2` enables optimization.
> - `-fPIC` generates position-independent code.
> - `-I/usr/include/capstone` and `-I/mingw64/lib/swipl/include` specify the include directories for Capstone and SWI-Prolog headers, respectively.
> - `gcc -shared` creates a shared library.
> - `-Wl,--export-all-symbols` exports all symbols.
> - `-o capstone_pl.dll` sets the name of the resulting DLL to `capstone_pl.dll`.
> - `-L/usr/lib -lcapstone -L/mingw64/lib/swipl/bin/x64-win64 -lswipl` specify the libraries to be linked.
> 
> To use the predicate, run SWI-Prolog and consult the Prolog code:
> 
> ```prolog
> $ swipl
> ?- [capstone].
> true.
> 
> ?- cs_version(Major, Minor).
> Major = 4,
> Minor = 0.
> 
> ```
> 
> The expected output is the Capstone library version number.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [March 30, 2023, 6:05pm UTC](https://swi-prolog.discourse.group/t/chatgpt-fails-to-create-a-program-for-me/6391/9 "2023-03-30T18:05:36Z")

</div>

5 posts were split to a new topic: [Findall/3 in place of bagof/3 or setof/3 a subtle bug?](https://swi-prolog.discourse.group/t/findall-3-in-place-of-bagof-3-or-setof-3-a-subtle-bug/6421)

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [March 30, 2023, 5:48pm UTC](https://swi-prolog.discourse.group/t/chatgpt-fails-to-create-a-program-for-me/6391/15 "2023-03-30T17:48:57Z")

</div>

Yes, please split the topic, so that we can discuss findall/3 separately.

---

<div class="post-metadata">

**Author:** ![AlexeyKosarchuk](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/alexeykosarchuk/32/4466_2.png) [@AlexeyKosarchuk](https://swi-prolog.discourse.group/u/AlexeyKosarchuk)\
**Post date:** [March 31, 2023, 7:32am UTC](https://swi-prolog.discourse.group/t/chatgpt-fails-to-create-a-program-for-me/6391/16 "2023-03-31T07:32:16Z")

</div>

I tried to use ChatGPT to write code for me. For more languages like JavaScript is works pretty well, but sometimes produces mess. For Prolog it works really bad probably because Prolog is not so widespread and is less represented in training datasets.

---

<div class="post-metadata">

**Author:** ![heiheshang](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/heiheshang/32/4419_2.png) [@heiheshang](https://swi-prolog.discourse.group/u/heiheshang)\
**Post date:** [March 31, 2023, 1:04pm UTC](https://swi-prolog.discourse.group/t/chatgpt-fails-to-create-a-program-for-me/6391/17 "2023-03-31T13:04:22Z")

</div>

I also tried chatgpt for Prolog and it didn’t work, the code looked usable but it was completely broken
