Variable-arity zipWith (re)invented.

Continued discussion from https://groups.google.com/d/topic/haskell-cafe/-e-xaCEbd-w/discussion https://groups.google.com/d/topic/haskell-cafe/kM_-NvXAcx8/discussion Thank you for all the answeres and thinkings; Here's zipWithN for general Zip functors: [1] . This, together with [2] may constitute a small hackage. A modification from Wren's idea to [1] is the use of fmap instead of repeat. I'm wondering if there are any laws for Zip functors. I first thought that there are similarity between Zips and Applicatives, as [3] states
instance Applicative f => Zip f where zip = liftA2 (,)
However, my intuition is that zipping two arrays should result in an array of size of the same order as two, giving rise to a Zip functor law candidate: zipWith const xs $ zipWith const xs ys == zipWith const xs ys which is violated by the above statement "zip = liftA2 (,)" . [1] https://github.com/nushio3/practice/blob/master/variable-arity/ZipWithN-2.hs [2] https://github.com/nushio3/practice/blob/master/free-objects/zipf-12.hs [3] http://hackage.haskell.org/packages/archive/TypeCompose/0.9.7/doc/html/Data-... -- Takayuki MURANUSHI The Hakubi Center for Advanced Research, Kyoto University http://www.hakubi.kyoto-u.ac.jp/02_mem/h22/muranushi.html

On Sat, Dec 8, 2012 at 10:27 AM, Takayuki Muranushi
Continued discussion from
https://groups.google.com/d/topic/haskell-cafe/-e-xaCEbd-w/discussion https://groups.google.com/d/topic/haskell-cafe/kM_-NvXAcx8/discussion
Thank you for all the answeres and thinkings;
Here's zipWithN for general Zip functors: [1] . This, together with [2] may constitute a small hackage. A modification from Wren's idea to [1] is the use of fmap instead of repeat.
I'm wondering if there are any laws for Zip functors. I first thought that there are similarity between Zips and Applicatives, as [3] states
instance Applicative f => Zip f where zip = liftA2 (,)
However, my intuition is that zipping two arrays should result in an array of size of the same order as two, giving rise to a Zip functor law candidate:
zipWith const xs $ zipWith const xs ys == zipWith const xs ys
which is violated by the above statement "zip = liftA2 (,)" .
[1] https://github.com/nushio3/practice/blob/master/variable-arity/ZipWithN-2.hs [2] https://github.com/nushio3/practice/blob/master/free-objects/zipf-12.hs [3] http://hackage.haskell.org/packages/archive/TypeCompose/0.9.7/doc/html/Data-...
Hi again, Takayuki While the forZN in zipf-12 is able to infer the result type given arguments, it doesn't give any useful information about types for arguments unlike an example here: http://code.haskell.org/~aavogt/flip_zipWithN/P4.hs which imports a slight modification of Paczesiowa's code: http://code.haskell.org/~aavogt/flip_zipWithN/Part1.lhs But maybe it isn't possible to infer much about earlier arguments given later ones since there is an instance Zip ((->) a), that forZN apparently can work with. Adam

