<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2002/12/16102b462cf4c140728af17f3cd54070"
  from="Alessandro Baretta &lt;alex@b...&gt;"
  author="Alessandro Baretta"
  date="2002-12-25T22:59:17"
  subject="Re: [Caml-list] dynamically extensible sum types"
  prev="2002/12/d303c7aeaacaa1386ce7d6db56c6ef5c"
  next="2002/12/c8805c0bb547349622f79196ca9e8b29"
  prev-in-thread="2002/12/0a83de82e890459a006d9ed53576a8c9"
  prev-thread="2002/12/3e970e2f840009aa76fa39ca1546430a"
  next-thread="2002/12/d303c7aeaacaa1386ce7d6db56c6ef5c"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] dynamically extensible sum types">
<msg 
  url="2002/12/0a83de82e890459a006d9ed53576a8c9"
  from="Ker Lutyn &lt;ker527mail@y...&gt;"
  author="Ker Lutyn"
  date="2002-12-25T21:05:44"
  subject="[Caml-list] dynamically extensible sum types">
<msg 
  url="2002/12/16102b462cf4c140728af17f3cd54070"
  from="Alessandro Baretta &lt;alex@b...&gt;"
  author="Alessandro Baretta"
  date="2002-12-25T22:59:17"
  subject="Re: [Caml-list] dynamically extensible sum types">
</msg>
</msg>
</thread>

<contents>


Ker Lutyn wrote:
&gt; I need a dynamically extensible sum type.  I can think of three approaches:
&gt; 
&gt; (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 -&gt; "name_of_table_1"
| `Table2 -&gt; "name_of_table_2"
...

let string_of_field = function
| `Field_name1 (_) -&gt; "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 -&gt; Application1.string_of_field f
| #Application2.field as f -&gt; 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.

&gt; (3) Use thunks: (fun () -&gt; foo a b), (fun () -&gt; 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

</contents>

</message>

