asibahi
asibahi

Reputation: 887

OCaml's set_signal's equivalent in F#

I searched for this a bit and could not find anything. I am "translating" an OCaml chess program to F#, both as a tool to understand how a Chess representation would work and to internalize, so to speak, F#'s way of doing things that is not OO.

These pieces of code are stumping me

set_signal sigint (Signal_handle (fun _ -> raise Interrupt));

and

set_signal sigint Signal_ignore;

Interrupt is an Exception defined earlier. Now I looked up what set_signal does (here) but I cannot figure out exactly what is its purpose here, or how sigint is defined at all. How can I replicate or imitate this behavior in F#.

If you want to see it in context, it is around line 532 in the OCaml source. This is the method in question:

let alpha_beta_deepening pos interval = 
    del_timer ();
    let current_best = ref (alpha_beta_search pos 2) in (* alpha_beta_seach _ 2   can only return legal moves *)
    ((try 
        set_signal sigint (Signal_handle (fun _ -> raise Interrupt));
        set_timer interval;
        let rec loop i = 
            if i > max_depth then () else
            let tmp = alpha_beta_search pos i in
            current_best := tmp;
            if (fst tmp) >=  win (* we can checkmate *)
            || (fst tmp) <= -win (* we get checkmated anyway, deny the opponent extra time to think *)
                then () else loop (i+1) 
        in loop 3;
        set_signal sigint Signal_ignore;
        del_timer ();
    with Interrupt -> ());
    set_signal sigint Signal_ignore;
    del_timer ();
    !current_best)

Upvotes: 2

Views: 150

Answers (1)

Lhooq
Lhooq

Reputation: 4441

That's the bad thing about namespaces, it's hard to know where things come from.

So, to start, sigint is defined in the Sys module :

val sigint : int

  Interactive interrupt (ctrl-C)

So, what

 set_signal sigint (Signal_handle (fun _ -> raise Interrupt));

and

 set_signal sigint Signal_ignore;

do ?

They just say to the system (set_signal communicate to the system what behaviour he should have on a particular signal) that when it catches a ctrl-C, in the first case it will raise Interrupt and in the second case it will do nothing.

Now that you have a better understanding of what it means, I think it's easy to implement it in F#, no ? ;-)

You could look at this, for example (look at both OCaml and F# codes)

Upvotes: 3

Related Questions