Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8594959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:19:13+00:00 2026-06-12T00:19:13+00:00

I am converting the OCaml Format module to F# and tracked a problem back

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:19:14+00:00Added an answer on June 12, 2026 at 12:19 am

    This should do it:

    module Pervasives =
        open System
        open System.Threading
    
        //
        let mutable private exitFunctions : (unit -> unit) list = List.empty
    
        //
        let mutable private exitFunctionsExecutedFlag = 0
    
        //
        let private tryExecuteExitFunctions _ =
            if Interlocked.CompareExchange (&exitFunctionsExecutedFlag, 1, 0) = 0 then
                // Run the exit functions in last-in-first-out order.
                exitFunctions
                |> List.iter (fun f -> f ())
    
        // Register handlers for events which fire when the process exits cleanly
        // or due to an exception being thrown.
        do
            AppDomain.CurrentDomain.ProcessExit.Add tryExecuteExitFunctions
            AppDomain.CurrentDomain.UnhandledException.Add tryExecuteExitFunctions
    
        //
        let at_exit f =
            // TODO : This function should be re-written using atomic operations
            // for thread-safety!
            exitFunctions <- f :: exitFunctions
    

    And some code to test it:

    open System
    
    // Register a couple of handlers to test our code.
    Pervasives.at_exit <| fun () ->
        Console.WriteLine "The first registered function has fired!"
    
    Pervasives.at_exit <| fun () ->
        Console.WriteLine "The second registered function has fired!"
        TimeSpan.FromSeconds 1.0
        |> System.Threading.Thread.Sleep
        Console.WriteLine "Exiting the second registered function!"
    
    Pervasives.at_exit <| fun () ->
        Console.WriteLine "The third registered function has fired!"
    
    // Do some stuff in our program
    printfn "blah"
    printfn "foo"
    printfn "bar"
    
    (* The functions we registered with at_exit should be fired here. *)
    
    // Uncomment this to see that our handlers work even when the
    // program crashes due to an unhandled exception.
    //failwith "Uh oh!"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am converting the OCaml Format module to F#; see my earlier question .
Background Converting a document from OpenOffice to DocBook format. Problem Parts of the document
While converting an old code, I encountered the following problem. Given an HTML string,
After converting to Visual Studio 2010 with ReSharper5 some of my unit tests started
I am converting several modules based on OCaml to F# and ran into the
I am converting several modules based on OCaml to F# and ran into something
While converting a webdesign given as a photoshop file to html+css, I got confused
I am converting several modules based on OCaml to F#. I have the code
I am converting several modules from OCaml to F#. To hunt bugs and verify
Converting numbers from double-precision floating-point format to single-precision floating-point format results in loss of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.