我试图在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)在第三次尝试中,我尝试将子类变成类型而不是类。(我意识到,由于新类型值构造函数的单字段限制,这一方法的适用性将有限,但没有任何想法):
{-# 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)发布于 2017-09-07 16:05:14
您的第一个猜测对我来说是很明智的,但是没有,Haskell不允许在(子)类定义中定义关联的类型或方法。
https://stackoverflow.com/questions/46096938
复制相似问题