Dependent type systems #1126
Replies: 4 comments 26 replies
-
Sorry for the late ( and incomplete ) reply, i am currently in a horrible time crunch. In the previous thread you asked why i mentioned dependent types under meta-programming. They are a kind of type system that allows meta-programming to generate generic programs in the traditional sense. But also programs that are generic across invariants you define with the type system. lets see a classic example #include <iostream>
#include <string>
#include <type_traits>
// Primary template
template<bool B>
struct ExampleFunction;
// Specialization for true
template<>
struct ExampleFunction<true> {
using type = int; // Type A for true
static type getValue() {
return 42;
}
};
// Specialization for false
template<>
struct ExampleFunction<false> {
using type = std::string; // Type B for false
static type getValue() {
return "Hello, world!";
}
};
int main() {
ExampleFunction<true>::type valueTrue = ExampleFunction<true>::getValue();
ExampleFunction<false>::type valueFalse = ExampleFunction<false>::getValue();
std::cout << "Value (true): " << valueTrue << std::endl;
std::cout << "Value (false): " << valueFalse << std::endl;
return 0;
} and in idris2 module Main
%default total
boolDependentFunction : (b : Bool) -> if b then Int else String
boolDependentFunction True = 42
boolDependentFunction False = "Hello, world!"
main : IO ()
main = do
printLn (boolDependentFunction True)
printLn (boolDependentFunction False) As for user defined invariants. In a language like c++ you can define types that depend on types or values ( like std array ). However dependent type systems bridge gaps for you allowing things like a pair of arrays with size unknown at compile time, but of provable equal value. Or functions with an output smaller than the input ( which can be used to prove the totality of functions ) https://github.com/stefan-hoeck/idris2-tutorial/blob/main/src/Tutorial/Dependent.md This not only enables extra security but also supercharges meta-programming |
Beta Was this translation helpful? Give feedback.
-
I've been wondering after reading this: is there something dependent types gets you that isn't available in design by contract? AFAICT, expressing that a value cannot be greater than the input is a post-condition. I don't have experience with languages like idris2, but I've seen dependent types used to constrain valid input which looks a lot like preconditions. |
Beta Was this translation helpful? Give feedback.
-
Hot take: Numbers in types are the "gateway drug" of dependent typing. Once I saw the value of this, I wanted more. For me, Frankly, it was C++ templates that sent me down the rabbit hole of dependent types. It was C++ that put the concept in my head, but for now I'm quite keen on learning Idris. I think anyone who's spent any serious time with modern C++ has started to form some intuitive notion of dependent types. The trouble with pointing to C++ as an example of anything is that C++ is, to put it delicately, a legacy language. So it's not the clearest expression of the idea.
Uh. What? I checked, and assertions throw an exceptions at runtime. C++ template instantiation may be "morally dynamic", but template instantiation still happens at compile time. That's where this discussion of "phases" comes in. For my part, as a developer and not a compiler author, I don't really care whether the error is found by the compiler itself, or a constraint supplied by a library author, so long as it finds it. My interset is more about giving library authors the tools needed to produce good libraries, rather than proving everything reduces to lambda terms. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your participation everyone. I think we failed to set the context for this discussion properly, which resulted in a lot of static, so I am closing it in favor #1200. I hope this helps! |
Beta Was this translation helpful? Give feedback.
-
@GunpowderGuy raised the topic of dependent type systems, suggesting that Idris2 had some innovations that make dependent types easier to use, and that automated proof systems could be easily built upon them. They offered to elaborate if we opened a thread for the topic, so here we are.
Beta Was this translation helpful? Give feedback.
All reactions