{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeOperators   #-}
--------------------------------------------------------------------------------
-- |
-- Module      :  Data.Comp.Multi.Derive.Show
-- Copyright   :  (c) 2011 Patrick Bahr
-- License     :  BSD3
-- Maintainer  :  Patrick Bahr <paba@diku.dk>
-- Stability   :  experimental
-- Portability :  non-portable (GHC Extensions)
--
-- Automatically derive instances of @ShowHF@.
--
--------------------------------------------------------------------------------

module Data.Comp.Multi.Derive.Show
    (
     ShowHF(..),
     KShow(..),
     makeShowHF
    ) where

import Data.Comp.Derive.Utils
import Data.Comp.Multi.Algebra
import Data.Comp.Multi.HFunctor
import Language.Haskell.TH

{-| Signature printing. An instance @ShowHF f@ gives rise to an instance
  @KShow (HTerm f)@. -}
class ShowHF f where
    showHF :: Alg f (K String)
    showHF = forall a i. a -> K a i
K forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: (* -> *) -> * -> *).
ShowHF f =>
f (K String) :=> String
showHF'
    showHF' :: f (K String) :=> String
    showHF' = forall a i. K a i -> a
unK forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: (* -> *) -> * -> *). ShowHF f => Alg f (K String)
showHF

class KShow a where
    kshow :: a i -> K String i

showConstr :: String -> [String] -> String
showConstr :: String -> [String] -> String
showConstr String
con [] = String
con
showConstr String
con [String]
args = String
"(" forall a. [a] -> [a] -> [a]
++ String
con forall a. [a] -> [a] -> [a]
++ String
" " forall a. [a] -> [a] -> [a]
++ [String] -> String
unwords [String]
args forall a. [a] -> [a] -> [a]
++ String
")"

{-| Derive an instance of 'ShowHF' for a type constructor of any higher-order
  kind taking at least two arguments. -}
makeShowHF :: Name -> Q [Dec]
makeShowHF :: Name -> Q [Dec]
makeShowHF Name
fname = do
  Just (DataInfo Cxt
_cxt Name
name [TyVarBndr flag]
args [Con]
constrs [DerivClause]
_deriving) <- Q Info -> Q (Maybe DataInfo)
abstractNewtypeQ forall a b. (a -> b) -> a -> b
$ Name -> Q Info
reify Name
fname
  let args' :: [TyVarBndr flag]
args' = forall a. [a] -> [a]
init [TyVarBndr flag]
args
      fArg :: Type
fArg = Name -> Type
VarT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {flag}. TyVarBndr flag -> Name
tyVarBndrName forall a b. (a -> b) -> a -> b
$ forall a. [a] -> a
last [TyVarBndr flag]
args'
      argNames :: Cxt
argNames = forall a b. (a -> b) -> [a] -> [b]
map (Name -> Type
VarT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {flag}. TyVarBndr flag -> Name
tyVarBndrName) (forall a. [a] -> [a]
init [TyVarBndr flag]
args')
      complType :: Type
complType = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl Type -> Type -> Type
AppT (Name -> Type
ConT Name
name) Cxt
argNames
      preCond :: Cxt
preCond = forall a b. (a -> b) -> [a] -> [b]
map (Name -> Cxt -> Type
mkClassP ''Show forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. a -> [a] -> [a]
: [])) Cxt
argNames
      classType :: Type
classType = Type -> Type -> Type
AppT (Name -> Type
ConT ''ShowHF) Type
complType
  [(Name, Cxt, Maybe Type)]
constrs' <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Con -> Q (Name, Cxt, Maybe Type)
normalConExp [Con]
constrs
  Dec
showFDecl <- forall (m :: * -> *). Quote m => Name -> [m Clause] -> m Dec
funD 'showHF (Type -> [(Name, Cxt, Maybe Type)] -> [Q Clause]
showFClauses Type
fArg [(Name, Cxt, Maybe Type)]
constrs')
  forall (m :: * -> *) a. Monad m => a -> m a
return [Cxt -> Type -> [Dec] -> Dec
mkInstanceD Cxt
preCond Type
classType [Dec
showFDecl]]
      where showFClauses :: Type -> [(Name, Cxt, Maybe Type)] -> [Q Clause]
showFClauses Type
fArg = forall a b. (a -> b) -> [a] -> [b]
map (Type -> (Name, Cxt, Maybe Type) -> Q Clause
genShowFClause Type
fArg)
            filterFarg :: Type -> Type -> Name -> (Bool, m Exp)
filterFarg Type
fArg Type
ty Name
x = (Type -> Type -> Bool
containsType Type
ty Type
fArg, forall (m :: * -> *). Quote m => Name -> m Exp
varE Name
x)
            mkShow :: (Bool, m Exp) -> m Exp
mkShow (Bool
isFArg, m Exp
var)
                | Bool
isFArg = [|unK $var|]
                | Bool
otherwise = [| show $var |]
            genShowFClause :: Type -> (Name, Cxt, Maybe Type) -> Q Clause
genShowFClause Type
fArg (Name
constr, Cxt
args, Maybe Type
ty) = do
              let n :: Int
n = forall (t :: * -> *) a. Foldable t => t a -> Int
length Cxt
args
              [Name]
varNs <- Int -> String -> Q [Name]
newNames Int
n String
"x"
              let pat :: Pat
pat = Name -> Cxt -> [Pat] -> Pat
ConP Name
constr [] forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Name -> Pat
VarP [Name]
varNs
                  allVars :: [(Bool, Q Exp)]
allVars = forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (forall {m :: * -> *}.
Quote m =>
Type -> Type -> Name -> (Bool, m Exp)
filterFarg (Type -> Maybe Type -> Type
getBinaryFArg Type
fArg Maybe Type
ty)) Cxt
args [Name]
varNs
                  shows :: Q Exp
shows = forall (m :: * -> *). Quote m => [m Exp] -> m Exp
listE forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall {m :: * -> *}. Quote m => (Bool, m Exp) -> m Exp
mkShow [(Bool, Q Exp)]
allVars
                  conName :: String
conName = Name -> String
nameBase Name
constr
              Exp
body <- [|K $ showConstr conName $shows|]
              forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ [Pat] -> Body -> [Dec] -> Clause
Clause [Pat
pat] (Exp -> Body
NormalB Exp
body) []