Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 类型级递归和多种类_Haskell_Type Level Computation_Polykinds - Fatal编程技术网

Haskell 类型级递归和多种类

Haskell 类型级递归和多种类,haskell,type-level-computation,polykinds,Haskell,Type Level Computation,Polykinds,我试图实现一个多态函数,它基本上遍历一个类型,积累一个标记值。我希望用户能够执行例如rec((1,2),('a',3)) 我以各种方式处理过这个问题,在当前的化身中,我得到了错误(首先,对于ab和c): 我对这个错误感到有点惊讶,因为它似乎表明第二个基本情况实例是在第一个实例的主体中预先选择的 有什么方法可以让它发挥作用,还是有更好的方法?我认为你应该跳过两节课。一个就够了 缺点是您的用户将无法编写与您的设计中的实例标记(可能是Int)对应的实例,这些实例对复合类型有特殊作用。。。但是他们已经不

我试图实现一个多态函数,它基本上遍历一个类型,积累一个
标记
值。我希望用户能够执行例如
rec((1,2),('a',3))

我以各种方式处理过这个问题,在当前的化身中,我得到了错误(首先,对于
ab
c
):

我对这个错误感到有点惊讶,因为它似乎表明第二个基本情况实例是在第一个实例的主体中预先选择的


有什么方法可以让它发挥作用,还是有更好的方法?

我认为你应该跳过两节课。一个就够了

缺点是您的用户将无法编写与您的设计中的
实例标记(可能是Int)
对应的实例,这些实例对复合类型有特殊作用。。。但是他们已经不能真正使用这些,因为
Rec
的应用程序实例无论如何都会与它重叠

因此,不言而喻:

{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}

import Data.Tagged

class Rec a where rec :: Tagged a Int

-- it is still possible for users to define their own instances for base types
instance Rec Int  where rec = 1
instance Rec Char where rec = 2
instance Rec (,)  where rec = 3

instance (Rec ab, Rec c) => Rec (ab c) where
  rec = retag (rec :: Tagged ab Int) * retag (rec :: Tagged c Int)
在ghci中:

> rec :: Tagged ((Int, Int), Char) Int
Tagged 18

为什么一个类使用代理而不是另一个?哦,是的,这更好,谢谢。我没有想到,我实际上也不需要这种方法的重叠实例
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}

import Data.Tagged

class Rec a where rec :: Tagged a Int

-- it is still possible for users to define their own instances for base types
instance Rec Int  where rec = 1
instance Rec Char where rec = 2
instance Rec (,)  where rec = 3

instance (Rec ab, Rec c) => Rec (ab c) where
  rec = retag (rec :: Tagged ab Int) * retag (rec :: Tagged c Int)
> rec :: Tagged ((Int, Int), Char) Int
Tagged 18