This question has been heavily rewritten as suggested by @leftaroundabout. An earlier version can be seen in the editing history.
Haskell is known for facilitating thought by allowing more direct coding of mathematical abstractions. Cartesian work is a very basic mental object, with which many are familiar from childhood. However, there is nothing like this in Haskell. I think I need one so that my thinking flows, if nothing else. (Although this post is really inspired by some kind of ground-related code that I have on hand.) Letβs then form a common understanding that it is Cartesian (I will call it simply Cartesian).
Given the sequence of d :: Intcollection lengths (for example [[1,2], ['a', 'b']]), I would like to have all the combinations of my elements in the shortest possible time. This means that they work for them as if they were in the usual Functor, Foldable, Traversable, Monoid, etc. Indeed, we can represent any Cartesian as a suitable nested list of tuples:
type Lattice = [[(x, y)]]
type WeightedLattice = [[(x, y, w)]]
zipWith2 :: (a -> b -> c) -> [[a]] -> [[b]] -> [[c]]
zipWith2 f = zipWith (zipWith f)
fmap2 :: (Functor f, Functor g) => (a -> b) -> g (f a) -> g (f b)
fmap2 = fmap . fmap
sequence2 :: (Traversable s, Traversable t, Monad m) => t (s (m a)) -> m (t (s a))
sequence2 = sequence . fmap sequence
Similar constructions can be written out manually for any nesting depth.
We now introduce the difference between the Cartesian and the initial Cartesian:
The n-dimensional Cartesian is constructed from a heterogeneous sequence of collections, taking one element from each collection and combining them in an ordered manner with a suitable typed function. This Cartesian signature [Int, Char, Bool] can be formed using a function such as:
f :: Int -> Char -> Bool -> Either Char Int
f i c b = if b then Left c else Right i
arity:
initial :: Int -> Char -> Bool -> (Int, Char, Bool)
initial = (,,)
, , , , :
(fmap . ... . fmap) (uncurryN f)
; , Char a Right 3. , - , .
Lattice, , , . , . , , . , , .
. , ennuy , : 128 x 64 64 x 128 , 64 x 64; . , , , foldr (.) id [replicate d concat], . haskelly .
, : . , , , -, p , , - . , . Set , ?
, , :
. , . Data.List .
. , , . , , [1..3] x ['a', 'b'], , , [1,2] x ['a'..'c'], , zip .
, , . :
Cartesian [[1..3], ['a', 'b']] <> Cartesian [[True, False]]
- , :
Cartesian [[1..3], ['a', 'b'], [True, False]]
- , .
- , , , . , a Lattice . , , , , , "".
.
, , , , , . - . , .