gpt4 book ai didi

haskell - 幻影和代理 - 导致 ghoSTLy 错误

转载 作者:行者123 更新时间:2023-12-02 16:27:33 24 4
gpt4 key购买 nike

我正在开发一个项目,在该项目中,我通过幻像类型跟踪我使用的数据库模式 - 它有一个 KnownSymbol - 这是模式名称。

就在前几天,我遇到了以下问题 - 我不明白:

为什么不可能定义 withoutProxy 或重新表述它 - 为什么 GHC 假设 test* 类型而不是 (Proxy::Proxy test) 中的 >Symbol,尽管类型签名另有说明并且启用了 ScopedTypeVariables

{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE KindSignatures #-}
module T where

import GHC.TypeLits
import Data.Proxy

newtype Phantom (x :: Symbol) y = Phantom y

withProxy :: (KnownSymbol test) => Proxy test -> Phantom test ()
withProxy _ = Phantom ()

withoutProxy :: (KnownSymbol test) => Phantom test ()
withoutProxy = withProxy (Proxy :: Proxy test)

我得到的错误是最令人困惑的

> ghci test.hs
GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling T ( test.hs, interpreted )

test.hs:14:27: error:
• Couldn't match type ‘*’ with ‘Symbol’
Expected type: Proxy test
Actual type: Proxy test
Use -fprint-explicit-kinds to see the kind arguments
• In the first argument of ‘withProxy’, namely
‘(Proxy :: Proxy test)’
In the expression: withProxy (Proxy :: Proxy test)
In an equation for ‘withoutProxy’:
withoutProxy = withProxy (Proxy :: Proxy test)
Failed, modules loaded: none.

然后启用-fprint-explicit-kinds

Prelude> :set -fprint-explicit-kinds 
Prelude> :r
[1 of 1] Compiling T ( test.hs, interpreted )

test.hs:15:27: error:
• Couldn't match type ‘*’ with ‘Symbol’
Expected type: Proxy Symbol test
Actual type: Proxy * test
• In the first argument of ‘withProxy’, namely
‘(Proxy :: Proxy test)’
In the expression: withProxy (Proxy :: Proxy test)
In an equation for ‘withoutProxy’:
withoutProxy = withProxy (Proxy :: Proxy test)
Failed, modules loaded: none.

最佳答案

简短回答:添加forall

withoutProxy :: forall test. (KnownSymbol test) => Phantom test ()
withoutProxy = withProxy (Proxy :: Proxy test)

没有它,第二行的test与上面的不同。


或者,根本不添加类型注释:

withoutProxy :: (KnownSymbol test) => Phantom test ()
withoutProxy = withProxy Proxy

或者您可以添加{-# LANGUAGE PolyKinds #-},然后事情也会统一,因为Proxy::Proxy testforall k (测试::k)。代理 k 测试,其中 k 是一种类型。

关于haskell - 幻影和代理 - 导致 ghoSTLy 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44306334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com