1(* ========================================================================= *)
2(* A POSSIBLY-INFINITE STREAM DATATYPE FOR ML                                *)
3(* Copyright (c) 2001 Joe Hurd, distributed under the BSD License            *)
4(* ========================================================================= *)
5
6signature Stream =
7sig
8
9(* ------------------------------------------------------------------------- *)
10(* The stream type.                                                          *)
11(* ------------------------------------------------------------------------- *)
12
13datatype 'a stream = Nil | Cons of 'a * (unit -> 'a stream)
14
15(* If you're wondering how to create an infinite stream: *)
16(* val stream4 = let fun s4 () = Stream.Cons (4,s4) in s4 () end; *)
17
18(* ------------------------------------------------------------------------- *)
19(* Stream constructors.                                                      *)
20(* ------------------------------------------------------------------------- *)
21
22val repeat : 'a -> 'a stream
23
24val count : int -> int stream
25
26val funpows : ('a -> 'a) -> 'a -> 'a stream
27
28(* ------------------------------------------------------------------------- *)
29(* Stream versions of standard list operations: these should all terminate.  *)
30(* ------------------------------------------------------------------------- *)
31
32val cons : 'a -> (unit -> 'a stream) -> 'a stream
33
34val null : 'a stream -> bool
35
36val hd : 'a stream -> 'a  (* raises Empty *)
37
38val tl : 'a stream -> 'a stream  (* raises Empty *)
39
40val hdTl : 'a stream -> 'a * 'a stream  (* raises Empty *)
41
42val singleton : 'a -> 'a stream
43
44val append : 'a stream -> (unit -> 'a stream) -> 'a stream
45
46val map : ('a -> 'b) -> 'a stream -> 'b stream
47
48val maps :
49    ('a -> 's -> 'b * 's) -> ('s -> 'b stream) -> 's -> 'a stream -> 'b stream
50
51val zipwith : ('a -> 'b -> 'c) -> 'a stream -> 'b stream -> 'c stream
52
53val zip : 'a stream -> 'b stream -> ('a * 'b) stream
54
55val take : int -> 'a stream -> 'a stream  (* raises Subscript *)
56
57val drop : int -> 'a stream -> 'a stream  (* raises Subscript *)
58
59(* ------------------------------------------------------------------------- *)
60(* Stream versions of standard list operations: these might not terminate.   *)
61(* ------------------------------------------------------------------------- *)
62
63val length : 'a stream -> int
64
65val exists : ('a -> bool) -> 'a stream -> bool
66
67val all : ('a -> bool) -> 'a stream -> bool
68
69val filter : ('a -> bool) -> 'a stream -> 'a stream
70
71val foldl : ('a * 's -> 's) -> 's -> 'a stream -> 's
72
73val concat : 'a stream stream -> 'a stream
74
75val mapPartial : ('a -> 'b option) -> 'a stream -> 'b stream
76
77val mapsPartial :
78    ('a -> 's -> 'b option * 's) -> ('s -> 'b stream) -> 's ->
79    'a stream -> 'b stream
80
81val mapConcat : ('a -> 'b stream) -> 'a stream -> 'b stream
82
83val mapsConcat :
84    ('a -> 's -> 'b stream * 's) -> ('s -> 'b stream) -> 's ->
85    'a stream -> 'b stream
86
87(* ------------------------------------------------------------------------- *)
88(* Stream operations.                                                        *)
89(* ------------------------------------------------------------------------- *)
90
91val memoize : 'a stream -> 'a stream
92
93val listConcat : 'a list stream -> 'a stream
94
95val concatList : 'a stream list -> 'a stream
96
97val toList : 'a stream -> 'a list
98
99val fromList : 'a list -> 'a stream
100
101val toString : char stream -> string
102
103val fromString : string -> char stream
104
105val toTextFile : {filename : string} -> string stream -> unit
106
107val fromTextFile : {filename : string} -> string stream  (* line by line *)
108
109end
110