Output ---------------------------------------------------------------------- > module Print ( Pretty > ) > where ---------------------------------------------------------------------- ---------------------------------------------------------------------- > import Ratio > > import Pretty > > import Expr ( Prim(..), Expr(..) ) > import Limes ( Extended(..) ) ---------------------------------------------------------------------- Displaying primitive functions ---------------------------------------------------------------------- > instance Pretty Prim where > ppPrec _ f = str (case f of > Sin -> "sin"; Cos -> "cos"; > Tan -> "tan"; Cot -> "cot"; > Exp -> "exp"; Ln -> "ln"; > Log -> "log"; > Arcsin -> "arcsin"; Arccos -> "arccos"; > Arctan -> "arctan"; Arccot -> "arccot"; > Sinh -> "sinh"; Cosh -> "cosh"; > Tanh -> "tanh"; Coth -> "coth"; > Abs -> "abs") ---------------------------------------------------------------------- Displaying Expressions ---------------------------------------------------------------------- > mn r = (numerator r, denominator r) > lithelp d m n | n == 1 && m >= 0 = ppShow m > lithelp d m n | n == 1 = paren (d > 4) [ppShow m] > lithelp d m n | otherwise = paren (d > 3) [ppShow m, str "/", ppShow n] > instance Pretty Expr where > ppPrec _ (Var x) = str x > ppPrec d (Lit r) = let (m,n) = mn r in lithelp d m n > ppPrec d (AppPrim f n e) = paren (d > 9) > [pp f, str (replicate n '\''), char ' ', ppPrec 9 e] > ppPrec d (AppFun f n e) = paren (d > 9) > [str f, str (replicate n '\''), char ' ', ppPrec 9 e] > ppPrec d (Negg e) = paren (d > 4) > [str " -", brk 1, ppPrec 5 e] > ppPrec d (Add e1 e2) = paren (d > 2) > [ppPrec 2 e1, str " +", brk 1, ppPrec 2 e2] > ppPrec d (Sub e1 e2) = paren (d > 2) > [ppPrec 2 e1, str " -", brk 1, ppPrec 3 e2] > ppPrec d (Mul e1 e2) = paren (d > 3) > [ppPrec 3 e1, str " *", brk 1, ppPrec 3 e2] > ppPrec d (Div e1 e2) = paren (d > 3) > [ppPrec 4 e1, str " /", brk 1, ppPrec 4 e2] > ppPrec d (Pow e1 e2) = paren (d > 5) > [ppPrec 6 e1, str "^", ppPrec 5 e2] ---------------------------------------------------------------------- ---------------------------------------------------------------------- > paren True ts = sequ [char '(', block 0 ts, char ')'] > paren False ts = sequ ts ---------------------------------------------------------------------- Displaying Extended Expressions ---------------------------------------------------------------------- > instance Pretty a => Pretty (Extended a) where > ppPrec _ MInfty = str "-oo" > ppPrec d (Proper v) = ppPrec d v > ppPrec _ Infty = str "oo" ----------------------------------------------------------------------