packages feed

cap-1.0: src/Language/Cap/Interpret/Pretty.hs

module Language.Cap.Interpret.Pretty
       (PrettyTerm(..),Mapping(..),pretty)
       where

-- | Defines terms that can be displayed by the debugger (ready for pretty
--   printing.
data PrettyTerm = PAtom String
                | PApplication PrettyTerm PrettyTerm
                | PMap [Mapping]

-- | Defines a mapping ready for display by the pretty printer.
data Mapping = M [PrettyTerm] PrettyTerm

-- | Pretty prints a given pretty term
pretty :: PrettyTerm -> String
pretty = pretty' False

pretty' :: Bool -> PrettyTerm -> String
pretty' _ (PAtom a) = a
pretty' True (PApplication f a) =
  "(" ++ (pretty' False f) ++ " " ++ (pretty' True a) ++ ")"
pretty' False (PApplication f a) =
  (pretty' False f) ++ " " ++ (pretty' True a)
pretty' _ (PMap ms) =
  "{" ++ commas (map prettyMap ms) ++ "}"

prettyMap :: Mapping -> String
prettyMap (M as r) = unwords (map pretty as) ++ " -> " ++ pretty r

commas [] = []
commas [x] = x
commas (x:xs) = x ++ ", " ++ commas xs