Cartesian Product in Standard Haskell Libraries

I need the usual Cartesian product of a list of lists. In the posts http://stackoverflow.com/questions/4119730/cartesian-product http://stackoverflow.com/questions/3387359/calculate-n-ary-cartesian-product it is recommended to use the sequence procedure, which is in the standard Prelude, as a "Cartesian product of a list of lists" function. That is we do have that sequence [["a", "b"], ["c", "d"]] evaluates to [["a","c"],["a","d"],["b","c"],["b","d"]] and often, sequence seems to give the expected answer. But we have also that, and here we quote from a GHCi session,
sequence [] [] it :: [()]
This looks to me to be a violation of the rule that the Cartesian product of an empty list of lists is a list with one element in it. It looks to be a violation because "[]" looks like a name for an empty list. But we also have
length (sequence []) 1 it :: Int
which almost reassures me. Appended are a Scheme session and a GHCi session showing some of what puzzles me. I remember when I wrote my now standard Cartesian product Scheme procedure I was delighted that, we get
(cprd '()) (())
which gives '() as the single element of the Cartesian product of an empty list of lists. ad Old Types vs New: Indeed every particular application of the Scheme procedure cprd should be to (some instance of) some "type" of lists of lists, and thus the one element in the output, in case the list of lists is the empty list, should be of the type of the elements, that is, the inner lists, of the input list of lists. In Haskell I do not know enough to say, but, certainly the behavior of sequence-plus-GHCi-plus-printing-conventions was surprising to me. Oi. Perhaps an/the object of type () just does not get printed clearly. So a list with only one object, and that one object of type (), gets printed on the page as [] which is a text name for the list, which is of length one, but this text name looks on the page just like the text name of an empty list. oo--JS. <blockquote what="Scheme session showing my standard version of Cartesian product" date="Monday 24 December 2012 01:30:37 -0500"> SCM version 5e5, Copyright (C) 1990-2006 Free Software Foundation. SCM comes with ABSOLUTELY NO WARRANTY; for details type `(terms)'. This is free software, and you are welcome to redistribute it under certain conditions; type `(terms)' for details. ;loading /usr/share/slib/require ;done loading /usr/share/slib/require.scm ;loading /usr/share/slib/require ;done loading /usr/share/slib/require.scm ;loading /usr/lib/scm/Link ;done loading /usr/lib/scm/Link.scm ;loading /usr/lib/scm/Transcen ;done loading /usr/lib/scm/Transcen.scm
(define cartesian:multiply-by-one-list (lambda (multiplier old-product) (if (equal? multiplier '()) '() (if (equal? (cdr multiplier) '()) (map (lambda (x) (cons (car multiplier) x)) old-product) (append (map (lambda (x) (cons (car multiplier) x)) old-product) (cartesian:multiply-by-one-list (cdr multiplier) old-product)))))) #<unspecified> (define cartesian:product (lambda (l) (if (equal? l '()) (list '()) (cartesian:multiply-by-one-list (car l) (cartesian:product (cdr l)))))) #<unspecified> (define cprd cartesian:product) #<unspecified> (cprd '()) (()) (cprd '(())) () (cprd '((a))) ((a)) (cprd '((a b))) ((a) (b)) (cprd '(() (a))) () (cprd '((a) ())) () (cprd '((a) (b))) ((a b)) (cprd '((a b) (c d))) ((a c) (a d) (b c) (b d)) (cprd '((a b) (c d) (e) (f g h))) ((a c e f) (a c e g) (a c e h) (a d e f) (a d e g) (a d e h) (b c e f) (b c e g) (b c e h) (b d e f) (b d e g) (b d e h)) (cprd '(() ())) () ;; OK, this looks right. On lists of lists of symbols ;; cprd seems to be a version of Cartesian product. (quit) ;EXIT
Process scheme finished </blockquote> <blockquote what="GHCi session showing sequence and an untested version of a Haskell Cartesian product function" point="To me sequence's behavior on the list of lists [] is puzzling. sequence seems not to obey the rule that the Cartesian product of an empty list of lists is a list with one element in it, whose name/manifestation, in general, might be hard to guess." date="Monday 24 December 2012 01:32:56 -0500"> GHCi, version 7.4.1: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Prelude> :set +t Prelude> :set prompt "> "
sequence [] [] it :: [()] sequence [[]] [] it :: [[a]] sequence [["a"]] [["a"]] it :: [[[Char]]] sequence [["a", "b"]] [["a"],["b"]] it :: [[[Char]]] sequence [[], ["a"]] [] it :: [[[Char]]] sequence [["a"], []] [] it :: [[[Char]]] sequence [["a"], ["b"]] [["a","b"]] it :: [[[Char]]] sequence [["a", "b"], ["c", "d"]] [["a","c"],["a","d"],["b","c"],["b","d"]] it :: [[[Char]]] sequence [["a", "b"], ["c", "d"], ["e"], ["f", "g", "h"]] [["a","c","e","f"],["a","c","e","g"],["a","c","e","h"],["a","d","e","f"],["a","d","e","g"],["a","d","e","h"],["b","c","e","f"],["b","c","e","g"],["b","c","e","h"],["b","d","e","f"],["b","d","e","g"],["b","d","e","h"]] it :: [[[Char]]] length it 12 it :: Int 2 * 2 * 1 * 3 12 it :: Integer sequence [[], []] [] it :: [[a]] -- OK, it looks as though 'sequence' as suggested in the -- Stackoverflow post -- http://stackoverflow.com/questions/4119730/cartesian-product -- in Answer 10, does most of the time compute a version -- of Cartesian product very much as does the Scheme version. -- Let us do the example in the answer: sequence [[1,2,3],[4,5,6]] [[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]] it :: [[Integer]] length it 9 it :: Int 3 * 3 9 it :: Integer -- But this seems to violate the convention that -- Cartesian product, when handed an empty list of lists -- returns a list with one element. This convention -- makes the 'length' function a homomorphism from the monoid -- of lists, with cartesian product as multiplication, and the empty list -- as the identity element, to the monoid of non-negative integers -- with '*' as multiplication, and '1' as the identity element. -- Let us have a look again as what 'sequence' does: sequence [] [] it :: [()] length it 0 it :: Int sequence [[]] [] it :: [[a]] length it 0 it :: Int length (sequence []) 1 it :: Int :t (sequence []) (sequence []) :: Monad m => m [a] :t (sequence [[]]) (sequence [[]]) :: [[a]] -- Let us try a version of our Scheme procedure 'cprd'. let hmbol multiplier oldproduct = [ x:xs | x <- multiplier, xs <- oldproduct] hmbol :: [a] -> [[a]] -> [[a]] hmbol [] [["a"]] [] it :: [[[Char]]] hmbol [] [[]] [] it :: [[a]] hmbol ["a"] [[]] [["a"]] it :: [[[Char]]] hmbol ["a", "b"] [[]] [["a"],["b"]] it :: [[[Char]]] hmbol ["a", "b"] [] [] it :: [[[Char]]] hmbol ["a", "b"] [["c"]] [["a","c"],["b","c"]] it :: [[[Char]]] hmbol ["a", "b"] [["c"], ["d", "e"]] [["a","c"],["a","d","e"],["b","c"],["b","d","e"]] it :: [[[Char]]] hmbol ["a", "b"] [["c"], [], ["d", "e"]] [["a","c"],["a"],["a","d","e"],["b","c"],["b"],["b","d","e"]] it :: [[[Char]]] let hcprod = foldr hmbol [[]] hcprod :: [[a]] -> [[a]] hcprod [] [[]] it :: [[a]] hcprod [[]] [] it :: [[a]] hcprod [["a", "b"], ["c"], [], ["d", "e"]] [] it :: [[[Char]]] -- Let us repeat all the tests of 'cprod' in the Scheme session: hcprod [] [[]] it :: [[a]] hcprod [[]] [] it :: [[a]] hcprod [["a"]] [["a"]] it :: [[[Char]]] hcprod [["a", "b"]] [["a"],["b"]] it :: [[[Char]]] hcprod [[], ["a"]] [] it :: [[[Char]]] hcprod [["a"], []] [] it :: [[[Char]]] hcprod [["a"], ["b"]] [["a","b"]] it :: [[[Char]]] hcprod [["a", "b"], ["c", "d"]] [["a","c"],["a","d"],["b","c"],["b","d"]] it :: [[[Char]]] hcprod [["a", "b"], ["c", "d"], ["e"], ["f", "g", "h"]] [["a","c","e","f"],["a","c","e","g"],["a","c","e","h"],["a","d","e","f"],["a","d","e","g"],["a","d","e","h"],["b","c","e","f"],["b","c","e","g"],["b","c","e","h"],["b","d","e","f"],["b","d","e","g"],["b","d","e","h"]] it :: [[[Char]]] length it 12 it :: Int 2 * 2 * 1 * 3 12 it :: Integer hcprod [[], []] [] it :: [[a]] -- My two puzzles are: -- Why does -- sequence [] -- evaluate to -- [] -- and not a list with one element? -- Why is the type of the thing that -- sequence [] -- evaluates to -- [()] -- ? :q Leaving GHCi.
Process haskell finished </blockquote>

On Mon, 24 Dec 2012, Jay Sulzberger wrote:
I need the usual Cartesian product of a list of lists. In the posts
http://stackoverflow.com/questions/4119730/cartesian-product http://stackoverflow.com/questions/3387359/calculate-n-ary-cartesian-product
it is recommended to use the sequence procedure, which is in the standard Prelude, as a "Cartesian product of a list of lists" function. That is we do have that
sequence [["a", "b"], ["c", "d"]]
evaluates to
[["a","c"],["a","d"],["b","c"],["b","d"]]
and often, sequence seems to give the expected answer. But we have also that, and here we quote from a GHCi session,
sequence [] [] it :: [()]
This looks to me to be a violation of the rule that the Cartesian product of an empty list of lists is a list with one element in it. It looks to be a violation because "[]" looks like a name for an empty list. But we also have
length (sequence []) 1 it :: Int
which almost reassures me.
But we also have
sequence [] [] it :: [()] length it 0 it :: Int
So if "it" is the result of evaluating (sequence []) then this seems to say the result is a list of length 0. Which seems in contradiction with the reassuring evaluation above. ad thinko in comments in Haskell transcript: The empty list in our monoid of lists is the zero, not the identity. Any one element list is an "identity", so of course our claimed monoid is also not actually a monoid, except, ah, there is a theory for dealing with the fact that '() is also an endstopper, and not just an empty list. Our "monoid" is really very much a monoidoid, and to take account of the triple identity of '() as endstopper and also empty list and identity for append, will, ah, yes, they are everywhere, VARIOUS FUNCTORS, perhaps not quite falling under the Haskell Functor typeclass. And some of this must be handled in the Haskell lists monad: argument: the thinko, in part, was due to a conflation of some different expressions involving T, mu, and eta, to use the terminology of http://en.wikipedia.org/wiki/Monad_%28mathematics%29 [page was last modified on 15 December 2012 at 16:03] and the fact that, and New Crazy Type theory deals with this, the only "definable" sets are those built from the empty set. oo--JS.
Appended are a Scheme session and a GHCi session showing some of what puzzles me. I remember when I wrote my now standard Cartesian product Scheme procedure I was delighted that, we get
(cprd '()) (())
which gives '() as the single element of the Cartesian product of an empty list of lists.
ad Old Types vs New: Indeed every particular application of the Scheme procedure cprd should be to (some instance of) some "type" of lists of lists, and thus the one element in the output, in case the list of lists is the empty list, should be of the type of the elements, that is, the inner lists, of the input list of lists. In Haskell I do not know enough to say, but, certainly the behavior of sequence-plus-GHCi-plus-printing-conventions was surprising to me.
Oi. Perhaps an/the object of type () just does not get printed clearly. So a list with only one object, and that one object of type (), gets printed on the page as
[]
which is a text name for the list, which is of length one, but this text name looks on the page just like the text name of an empty list.
oo--JS.
<blockquote what="Scheme session showing my standard version of Cartesian product" date="Monday 24 December 2012 01:30:37 -0500">
SCM version 5e5, Copyright (C) 1990-2006 Free Software Foundation. SCM comes with ABSOLUTELY NO WARRANTY; for details type `(terms)'. This is free software, and you are welcome to redistribute it under certain conditions; type `(terms)' for details. ;loading /usr/share/slib/require ;done loading /usr/share/slib/require.scm ;loading /usr/share/slib/require ;done loading /usr/share/slib/require.scm ;loading /usr/lib/scm/Link ;done loading /usr/lib/scm/Link.scm ;loading /usr/lib/scm/Transcen ;done loading /usr/lib/scm/Transcen.scm
(define cartesian:multiply-by-one-list (lambda (multiplier old-product) (if (equal? multiplier '()) '() (if (equal? (cdr multiplier) '()) (map (lambda (x) (cons (car multiplier) x)) old-product) (append (map (lambda (x) (cons (car multiplier) x)) old-product) (cartesian:multiply-by-one-list (cdr multiplier) old-product)))))) #<unspecified> (define cartesian:product (lambda (l) (if (equal? l '()) (list '()) (cartesian:multiply-by-one-list (car l) (cartesian:product (cdr l)))))) #<unspecified> (define cprd cartesian:product) #<unspecified> (cprd '()) (()) (cprd '(())) () (cprd '((a))) ((a)) (cprd '((a b))) ((a) (b)) (cprd '(() (a))) () (cprd '((a) ())) () (cprd '((a) (b))) ((a b)) (cprd '((a b) (c d))) ((a c) (a d) (b c) (b d)) (cprd '((a b) (c d) (e) (f g h))) ((a c e f) (a c e g) (a c e h) (a d e f) (a d e g) (a d e h) (b c e f) (b c e g) (b c e h) (b d e f) (b d e g) (b d e h)) (cprd '(() ())) () ;; OK, this looks right. On lists of lists of symbols ;; cprd seems to be a version of Cartesian product. (quit) ;EXIT
Process scheme finished
</blockquote>
<blockquote what="GHCi session showing sequence and an untested version of a Haskell Cartesian product function" point="To me sequence's behavior on the list of lists [] is puzzling. sequence seems not to obey the rule that the Cartesian product of an empty list of lists is a list with one element in it, whose name/manifestation, in general, might be hard to guess." date="Monday 24 December 2012 01:32:56 -0500">
GHCi, version 7.4.1: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Prelude> :set +t Prelude> :set prompt "> "
sequence [] [] it :: [()] sequence [[]] [] it :: [[a]] sequence [["a"]] [["a"]] it :: [[[Char]]] sequence [["a", "b"]] [["a"],["b"]] it :: [[[Char]]] sequence [[], ["a"]] [] it :: [[[Char]]] sequence [["a"], []] [] it :: [[[Char]]] sequence [["a"], ["b"]] [["a","b"]] it :: [[[Char]]] sequence [["a", "b"], ["c", "d"]] [["a","c"],["a","d"],["b","c"],["b","d"]] it :: [[[Char]]] sequence [["a", "b"], ["c", "d"], ["e"], ["f", "g", "h"]] [["a","c","e","f"],["a","c","e","g"],["a","c","e","h"],["a","d","e","f"],["a","d","e","g"],["a","d","e","h"],["b","c","e","f"],["b","c","e","g"],["b","c","e","h"],["b","d","e","f"],["b","d","e","g"],["b","d","e","h"]] it :: [[[Char]]] length it 12 it :: Int 2 * 2 * 1 * 3 12 it :: Integer sequence [[], []] [] it :: [[a]] -- OK, it looks as though 'sequence' as suggested in the -- Stackoverflow post -- http://stackoverflow.com/questions/4119730/cartesian-product -- in Answer 10, does most of the time compute a version -- of Cartesian product very much as does the Scheme version. -- Let us do the example in the answer: sequence [[1,2,3],[4,5,6]] [[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]] it :: [[Integer]] length it 9 it :: Int 3 * 3 9 it :: Integer -- But this seems to violate the convention that -- Cartesian product, when handed an empty list of lists -- returns a list with one element. This convention -- makes the 'length' function a homomorphism from the monoid -- of lists, with cartesian product as multiplication, and the empty list -- as the identity element, to the monoid of non-negative integers -- with '*' as multiplication, and '1' as the identity element. -- Let us have a look again as what 'sequence' does: sequence [] [] it :: [()] length it 0 it :: Int sequence [[]] [] it :: [[a]] length it 0 it :: Int length (sequence []) 1 it :: Int :t (sequence []) (sequence []) :: Monad m => m [a] :t (sequence [[]]) (sequence [[]]) :: [[a]] -- Let us try a version of our Scheme procedure 'cprd'. let hmbol multiplier oldproduct = [ x:xs | x <- multiplier, xs <- oldproduct] hmbol :: [a] -> [[a]] -> [[a]] hmbol [] [["a"]] [] it :: [[[Char]]] hmbol [] [[]] [] it :: [[a]] hmbol ["a"] [[]] [["a"]] it :: [[[Char]]] hmbol ["a", "b"] [[]] [["a"],["b"]] it :: [[[Char]]] hmbol ["a", "b"] [] [] it :: [[[Char]]] hmbol ["a", "b"] [["c"]] [["a","c"],["b","c"]] it :: [[[Char]]] hmbol ["a", "b"] [["c"], ["d", "e"]] [["a","c"],["a","d","e"],["b","c"],["b","d","e"]] it :: [[[Char]]] hmbol ["a", "b"] [["c"], [], ["d", "e"]] [["a","c"],["a"],["a","d","e"],["b","c"],["b"],["b","d","e"]] it :: [[[Char]]] let hcprod = foldr hmbol [[]] hcprod :: [[a]] -> [[a]] hcprod [] [[]] it :: [[a]] hcprod [[]] [] it :: [[a]] hcprod [["a", "b"], ["c"], [], ["d", "e"]] [] it :: [[[Char]]] -- Let us repeat all the tests of 'cprod' in the Scheme session: hcprod [] [[]] it :: [[a]] hcprod [[]] [] it :: [[a]] hcprod [["a"]] [["a"]] it :: [[[Char]]] hcprod [["a", "b"]] [["a"],["b"]] it :: [[[Char]]] hcprod [[], ["a"]] [] it :: [[[Char]]] hcprod [["a"], []] [] it :: [[[Char]]] hcprod [["a"], ["b"]] [["a","b"]] it :: [[[Char]]] hcprod [["a", "b"], ["c", "d"]] [["a","c"],["a","d"],["b","c"],["b","d"]] it :: [[[Char]]] hcprod [["a", "b"], ["c", "d"], ["e"], ["f", "g", "h"]] [["a","c","e","f"],["a","c","e","g"],["a","c","e","h"],["a","d","e","f"],["a","d","e","g"],["a","d","e","h"],["b","c","e","f"],["b","c","e","g"],["b","c","e","h"],["b","d","e","f"],["b","d","e","g"],["b","d","e","h"]] it :: [[[Char]]] length it 12 it :: Int 2 * 2 * 1 * 3 12 it :: Integer hcprod [[], []] [] it :: [[a]] -- My two puzzles are: -- Why does -- sequence [] -- evaluate to -- [] -- and not a list with one element? -- Why is the type of the thing that -- sequence [] -- evaluates to -- [()] -- ? :q Leaving GHCi.
Process haskell finished
</blockquote>

This looks to me to be a violation of the rule that the Cartesian product of an empty list of lists is a list with one element in it.
Citation? That flies in the face of set theory. nay, even deeper than that,
i.e. fundamentally, how a product should behave.
Perhaps you've conflated it with the cardinality of the function space from
null to null, which is indeed 1: the trivial function.
Also observe that
sequence [] :: (Monad m) => m [a]
and it's only a consequence of GHCi's type defaulting [1] that it's typed
[()]. Can you trace the chain of reasoning that leads to that type? In
particular, what happens to the Monad constraint?
[1] Section 2.4.7 of
http://www.haskell.org/ghc/docs/7.4.1/html/users_guide/interactive-evaluatio...
-- Kim-Ee
On Mon, Dec 24, 2012 at 2:01 PM, Jay Sulzberger
I need the usual Cartesian product of a list of lists. In the posts
http://stackoverflow.com/**questions/4119730/cartesian-**producthttp://stackoverflow.com/questions/4119730/cartesian-product http://stackoverflow.com/**questions/3387359/calculate-n-** ary-cartesian-producthttp://stackoverflow.com/questions/3387359/calculate-n-ary-cartesian-product
it is recommended to use the sequence procedure, which is in the standard Prelude, as a "Cartesian product of a list of lists" function. That is we do have that
sequence [["a", "b"], ["c", "d"]]
evaluates to
[["a","c"],["a","d"],["b","c"]**,["b","d"]]
and often, sequence seems to give the expected answer. But we have also that, and here we quote from a GHCi session,
sequence [] [] it :: [()]
This looks to me to be a violation of the rule that the Cartesian product of an empty list of lists is a list with one element in it. It looks to be a violation because "[]" looks like a name for an empty list. But we also have
length (sequence []) 1 it :: Int
which almost reassures me.
Appended are a Scheme session and a GHCi session showing some of what puzzles me. I remember when I wrote my now standard Cartesian product Scheme procedure I was delighted that, we get
(cprd '()) (())
which gives '() as the single element of the Cartesian product of an empty list of lists.
ad Old Types vs New: Indeed every particular application of the Scheme procedure cprd should be to (some instance of) some "type" of lists of lists, and thus the one element in the output, in case the list of lists is the empty list, should be of the type of the elements, that is, the inner lists, of the input list of lists. In Haskell I do not know enough to say, but, certainly the behavior of sequence-plus-GHCi-plus-**printing-conventions was surprising to me.
Oi. Perhaps an/the object of type () just does not get printed clearly. So a list with only one object, and that one object of type (), gets printed on the page as
[]
which is a text name for the list, which is of length one, but this text name looks on the page just like the text name of an empty list.
oo--JS.
<blockquote what="Scheme session showing my standard version of Cartesian product" date="Monday 24 December 2012 01:30:37 -0500">
SCM version 5e5, Copyright (C) 1990-2006 Free Software Foundation. SCM comes with ABSOLUTELY NO WARRANTY; for details type `(terms)'. This is free software, and you are welcome to redistribute it under certain conditions; type `(terms)' for details. ;loading /usr/share/slib/require ;done loading /usr/share/slib/require.scm ;loading /usr/share/slib/require ;done loading /usr/share/slib/require.scm ;loading /usr/lib/scm/Link ;done loading /usr/lib/scm/Link.scm ;loading /usr/lib/scm/Transcen ;done loading /usr/lib/scm/Transcen.scm
(define cartesian:multiply-by-one-list
(lambda (multiplier old-product) (if (equal? multiplier '()) '() (if (equal? (cdr multiplier) '()) (map (lambda (x) (cons (car multiplier) x)) old-product) (append (map (lambda (x) (cons (car multiplier) x)) old-product) (cartesian:multiply-by-one-**list (cdr multiplier) old-product)))))) #<unspecified>
(define cartesian:product
(lambda (l) (if (equal? l '()) (list '()) (cartesian:multiply-by-one-**list (car l) (cartesian:product (cdr l)))))) #<unspecified>
(define cprd cartesian:product)
#<unspecified>
(cprd '())
(())
(cprd '(()))
()
(cprd '((a)))
((a))
(cprd '((a b)))
((a) (b))
(cprd '(() (a)))
()
(cprd '((a) ()))
()
(cprd '((a) (b)))
((a b))
(cprd '((a b) (c d)))
((a c) (a d) (b c) (b d))
(cprd '((a b) (c d) (e) (f g h)))
((a c e f) (a c e g) (a c e h) (a d e f) (a d e g) (a d e h) (b c e f) (b c e g) (b c e h) (b d e f) (b d e g) (b d e h))
(cprd '(() ()))
()
;; OK, this looks right. On lists of lists of symbols
;; cprd seems to be a version of Cartesian product. (quit) ;EXIT
Process scheme finished
</blockquote>
<blockquote what="GHCi session showing sequence and an untested version of a Haskell Cartesian product function" point="To me sequence's behavior on the list of lists [] is puzzling. sequence seems not to obey the rule that the Cartesian product of an empty list of lists is a list with one element in it, whose name/manifestation, in general, might be hard to guess." date="Monday 24 December 2012 01:32:56 -0500">
GHCi, version 7.4.1: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Prelude> :set +t Prelude> :set prompt "> "
sequence []
[] it :: [()]
sequence [[]]
[] it :: [[a]]
sequence [["a"]]
[["a"]] it :: [[[Char]]]
sequence [["a", "b"]]
[["a"],["b"]] it :: [[[Char]]]
sequence [[], ["a"]]
[] it :: [[[Char]]]
sequence [["a"], []]
[] it :: [[[Char]]]
sequence [["a"], ["b"]]
[["a","b"]] it :: [[[Char]]]
sequence [["a", "b"], ["c", "d"]]
[["a","c"],["a","d"],["b","c"]**,["b","d"]] it :: [[[Char]]]
sequence [["a", "b"], ["c", "d"], ["e"], ["f", "g", "h"]]
[["a","c","e","f"],["a","c","**e","g"],["a","c","e","h"],["a"** ,"d","e","f"],["a","d","e","g"**],["a","d","e","h"],["b","c","** e","f"],["b","c","e","g"],["b"**,"c","e","h"],["b","d","e","f"** ],["b","d","e","g"],["b","d","**e","h"]] it :: [[[Char]]]
length it
12 it :: Int
2 * 2 * 1 * 3
12 it :: Integer
sequence [[], []]
[] it :: [[a]]
-- OK, it looks as though 'sequence' as suggested in the -- Stackoverflow post -- http://stackoverflow.com/**questions/4119730/cartesian-**producthttp://stackoverflow.com/questions/4119730/cartesian-product -- in Answer 10, does most of the time compute a version -- of Cartesian product very much as does the Scheme version. -- Let us do the example in the answer: sequence [[1,2,3],[4,5,6]]
[[1,4],[1,5],[1,6],[2,4],[2,5]**,[2,6],[3,4],[3,5],[3,6]] it :: [[Integer]]
length it
9 it :: Int
3 * 3
9 it :: Integer
-- But this seems to violate the convention that -- Cartesian product, when handed an empty list of lists -- returns a list with one element. This convention -- makes the 'length' function a homomorphism from the monoid -- of lists, with cartesian product as multiplication, and the empty list -- as the identity element, to the monoid of non-negative integers -- with '*' as multiplication, and '1' as the identity element. -- Let us have a look again as what 'sequence' does: sequence []
[] it :: [()]
length it
0 it :: Int
sequence [[]]
[] it :: [[a]]
length it
0 it :: Int
length (sequence [])
1 it :: Int
:t (sequence [])
(sequence []) :: Monad m => m [a]
:t (sequence [[]])
(sequence [[]]) :: [[a]]
-- Let us try a version of our Scheme procedure 'cprd'. let hmbol multiplier oldproduct = [ x:xs | x <- multiplier, xs <- oldproduct]
hmbol :: [a] -> [[a]] -> [[a]]
hmbol [] [["a"]]
[] it :: [[[Char]]]
hmbol [] [[]]
[] it :: [[a]]
hmbol ["a"] [[]]
[["a"]] it :: [[[Char]]]
hmbol ["a", "b"] [[]]
[["a"],["b"]] it :: [[[Char]]]
hmbol ["a", "b"] []
[] it :: [[[Char]]]
hmbol ["a", "b"] [["c"]]
[["a","c"],["b","c"]] it :: [[[Char]]]
hmbol ["a", "b"] [["c"], ["d", "e"]]
[["a","c"],["a","d","e"],["b",**"c"],["b","d","e"]] it :: [[[Char]]]
hmbol ["a", "b"] [["c"], [], ["d", "e"]]
[["a","c"],["a"],["a","d","e"]**,["b","c"],["b"],["b","d","e"]**] it :: [[[Char]]]
let hcprod = foldr hmbol [[]]
hcprod :: [[a]] -> [[a]]
hcprod []
[[]] it :: [[a]]
hcprod [[]]
[] it :: [[a]]
hcprod [["a", "b"], ["c"], [], ["d", "e"]]
[] it :: [[[Char]]]
-- Let us repeat all the tests of 'cprod' in the Scheme session: hcprod []
[[]] it :: [[a]]
hcprod [[]]
[] it :: [[a]]
hcprod [["a"]]
[["a"]] it :: [[[Char]]]
hcprod [["a", "b"]]
[["a"],["b"]] it :: [[[Char]]]
hcprod [[], ["a"]]
[] it :: [[[Char]]]
hcprod [["a"], []]
[] it :: [[[Char]]]
hcprod [["a"], ["b"]]
[["a","b"]] it :: [[[Char]]]
hcprod [["a", "b"], ["c", "d"]]
[["a","c"],["a","d"],["b","c"]**,["b","d"]] it :: [[[Char]]]
hcprod [["a", "b"], ["c", "d"], ["e"], ["f", "g", "h"]]
[["a","c","e","f"],["a","c","**e","g"],["a","c","e","h"],["a"** ,"d","e","f"],["a","d","e","g"**],["a","d","e","h"],["b","c","** e","f"],["b","c","e","g"],["b"**,"c","e","h"],["b","d","e","f"** ],["b","d","e","g"],["b","d","**e","h"]] it :: [[[Char]]]
length it
12 it :: Int
2 * 2 * 1 * 3
12 it :: Integer
hcprod [[], []]
[] it :: [[a]]
-- My two puzzles are: -- Why does -- sequence [] -- evaluate to -- [] -- and not a list with one element? -- Why is the type of the thing that -- sequence [] -- evaluates to -- [()] -- ? :q
Leaving GHCi.
Process haskell finished
</blockquote>
______________________________**_________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/**mailman/listinfo/beginnershttp://www.haskell.org/mailman/listinfo/beginners

On Mon, Dec 24, 2012 at 8:01 AM, Jay Sulzberger
sequence [] [] it :: [()]
This looks to me to be a violation of the rule that the Cartesian product of an empty list of lists is a list with one element in it. It looks to be a violation because "[]" looks like a name for an empty list. But we also have
length (sequence []) 1 it :: Int
which almost reassures me.
Well the type of the first response is a dead give-away : the result is of type [()] but for a cartesian n-product, you would like [[a]] (with a maybe instantiated to a concrete type) ... What's happening here is that sequence is not "the cartesian n-product" in general, it is only that in the list monad but in "sequence []" there's nothing to indicate that we're in the list monad, so GHC default to the IO monad and unit () so sequence has the type "[IO ()] -> IO [()]" and there's no IO action in the list parameter, so there's nothing in the result list. Try :
sequence [] :: [[Int]] and you should be reassured.
-- Jedaï

the result is of type [()] but for a cartesian n-product, you would like [[a]]
Right. So what we have here is a product over a count of 0 sets, which is isomorphic to the function space that has the null set as domain. The latter has exactly one element: the trivial function. My apologies for misreading what OP wrote:
This looks to me to be a violation of the rule that the Cartesian product of an empty list of lists is a list with one element in it.
I thought he meant something along the lines of sequence ["","x"].
-- Kim-Ee
On Mon, Dec 24, 2012 at 4:42 PM, Chaddaï Fouché
On Mon, Dec 24, 2012 at 8:01 AM, Jay Sulzberger
wrote: sequence [] [] it :: [()]
This looks to me to be a violation of the rule that the Cartesian product of an empty list of lists is a list with one element in it. It looks to be a violation because "[]" looks like a name for an empty list. But we also have
length (sequence []) 1 it :: Int
which almost reassures me.
Well the type of the first response is a dead give-away : the result is of type [()] but for a cartesian n-product, you would like [[a]] (with a maybe instantiated to a concrete type) ... What's happening here is that sequence is not "the cartesian n-product" in general, it is only that in the list monad but in "sequence []" there's nothing to indicate that we're in the list monad, so GHC default to the IO monad and unit () so sequence has the type "[IO ()] -> IO [()]" and there's no IO action in the list parameter, so there's nothing in the result list.
Try :
sequence [] :: [[Int]] and you should be reassured.
-- Jedaï
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Mon, 24 Dec 2012, Kim-Ee Yeoh
the result is of type [()] but for a cartesian n-product, you would like [[a]]
Right. So what we have here is a product over a count of 0 sets, which is isomorphic to the function space that has the null set as domain. The latter has exactly one element: the trivial function.
My apologies for misreading what OP wrote:
This looks to me to be a violation of the rule that the Cartesian product of an empty list of lists is a list with one element in it.
I thought he meant something along the lines of sequence ["","x"].
-- Kim-Ee
Thanks, Kim-Ee! Your answer and also Chaddai's are very helpful. I hope to post more in this thread. oo--JS.
On Mon, Dec 24, 2012 at 4:42 PM, Chadda� Fouch�
wrote: On Mon, Dec 24, 2012 at 8:01 AM, Jay Sulzberger
wrote: sequence [] [] it :: [()]
This looks to me to be a violation of the rule that the Cartesian product of an empty list of lists is a list with one element in it. It looks to be a violation because "[]" looks like a name for an empty list. But we also have
length (sequence []) 1 it :: Int
which almost reassures me.
Well the type of the first response is a dead give-away : the result is of type [()] but for a cartesian n-product, you would like [[a]] (with a maybe instantiated to a concrete type) ... What's happening here is that sequence is not "the cartesian n-product" in general, it is only that in the list monad but in "sequence []" there's nothing to indicate that we're in the list monad, so GHC default to the IO monad and unit () so sequence has the type "[IO ()] -> IO [()]" and there's no IO action in the list parameter, so there's nothing in the result list.
Try :
sequence [] :: [[Int]] and you should be reassured.
-- Jeda�
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Mon, 24 Dec 2012, Jay Sulzberger wrote:
On Mon, 24 Dec 2012, Kim-Ee Yeoh
wrote: the result is of type [()] but for a cartesian n-product, you would like [[a]]
Right. So what we have here is a product over a count of 0 sets, which is isomorphic to the function space that has the null set as domain. The latter has exactly one element: the trivial function.
My apologies for misreading what OP wrote:
This looks to me to be a violation of the rule that the Cartesian product of an empty list of lists is a list with one element in it.
I thought he meant something along the lines of sequence ["","x"].
-- Kim-Ee
Thanks, Kim-Ee!
Your answer and also Chaddai's are very helpful.
I hope to post more in this thread.
oo--JS.
Kim-Ee, I started to write a response answering your first post in this thread, but then I saw your second. My intended answer started by defining some old classic functions, such as binary append of lists, binary cartesian product of lists, binary + of non-negative integers, binary * of non-negative integers, and extending these four binary operations by using fold to get the usual n-ary extensions of these four operations. Then I started to list the most well known identities amongst the four operations, and the four extended operations, including identities using various functors defined using the fundamental map card: FSets -> NNIntegers, where FSets is the collection of finite sets, ah, this should be length: FLists -> NNIntegers where FLists is the collection of finite lists and NNIntegers is the set of non-negative integers. This morning, I realized I was headed for a wonderful old problem, which we now see as a question in New Crazy Types theory: Tarski's High School Algebra Problem. Here is some stuff on the problem: http://en.wikipedia.org/wiki/Tarski%27s_high_school_algebra_problem [page was last modified on 3 May 2012 at 07:37] A note by John Baez which also gives some references: http://math.ucr.edu/home/baez/week172.html A paper by Roberto Di Cosmo and Thomas Dufour which proves that Tarski High School Algebra is decidable but not finitely axiomatizable: http://www.pps.univ-paris-diderot.fr/~dufour/zeronfa.pdf Some notes from 1999 by R. Di Cosmo, with pointer to his book on the problem: http://www.dicosmo.org/Tarski/Tarski.html A note by Thorsten Altenkirch on one of the dependent types case: http://www.cs.nott.ac.uk/~txa/publ/unialg.pdf R. Gurevic's 1985 paper, which is available for free: http://www.ams.org/journals/proc/1985-094-01/S0002-9939-1985-0781071-1/ A note by S. N. Burris and K. A. Yeats on the history of the problem: math.sfu.ca/~kya17/papers/saga_paper4.pdf and here are some things on zero inputs in the lambda calculus, one by Todd Trimble, one by John Baez, and one by Peter Selinger: http://math.ucr.edu/home/baez/trimble/holodeck.html http://math.ucr.edu/home/baez/week240.html http://arxiv.org/pdf/1207.6972 oo--JS.
On Mon, Dec 24, 2012 at 4:42 PM, Chadda� Fouch�
wrote: On Mon, Dec 24, 2012 at 8:01 AM, Jay Sulzberger
wrote: sequence [] [] it :: [()]
This looks to me to be a violation of the rule that the Cartesian product of an empty list of lists is a list with one element in it. It looks to be a violation because "[]" looks like a name for an empty list. But we also have
length (sequence []) 1 it :: Int
which almost reassures me.
Well the type of the first response is a dead give-away : the result is of type [()] but for a cartesian n-product, you would like [[a]] (with a maybe instantiated to a concrete type) ... What's happening here is that sequence is not "the cartesian n-product" in general, it is only that in the list monad but in "sequence []" there's nothing to indicate that we're in the list monad, so GHC default to the IO monad and unit () so sequence has the type "[IO ()] -> IO [()]" and there's no IO action in the list parameter, so there's nothing in the result list.
Try :
sequence [] :: [[Int]] and you should be reassured.
-- Jeda�
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Jay, I appreciate your references. I'm not sure whether you're asking a
question this time around.
I've skimmed some of Di Cosmo's work, and I particularly liked the type
isomorphism narrative as applied to a queryable database of functions.
Anyone who has used Hayoo or Hoogle enough encounters the problem of
non-canonicity of type signatures. Which of the following isomorphic
signatures should we search for
(a, Either a b) -> (s,c)
((a,a)->(s,c), (a,b)->(s,c))
(a->a->(s,c), a->b->(s,c))
(a->a->s, a->a->c, a->b->(s,c))
...
?
Cue Tarski's HSA problem.
-- Kim-Ee
On Wed, Dec 26, 2012 at 5:56 AM, Jay Sulzberger
On Mon, 24 Dec 2012, Jay Sulzberger wrote:
On Mon, 24 Dec 2012, Kim-Ee Yeoh
wrote: the result is of type [()] but for a cartesian n-product, you would like
[[a]]
Right. So what we have here is a product over a count of 0 sets, which is isomorphic to the function space that has the null set as domain. The latter has exactly one element: the trivial function.
My apologies for misreading what OP wrote:
This looks to me to be a violation of the rule that the Cartesian
product of an empty list of lists is a list with one element in it.
I thought he meant something along the lines of sequence ["","x"].
-- Kim-Ee
Thanks, Kim-Ee!
Your answer and also Chaddai's are very helpful.
I hope to post more in this thread.
oo--JS.
Kim-Ee, I started to write a response answering your first post in this thread, but then I saw your second. My intended answer started by defining some old classic functions, such as binary append of lists, binary cartesian product of lists, binary + of non-negative integers, binary * of non-negative integers, and extending these four binary operations by using fold to get the usual n-ary extensions of these four operations. Then I started to list the most well known identities amongst the four operations, and the four extended operations, including identities using various functors defined using the fundamental map card: FSets -> NNIntegers, where FSets is the collection of finite sets, ah, this should be length: FLists -> NNIntegers where FLists is the collection of finite lists and NNIntegers is the set of non-negative integers. This morning, I realized I was headed for a wonderful old problem, which we now see as a question in New Crazy Types theory: Tarski's High School Algebra Problem. Here is some stuff on the problem:
http://en.wikipedia.org/wiki/**Tarski%27s_high_school_**algebra_problemhttp://en.wikipedia.org/wiki/Tarski%27s_high_school_algebra_problem [page was last modified on 3 May 2012 at 07:37]
A note by John Baez which also gives some references: http://math.ucr.edu/home/baez/**week172.htmlhttp://math.ucr.edu/home/baez/week172.html
A paper by Roberto Di Cosmo and Thomas Dufour which proves that Tarski High School Algebra is decidable but not finitely axiomatizable: http://www.pps.univ-paris-**diderot.fr/~dufour/zeronfa.pdfhttp://www.pps.univ-paris-diderot.fr/~dufour/zeronfa.pdf
Some notes from 1999 by R. Di Cosmo, with pointer to his book on the problem: http://www.dicosmo.org/Tarski/**Tarski.htmlhttp://www.dicosmo.org/Tarski/Tarski.html
A note by Thorsten Altenkirch on one of the dependent types case: http://www.cs.nott.ac.uk/~txa/**publ/unialg.pdfhttp://www.cs.nott.ac.uk/~txa/publ/unialg.pdf
R. Gurevic's 1985 paper, which is available for free: http://www.ams.org/journals/**proc/1985-094-01/S0002-9939-** 1985-0781071-1/http://www.ams.org/journals/proc/1985-094-01/S0002-9939-1985-0781071-1/
A note by S. N. Burris and K. A. Yeats on the history of the problem: math.sfu.ca/~kya17/papers/**saga_paper4.pdfhttp://math.sfu.ca/~kya17/papers/saga_paper4.pdf
and here are some things on zero inputs in the lambda calculus, one by Todd Trimble, one by John Baez, and one by Peter Selinger:
http://math.ucr.edu/home/baez/**trimble/holodeck.htmlhttp://math.ucr.edu/home/baez/trimble/holodeck.html
http://math.ucr.edu/home/baez/**week240.htmlhttp://math.ucr.edu/home/baez/week240.html
http://arxiv.org/pdf/1207.6972
oo--JS.
On Mon, Dec 24, 2012 at 4:42 PM, Chaddaï Fouché < chaddai.fouche@gmail.com>**wrote:
On Mon, Dec 24, 2012 at 8:01 AM, Jay Sulzberger
wrote: sequence [] [] it :: [()]
This looks to me to be a violation of the rule that the Cartesian product of an empty list of lists is a list with one element in it. It looks to be a violation because "[]" looks like a name for an empty list. But we also have
length (sequence []) 1 it :: Int
which almost reassures me.
Well the type of the first response is a dead give-away : the result is of type [()] but for a cartesian n-product, you would like [[a]] (with a maybe instantiated to a concrete type) ... What's happening here is that sequence is not "the cartesian n-product" in general, it is only that in the list monad but in "sequence []" there's nothing to indicate that we're in the list monad, so GHC default to the IO monad and unit () so sequence has the type "[IO ()] -> IO [()]" and there's no IO action in the list parameter, so there's nothing in the result list.
Try :
sequence [] :: [[Int]]
and you should be reassured.
-- Jedaï
______________________________**_________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/**mailman/listinfo/beginnershttp://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Wed, 26 Dec 2012, Kim-Ee Yeoh
Jay, I appreciate your references. I'm not sure whether you're asking a question this time around.
Thanks, Kim-Ee! I was not directly asking a question in the post.
I've skimmed some of Di Cosmo's work, and I particularly liked the type isomorphism narrative as applied to a queryable database of functions.
Yes. This is not stupid, and I had been thinking a bit about such a thing before I saw Di Cosmo's stuff. There was last semester a course at Columbia on "Big Data". I never attended any of it but read Cathy O'Neil's blog posts about it at http://mathbabe.org I thought that a good project for the class would be to attempt to make a search engine for code. Now I'd want the search engine to be able to classify two programs in two different programming systems, say Basic and Scheme, or C and Haskell, as "being the same program", even if their source code does not use the same names for things. Of course, the general principle that gross human language word similarities gets you a lot, will hold here too. The interesting case would be if we stripped away all comment, and surrounding text, and then tried to do the grouping/indexing. Note that this does not, at least at first, look like the sort of thing one does in, ah, econometrics, nor in most computer helped "market research". I know that there has been some work on the problem.
Anyone who has used Hayoo or Hoogle enough encounters the problem of non-canonicity of type signatures. Which of the following isomorphic signatures should we search for
(a, Either a b) -> (s,c) ((a,a)->(s,c), (a,b)->(s,c)) (a->a->(s,c), a->b->(s,c)) (a->a->s, a->a->c, a->b->(s,c))
I will have to read this carefully, but I know the general problem ;)
...
?
Cue Tarski's HSA problem.
Yes.
-- Kim-Ee
I will attempt to post more, and this time I will put some elementary, very elementary, Haskell code in, showing what GHCi tells me about sequence just as you and Chaddai have said to do. oo--JS.
On Wed, Dec 26, 2012 at 5:56 AM, Jay Sulzberger
wrote: On Mon, 24 Dec 2012, Jay Sulzberger wrote:
On Mon, 24 Dec 2012, Kim-Ee Yeoh
wrote: the result is of type [()] but for a cartesian n-product, you would like
[[a]]
Right. So what we have here is a product over a count of 0 sets, which is isomorphic to the function space that has the null set as domain. The latter has exactly one element: the trivial function.
My apologies for misreading what OP wrote:
This looks to me to be a violation of the rule that the Cartesian
product of an empty list of lists is a list with one element in it.
I thought he meant something along the lines of sequence ["","x"].
-- Kim-Ee
Thanks, Kim-Ee!
Your answer and also Chaddai's are very helpful.
I hope to post more in this thread.
oo--JS.
Kim-Ee, I started to write a response answering your first post in this thread, but then I saw your second. My intended answer started by defining some old classic functions, such as binary append of lists, binary cartesian product of lists, binary + of non-negative integers, binary * of non-negative integers, and extending these four binary operations by using fold to get the usual n-ary extensions of these four operations. Then I started to list the most well known identities amongst the four operations, and the four extended operations, including identities using various functors defined using the fundamental map card: FSets -> NNIntegers, where FSets is the collection of finite sets, ah, this should be length: FLists -> NNIntegers where FLists is the collection of finite lists and NNIntegers is the set of non-negative integers. This morning, I realized I was headed for a wonderful old problem, which we now see as a question in New Crazy Types theory: Tarski's High School Algebra Problem. Here is some stuff on the problem:
http://en.wikipedia.org/wiki/**Tarski%27s_high_school_**algebra_problemhttp://en.wikipedia.org/wiki/Tarski%27s_high_school_algebra_problem [page was last modified on 3 May 2012 at 07:37]
A note by John Baez which also gives some references: http://math.ucr.edu/home/baez/**week172.htmlhttp://math.ucr.edu/home/baez/week172.html
A paper by Roberto Di Cosmo and Thomas Dufour which proves that Tarski High School Algebra is decidable but not finitely axiomatizable: http://www.pps.univ-paris-**diderot.fr/~dufour/zeronfa.pdfhttp://www.pps.univ-paris-diderot.fr/~dufour/zeronfa.pdf
Some notes from 1999 by R. Di Cosmo, with pointer to his book on the problem: http://www.dicosmo.org/Tarski/**Tarski.htmlhttp://www.dicosmo.org/Tarski/Tarski.html
A note by Thorsten Altenkirch on one of the dependent types case: http://www.cs.nott.ac.uk/~txa/**publ/unialg.pdfhttp://www.cs.nott.ac.uk/~txa/publ/unialg.pdf
R. Gurevic's 1985 paper, which is available for free: http://www.ams.org/journals/**proc/1985-094-01/S0002-9939-** 1985-0781071-1/http://www.ams.org/journals/proc/1985-094-01/S0002-9939-1985-0781071-1/
A note by S. N. Burris and K. A. Yeats on the history of the problem: math.sfu.ca/~kya17/papers/**saga_paper4.pdfhttp://math.sfu.ca/~kya17/papers/saga_paper4.pdf
and here are some things on zero inputs in the lambda calculus, one by Todd Trimble, one by John Baez, and one by Peter Selinger:
http://math.ucr.edu/home/baez/**trimble/holodeck.htmlhttp://math.ucr.edu/home/baez/trimble/holodeck.html
http://math.ucr.edu/home/baez/**week240.htmlhttp://math.ucr.edu/home/baez/week240.html
http://arxiv.org/pdf/1207.6972
oo--JS.
On Mon, Dec 24, 2012 at 4:42 PM, Chadda� Fouch� < chaddai.fouche@gmail.com>**wrote:
On Mon, Dec 24, 2012 at 8:01 AM, Jay Sulzberger
wrote: > sequence [] [] it :: [()]
This looks to me to be a violation of the rule that the Cartesian product of an empty list of lists is a list with one element in it. It looks to be a violation because "[]" looks like a name for an empty list. But we also have
> length (sequence []) 1 it :: Int
which almost reassures me.
Well the type of the first response is a dead give-away : the result is of type [()] but for a cartesian n-product, you would like [[a]] (with a maybe instantiated to a concrete type) ... What's happening here is that sequence is not "the cartesian n-product" in general, it is only that in the list monad but in "sequence []" there's nothing to indicate that we're in the list monad, so GHC default to the IO monad and unit () so sequence has the type "[IO ()] -> IO [()]" and there's no IO action in the list parameter, so there's nothing in the result list.
Try :
sequence [] :: [[Int]]
and you should be reassured.
-- Jeda�
______________________________**_________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/**mailman/listinfo/beginnershttp://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Wed, 26 Dec 2012, Jay Sulzberger wrote:
On Wed, 26 Dec 2012, Kim-Ee Yeoh
wrote: Jay, I appreciate your references. I'm not sure whether you're asking a question this time around.
Thanks, Kim-Ee!
I was not directly asking a question in the post.
I've skimmed some of Di Cosmo's work, and I particularly liked the type isomorphism narrative as applied to a queryable database of functions.
Yes. This is not stupid, and I had been thinking a bit about such a thing before I saw Di Cosmo's stuff.
There was last semester a course at Columbia on "Big Data". I never attended any of it but read Cathy O'Neil's blog posts about it at
I thought that a good project for the class would be to attempt to make a search engine for code. Now I'd want the search engine to be able to classify two programs in two different programming systems, say Basic and Scheme, or C and Haskell, as "being the same program", even if their source code does not use the same names for things. Of course, the general principle that gross human language word similarities gets you a lot, will hold here too. The interesting case would be if we stripped away all comment, and surrounding text, and then tried to do the grouping/indexing. Note that this does not, at least at first, look like the sort of thing one does in, ah, econometrics, nor in most computer helped "market research". I know that there has been some work on the problem.
Anyone who has used Hayoo or Hoogle enough encounters the problem of non-canonicity of type signatures. Which of the following isomorphic signatures should we search for
(a, Either a b) -> (s,c) ((a,a)->(s,c), (a,b)->(s,c)) (a->a->(s,c), a->b->(s,c)) (a->a->s, a->a->c, a->b->(s,c))
I will have to read this carefully, but I know the general problem ;)
...
?
Cue Tarski's HSA problem.
Yes.
To lay out explicitly what you already know: The type similarity problem and the code similarity problem are alike, and one statistical (partial) solution is to run the two types/programs on the same inputs. In the case of types, this means, as you say, substituting numbers for types, replacing Cartesian product by multiplication, etc.., and then evaluate the values of the two Card(type)s at various numbers. In the case of code, we'd just run the two different programs on the same data, and see if we got different results. Of course sometimes we'd like to time, or take the temperature of the computer, for the runnings of the two programs, because there are programs that are different, but have the same input output behavior. I will not post more tonight. oo--JS.
-- Kim-Ee
I will attempt to post more, and this time I will put some elementary, very elementary, Haskell code in, showing what GHCi tells me about sequence just as you and Chaddai have said to do.
oo--JS.
On Wed, Dec 26, 2012 at 5:56 AM, Jay Sulzberger
wrote: On Mon, 24 Dec 2012, Jay Sulzberger wrote:
On Mon, 24 Dec 2012, Kim-Ee Yeoh
wrote: the result is of type [()] but for a cartesian n-product, you would like
[[a]]
Right. So what we have here is a product over a count of 0 sets, which is isomorphic to the function space that has the null set as domain. The latter has exactly one element: the trivial function.
My apologies for misreading what OP wrote:
This looks to me to be a violation of the rule that the Cartesian
product of an empty list of lists is a list with one element in it.
I thought he meant something along the lines of sequence ["","x"].
-- Kim-Ee
Thanks, Kim-Ee!
Your answer and also Chaddai's are very helpful.
I hope to post more in this thread.
oo--JS.
Kim-Ee, I started to write a response answering your first post in this thread, but then I saw your second. My intended answer started by defining some old classic functions, such as binary append of lists, binary cartesian product of lists, binary + of non-negative integers, binary * of non-negative integers, and extending these four binary operations by using fold to get the usual n-ary extensions of these four operations. Then I started to list the most well known identities amongst the four operations, and the four extended operations, including identities using various functors defined using the fundamental map card: FSets -> NNIntegers, where FSets is the collection of finite sets, ah, this should be length: FLists -> NNIntegers where FLists is the collection of finite lists and NNIntegers is the set of non-negative integers. This morning, I realized I was headed for a wonderful old problem, which we now see as a question in New Crazy Types theory: Tarski's High School Algebra Problem. Here is some stuff on the problem:
http://en.wikipedia.org/wiki/**Tarski%27s_high_school_**algebra_problemhttp://en.wikipedia.org/wiki/Tarski%27s_high_school_algebra_problem [page was last modified on 3 May 2012 at 07:37]
A note by John Baez which also gives some references: http://math.ucr.edu/home/baez/**week172.htmlhttp://math.ucr.edu/home/baez/week172.html
A paper by Roberto Di Cosmo and Thomas Dufour which proves that Tarski High School Algebra is decidable but not finitely axiomatizable: http://www.pps.univ-paris-**diderot.fr/~dufour/zeronfa.pdfhttp://www.pps.univ-paris-diderot.fr/~dufour/zeronfa.pdf
Some notes from 1999 by R. Di Cosmo, with pointer to his book on the problem: http://www.dicosmo.org/Tarski/**Tarski.htmlhttp://www.dicosmo.org/Tarski/Tarski.html
A note by Thorsten Altenkirch on one of the dependent types case: http://www.cs.nott.ac.uk/~txa/**publ/unialg.pdfhttp://www.cs.nott.ac.uk/~txa/publ/unialg.pdf
R. Gurevic's 1985 paper, which is available for free: http://www.ams.org/journals/**proc/1985-094-01/S0002-9939-** 1985-0781071-1/http://www.ams.org/journals/proc/1985-094-01/S0002-9939-1985-0781071-1/
A note by S. N. Burris and K. A. Yeats on the history of the problem: math.sfu.ca/~kya17/papers/**saga_paper4.pdfhttp://math.sfu.ca/~kya17/papers/saga_paper4.pdf
and here are some things on zero inputs in the lambda calculus, one by Todd Trimble, one by John Baez, and one by Peter Selinger:
http://math.ucr.edu/home/baez/**trimble/holodeck.htmlhttp://math.ucr.edu/home/baez/trimble/holodeck.html
http://math.ucr.edu/home/baez/**week240.htmlhttp://math.ucr.edu/home/baez/week240.html
http://arxiv.org/pdf/1207.6972
oo--JS.
On Mon, Dec 24, 2012 at 4:42 PM, Chadda� Fouch� < chaddai.fouche@gmail.com>**wrote:
On Mon, Dec 24, 2012 at 8:01 AM, Jay Sulzberger
wrote: > > > sequence [] > [] > it :: [()] > > This looks to me to be a violation of the rule that the Cartesian > product of an empty list of lists is a list with one element in > it. It looks to be a violation because "[]" looks like a name > for an empty list. But we also have > > > length (sequence []) > 1 > it :: Int > > which almost reassures me. > > Well the type of the first response is a dead give-away : the result is of type [()] but for a cartesian n-product, you would like [[a]] (with a maybe instantiated to a concrete type) ... What's happening here is that sequence is not "the cartesian n-product" in general, it is only that in the list monad but in "sequence []" there's nothing to indicate that we're in the list monad, so GHC default to the IO monad and unit () so sequence has the type "[IO ()] -> IO [()]" and there's no IO action in the list parameter, so there's nothing in the result list.
Try :
> sequence [] :: [[Int]] > and you should be reassured.
-- Jeda�
______________________________**_________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/**mailman/listinfo/beginnershttp://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Mon, 24 Dec 2012, Chaddaï Fouché
On Mon, Dec 24, 2012 at 8:01 AM, Jay Sulzberger
wrote: sequence [] [] it :: [()]
This looks to me to be a violation of the rule that the Cartesian product of an empty list of lists is a list with one element in it. It looks to be a violation because "[]" looks like a name for an empty list. But we also have
length (sequence []) 1 it :: Int
which almost reassures me.
Well the type of the first response is a dead give-away : the result is of type [()] but for a cartesian n-product, you would like [[a]] (with a maybe instantiated to a concrete type) ... What's happening here is that sequence is not "the cartesian n-product" in general, it is only that in the list monad but in "sequence []" there's nothing to indicate that we're in the list monad, so GHC default to the IO monad and unit () so sequence has the type "[IO ()] -> IO [()]" and there's no IO action in the list parameter, so there's nothing in the result list.
Try :
sequence [] :: [[Int]] and you should be reassured.
-- Jedaï
Thanks, Chaddai Fouche! I hope to post more in this thread. oo--JS.
participants (3)
-
Chaddaï Fouché
-
Jay Sulzberger
-
Kim-Ee Yeoh