module Nominal.NominalShow where
import Data.List
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Set (Set)
import qualified Data.Set as Set
import GHC.Generics
import Nominal.Atom
import Nominal.Nominal
import Nominal.NominalSupport
import Nominal.Bindable
import Nominal.Atomic
class (NominalSupport t) => NominalShow t where
showsPrecSup :: Support -> Int -> t -> ShowS
nominal_showList :: Support -> [t] -> ShowS
nominal_showList sup ts = showString $
"["
++ intercalate "," [ showsPrecSup sup 0 t "" | t <- ts ]
++ "]"
default showsPrecSup :: (Generic t, GNominalShow (Rep t)) => Support -> Int -> t -> ShowS
showsPrecSup sup d x = gshowsPrecSup Pre sup d (from x)
nominal_show :: (NominalShow t) => t -> String
nominal_show t = showsPrecSup (support t) 0 t ""
nominal_showsPrec :: (NominalShow t) => Int -> t -> ShowS
nominal_showsPrec d t = showsPrecSup (support t) d t
basic_showsPrecSup :: (Show t) => Support -> Int -> t -> ShowS
basic_showsPrecSup sup d x = showsPrec d x
instance NominalShow Atom where
showsPrecSup sup d t = showString (atomic_show t)
instance NominalShow Bool where
showsPrecSup = basic_showsPrecSup
instance NominalShow Integer where
showsPrecSup = basic_showsPrecSup
instance NominalShow Int where
showsPrecSup = basic_showsPrecSup
instance NominalShow Char where
showsPrecSup = basic_showsPrecSup
nominal_showList sup ts = shows ts
instance NominalShow Double where
showsPrecSup = basic_showsPrecSup
instance NominalShow Float where
showsPrecSup = basic_showsPrecSup
instance (Show t) => NominalShow (Basic t) where
showsPrecSup = basic_showsPrecSup
instance NominalShow Literal where
showsPrecSup = basic_showsPrecSup
instance NominalShow ()
instance (NominalShow t, NominalShow s) => NominalShow (t,s)
instance (NominalShow t, NominalShow s, NominalShow r) => NominalShow (t,s,r)
instance (NominalShow t, NominalShow s, NominalShow r, NominalShow q) => NominalShow (t,s,r,q)
instance (NominalShow t, NominalShow s, NominalShow r, NominalShow q, NominalShow p) => NominalShow (t,s,r,q,p)
instance (NominalShow t, NominalShow s, NominalShow r, NominalShow q, NominalShow p, NominalShow o) => NominalShow (t,s,r,q,p,o)
instance (NominalShow t, NominalShow s, NominalShow r, NominalShow q, NominalShow p, NominalShow o, NominalShow n) => NominalShow (t,s,r,q,p,o,n)
instance (NominalShow t) => NominalShow [t] where
showsPrecSup sup d ts = nominal_showList sup ts
instance (NominalShow t) => NominalShow (Defer t) where
showsPrecSup sup d t = showsPrecSup sup d (force t)
instance (AtomKind a) => NominalShow (AtomOfKind a) where
showsPrecSup sup d t = showString (atomic_show t)
instance (Bindable a, NominalShow a, NominalShow t) => NominalShow (Bind a t) where
showsPrecSup sup d t =
open_for_printing sup t $ \a s sup' ->
showParen (d > 5) $
showString (nominal_show a ++ " . " ++ showsPrecSup sup' 5 s "")
instance (Ord k, NominalShow k, NominalShow v) => NominalShow (Map k v) where
showsPrecSup sup d m =
showParen (d > 10) $
showString "fromList " ∘ showsPrecSup sup 11 (Map.toList m)
instance (Ord k, NominalShow k) => NominalShow (Set k) where
showsPrecSup sup d s =
showParen (d > 10) $
showString "fromList " ∘ showsPrecSup sup 11 (Set.toList s)
instance (Bindable a, NominalShow a, NominalShow t) => Show (Bind a t) where
showsPrec = nominal_showsPrec
data Separator = Rec | Tup | Inf String | Pre
class GNominalShow f where
gshowsPrecSup :: Separator -> Support -> Int -> f a -> ShowS
isNullary :: f a -> Bool
isNullary x = False
instance GNominalShow V1 where
gshowsPrecSup sep sup d t s = undefined
instance GNominalShow U1 where
gshowsPrecSup sep sup d t s = s
isNullary x = True
instance (GNominalShow a, GNominalShow b) => GNominalShow (a :*: b) where
gshowsPrecSup sep sup d (x :*: y) =
gshowsPrecSup sep sup prec x
∘ showString separator
∘ gshowsPrecSup sep sup prec y
where
(separator, prec) = case sep of
Rec -> (", ", d)
Tup -> (",", d)
Inf s -> (" " ++ s ++ " ", d)
Pre -> (" ", 11)
instance (GNominalShow a, GNominalShow b) => GNominalShow (a :+: b) where
gshowsPrecSup sep sup d (L1 x) = gshowsPrecSup sep sup d x
gshowsPrecSup sep sup d (R1 x) = gshowsPrecSup sep sup d x
instance (GNominalShow a) => GNominalShow (M1 D c a) where
gshowsPrecSup sep sup d (M1 x) = gshowsPrecSup sep sup d x
instance (GNominalShow a, Constructor c) => GNominalShow (M1 C c a) where
gshowsPrecSup sep sup d c@(M1 x) =
case fixity of
Prefix
| isNullary x -> showString (prefix name)
| isTuple name -> showParen True $ gshowsPrecSup Tup sup 0 x
| conIsRecord c -> showParen (d > 10) $
showString (prefix name)
∘ showString " "
∘ showString "{"
∘ gshowsPrecSup Rec sup 0 x
∘ showString "}"
| otherwise -> showParen (d > 10) $
showString (prefix name)
∘ showString " "
∘ gshowsPrecSup Pre sup 11 x
Infix assoc prec -> showParen (d > prec) $
gshowsPrecSup (Inf name) sup (prec+1) x
where
name = conName c
prefix n@(':':s) = "(" ++ n ++ ")"
prefix n = n
fixity = conFixity c
isTuple ('(' : ',' : _) = True
isTuple _ = False
instance (GNominalShow a, Selector c) => GNominalShow (M1 S c a) where
gshowsPrecSup sep sup d s@(M1 x)
| null name = gshowsPrecSup sep sup d x
| otherwise =
showString name
∘ showString " = "
∘ gshowsPrecSup sep sup d x
where
name = selName s
instance (NominalShow a) => GNominalShow (K1 i a) where
gshowsPrecSup sep sup d (K1 x) = showsPrecSup sup d x