5 ms·
Just-In-Time Static Type Checking for Dynamic Languages
- raould42 11y agovs. "¿How can a dynamically typed language not actively prevent static checking?" http://lambda-the-ultimate.org/node/5320 http://lambda-the-ultimate.org/node/5320
- eridius 11y agoJust reading the abstract it's not entirely clear what's going on. My impression is that it instruments the app at runtime to gather type signatures for methods, and then.. it saves those type signatures somewhere that can be used to statically type-check the program later? Two things come to mind when considering this approach: 1. How does it handle the type signatures changing as the code is modified? Either it must detect when the code is modified and invalidate its saved signatures for that (although dynamic metaprogramming would make that hard), or you'll just have to live with static type-checking temporarily failing until you've run the program again to let it gather a new type signature. 2. This can only be accurate if you've actually tested all of your code paths, because any code path you don't test may call some method using a type signature that Hummingbird hasn't seen before. Granted, the scripting language community already focuses heavily on 100% code coverage (though 100% code coverage doesn't necessarily mean all the code paths have been tested), but it still seems like an important limitation to be aware of.
- sillysaurus3 11y ago1. When the only way to modify code is to create a new function, this technique would work fine. 2. I think you're right.
- scott_s 11y agoPoint 1 is explained, at a high level, in the introduction: "In Hummingbird, user-provided type annotations actually execute at run-time, adding types to an environment that is maintained during execution. As metaprogramming code creates new methods, it, too, executes type annotations to assign types to dynamically created methods. Then whenever a method m is called, Hummingbird statically type checks m’s body in the current dynamic type environment. More precisely, Hummingbird checks that m calls methods at their types as annotated, and that m itself matches its annotated type. Moreover, Hummingbird caches the type check so that it need not recheck m at the next call unless the dynamic type environment has changed in a way that affects m." Personally, I think they're pushing the bounds of what "statically" means, but hey, they do say it's "just-in-time static". It does indeed do the checking before executing the function, it just happens to be immediately before executing the function. A paragraph later: "To ensure our approach to type checking is correct, we formalize Hummingbird using a core, Ruby-like language in which method creation and method type annotation can occur at arbitrary points during execution. We provide a flowsensitive type checking system and a dynamic semantics that invokes the type system at method entry, caching the resulting typing proof. Portions of the cache may be invalidated as new methods are defined or type annotations are changed. We prove soundness for our type system." Based on how their approach works, I don't think Point 2 stands.
- eridius 11y agoOh ok. So it's not really static at all, they're just calling it that because it sounds better. It's simply dynamic type-checking that checks the types immediately prior to calling the function instead of checking them at the top of the function body. Offhand, it seems that the only benefit to this approach (checking prior to calling the function instead of checking inside the function) is the ability to cache the results and skip checking the same callsite in the future if nothing's changed. Being able to significantly optimize type-checking for scripting languages like this to the point where you can actually run this in production seems quite valuable. But calling it "static type-checking" is simply wrong. Granted, they're calling it "just-in-time static", but that phrase is contradictory, it would be like saying "dynamic static type checking".
- junke 11y agoFine, but there is surprisingly no mention of Lisp in the related work, which defines a COMPILE function in its specification. In a language where the compiler is part in your runtime, type checking is equally available at different execution times. I mean, when I connect to a running instance of SBCL (Common Lisp) to compile my programs, that's exactly what happens: static-type analysis at runtime. Let's say I define a function which builds a function and compiles it: CL-USER> (defun foo (x) (compile () `(lambda (u) (aref ,x u)))) FOO Call it with an array: CL-USER> (foo #(2 3 2)) #<FUNCTION (LAMBDA (U)) {100B3C610B}> NIL NIL The resulting closure allows to access elements of that array: CL-USER> (funcall * 0) 2 Call FOO again, with a bad input: CL-USER> (foo 332) ; in: LAMBDA (U) ; (AREF 332 U) ; ; note: deleting unreachable code ; ; caught WARNING: ; Constant 332 conflicts with its asserted type ARRAY. ; See also: ; The SBCL Manual, Node "Handling of Types" ; ; compilation unit finished ; caught 1 WARNING condition ; printed 1 note #<FUNCTION (LAMBDA (U)) {100B41E16B}> T T COMPILE reports a type error (an exception that I could handle if needed).
- scott_s 11y agoIs it catching the type error before executing the code with the type error? The technique presented by the authors does not wait until there's a type mismatch in an individual expression inside of a function. It catches the type mismatch before executing the function. That's what they mean by "just-in-time static". It happens "just-in-time" because it's during program execution, but it's "static" because it happens before executing the function itself.
- junke 11y agoYes, during compilation. (defun foo (x) (compile () `(lambda (u) (print "Executed") (aref ,x u)))) (foo 0) ... reports a compilation error and of course does not print anything. If however I directly type the offending form (aref 0 0) a runtime type error is signaled.
- IshKebab 11y agoDoesn't sound like it solves my main objections to dynamic typing: 1. No compile-time type checking. 2. No autocomplete / intellisense in IDEs (you can try but it's never very good).
- sillysaurus3 11y ago#2 isn't really true. ETags/GTags provide you with a TAGS file consisting of function definitions. Your intellisense can use those however it wants. Autocompletion and "goto definition" are features of certain emacs packages, for example. (See emacs company-mode for some pretty nice autocompletion on various dynamic languages.)
- veidr 11y agoI think #2 is true. It's never any good. There are a lot of rudimentary "caveman completion" packages available for Ruby coding, but nobody has ever written a good "intellisense" feature for Ruby, where "good" means something on the level of, say, Xcode's "CodeSense" auto-completion for Objective-C or Swift, or JetBrains IDEA's support for Java. I work in Xcode a lot, but I also maintain a couple Rails apps at work. A few times a year, I get so aggravated I spend an afternoon googling the state of the art in code completion for Ruby, and trying out various editors and IDEs. JetBrains RubyMine is consistently the best, but it is far, far inferior to Xcode in this regard. It suggests methods but not the one I am looking for, or it suggests inappropriate methods that would cause an error, etc. It can't always jump to the definition of something, or tell me all the places the method I am currently editing is called from. Xcode has a lot of things I could complain about, but the code completion, finding caller/callees, jump to definition/counterpart... all that stuff is incomparably better than it is for Ruby in any editor I have ever seen. And of course it is — it is incredibly hard and complicated to do a complete or even mostly-complete job of it with Ruby code. There are just too many ways to metaprogram and too little information available to the editor to make it a straightforward task.
- riyadparvez 11y agoMy main objection is always readability. Sure, it is nice when I am writing code and on top of my head I know the type of each variable, but when I am reading code, this becomes nightmare for lack of type information.
- MustardTiger 11y agoThis isn't static type checking though, if it happens at run time then it is by definition dynamic. And these languages already do that. What problem is this solving? I'll already get a runtime error now, how is this runtime error better?
- munificent 11y agoIt's somewhere between the two. Consider this program (in C-ish pseudocode): main() { foo(); } foo() { if (false) { int i = "not an int"; } } bar() { int j = "not an int"; } A statically typed language would report two type errors. A dynamically checked language would report none. Hummingbird would report one. It gives a some of the benefits of static typing: it reports errors in code paths not hit through imperative branching. But it doesn't give all of it since it doesn't report errors in uncalled functions. However, what it gives you in return is the ability to handle functions that don't even exist at program startup and are instead defined at runtime later in the program's execution through metaprogramming.
- MustardTiger 11y ago>Hummingbird would report one. When? >However, what it gives you in return is the ability to handle functions that don't even exist at program startup and are instead defined at runtime later in the program's execution through metaprogramming. Those are already dynamically "type checked", how is this adding anything?
- munificent 11y ago> When? It would report the error in foo() when foo() is invoked, even though the code path containing the error is never executed. > Those are already dynamically "type checked", how is this adding anything? Only code paths that actually get executed are dynamically checked. This checks all code paths in the method.
- MustardTiger 11y ago
- deleted 11y ago[deleted]