{-# LANGUAGE Safe #-}
module Text.Show.Value ( Name, Value(..), hideCon ) where
import Data.Maybe(fromMaybe,isNothing)
type Name = String
data Value = Con Name [Value]
| InfixCons Value [(Name,Value)]
| Rec Name [ (Name,Value) ]
| Tuple [Value]
| List [Value]
| Neg Value
| Ratio Value Value
| Integer String
| Float String
| Char String
| String String
| Date String
| Time String
| Quote String
deriving (Value -> Value -> Bool
(Value -> Value -> Bool) -> (Value -> Value -> Bool) -> Eq Value
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Value -> Value -> Bool
== :: Value -> Value -> Bool
$c/= :: Value -> Value -> Bool
/= :: Value -> Value -> Bool
Eq,Int -> Value -> ShowS
[Value] -> ShowS
Value -> Name
(Int -> Value -> ShowS)
-> (Value -> Name) -> ([Value] -> ShowS) -> Show Value
forall a.
(Int -> a -> ShowS) -> (a -> Name) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Value -> ShowS
showsPrec :: Int -> Value -> ShowS
$cshow :: Value -> Name
show :: Value -> Name
$cshowList :: [Value] -> ShowS
showList :: [Value] -> ShowS
Show)
hideCon :: Bool -> (Name -> Bool) -> Value -> Value
hideCon :: Bool -> (Name -> Bool) -> Value -> Value
hideCon Bool
collapse Name -> Bool
hidden = Maybe Value -> Value
toVal (Maybe Value -> Value) -> (Value -> Maybe Value) -> Value -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> Maybe Value
delMaybe
where
hiddenV :: Value
hiddenV = Name -> [Value] -> Value
Con Name
"_" []
toVal :: Maybe Value -> Value
toVal = Value -> Maybe Value -> Value
forall a. a -> Maybe a -> a
fromMaybe Value
hiddenV
delMany :: [Value] -> Maybe [Value]
delMany [Value]
vals
| Bool
collapse Bool -> Bool -> Bool
&& (Maybe Value -> Bool) -> [Maybe Value] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Maybe Value -> Bool
forall a. Maybe a -> Bool
isNothing [Maybe Value]
newVals = Maybe [Value]
forall a. Maybe a
Nothing
| Bool
otherwise = [Value] -> Maybe [Value]
forall a. a -> Maybe a
Just ((Maybe Value -> Value) -> [Maybe Value] -> [Value]
forall a b. (a -> b) -> [a] -> [b]
map Maybe Value -> Value
toVal [Maybe Value]
newVals)
where
newVals :: [Maybe Value]
newVals = (Value -> Maybe Value) -> [Value] -> [Maybe Value]
forall a b. (a -> b) -> [a] -> [b]
map Value -> Maybe Value
delMaybe [Value]
vals
delMaybe :: Value -> Maybe Value
delMaybe Value
val =
case Value
val of
Con Name
x [Value]
vs
| Name -> Bool
hidden Name
x -> Maybe Value
forall a. Maybe a
Nothing
| [Value] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Value]
vs -> Value -> Maybe Value
forall a. a -> Maybe a
Just Value
val
| Bool
otherwise -> Name -> [Value] -> Value
Con Name
x ([Value] -> Value) -> Maybe [Value] -> Maybe Value
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` [Value] -> Maybe [Value]
delMany [Value]
vs
Rec Name
x [(Name, Value)]
fs
| Name -> Bool
hidden Name
x -> Maybe Value
forall a. Maybe a
Nothing
| [(Name, Value)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Name, Value)]
fs -> Value -> Maybe Value
forall a. a -> Maybe a
Just Value
val
| Bool
collapse Bool -> Bool -> Bool
&& (Maybe Value -> Bool) -> [Maybe Value] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Maybe Value -> Bool
forall a. Maybe a -> Bool
isNothing [Maybe Value]
mbs -> Maybe Value
forall a. Maybe a
Nothing
| Bool
otherwise -> Value -> Maybe Value
forall a. a -> Maybe a
Just (Name -> [(Name, Value)] -> Value
Rec Name
x [ (Name
f,Value
v) | (Name
f,Just Value
v) <- [Name] -> [Maybe Value] -> [(Name, Maybe Value)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Name]
ls [Maybe Value]
mbs ])
where ([Name]
ls,[Value]
vs) = [(Name, Value)] -> ([Name], [Value])
forall a b. [(a, b)] -> ([a], [b])
unzip [(Name, Value)]
fs
mbs :: [Maybe Value]
mbs = (Value -> Maybe Value) -> [Value] -> [Maybe Value]
forall a b. (a -> b) -> [a] -> [b]
map Value -> Maybe Value
delMaybe [Value]
vs
InfixCons Value
v [(Name, Value)]
ys
| (Name -> Bool) -> [Name] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Name -> Bool
hidden [Name]
cs -> Maybe Value
forall a. Maybe a
Nothing
| Bool
otherwise -> do ~(Value
v1:[Value]
vs1) <- [Value] -> Maybe [Value]
delMany (Value
vValue -> [Value] -> [Value]
forall a. a -> [a] -> [a]
:[Value]
vs)
Value -> Maybe Value
forall a. a -> Maybe a
Just (Value -> [(Name, Value)] -> Value
InfixCons Value
v1 ([Name] -> [Value] -> [(Name, Value)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Name]
cs [Value]
vs1))
where ([Name]
cs,[Value]
vs) = [(Name, Value)] -> ([Name], [Value])
forall a b. [(a, b)] -> ([a], [b])
unzip [(Name, Value)]
ys
Tuple [Value]
vs | [Value] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Value]
vs -> Value -> Maybe Value
forall a. a -> Maybe a
Just Value
val
| Bool
otherwise -> [Value] -> Value
Tuple ([Value] -> Value) -> Maybe [Value] -> Maybe Value
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` [Value] -> Maybe [Value]
delMany [Value]
vs
List [Value]
vs | [Value] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Value]
vs -> Value -> Maybe Value
forall a. a -> Maybe a
Just Value
val
| Bool
otherwise -> [Value] -> Value
List ([Value] -> Value) -> Maybe [Value] -> Maybe Value
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` [Value] -> Maybe [Value]
delMany [Value]
vs
Neg Value
v -> Value -> Value
Neg (Value -> Value) -> Maybe Value -> Maybe Value
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Value -> Maybe Value
delMaybe Value
v
Ratio Value
v1 Value
v2 -> do ~[Value
a,Value
b] <- [Value] -> Maybe [Value]
delMany [Value
v1,Value
v2]
Value -> Maybe Value
forall a. a -> Maybe a
Just (Value -> Value -> Value
Ratio Value
a Value
b)
Integer {} -> Value -> Maybe Value
forall a. a -> Maybe a
Just Value
val
Float {} -> Value -> Maybe Value
forall a. a -> Maybe a
Just Value
val
Char {} -> Value -> Maybe Value
forall a. a -> Maybe a
Just Value
val
String {} -> Value -> Maybe Value
forall a. a -> Maybe a
Just Value
val
Date {} -> Value -> Maybe Value
forall a. a -> Maybe a
Just Value
val
Time {} -> Value -> Maybe Value
forall a. a -> Maybe a
Just Value
val
Quote {} -> Value -> Maybe Value
forall a. a -> Maybe a
Just Value
val