<span style="font-family: courier new,monospace;">What is the reasoning behind the ghc restriction that "A lazy (~) pattern cannot bind existential type variables"?</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">This error came up for me in the following code:</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> -- | Add a continuation.</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> data WithCont h b c = forall a. WC (h b a) (a -> c)</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> instance Functor (WithCont h b) where</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> fmap g ~(WC f k) = WC f (fmap g k)</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">The error message:</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> A lazy (~) pattern cannot bind existential type variables</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> `a' is a rigid type variable bound by</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> the constructor `WC' at Data/Zip/FoldL.hs:66:11</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> In the pattern: ~(WC f k)</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">I also tried this variation:</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> instance Functor (WithCont h b) where</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> fmap g wc = WC f (fmap g k)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> where WC f k = wc</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">and got this message:</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> My brain just exploded.</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> I can't handle pattern bindings for existentially-quantified constructors.</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> Instead, use a case-expression, or do-notation, to unpack the constructor.</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> In the binding group for</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> WC f k</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> In a pattern binding: WC f k = wc</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">I can work around these limitations by using a lambda:</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> instance Functor (WithCont h b) where</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> fmap g = \ (WC f k) -> WC f (fmap g k)</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">which I believe is equivalent. Please correct me if I'm wrong here.</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">For infix definitions like (<*>), however, this work-around is less pleasant.</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">For instance,</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (<*>) = \ (WC hf hk) (WC xf xk) -></span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> WC (hf `zip` xf) (\ (a,a') -> (hk a) (xk a'))</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">instead of the prettier but forbidden</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> ~(WC hf hk) <*> ~(WC xf xk) =</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> WC (hf `zip` xf) (\ (a,a') -> (hk a) (xk a'))</span><br style="font-family: courier new,monospace;">
<br><br>If you're curious what these definitions are about, see <a href="http://conal.net/blog/posts/enhancing-a-zip/">http://conal.net/blog/posts/enhancing-a-zip/</a> .<br><br>Thanks, - Conal<br><br style="font-family: courier new,monospace;">