{-# LANGUAGE DataKinds #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}

module Data.Registry.Internal.TH where

import Control.Monad.Fail (fail)
import Data.Registry.Internal.Hedgehog
import Data.Text (splitOn)
import Hedgehog
import Language.Haskell.TH
import Language.Haskell.TH.Syntax
import Protolude hiding (Type)
import Prelude (last)

-- | Create a generator for selecting between constructors of an ADT
--   One parameter is a Gen Chooser in order to be able to later on
--   switch the selection strategy
makeSelectGenerator :: Name -> [Con] -> ExpQ
makeSelectGenerator :: Name -> [Con] -> ExpQ
makeSelectGenerator Name
name [Con]
constructors = do
  Pat
chooserParam <- [p|(chooser :: Gen Chooser)|]
  [Pat]
otherParams <- forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (Name -> Con -> Q Pat
parameterFor Name
name) [Con]
constructors
  [Exp]
untaggedGenerators <- forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Con -> ExpQ
untagGenerator [Con]
constructors
  Exp
expression <- forall (m :: * -> *). Quote m => m Exp -> m Exp -> m Exp
appE (forall (m :: * -> *). Quote m => m Exp -> m Exp -> m Exp
appE (forall (m :: * -> *). Quote m => Name -> m Exp
varE (String -> Name
mkName String
"chooseOne")) (forall (m :: * -> *). Quote m => Name -> m Exp
varE (String -> Name
mkName String
"chooser"))) (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ [Exp] -> Exp
ListE [Exp]
untaggedGenerators)
  forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ [Pat] -> Exp -> Exp
LamE (Pat
chooserParam forall a. a -> [a] -> [a]
: [Pat]
otherParams) Exp
expression
  where
    parameterFor :: Name -> Con -> Q Pat
    parameterFor :: Name -> Con -> Q Pat
parameterFor Name
typeName Con
constructor = do
      Name
constructorParam <- Con -> Q Name
constructorParameterName Con
constructor
      Name
constructorTag <- Con -> Q Name
tagName Con
constructor
      forall (m :: * -> *). Quote m => m Pat -> m Type -> m Pat
sigP (forall (m :: * -> *). Quote m => Name -> m Pat
varP Name
constructorParam) (forall (m :: * -> *). Quote m => m Type -> m Type -> m Type
appT (forall (m :: * -> *). Quote m => Name -> m Type
conT (String -> Name
mkName String
"Gen")) (forall (m :: * -> *). Quote m => m Type -> m Type -> m Type
appT (forall (m :: * -> *). Quote m => m Type -> m Type -> m Type
appT (forall (m :: * -> *). Quote m => Name -> m Type
conT (String -> Name
mkName String
"Tag")) (forall (m :: * -> *). Quote m => m TyLit -> m Type
litT (forall (m :: * -> *). Quote m => String -> m TyLit
strTyLit (forall a b. (Show a, StringConv String b) => a -> b
show Name
constructorTag)))) (forall (m :: * -> *). Quote m => Name -> m Type
conT Name
typeName)))

-- Create a generator expression for a specific constructor of a data type
-- runQ [|tag @"permanent" Permanent|]
-- AppE (AppTypeE (VarE Data.Registry.Lift.tag) (LitT (StrTyLit "permanent"))) (ConE Test.Data.Registry.Generators.Permanent)
makeConstructorGenerator :: Con -> ExpQ
makeConstructorGenerator :: Con -> ExpQ
makeConstructorGenerator Con
constructor = do
  Name
constructorTag <- Con -> Q Name
tagName Con
constructor
  Name
constructorType <- Con -> Q Name
nameOf Con
constructor
  forall (m :: * -> *). Quote m => m Exp -> m Exp -> m Exp
appE (forall (m :: * -> *). Quote m => m Exp -> m Type -> m Exp
appTypeE (forall (m :: * -> *). Quote m => Name -> m Exp
varE (String -> Name
mkName String
"tag")) (forall (m :: * -> *). Quote m => m TyLit -> m Type
litT (forall (m :: * -> *). Quote m => String -> m TyLit
strTyLit (forall a b. (Show a, StringConv String b) => a -> b
show Name
constructorTag)))) (forall (m :: * -> *). Quote m => Name -> m Exp
conE Name
constructorType)

-- | Remove the tag of a given constructor: fmap unTag g :: Gen (Tag "t" SomeType) -> Gen SomeType
untagGenerator :: Con -> ExpQ
untagGenerator :: Con -> ExpQ
untagGenerator Con
constructor = do
  Name
constructorParam <- Con -> Q Name
constructorParameterName Con
constructor
  forall (m :: * -> *). Quote m => m Exp -> m Exp -> m Exp
appE (forall (m :: * -> *). Quote m => m Exp -> m Exp -> m Exp
appE (forall (m :: * -> *). Quote m => Name -> m Exp
varE (String -> Name
mkName String
"fmap")) (forall (m :: * -> *). Quote m => Name -> m Exp
varE (String -> Name
mkName String
"unTag"))) (forall (m :: * -> *). Quote m => Name -> m Exp
varE Name
constructorParam)

-- | Create a tag used to distinguish constructors in an ADT
tagName :: Con -> Q Name
tagName :: Con -> Q Name
tagName Con
constructor = do
  Text
name <- Con -> Q Text
constructorName Con
constructor
  forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ String -> Name
mkName forall a b. (a -> b) -> a -> b
$ forall a b. ConvertText a b => a -> b
toS Text
name

-- | Same as the tag name but lower cased
constructorParameterName :: Con -> Q Name
constructorParameterName :: Con -> Q Name
constructorParameterName Con
constructor = do
  Text
name <- Con -> Q Text
constructorName Con
constructor
  forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ String -> Name
mkName (Char -> Char
toLower forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a b. ConvertText a b => a -> b
toS Text
name)

-- | Extract the last name of a constructor
constructorName :: Con -> Q Text
constructorName :: Con -> Q Text
constructorName Con
constructor = do
  Name
n <- Con -> Q Name
nameOf Con
constructor
  forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. [a] -> a
last forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> [Text]
splitOn Text
"." forall a b. (a -> b) -> a -> b
$ forall a b. (Show a, StringConv String b) => a -> b
show Name
n

-- | The name of a given constructor
nameOf :: Con -> Q Name
nameOf :: Con -> Q Name
nameOf (NormalC Name
n [BangType]
_) = forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
nameOf (RecC Name
n [VarBangType]
_) = forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
nameOf Con
other = do
  forall (m :: * -> *). Quasi m => Bool -> String -> m ()
qReport Bool
True (String
"we can only create generators for normal constructors and records, got: " forall a. Semigroup a => a -> a -> a
<> forall a b. (Show a, StringConv String b) => a -> b
show Con
other)
  forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"generators creation failed"

-- | The list of types necessary to create a given constructor
typesOf :: Con -> Q [Type]
typesOf :: Con -> Q [Type]
typesOf (NormalC Name
_ [BangType]
types) = forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [BangType]
types)
typesOf (RecC Name
_ [VarBangType]
types) = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ (\(Name
_, Bang
_, Type
t) -> Type
t) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [VarBangType]
types
typesOf Con
other = do
  forall (m :: * -> *). Quasi m => Bool -> String -> m ()
