scala - 未能减少 Scala 中的匹配类型
问题描述
我正在破解 Scala 3 中的匹配类型,并希望定义这样的匹配类型,以便Either[A, B]
将任何匹配类型重新关联为右侧递归,即:
Rebalance[(Int Either String) Either (Boolean Either Long)] =:= (Int Either (String Either (Boolean Either Long)))
我已经组装了以下代码(也是scastie):
type Rebalance[A] = A match {
case Either[Either[a, b], c] => Rebalance[Either[a, Either[b, c]]]
case Either[a, Either[b, c]] => Either[a, Rebalance[Either[b, c]]]
case Either[a, b] => Either[a, b]
}
type RebalancePair[A, B] = (A, B) match {
case (Either[a, b], c) => RebalancePair[a, Either[b, c]]
case (a, Either[b, c]) => Either[a, RebalancePair[b, c]]
case (a, b) => Either[a, b]
}
type RebalanceProxy[A] = A match {
case Either[a, b] => RebalancePair[a, b]
}
type Balanced = Int Either (String Either (Boolean Either Long))
summon[RebalanceProxy[(Int Either String) Either (Boolean Either Long)] =:= Balanced] //this works
summon[Rebalance[(Int Either String) Either (Boolean Either Long)] =:= Balanced] //this doesn't
^^^
Cannot prove that Playground.Rebalance[Either[Int, Either[String, Either[Boolean, Long]]]] =:= Playground.Balanced.
Note: a match type could not be fully reduced:
trying to reduce Playground.Rebalance[Either[Int, Either[String, Either[Boolean, Long]]]]
failed since selector Either[Int, Either[String, Either[Boolean, Long]]]
does not match case Either[Either[a, b], c] => Playground.Rebalance[Either[a, Either[b, c]]]
and cannot be shown to be disjoint from it either.
Therefore, reduction cannot advance to the remaining cases
case Either[a, Either[b, c]] => Either[a, Playground.Rebalance[Either[b, c]]]
case Either[a, b] => Either[a, b]
我很好奇为什么它可以通过RebalanceProxy
&RebalancePair
匹配工作,却以明显相同的匹配类型失败Rebalance
?我想直接通过它来实现它的原因Rebalance
是,为 定义一个依赖类型的函数会非常简单Rebalance
,但不是为RebalancePair
.
解决方案
推荐阅读
- java - 仅存根一个类中的一个私有静态方法
- rest - 资源不能在路径中时 GET 请求的 REST 标准
- javascript - 将 ACF 与 GraphQL 和 Gatsby 一起使用,灵活的内容不会返回其子块的顺序
- html - Bootstrap Carousel li 项目活动类不工作
- c# - 在 Sql 查询中使用参数
- macos - 由于“二进制文件使用的 SDK 早于 10.9 SDK”,电子公证失败。
- php - 避免 3 个 Wordpress 循环中的重复帖子
- javascript - 如何检查 JS 中是否存在(根本)变量?
- python - Yaml 转储程序从 None 值输出空白而不是 null。如何让它输出 null 呢?
- elasticsearch - Send custom build info from Jenkins to Elasticsearch