Version française
Home     About     Download     Resources     Contact us    
Browse thread
generic functions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Alex Baretta <alex@b...>
Subject: Re: [Caml-list] generic functions
Olivier Andrieu wrote:
>  Alex Baretta [Mon, 10 Jan 2005]:
>  > type poly_int = [ `Int of int ]
>  > type poly_float = [ `Float of float ]
>  > 
>  > let foo_int foo = function
>  >    | `Int x -> `Int(x * x)
>  >    | other -> foo other
>  > 
>  > let foo_float foo = function 
>  >    | `Float x -> `Float(x *. x) 
>  >    | other -> foo other
>  > 
>  > let rec foo x = match x with
>  >    | #poly_int -> foo_int foo x
>  >    | #poly_float -> foo_float foo x
> 
> Hum, I would have written this in that way :
> ,----
> | let foo_int   (`Int x)   = `Int   (x * x)
> | let foo_float (`Float x) = `Float (x *. x)
> | 
> | let foo = function
> |   | #poly_int   as x -> foo_int x
> |   | #poly_float as x -> foo_float x
> `----
> 
> What's the use for the `other -> foo other' clauses ?

In my previous post I mentioned *extensible functions*. The first 
parameter of single instances of the polymorphic foo (i.e. foo_int and 
foo_float) allow the generalization of each of these to arbitrarily 
complex polymorphic type schema. We have exactly 7888 lines of code 
which contain such late-bound-recursive functions à la foo_int. 
Obviously it takes a relatively complex example to show why 
late-bound-recursion is essential to the implementation of a fairly 
extensional polymorphism with Ocaml's polymorphic variants.

Alex


-- 
*********************************************************************
http://www.barettadeit.com/
Baretta DE&IT
A division of Baretta SRL

tel. +39 02 370 111 55
fax. +39 02 370 111 54

Our technology:

The Application System/Xcaml (AS/Xcaml)
<http://www.asxcaml.org/>

The FreerP Project
<http://www.freerp.org/>