From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id XAA18419; Wed, 25 Dec 2002 23:59:17 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id XAA18339 for ; Wed, 25 Dec 2002 23:59:15 +0100 (MET) X-SPAM-Warning: Sending machine is listed in blackholes.five-ten-sg.com Received: from athlon.baretta.com (host25-68.pool80116.interbusiness.it [80.116.68.25]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id gBPMxEn12655 for ; Wed, 25 Dec 2002 23:59:14 +0100 (MET) Received: from baretta.com (localhost.localdomain [127.0.0.1]) by athlon.baretta.com (Postfix) with ESMTP id 1FF21273B6; Thu, 26 Dec 2002 00:01:14 +0100 (CET) Message-ID: <3E0A38BA.7090701@baretta.com> Date: Thu, 26 Dec 2002 00:01:14 +0100 From: Alessandro Baretta Organization: Baretta srl -- www.baretta.com User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.1) Gecko/20020826 X-Accept-Language: it, en MIME-Version: 1.0 To: Ker Lutyn , Ocaml Subject: Re: [Caml-list] dynamically extensible sum types References: <20021224183708.89659.qmail@web40611.mail.yahoo.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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