2016-07-26 11 views
4

하스켈의 Data.Collection 라이브러리의 디자인을 이해하려고 시도 중입니다. 스칼라에 대해 잘 알고있는 배경에서 왔습니다.Haskell Collection API의 'Unfoldable'typeclass에서의 기능적 종속성의 역할

(Scala analog을 사용하는) Functional Dependencies을 사용하지만 사용 방법은 나에게 맞지 않습니다. 아래에 재현 된 클래스에서 요소 유형 i은 컬렉션 유형 c으로 결정되는 으로 표시됩니다. 관측 요소와 수집의

class Unfoldable c i | c -> i 

클래스입니다. Foldable 클래스의 이중입니다.

여기에 c -> i의 종속성이있는 역할과 디자인 의도, 이상적으로는 사용 예를 설명해주십시오.

답변

7

해당 기능 종속성에 의해 표현되는 제한 조건은 컬렉션 유형 c이 주어진 경우 해당 항목 유형이 i 인 것으로 결정됩니다. 예를 들어, c ~ [a], 즉 컬렉션이 a의 목록 인 경우 i ~ a을 확인할 수 있어야합니다.

기능 의존성이 없으면 우리는 두 개의 인스턴스를 가질 수 있습니다. Unfoldable [a] a (명백한/예정된 인스턴스) 및 Unfoldable [a] [a] (insert = concat, singleton = id과 같은 것).

{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-} 

class Unfoldable c i where 
    empty :: c 

instance Unfoldable [a] a where 
    empty = [] 

instance Unfoldable [a] [a] where 
    empty = [] 

xs :: [a] 
xs = empty 

을이 결과 :

No instance for (Unfoldable [a] i0) arising from a use of `empty' 
The type variable `i0' is ambiguous 
Relevant bindings include 
    xs :: [a] 
Note: there are several potential instances: 
    instance Unfoldable [a] a 
    instance Unfoldable [a] [a] 
In the expression: empty 
In an equation for `xs': xs = empty 
typechecker 다음 empty :: [a] 같은 것을 볼 경우, 인스턴스가 사용하는 선택의 방법이 없습니다