{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs #-}
+{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE Rank2Types #-}
-- e.g. RTER (a,b) r = (r a, r b)
class (MKLT (TOL base) ~ base) => Tupled base where
-- | Apply r to each element of the tuple
- type RTER base (r :: * -> *) :: *
+ type RTER base (r :: k -> *) :: *
-- | Go from the tuple representation to a promoted list;
-- the inverse of MKLT (as asserted by class constraints).
- type TOL base :: [*]
+ type TOL base :: [k]
-- | Send a tuple to an HList
tupleHL :: base -> HList (TOL base)
MKRLT (RTR arred) (TOL (RTE arred)) ~ arred
)
=> RTupled arred where
- type RTR arred :: (* -> *)
- type RTE arred :: *
+ type RTR arred :: (k -> *)
+ type RTE arred :: k
-- | Eliminate an rtuple out to a list.
tupleopEL :: (RTR arred ~ r) => (forall x . r x -> a) -> arred -> [a]
-- XXX
foreachTupleSize f = mapM f [2..10] -- maxTupleSize]
-mkNames n = mapM (newName . ("mti" ++) . show) [1..n]
+mkNames' pfx n = mapM (newName . (pfx ++) . show) [1..n]
+
+mkNames = mkNames' "mti"
genMap app con var = foldl app con . map var
------------------------------------------------------------------------}}}
-- Experimental detritus (XXX) {{{
+-- Tuple-folding/mapping function generation
+
+{-
+-- | Monadic Tuple Fold and Map
+mkMTFoldMap :: Name -- ^ Function name
+
+ -> Maybe Name -- ^ Constructor, if any,
+ -- to be pattern matched away
+
+ -> ExpQ -- ^ Mapping applied at each element
+ -- Should be @a -> m b@
+
+ -> Maybe (ExpQ, ExpQ) -- ^ Common extractor and folder.
+ -- Should be @(b -> c, [c] -> m d)@
+
+ -> Maybe ExpQ -- ^ Map extractor.
+
+ -> Int -- ^ Tuple size
+ -> Q Clause
+mkMTFoldMap fn mc m mcf mm n = do
+ tnames <- mkNames' "t" n
+ rnames <- mkNames' "r" n
+ mnames <- mkNames' "m" n
+
+ -- Build the pattern to extract arguments to tnames
+ let pat = maybe (id) (\na t -> conP na [t]) mc $
+ tupP $ map varP tnames
+
+ -- Apply m
+ rstmts <- mapM (\(te,re) -> bindS re $ appE m te) $ zip tnames rnames
+
+ -- Build folding result
+ let mfstmt = (\(e,f) -> appE f $ listE $ map (appE e . varE) rnames)
+ `fmap` mcf
+
+ -- Build mapping result
+ let mmstmt = (\m -> genMap appE (conE $ tupleDataName n)
+ $ map (appE m . varE) rnames)
+ `fmap` mm
+
+ let pfx = rstmts
+
+ clause pat (normalB $ doE pfx) []
+-}
+
+
{-
mkNpleFunction :: String -> TypeQ -> Int -> TypeQ
mkNpleFunction _pfx rt n = do