Import Upstream version 20180207
[hcoop/debian/mlton.git] / lib / mlton / env / move-to-front.fun
CommitLineData
7f918cf1
CE
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
8functor MoveToFrontEnv(S: MONO_ENV_STRUCTS): MONO_ENV =
9BasicEnvToEnv
10(open S
11
12 datatype t = T of (Domain.t * Range.t) list ref
13
14 val fromList = T o ref
15
16 fun toList(T(ref drs)) = drs
17
18 fun extend(T(ref drs), d, r) =
19 T(ref((d, r) ::
20 (* poor man's profiling *)
21 let fun f() = List.remove(drs, fn (d', _) => Domain.equals(d, d'))
22 in (*f() ;*) f()
23 end))
24
25 fun peek(T reff, d) =
26 let
27 fun loop(drs, accum) =
28 case drs of
29 (d', r) :: drs =>
30 if Domain.equals(d, d')
31 then (reff := (d, r) :: List.appendRev(accum, drs)
32 ; SOME r)
33 else loop(drs, (d', r) :: accum)
34 | [] => NONE
35 (* poor man's profiling *)
36 fun f() = loop(!reff, [])
37 in (*f() ;*) f()
38 end)