Converting OCaml to F#: F# equivelent of Pervasives at_exit

Posted by Guy Coder on Stack Overflow See other posts from Stack Overflow or by Guy Coder
Published on 2012-09-15T23:57:44Z Indexed on 2012/09/16 3:38 UTC
Read the original article Hit count: 179

Filed under:
|
|

I am converting the OCaml Format module to F# and tracked a problem back to a use of the OCaml Pervasives at_exit.

val at_exit : (unit -> unit) -> unit

Register the given function to be called at program termination time. The functions registered with at_exit will be called when the program executes exit, or terminates, either normally or because of an uncaught exception. The functions are called in "last in, first out" order: the function most recently added with at_exit is called first.

In the process of conversion I commented out the line as the compiler did not flag it as being needed and I was not expecting an event in the code.

I checked the FSharp.PowerPack.Compatibility.PervasivesModule for at_exit using VS Object Browser and found none.

I did find how to run code "at_exit"? and How do I write an exit handler for an F# application?

The OCaml line is

at_exit print_flush 

with print_flush signature: val print_flush : (unit -> unit)

Also in looking at the use of it during a debug session of the OCaml code, it looks like at_exit is called both at the end of initialization and at the end of each use of a call to the module.

Any suggestions, hints on how to do this. This will be my first event in F#.

EDIT

Here is some of what I have learned about the Format module that should shed some light on the problem.

The Format module is a library of functions for basic pretty printer commands of simple OCaml values such as int, bool, string. The format module has commands like print_string, but also some commands to say put the next line in a bounded box, think new set of left and right margins. So one could write:

print_string "Hello"

or

open_box 0; print_string "<<";
open_box 0; print_string "p \/ q ==> r"; close_box();
print_string ">>"; close_box()

The commands such as open_box and print_string are handled by a loop that interprets the commands and then decides wither to print on the current line or advance to the next line. The commands are held in a queue and there is a state record to hold mutable values such as left and right margin.

The queue and state needs to be primed, which from debugging the test cases against working OCaml code appears to be done at the end of initialization of the module but before the first call is made to any function in the Format module. The queue and state is cleaned up and primed again for the next set of commands by the use of mechanisms for at_exit that recognize that the last matching frame for the initial call to the format modules has been removed thus triggering the call to at_exit which pushes out any remaining command in the queue and re-initializes the queue and state.

So the sequencing of the calls to print_flush is critical and appears to be at more than what the OCaml documentation states.

© Stack Overflow or respective owner

Related posts about events

Related posts about F#