Haskell:类型族的实例定义 [英] Haskell: Instance definitions for type families

查看:121
本文介绍了Haskell:类型族的实例定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我们有以下代码:

  class C t其中
g :: t

实例C Int其中
g = 42

简单。我们还可以在Int上定义函数,如下所示:

  f1 :: Int  - > Int 
f1 x = x * x

我一直在使用类型族,特别是因为 数据.Has 使用它们,我想将它们插入 IxSet

但是,我要介绍一个简单的例子。假设我们要定义一个新的类型 X ,这与Int类似。我们可以这样做:

 类型系列X 
类型实例X = Int
X
上的函数:

  f2 :: X  - > X 
f2 x = x * x + 1

到目前为止没有问题。现在让我们尝试定义一个实例 CX ,就像我们为 C Int 所做的那样:

 实例CX其中
g = 43

呃哦,现在我们有以下错误:


非法类型同义词族应用程序实例: X

'C X'的实例声明中


现在让我们尝试一些有点不同:

  newtype NewX = NewX X 

实例C NewX其中
g = 43

现在我们有了另一个错误,即:


没有的实例(Num NewX)
$



看起来像是 newtype 关键字消除了关于上一个类属于哪个类的任何信息。但是,我似乎也无法避免 newtype ,因为我不能在实例定义中使用类型族。



有没有更好的方法来做到这一点,而不必重写实例定义,其他明确的实例提及否则将被推断?






>

  import Data.Has 
import Data.IxSet

data Col1 = Col1;类型实例TypeOf Col1 =文本
数据Col2 = Col2;类型实例TypeOf Col2 =文本

类型Row = FieldOf Col1:&:FieldOf Col2;

实例可索引行其中
empty = ixSet [ixFun $(\ x - > [Col1 ^。x])] - 也许稍后再添加一些索引

失败:


实例中非法类型同义词族应用程序: Row


'可索引行'的实例声明中


制作行 a newtype 导致以下错误:


没有用于(包含(标记的Col1文本)行)的实例
从a使用`^。'
可能的修正:

添加一个实例声明(包含(Labeled Col1 Text)行)

我可以解决这个问题的唯一方法是添加一个很长的派生子句,如下所示:

  newtype Row = Row(FieldOf Col1:&:FieldOf Col2)
导出

包含(Labeled Col1 Text), - 添加此项对于每一列
包含(Labeled Col2 Text) - ...

甚至允许我使用typedef Contains(Labeled x(TypeOf x))来说 HasCol x

定义了一个新类型,而类型定义了一个同义词。如果你不喜欢一堆派生子句,总是可以使用与底层类型同构的

 实例C NewX其中
g = NewX 43

原因类型同义词不要和实例声明一起玩,是函数(包括类型函数)只能在一个方向上工作。您只能对构造函数进行模式匹配,因此 newtype 允许您以零运行时代价引入一个新的类型构造函数。在你的问题中,为什么不是

  newtype Row = Row {runRow :: FieldOf Col1:&:FieldOf Col2} 

实例可索引行其中
empty = ixSet [ixFun $(\ x - > [Col1 ^。(runRow x)])]
pre>

我应该注意,通常 GeneralizedNewtypeDeriving 是不合适的。这并不意味着你应该避免使用它,但暗示你想要的可能是不可能的。



更好的是,甚至不需要改变数据类型Row

  newtype Row = Row(FieldOf Col1:&:FieldOf Col2)

instance可索引行其中
empty = ixSet [ixFun $(\(Row x) - > [Col1 ^。x]) ]


Lets say we have the following code:

class C t where
  g :: t

instance C Int where
  g = 42

Simple. We can also define functions on Int, like so:

f1 :: Int -> Int
f1 x = x * x

I've been working with type families, in particular because Data.Has uses them, and I want to insert them into an IxSet.

But here I'm going to present a simplified example. Lets say we want to define a new type X, that is similar to an Int. We could do this:

type family X
type instance X = Int

We can then define functions on X like so:

f2 :: X -> X
f2 x = x * x + 1

No problems so far. Now lets try to define an instance C X, like we did for C Int:

instance C X where
  g = 43

Uh oh, now we have the following error:

Illegal type synonym family application in instance: X
In the instance declaration for 'C X'

Now lets try something a bit different:

newtype NewX = NewX X

instance C NewX where
  g = 43

Now we've got another error, namely:

No instance for (Num NewX)
arising from the literal '43'

It seems like the newtype keyword eliminates any information about what classes the previous class belonged too. However, it also seems I can't avoid newtype, as I can't use type families in instance definitions.

Is there a better way to do this without having to rewrite instance definitions with additional explicit instances mentions which otherwise would be inferred?


Background information:

The reason why I need this to work is the following:

import Data.Has
import Data.IxSet

data Col1 = Col1; type instance TypeOf Col1 = Text
data Col2 = Col2; type instance TypeOf Col2 = Text

type Row = FieldOf Col1 :&: FieldOf Col2;

instance Indexable Row where
  empty = ixSet [ixFun $ (\x -> [ Col1 ^. x ]) ] -- Maybe add some more indexes later

This fails with:

Illegal type synonym family application in instance: Row
In the instance declaration for 'Indexable Row'

Making Row a newtype causes the following error:

No instance for (Contains (Labelled Col1 Text) Row) arising from a use of `^.' Possible fix: add an instance declaration for (Contains (Labelled Col1 Text) Row)

The only way I can work around this is by adding a long deriving clause as follows:

newtype Row = Row (FieldOf Col1 :&: FieldOf Col2)
  deriving 
  (
    Contains (Labelled Col1 Text), -- Add this for every column
    Contains (Labelled Col2 Text)  -- ...
  )

Even something which allows me to "typedef" Contains (Labelled x (TypeOf x)) to say HasCol x would be helpful.

解决方案

A newtype does just that--it defines a new type, while a type defines a synonym. If you dont like a bunch of deriving clauses, one can always use the isomorphism with the underlying type

instance C NewX where
   g = NewX  43

the reason type synonyms dont play nice with Instance declarations, is that functions (including type functions) only work in one direction. You can only pattern match on constructors, so newtype allows you to introduce a new type constructor at zero runtime cost. In your problem, why not

newtype Row = Row {runRow :: FieldOf Col1 :&: FieldOf Col2}

instance Indexable Row where
  empty = ixSet [ixFun $ (\x -> [ Col1 ^. (runRow x) ]) ]

I should note that in general GeneralizedNewtypeDeriving is unsound. Doesn't mean you should avoid using it, but does imply what you want is probably impossible.


Edit (Question asker):

Better yet, no need to even change the data type Row

newtype Row = Row ( FieldOf Col1 :&: FieldOf Col2 )

instance Indexable Row where
  empty = ixSet [ixFun $ (\(Row x) -> [ Col1 ^. x ]) ]

这篇关于Haskell:类型族的实例定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