Haskell:类型检查是否需要所有实例?

时间:2018-10-07 20:03:40

标签: haskell types

我正在研究如何在复杂的类层次结构中使用类型类实例来检查方法的类型正确性。该问题与previous post有关,但与paper不同。

BoatHousesHouseBoats类位于类层次结构的底部,因此需要先存在超类的实例,然后才能实例化它们。当我尝试为这些类构造实例时,我发现必须添加72个实例才能满足编译器的要求。我首先尝试为BoatHousesHouseBoats创建实例,然后遵循编译器消息来了解需要实例化哪些其他类,从而得出数字72。在下面的 Listing 1 (省略大多数方法签名)和 Listing 2 之下,有13种数据类型,14种类,45个类型变量。这段代码基于{{3}},但是实际的细节和目的对于我的问题并不重要。

问题

要键入在复杂类格的下端指定的检查方法,是否真的有必要实例化所有编译器建议的类?

列出1

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-#  OPTIONS_GHC  -Wno-missing-methods #-} 
class Containers a b where
     insert :: b -> a b -> a b
     remove :: b -> a b -> a b
     whatsIn :: a b -> [b]


    class Surfaces a b where
     put ::  b -> a b -> a b
     takeOff :: b -> a b -> a b
     whatsOn :: a b -> [b]

    class Contacts a b where
     attach :: b -> a b -> a b
     detach :: b -> a b -> a b
     whatsAt :: a b -> [b]

    class Paths a b c where
     move :: c -> a b c -> a b c
     origin, destination :: a b c -> b
     whereIs :: a b c -> c -> b

    class People p
    class HeavyLoads l
    class Surfaces w o => WaterBodies w o
    class Containers h o => Houses h o
    class (Surfaces v o, Paths a b (v o)) => Vehicles v o a b
    class (Vehicles v o a b, WaterBodies w (v o)) => Vessels v o a b w
    class (Vessels v p a b w, People p) => Boats v p a b w
    class (Boats v p a b w, HeavyLoads p) => Barges v p a b w
    class (Houses h (v p), Boats v p a b w, Contacts w (h (v p))) => BoatHouses h v p a b w
    class (Barges v p a b w, Houses v p, People p) => HouseBoats v p a b w

清单2

data Person = Person deriving Show
data Vehicle a =  Vehicle a deriving Show
data Vessel a = Vessel a deriving Show
data House a = House a  deriving Show
data HouseBoat a = HouseBoat a  deriving Show
data Boat a  = Boat a deriving Show
data BoatHouse a  = BoatHouse a deriving Show
data Barge a = Barge  a deriving Show
data Path a b = Path a b deriving Show
data Source = Source deriving Show
data Destination = Destination deriving Show
data Water a = Water a deriving Show
data Land a = Land a deriving Show

0 个答案:

没有答案