Counting backtracking in LogicT

Is it possible to measure how much work does LogicT do ?

Is it as simple as adding a mutable reference in the underlying monad state and updating it whenever the failure continuation (see runLogicT @ Control.Monad.Logic) returns? Is there a more principled approach? I’m not yet clear on how do state and backtracking play together.

Thanks for all pointers!

1 Like

The failure continuation of runLogicT will be called just once, so that won’t help you count backtracking.

One approach is to define a monad on top of LogicT, such as a newtype around LogicT (State Int), which makes empty :: m a count every call.

State as the base monad provides a “global” state that cannot be rolled back by monad transformers you apply on top of it.

5 Likes

So for this approach the newtype would need also a custom MonadLogic instance, correct?

Indeed your approach corresponds with that of @econlon : Backtracking state with LogicT - Eric Conlon . Thanks!

1 Like