Name: Anonymous 2018-10-12 17:08
So, why are you not using dependent types yet? Do you like your programs randomly crashing?
and a crash is what I need to determine something has gone wrong.Dependent types help you find logical errors in your program, such as divisions by 0 and possible out of bounds accesses. Why would you need to wait until runtime in order to find a bug in your program? There are many released and supposedly stable programs in production that have passed testing and still have all kinds of bugs (including security bugs) that could have been prevented by dependent types.
List 50 Int
for the type of a list with 50 elements. A functions like head that take the 1st element of a list could have a type like {t : Type} -> {length : t} -> (value : t) -> List (1 + length) t -> t
head (cons 9 nil) = 9
head nil = compile time type error as the type of nil is something like List 0 Int
int sussman[10]; sussman[90] = 100;
so it helps with array index out of bounds stuffNot only, you can even use it to avoid divisions by 0, as well as implement type-safe printfs. All kinds of things that have to do with safety and correctness really.
how is this different from just exception handling where you make sure something isn't out of bounds?That this checks if your program is correct at compile time. You get an exception at runtime - possibly crashing your program.
You're still going to have a conditional branch in the machine code.Yes, did anyone tell you otherwise?
The idea is basically that types may depend on values, so you can have something like List 50 Int for the type of a list with 50 elements.Then C got dependent types:
That way you basically can't do things like int sussman[10]; sussman[90] = 100;
typedef int dependent_type[50];
dependent_type A;
#include <stdio.h>
int main(){A[90]=100;printf("%d",A[90]);;}
int main(){A[90]=100;printf("%d",A[90]);;}Now try
int main(){int i; scanf("%d\n", &i); A[i] = 100; printf("%d\n", i);}
printf("%d\n", A[i]);
Type safe division: https://gist.github.com/edwinb/0047a2aff46a0f49c881I can just
catch ZeroDivisionError
in other langs to achieve the same exact thing. the same exact thing"Data loss" and "crashing on runtime" is not the exact same effect as "catching a possible error at compile time".
print "Attempt to divide by zero!"
, which is literally no different than catching the exception and doing that.instead of crashing, in Idris you have, print "Attempt to divide by zero!"You have to use
choose
only on user input. For non-user input you might not need to do that (so you will not need to have any print "Attempt to divide by zero!"
).which is literally no different than catching the exception and doing thatExcept that you might forget to catch said exception. "b-buuut the compiler will warn me!" - what if you pass a provably non-zero divisor? You will have to still catch the exception, which is retarded.
sizeof array / sizeof *array
instead - which does not work correctly for pointers), along with no warning if one is incorrect or missing. for freeThats a lie. Garbage collection, performance, long compile times and complexity of implementation.
Garbage collectionNot more necessary than any language.
performanceType checking happens only at compile time, in fact it has the opportunity of being faster since you do not need to check if your arguments are null everywhere.
long compile timesFaster than C++. And it's not like compile times really matter, you are not going to recompile the whole project every time you make a change - only the file(s) that you changed.
complexity of implementationI have implemented a dependent type system in 100 lines of code. Pretty sure that this is less than even the type system of the average C implementation or haskell.
I have implemented a dependent type system in 100 lines of code.Extraordinary claims require extraordinary evidence.
shows paper clearly lacking code and clearly not written by him"I have implemented a dependent type system in 100 lines of code."
where it is"Its...in the paper..somewhere.."
Vect n Int
while the head function requires a value of type Vect (n + 1) Int
C++ version with setters/getters on protected boxed value would work fine at runtimeThe issue is that the compiler will not warn you if you forget or mis-implement said getters/setters. See >>24
while the head function requires a value of type Vect (n + 1) IntIn that case dependent types are safer.
Would that also mean scanf equivalent function needs to implement handling dependent types as output?No
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
template<typename T,size_t size> class contain {private: T arr[size];
public:
T operator [](size_t i) const{
if(!(i<size)){puts("Requested Index out of range. Aborting");exit(2);}
return arr[i];};
T& operator[](size_t i) {
if(!(i<size)){puts("Input Index out of range. Aborting");exit(1);}
return arr[i];};};
int main(){
contain<int,50> A;
int i;
int rts=scanf("%d", &i);
A[i] = 100;
printf("%d", A[i]); return rts;}
Edited on 15/10/2018 04:53.
if it was so simple, we'd have done it by nowBut we have.
why would corporations who stand to lose a lot by getting hacked avoid something that would make them hack-proof?Because it is cheaper to hire Indians for pennies who have no idea about dependent types than hire people with some CS background. Why do you think that crap like Windows and Java are so popular?
B-b-but you can write code that output types!But I can also directly write that code without types as a middleman.
There’s no way you can encode all the logic to sanitize input in just types.What makes you think that? Haven't you heard of Curry–Howard isomorphism?
But I can also directly write that code without types as a middleman.Code that you can't prove that is correct, sure. This is why most programs nowadays are unstable and full of vulnerabilities.
https://www.exploit-db.com/All I see are bugs in software written in C and Java, neither of which have dependent types.
https://www.cvedetails.com/
which is not the caseWhat makes you think that in this specific case?
privilege escalation?Yes, dependent types protect against these.
overflows? numeric underflows?
null pointer dereferencing? uninitialized/improperly initialized memory?
format string vulnerabilities? array indexing errors? mismatched array new/delete? stack overrun? unused values?
remote code execution?
iterator invalidation of mutable data structures leading to undefined behavior?
unhandled return codes?
return pointers to local variables? invalid use of negative values?
underallocations of dynamic data?
file handle leaks?Pretty sure that you need linear types for these.
network resource leaks?
race conditions?
So, why are you not using dependent types yet? Do you like your programs randomly crashing?What if NPCs are incapable of writing safe code by design? They lack the level of abstract thought required to imagine the code being exploited. They just write code as requested by design and wonder why it randomly crashes and allows exploits.
Please post about it.It was mostly vidya logic, so not very /prog/ relevant. Shit like not realizing that bumping numbers without touching the underlying mechanics will just lead to more tedious kiting.
I did something therefore it's fixed, thread over, issue closed, working as intendedthat is impossible to work with. With those people, you have to convince them emotionally, by licking their asses and trying to sneak your ideas as theirs.
buffer overflows and injectionsCan be prevented by dependent types.
but also logic bugsCan also be prevented by dependent types.
and race conditionsYou probably need linear types for that.
although it's harder for injections as they usually happen on the boundaries of languagesYou could have a dependent type like
system : (s : string) -> doesNotContainUnescapedQuotes s = True -> IO ()
logic bugs only theoretically canBuffer overflows are also logic bugs.
can't be done in general case (because of halting problem).Nice meme
by exploiting OS-level stuffDependent types can't prevent bugs in other programs.
doesNotContainUnescapedQuotesthat might be the only way to prevent shell injection but it's an anus-backwards approach when it comes to SQLi. the correct solution is not treating SQL queries as strings. see: parametrized queries.
Buffer overflows are also logic bugs.buffer overflows aren't really what security people would call logic bugs. this term refers to unintended consequences of a program's high-level design, not low-level details about programming language, calling convention and the processor architecture.
Nice memedependent types solve halting problem?
Dependent types can't prevent bugs in other programs.how would they prevent those bugs if an OS was written in a dependently typed language? that's a clear example of what I mean by 'logic bugs'
dependent types solve halting problem?Languages with dependent types tend to not be Turing complete. Moreover the halting problem is irrelevant for just about anything.
how would they prevent those bugs if an OS was written in a dependently typed language?Formal verification
Languages with dependent types tend to not be Turing complete.lets see these guys implement
Formal verificationwhich is non-trivial, time-intensive and doesn't even require dependent types. you can look at any bug and say '100k additional man-hours by guys with PhDs would have prevent it', but that's not saying much.