caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Anthony Tavener <anthony.tavener@gmail.com>
To: Yaron Minsky <yminsky@janestreet.com>
Cc: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] Heterogeneous dictionary
Date: Wed, 3 Apr 2013 20:18:31 -0600	[thread overview]
Message-ID: <CAN=ouMTydyqTgx57uoFg7Q5Bp2P_8_XQzeyqzhQY_mDni9AH6w@mail.gmail.com> (raw)
In-Reply-To: <CACLX4jTXQpEztdmGVaPDT_R+CTQGGouHk7zm5JytBscMD+2uJA@mail.gmail.com>

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

Oh, maybe I created a limitation in my own head...

Would this work then (for example):

module Modifier = struct
  let recovery = Key.create "recovery"
  let resist_pain = Key.create "resist_pain"
  let resist_magic = Key.create "resist_magic"
  (* a whole lot of these... *)
end

These would then be monomorphic? Anyway, the important thing is
they didn't require knowledge of any types to create all the keys, so
now having all other modules referencing Modifier wouldn't be bad at all.

Then something like the following will reify the type of the
Modifier.recovery key?

  let mods = set mods Modifier.recovery (fun (a,b) -> a+6,b) in ...

I kept thinking that if I had a "grand central" of universal embeddors
they'd need to be aware of the types they're embedding... thereby
creating the type-dependencies I was worried about!

This solution, if I'm understanding it right, is perfectly fine!
Thank-you for cluing me in, Yaron!



On Wed, Apr 3, 2013 at 7:29 PM, Yaron Minsky <yminsky@janestreet.com> wrote:

> Have you looked at Univ_map in Core?  It has the same problem you're
> complaining about, in that the keys do need to be tied to the type of
> the data stored under that key.  You then of course need to stash the
> key somewhere.  But I'm not sure what problem that creates.  In
> particular, I don't know why this creates a bottleneck of type
> dependencies.  Each Univ_map.Key.t is created independently, and can
> be created at any point in the code.
>
> y
>
> On Wed, Apr 3, 2013 at 8:45 PM, Anthony Tavener
> <anthony.tavener@gmail.com> wrote:
> > I think I might be up against a brick wall. But maybe there's a door I
> don't
> > see, without Obj.magicing myself through the wall. (I haven't had to use
> > magic
> > for anything yet!) :)
> >
> > I want to stash values under a key, and retrieve them by that key. All
> > values
> > bound to a given key are of the same type, but the type will differ
> between
> > keys. Basically like a hashtable you'd find in a dynamic language.
> >
> >   (* modifier-additions like this would be scattered across the
> code-base *)
> >   contribute `RecoveryRoll (fun (a,b) -> a+3,b)
> >   contribute `RecoveryRoll (fun (a,b) -> a,b-1)
> >   contribute `ResistPain (fun a -> a+1)
> >   contribute `MagicResistance (fun a -> match a with None -> None | Some
> x
> > -> Some(x+3))
> >   contribute `MagicResistance (fun a -> match a with None -> Some 7 |
> Some x
> > -> Some(x+7))
> >
> >   (* example of applying modifiers... *)
> >   let modified = fold `RecoveryRoll (4,1) in
> >   ...
> >
> > (There are details I've left out here, like the need for
> > 'contribute' returning an id by which to remove a modifier, as well
> > as control over order-of-application.)
> >
> > Now I think the type signature of these functions would be:
> >
> >   val contribute: a'. 'a key -> ('a -> 'a) -> unit
> >   val fold: 'a. 'a key -> 'a -> 'a
> >
> > And the thorn in my side would be that the key must be "keyed" to
> > the type, or is there an escape? At one point I tried a universal
> > type to "hide" the signature of the function-list, but I also
> > stashed the inj/proj under the key -- well, of course the inj/proj
> > functions had different types per entry in a hashtable, so that
> > worked as well as not having a universal type involved.  :)
> >
> > If I provide the inj/proj functions at each invocation then I need
> > to pre-create these and house them somewhere (which could create a
> > bottleneck of type-dependencies) -- Imagine several hundred
> > modifiers like `RecoveryRoll; some might use types that only need
> > visibility in one module. Trying to place each modifier in suitable
> > modules also seems a mess... a lot of them conceptually exist "in
> > the spaces between modules".
> >
> > So I keep trying to create an airy light-weight "implied"
> > association to connect modifiers to use-sites... but to satisfy
> > typing it seems I need to be explicit at some point.
> >
> > Does anyone have any ideas? I'm often surprised at the gymnastics
> > OCaml's type-system can accomplish under the guidance of some smart
> > folks. If anyone's made a heterogenous dictionary/hashtable that
> > doesn't need types explicity declared, that would probably be what
> > I'm looking for.
> >
> >
> > Note that I've had this problem surface several times and managed to
> > find solutions that suited the specific problem, but each problem
> > can have it's subtle details. In this case, the large number of keys
> > and functions, combined with their spread across codebase and the
> > sparse nature of their use (a game-entity might have a few dozen
> > modifiers out of hundreds)...  really seems to push for
> > association-by-name-only. At least that's all my brain gravitates
> > toward.
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

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

  reply	other threads:[~2013-04-04  2:18 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-04  0:45 Anthony Tavener
2013-04-04  1:29 ` Yaron Minsky
2013-04-04  2:18   ` Anthony Tavener [this message]
2013-04-04  6:19 ` Martin Jambon
2013-04-04  7:32   ` Alain Frisch
2013-04-04 18:16     ` Martin Jambon
2013-04-04  7:38 ` Raphaël Proust
2013-04-04  8:37   ` Anthony Tavener
2013-04-04  9:04     ` David House
2013-04-04 18:48       ` Anthony Tavener
2013-04-05 16:37         ` Yaron Minsky
2013-04-05 18:27           ` Anthony Tavener
2013-04-05 18:51             ` Yaron Minsky
2013-04-05 19:55               ` Anthony Tavener
2013-04-05 20:03                 ` Yaron Minsky
2013-04-05 20:27                   ` Anthony Tavener
2013-04-08  8:33                     ` David House

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='CAN=ouMTydyqTgx57uoFg7Q5Bp2P_8_XQzeyqzhQY_mDni9AH6w@mail.gmail.com' \
    --to=anthony.tavener@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=yminsky@janestreet.com \
    /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).