Repeated thanks to you, Adam! Your code is brilliantly simple. Sadly, I cannot reproduce the behaviors in your comments on my ghci (7.6.1) ..... Can we guess why? The version of packages we are using? Mines are here. https://github.com/nushio3/practice/tree/master/variable-arity/adam
:t forZ [1,2,3] (+) forZ [1,2,3] (+) :: (Num t, Num a, TypeCast br HFalse, HBuild2' br (HCons [t] HNil) (a -> a -> a) r) => r forZ [1,2,3] [10] (+)
<interactive>:13:1: Couldn't match type `[y]' with `(a0 -> a0 -> a0) -> t0' When using functional dependencies to combine Apply ApplyZap (a, b) [y], arising from the dependency `f a -> r' in the instance declaration at Part1.lhs:193:12 Apply ApplyZap ([[t2]], [t1]) ((a0 -> a0 -> a0) -> t0), arising from a use of `forZ' at <interactive>:13:1-4 In the expression: forZ [1, 2, 3] [10] (+) In an equation for `it': it = forZ [1, 2, 3] [10] (+)
forZ [1,2,3] "hi there" (,)
<interactive>:14:1:
Couldn't match type `[y]' with `(a0 -> b0 -> (a0, b0)) -> t0'
When using functional dependencies to combine
Apply ApplyZap (a, b) [y],
arising from the dependency `f a -> r'
in the instance declaration at Part1.lhs:193:12
Apply ApplyZap ([[Char]], [t1]) ((a0 -> b0 -> (a0, b0)) -> t0),
arising from a use of `forZ' at <interactive>:14:1-4
In the expression: forZ [1, 2, 3] "hi there" (,)
In an equation for `it': it = forZ [1, 2, 3] "hi there" (,)
Best,
Takayuki
2012/12/11 adam vogt
On Sat, Dec 8, 2012 at 10:27 AM, Takayuki Muranushi
wrote: Continued discussion from
https://groups.google.com/d/topic/haskell-cafe/-e-xaCEbd-w/discussion https://groups.google.com/d/topic/haskell-cafe/kM_-NvXAcx8/discussion
Thank you for all the answeres and thinkings;
Here's zipWithN for general Zip functors: [1] . This, together with [2] may constitute a small hackage. A modification from Wren's idea to [1] is the use of fmap instead of repeat.
I'm wondering if there are any laws for Zip functors. I first thought that there are similarity between Zips and Applicatives, as [3] states
instance Applicative f => Zip f where zip = liftA2 (,)
However, my intuition is that zipping two arrays should result in an array of size of the same order as two, giving rise to a Zip functor law candidate:
zipWith const xs $ zipWith const xs ys == zipWith const xs ys
which is violated by the above statement "zip = liftA2 (,)" .
[1] https://github.com/nushio3/practice/blob/master/variable-arity/ZipWithN-2.hs [2] https://github.com/nushio3/practice/blob/master/free-objects/zipf-12.hs [3] http://hackage.haskell.org/packages/archive/TypeCompose/0.9.7/doc/html/Data-...
Hi again, Takayuki
While the forZN in zipf-12 is able to infer the result type given arguments, it doesn't give any useful information about types for arguments unlike an example here:
http://code.haskell.org/~aavogt/flip_zipWithN/P4.hs
which imports a slight modification of Paczesiowa's code: http://code.haskell.org/~aavogt/flip_zipWithN/Part1.lhs
But maybe it isn't possible to infer much about earlier arguments given later ones since there is an instance Zip ((->) a), that forZN apparently can work with.
Adam
-- Takayuki MURANUSHI The Hakubi Center for Advanced Research, Kyoto University http://www.hakubi.kyoto-u.ac.jp/02_mem/h22/muranushi.html

On Mon, Dec 10, 2012 at 7:23 PM, Takayuki Muranushi
Repeated thanks to you, Adam! Your code is brilliantly simple.
Sadly, I cannot reproduce the behaviors in your comments on my ghci (7.6.1) ..... Can we guess why? The version of packages we are using?
Mines are here.
https://github.com/nushio3/practice/tree/master/variable-arity/adam
:t forZ [1,2,3] (+) forZ [1,2,3] (+) :: (Num t, Num a, TypeCast br HFalse, HBuild2' br (HCons [t] HNil) (a -> a -> a) r) => r forZ [1,2,3] [10] (+)
<interactive>:13:1: Couldn't match type `[y]' with `(a0 -> a0 -> a0) -> t0' When using functional dependencies to combine Apply ApplyZap (a, b) [y], arising from the dependency `f a -> r' in the instance declaration at Part1.lhs:193:12 Apply ApplyZap ([[t2]], [t1]) ((a0 -> a0 -> a0) -> t0), arising from a use of `forZ' at <interactive>:13:1-4 In the expression: forZ [1, 2, 3] [10] (+) In an equation for `it': it = forZ [1, 2, 3] [10] (+)
forZ [1,2,3] "hi there" (,)
<interactive>:14:1: Couldn't match type `[y]' with `(a0 -> b0 -> (a0, b0)) -> t0' When using functional dependencies to combine Apply ApplyZap (a, b) [y], arising from the dependency `f a -> r' in the instance declaration at Part1.lhs:193:12 Apply ApplyZap ([[Char]], [t1]) ((a0 -> b0 -> (a0, b0)) -> t0), arising from a use of `forZ' at <interactive>:14:1-4 In the expression: forZ [1, 2, 3] "hi there" (,) In an equation for `it': it = forZ [1, 2, 3] "hi there" (,)
Best,
Takayuki
Hi Takayuki I must have changed the imports at some point: the file doesn't work here with the same HList and ghc versions either. It does work if I change the `import Data.HList.FakePrelude' to `import Data.HList.TypeCastGeneric2'. Finally another note is that it only simplifies the type for the first argument probably because of the hTrue in the definition of forZ: *P4> :t \x -> forZ [1,2,3] x (+) \x -> forZ [1,2,3] x (+) :: (Num a, Num t1, HBuild2' br (HCons [t1] HNil) b ((a -> a -> a) -> t), IsZip b br) => b -> t *P4> :t \x -> forZ x [1,2,3] (+) \x -> forZ x [1,2,3] (+) :: Num x => [x] -> [x] Adam

Hi Takayuki,
Just thought I'd mention another approach to a variadic zipWith, this one
using type families:
http://typesandkinds.wordpress.com/2012/11/26/variable-arity-zipwith/
The current lack of overlap in type families makes things a bit more
complicated, but it can
be solved using the upcoming overlapping type families.
Cheers,
Pedro
On Sat, Dec 8, 2012 at 3:27 PM, Takayuki Muranushi
Continued discussion from
https://groups.google.com/d/topic/haskell-cafe/-e-xaCEbd-w/discussion https://groups.google.com/d/topic/haskell-cafe/kM_-NvXAcx8/discussion
Thank you for all the answeres and thinkings;
Here's zipWithN for general Zip functors: [1] . This, together with [2] may constitute a small hackage. A modification from Wren's idea to [1] is the use of fmap instead of repeat.
I'm wondering if there are any laws for Zip functors. I first thought that there are similarity between Zips and Applicatives, as [3] states
instance Applicative f => Zip f where zip = liftA2 (,)
However, my intuition is that zipping two arrays should result in an array of size of the same order as two, giving rise to a Zip functor law candidate:
zipWith const xs $ zipWith const xs ys == zipWith const xs ys
which is violated by the above statement "zip = liftA2 (,)" .
[1] https://github.com/nushio3/practice/blob/master/variable-arity/ZipWithN-2.hs [2] https://github.com/nushio3/practice/blob/master/free-objects/zipf-12.hs [3] http://hackage.haskell.org/packages/archive/TypeCompose/0.9.7/doc/html/Data-...
-- Takayuki MURANUSHI The Hakubi Center for Advanced Research, Kyoto University http://www.hakubi.kyoto-u.ac.jp/02_mem/h22/muranushi.html
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (3)
-
adam vogt
-
José Pedro Magalhães
-
Takayuki Muranushi