2 Implementation of optimally compiled list comprehensions using Wadler's algorithm from
3 Peyton-Jones "Implementation of Functional Programming Languages", 1987
5 TQ transforms a list of qualifiers (either boolean expressions or generators) into a
6 single expression which implements the list comprehension.
8 TE << [E || Q] >> = TQ << [E || Q] ++ [] >>
10 TQ << [E || p <- L1, Q] ++ L2 >> =
12 h ( TE << L1 >> ) where
16 (TE << p >> -> ( TQ << [E || Q] ++ (h us') >> )) u
25 if(tlist(quals) == lnil)
26 return(mkcons(zfexpr,l2));
28 qualh = (tree) lhd(quals);
31 if(ttree(qualh) != qual)
32 return(mkif(qualh,TQ(rest,l2),l2));
35 tree h = mkident(uniqueident("Zh%d")),
36 u = mkident(uniqueident("Iu%d")),
37 us = mkident(uniqueident("Ius%d")),
40 pbinding tq = mkppat(gqpat(qualh),TQ(rest,mkap(h,us)));
57 replaces the following code which elides patterns in list comprehensions a la M*****a
60 ttree(pat) == ident && !isconstr(gident(pat))?
62 ldub(tq,mkppat(mkident("_"),mkap(h,us))))
65 mkap(h,gqexp(qualh))));