caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Tom <tom.primozic@gmail.com>
To: "Diego Olivier FERNANDEZ PONS" <diego.fernandez_pons@etu.upmc.fr>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] More problems with memoization
Date: Sat, 30 Sep 2006 21:19:25 +0200	[thread overview]
Message-ID: <c1490a380609301219j252d3210he589212fc68350dc@mail.gmail.com> (raw)
In-Reply-To: <20060930200125.bjpzgnh7kkkogkgk@webmail.etu.upmc.fr>

[-- Attachment #1: Type: text/plain, Size: 1262 bytes --]

In case you know me, you probably know what kind of solution I am going to
tell you...

Well, in case you don't... my solution is going to be dirty, is going to use
the undocumented Obj module (Obj.magic lets you change an ocaml value into
another ocaml value of any type).

-----------

The solution is to memoize the very make_mem function!

let make_mem' = function f ->
      let table = ref [] in
      function n ->
      try
       List.assoc n !table
      with Not_found ->
       let f_n = f n in
         table := (n, f_n) :: !table;
   f_n;;

let make_mem = make_mem' make_mem'

Well, the problem is, that the ocaml type inference forbids a function to
return a polymorphic value, so the type of make_mem' is only ('_a -> '_b) ->
'_a -> '_b. So the right thing to do here (as this type is, obviously,
incorrect) is to use Obj.magic:

let make_mem'' = Obj.magic make_mem'

let make_mem = (make_mem'' : ('a -> 'b) -> 'a -> 'b)

Now, the fibb will work as expected (instantaneously) and memoization will
be simple to apply. A bit of thinking is needed, of course, to reckon
whether my implementation is optimal and safe (not yielding unexpected
results, for example when you rename functions, use partial evaluation,
etc.). But it works.

Have fun, Tom

[-- Attachment #2: Type: text/html, Size: 2995 bytes --]

  reply	other threads:[~2006-09-30 19:19 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-09-30 18:01 Diego Olivier FERNANDEZ PONS
2006-09-30 19:19 ` Tom [this message]
2006-09-30 19:26   ` [Caml-list] " Tom
2006-10-01  0:23 ` Jon Harrop
2006-10-01  0:51   ` Martin Jambon
2006-10-02 15:29   ` Diego Olivier FERNANDEZ PONS
2006-10-02 23:00     ` Andrej Bauer
2006-10-02 23:04       ` Andrej Bauer
2006-10-03  0:50       ` skaller
2006-10-02 23:37     ` Don Syme

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c1490a380609301219j252d3210he589212fc68350dc@mail.gmail.com \
    --to=tom.primozic@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=diego.fernandez_pons@etu.upmc.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).