Running old code examples today

I’m trying to run a code example from Real World Haskell (published in 2008) on my fairly recent installation of Haskell, using Stack. I can’t get it to compile. The main obstacle so far, is that a function “sendTo” has been deprecated and then removed in the library where it first existed, and use slightly different types where it is now.

The code I’m trying to run is available from https://book.realworldhaskell.org/, in the file syslogclient.hs which is in Chapter 27. Sockets and Syslog.

It’s not clear to me, from the book, which modules various functions come from (such as sendTo), or which packages these modules come from. Hoogle is helpful, but I can’t easily see how it was back in 2008. It seems to me that the network package contains the Network.Socket module, which contained the sendTo function up to version v. 2.8.0.1. There, it’s deprecated, and they tell me to use Network.Socket.ByteString instead.

Now this starts to feel messy in my head. I can’t simply replace the old sendTo with the new one. When I try, I get type errors. I also get various import problems, with symbols being available in more than one place (module).

What would be a good strategy to solve the problem?

Since I’m using Stack, I need to update my package.yaml file with the right packages. I need to import the right modules. Conflicts with the same symbol being available in more than one module must be resolved. The new sendTo function must get an argument of type ByteString, instead of the String argument of the old function. But this is just one singel code example. My question is about a general strategy.

3 Likes

Welcome Helge. You can still hunt down # Version 6.8.3 (released 17 June 2008) and find the as-was sendTo :: Socket -> String -> SockAddr -> IO Int

Admittedly it took me a bit of Hoogle-hinted guesswork the library would be Network.Socket.

Yeah. It would be tedious guessing all the time. The Release Notes back then mostly related to language changes, not libraries. And you’re talking about a time before the FTP/AMP libraries reorg.

You reached Chapter 27, I would say you can read Haskell code and Haskell documentation at speed. If the examples are that much bitrot, maybe just learning the library on your own (with modern tutorials, documentation, etc.) is more efficient.

With cabal you could pin a package, select the appropriate compiler for base and hope everything goes fine from there. The alternative: writing a wrapper around the modern library. Both do not seem to be worth the hassle.

2 Likes

@HelgeStenstrom for completeness, one should also mention GitHub - tssm/up-to-date-real-world-haskell: I'm trying to update the Real World Haskell book .

But yeah I agree with your general sentiment about the discoverability of things. Hoogle is your friend, as well as github code search.

2 Likes

Unfortunately, the code in https://github.com/tssm/up-to-date-real-world-haskell/blob/master/27-sockets-and-syslog.org is exactly as in the book. In particular, the second argument to sendTo is a String, not a ByteString. So this works with old versions of the network package, not with the current one.

1 Like

I can’t honestly say that I have reached chapter 27. I’ve read parts of the book, a long time ago, and forgotten a lot of it. This time, I thought that I would do an exercise that I’ve partly done in Java already (a DNS resolver). For that, I wanted a code example that uses UDP. I thought this chapter would be handy.

Now I’ve code to compile, at least the part that didn’t before. Some functions are still defines as “=undefined”, so I get fewer compiler problems, and can tackle one at a time. I did it by adjusting the imports, and by wrapping the String with “pack theString”, so that I get a ByteString where needed.

2 Likes

Apologies! I didn’t catch that when sharing the link.