4

Is there a way to get ocaml to tell me if a function implements a recursion using tail recursion? I don't mean reading the code. I mean getting ocaml to tell me, say like this:

let x = tail_recursion f;;
Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
user2926204
  • 111
  • 8

1 Answers1

4

You can compile your source code with '-annot'. It will produce an annotation file, that some editors can use.

In caml-mode (emacs) the command ist:

 M-x caml-types-show-call
rafix
  • 1,739
  • 9
  • 9
  • I did ocaml -annot a.ml. But I don't see an annotation file. I have a.cmi, a.cmo, a.o. – user2926204 Feb 07 '14 at 04:52
  • Also, instead of emacs/xemacs, is there a command line tool that I can use? – user2926204 Feb 07 '14 at 04:57
  • Sorry. I mistyped. I did do "ocamlc -annot a.ml. I don't have any "annotation file". I'm using 4.00.1. – user2926204 Feb 07 '14 at 05:03
  • 2
    `ocamlc -annot foo.ml` should produce `foo.annot`, unless there are type errors. (If `foo.ml` depends on other modules you'll have to pass them as usual, too.) – gsg Feb 07 '14 at 05:17
  • No. I don't have a.annot. I'm sure my a.ml file is ok. It's just: let rec ffffffffff x = if x == 0 then 0 else ffffffffff (x - 1);; – user2926204 Feb 07 '14 at 05:21
  • Nevermind. My mistake. I types "ocamlc a.ml -annot" instead of "ocamlc -annot a.ml". Thanks everyone. – user2926204 Feb 07 '14 at 05:26
  • According to the earlier reply, I should be able to tell from the annotation file on whether a recursive function uses tail or not. I'm reading the annotation and I don't see any direct information on that. Is there a tool to interpret the annotation file? – user2926204 Feb 07 '14 at 05:31
  • 2
    You should get an entry that looks something like `"test.ml" 1 0 19 "test.ml" 1 0 22\ncall(\n tail\n)`. Note that this refers to the textual extent of a *call site* (not a function definition) within the source file. I'm not sure if there are any editor-independent tools to parse this information, as I just use emacs. – gsg Feb 07 '14 at 07:17