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

polymorphic recursion clarification
• Jacques Le Normand
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: -- (:) From: Jacques Le Normand Subject: polymorphic recursion clarification
```maybe I should state why I need polymorphic recursion
consider the function traverse which takes a function and an element of what
I want to traverse and returns a list of results.

let rec traverse f n=
let ret = f n in
let rest = match n#get_right_sibling_specific with
None -> []
| Some y -> traverse f y
in
let rest2 = match n#get_child_specific with
None -> []
| Some y -> traverse f y
in
match ret with
None -> rest @ rest2
| Some y -> y :: rest @ rest2

with type

val traverse :
((< get_child_specific : 'a option; get_right_sibling_specific : 'a
option;
.. >
as 'a) ->
'b option) ->
'a -> 'b list

the problem is that get_child_specific may not return an 'a option, but it
will always return a subtype of 'a option. is there any way to do this? ie,
give it a type

((< get_child_specific : #'a option; get_right_sibling_specific : #'a
option;
.. >
as 'a) ->
'b option) ->
'a -> 'b list

? (I believe this can be done with polymorphic recursion)
--Jacques

```