From 197c61b2234d1048f8fff8d4dc761bacddc98f15 Mon Sep 17 00:00:00 2001 From: IC Rainbow <486682+dpwiz@users.noreply.github.com> Date: Fri, 6 Mar 2026 23:33:10 +0200 Subject: [PATCH 1/2] Extract and generalize makeTypeSynonym from destructible_decl Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- apecs/src/Apecs/TH.hs | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/apecs/src/Apecs/TH.hs b/apecs/src/Apecs/TH.hs index b4674fc..67335f0 100644 --- a/apecs/src/Apecs/TH.hs +++ b/apecs/src/Apecs/TH.hs @@ -8,6 +8,7 @@ module Apecs.TH , makeWorldAndComponents , makeMapComponents , makeMapComponentsFor + , makeTypeSynonym ) where import Control.Monad (filterM) @@ -54,27 +55,30 @@ makeWorldNoEC worldName cTypes = do |] -- Destructible type synonym - destructible_decl <- do - let name = mkName $ worldName ++ "Destructible" - let destructible ty - | nameBase ty == "EntityCounter" = pure False - | otherwise = isInstance ''ExplDestroy [ConT ''IO, AppT (ConT ''Storage) (ConT ty)] - destroyableTypes <- filterM destructible cTypes - let getType ty = ConT ty - tySynD name [] (pure $ mkTupleT $ getType <$> destroyableTypes) - - pure $ data_decl : destructible_decl : concat (init_world : instances) + destructible_decl <- makeTypeSynonym (worldName ++ "Destructible") ''ExplDestroy cTypes + + pure $ data_decl : destructible_decl ++ concat (init_world : instances) where enumerate :: [a] -> [(Int,a)] enumerate = zip [0..] - mkTupleT :: [Type] -> Type - mkTupleT [] = ConT ''() - mkTupleT [t] = t - mkTupleT ts - | len <= 8 = foldl AppT (TupleT len) ts - | otherwise = foldl AppT (TupleT 8) (take 7 ts ++ [mkTupleT (drop 7 ts)]) - where len = length ts +makeTypeSynonym :: String -> Name -> [Name] -> Q [Dec] +makeTypeSynonym synName cls cTypes = do + let destructible ty + | nameBase ty == "EntityCounter" = pure False + | otherwise = isInstance cls [ConT ''IO, AppT (ConT ''Storage) (ConT ty)] + destroyableTypes <- filterM destructible cTypes + let getType ty = ConT ty + decl <- tySynD (mkName synName) [] (pure $ mkTupleT $ getType <$> destroyableTypes) + return [decl] + +mkTupleT :: [Type] -> Type +mkTupleT [] = ConT ''() +mkTupleT [t] = t +mkTupleT ts + | len <= 8 = foldl AppT (TupleT len) ts + | otherwise = foldl AppT (TupleT 8) (take 7 ts ++ [mkTupleT (drop 7 ts)]) + where len = length ts -- | Creates 'Component' instances with 'Map' stores makeMapComponents :: [Name] -> Q [Dec] From 8a3b082b77a9c7b2b89ff8fd229041789baed3b6 Mon Sep 17 00:00:00 2001 From: IC Rainbow <486682+dpwiz@users.noreply.github.com> Date: Fri, 6 Mar 2026 23:35:15 +0200 Subject: [PATCH 2/2] Rename to makeInstanceTuples --- apecs/src/Apecs/TH.hs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apecs/src/Apecs/TH.hs b/apecs/src/Apecs/TH.hs index 67335f0..5bfe364 100644 --- a/apecs/src/Apecs/TH.hs +++ b/apecs/src/Apecs/TH.hs @@ -8,7 +8,7 @@ module Apecs.TH , makeWorldAndComponents , makeMapComponents , makeMapComponentsFor - , makeTypeSynonym + , makeInstanceTuples ) where import Control.Monad (filterM) @@ -55,15 +55,15 @@ makeWorldNoEC worldName cTypes = do |] -- Destructible type synonym - destructible_decl <- makeTypeSynonym (worldName ++ "Destructible") ''ExplDestroy cTypes + destructible_decl <- makeInstanceTuples (worldName ++ "Destructible") ''ExplDestroy cTypes pure $ data_decl : destructible_decl ++ concat (init_world : instances) where enumerate :: [a] -> [(Int,a)] enumerate = zip [0..] -makeTypeSynonym :: String -> Name -> [Name] -> Q [Dec] -makeTypeSynonym synName cls cTypes = do +makeInstanceTuples :: String -> Name -> [Name] -> Q [Dec] +makeInstanceTuples synName cls cTypes = do let destructible ty | nameBase ty == "EntityCounter" = pure False | otherwise = isInstance cls [ConT ''IO, AppT (ConT ''Storage) (ConT ty)]