2

After about two years on and off with Haskell, I'm still pretty much a beginner. Thinking about what slows me down most is the difficulty to understand data structures and monad stacks that result from using the various libraries needed for a production system. I often find myself in a situation where I would love to simply set a breakpoint somewhere and then inspect bindings and nested data structures at that point. Working with complex frameworks in other languages, this often provides much more insight into data and runtime behavior than studying their source code and (often inadequate) documentation. What would be a similar "explorative" route in Haskell?

Yes, this a rather imperative thinking, with code that pretty much equals control flow, and where bindings are variables with mutable state. Yet, working with complex monad stacks from other libraries, Haskell programs are not that different, I think.

For example: I'm trying to build a Servant application with sessions. In a Servant client, the session information definitively must be maintained somewhere in the stack. Is it Servant-Auth or at a lower level (Warp?) I'd like to know exactly where, and figure out how to access the session information. I'd like to write a test that performs a login, does some requests, and then logs out again.

In Python, Java, etc. I would do the login, break, and poke around in the in-memory data structures to figure out where the information resides; then compare with the documentation to understand how to best use it. In Haskell, I feel stuck wading through library source code and piecing together layers of type signatures, or even construct minimal example programs to understand what is going on. This is certainly ten to fifty times slower than simply inspecting what is already there in memory, but which I can't readily explore.

Thus, I think an "explorative" style to be helpful understanding complex libraries and frameworks, and to become productive quickly. Therefore, I'm looking for recommendations how to best explore Haskell code. Is there a way to use ghci for that, even with a complex application stack? The debugger? Any other suggestions, that provide more insight than sprinkling trace statements all over my code?

Ulrich Schuster
  • 1,486
  • 11
  • 18
  • Concretely in that case: Looking at the application's monad stack should give you a few pointers how it's done. In general I've found that the source code is often the best and easiest way to inspect applications (but then I don't have a lot of experience Java, which has _outstanding_ debugger support). Often, I've found that a good IDE with goto definition support is sufficient. – ThreeFx May 04 '21 at 09:10
  • While I could certainly see reasons to want what you're asking for, they're mostly concerned with low-level optimisation. For learning / exploring, it shouldn't really be _interesting_ what concrete values you have in a given application and where they reside – those are specifics of the particular use case. What's far more interesting is what values there _could potentially be_, and for that you don't so much need to look at source, documentation and certainly not at anything happening at runtime, but at the _types_ (and Haskell's ADTs are much clearer than OO class hierarchies, IMO). – leftaroundabout May 04 '21 at 09:19
  • 1
    ...but, yeah, for the use case you're asking about this is definitely a bit of a different story – it's just a very imperative domain, not really representative of typical Haskell projects. – leftaroundabout May 04 '21 at 09:28
  • Well, I beg to disagree. Being representative is probably limited to a certain domain. I'm looking at web servers and other request/response and interactive applications, where the pattern applies. It's not about the concrete values, but about the structure at runtime, which is often hard to deduct from layers of library functions. Therefore, I am looking for a complementary inductive way. – Ulrich Schuster May 04 '21 at 09:34
  • 1
    Sure. I suppose many would say Haskell is just not the right language for this kind of application. Others might say it should be done in a completely different style from how you'd do in OO languages, more towards FRP. But there certainly are also Haskellers who approach it pragmatically with their monad-trafo stacks, and they probably have found ways to get what you're asking for. – leftaroundabout May 04 '21 at 16:25
  • @UlrichSchuster I agree that Haskell is not as well suited as Java for this kind of exploratory debugging. Both type erasure and the absence of stack traces make it difficult. In the particular case of [servant-auth-server](http://hackage.haskell.org/package/servant-auth-server), it seems that the library doesn't concern itself with maintaining sessions state with JWTs, because they are a [stateless authentication mechanism](https://stackoverflow.com/questions/55881611/why-jwt-is-a-stateless-authentication) (not sure about how the other methods are handled, though). – danidiaz May 04 '21 at 17:41
  • 1
    There’s a debugger in GHCi, it’s probably not exactly what you’re used to, but it’s not THAT weird either. Can you clear up for me where your question is in: knowing the GHCi debugger exists, loading the application code in GHCi, or using the debugger to explore things in a non-strict context? – bisserlis May 06 '21 at 07:33
  • It's the last step, in combination with involved type-level machinery (in my case from Servant). I find it hard at times to understand which types actually result from type-level computations. – Ulrich Schuster May 07 '21 at 07:05
  • @UlrichSchuster I raised a flag on this question following a triage review. The manner in which it is posed invites subjective discussion-orientated and opinion-based answers. In addition, it lacks some focus (i.e. multiple questions and broadly looking for 'other suggestions'). Note: subjective questions are not categorial off-topic though. So, please review [don't ask](https://stackoverflow.com/help/dont-ask) as it relates to "Some subjective questions are allowed" as a means of improving this question and addressing the off-topic flagging reasoning. – Brett Caswell May 20 '21 at 01:08

0 Answers0