module Clash.Class.HasDomain.CodeGen
( mkTryDomainTuples
, mkHasDomainTuples
) where
import Language.Haskell.TH.Syntax
import Clash.CPP (maxTupleSize)
import Language.Haskell.TH.Compat (mkTySynInstD)
mkTup :: [Type] -> Type
mkTup names@(length -> n) =
foldl AppT (TupleT n) names
mkTryDomainTupleInstance :: Name -> Name -> Int -> Dec
mkTryDomainTupleInstance tryDomainName mergeName n =
mkTySynInstD tryDomainName [t, tupPat] tupBody
where
bcde = map (VarT . mkName . ("a"++) . show) [1..n-1]
a = VarT (mkName "a0")
t = VarT (mkName "t")
tupBody = ConT mergeName `AppT` t `AppT` a `AppT` (mkTup bcde)
tupPat = mkTup (a : bcde)
mkTryDomainTuples :: Name -> Name -> Q [Dec]
mkTryDomainTuples tryDomainName mergeName =
pure (map (mkTryDomainTupleInstance tryDomainName mergeName) [3..maxTupleSize])
mkHasDomainTupleInstance :: Name -> Name -> Int -> Dec
mkHasDomainTupleInstance hasDomainName mergeName n =
mkTySynInstD hasDomainName [dom, tupPat] merge
where
bcde = map (VarT . mkName . ("a"++) . show) [1..n-1]
a = VarT (mkName "a0")
dom = VarT (mkName "dom")
merge = ConT mergeName `AppT` dom `AppT` a `AppT` mkTup bcde
tupPat = mkTup (a : bcde)
mkHasDomainTuples :: Name -> Name -> Q [Dec]
mkHasDomainTuples hasDomainName mergeName =
pure (map (mkHasDomainTupleInstance hasDomainName mergeName) [3..maxTupleSize])