~marionnet-drivers/ocamlbricks/trunk

« back to all changes in this revision

Viewing changes to STRUCTURES/extreme_sharing.mli

  • Committer: Jean-Vincent Loddo
  • Date: 2020-05-23 20:20:55 UTC
  • Revision ID: loddo@lipn.univ-paris13.fr-20200523202055-pg9f4cxrjo3cuw2k
In module Extreme_sharing, added function `weakly_memoize_with_prj'. This variant of `weakly_memoize' prevent the collection of arguments that may occur memoizing functions with several arguments. Indeed, functions with several arguments must be uncurried before to be memoized. Doing that, it is possible that their arguments are built only to call the function, then unused hence collected.

Show diffs side-by-side

added added

removed removed

Lines of Context:
116
116
 
117
117
(* All the tools above are implemented with this function based on ephemerons
118
118
   (see the standard module Ephemeron.K1): *)
119
 
val weakly_memoize : ?equality:('a -> 'a -> bool) (* (=) *) -> ?size:int (* 0 *) -> ('a -> 'b) -> 'a -> 'b
 
119
val weakly_memoize          : ?equality:('a -> 'a -> bool) (* (=) *) -> ?size:int (* 0 *) -> ('a -> 'b) -> 'a -> 'b
 
120
 
 
121
(* Variant with a projection function that prevent collection of 'a arguments. This may be useful to memoize functions with
 
122
   several arguments. Because these functions must be uncurried before to be memoized, their arguments are probably built
 
123
   only to call the function, then unused and collected.
 
124
   Example:
 
125
 
 
126
    (* val tool : string -> string list -> string *)
 
127
    let tool = String.concat
 
128
 
 
129
    (* val memoised_tool : string -> string list -> string *)
 
130
    let memoised_tool =
 
131
      let uncurried (sep, xs) = tool sep xs in
 
132
      let equality (sep1, xs1) (sep2, xs2) = (sep1 = sep2) && (xs1 == xs2) in
 
133
      let memo = Extreme_sharing.weakly_memoize_with_prj ~trace_faults:() ~equality ~prj:(snd) (uncurried) in
 
134
      fun sep xs -> memo (sep, xs)
 
135
 
 
136
    let r = memoised_tool ":" ["/bin"; "/usr/bin"; "/usr/local/bin"] ;;
 
137
    (* Extreme_sharing: weakly_memoize_with_prj: FAULT *)
 
138
    (* val r : string = "/bin:/usr/bin:/usr/local/bin" *)
 
139
 
 
140
    let dirs = ["/bin";"/usr/bin";"/usr/local/bin"] ;;
 
141
 
 
142
    let r = memoised_tool ":" dirs ;;
 
143
    (* Extreme_sharing: weakly_memoize_with_prj: FAULT *)
 
144
    (* val r : string = "/bin:/usr/bin:/usr/local/bin" *)
 
145
 
 
146
    let r = memoised_tool ":" dirs ;;
 
147
    (* val r : string = "/bin:/usr/bin:/usr/local/bin" *)
 
148
   *)
 
149
val weakly_memoize_with_prj : ?trace_faults:unit -> ?equality:('a -> 'a -> bool) (* (=) *) -> ?size:int (* 0 *) -> prj:('a -> 'c) -> ('a -> 'b) -> 'a -> 'b
 
150
 
120
151
 
121
152
(* ------------------------------------ *)
122
153
(*        Facilities for arrays'        *)