2017-10-18 15 views
2

아래와 같이 비교 세트를 쓰고 싶습니다.sml의 타입 클래스를 달성하는 방법

signature COMPARABLE_SET= 
sig 
    type 'a set 
    val empty: 'a set 
    val insert: 'a * 'a set -> 'a set 
    val member: 'a * 'a set -> bool 
end 

난 비교 가능하도록 설정 유형 '의 요소를 제한 할 필요가 (종류와 기능이있다 :이 'a * 'a -> order).

어떻게 구현합니까?

+0

SML/NJ 라이브러리의'ORD_SET' 서명이 어떻게 정의되는지보십시오 : http://www.smlnj.org/doc/smlnj-lib/Manual/ord-set.html#ORD_SET:SIG : SPEC –

+1

또한 원하는 것은 SML로 안전하게 작성할 수 없습니다. 이 주제와 관련된 두 개의 블로그 게시물 (http://igstan.ro/posts/2017-04-08-a-safe-type-indexed-set-for-standard-ml.html 및 http : //)을 작성했습니다. igstan.ro/posts/2017-04-12-a-safe-type-indexed-set-for-standard-ml-errata.html. –

답변

4

당신은 OCaml이 그것을하고 싶은 경우에, 이것은 단순히 펑터 경우입니다 :

첫째, 당신은 당신의 요소의 형태를 정의해야합니다

module type OrderedType = sig 
    type t 
    val compare : t -> t -> int 
end 

을 그리고 당신은을 정의 할 수 있습니다 이 유형의 펀터 :

module MakeComparableSet (Ord : OrderedType) : 
    sig 
    type elt = Ord.t 
    type t 
    val empty : t 
    val insert : elt -> t -> t 
    val member : elt -> t -> bool 
    end = struct 
    type elt = Ord.t 
    type t 
    let empty = failwith "TODO" 
    let insert = failwith "TODO" 
    let member = failwith "TODO" 
    end 

정확하게 만들어진 것은 here입니다.


새로운 모듈을 만들 모듈에서 함수로 functor를 볼 수 있습니다. 여기에서, 펑터 ComparableSet은 서명 된 OrderedType의 모듈을 취해서 하나의 세트 인 모듈을 리턴한다.