2

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)
asibahi
  • 857
  • 8
  • 14
  • The signal stuff is a Unixism. For your port, you need to come up with a less specific solution to the same problem. As you commented below yourself, Winboard also uses another approach. Also, if you compare the xboard protocol with UCI, you will notice that in UCI the UI assumes, that the command processing is running in parallel to the search of the engine. UCI engines typically check some (mutable,, nasty) flag to see if they should bail out of search. You could consider to do it the same here. – BitTickler Jun 11 '16 at 00:19

1 Answers1

3

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)

Lhooq
  • 4,281
  • 1
  • 18
  • 37
  • Thanks for the link, that actually explains a fair bit. I actually looked up the documentation for Xboard (the GUI software the app interacts with) and it uses these cancel signals to force an engine to move immediately. However apparently Winboard (the Win32 port) doesn't have them so I need not bother with them at all. – asibahi Jun 10 '16 at 12:50