packages feed

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

module Language.Cap.Debug.EDT (buildEDT,edtParent,edtQuestion) where

import Language.Cap.Interpret.Pretty
import Language.Cap.Debug.Algorithmic
import Language.Cap.Debug.Trace

{- | Generates a question for a node in the ART.  Questions are made up by
     comparing the redex to a most evaluated form.
-}
edtQuestion :: Graph -> NodeName -> String
edtQuestion g n =
  (pretty $ edtRedex g n) ++ " =?= " ++ (pretty $ edtMef g n)

-- | Constructs an EDT for a given trace.
buildEDT :: Graph -> ADT
buildEDT g =
  buildEDT' g ""
  where
    -- | Constructs an EDT starting at a specific node in the ART.
    buildEDT' :: Graph -> NodeName -> ADT
    buildEDT' g n = Node n (map (buildEDT' g) (children g n))
    
    -- | Gathers the children of a node in the ART.
    children :: Graph -> NodeName -> [NodeName]
    children g n = [name | name <- allNodes g,    ('r':name) `elem` allNodes g
                                               && name /= ""
                                               && edtParent name == Just n]

-- | Find the parent (as defined for the EDT) of a node in the ART.
--   This is a partial function and is undefined for the epsilon node.
edtParent :: NodeName -> Maybe NodeName
edtParent ('f':n) = edtParent n
edtParent ('a':n) = edtParent n
edtParent ('r':n) = Just n
edtParent [] = Nothing

-- | Create a redex for any application or atom node in the trace.
edtRedex :: Graph -> NodeName -> PrettyTerm
edtRedex g n =
  case nodeValue g n of
    Just (Application i j) -> PApplication (edtMef g i) (edtMef g j)
    Just (Atom a)          -> PAtom a

-- | Find the most evaluated form for any node in the trace
edtMef :: Graph -> NodeName -> PrettyTerm
edtMef g n =
  case nodeValue g l of
    Just x -> edtMeft g x
  where
    l = nodeLast g n

edtMeft :: Graph -> NodeInfo -> PrettyTerm
edtMeft g (Atom a) = PAtom a
edtMeft g (Application i j) = PApplication (edtMef g i) (edtMef g j)