加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

haskell – 如何使用函数类型依赖来导出类型参数的相等性?

发布时间:2020-12-13 20:44:42 所属栏目:百科 来源:网络整理
导读:请考虑以下代码: {-# LANGUAGE RankNTypes,MultiParamTypeClasses,FunctionalDependencies #-}data St t = St { use_t :: t }class S s t | s - t where -- Nothing reallynewtype P s = P { unP :: forall b t. (S s t) = St t - (St t - b) -- pok - b }f
请考虑以下代码:
{-# LANGUAGE RankNTypes,MultiParamTypeClasses,FunctionalDependencies #-}

data St t = St
    {       use_t :: t
    }

class S s t | s -> t
    where   -- Nothing really

newtype P s = P
    {       unP :: forall b t. (S s t) =>
                    St t
            ->      (St t -> b)     -- pok
            ->      b
    }

f :: (S s t) => t -> P s
f t = P $s pok -> pok s { use_t = t }

代码看起来很人为,但是想法是类S用于表示类型参数t由类型参数s确定,所以我不必将类型参数添加到类型P中.

然而,上述代码简短地给出了以下错误:不能从上下文(S s t)或(S s t1)推导出(t1~t).此错误消息表明编译器想要使用这些上下文中的一个或另一个,而我希望它将使用它们并从它们结束t1~t.

我将不胜感激任何建议,无需添加t作为类型P的类型参数.

你不能用写的类来做.请参阅 Can I magic up type equality from a functional dependency?.但您可以使用其他类来执行此操作:
class t ~ T s => S s t where
  type T s :: *

你需要为每个实例定义T,但至少这并不难.如果有合适的T,您可以提供T的默认定义.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读