From: Christophe DEHLINGER <christophe.dehlinger@ifp.fr>
To: caml-list@yquem.inria.fr
Subject: typing a value cache
Date: Tue, 30 Nov 2004 09:59:57 +0100 [thread overview]
Message-ID: <41AC368D.8040808@ifp.fr> (raw)
Hi all,
I'm trying to find an elegant way to build a data structure.
My program must deal with a large collection (1000+) of unary functions.
The collection should be dynamically expandable, but I may have it be
constant and known at compile time if I have no other choice. These
functions may use other functions of the collection in their body.
As the exact same function calls will very often be made during
execution, I wanted to build a cache structure in which frequently asked
values would be stored.
Now things get tricky: the body of any function may change during
execution (so I actually deal with function references rather than
functions). When this happens, the cache entries corresponding to the
function calls that used this function during evaluation become
obsolete, and thus should be removed from the cache. So - and this is
the hard part typing-wise - this means that the cache must somehow
record the dependencies between function calls.
Simple example, with 3 functions f:int -> bool, g:bool->string, h:unit->int
Initially :
. f n = n>0
. g b = if b then "yo" else (string_of_bool (f 5))
. h () = String.length (g false)
Then my program then tries to evaluate h () for some purpose. The
following entries are then added to the cache:
1) f : 5 -> true
2) g : false -> "true"
3) h : () -> 4
It then evaluates g true. The following entry is then added to the cache:
4) g : true -> "yo"
Then, if the body of f is changed to something else (e.g. (=) 0),
entries 1, 2 and 3 should be removed from the cache, but not 4.
My question is : how do I build this cache thing in ocaml ? Is there a
way to avoid coding type information ?
I have found (I think) two ways to do that:
- defining notype_cache and no_type_cache_key, two class types with
methods to handle addition of a dependency in a cache. They are
inherited respectively by ('a,'b) cache and ('a,'b) cache_key. Each
function of type 'a->'b in the collection has its own ('a,'b) cache.
Seems to work fine, but notype_cache_key object instances cannot be
compared (by value) in general, so dependencies cannot be removed (as it
involves parsing a list of dependencies (of type no_type_cache_key list)
and trying to find a particular notype_cache_key value in it). This can
be worked around with some info duplication, but all in all although
this approach seems to work, it is definitely unwieldy and has shortcomings
- too ugly to tell. Suffice to say it involves camlp4.
So, have I missed the simple-and-obvious solution to this problem ?
Cheers,
Christophe
__________________________
Ce message (et toutes ses pièces jointes éventuelles) est confidentiel et établi à l'intention exclusive de ses destinataires. Toute utilisation de ce message non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. L'IFP décline toute responsabilité au titre de ce message.
This message and any attachments (the message) are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP should not be liable for this message.
Visitez notre site Web / Visit our web site : http://www.ifp.fr
__________________________
next reply other threads:[~2004-11-30 9:00 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-11-30 8:59 Christophe DEHLINGER [this message]
2004-12-01 14:09 ` [Caml-list] " Virgile Prevosto
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=41AC368D.8040808@ifp.fr \
--to=christophe.dehlinger@ifp.fr \
--cc=caml-list@yquem.inria.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