caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Alessandro Baretta <alex@baretta.com>
To: Ker Lutyn <ker527mail@yahoo.com>, Ocaml <caml-list@inria.fr>
Subject: Re: [Caml-list] dynamically extensible sum types
Date: Thu, 26 Dec 2002 00:01:14 +0100	[thread overview]
Message-ID: <3E0A38BA.7090701@baretta.com> (raw)
In-Reply-To: <20021224183708.89659.qmail@web40611.mail.yahoo.com>



Ker Lutyn wrote:
> I need a dynamically extensible sum type.  I can think of three approaches:
> 
> (1) Use polymorphic variants: `Foo of a * b, `Bar of c * d * e, etc

I have taken this approach to statically type SQL queries. I 
have various application modules defining sum types of 
polymorphic variants such as the following:
module Application1 = struct
type table = [ | `Table1 | `Table2 | ... | `TableN ]
type field = [ | `Field_name1 of foo | `Field_name2 of bar | 
... ]

let string_of_table = function
| `Table1 -> "name_of_table_1"
| `Table2 -> "name_of_table_2"
...

let string_of_field = function
| `Field_name1 (_) -> "field_name_1"
...

end

Several modules share this common approach. I finally tie 
everything up in a Db_interface, whose main element is the 
SQL query generator function. This function takes abstract 
representation of queries, such as
`SELECT(`From_table, field_list, where_conditions,
	order_by_field_list, group_by_field_list)

The names of tables and fields are converted from their 
variant-constructor representation to their string 
representation by a function which is defined as the 
parallelization of all functions of the kind 
ApplicationN.string_of_table/field.
(* In module Db_interface *)
let string_of_field = function
| #Application1.field as f -> Application1.string_of_field f
| #Application2.field as f -> Application2.string_of_field f
...

I like this approach, but it requires some source 
preprocessing to be able to selectively link only some 
application code. I still haven't gotten to figure out how 
to get camlp4 to filter out pattern-matching lines depending
on what application modules are actually being compiled and 
linked.

> (3) Use thunks: (fun () -> foo a b), (fun () -> bar c d e), etc

I would greatly indebted to you if you could explain to me 
what thunks are.


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


      reply	other threads:[~2002-12-25 22:59 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-12-24 18:37 Ker Lutyn
2002-12-25 23:01 ` Alessandro Baretta [this message]

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=3E0A38BA.7090701@baretta.com \
    --to=alex@baretta.com \
    --cc=caml-list@inria.fr \
    --cc=ker527mail@yahoo.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).