Import Upstream version 20180207
[hcoop/debian/mlton.git] / lib / mlton / basic / pointer.sml
1 (* Copyright (C) 1999-2006 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 Pointer: POINTER =
9 struct
10
11 datatype 'a t = T of 'a option ref
12
13 fun !(T r) =
14 case Ref.! r of
15 NONE => Error.bug "Pointer.!"
16 | SOME v => v
17
18 fun (T r) := v = Ref.:=(r, SOME v)
19
20 fun clear(T r) = Ref.:=(r, NONE)
21
22 fun copy(T r, T r') = Ref.:=(r, Ref.! r')
23
24 fun eq(T r, T r') = Ref.equals(r, r')
25
26 fun follow(T r) = Ref.! r
27
28 fun equals(p, p', equals) =
29 case (follow p, follow p') of
30 (NONE, NONE) => true
31 | (SOME v, SOME v') => equals(v, v')
32 | _ => false
33
34 fun isNull p = Option.isNone(follow p)
35
36 fun make v = T(ref v)
37
38 fun new v = make(SOME v)
39
40 fun null() = make NONE
41
42 fun swap(T p, T p') = Ref.swap(p, p')
43
44 end