qReport Bool
True (String
"we can only create generators for normal constructors and records, got: " forall a. Semigroup a => a -> a -> a
<> forall a b. (Show a, StringConv String b) => a -> b
show Con
other)
  forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"generators creation failed"

-- | runQ [| fun g +: genFun e +: genFun f|]
--   InfixE (Just (AppE (VarE Data.Registry.Registry.fun) (UnboundVarE g))) (VarE +:) (Just (InfixE (Just (AppE (VarE Data.Registry.Hedgehog.genFun) (UnboundVarE e)))
--  (VarE Data.Registry.Registry.+:) (Just (AppE (VarE Data.Registry.Hedgehog.genFun) (UnboundVarE f)))))
assembleGeneratorsToRegistry :: Exp -> [Exp] -> ExpQ
assembleGeneratorsToRegistry :: Exp -> [Exp] -> ExpQ
assembleGeneratorsToRegistry Exp
selectorGenerator [Exp]
gens =
  ExpQ -> ExpQ -> ExpQ
app (ExpQ -> ExpQ
funOf (forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
selectorGenerator)) forall a b. (a -> b) -> a -> b
$
    ExpQ -> ExpQ -> ExpQ
app (ExpQ -> ExpQ
genFunOf (forall (m :: * -> *). Quote m => Name -> m Exp
varE (String -> Name
mkName String
"choiceChooser"))) forall a b. (a -> b) -> a -> b
$
      [Exp] -> ExpQ
go [Exp]
gens
  where
    go :: [Exp] -> ExpQ
go [] = forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"generators creation failed"
    go [Exp
g] = ExpQ -> ExpQ
genFunOf (forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
g)
    go (Exp
g:[Exp]
gs) =
      ExpQ -> ExpQ -> ExpQ
app (ExpQ -> ExpQ
genFunOf forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
g) ([Exp] -> ExpQ
go [Exp]
gs)



app :: ExpQ -> ExpQ -> ExpQ
app :: ExpQ -> ExpQ -> ExpQ
app ExpQ
e1 ExpQ
e2 =
  forall (m :: * -> *).
Quote m =>
Maybe (m Exp) -> m Exp -> Maybe (m Exp) -> m Exp
infixE (forall a. a -> Maybe a
Just ExpQ
e1) (forall (m :: * -> *). Quote m => Name -> m Exp
varE (String -> Name
mkName String
"<+")) (forall a. a -> Maybe a
Just ExpQ
e2)

genFunOf :: ExpQ -> ExpQ
genFunOf :: ExpQ -> ExpQ
genFunOf = forall (m :: * -> *). Quote m => m Exp -> m Exp -> m Exp
appE (forall (m :: * -> *). Quote m => Name -> m Exp
varE (String -> Name
mkName String
"genFun"))

funOf :: ExpQ -> ExpQ
funOf :: ExpQ -> ExpQ
funOf = forall (m :: * -> *). Quote m => m Exp -> m Exp -> m Exp
appE (forall (m :: * -> *). Quote m => Name -> m Exp
varE (String -> Name
mkName String
"fun"))