2

This is a data type of lambda calculus and a shifting function. It does not matter what it does, don't look into details, look at the structure.

import Debug.Trace

data Term = Idx Int | Lam Term | Term :@: Term deriving (Read, Show, Eq)
shift :: Int -> Term -> Term
shift val term = go 0 val term

go :: Int -> Int -> Term -> Term

go m k term | trace ("myfun " ++ show m ++ " " ++ show k ++ " " ++ show term) False = undefined

go m k (Idx n) | n < m = Idx n
               | otherwise = Idx (n + k)
go m k (a :@: b) = go m k a :@: go m k b
go m k (Lam a) = Lam $ go (m + 1) k a

It has several pattern-matching blocks by the same type.

What I want to do is to run

shift 5 (Lam (Lam (Idx 0 :@: Idx 1)))

and see, the order of execution with lines. Which go function would be executed first, second and so on.

You can see, I included the tracing function. It's a good trick and I like that I have to include it once, to check the input of every pattern-matching implementation.

However, I want to have line numbers!

There is an answer that have line numbers, but it does not work for single_trace_for_whole_block https://stackoverflow.com/a/7253991/2787507

Maybe there are some Haskell libraries that gives this functionality?

nutella_eater
  • 3,393
  • 3
  • 27
  • 46
  • I suppose it is in principle possible to use template haskell to add traces to each pattern automatically. It's a good idea. I don't know of anything offhand though. – luqui Aug 02 '22 at 01:34
  • 1
    There is the [ghci debugger](https://downloads.haskell.org/ghc/latest/docs/html/users_guide/ghci.html#ghci-debugger)... – Daniel Wagner Aug 02 '22 at 05:31
  • @DanielWagner I was about to suggest the same but looking into it closer: there's really a big hassle to get out of GHCi what OP wants, i.e. a trace of all calls to a function and respective arguments. It seems that's really a point where debugger vs. tracer distinction is relevant. Debugger is good for stopping program at a certain point and observe local context or step forward/back, whereas tracing is about getting a log of particular events all at once, and there may be many. That said, the current OP's approach is limited and I don't think it's easy to get line numbers on top of it. – Artem Pelenitsyn Aug 02 '22 at 14:36
  • I would stick with the debugger. I've read about it, and it's actually pretty easy. – nutella_eater Aug 02 '22 at 16:39

0 Answers0