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

RE: scanning recursive variable type
• Manuel Fahndrich
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2000-09-22 (13:56) From: Manuel Fahndrich Subject: RE: scanning recursive variable type
```
What you need is polymorphic recursion in functions, which OCaml does not
provide. In the body of your function "int_of_t", it's type is monomorphic,
so the two recursive invocations don't both type check.

http://caml.inria.fr/archives/199809/msg00031.html

-Manuel

-----Original Message-----
From: Julien.Soula [mailto:Julien.Soula@lifl.fr]
Sent: Thursday, September 21, 2000 5:00 AM
To: caml-list@inria.fr
Subject: scanning recursive variable type

hello

I have defined a type that allow me to recurse on constructor (option
and list):

# type 'a t = N of 'a | L of ('a list) t | O of ('a option) t;;
type 'a t = | N of 'a | L of 'a list t | O of 'a option t

So I can do this:

# let lo = [ Some 1; None ];;
val lo : int option list = [Some 1; None]
# let lot = O (L (N lo));;
val lot : int t = O (L (N [Some 1; None]))

Assume that I know how to pass from a function ('a->int) to functions
('a list->int) and ('a option->int) :

val fun_of_list : ('a -> int) -> 'a list -> int = <fun>
val fun_of_option : ('a -> int) -> 'a option -> int = <fun>

Now I want to apply such a function on ('a t). So I think of a
recursive function with signature:

val int_of_t : ('a->int) -> 'a t -> int = <fun>

I try to define this recursive function like that but... :

# let rec int_of_t f = function
N x -> f x
| L lx -> int_of_t (fun_of_list f) lx
| O ox -> int_of_t (fun_of_option f) ox ;;
Characters 67-80:
This expression has type 'a list -> int but is here used with type 'a -> int

I don't know how to make it otherway with this type ?

-- Julien

```