プログラム等価性クイズの答え

第2回プログラム等価性クイズ - sumiiのブログの答え。













f5とかf6とかの定義は、何かの答 - camlspotter’s blogからコピペしました。

let rec d () =
  d ()

let f5 : ((unit -> unit) -> unit) -> unit = fun g -> g d

let f6 : ((unit -> unit) -> unit) -> unit = fun g ->
  let y = ref 0 in
  let h () = y := 1 in  g h;
  if !y = 1 then d ()

let answer f5_or_f6 =
  let d_or_h = ref None in
    f5_or_f6 (fun x -> d_or_h := Some x) ;
    match !d_or_h with
	Some x -> x ()
      | None -> failwith "must not happen"