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

Re: [Caml-list] Weird types
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2001-06-19 (07:12) From: Didier Remy Subject: Re: [Caml-list] Weird types
```Tyng-Ruey Chuang <trc@iis.sinica.edu.tw> writes:

> Jean-Christophe FILLIATRE wrote:
> > Actually, there  is a  type-able way of  writing this  function, which
> > consists in duplicating it into two functions, like this:
> >
> > ======================================================================
> > type ('a,'b,'c) t =
> >   | A of 'a * 'b * 'c
> >   | B of ('b, 'a, 'c) t
> >
> > let rec gamma = function
> >   | A _ -> 0
> >   | B x -> 1 + gamma' x
> >
> > and gamma' = function
> >   | A _ -> 0
> >   | B x -> 1 + gamma x
> > ======================================================================
> >
> > which gives the expected types:
> >
> > ======================================================================
> > val gamma : ('a, 'b, 'c) t -> int = <fun>
> > val gamma' : ('a, 'b, 'c) t -> int = <fun>
> > ======================================================================
>
> Interesting! But then size of the duplicated code grows exponentially.

Actually, you can share a little more:

let gamma_body gamma = function
| A _ -> 0
| B x -> 1 + gamma x

let rec gamma  x = gamma_body gamma' x
and gamma' x = gamma_body gamma  x;;
;;

and a for three parameters:

type ('a, 'b, 'c) sigma =
| I of 'a * 'b * 'c
| T of ('b, 'a, 'c) sigma
| P of ('b, 'c, 'a) sigma

let body gT gP = function
| I _ -> 0
| T x -> 1 + gT x
| P x -> 1 + gP x

let rec abc s = body bac bca s
and acb s = body cab cba s
and bac s = body abc acb s
and bca s = body cba cab s
and cab s = body acb abc s
and cba s = body bca bac s
;;

Didier
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr

```