[Haskell-cafe] GPT & Haskell

Gregory Guthrie guthrie at miu.edu
Fri Mar 31 22:07:03 UTC 2023


---------- Forwarded message ----------
From: "Nenad Končar" <nlpresearcher at gmail.com<mailto:nlpresearcher at gmail.com>>
To: Levent Erkok <erkokl at gmail.com<mailto:erkokl at gmail.com>>
Cc: Gregory Guthrie <guthrie at miu.edu<mailto:guthrie at miu.edu>>, "haskell-cafe at haskell.org<mailto:haskell-cafe at haskell.org>" <haskell-cafe at haskell.org<mailto:haskell-cafe at haskell.org>>, Haskell Community <replies+f6607596e3ee63f105e2b0327b86f062 at discourse.haskell.org<mailto:replies%2Bf6607596e3ee63f105e2b0327b86f062 at discourse.haskell.org>>
Bcc:
Date: Sat, 1 Apr 2023 00:00:09 +0200
Subject: Re: [Haskell-cafe] GPT & Haskell
On Fri, 31 Mar 2023 at 23:24, Levent Erkok <erkokl at gmail.com<mailto:erkokl at gmail.com>> wrote:
This code is buggy when lo + hi overflows your integer type. Classic (extremely) corner  case bug: https://ai.googleblog.com/2006/06/extra-extra-read-all-about-it-nearly.html

Thank you kindly for pointing this out. GPT-4, when prompted, is able to detect and correct the overflow mistake as follows:


After I introduced a purposeful mistake in the Haskel code, I prompted GPT-4 as follows:
If the following Haskel code contains any logical mistakes then correct the code for me:


-- Binary search in Haskell
-- Precondition: the list is sorted in ascending order
-- Postcondition: the function returns the index of the target element if found, or Nothing otherwise
-- Invariant: the search space is always within the bounds of the list
-- Variant: the search space decreases by half at each recursive call
-- Internal state: the lower and upper bounds of the search space

binarySearch :: Ord a => [a] -> a -> Maybe Int
binarySearch xs x = go xs x 0 (length xs - 1)
  where
    go xs x lo hi
      | lo > hi = Nothing -- base case: search space is empty
      | x == midElem = Just mid -- base case: target element is found
      | x < midElem = go xs x lo (mid - 1) -- recursive case: search in the left half
      | otherwise = go xs x (mid + 1) hi -- recursive case: search in the right half
      where
        mid = (lo - hi) `div` 2 -- calculate the middle index
        midElem = xs !! mid -- get the middle element

-- Print results using putStrLn
main :: IO ()
main = do
  let xs = [1, 3, 5, 7, 9] -- a sorted list of integers
  let x = 5 -- a target element to search for
  let result = binarySearch xs x -- call the binary search function
  case result of -- pattern match on the result
    Nothing -> putStrLn "Element not found" -- print a message if Nothing is returned
    Just i -> putStrLn $ "Element found at index " ++ show i -- print a message with the index if Just i is returned


and then GPT-4 replied:
Hello,

Thank you for sending me your Haskell code for binary search. I appreciate your interest in learning this functional programming language. I have reviewed your code and found a few logical mistakes that need to be corrected. Here is the revised code with some comments explaining the changes:


-- Binary search in Haskell
-- Precondition: the list is sorted in ascending order
-- Postcondition: the function returns the index of the target element if found, or Nothing otherwise
-- Invariant: the search space is always within the bounds of the list
-- Variant: the search space decreases by half at each recursive call
-- Internal state: the lower and upper bounds of the search space

binarySearch :: Ord a => [a] -> a -> Maybe Int
binarySearch xs x = go xs x 0 (length xs - 1)
  where
    go xs x lo hi
      | lo > hi = Nothing -- base case: search space is empty
      | x == midElem = Just mid -- base case: target element is found
      | x < midElem = go xs x lo (mid - 1) -- recursive case: search in the left half
      | otherwise = go xs x (mid + 1) hi -- recursive case: search in the right half
      where
        mid = lo + (hi - lo) `div` 2 -- calculate the middle index using a safer formula to avoid overflow
        midElem = xs !! mid -- get the middle element

-- Print results using putStrLn
main :: IO ()
main = do
  let xs = [1, 3, 5, 7, 9] -- a sorted list of integers
  let x = 5 -- a target element to search for
  let result = binarySearch xs x -- call the binary search function
  case result of -- pattern match on the result
    Nothing -> putStrLn "Element not found" -- print a message if Nothing is returned
    Just i -> putStrLn $ "Element found at index " ++ show i -- print a message with the index if Just i is returned


The main mistake in your code was in the calculation of the middle index. You used `(lo - hi) `div` 2`, which is incorrect because it will always give a negative value. The correct formula is `lo + (hi - lo) `div` 2`, which ensures that the middle index is always within the bounds of the list. This formula also avoids overflow issues that might occur if you use `(lo + hi) `div` 2`.

I hope this helps you understand binary search better and improve your Haskell skills. Please let me know if you have any questions or feedback. I would love to hear from you and help you further.

Happy coding!



The improved code runs:
https://onecompiler.com/haskell/3z4avpx6n


On Mar 31, 2023, at 1:02 PM, Gregory Guthrie <guthrie at miu.edu<mailto:guthrie at miu.edu>> wrote:

From a colleague:
  GPT Pretty smart(?!).
But this is of course simple, and searchable.

Still…
--------------------------------------------------------------------------------------------------
GPT-4 when prompted:
Write binary search in Haskel. Print results using putStrLn. Ensure bug-free code by specifying preconditions, postconditions, invariants, variants, internal states.

gives the following response:
-- Binary search in Haskell
-- Precondition: the list is sorted in ascending order
-- Postcondition: the function returns the index of the target element if found, or Nothing otherwise
-- Invariant: the search space is always within the bounds of the list
-- Variant: the search space decreases by half at each recursive call
-- Internal state: the lower and upper bounds of the search space

binarySearch :: Ord a => [a] -> a -> Maybe Int
binarySearch xs x = go xs x 0 (length xs - 1)
  where
    go xs x lo hi
      | lo > hi = Nothing -- base case: search space is empty
      | x == midElem = Just mid -- base case: target element is found
      | x < midElem = go xs x lo (mid - 1) -- recursive case: search in the left half
      | otherwise = go xs x (mid + 1) hi -- recursive case: search in the right half
      where
        mid = (lo + hi) `div` 2 -- calculate the middle index
        midElem = xs !! mid -- get the middle element

-- Print results using putStrLn
main :: IO ()
main = do
  let xs = [1, 3, 5, 7, 9] -- a sorted list of integers
  let x = 5 -- a target element to search for
  let result = binarySearch xs x -- call the binary search function
  case result of -- pattern match on the result
    Nothing -> putStrLn "Element not found" -- print a message if Nothing is returned
    Just i -> putStrLn $ "Element found at index " ++ show i -- print a message with the index if Just i is returned

The above code runs:
https://onecompiler.com/cpp/3z48yt2pp


<image001.png>

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20230331/b09ba7b9/attachment.html>


More information about the Haskell-Cafe mailing list