함께 교차 할 수있는 기하학적 객체의 유형 클래스를 정의하고 싶습니다.
class Intersect a b c | a b -> c where
intersect :: a -> b -> c
-- Language extensions: -XMultiParamTypeClasses, -XFunctionalDependencies
아이디어는 다른 유형의 객체를 처리 할 수있는 범용 교차 함수를 갖는 것입니다. 예를 들어
instance Intersect Line Plane (Maybe Point) where
...
instance Intersect Plane Plane (Maybe Line) where
...
그러나 나는 또한 교차점이 정식 적이라고 선언하고 싶다.
instance (Intersect a b c) => Intersect b a c where
intersect x y = intersect y x
-- Language extensions: -XUndecidableInstances
문제는 그 평가 점이다마다 intersect x y
제 형태의 인스턴스 정의하지 않고 Intersect a b c
, a
의 유형 x
과 b
의 타입을 y
, 프로그램이 무한 반복에 들어가는 아마도 교환 법칙에 대한 재귀 인스턴스 선언 의한. 이상적으로 intersect Egg Bacon
는 인스턴스가 정의되지 않았기 때문에 유형 검사에 실패하고 무한 루프에 빠지지 않기를 원합니다 . 이것을 어떻게 구현할 수 있습니까?
유형 패밀리를 사용하여 시도 할 수있는 것 같습니다. 스택 오버플로에 대해 더 나은 응답을 얻을 수 있습니다.
—
Benjamin Hodgson
다음은 commutativity를 시행하는 모나드에 관한 블로그 글입니다. gelisam.blogspot.ca/2013/07/the-commutative-monad.html
—
Daniel Díaz Carrete