« Return to Thread: tail recursion

Re: "ocaml_beginners"::[] Re: tail recursion

by Fabrice Marchant-2 :: Rate this Message:

Reply to Author | View in Thread

  Thanks a  lot,
 

> > let append =
> >   let rec rappend = function
> >       [] -> id
> >     | h::t -> rappend t <<- (cons h) in
> >     rappend <<- List.rev
>
> There is a problem on this code: it is not polymorphic enough, just
> try
>
> rappend [2] [3];;
 This works.

> rappend [None] [];;
  Yes, I discover you are right.

The type of this expression,
'_a option list -> '_a option list -> '_a option list,
contains type variables that cannot be generalized

Or maybe the lack of polymorphism you spoke about is related to these two lines taken together ?

It's not the first time I run into problems ditching last parameters.
 Please what are the things to think to, to detect that kind of problem ?

> if you want a fully polymorphic version, you need to wrote it as :
> let append l =
>    let rec rappend = function
>        [] -> id
>      | h::t -> rappend t <<- (cons h) in
>      (rappend <<- List.rev) l
>
> which defeat your goal.
A harsh term for half a failure : just one parameter is yet kept !

> > Is it absolutely true that the 3 tiny functions id, cons and ( <<- )
> > will be inlined without any efficiency loss ?
>
> Not sure, well, not with bytecode, I don't know with ocamlopt, you
> should test (option -S to ask ocamlopt for the assembly code).

 Looking at the generated code for comparison ( ugly 'as' syntax ) :
I've programmed several assemblers, 8086 too, but this compiler output - as often - is pretty unreadable.

However, I love the 'id' code : just 'ret' !

I cannot be assertive but this indirect call, depending on what lies on camlTrop+... :

       movl    %eax, 0(%esp)
       movl    camlTrop + 4, %ebx
       movl    (%eax), %eax
       movl    (%ebx), %ecx
       call    *%ecx

doesn't seem to refer to an OCaml internal but more probably to a not inlined function.

Adding a -inline 1 (or 2) option doesn't affect generated code.

Ciao,

Fabrice

 « Return to Thread: tail recursion