首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用‘`Proxy’绑定类型与使用forall绑定类型

使用‘`Proxy’绑定类型与使用forall绑定类型
EN

Stack Overflow用户
提问于 2018-12-14 03:52:21
回答 1查看 63关注 0票数 2

在下面的例子中,我不清楚为什么toto会失败,而tata可以工作。

对此有什么解释吗?

代码语言:javascript
复制
{-# LANGUAGE AllowAmbiguousTypes  #-}
{-# LANGUAGE  TypeFamilies, KindSignatures, FlexibleContexts #-}
{-# LANGUAGE TypeApplications, FunctionalDependencies, MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables , GADTs, MultiParamTypeClasses, RankNTypes                #-}
{-# LANGUAGE TypeOperators, UnicodeSyntax, DataKinds              #-}

import Data.Proxy

data KNat where
class ReflectNum (s :: KNat) where

toto ∷ (∀ (s :: KNat). ReflectNum s ⇒ Int) → Int
toto k = toto k

tata ∷ (∀ (s :: KNat). ReflectNum s ⇒ Proxy s -> Int) → Int
tata k = tata (\(p :: Proxy s) -> k p)

错误是

代码语言:javascript
复制
SO.hs:14:15: error:
    • Could not deduce (ReflectNum s0) arising from a use of ‘k’
      from the context: ReflectNum s
        bound by a type expected by the context:
                   forall (s :: KNat). ReflectNum s => Int
        at SO.hs:14:10-15
      The type variable ‘s0’ is ambiguous
    • In the first argument of ‘toto’, namely ‘k’
      In the expression: toto k
      In an equation for ‘toto’: toto k = toto k
   |
14 | toto k = toto k
   |               ^
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-14 04:39:17

这是GHC实现可见类型应用程序的一个已知限制。具体地说,为了允许更高级别函数(如toto函数)的参数访问类型变量,有时仍然需要使用Proxy

有一个GHC提议以type variable bindings in lambda-expressions的形式为这个问题添加一个解决方案。使用提案中的语法,您的toto函数可以编写为

代码语言:javascript
复制
toto k = toto (\@s -> k @s)

在本地绑定s变量。可悲的是,这项提议还没有实施。

同时,对于像这样的高阶函数,我认为你只需要使用Proxy。抱歉的。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53769147

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档