Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] dynamically extensible sum types
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Alessandro Baretta <alex@b...>
Subject: Re: [Caml-list] dynamically extensible sum types

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"


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 
(* 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 

> (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 Archives:
Bug reports: FAQ:
Beginner's list: