packages feed

cap-1.0: src/Language/Cap/Debug/Algorithmic.hs

module Language.Cap.Debug.Algorithmic (ADT(..),debug) where

import qualified Language.Cap.Interpret.Pretty as Pretty
import           Language.Cap.Debug.Trace

import Data.Char
import Data.List

{- | An algorithmic debugging tree, currently may only be the EDT, but can be
     extended to support any tree based on different functions for building.
-}
data ADT = Node NodeName [ADT]

{- | Runs an algorithmic debugging session.  Splits answers into boolean values
     and asks appropriate questions.
-}
debug :: Graph -> (Graph -> ADT) -> (Graph -> NodeName -> String) -> IO ()
debug g buildTree question = questions g [buildTree g] question Nothing

-- | Convert a yes or no answer into a boolean value.
toBool :: String -> Bool
toBool n
  | n `isPrefixOf` "yes" = True
  | n `isPrefixOf` "no"  = False

-- | The list of questions asked about a tree based on an input list of answers.
questions :: Graph
          -> [ADT]
          -> (Graph -> NodeName -> String)
          -> Maybe NodeName
          -> IO ()
questions g ((Node name children):r) question b =
  do putStrLn $ question g name
     x <- getLine
     if ":q" `isPrefixOf` (map toLower x)
       then putStrLn "Debugging aborted"
       else
         if toBool (map toLower x)
           then questions g r question b
           else questions g children question (Just name)
questions g [] question b =
  case b of
    Nothing -> putStrLn "No bugs"
    Just b' -> putStrLn ("Bug identified in node: " ++ reverse b' ++ "\nReduction: " ++ Pretty.pretty (nodeContents g b')  ++ " ~> " ++ Pretty.pretty (nodeContents g ('r':b')))