--* From PETEB%WATSON.vnet.ibm.com@yktvmh.watson.ibm.com  Tue Apr 26 23:08:38 1994
--* Received: from yktvmh-3172.watson.ibm.com by leonardo.watson.ibm.com (AIX 3.2/UCB 5.64/920123)
--*           id AA27376; Tue, 26 Apr 1994 23:08:38 -0400
--* Received: from watson.vnet.ibm.com by yktvmh.watson.ibm.com (IBM VM SMTP V2R3)
--*    with BSMTP id 8976; Tue, 26 Apr 94 23:08:02 EDT
--* Received: from YKTVMH by watson.vnet.ibm.com with "VAGENT.V1.0"
--*           id <A.PETEB.NOTE.VAGENT2.3083.Apr.26.23:08:01.-0400>
--*           for asbugs@watson; Tue, 26 Apr 94 23:08:02 -0400
--* Received: from YKTVMH by watson.vnet.ibm.com with "VAGENT.V1.0"
--*           id 3081; Tue, 26 Apr 1994 23:08:01 EDT
--* Received: from cyst.watson.ibm.com by yktvmh.watson.ibm.com (IBM VM SMTP V2R3)
--*    with TCP; Tue, 26 Apr 94 23:08:01 EDT
--* Received: from radical.watson.ibm.com by cyst.watson.ibm.com (AIX 3.2/UCB 5.64/900528)
--*   id AA58212; Tue, 26 Apr 1994 23:04:02 -0400
--* Received: by radical.watson.ibm.com (AIX 3.2/UCB 5.64/900524)
--*           id AA35500; Tue, 26 Apr 1994 23:03:38 -0400
--* Date: Tue, 26 Apr 1994 23:03:38 -0400
--* From: pab@radical.watson.ibm.com
--* X-External-Networks: yes
--* Message-Id: <9404270303.AA35500@radical.watson.ibm.com>
--* To: asbugs@watson.ibm.com
--* Subject: [7] Confused type inference for empty generators [genops.as][v34.6]

--@ Fixed  by:  PAB   Thu May 11 11:46:42 EDT 1995 
--@ Tested by:  none 
--@ Summary:    Type inference for empty generators now working. 


#include "aslib.as"

-- Program dies in tpossFree while looking at sieve.

GEN ==> Generator X;

GeneratorOps(X: Type): with {
	map: (X -> X, GEN)-> GEN;
	filter: ( X->Boolean, GEN) -> GEN;
	concat: (GEN, GEN) -> GEN;
	combine: ( (X, X) -> X, GEN, GEN) -> GEN
}
== add {
	map(f: X->X, g: GEN): GEN ==
		generate for x in g repeat yield f x;

	filter(f: X->Boolean, g: GEN): GEN == {
		generate
			for x in g repeat
				if f x then yield x;
	}

	concat(g1: GEN, g2: GEN): GEN == {
		generate {
			for x in g1 repeat yield x;
			for x in g2 repeat yield x;
		}
	}
	
	combine(f: (X, X) -> X, g1: GEN, g2: GEN): GEN == {
		generate for x in g1
			 for y in g2 repeat
				yield f(x, y);
	}

}

--- candidate for Daftest Program for Printing Prime Numbers Award

I    ==> SingleInteger
IGEN ==> Generator I
import from GeneratorOps I
import from Generator I

numFilter(n: I, G: IGEN): IGEN == filter( (m: I): Boolean +-> not zero? (m mod n), G);

sieve(G: IGEN): IGEN == {
	step! G;
	empty? G => generate {};
	next := value G;	
	concat (generate yield next,
		i for i in sieve numFilter(next, G))
}

T1(): () == for n in sieve generator(2..200) repeat
		print(n)();

T1();
 
