159 points · by jstrieb · 5y ago · 76 comments
Even though this sounds like formal verification gobbledygook, there is some information of practical value there.
How many JSON validators have you seen in python and javascript? Why can't the static type checkers like mypy and pyright do this for us?
The answer is that the type systems of these languages are not sufficiently developed. They can tell you if you're assigning a string to an int, but not if you're assigning 200 to an int whose type allows only numbers in [10, 100].
So one creates a new class with a validate() method, which is completely unnecessary and doesn't protect you against integer overflows elsewhere in the code.
F* and OCaml derivatives are our best hope in developing a type system which perform these types of common checks in one integrated framework via refinement types and dependent types.
Hopefully, one day that work can hit mainstream languages like python, javascript and whatever ends up being the system programming language of choice replacing C in the coming years.
I am pretty sure Ada did that in the 80s. :)
I get what you are saying, but the example you gave is a pretty trivial one that has been solved (although not implemented very often...) for awhile in more "regular" languages!
Recent languages like swift/kotlin (which I expect will see increased usage in coming years) seem to have ignored this area.
https://github.com/peter-tomaselli/swift-refined/blob/master... https://discuss.kotlinlang.org/t/refinement-types/9753/19
I'd love to be able to opt in to bounds checking when I feel it is needed.
I have no bloody idea why language designers seem so intent on not letting me choose what perf/safety trade-off is worthwhile for me. :/
F* implements refinement and dependent types, which allow true logic to be computed at _compile_ time.
When TS 4.1 hit beta, I did a proof of concept validating that a `number` was a non-negative integer, and it works[1]. Many similar or even substantially more complex constraints are possible (I’m exploring type level JSON Schema validation of example values, and I’m not quite there but do I believe it’s possible).
I’m sure this kind of thing is child’s play in ML family languages, but it’s really cool to get this level of compile time certainty in a dynamic language.
[1]: my proof of concept was using literals without accounting for exponential representations etc, but I have no reason to doubt these are also achievable based on the ways I’ve seen 4.1+ used.
Typescript cannot do what you’re saying at compile time. F* knows _at compile time_ whether or not a value is within a range, and you can get a compiler error _without executing the program_.
Because JSON is external data, mypy and pyright operate on Python code.
> They can tell you if you're assigning a string to an int, but not if you're assigning 200 to an int whose type allows only numbers in [10, 100].
Mypy can, in fact, do that (via a union of literal types), though the UX is horrible for that specific case and it only handles simple literals, but that's actually enough for JSON semantics. Of course, to validate JSON with it, you'd have to turn the JSON into Python code with type annotations and validate that.
https://www.python.org/dev/peps/pep-0589/
movie: Movie = {'name': 'Blade Runner',
'year': 1982}
It's all possible in theory, but the support in widely used static type checkers is very basic or has bad UX.On the other hand, this is exactly what you need to write secure code.
function foo(n) {
if(n < 10 || n > 100) throw new Error("Expected n=" + n + " to be between 10 and 100");
return n*2;
}
And while you are at it you can write an unit test that is tested at compile time or in the editor/IDE. // assert(foo(10),20);Typically such languages are more academic than F*.
Javascript is dynamically typed, and so has no type checking until runtime, you don't know whether a passed in parameter is a string or not until you try to use it.
C# is statically typed, so the compiler knows whether something is a string when passed in at compile time, not making you wait until runtime.
Ocaml/F# have a stronger type system than c#, so you get the compiler looking for you not only that something is a string, but that it is a named type like fifteenString, which is a string with a length of always 15 characters.
F* takes this even further, where the compiler checks for you that you've passed in a fifteenString and that it matches criteria in its value like "starts with hello" or "does not contain bye".
C# saves you from needing to check if a param is even a astring at runtime.
F# additionally saves you from needing to check if it is null and 15 characters in length at runtime.
F* additionally saves you needing to check its value starts with hello at runtime.
Again this is a single language feature probably poorly explained!
[1]: https://fsharpforfunandprofit.com/posts/designing-with-types...
[1] A Security Model and Fully Verified Implementation for the IETF QUIC Record Layer (Antoine Delignat-Lavaud, Cédric Fournet, Bryan Parno, Jonathan Protzenko, Tahina Ramananandro, Jay Bosamiya, Joseph Lallemand, Itsaka Rakotonirina, Yi Zhou) To Appear In The Proceedings of the 42nd IEEE Symposium on Security & Privacy 2021.
it required several person-months to implement and verify a generic doubly-linked list library in F*, while it required only three hours to do so in Dafny
This could be deceptive. Structures like linked-lists are fiendishly hard to implement in safe Rust too, but people are able to stay productive by (mostly) not implementing things like that themselves. In Rust's case the difficulty comes from ownership: anything involving complex reference graphs for the sake of traversal is going to be really gnarly to establish a single-ownership story for. I don't know about F*.> F* (pronounced F star) is a general-purpose functional programming language with effects aimed at program verification.
> The main ongoing use case of F* is building a verified, drop-in replacement for the whole HTTPS stack in Project Everest [2]. This includes verified implementations of TLS 1.2 and 1.3 and of the underlying cryptographic primitives.
I suggest you to read (or rather solve) this masterpiece
Just joking: will the version 3 called f***?
I like the language btw.Syntax is what I look at first with any unfamiliar programming language. The syntax is the UI of a language and if that's terrible, I won't enjoy using it.
I don’t, but then I've spent time with a variety of languages that, like F*, are from the ML syntax family, and find that syntax quite comfortable.
I can see why it would seem that way at first impression to someone who, for instance, has almost exclusively used languages with Algol-derived syntax (which is pretty common these days, since outside of Haskell from the ML family and Clojure from the Lisp family, most currently popular languages have Algol-derived syntax, or at least superficially Algol-like syntax.)
> Syntax is what I look at first with any unfamiliar programming language.
It's one of the things I look at early on, too, but it's also something I try to not to judge to firmly early on, because subjective view of syntax, like other UI impressions, is highly dependent on familiarity, and it takes a while to get past superficial similarity and dissimilarity to what one is used to and into any deeper strengths and weaknesses of the syntax (or other UI).
I really like the philosophy behind Haskell, for example, but I really think it's syntax let's it down - it encourages very snippet-y programming which in turn means structure can be require parsing rather than glancing at and also variables name that are very short.
I believe that other than being ML-like language and being developed jointly by Microsoft Research and Inria, F* has nothing to do with F# and .NET.
[1]- https://en.wikipedia.org/wiki/F*_(programming_language)
> F* provides a facility to specify interfaces to external modules that are implemented elsewhere. For example, operations that perform file input/output are implemented by the operating system and made available to F* programs via the underlying framework, e.g., .NET or OCaml.
Isn't it yet another copy of OCAML? I think it shouldn't say familiar but something different. I really don't underestimate any effort to make OCAML available on dot-net, but referring to excellent OCAML books can't be explained by simple 'familiarity'...
No, closer to ATS maybe?