Effective Haskell: Observation - Chapter 15 - associated data families (B9 - PDF version)

I’m hopeful for someone that could explain what GHCI does to differentiate an IO action returning a value that has a Show instance versus another IO action returning a value that does not.

Is this difference in behavior related to using associated data families ?

Here is the context that brought this question.

Chapter 15, in the section "Associated Data Families.

On p.574, we find the instance definition of ListDirectory.
Note that the associated data family has a deriving clause.

instance ShellCommand ListDirectory where
    data ShellOutput ListDirectory = 
        DirectoryListing 
          { containingDirectory :: FilePath
          , filenamesInListing :: [FilePath]
          } deriving (Show, Eq)

At the ghci prompt, when you run either of these commands, you get some output.
With the first command, you get a list of values. With the second command, you get a DirectoryListing. Make sense, we have derived a Show instance.

λ> directoryListingWithParent <$> runShellCommand (ListDirectory ".")
[ "./app", "./CHANGELOG.md", "./dist-newstyle",  ... ]

λ> runShellCommand (ListDirectory ".")
DirectoryListing
    { containingDirectory = "."
    , filenamesInListing = [ "app" , "CHANGELOG.md", "dist-newstyle", ...  ]
    }


Moving on to the following page, we find the instance definition of Grep.
Note that the associated data family does NOT have a deriving clause

instance ShellCommand Grep where
    newtype ShellOutput Grep =
        ListOfGrepMatches { getListOfGrepMatches :: [GrepMatch] }

At the GHCI prompt, when you run this command, you do NOT get any output
This makes sense since we have no Show instance. However, why not throwing an error ?

λ> runShellCommand (Grep "version" ["./effectiveHaskell.cabal", "./CHANGELOG.md"])

I’m not sure if what I’m about to say is correct. I suspect that GHCI understands that there is no Show instance.

λ> :t runShellCommand (Grep "version" ["./effectiveHaskell.cabal", "./CHANGELOG.md"])
runShellCommandV3 (Grep "version" ["./effectiveHaskell.cabal", "./CHANGELOG.md"])
  :: IO (ShellOutput Grep)
λ> putStrLn . show <$> runShellCommand (Grep "version" ["./effectiveHaskell.cabal", "./CHANGELOG.md"])

    <interactive>:45:12: error: [GHC-39999]
        • No instance for ‘Show (ShellOutput Grep)’
            arising from a use of ‘show’

If you add the deriving clause, the command returns some output.

instance ShellCommand Grep where
    newtype ShellOutput Grep =
        ListOfGrepMatches { getListOfGrepMatches :: [GrepMatch] }
           deriving Show

-- Same type as the above example, which produces not output
λ> :t (runShellCommand (Grep "version" ["./effectiveHaskell.cabal", "./CHANGELOG.md"]))
(runShellCommandV3 (Grep "version" ["./effectiveHaskell.cabal", "./CHANGELOG.md"]))
  :: IO (ShellOutput Grep)


λ> runShellCommand (Grep "version" ["./effectiveHaskell.cabal", "./CHANGELOG.md"])`

    ListOfGrepMatches
        { getListingOfGrepMatches =
            [ GrepMatch
                { grepMatchingFileName = "./effectiveHaskell.cabal"
                , grepMatchingLineNumber = 1
                , grepMatchingLineContents = "cabal-version:      2.4"
                }
   << rest of output truncated >>