3 datatype 'a u
= Nil | Cons
of 'a
* 'a t
4 withtype 'a t
= unit
-> 'a u
6 fun unfold (f
: 'b
-> ('a
* 'b
) option
) : 'b
-> 'a t
=
11 |
SOME (x
, b
) => Cons (x
, loop b
)
15 fun map (f
: 'a
-> 'b
) : 'a t
-> 'b t
=
19 |
Cons (x
, xs
) => SOME (f x
, xs
))
24 fun stream (next
: 'b
-> 'c
)
25 (safe
: 'b
-> 'c
-> bool)
26 (prod
: 'b
-> 'c
-> 'b
)
27 (cons
: 'b
-> 'a
-> 'b
)
28 : 'b
-> 'a Stream
.t
-> 'c Stream
.t
=
35 then Stream
.Cons (y
, loop (prod z y
) s
)
37 Stream
.Nil
=> Stream
.Nil
38 | Stream
.Cons (x
, xs
) => loop (cons z x
) xs ())
44 type lft
= (IntInf
.int * IntInf
.int * IntInf
.int * IntInf
.int)
46 val unit
: lft
= (1,0,0,1)
48 fun comp (q
,r
,s
,t
) (u
,v
,w
,x
) : lft
= (q
*u
+r
*w
, q
*v
+r
*x
, s
*u
+t
*w
, s
*v
+t
*x
)
53 val lfts
= Stream
.map (fn k
=> (k
, 4*k
+2, 0, 2*k
+1)) (Stream
.unfold (fn i
=> SOME (i
, i
+1)) 1)
54 fun floor_extr (q
,r
,s
,t
) x
= (q
* x
+ r
) div (s
* x
+ t
)
55 fun next z
= floor_extr z
3
56 fun safe z n
= n
= floor_extr z
4
57 fun prod z n
= comp (10, ~
10*n
, 0, 1) z
58 fun cons z z
' = comp z z
'
60 stream next safe prod cons init lfts
64 structure MainShootout
=
68 fun loop (ds
, (k
, col
)) =
73 then (print
"\t:"; print (IntInf
.toString k
); print
"\n";
78 Stream
.Nil
=> raise Empty
79 | Stream
.Cons (d
, ds
) =>
80 (print (IntInf
.toString d
);
81 loop (ds
, (k
+ 1, col
)))
83 else (print (CharVector
.tabulate (10 - col
, fn _
=> #
" "));
84 print
"\t:"; print (IntInf
.toString k
); print
"\n";
87 loop (PiDigits
.pi
, (0, 0))
90 (TextIO.output (TextIO.stdErr
,
91 concat
["usage: ", OS
.Path
.file name
, " <n>\n"]);
93 fun main (name
, arguments
) =
95 [n
] => (case IntInf
.fromString n
of
97 then (display n
; OS
.Process
.success
)
103 val _
= OS
.Process
.exit (Main
.main (CommandLine
.name (), CommandLine
.arguments ()))
106 structure MainBenchmark
=
110 fun loop (ds
, k
, n
) =
112 Stream
.Nil
=> raise Empty
113 | Stream
.Cons (d
, ds
) =>
116 then (print (IntInf
.toString k
); print
"\n")
117 else loop (ds
, k
+ 1, n
- 1)
118 else loop (ds
, k
+ 1, n
)
120 loop (PiDigits
.pi
, 0, n
)
123 (TextIO.output (TextIO.stdErr
,
124 concat
["usage: ", OS
.Path
.file name
, " <n>\n"]);
126 fun main (name
, arguments
) =
128 [n
] => (case IntInf
.fromString n
of
130 then (display n
; OS
.Process
.success
)
132 | NONE
=> usage name
)
135 val doit
= display
o IntInf
.fromInt
138 structure Main
= MainBenchmark