Any reason why scala does not explicitly support dependent types?

后端 未结 4 1961
长发绾君心
长发绾君心 2020-11-28 17:54

There are path dependent types and I think it is possible to express almost all the features of such languages as Epigram or Agda in Scala, but I\'m wondering why Scala does

4条回答
  •  隐瞒了意图╮
    2020-11-28 18:06

    I believe that Scala's path-dependent types can only represent Σ-types, but not Π-types. This:

    trait Pi[T] { type U }
    

    is not exactly a Π-type. By definition, Π-type, or dependent product, is a function which result type depends on argument value, representing universal quantifier, i.e. ∀x: A, B(x). In the case above, however, it depends only on type T, but not on some value of this type. Pi trait itself is a Σ-type, an existential quantifier, i.e. ∃x: A, B(x). Object's self-reference in this case is acting as quantified variable. When passed in as implicit parameter, however, it reduces to an ordinary type function, since it is resolved type-wise. Encoding for dependent product in Scala may look like the following:

    trait Sigma[T] {
      val x: T
      type U //can depend on x
    }
    
    // (t: T) => (∃ mapping(x, U), x == t) => (u: U); sadly, refinement won't compile
    def pi[T](t: T)(implicit mapping: Sigma[T] { val x = t }): mapping.U 
    

    The missing piece here is an ability to statically constraint field x to expected value t, effectively forming an equation representing the property of all values inhabiting type T. Together with our Σ-types, used to express the existence of object with given property, the logic is formed, in which our equation is a theorem to be proven.

    On a side note, in real case theorem may be highly nontrivial, up to the point where it cannot be automatically derived from code or solved without significant amount of effort. One can even formulate Riemann Hypothesis this way, only to find the signature impossible to implement without actually proving it, looping forever or throwing an exception.

提交回复
热议问题