{-# LANGUAGE TypeFamilies, FlexibleContexts, CPP #-}
module Math.FreeModule.PrettyPrint where

--------------------------------------------------------------------------------

import Math.FreeModule.Class

--------------------------------------------------------------------------------

bracket :: (a -> String) -> a -> String
bracket f x = "(" ++ f x ++ ")"

-- | Print stuff with real (eg integral or rational) coefficients
prettyPrintRealWith 
  :: (FreeModule x, Real (Coeff x), Show (Coeff x)) 
  => (Base x -> String) -> x -> String
prettyPrintRealWith showBase x = s where
  y = toList x
  s = if isZero x 
    then "0"
    else if take 3 t == " + " then drop 3 t else t
  t = concatMap h y
  h (b,c) = (if c<0 then " - " else " + ") ++ show (abs c) ++ t
    where t = case showBase b of
            "" -> "" 
            xs -> "*" ++ xs

prettyPrintRealWith'
  :: (FreeModule x, Real (Coeff x), Show (Coeff x)) 
  => (Coeff x -> String) -> (Base x -> String) -> x -> String
prettyPrintRealWith' showCoeff showBase x = s where
  y = toList x
  s = if isZero x 
    then "0"
    else if take 3 t == " + " then drop 3 t else t
  t = concatMap h y
  h (b,c) = (if c<0 then " - " else " + ") ++ showCoeff (abs c) ++ t
    where t = case showBase b of
            "" -> "" 
            xs -> "*" ++ xs
               
-- | Print stuff with arbitrary coefficients
prettyPrintArbWith 
  :: (FreeModule x) 
  => (Base x -> String) -> (Coeff x -> String) -> x -> String
prettyPrintArbWith showBase showCoeff x = s where
  y = toList x
  s = if isZero x 
    then "0"
    else drop 3 t
  t = concatMap h y
  h (b,c) = " + " ++ showCoeff c ++ "*" ++ showBase b

--------------------------------------------------------------------------------