Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.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 Families_Idris - Fatal编程技术网

Haskell类型族

Haskell类型族,haskell,type-families,idris,Haskell,Type Families,Idris,在Haskell中,我可能会编写一个带有type声明的typeclass来创建一个类型族,如下所示: class ListLike k where type Elem :: * -> * fromList :: [Elem k] -> k 然后编写如下实例: instance ListLike [a] where type Elem [a] = a fromList = id instance ListLike Bytestring where

在Haskell中,我可能会编写一个带有
type
声明的typeclass来创建一个类型族,如下所示:

class ListLike k where
    type Elem ::  * -> *
    fromList :: [Elem k] -> k
然后编写如下实例:

instance ListLike [a] where
    type Elem [a] = a
    fromList = id

instance ListLike Bytestring where
    type Elem Bytestring = Char
    fromList = pack
我知道您可以在Idris中创建TypeClass和类型级函数,但是这些方法操作的是给定类型的数据,而不是类型本身


我怎样才能在Idris中创建一个像上面那样的typeclass约束类型族呢?

我不知道你是否能找到它的用途,但我认为显而易见的翻译应该是

class ListLike k where
    llElem : Type
    fromList : List llElem -> k

instance ListLike (List a) where
    llElem = a
    fromList = id

instance ListLike (Maybe a) where
  llElem = a
  fromList [] = Nothing
  fromList (a::_) = Just a
使用
我想你不需要这个-记住你有更好的工具:-这样你就可以把
Elem
作为一个function@Carsten我意识到了这一点,但如何将该函数约束到该类型类的实例中呢?我认为您可能根本就没有关联类型族的问题。然而,Idris不提供相关的数据族,我不知道这是否会阻止您做某些事情。它也没有Haskell样式的非关联类型族;类型构造函数上没有任何模式匹配。我可能会问,为什么不是
llElem:type->type
而不是
type
?肯定是不可判定的实例吗?将其加载到idris中并查看类型(
:t
)-在Haskell中,你可以用
数据
而不是
类型
在你的家庭中得到几乎相同的结果-
llElem
listlikk=>type
所以在
listlikk=>listlellem->k
中是有意义的,你在这里真的不需要/想要更多(或者我猜是这样)在Haskell中实现这一点的另一种方法是使用两个参数类型的类,重要的事实是,
k
意味着
llElem
@AJFarmar
llElem
不能单独使用,但是可以通过使用
fromList
来确定合适的实例。Idris目前并没有在这个类的定义中检查这一点(但它应该而且很快就会检查).For me Idris 0.9.18报告给定代码的3个错误
DecEq。idr:1:7:在详细说明Main的类型时。fromList:无法解析类型类ListLike k DecEq。idr:5:10:ListLike不是类型类DecEq。idr:9:10:ListLike不是类型类
λΠ> the (Maybe Int) (fromList [3])
Just 3 : Maybe Int
λΠ> the (List Int) (fromList [3])
[3] : List Int