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 转换类型级别列表';[a,b,c,…]功能a->;b->;c->;_Haskell_Type Families_Type Level Computation - Fatal编程技术网

Haskell 转换类型级别列表';[a,b,c,…]功能a->;b->;c->;

Haskell 转换类型级别列表';[a,b,c,…]功能a->;b->;c->;,haskell,type-families,type-level-computation,Haskell,Type Families,Type Level Computation,我有一个按类型级别列表索引的数据族,其中列表中的类型对应于数据实例的参数。 我想编写一个函数,根据数据实例的不同,它将具有不同的算术和参数,因此我可以像同义词一样使用它来表示族中的每个数据实例 {-# LANGUAGE KindSignatures, DataKinds, TypeOperators, TypeFamilies, FlexibleInstances, PolyKinds #-} module Issue where type family (-&

我有一个按类型级别列表索引的数据族,其中列表中的类型对应于数据实例的参数。 我想编写一个函数,根据数据实例的不同,它将具有不同的算术和参数,因此我可以像同义词一样使用它来表示族中的每个数据实例

{-# LANGUAGE KindSignatures, DataKinds, TypeOperators, 
             TypeFamilies, FlexibleInstances, PolyKinds #-}

module Issue where


type family (->>) (l :: [*]) (y :: *) :: * where
    '[]       ->> y = y
    (x ': xs) ->> y = x -> (xs ->> y)

class CVal (f :: [*]) where
    data Val f :: *
    construct :: f ->> Val f

instance CVal '[Int, Float, Bool] where
    data Val '[Int, Float, Bool] = Val2 Int Float Bool
    construct = Val2
这很好。但当我尝试应用
构造
函数时:

v :: Val '[Int, Float, Bool]
v = construct 0 0 True
它会产生错误:

Couldn't match expected type `a0
                              -> a1 -> Bool -> Val '[Int, Float, Bool]'
            with actual type `f0 ->> Val f0'
The type variables `f0', `a0', `a1' are ambiguous
The function `construct' is applied to three arguments,
but its type `f0 ->> Val f0' has none
In the expression: construct 0 0 True
In an equation for `v': v = construct 0 0 True

您的代码无法进行类型检查,因为。如果您通过在
f->Val f
中指定
f
选项来帮助GHC,则它将按预期工作:

{-# LANGUAGE KindSignatures, DataKinds, TypeOperators, 
             TypeFamilies, FlexibleInstances, PolyKinds #-}

module Issue where

import Data.Proxy

type family (->>) (l :: [*]) (y :: *) :: * where
    '[]       ->> y = y
    (x ': xs) ->> y = x -> (xs ->> y)

class CVal (f :: [*]) where
    data Val f :: *
    construct :: proxy f -> f ->> Val f

instance CVal '[Int, Float, Bool] where
    data Val '[Int, Float, Bool] = Val2 Int Float Bool deriving Show
    construct _ = Val2

v :: Val '[Int, Float, Bool]
v = construct (Proxy :: Proxy '[Int, Float, Bool]) 0 0 True
关键点是将
Proxy::Proxy'[Int,Float,Bool]
参数传递给
construct
,从而修复
f
的选择。这是因为没有什么可以阻止您使用类型
f1
f2
,从而
f1->Val f1~f2->Val f2


别担心,.

“没有什么能阻止您拥有类型
f1
f2
,这样
f1->Val f1~f2->Val f2
”实际上,有:
(>)
是关闭的,
Val
是一个数据族(不是类型族),因此是内射的。不幸的是,GHC在任何推理过程中都没有利用类型族的紧密性(还没有?)。