1
การใช้คลาสชนิด Haskell เพื่อบังคับใช้การสับเปลี่ยน
ฉันต้องการกำหนดคลาสประเภทสำหรับวัตถุทางเรขาคณิตที่สามารถตัดกันด้วยกัน: 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 …