Derive the Applicative instance for a ZipList—explain why it cannot be the default…
- User
Derive the
Applicativeinstance for aZipList—explain why it cannot be the default instance forList. Assistant
Derive the Applicative instance for a ZipList—explain why it cannot be the default instance for List.
1. Understand the Goal: The user wants two things: (a) derive (define/explain) the Applicative instance for ZipList (often found in Haskell libraries like Control.Applicative), and (b) explain why this specific instance isn't the default Applicative instance for the standard List type.
Part 1: The ZipList Applicative Instance:
ZipList? It's a newtype wrapper around a list. newtype ZipList a = ZipList { getZipList :: [a] }. This exists to give lists a different Applicative (and previously Monad, though ZipList isn't a Monad easily) behavior.[]) is non-deterministic computation (cartesian product). ZipList is... zipping.pure function: If I have a function f and a list of values [x, y, z], and I want to apply f to all of them via <*> (zipping), I need an infinite list of fs.
pure x = ZipList (repeat x).<*> (ap) function:
[f, g] <*> [x, y] = [f x, f y, g x, g y].[f, g] <*> [x, y] = [f x, g y].ZipList fs <*> ZipList xs = ZipList (zipWith ($) fs xs).zipWith works like vector/matrix component-wise operations.Part 2: Why isn't this the default for List?
pure x = [x], fs <*> xs = [f x | f <- fs, x <- xs].pure for ZipList requires repeat. If pure were repeat for standard lists, simple operations might hang or consume infinite memory if the list isn't processed lazily enough or if the context requires a finite list. But actually, Haskell handles infinite lists fine.instance Monad []). There is a law relating Applicative and Monad: pure f <*> x should equal f <$> x (which is fine) and generally mf <*> mx implies something about ap. Specifically, if a type has a Monad instance, the standard expectation is that Applicative is consistent with it (i.e., <*> = ap).ap: fs ap xs is [f x | f <- fs, x <- xs]. This is the Cartesian product.Monad instance for lists that behaves like zipping easily (without constraints like MonadZip).Structuring the Answer:
ZipList as a newtype.pure: Explain the need for repeat.<*>: Explain zipWith ($).Applicative should be compatible with Monad (<*> = ap). The standard list Monad represents non-determinism. The ZipList logic does not support a standard Monad interface easily.pure. pure x = [x] is finite and safe. pure x = repeat x is infinite. While Haskell handles this, it changes the semantic meaning entirely (a single result vs a stream of results).Refining the Content:
<*> usually comes from ap.Self-Correction/Double Check during drafting:
ZipList isn't a Monad? I should clarify that ZipList doesn't have a standard Monad instance. This reinforces why it can be the default for [] (which is a Monad).repeat correct? Yes. If you zip [1, 2] with pure (+1), you get [2, 3]. pure must provide enough copies of the function to match the longest list it might be applied to. Since we don't know the length, it must be infinite.Final Polish: Ensure clear headings and bold text for readability. (Proceed to generate output).
Here is the derivation of the Applicative instance for ZipList and an explanation of why this behavior differs from the standard List instance.
ZipList InstanceTo understand ZipList, we first need to look at the standard Applicative instance for lists to see what we are changing.
The default instance for lists ([]) treats the list as a non-deterministic computation (a set of possible values).
pure: Creates a list with a single result (returning a value deterministically).<*>: Takes every function from the left list and applies it to every value in the right list (Cartesian product).-- Standard List instance
instance Applicative [] where
pure x = [x]
fs <*> xs = [f x | f <- fs, x <- xs]
-- Example:
-- [(+1), (*2)] <*> [1, 2, 3]
-- Result: [2, 3, 4, 2, 4, 6]
ZipList Instance (Zipping)ZipList is a newtype wrapper around a list that changes the applicative behavior to index-wise application (like zipWith).
Defining pure
If we want to apply a function list to a value list index-by-index, we need a function for every index.
If we have a list of 100 values, we need 100 copies of the function.
Therefore, pure must create an infinite list of the function.
pure x = ZipList (repeat x)
Defining <*>
We want to apply the first function to the first value, the second to the second, etc. This corresponds exactly to zipWith ($).
(ZipList fs) <*> (ZipList xs) = ZipList (zipWith ($) fs xs)
The Final Instance:
newtype ZipList a = ZipList { getZipList :: [a] }
instance Applicative ZipList where
pure x = ZipList (repeat x)
ZipList fs <*> ZipList xs = ZipList (zipWith ($) fs xs)
Example Usage:
-- We apply (+1) to 1, (*2) to 2, and (*3) to 3
getZipList $ ZipList [(+1), (*2), (*3)] <*> ZipList [1, 2, 3]
-- Result: [2, 4, 9]
ZipList cannot be the default List instanceYou might ask: "Why isn't this the default? It seems intuitive."
There are two primary reasons: The behavior of pure and consistency with Monad.
pure Problem (Finite vs. Infinite)If the default list instance used the ZipList logic, pure would have to produce an infinite list (repeat x).
It breaks termination guarantees:
In Haskell, pure 5 is expected to be a small, finite data structure that can be printed or processed immediately. If pure 5 resulted in [5, 5, 5, 5, ...], simply typing it in the REPL (GHCI) would cause an infinite loop (or infinite output), crashing basic introspection tools.
It ruins shorter lists: Zipping stops at the end of the shorter list.
-- Using hypothetical Zip-logic for default lists
(pure (+1)) <*> [1, 2, 3]
-- would become: [2, 3, 4] (Fine)
-- But what if we use a finite function list?
[(+1)] <*> [1, 2, 3]
-- Zip logic result: [2] (The rest are lost!)
Usually, when programmers use lists as "containers," they expect the structure to be preserved. Losing data because one list is shorter is specific to zipping, not general application.
This is the "Killer Argument." In Haskell, there is a mathematical relationship between Applicative and Monad.
If a type is a Monad, it must define its Applicative instance to behave exactly like the Monad's return and ap.
List as a Monad: The list monad is designed for non-determinism. It represents "all possible outcomes."
do
x <- [1, 2]
y <- [10, 20]
return (x + y)
-- Result: [11, 21, 12, 22]
This relies on the Cartesian product logic.
List as Applicative:
Because the List is defined as a Monad (for list comprehensions and do notation), its Applicative instance must mirror that behavior.
pure must be return (wrapping in a list), and <*> must be ap (Cartesian product).
Summary:
The ZipList instance implies that a single value (pure x) is actually an infinite stream of that value. This is semantically incompatible with the standard view of a List as a finite container of values or a set of non-deterministic choices. Therefore, ZipList is separated into its own type to allow users to opt-in to "zipping" semantics without breaking the standard "monadic" behavior of lists.