Scala编译器使用复杂类型参数推断过度特定的类型

Scala编译器使用复杂类型参数推断过度特定的类型,scala,types,functional-programming,higher-kinded-types,Scala,Types,Functional Programming,Higher Kinded Types,我正在写一个FRM,其中字段在F[\u]上抽象 trait Query[A] case class Field[A](name: String) case class DBTable[T[_[_]]](fields: T[Field]) extends Query[T[Field]] // example table case class PersonalInfo[F[_]](name: F[String], age: F[Int]) 我想使用递归方案执行查询重写,所以我需要定义一个模式函子

我正在写一个FRM,其中字段在
F[\u]
上抽象

trait Query[A]
case class Field[A](name: String)
case class DBTable[T[_[_]]](fields: T[Field]) extends Query[T[Field]]

// example table
case class PersonalInfo[F[_]](name: F[String], age: F[Int])
我想使用递归方案执行查询重写,所以我需要定义一个模式函子

trait QueryF[A, B]
case class DBTableF[T[_[_]], B](fields: T[Field]) extends QueryF[T[Field], B]
然后使用
coalgebra
将查询提升到其中:

type Coalgebra[F[_], A] = A => F[A]

def coalg[A]: Coalgebra[QueryF[A, ?], Query[A]] = {
    case DBTable(fields) => DBTableF(fields)
}
这是可行的,但定义一个
代数
以在相反方向进行转换并不能:

type Algebra[F[_], A] = F[A] => A

def alg[A]: Algebra[QueryF[A, ?], Query[A]] = {
  case DBTableF(value) => DBTable[A](value)
}
alg函数抛出一个编译器错误,即类型控制器的构造函数.thing.DBTableF[T,B]不能唯一地实例化为预期的类型控制器.thing.QueryF[?,controllers.thing.Query[?]

这里如何:

import scala.language.higherKinds

trait Query[A]
case class Field[A](name: String)
case class DBTable[T[_[_]]](fields: T[Field]) extends Query[T[Field]]

trait QueryF[A, B]
case class DBTableF[T[_[_]], B](fields: T[Field]) extends QueryF[T[Field], B]

type Coalgebra[F[_], A] = A => F[A]

def coalg[A]: Coalgebra[({ type L[X] = QueryF[A, X]})#L, Query[A]] = {
    case DBTable(fields) => DBTableF(fields)
}

type Algebra[F[_], A] = F[A] => A

def alg[A]: Algebra[({ type L[X] = QueryF[A, X]})#L, Query[A]] = {
  case dbtf: DBTableF[t, b] => DBTable(dbtf.fields)
}
或者,将最后一个
案例
替换为:

  case dbtf: DBTableF[t, b] => DBTable[t](dbtf.fields)
如果这更清楚一点的话

这两个变体都在2.12.6上使用
-Ypartial unification
编译