What is the difference between path-dependent types and dependent types?

允我心安 提交于 2020-05-12 11:50:13

问题


Scala has path-dependent types, but it is said that Scala doesn’t support dependent typing. What is the difference between path-dependent types and dependent types?

As far as I understand, path-dependent types are one kind of dependent types.


回答1:


A dependent type is a type that depends on a value. A path dependent type is a specific kind of dependent type in which the type depends on a path.

I am not sure if the term "path dependent type" exists outside the Scala community. In any case, the question is, what is a path? For Scala, this is defined in the language specification: basically it's a sequence of selections a.b.c... on non-variable values.

A path dependent type is a type with a path, for example a.T in

class A { type T; def f: T }
def f(a: A): a.T = a.f

There are other kinds of dependent types. For example, in Scala, the is a pending proposal to add literal-based types to the language, so that you could write val x: 42.type = 21 + 21.

In order to type check a program that uses dependent types, the type system (and the compiler) needs to know about the semantics of these values and its operations. The Scala compiler knows the semantics of selection and can decide whether two paths are the same or not. For the example using literal-based types, the compiler needs to be extended to know what the + operation on integer means.



来源:https://stackoverflow.com/questions/24960722/what-is-the-difference-between-path-dependent-types-and-dependent-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!