首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何应用增量特异性,在Haskell中使用子类?

如何应用增量特异性,在Haskell中使用子类?
EN

Stack Overflow用户
提问于 2017-09-07 12:55:06
回答 1查看 163关注 0票数 1

我试图在Haskell中使用子类来对我的类应用增量专用性,而没有运气。

我第一次尝试:

代码语言:javascript
复制
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

产生了这样的结果:

代码语言:javascript
复制
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’

我想知道是否有一些语法方法可以将超类的类型/方法公开给子类。所以,我在网上搜索了一下,但什么也没找到。

在第二次尝试中,我试图通过定义超类的一个通用实例来强制解决这个问题,该子类受到限制:

代码语言:javascript
复制
{-# 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)

它产生了以下结果:

代码语言:javascript
复制
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)

在第三次尝试中,我尝试将子类变成类型而不是类。(我意识到,由于新类型值构造函数的单字段限制,这一方法的适用性将有限,但没有任何想法):

代码语言:javascript
复制
{-# 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)

它产生的结果是:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-07 16:05:14

您的第一个猜测对我来说是很明智的,但是没有,Haskell不允许在(子)类定义中定义关联的类型或方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46096938

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档