package core-and-more

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file my_memoizer.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
open Util
open My_dict
open My_hash_cons_dict

module type UnfixedDataFunction = sig
  module Arg : Data
  module Result : Data
  val f : (Arg.t -> Result.t) unfixed
end

module FixMemoizerOf(F:UnfixedDataFunction) = struct
  module ResultDict = DictOf(F.Arg)(F.Result)

  let result_storage : ResultDict.t ref = ref ResultDict.empty

  let rec evaluate
      (x:F.Arg.t)
    : F.Result.t =
    begin match ResultDict.lookup (!result_storage) x with
      | None ->
        let y = F.f evaluate x in
        result_storage := ResultDict.insert (!result_storage) x y;
        y
      | Some y -> y
    end
end


module type UnfixedHCDataFunction = sig
  module Arg : UIDData
  module Result : Data
  val f : (Arg.t -> Result.t) unfixed
end

module FixHCMemoizerOf(F:UnfixedHCDataFunction) = struct
  module ResultDict = HCDictOf(F.Arg)(F.Result)

  let result_storage : ResultDict.t ref = ref ResultDict.empty

  let clear () : unit = result_storage := ResultDict.empty

  let rec evaluate
      (x:F.Arg.t)
    : F.Result.t =
    begin match ResultDict.lookup (!result_storage) x with
      | None ->
        let y = F.f evaluate x in
        result_storage := ResultDict.insert (!result_storage) x y;
        y
      | Some y -> y
    end
end