Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Haskell support debug?

For example this is a function:

iffthen :: [String] -> Parser String
iffthen a = do 
x <- symbol (head a)
               y <- booleana (head (tail a))
               z <- symbol (head (tail (tail a)))
               k <- assignP (head (tail (tail (tail a))))
               l <- symbol (head (tail (tail (tail (tail a)))))
               m <- assignP (head (tail (tail (tail (tail (tail a))))))
               return k

I need to see what really do every instructions

like image 250
Riccardo Monterisi Avatar asked Aug 13 '26 01:08

Riccardo Monterisi


1 Answers

You can use the trace :: String -> a -> a of the Debug.Trace, or related functions like traceShowId :: Show a => a -> a. This function basically prints the attached String parameter in case the function is evaluated, and returns the result of the function.

We can thus - for some content that can be printed - attach such trace functions, and thus print information. Note that debugging in Haskell is typically different from debugging in imperative languages, mainly due to laziness: usually functions are not evaluated, unless we need the result. So that means that some functions will never get evaluated, or these are evaluated long after we constructed that function.

Abut your function, I would advice to use pattern matching here, and remove the noise of unused variables:

iffthen :: [String] -> Parser String
iffthen (ifs : cond : thens : val1 : elses : val2 : _) = do 
    symbol ifs
    booleana cond
    symbol thens
    k <- assignP val1
    symbol elses
    assignP val2
    return k

(Given I interpreted what you want correnctly, and ifs, thens, elses are symbols, cond is the condition, and val1 and val2 are the values of the if-then-else expression).

It is probably even better not to use a list (since the number of elements is not guaranteed at compile time), and thus construct a sperate type with specific parameters.

like image 92
Willem Van Onsem Avatar answered Aug 14 '26 22:08

Willem Van Onsem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!