Import Upstream version 20180207
[hcoop/debian/mlton.git] / lib / mlton / basic / fixed-point.sml
1 (* Copyright (C) 1999-2005 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
3 *
4 * MLton is released under a BSD-style license.
5 * See the file MLton-LICENSE for details.
6 *)
7
8 structure FixedPoint: FIXED_POINT =
9 struct
10
11 fun fix{start, step, equals} =
12 let
13 fun loop s =
14 let val s' = step s
15 in if equals(s, s')
16 then s
17 else loop s'
18 end
19 in loop start
20 end
21
22 fun fix' (f: (unit -> unit) -> unit) =
23 let
24 fun loop() =
25 let val changed = ref false
26 in f(fn () => changed := true);
27 if !changed
28 then loop()
29 else ()
30 end
31 in loop()
32 end
33
34
35 end