刚性变量数组 [英] Arrays with rigid variable
问题描述
好吧,我是做哪些功能我使用了刚性变量的一个问题。我有使用数组对于此问题的想法。所以,我想用阵列,同样的刚性变量作为我创建的功能,但我不知道如何使刚性变量数组。
我想以后的事,但没有效果:
Well, I was doing a problem in which function I was using had a rigid variable. I had an idea of using arrays for that problem. So I thought of using arrays with same rigid variable as of the function I was creating, but I have no idea how to make an array with a rigid variable. I tried the following thing but with no effect:
rearrange :: [Int] -> [a] -> [a]
rearrange l la = elems (f 1 posarr)
where
b = length l
listarr :: Array Int Int
listarr = listArray (1,b) l
arra :: Array Int c
arra = listArray (1,b) la
posarr :: Array Int c
posarr = listArray (1,b) la
f i posarr
| (b < i) = posarr
| otherwise = f (i+1) (posarr // [(listarr!i,arra!i)] )
我得到的错误是刚性的变量。
什么是为可能的解决?请提供如何使刚性变量数组就像一个在下面的功能我用的想法
The error I am getting is of rigid variable. What is the possible fix for that? Please give the idea of how to make arrays with rigid variables like a in the following function I have used
推荐答案
您遇到麻烦,因为你写出来的内部类型签名并不意味着它们看起来像什么比较他们会意味着什么。特别是,你的c 的用途不彼此不
A
在上面签名,他们必须对应。 Haskell是抱怨这些严格定义类型的变量,尽管是变量,不能是相同的,但因为他们是根据什么 A
是刚性的选择......他们必须!
You're having trouble because the "inner" type signatures you've written out don't mean quite what they look like they'd mean. In particular, your uses of c
do not correspond with one another not the a
in the top signature and they must. Haskell is complaining that these "rigidly defined" type variables, despite being variables, cannot be the same, but since they're based on a "rigid" choice of what a
is... they must!
您可以GHC(但不哈斯克尔一般)行为你有一个名为延长喜欢的方式{ - #语言ScopedTypeVariables# - }
在您的code变为
You can make GHC (but not Haskell in general) behave the way you'd like with an extension called {-# LANGUAGE ScopedTypeVariables #-}
where your code becomes
{-# LANGUAGE ScopedTypeVariables #-}
rearrange :: forall a . [Int] -> [a] -> [a]
rearrange l la = elems (f 1 posarr)
where
b = length l
listarr :: Array Int Int
listarr = listArray (1, b) l
arra :: Array Int a
arra = listArray (1,b) la
posarr :: Array Int a
posarr listArray (1,b) la
f i posarr
| (b < i) = posarr
| otherwise = f (i+1) (posarr // [(listarr!i,arra!i)])
请注意,我所做的只是添加明确的 FORALL
和改变了一些 C
变量在
。什么 ScopedTypeVariables
让你这样做是利用引入类型变量的作用域 FORALL
其中code任何类型的签名,这是缩进下面这样一个明确的 FORALL
'D签名可以重新使用在导入型变量名 FORALL
,并让他们对应没错。
Note that all I did was add the explicit forall
and changed some c
variables to a
. What ScopedTypeVariables
let you do is introduce type variable scopes using forall
where any type signatures in code that is indented below such an explicitly forall
'd signature can re-use the type variable names introduced in that forall
and have them correspond exactly.
这可能使下研究如何哈斯克尔间$ P $点类型签名不带扩展名更有意义。特别是,有一个隐含的 FORALL
之前的每个的类型签名
Which might make more sense under examining how Haskell interprets type signatures without the extension. In particular, there is an implicit forall
before every type signature
-- is actually
foo :: [a] -> [a] -> [a] foo :: forall a. [a] -> [a] -> [a]
foo xs ys = it where foo xs ys = it where
it :: [a] it :: forall a. [a]
it = xs ++ ys it = xs ++ ys
这迫使 A
在这些类型签名的变量是不同的,因此code这个片段不能编译,因为它是唯一有效的,如果这两个 A
是相同的。随着 ScopedTypeVariables
我们
Which forces the a
variable in each of these type signatures to be different and thus this fragment of code cannot compile because it is only valid if those two a
s are the same. With ScopedTypeVariables
we have
foo :: forall a . [a] -> [a] -> [a]
foo xs ys = it where
it :: [a]
it = xs ++ ys
其中内签署的 A
的作用范围是指完全相同的 A
在外部签名的。
where the inner signature's a
is scoped to mean exactly the same a
as in outer signature's.
这篇关于刚性变量数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!