gpt4 book ai didi

haskell - Reify 存在实例类型参数

转载 作者:行者123 更新时间:2023-12-02 02:26:59 25 4
gpt4 key购买 nike

我有一些这样的代码:

{-# LANGUAGE AllowAmbiguousTypes #-}
module Foo where

import Data.Proxy

class Foo x y
class Bar x y
class Baz x y
where
baz :: Proxy x -> Proxy y -> ()

instance (Foo a v, Bar b v) => Baz a b
where
baz _ _ = ()

instance Foo String String
instance Bar Int String
现在我真的想用那个 Baz例如,所以我写:
test :: Proxy String -> Proxy Int -> ()
test = baz
但是当然有一个模棱两可的“存在” v我尚未修复的类型参数 String (并且没有基金),所以我得到:
[typecheck] [E] /tmp/foo/src/Main.hs:20:8: error:
• Ambiguous type variable ‘v1’ arising from a use of ‘baz’
prevents the constraint ‘(Foo [Char] v1)’ from being solved.
Probable fix: use a type annotation to specify what ‘k1’,
‘v1’ should be.
These potential instance exist:
one instance involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the expression: baz
In an equation for ‘test’: test = baz
但是我怎样才能真正修复那个类型变量呢?我看不到使用可见类型应用程序修复它的方法,因为例如以下内容不起作用:
test2 :: Proxy String -> Proxy Int -> ()
test2 = baz @String @Int @String -- is there some variation of this that would work?
我也看不到使用显式类型注释来修复该类型参数的方法。我是否编写了一个无法实际使用的实例?

最佳答案

确实不可能使用那个实例。当您调用 baz ,可以提供ab ,但不是 v . v必须由父类(super class)和实例约束的某种组合来确定,但事实并非如此。
你应该能够修补这个不同的地方。尝试任一

instance s ~ String => Foo String s
或者
instance s ~ String => Bar Int s
例如。

关于haskell - Reify 存在实例类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62747718/

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