顶级Ocaml无穷解释循环

时间:2018-12-04 18:01:10

标签: compiler-errors ocaml interpreter

此代码不会输出语法错误,但也不会结束编译。当我注释toutes_reponses函数时,代码将编译并执行。 toutes_reponses是否在一种无限解释器循环中运行?

我使用ocaml#use "code.ml"进行编译。

此外,CPU在此期间以100%的速度运行。

#load "str.cma";;
module Code:
sig
        (** Le type d'un pion *)
        type pion = string

        (** Le type d'un code *)
        type t = pion list

        (** Le nombre de pions par code*)
        val nombre_pion : int          

        (** Le nombre de couleur et symboles possibles*)
        val couleur_possibles : pion list

        (** Compare deux codes
         *  @param code1 premier code a comparer
         *  @param code2 second  code a comparer
         *  @return 0 si les deux codes sont identiques,
                      un entier positif si [code1] est strictement plus grand que [code2]
                      un entier negatif si [code1] est strictement plus petit que [code2]       
         *)
        val compare : t -> t -> int

        (** Conversion chaine de caracteres vers code (pour saisie) 
         *  @param string chaine de caractere saisie
         *  @return le code correspondant a la saisie si la conversion est     possible
                      [None] si la conversion n'est pas possible
         *)
        val string_of_code : t -> string

        (** Conversion chaine de caracteres vers code (pour saisie)
         *  @param string chaine de caractere saisie
         *  @return le code correspondant a la saisie si la conversion est possible
                      [None] si la conversion n'est pas possible
     *)
        val code_of_string : string -> t

        (** La liste de tous les codes permis *)
        val tous : t list

        (** La liste de toutes les reponses possibles *)  
        val toutes_reponses : (int * int) list

        (** Calcule la reponse d'un code par rapport au code cache
         *  @param      code le code propose
         *  @param vrai_code le code cache
         *  @return un couple (nombre de pions bien places, nombre de pions mal places)
            [None] si la reponse ne peut etre calculee
         *)
        val reponse : t -> t -> (int * int)

        val convert : int -> int -> int -> int list -> int list

        val finirTableau : int -> int list -> int list
end = struct    
        type pion = string;;
        type t = pion list;;
        let nombre_pion = 4;;
        let couleur_possibles = ["Rouge"; "Vert"; "Bleu"; "Orange"; "Noir"; "Blanc"];;

        let compare code1 code2 =
          match (code1, code2) with
          | a::b, c::d -> (if a = c then compare b d else
                                  if a > c then 1 else -1)
          | _, _ -> 0;;

        let string_of_code code = List.fold_right (fun x y -> x ^ "_" ^ y) code "";;

        let code_of_string code = List.filter (fun x -> String.length x != 0) (Str.split_delim (Str.regexp "_") code);;

        let rec convert x b i tmp =
          if x = 0 then tmp
          else convert (x / b) b (i + 1) tmp @ [x mod b];;

        let rec finirTableau b tab =
          if (List.length tab) = b then tab
          else finirTableau b (tab @ [0]);;

        let tous =
          let n = List.length couleur_possibles and m = nombre_pion in
          let rec aux i acc =
            if i = -1 then acc else aux (i - 1) acc @ [(List.map (fun x -> (List.nth couleur_possibles x))(finirTableau m (convert i n 0 [])))];
          in aux (int_of_float ((float_of_int n) ** (float_of_int m)) - 1) [];;

        let toutes_reponses =
          let m = nombre_pion in
          let rec aux i acc =
            let array = finirTableau 2 (convert i m 0 []) in
            if i = -1 then acc else aux (i - 1) (acc @ [(List.nth array 0, List.nth array 1)]);
          in aux (nombre_pion * nombre_pion) [];;

        let reponse code secret = (1,2);;
end;;

2 个答案:

答案 0 :(得分:1)

您的代码编译良好,它只是在tous之前的函数之一中陷入无限循环。要调试此问题,可以先添加断言语句来记录递归函数的输入条件。

还请注意,代码中存在许多低效率的地方,应避免:

  • List.nth(如果需要随机访问,则应使用_ array的数据结构)
  • 附加在列表末尾(例如tab @ [0])。通常,@左侧的列表应该很小。
  • 在递归函数中调用List.length

类似地,如果要在字符上分割字符串,则标准库中有String.spilt_on_char(因为OCaml≥4.04)。

答案 1 :(得分:0)

toutes_reponses的类型是

toutes_reponses : (int * int) list

将文件加载到解释器中会导致评估所有声明的术语,包括该术语。显然,您陷入了评估的困境。如果您希望能够在解释器中加载文件,请将其转换为暂停的计算,

toutes_reponses : unit -> (int * int) list

let toutes_reponses () = ...