-- This file is part of the 'term-rewriting' library. It is licensed
-- under an MIT license. See the accompanying 'LICENSE' file for details.
--
-- Auhtors: Martin Avanzini, Christian Sternagel

module Data.Rewriting.Term.Pretty (
    prettyTerm,
) where

import Data.Rewriting.Term.Type
import Text.PrettyPrint.ANSI.Leijen

-- | Given a pretty printer @f@ for function symbols and pretty printer @v@ for variables
-- @prettyTerm f v@ produces a pretty printer for terms

prettyTerm :: (f -> Doc) -> (v -> Doc) -> Term f v -> Doc
prettyTerm :: forall f v. (f -> Doc) -> (v -> Doc) -> Term f v -> Doc
prettyTerm f -> Doc
_         v -> Doc
var (Var v
x) = v -> Doc
var v
x
prettyTerm f -> Doc
fun v -> Doc
var (Fun f
f [Term f v]
ts)    = f -> Doc
fun f
f Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
args where
    args :: Doc
args = Doc -> Doc -> Doc -> [Doc] -> Doc
encloseSep Doc
lparen Doc
rparen Doc
comma [(f -> Doc) -> (v -> Doc) -> Term f v -> Doc
forall f v. (f -> Doc) -> (v -> Doc) -> Term f v -> Doc
prettyTerm f -> Doc
fun v -> Doc
var Term f v
ti | Term f v
ti <- [Term f v]
ts]

instance (Pretty f, Pretty v) => Pretty (Term f v) where
    pretty :: Term f v -> Doc
pretty = (f -> Doc) -> (v -> Doc) -> Term f v -> Doc
forall f v. (f -> Doc) -> (v -> Doc) -> Term f v -> Doc
prettyTerm f -> Doc
forall a. Pretty a => a -> Doc
pretty v -> Doc
forall a. Pretty a => a -> Doc
pretty