2011-08-18 5 views
3

내가 OCaml의이 트라이 구현을 사용하려고 해요 :OCaml의 트라이 구현

module M = 
    struct  
    type key = int 
    type 'a t = (int * 'a) list 
    let empty = [] 
    let equal x y = false 
    let compare f x y = 1 

    let find x l = 
     let pred e = fst e == x in 
     let z = List.find pred l in 
     snd z 

    let add x t m = (x,t)::m 

    let remove x m = filter (fun e -> fst e != x) m 

    let map f m = 
     let aux (x,y) = (x, f y) in 
     List.map aux m 

    let mapi f m = 
     let aux i (x,y) = (x, f i y) in 
     List.mapi aux m 

    let mem x m = 
     let l = List.map snd m in 
     List.mem x l 

    let iter f m = 
     let aux x = f (snd x) in 
     List.iter aux m 

    let fold f m acc1 = 
     let aux acc x = f acc (snd x) in 
     List.fold_left aux acc1 m    

    let is_empty m = m == empty 

end;; 

가 (비교 등 같음) 잘못된 의미를 무시 :이 모듈 "M"내 구현 http://www.lri.fr/~filliatr/ftp/ocaml/ds/trie.ml.html

입니다 .

나는 OCaml의 배터리를 사용하고, 나는이 작품 만들려고 방법이 있습니다 :

# #use "trie.ml";; 
module Make : 
    functor (M : Batteries.Map.S) -> 
    sig 
     type key = list M.key; 
     type t 'a = [ Node of option 'a and M.t (t 'a) ]; 
     value empty : t 'a; 
     value find : list M.key -> t 'a -> 'a; 
     value mem : list M.key -> t 'a -> bool; 
     value add : list M.key -> 'a -> t 'a -> t 'a; 
     value remove : list M.key -> t 'a -> t 'a; 
     value map : ('a -> 'b) -> t 'a -> t 'b; 
     value mapi : (list M.key -> 'a -> 'b) -> t 'a -> t 'b; 
     value iter : (list M.key -> 'a -> 'b) -> t 'a -> unit; 
     value fold : (list M.key -> 'a -> 'b -> 'b) -> t 'a -> 'b -> 'b; 
     value compare : ('a -> 'a -> int) -> t 'a -> t 'a -> int; 
     value equal : ('a -> 'a -> bool) -> t 'a -> t 'a -> bool; 
     value is_empty : t 'a -> bool; 
    end 
# #use "m.ml";; 
module M : 
    sig 
    type key = int; 
    type t 'a = list (int * 'a); 
    value empty : list 'a; 
    value equal : 'a -> 'b -> bool; 
    value compare : 'a -> 'b -> 'c -> int; 
    value find : 'a -> list ('a * 'b) -> 'b; 
    value add : 'a -> 'b -> list ('a * 'b) -> list ('a * 'b); 
    value remove : 'a -> BatEnum.t ('a * 'b) -> BatEnum.t ('a * 'b); 
    value map : ('a -> 'b) -> list ('c * 'a) -> list ('c * 'b); 
    value mapi : (int -> 'a -> 'b) -> list ('c * 'a) -> list ('c * 'b); 
    value mem : 'a -> list ('b * 'a) -> bool; 
    value iter : ('a -> unit) -> list ('b * 'a) -> unit; 
    value fold : ('a -> 'b -> 'a) -> list ('c * 'b) -> 'a -> 'a; 
    value is_empty : list 'a -> bool; 
    end 

# module X = Make(M);; 
Error: Signature mismatch: 
     Modules do not match: 
     sig 
      type key = int; 
      type t 'a = list (int * 'a); 
      value empty : list 'a; 
      value equal : 'a -> 'b -> bool; 
      value compare : 'a -> 'b -> 'c -> int; 
      value find : 'a -> list ('a * 'b) -> 'b; 
      value add : 'a -> 'b -> list ('a * 'b) -> list ('a * 'b); 
      value remove : 'a -> BatEnum.t ('a * 'b) -> BatEnum.t ('a * 'b); 
      value map : ('a -> 'b) -> list ('c * 'a) -> list ('c * 'b); 
      value mapi : (int -> 'a -> 'b) -> list ('c * 'a) -> list ('c * 'b); 
      value mem : 'a -> list ('b * 'a) -> bool; 
      value iter : ('a -> unit) -> list ('b * 'a) -> unit; 
      value fold : ('a -> 'b -> 'a) -> list ('c * 'b) -> 'a -> 'a; 
      value is_empty : list 'a -> bool; 
     end 
     is not included in 
     Batteries.Map.S 
     The field `Labels' is required but not provided 
     The field `Infix' is required but not provided 
     The field `Exceptionless' is required but not provided 
     The field `print' is required but not provided 
     The field `of_enum' is required but not provided 
     The field `backwards' is required but not provided 
     The field `enum' is required but not provided 
     The field `choose' is required but not provided 
     The field `max_binding' is required but not provided 
     The field `min_binding' is required but not provided 
     The field `values' is required but not provided 
     The field `keys' is required but not provided 
     The field `filter_map' is required but not provided 
     The field `filteri' is required but not provided 
     The field `filter' is required but not provided 
     The field `modify' is required but not provided 
# 

나는이 오류를 이해하지 못하고 있습니다. 모듈 "M"을 구현할 때 사용할 수있는 메소드 유형은 유효합니다.

ocaml이 TRIE의 구현 (레이블, 삽입 문자 등)에서 필요하지 않은 필드를 원하는 이유를 이해할 수 없습니다.

답변

5

귀하의 최상위 디렉토리에 먼저 open Batteries;;과 같은 내용을 입력해야합니다. 이로 인해 trie.ml의 module Make (M : Map.S) = structBatteries.Map.S이라는 인수의 펑터 Make을 정의하는 것으로 해석됩니다.

Batteries.Map.S에는 Map.S 표준의 모든 유형, 방법 등이 포함되어 있으므로 Make을 적용하려고 할 때만 # # trie.ml을 사용할 때 문제를 알 수 없습니다. 그러나 Jean-Christophe Filliâtre는 그의 파일을 쓸 때 표준 Map.S을 고안했습니다. #rie 대신에 trie.ml을 컴파일했다면 Map.S이 표준 라이브러리의 것으로 해석 될 것입니다. trie.ml을 컴파일하고 #loading하는 또 다른 장점은 trie 모듈을 만드는 functor가 Trie.Make이라는 것입니다 (다시 Jean-Christophe의 의도대로 : Make 만 모호하며 다른 모듈에서 규칙을 사용하는 경우에만 사용됩니다). 컨텍스트 : Hashtbl.Make, Set.Make, ...)