debugginghaskellsyntactic-sugarfunction-compositionpointfree

Debug Haskell composition chain without converting to point-full representation


I have learned that point-free style is preferred in the Haskell community, and I often write expressions like this:

naive = (slugifyUnicode . T.take maxFilenameSize . T.pack . stripHtmlTags . T.unpack . plainToHtml) sfld

However, while debugging, I find myself repeatedly converting expressions like this into chains of $ operators in order to use some variant of trace, and I am starting to think it is preferable to forgo the point-free style and just write lots of $s to start with, in order to make debugging less cumbersome. After all, it ends up being about the same number of characters.

Does anyone know of a way to debug long chains of composed functions without de-composing them?

And more generally, any comments on this inconvenience are very welcome.


Solution

  • For any intermediate value that has a Show instance you can just use traceShowId inline:

    naive = (slugifyUnicode . T.take maxFilenameSize . traceShowId . T.pack . stripHtmlTags . T.unpack .plainToHtml) sfld
    

    If the intermediary value is a String you can use traceId instead.

    For anything that doesn't have a Show instance you'd have to define a helper:

    data CustomType = CustomType String
    traceHelper :: CustomType -> CustomType
    traceHelper s@(CustomType c) = trace c $ s
    
    -- arbitrary functions we want to compose
    a :: aIn -> CustomType
    b :: CustomType -> bOut
    
    c :: aIn -> bOut
    c = b . traceHelper . a