mirror of
https://gitlab.com/sheaf/metabrush.git
synced 2024-11-06 07:13:37 +00:00
24 lines
593 B
Haskell
24 lines
593 B
Haskell
|
{-# LANGUAGE TemplateHaskell #-}
|
||
|
|
||
|
module TH.Utils where
|
||
|
|
||
|
-- template-haskell
|
||
|
import Language.Haskell.TH
|
||
|
( CodeQ )
|
||
|
|
||
|
-- MetaBrush
|
||
|
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 ) ||]
|