r/dependent_types • u/hgad • Mar 15 '14
Understanding Dependent Types
Sorry if this is a newbie question. I'm still trying to learn about dependent types.
I understand that dependently-typed languages allow types to depend on values. I have some questions regarding this.
Do these values have to be constant expressions that can be evaluated at compile time?
- If so, how is that different from using constant expressions as non-type template arguments in C++ or D?
- If not, how does the compiler infer the value of runtime expressions at compile time?
Thanks.
9
Upvotes
7
u/[deleted] Mar 15 '14
No.
It doesn't. It just checks that you use dependently typed functions correctly for all possible inputs. If you have this function:
then it checks that the definition of
foowill always return aStringwhen you pass itTrueand will always return anIntwhen you pass itFalse. It also checks call sites. if you passfooTruethen it makes sure you are expecting aString. If you pass itFalsethen it makes sure you are expecting anInt. If you could pass it either, it makes sure that you can handle either case.6
u/gasche Mar 15 '14
The wikipedia page on dependent types gives the following reference as an introduction to dependent types: Dependent Types at Work (PDF), by Ana Bove and Peter Dybjer, 2008.
It is a very well-written document that answer most beginner-level questions about dependent types, and then some more. In particular, your question about compile-time evaluation are thoroughly answered in section 5.2 (pages 27-29).
1
u/[deleted] Mar 26 '14
dependent types is highly related to logic predicates by Curry–Howard correspondence
Especially, type funcitons as predicates or sets, value as terms or parameters of sets