| 1 |
= Fixpoints |
| 2 |
|
| 3 |
This page discusses a framework that makes it possible to compute |
| 4 |
fixpoints over arbitrary products of abstract types. The code is from |
| 5 |
an Extended Basis library |
| 6 |
(https://github.com/MLton/mltonlib/blob/master/com/ssh/extended-basis/unstable/README[`README`]). |
| 7 |
|
| 8 |
First the signature of the framework |
| 9 |
(https://github.com/MLton/mltonlib/blob/master/com/ssh/extended-basis/unstable/public/generic/tie.sig[`tie.sig`]): |
| 10 |
[source,sml] |
| 11 |
---- |
| 12 |
include::https://raw.github.com/MLton/mltonlib/master/com/ssh/extended-basis/unstable/public/generic/tie.sig[indent=0,lines=6..-1] |
| 13 |
---- |
| 14 |
|
| 15 |
`fix` is a <<TypeIndexedValues#,type-indexed>> function. The type-index |
| 16 |
parameter to `fix` is called a "witness". To compute fixpoints over |
| 17 |
products, one uses the `*{backtick}` operator to combine witnesses. To provide |
| 18 |
a fixpoint combinator for an abstract type, one implements a witness |
| 19 |
providing a thunk whose instantiation allocates a fresh, mutable proxy |
| 20 |
and a procedure for updating the proxy with the solution. Naturally |
| 21 |
this means that not all possible ways of computing a fixpoint of a |
| 22 |
particular type are possible under the framework. The `pure` |
| 23 |
combinator is a generalization of `tier`. The `iso` combinator is |
| 24 |
provided for reusing existing witnesses. |
| 25 |
|
| 26 |
Note that instead of using an infix operator, we could alternatively |
| 27 |
employ an interface using <<Fold#>>. Also, witnesses are eta-expanded |
| 28 |
to work around the <<ValueRestriction#,value restriction>>, while |
| 29 |
maintaining abstraction. |
| 30 |
|
| 31 |
Here is the implementation |
| 32 |
(https://github.com/MLton/mltonlib/blob/master/com/ssh/extended-basis/unstable/detail/generic/tie.sml[`tie.sml`]): |
| 33 |
[source,sml] |
| 34 |
---- |
| 35 |
include::https://raw.github.com/MLton/mltonlib/master/com/ssh/extended-basis/unstable/detail/generic/tie.sml[indent=0,lines=6..-1] |
| 36 |
---- |
| 37 |
|
| 38 |
Let's then take a look at a couple of additional examples. |
| 39 |
|
| 40 |
Here is a naive implementation of lazy promises: |
| 41 |
[source,sml] |
| 42 |
---- |
| 43 |
structure Promise :> sig |
| 44 |
type 'a t |
| 45 |
val lazy : 'a Thunk.t -> 'a t |
| 46 |
val force : 'a t -> 'a |
| 47 |
val Y : 'a t Tie.t |
| 48 |
end = struct |
| 49 |
datatype 'a t' = |
| 50 |
EXN of exn |
| 51 |
| THUNK of 'a Thunk.t |
| 52 |
| VALUE of 'a |
| 53 |
type 'a t = 'a t' Ref.t |
| 54 |
fun lazy f = ref (THUNK f) |
| 55 |
fun force t = |
| 56 |
case !t |
| 57 |
of EXN e => raise e |
| 58 |
| THUNK f => (t := VALUE (f ()) handle e => t := EXN e ; force t) |
| 59 |
| VALUE v => v |
| 60 |
fun Y ? = Tie.tier (fn () => let |
| 61 |
val r = lazy (raising Fix.Fix) |
| 62 |
in |
| 63 |
(r, r <\ op := o !) |
| 64 |
end) ? |
| 65 |
end |
| 66 |
---- |
| 67 |
|
| 68 |
An example use of our naive lazy promises is to implement equally naive |
| 69 |
lazy streams: |
| 70 |
[source,sml] |
| 71 |
---- |
| 72 |
structure Stream :> sig |
| 73 |
type 'a t |
| 74 |
val cons : 'a * 'a t -> 'a t |
| 75 |
val get : 'a t -> ('a * 'a t) Option.t |
| 76 |
val Y : 'a t Tie.t |
| 77 |
end = struct |
| 78 |
datatype 'a t = IN of ('a * 'a t) Option.t Promise.t |
| 79 |
fun cons (x, xs) = IN (Promise.lazy (fn () => SOME (x, xs))) |
| 80 |
fun get (IN p) = Promise.force p |
| 81 |
fun Y ? = Tie.iso Promise.Y (fn IN p => p, IN) ? |
| 82 |
end |
| 83 |
---- |
| 84 |
|
| 85 |
Note that above we make use of the `iso` combinator. Here is a finite |
| 86 |
representation of an infinite stream of ones: |
| 87 |
|
| 88 |
[source,sml] |
| 89 |
---- |
| 90 |
val ones = let |
| 91 |
open Tie Stream |
| 92 |
in |
| 93 |
fix Y (fn ones => cons (1, ones)) |
| 94 |
end |
| 95 |
---- |