metabrush/brush-strokes/src/lib/TH/Utils.hs

24 lines
597 B
Haskell
Raw Normal View History

2023-01-20 15:34:04 +00:00
{-# LANGUAGE TemplateHaskell #-}
module TH.Utils where
-- template-haskell
import Language.Haskell.TH
( CodeQ )
-- brush-strokes
2023-01-20 15:34:04 +00:00
import Math.Ring ( Ring )
import qualified Math.Ring as Ring
--------------------------------------------------------------------------------
foldQ :: CodeQ ( a -> a -> a ) -> CodeQ a -> [ CodeQ a ] -> CodeQ a
foldQ _ a0 [] = a0
foldQ _ _ [a] = a
foldQ f a0 (a:as) = [|| $$f $$a $$( foldQ f a0 as ) ||]
powQ :: Ring a => CodeQ a -> Word -> CodeQ a
powQ _ 0 = [|| Ring.fromInteger ( 1 :: Integer ) ||]
powQ x 1 = x
powQ x n = [|| $$x Ring.^ ( n :: Word ) ||]