{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE Trustworthy #-}
{-# OPTIONS_HADDOCK show-extensions #-}
module Clash.Promoted.Nat.TH
(
decLiteralD
, decLiteralsD
)
where
import Language.Haskell.TH
import Clash.Promoted.Nat
decLiteralD :: Integer
-> Q [Dec]
decLiteralD :: Integer -> Q [Dec]
decLiteralD n :: Integer
n = do
let suffix :: String
suffix = if Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< 0 then String -> String
forall a. HasCallStack => String -> a
error ("Can't make negative SNat: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Integer -> String
forall a. Show a => a -> String
show Integer
n) else Integer -> String
forall a. Show a => a -> String
show Integer
n
valName :: Name
valName = String -> Name
mkName (String -> Name) -> String -> Name
forall a b. (a -> b) -> a -> b
$ 'd'Char -> String -> String
forall a. a -> [a] -> [a]
:String
suffix
Dec
sig <- Name -> TypeQ -> DecQ
sigD Name
valName (TypeQ -> TypeQ -> TypeQ
appT (Name -> TypeQ
conT ''SNat) (TyLitQ -> TypeQ
litT (Integer -> TyLitQ
numTyLit Integer
n)))
Dec
val <- PatQ -> BodyQ -> [DecQ] -> DecQ
valD (Name -> PatQ
varP Name
valName) (ExpQ -> BodyQ
normalB [| SNat |]) []
[Dec] -> Q [Dec]
forall (m :: Type -> Type) a. Monad m => a -> m a
return [ Dec
sig, Dec
val ]
decLiteralsD :: Integer
-> Integer
-> Q [Dec]
decLiteralsD :: Integer -> Integer -> Q [Dec]
decLiteralsD from :: Integer
from to :: Integer
to =
([[Dec]] -> [Dec]) -> Q [[Dec]] -> Q [Dec]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap [[Dec]] -> [Dec]
forall (t :: Type -> Type) a. Foldable t => t [a] -> [a]
concat (Q [[Dec]] -> Q [Dec]) -> Q [[Dec]] -> Q [Dec]
forall a b. (a -> b) -> a -> b
$ [Q [Dec]] -> Q [[Dec]]
forall (t :: Type -> Type) (m :: Type -> Type) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence ([Q [Dec]] -> Q [[Dec]]) -> [Q [Dec]] -> Q [[Dec]]
forall a b. (a -> b) -> a -> b
$ [ Integer -> Q [Dec]
decLiteralD Integer
n | Integer
n <- [Integer
from..Integer
to] ]