我正在尝试在Haskell中使用子类来将增量特异性应用于我的类并且没有运气。
我的第一次尝试:
module Subclassing where
class SuperClass a where
type TheType a :: *
theFunc :: TheType a -> TheType a
class SuperClass b => SubClass b where
type TheType b = Int
data MyType
instance SubClass MyType where
theFunc x = x + x
得出了这个:
Subclassing.hs:10:8: error:
‘TheType’ is not a (visible) associated type of class ‘SubClass’
Subclassing.hs:15:3: error:
‘theFunc’ is not a (visible) method of class ‘SubClass’
我想知道是否有一些语法方法可以将超类的类型/方法暴露给子类。所以,我在网上搜索了一下,但没有找到任何东西。
在我的第二次尝试中,我尝试通过定义超类的通用实例来强制解决问题,并限制在子类上:
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
module Subclassing where
class SuperClass a where
type TheType a :: *
theFunc :: TheType a -> TheType a
class SubClass b
instance SubClass c => SuperClass c where
type TheType c = Int
data MyType
instance SubClass MyType
instance SuperClass MyType where
theFunc x = x + x
testFunc :: SuperClass d => [TheType d] -> TheType d
testFunc = sum . (map theFunc)
它产生了这个:
Subclassing2.hs:25:23: error:
• Overlapping instances for SuperClass a0
arising from a use of ‘theFunc’
Matching givens (or their superclasses):
SuperClass d
bound by the type signature for:
testFunc :: SuperClass d => [TheType d] -> TheType d
at Subclassing2.hs:24:1-52
Matching instances:
instance SubClass c => SuperClass c
-- Defined at Subclassing2.hs:15:10
instance SuperClass MyType -- Defined at Subclassing2.hs:21:10
(The choice depends on the instantiation of ‘a0’)
• In the first argument of ‘map’, namely ‘theFunc’
In the second argument of ‘(.)’, namely ‘(map theFunc)’
In the expression: sum . (map theFunc)
在我的第三次尝试中,我尝试将子类设为类型而不是类。 (我意识到由于 newtype 值构造函数的单个字段限制,这会有限的适用性,但是没有想法。):
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
module Subclassing where
class SuperClass a where
type TheType a :: *
theFunc :: TheType a -> TheType a
newtype SubClass = SubClass { unSubClass :: Int -> Int }
instance SuperClass SubClass where
type TheType SubClass = Int
theFunc = unSubClass
testFunc :: SuperClass d => [TheType d] -> TheType d
testFunc = sum . (map theFunc)
它产生了这个:
Subclassing3.hs:17:13: error:
• Couldn't match type ‘Int -> Int’ with ‘Int’
Expected type: TheType SubClass -> TheType SubClass
Actual type: SubClass -> Int -> Int
• In the expression: unSubClass
In an equation for ‘theFunc’: theFunc = unSubClass
In the instance declaration for ‘SuperClass SubClass’
Subclassing3.hs:20:23: error:
• Couldn't match type ‘TheType a0’ with ‘TheType d’
Expected type: TheType a0 -> TheType d
Actual type: TheType a0 -> TheType a0
NB: ‘TheType’ is a type function, and may not be injective
The type variable ‘a0’ is ambiguous
• In the first argument of ‘map’, namely ‘theFunc’
In the second argument of ‘(.)’, namely ‘(map theFunc)’
In the expression: sum . (map theFunc)
• Relevant bindings include
testFunc :: [TheType d] -> TheType d
(bound at Subclassing3.hs:20:1)
答案 0 :(得分:0)
你的第一个猜测似乎对我来说足够明智,但不,Haskell不允许在(子)类定义中定义关联的类型或方法。