author | desharna |
Thu, 09 Jul 2020 11:39:16 +0200 | |
changeset 72004 | 913162a47d9f |
parent 45778 | df6e210fb44c |
permissions | -rw-r--r-- |
39348 | 1 |
(* ========================================================================= *) |
2 |
(* A POSSIBLY-INFINITE STREAM DATATYPE FOR ML *) |
|
72004 | 3 |
(* Copyright (c) 2001 Joe Leslie-Hurd, distributed under the BSD License *) |
39348 | 4 |
(* ========================================================================= *) |
5 |
||
6 |
structure Stream :> Stream = |
|
7 |
struct |
|
8 |
||
9 |
val K = Useful.K; |
|
10 |
||
11 |
val pair = Useful.pair; |
|
12 |
||
13 |
val funpow = Useful.funpow; |
|
14 |
||
15 |
(* ------------------------------------------------------------------------- *) |
|
16 |
(* The stream type. *) |
|
17 |
(* ------------------------------------------------------------------------- *) |
|
18 |
||
19 |
datatype 'a stream = |
|
20 |
Nil |
|
21 |
| Cons of 'a * (unit -> 'a stream); |
|
22 |
||
23 |
(* ------------------------------------------------------------------------- *) |
|
24 |
(* Stream constructors. *) |
|
25 |
(* ------------------------------------------------------------------------- *) |
|
26 |
||
27 |
fun repeat x = let fun rep () = Cons (x,rep) in rep () end; |
|
28 |
||
29 |
fun count n = Cons (n, fn () => count (n + 1)); |
|
30 |
||
31 |
fun funpows f x = Cons (x, fn () => funpows f (f x)); |
|
32 |
||
33 |
(* ------------------------------------------------------------------------- *) |
|
34 |
(* Stream versions of standard list operations: these should all terminate. *) |
|
35 |
(* ------------------------------------------------------------------------- *) |
|
36 |
||
37 |
fun cons h t = Cons (h,t); |
|
38 |
||
39 |
fun null Nil = true |
|
40 |
| null (Cons _) = false; |
|
41 |
||
42 |
fun hd Nil = raise Empty |
|
43 |
| hd (Cons (h,_)) = h; |
|
44 |
||
45 |
fun tl Nil = raise Empty |
|
46 |
| tl (Cons (_,t)) = t (); |
|
47 |
||
48 |
fun hdTl s = (hd s, tl s); |
|
49 |
||
50 |
fun singleton s = Cons (s, K Nil); |
|
51 |
||
52 |
fun append Nil s = s () |
|
53 |
| append (Cons (h,t)) s = Cons (h, fn () => append (t ()) s); |
|
54 |
||
55 |
fun map f = |
|
56 |
let |
|
57 |
fun m Nil = Nil |
|
58 |
| m (Cons (h,t)) = Cons (f h, m o t) |
|
59 |
in |
|
60 |
m |
|
61 |
end; |
|
62 |
||
63 |
fun maps f g = |
|
64 |
let |
|
65 |
fun mm s Nil = g s |
|
66 |
| mm s (Cons (x,xs)) = |
|
67 |
let |
|
68 |
val (y,s') = f x s |
|
69 |
in |
|
70 |
Cons (y, mm s' o xs) |
|
71 |
end |
|
72 |
in |
|
73 |
mm |
|
74 |
end; |
|
75 |
||
76 |
fun zipwith f = |
|
77 |
let |
|
78 |
fun z Nil _ = Nil |
|
79 |
| z _ Nil = Nil |
|
80 |
| z (Cons (x,xs)) (Cons (y,ys)) = |
|
81 |
Cons (f x y, fn () => z (xs ()) (ys ())) |
|
82 |
in |
|
83 |
z |
|
84 |
end; |
|
85 |
||
86 |
fun zip s t = zipwith pair s t; |
|
87 |
||
88 |
fun take 0 _ = Nil |
|
89 |
| take n Nil = raise Subscript |
|
90 |
| take 1 (Cons (x,_)) = Cons (x, K Nil) |
|
91 |
| take n (Cons (x,xs)) = Cons (x, fn () => take (n - 1) (xs ())); |
|
92 |
||
93 |
fun drop n s = funpow n tl s handle Empty => raise Subscript; |
|
94 |
||
72004 | 95 |
fun unfold f = |
96 |
let |
|
97 |
fun next b () = |
|
98 |
case f b of |
|
99 |
NONE => Nil |
|
100 |
| SOME (a,b) => Cons (a, next b) |
|
101 |
in |
|
102 |
fn b => next b () |
|
103 |
end; |
|
104 |
||
39348 | 105 |
(* ------------------------------------------------------------------------- *) |
106 |
(* Stream versions of standard list operations: these might not terminate. *) |
|
107 |
(* ------------------------------------------------------------------------- *) |
|
108 |
||
109 |
local |
|
110 |
fun len n Nil = n |
|
111 |
| len n (Cons (_,t)) = len (n + 1) (t ()); |
|
112 |
in |
|
113 |
fun length s = len 0 s; |
|
114 |
end; |
|
115 |
||
116 |
fun exists pred = |
|
117 |
let |
|
118 |
fun f Nil = false |
|
119 |
| f (Cons (h,t)) = pred h orelse f (t ()) |
|
120 |
in |
|
121 |
f |
|
122 |
end; |
|
123 |
||
124 |
fun all pred = not o exists (not o pred); |
|
125 |
||
126 |
fun filter p Nil = Nil |
|
127 |
| filter p (Cons (x,xs)) = |
|
128 |
if p x then Cons (x, fn () => filter p (xs ())) else filter p (xs ()); |
|
129 |
||
130 |
fun foldl f = |
|
131 |
let |
|
132 |
fun fold b Nil = b |
|
133 |
| fold b (Cons (h,t)) = fold (f (h,b)) (t ()) |
|
134 |
in |
|
135 |
fold |
|
136 |
end; |
|
137 |
||
138 |
fun concat Nil = Nil |
|
139 |
| concat (Cons (Nil, ss)) = concat (ss ()) |
|
140 |
| concat (Cons (Cons (x, xs), ss)) = |
|
141 |
Cons (x, fn () => concat (Cons (xs (), ss))); |
|
142 |
||
143 |
fun mapPartial f = |
|
144 |
let |
|
145 |
fun mp Nil = Nil |
|
146 |
| mp (Cons (h,t)) = |
|
147 |
case f h of |
|
148 |
NONE => mp (t ()) |
|
149 |
| SOME h' => Cons (h', fn () => mp (t ())) |
|
150 |
in |
|
151 |
mp |
|
152 |
end; |
|
153 |
||
154 |
fun mapsPartial f g = |
|
155 |
let |
|
156 |
fun mp s Nil = g s |
|
157 |
| mp s (Cons (h,t)) = |
|
158 |
let |
|
159 |
val (h,s) = f h s |
|
160 |
in |
|
161 |
case h of |
|
162 |
NONE => mp s (t ()) |
|
163 |
| SOME h => Cons (h, fn () => mp s (t ())) |
|
164 |
end |
|
165 |
in |
|
166 |
mp |
|
167 |
end; |
|
168 |
||
169 |
fun mapConcat f = |
|
170 |
let |
|
171 |
fun mc Nil = Nil |
|
172 |
| mc (Cons (h,t)) = append (f h) (fn () => mc (t ())) |
|
173 |
in |
|
174 |
mc |
|
175 |
end; |
|
176 |
||
177 |
fun mapsConcat f g = |
|
178 |
let |
|
179 |
fun mc s Nil = g s |
|
180 |
| mc s (Cons (h,t)) = |
|
181 |
let |
|
182 |
val (l,s) = f h s |
|
183 |
in |
|
184 |
append l (fn () => mc s (t ())) |
|
185 |
end |
|
186 |
in |
|
187 |
mc |
|
188 |
end; |
|
189 |
||
190 |
(* ------------------------------------------------------------------------- *) |
|
191 |
(* Stream operations. *) |
|
192 |
(* ------------------------------------------------------------------------- *) |
|
193 |
||
72004 | 194 |
val primes = |
195 |
let |
|
196 |
fun next s = SOME (Useful.nextSieve s) |
|
197 |
in |
|
198 |
unfold next Useful.initSieve |
|
199 |
end; |
|
200 |
||
39348 | 201 |
fun memoize Nil = Nil |
202 |
| memoize (Cons (h,t)) = Cons (h, Lazy.memoize (fn () => memoize (t ()))); |
|
203 |
||
204 |
fun concatList [] = Nil |
|
205 |
| concatList (h :: t) = append h (fn () => concatList t); |
|
206 |
||
207 |
local |
|
45778 | 208 |
fun toLst res Nil = List.rev res |
39348 | 209 |
| toLst res (Cons (x, xs)) = toLst (x :: res) (xs ()); |
210 |
in |
|
211 |
fun toList s = toLst [] s; |
|
212 |
end; |
|
213 |
||
214 |
fun fromList [] = Nil |
|
215 |
| fromList (x :: xs) = Cons (x, fn () => fromList xs); |
|
216 |
||
217 |
fun listConcat s = concat (map fromList s); |
|
218 |
||
39443
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39414
diff
changeset
|
219 |
fun toString s = String.implode (toList s); |
39348 | 220 |
|
39443
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39414
diff
changeset
|
221 |
fun fromString s = fromList (String.explode s); |
39348 | 222 |
|
223 |
fun toTextFile {filename = f} s = |
|
224 |
let |
|
225 |
val (h,close) = |
|
226 |
if f = "-" then (TextIO.stdOut, K ()) |
|
227 |
else (TextIO.openOut f, TextIO.closeOut) |
|
228 |
||
229 |
fun toFile Nil = () |
|
230 |
| toFile (Cons (x,y)) = (TextIO.output (h,x); toFile (y ())) |
|
231 |
||
232 |
val () = toFile s |
|
233 |
in |
|
234 |
close h |
|
235 |
end; |
|
236 |
||
237 |
fun fromTextFile {filename = f} = |
|
238 |
let |
|
239 |
val (h,close) = |
|
240 |
if f = "-" then (TextIO.stdIn, K ()) |
|
241 |
else (TextIO.openIn f, TextIO.closeIn) |
|
242 |
||
243 |
fun strm () = |
|
244 |
case TextIO.inputLine h of |
|
245 |
NONE => (close h; Nil) |
|
246 |
| SOME s => Cons (s,strm) |
|
247 |
in |
|
248 |
memoize (strm ()) |
|
249 |
end; |
|
250 |
||
251 |
end |