1

Hello i want to ask fellow haskellers how can you debug haskell programs efficiently if you have a long chain of pure methods.E.g:

do
  a<-impure
  (pure a)  # where pure calls pure1->pure2->pure2 

How can i debug anything deep in the call stack if everything is not IO ,therefore i can't print or writeFile anywhere without significant effort ?
Do haskell fellows just change all signatures to IO something to see where the code crashes?

Concrete example

Main

module Main where 
    import Data.Text(Text,pack,unpack)
    import Data.Text.IO(readFile,writeFile)
    import TCPFile(TCPFile)
    import TCPEncoding(toText,fromText)
    main::IO()
    main=do
        dat<-readTcpFile "test.txt"
        print dat
        writeTcpFile "output.txt" dat

    writeTcpFile::FilePath->TCPFile->IO()
    writeTcpFile path file=Data.Text.IO.writeFile path (toText file)

    readTcpFile::FilePath->IO TCPFile
    readTcpFile path =fromText <$> Data.Text.IO.readFile path 


module TCPEncoding where 
import Data.Text
class TextEncode a where
    toText::a-> Text
    fromText::Text-> a

I will not write the whole hierarchy of methods.I just want to know how can i run the program until a certain line and get the variable values and the stack frame.

In the case of this program all i get at the end written in my file with writeTcpFileis:

main: <<loop>>

So how can i debug the fromText method used inside readTcpFile and its internals?

Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152

1 Answers1

1

I tend to use trace as previously mentioned

trace :: String -> a -> a so it take a String and the value and returns the same value like id. It will be triggered at the point you want to use it, so for you:

main = do
  dat <- readTcpFile "test.txt"
  _ <- trace "write what you want to see in console" dat
  writeTcpFile "output.txt" dat

if you want to trace the value of dat you have to make sure to convert it to a String. Also inside the do block it's been bound to _ so it doesn't do any side effects.

cmdv
  • 1,693
  • 3
  • 15
  • 23