caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Anthony Tavener <anthony.tavener@gmail.com>
To: caml-list@inria.fr
Subject: [Caml-list] A use-case for first-class modules... out of curiosity is there another way?
Date: Mon, 6 Aug 2012 19:48:04 -0600	[thread overview]
Message-ID: <CAN=ouMTG6+pqzNKcp46z76akQj2Dx1c2KsdE30Bu9T=BWB8yJA@mail.gmail.com> (raw)

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

Embedding "database" features into other modules.

First-class modules are allowing me to neatly unpack table implementations
into
other modules, using local types.

This is really quite basic, but I wonder if I could have done this before
first-class modules, and without leveraging the object system? I also don't
recall seeing first-class modules used for something like this.

So, is there another way to do this, aside from the object system? It's very
much like creating a basic object.

This is an extracted and simplified example...
--------------------

module Db = struct

  (* Signature for a single 'table' in the database, elements of type t. *)
  module type S = sig
    (* Full implementation relies on a Key module for different key types
than 'int' *)
    type t
    val get : int -> t
    val set : int -> t -> unit
    val del : int -> unit
    val iter : (int -> t -> unit) -> unit
    val fold : (int -> t -> 'a -> 'a) -> 'a -> 'a
  end

  (* Instantiates storage for a table, and returns FC module to interact
with the store. *)
  let create_with_default (type s) ?(size=19) default =
    (* Full implementation is parameterized by Key and Table modules *)
    let h = Hashtbl.create size in
    let module H = struct
      type t = s
      let get (id:int) =
        try Hashtbl.find h id
        with Not_found -> default
      let set id (v:t) = Hashtbl.replace h id v
      let del id = Hashtbl.remove h id
      let iter f = Hashtbl.iter f h
      let fold f init = Hashtbl.fold f h init
    end in
    (module H : S with type t = s)

end

(* An example table... *)
module Location = struct
  let unknown = "Unknown"
  include (val (Db.create_with_default unknown) : S with type t = string)
  (* Location might have a bunch of other functionality as well... *)
end

(* and basic usage... *)
# Location.get 1;;
- : Location.t = "Unknown"
# Location.set 1 "Mars";;
- : unit = ()
# Location.get 1;;
- : Location.t = "Mars"

--------------------
Some background on what this is for: (skip unless you're interested!)

I use a "component architecture" with most games -- basically a database of
properties keyed off "game object IDs". I thought this was a very powerful
feature in old MUDs/MUSHs. It's one of the first things I tried making when
I
started in OCaml, but I had some difficulties and ended up explicity
instantiating hashtables or maps in the global context of various modules.
Sloppy, but workable. (The reason I had difficulty is because I was trying
to
create a database of tables which were created at runtime -- not statically
known.)

Recently I decided to fix this mess. I had many modules, each which tended
to
have a corresponding "table". Eg. Characteristics, Size, Wounds, Faction,
Inventory, etc. So ideally I wanted a convenient way to embed "database"
functions into such modules while declaring the implementation of the
underlying datastore (hashtable, map, whatever).

This might seem a bit ugly from a functional-programming perspective, but
I've
found components to be quite powerful, and overall helps to constrain where
and how mutation happens. "Game state" is generally in flux -- well, it is
everything variable, and can be compared closely with save-game state. Most
code
which doesn't update game state can be functional. Actually, it feels
creepy to
have a variable assignment in the code, since mutation is generally to
game-state
and that's handled through a database. So the resulting style is
functional+database.

 -Tony

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

             reply	other threads:[~2012-08-07  1:48 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-07  1:48 Anthony Tavener [this message]
2012-08-08  9:21 ` Arnaud Spiwack
2012-08-08 14:17   ` Anthony Tavener

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=ouMTG6+pqzNKcp46z76akQj2Dx1c2KsdE30Bu9T=BWB8yJA@mail.gmail.com' \
    --to=anthony.tavener@gmail.com \
    --cc=caml-list@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;
as well as URLs for NNTP newsgroup(s).