12 ms·
Parse, don’t type-check
- PaulHoule 6y agoIt's the message you should really take away from meta-programming in LISP is that code is a message that can be read different ways. That's limited though by practical considerations such as the Turing and Gödel results, how your algorithms scale with problem size, etc. There is room for a revolution in parsing tools, but it seems almost every attempt falls short. (PEG in Python is the latest, maybe they will expose a general purpose interface to it someday, but by then certain performance concerns will be so baked in that features that expand use-cases for parsing such as automatic unparsing and round-tripping won't materialize) We are still coding to 1970's style 'callback hell' interfaces from yacc when we should be able to write one grammar and get not just the parser but also a set of semantic structures, for instance if you were parsing Java in Java you'd have objects to represent expressions, operators, etc.
- nsajko 6y ago> PEG in Python is the latest, [...] The popularity of PEG baffles me. I guess it ties in nicely with the wider and older pattern of rejecting good formal language and parsing theory, but PEG seems like an all around bad technology; like, is there even a single benefit to using PEG? The difference between PEG and the usual grammars is that PEG introduces special meaning to the "choice" grammar operation so as to define away ambiguity. But defining away ambiguity actually just "hides" language constructions that will be ambiguous to users. Although PEG notation may be indistinguishable from those used to define CFGs, PEG sucks as a language specification tool as it is unclear what language a PEG defines (unlike with CFGs). From the perspective of implementation, PEG parsers backtrack, meaning either exponential run-time or additional linear memory requirements. Also some Jeffrey Kegler quotes: > With PEG, what you see in the extended BNF is not what you get. PEG parsing has been called "precise", apparently based on the idea that PEG parsing is in a certain sense unambiguous. In this case "precise" is taken as synonymous with "unique". That is, PEG parsing is precise in exactly the same sense that Jimmy Hoffa's body is at a precise location. There is (presumably) exactly one such place, but we are hard put to be any more specific about the matter. and > When you do not know the language your parser is parsing, you of course have the problem that your parser might not parse all the strings in your language. That can be dealt with by fixing the parser to accept the correct input, as you encounter problems. > A second, more serious, problem is often forgotten. Your PEG parser might accept strings that are not in your language. At worst, this creates a security loophole. At best, it leaves with a choice: break compatiblity, or leave the problem unfixed. and > PEG is not unambiguous in any helpful sense of that word. BNF allows you to specify ambiguous grammars, and that feature is tied to its power and flexibility and often useful in itself. PEG will only deliver one of those parses. But without an easy way of knowing which parse, the underlying ambiguity is not addressed -- it is just ignored.
- PaulHoule 6y agoPEG for one thing offers the possibility of composibility. For a language like Python or Java it shouldn't be much harder to add an "unless(X) {Y}" equivalent to "if(!X) {Y}" than it is in LISP if the compiler was structured in such a way that you could take "Java Compiler A" and specialize it to "Java Compiler B" such that one term was added to the grammar and one AST tree transformation that writes the term. Exclusive of POM files, import statements and other packing material that should be less than 50 lines of code if we built compilers to be extensible. The arangodb database has a kind of SELECT statement that returns a list of unordered dictionaries which is frustrating if you want to plot the result in a pandas Dataframe. I wrote something in Python using PEG that would parse an adb query and static analyze it and determine the intended field order when definite. I am up for any compiler technology that makes it easy to do that even if it means I don't get compilation as fast as Go. Another issue that matters in error handling. Drools is a great rules engine on paper, but as it is composed with an external Java compiler you can often get into cases where the error messages don't make sense at all and it isn't like the Jena rules engine which is simple enough that I could get quick results looking at it in the debugger. That's a complicated example but I think language users deserve better error handling and compiler authors need better tools to do that.
- kd5bjo 6y ago> Is there even a single benefit to using PEG? In a formal sense, there are some non-context-free languages that PEG can recognize ( A^n B^n C^n is the classic example ). It’s an open question whether there are any context-free languages PEG isn’t capable of recognizing. Fundamentally, PEG is a formalization of the common practice of writing a recursive-descent parser: It defines languages based on an exemplar algorithm that parses them instead of the generative approach taken by BNF, which defines how you can enumerate the legal strings of the language. They’re both as mathematically rigorous as each other, but approach the problem space from opposite sides. For BNF, ambiguity is a question of whether the generation process is reversible: An ambiguous grammar has multiple paths that produce the same string which means that there’s no way to recover the path used to generate that string. PEG is “unambiguous” because it’s not based on a generative model at all, so this definition is nonsensical. With PEG, we know, by definition, how any given string will parse but it can be hard to enumerate the strings that will parse in a given way. If PEG has an interesting notion of ambiguity (which it probably does), that’s the place to look for it.
- valenterry 6y agoLisp is certainly the most powerful/elegant meta-programming language out there. But there is a nuance here: > code is a message that can be read different ways Think about it. When you read code directly, then you are now dependent on code. And code can change for many reasons, including language changes and mere syntax changes. For that reason, I like the approach of describing the required actions in a certain data format and then reading this data format in a different ways, in opposition to read code directly. Both in the same language of course. This also reliefs you from having to represent generic expressions, operators etc. you only need the subset that is relevant for you, which is most likely much more coarse grained. This is imho the cleaner approach but it comes with the overhead of having to design a "sublanguage" in your language and than interpreting it in your language.
- benschulz 6y agoI'm not sure I buy the premise. The point of the original article was that you shouldn't dig a hole for yourself to get out of later. Compiler writers usually go out of their way, digging extra deep holes, to accept a broad range of invalid input. Not because they like complexity---though, as a group they.. I digress---but because it lets them give more valuable user feedback.
- alipang 6y agoYeah, the main coherent point I take out of this is that it makes sense to provide capabilities rather than to check them, i.e. if you have access to a certain API then you're by construction allowed to use it. It seems this is something the author has been working on. This is then somehow shoehorned into "Parse / Don't typecheck" but it doesn't make much sense honestly.
- red2awn 6y agoYeah I am currently writing a compiler, turns out for useful error messages my parser should accept a superset of my language, then validate it afterwards.
- nsajko 6y agoMaybe you should check out the Menhir parser generator instead. Some links: http://gallium.inria.fr/~fpottier/menhir/manual.pdf http://gallium.inria.fr/~fpottier/menhir/manual.pdf https://semantic-domain.blogspot.com/2015/11/error-handling-in-menhir.html https://semantic-domain.blogspot.com/2015/11/error-handling-... https://baturin.org/blog/declarative-parse-error-reporting-with-menhir/ https://baturin.org/blog/declarative-parse-error-reporting-w...
- mhh__ 6y agoThe way the D compiler does it is by parsing until it finds an error, marking the node as "poisoned" then walking back up until it can keep going. It seems to work although I would include a "fail early" flag just to help with enormous error messages (if you apply the principle to sema as well)
- deleted 6y ago[deleted]
- AstralStorm 6y agoAnd pray tell how do you verify that the parser is actually correct? How strict can you get? Lax parsers are the bane of every web browser's existence. Ultimately parsing does not get you to correctness, ensuring properties does, and a type system is the lamest way to do it. There are actual logic solvers/theorem provers that can handle properties and are as powerful as programming languages while also ensuring correctness and most importantly completeness. Some of them were even available as extensions for Java, though constraint programming is not currently in the mainstream. (Besides a small variant called borrow checking, used in Rust for instance.)
- wvenable 6y agoLax parsers made the modern web possible. New web pages with image tags, forms, and scripts were possible without breaking that page's parsing in older browsers.
- jcelerier 6y ago> Lax parsers made the modern web possible. that's the whole problem indeed
- deleted 6y ago[deleted]
- jacobush 6y agohttps://mcshowblog.files.wordpress.com/2019/08/dumpsterfire.jpg https://mcshowblog.files.wordpress.com/2019/08/dumpsterfire.... Yeah bad example if you wanted to convince me it’s any good :-D
- anoncake 6y agoThat's like saying sloppy programming makes software development possible. At best, it speeds things up (a temporary benefit) at the cost of quality (causing potentially permanent damage).
- 6y ago
- bern4444 6y agoI don’t understand the idea here. Isn’t typechecking a means of making impossible states impossible? The article references ideas like non empty arrays or limiting the input from all numbers to just integers between 1 and 5 inclusive. These are both ideas you can express in a type system (quite trivially in most as well). But instead we should use a function at runtime to parse the input and return an object of this type? Is this article against static type systems, or against runtime type checks? Or both... Personally I love having a type system and typescript (what I work with professionally) allows for making impossible states impossible just like many other type systems
- morelisp 6y ago> Is this article against static type systems, or against runtime type checks? Or both... It's not "against" anything. It's pointing out that "make impossible states unrepresentable" is poorly satisfied by most typed languages themselves. The more of the language you lift into the parser (decidable! easily-analyzable memory bounds!), the less you need to worry about types. As a trivial case consider a language where `5 + ""` would fail typecheck. But why even get so far? Why does your parser accept `lit-term := lit '+' lit` and not `lit-term := [str '+' str] | [ num + num ]`, etc.
- firethief 6y agoBecause the parser can't typecheck "ident '+' ident", so why make it vastly more complicated only to overlap with a later pass that is necessary anyway?
- morelisp 6y agoWhat constitutes "vastly more complicated"? "The type checker can't find all errors, why make the compiler vastly more complicated only to overlap with my unit tests later anyway?" is a pretty common argument. What differentiates your case aside from your personal aesthetics?
- masklinn 6y ago
- rstarast 6y agoI'd say that the type-checker in e.g. Haskell does already do a bit of the "parsing" here, in that it takes code with some type annotations, and returns (on success) that code with fully resolved types. I.e., type variables will be resolved, missing annotations inferred, etc. That said, the idea of moving parts of the type-checking to the parsing stage is novel and interesting.
- nsajko 6y ago> That said, the idea of moving parts of the type-checking to the parsing stage is novel and interesting. It's exactly the opposite of novel, from the very beginnings of parsing theory its goal was to separate lexical, syntactic and semantic analysis. The benefits of the separation come as both better run-time performance and less code size and complexity.
- crazydoggers 6y agoI use a parser to check for a non empty list return. It’s called a unit test. All joking aside, what is the practical difference or benefit here to testing?
- crazydoggers 6y agoNo one genuinely has an answer to this? I get that we’re talking about Haskell here which is compiled. But why wouldn’t we talk about running the code through unit tests. For example the article talks about given function that requires a number that must be between one and five... and then goes on to specify specific grammar rules. How is that better than simply typing that up in a test. And by simply running test suites you eliminate issues with bugs in your parser system. What am I missing?
- ghostwriter 6y agoWhen you have a non-emptiness property on a type, the compiler can guarantee you that all current and future occurences of values of that type in the system will be interacted consistently with the specified non-empty property. Or in other words, the accent is moved from the limited set of units that you are able (and willing) to test to the invariants that will be held for all occurences of the type at all times, regardless of an unlimited number of units that are willing to use them.
- crazydoggers 6y agoHow does one guarantee that for all values? If we’re talking about a parser, wouldn’t it need to test all the values? And if your not testing all the values, aside from simple cases, it’s not something that could just be computed (np, halting problem etc)... so wouldn’t the parser still just need to test some preset number of values? I guess maybe we’re talking kind of like a prebaked in set of unit tests for certain circumstances?
- valenterry 6y agoVery simple. Say you have a user that inputs a list. And then you have another function somewhere in the code, that takes a list, sorts it and uses the max value to send a notification email. If it's done your way, then you'll have to add an assertion in that function, such as if(input_list.length < 1) explode("Bug! Bug! You can't call me with an empty list") And you will have to write multiple unit tests _each time_ you call this function from somewhere, to make sure it's not called with an empty list. If you use a compiler... all of this goes away. The function just expects you to give it a non-empty list and if you don't the code just doesn't compile. Less code to maintain = win. The price you pay for this is learning, understanding and using a more advanced typesystem. That's not a small price to pay. Pick your poison.
- firethief 6y agoThe parts of this that are fully-baked aren't about parsing or type-checking. - the trivial language given doesn't solve anything - dependent-typing is more type-checking, not less - preferring internal iteration over indexing is several degrees removed from type-checking issue - on E: dynamic typing requires type-checking at runtime - DSLs are not at odds with static typing I like this essay. It's a walk through a space of ideas guided by a what-looks-interesting greedy algorithm, along the lines of an "essais" [1]. But I think it's stretching awfully hard to fit the theme it started out with. 1: http://www.paulgraham.com/essay.html http://www.paulgraham.com/essay.html
- nmadden 6y agoThanks. For what it's worth, the flow of thought was the following: 1. The original "parse, don't validate" essay clearly describes why parsers are preferable to validators. 2. It uses a type system to make illegal states unrepresentable in a programming language - but a type system is a validator under its own terminology. 3. What would a parser-like approach to solving the problem look like? I believe that actually adjusting the programming language grammar (and thus the actual parser) is clearly a parser approach to solving the problem. Abstraction is also, to me, a parser-like approach to solving the problem - it transforms code that allows illegal states into code that enforces particular invariants. I probably didn't make those connections explicit enough in the blog. Perhaps the diversion into object-capabilities is a bit off the track, but I found it an interesting connection at least. (And frankly, I'm usually close to the sole audience of my blog so I better make it interesting for me!) Edit: formatting
- contravariant 6y agoI see, it's an interesting line of thought but I think trying to move type checking into the grammar is fundamentally a bad idea. You don't want to mix grammar and semantics because that's not how people think about code. That said I agree that it is interesting to see what the 'parse, don't validate' approach to type checking would be, but I think it should still take place on the type level. And I think this leads you towards interfaces, though perhaps we can improve interfaces by embracing the idea. Taking C# as an example interfaces basically ensure that if the type check succeeds then the function will be given an object that supports the methods of the interface and only the methods of that interface. Even further the methods and fields names of the interface can conflict with the fields and method names of the object but the type checker 'resolves' those conflicts and essentially returns a 'parsed' object with the right methods. Unfortunately this doesn't yet allow for arbitrary conditions (explicitly). Provability is always going to be an issue so let's say we fix that by dynamic type casts. Then I think the behaviour we want is essentially an interface defined by a function IMyInterface parse(MyObject: MyType) where the constructor of IMyInterface enforces some checks and an object implements the interface if it defines an implementation of 'parse'. Now we only need a language to support this. Maybe we can persuade the Julia guys? They seem to like this kind of type-polymorphism based aproach. Edit: Just checked, apparently they have implemented it, it's just called 'convert'.
- alisonkisk 6y agoThe author contradicts the headline in the body. This seems like a poorly written bit of highbrow content marketing for his book.
- morelisp 6y agoRhetorical Flourishes Considered Harmful?
- Cu3PO42 6y agoThe title suggests that type checking should be replaced with parsing, but that's not the article's message. Rather the article recognizes that powerful type systems (more powerful than in many mainstream languages, perhaps) are a great tool to enforce certain invariants. However, those type systems (dependently typed ones, in particular) are also complex and often fickle beasts. The article suggests that it may therefore be worth replacing them with different, less powerful, but easier to work with approaches.
- carapace 6y agoIn re: E and capabilities, see also "macaroons": > This paper introduces macaroons: flexible authorization credentials for Cloud services that support decentralized delegation between principals. Macaroons are based on a construction that uses nested, chained MACs (e.g., HMACs) in a manner that is highly efficient, easy to deploy, and widely applicable. > Although macaroons are bearer credentials, like Web cookies, macaroons embed caveats that attenuate and contextually confine when, where, by who, and for what purpose a target service should authorize requests. https://research.google/pubs/pub41892/ https://research.google/pubs/pub41892/
- vzaliva 6y agoIs it me misreading it or the author trying to re-invent dependent types?
- dgentile 6y agoThe problem is that fundamentally we always have types. We must define different types of bytes and the different things we can do with those bytes. If you can generate a binary from your programming language, it must use a form of static or inferred typing to know what assembly code to generate. A dynamic language just checks at runtime, and adds a bunch of overhead, which isn't always necessary, and is more likely to result in weird "defined" behavior like we get from javascript and php. Typescript is very popular because you can make certain guarantees at compile time, instead of a vague hand-wave of "the code looks good". As an example of the benefits of type-checking, in the Vulkan api, there are a lot of handles. VkRenderPass, VkPipeline, VkSwapchain, VkDevice, etc. All of these are just pointers. If we take a function like: void vkDestroyCommandPool(VkDevice,VkCommandPool,const VkAllocationCallbacks*);` And remove the type checking: void vkDestroyCommandPool(void*,void*,const void*); It is the same API, but without type-checking. Even if the parameters are labelled, with this new API I could mess up and pass the wrong thing.
- wool_gather 6y agoThe article is not even remotely suggesting removing type information. On the contrary, it's talking about some ways to augment a type checker with different grammar or ways of constructing APIs.
- MetaDark 6y agoNo, but you can always encode this augmented information in types to make it explicit, and possibly help the compiler make optimizations. Using a weaker type when you can use a stronger type is just as bad as removing type information.
- masklinn 6y ago> No, but you can always encode this augmented information in types to make it explicit Technically yes, but not all type systems make it practical, or really relevant e.g. you can encode nullability in a type in java… but your reference to your Optional is still itself nullable.
- darkr 6y agohttps://github.com/fthomas/refined https://github.com/fthomas/refined
- cryptica 6y agoReading articles like this makes me want to avoid Haskell. It seems like the language is so over-complicated that it requires developers to invent an entire religion to be able to effectively apply it to real world use cases. From the perspective of a non-Haskell coder who worked with many different mainstream languages, I find that many articles written about Haskell, Erlang and Elixir seem to have no meaning outside of their own narrow domain; they try to present some solutions as some universal insight which is applicable to all languages. In fact, they seem to revolve around solving problems which the language itself created and only exist in that language. I like languages which stay out of my way and don't create any new problems or require inventing unnecessary abstractions and ideology (my OOP philosophy is so simple, I could explain it to a 10 year old). I can't believe that you would need several articles to explain to developers how to do something as trivial as validation. It's not rocket science (...well it shouldn't be). In the communities I participate in, most developers seem to intuitively know how to do validation... There are multiple ways to do it. You just have to think a bit to figure out which way is the best for your project. A lot of these trendy languages seem to be on a futile mission to try to automate away some of the complexity of building software. Building software is irreducibly complex because there are always going to be a huge number of possibilities at every step; this is because the real world (which software tries to model) is complex, always more complex than it seems. That's why I like simple expressive languages which stay out of my way. Any tool which constrains my thinking into specific narrow patterns is going to make my work harder and more error-prone, not easier.
- golemiprague 6y agoWhat do you mean by simple expressive language that stay out of your way? Every language has some concepts and assumptions and abstractions and certain ways to model this complicated world. Can you give some concrete example and explain why?
- reuben364 6y agoReading articles like this make me want to avoid OOP. It seems that like the concept is so over-complicated that it requires developers to invent an entire religion to be able to effectively apply it to real world use cases...( my procedural philosophy is so simple, I could explain it to a 10 year old). There are many ways to build software, but only few ways to build it reliably. One major aspect of reliable software is composability, so that may ensure correct large systems are out of correct smaller systems. Correctness in the small, trivial bits and their glue is what makes correctness at large. You say you just have to think about what's best for your project. Why not think about and understand the problem in general and build tools to tackle the specific complexities you have. Feel free to not use Haskell. Just keep in mind that you too live in a bubble with a certain mixture of doctrine and trade-offs.
- didibus 6y agoUsing parsing and abstractions for safety is the basis of why Clojure, even though dynamically typed, is a relatively safe language[1]. The abstractions are safer in Clojure: no for loops, no while loops, only higher level iteration and transformation constructs, data interfaces are all immutable and functional. Shared state can only be used through higher level managed APIs, numbers auto-promote themselves to higher precision, there's no linear search APIs to force the use of indexed data-structures instead, etc. And for parsing: Spec is a way to define contracts, but it's actually a parser DSL. User defined macros which specify a Spec will have their input parsed by the Spec at read-time, and if the Parser fails, a read error will show up. Unfortunately, this only happens for macro, but the article made me curious about possible value of having this done on functions as well. An example of this is say you have some macro which would let you do: (:from coll :take 5) Your Spec can parse this, and error if the first element isn't :from, the second element isn't a reference or a collection literal, the third element isn't :take, the fourth element isn't a reference or an int, as well as if it isn't called with exactly 4 elements. And all this happens at read time, when the code is parsed. You could go further, and if a collection literal was used, you could assert that the take int isn't bigger then the number of elements in the collection literal. Now, what I've observed is that reference types kind of limit what the parser can do. I'm not sure the article addresses that. Like in my last example, I can validated the range of take over the size of the collection, but not if the collection is a variable. How do I track those references? Maybe somewhere else in the program you can find the collection literal, but not here. P.S.: It's hard to know why Clojure demonstrates relative safety, and I assume here it has a little bit to do with the abstractions it uses and this parser Specifying mechanism. But it's also likely it's all due to the REPL or something else. [1] https://www.i-programmer.info/news/98-languages/11184-which-languages-are-bug-prone.html https://www.i-programmer.info/news/98-languages/11184-which-... - Shows Clojure as having some of the least number of defects per commits
- deleted 6y ago[deleted]
- archgoon 6y agoClojure, for those unfamiliar with the language, unsurprisingly, does in fact allow for both 'for' and 'while' loops, and are part of the core language library. https://clojuredocs.org/clojure.core/while https://clojuredocs.org/clojure.core/while https://clojuredocs.org/clojure.core/for https://clojuredocs.org/clojure.core/for And here are examples picked from a random repository of these constructs being used in the wild: https://github.com/ring-clojure/ring/search?q=while https://github.com/ring-clojure/ring/search?q=while What clojure does do is also provide a number of higher level control constructs, and, by the nature of the syntax of the language, puts them on the same level as 'while' and 'for', which themselves are part of a library, rather than baked into the language itself. This can, of course, be done in other languages, using fluent interfaces. https://en.wikipedia.org/wiki/Fluent_interface https://en.wikipedia.org/wiki/Fluent_interface But is typically much more awkward and harder to compose. Also, you typically have to wrap expressions in lambdas.
- jldugger 6y agoIsn't the first step of writing a type checker to write a parser?
- raiflip 6y agoTypes are just labels that a parse has already been done and doesn't need to be done again. Use them when you don't need to parse again, don't when you do want to parse again.
- gugagore 6y agoI like the principle of "make unauthorized states unrepresentable", but it makes me nervous, or at least evokes a nuance that is well addressed by https://github.com/tree-sitter/tree-sitter https://github.com/tree-sitter/tree-sitter > Robust enough to provide useful results even in the presence of syntax errors Especially when it comes to human-generated input (but even outside of that) it is beneficial to have a representation for unauthorized states, so long as you can easily detect that that's what you're representing. That allows you to better report errors, or try to mitigate them. In other words, don't forget to have an https://wiki.c2.com/?EscapeHatch https://wiki.c2.com/?EscapeHatch
- mumblemumble 6y agoThe dictum is usually phrased as, "make illegal states unrepresentable". Perhaps this is just playing with semantics, but, to me, that difference in wording is incredibly important. An unauthorized state (which I'm interpreting here to mean an error state) may not be on the happy path, but that doesn't necessarily mean it's invalid. For example, "the user is trying to open a file, but they don't have permission to read it" is a valid state. The resulting error condition is also valid, even if it's not somewhere they want to find themself. An illegal state is, in the context of this phrase, meant to be one that's nonsensical. Perhaps "invalid" would be a better word. Think negative array indexes in languages that only let you index from the front of the array, or objects that are only halfway initialized. Situations where, having entered such a state, the behavior of the program is now undefined. We should strive to make it physically impossible get the program into a state where its behavior is undefined.
- ncmncm 6y agoThe author clearly does not understand what type checking is about. In a good strongly-typed language, types don't just prevent errors. They are put to work to perform computation. They choose overloads, and therefore control flow. Different overloads produce different results, therefore data. In the extreme, the type organization of a library is itself a program, one that runs at compile time, and generates the program that will then be compiled and run.
- EugeneOZ 6y agoIt really depends on language.
- WalterBright 6y ago> The essential message is to make illegal states unrepresentable. That's why, in C we have: if (a = b) and: if (a < b < c) that cause (in some compilers) warnings. I wanted to make this an error in D. One option is to check for and disallow them after the parse, but that left: if ((a < b) < c) as still an error. I wanted to allow these constructs to remain if they were parenthesized, as that would imply they were deliberate. (Yes, parenthesized expressions could be a separate grammar, but I thought that was an awful idea.) The solution is to fix the grammar, so they wouldn't even parse. The beauty of this is one doesn't have to enable warnings, or have a separate check for them, it just won't compile. Even better, D's expression grammar is the same as C's, and it did not upset anything else in the grammar as a side effect. Based on my experience at Boeing designing hardware, defining the problem out of existence is a great way to design robust systems.
- andreareina 6y agoWhat's your take on desugaring it to (a < b) && (b < c) ala Python? Speaking hypothetically of course, since it would be a breaking change.
- st1ck 6y agoNote that Python applies same logic to other operators, which can be pretty unintuitive: 1 in [1,2,3] is True evaluates to False.
- vanni 6y ago"pretty unintuitive" is an understatement... In [1]: import dis In [2]: def a(): ...: return (1 in [1,2,3] is True) In [3]: def b(): ..: return ((1 in [1,2,3]) is True) In [4]: a() Out[4]: False In [5]: b() Out[5]: True In [6]: dis.dis(a) 2 0 LOAD_CONST 1 (1) 2 LOAD_CONST 1 (1) 4 LOAD_CONST 2 (2) 6 LOAD_CONST 3 (3) 8 BUILD_LIST 3 10 DUP_TOP 12 ROT_THREE 14 COMPARE_OP 6 (in) 16 JUMP_IF_FALSE_OR_POP 24 18 LOAD_CONST 4 (True) 20 COMPARE_OP 8 (is) 22 RETURN_VALUE >> 24 ROT_TWO 26 POP_TOP 28 RETURN_VALUE In [7]: dis.dis(b) 2 0 LOAD_CONST 1 (1) 2 LOAD_CONST 5 ((1, 2, 3)) 4 COMPARE_OP 6 (in) 6 LOAD_CONST 4 (True) 8 COMPARE_OP 8 (is) 10 RETURN_VALUE Why?!
- lmm 6y agoI would certainly hope that a type "checker" returns a more detailed representation of a program than a parse tree - one in which all expressions are typed and ill-typed expressions cannot be represented. So the premise is fundamentally wrong. Are there non-type ways to improve program safety? Maybe. But since we all understand types and types can cover all the cases we've managed to find so far, I'd stick to types unless and until we can demonstrate a need for something else. Yes, handing out fundamentally unsafe APIs like array-access-by-index is a bad idea - but trying to put types on those APIs is the easiest way to see that they're the wrong abstraction. If you insist that your APIs be type-safe, you'll naturally be guided away from APIs like get-at-index and towards APIs like map / fold / find.
- nmadden 6y ago> I would certainly hope that a type "checker" returns a more detailed representation of a program than a parse tree - one in which all expressions are typed and ill-typed expressions cannot be represented. Do you have an example of this? It is not my experience of compiler intermediate representations or ASTs that they have this property. > But since we all understand types and types can cover all the cases we've managed to find so far, I'd stick to types unless and until we can demonstrate a need for something else. This doesn’t really argue with anything I wrote in the article. (Although I’d argue that nobody strictly “sticks to types” as their sole way to enforce correctness). > trying to put types on those APIs is the easiest way to see that they're the wrong abstraction I’m not sure I’d say it’s the easiest, but it certainly helps. I love Haskell as a language, primarily because it has a lovely set of well-designed abstractions. It’s clear the design of those abstractions were guided by types and benefit from that. Many of them remain good abstractions when translated to untyped languages.
- tome 6y ago> Typed intermediate language. GHC has always used a statically typed intermediate language, one of very few production compilers to do so. (Maybe even the only one.) Let's keep that property. Richard's thesis makes it clear that doing so is not straightforward. -- Simon PJ (https://github.com/ghc-proposals/ghc-proposals/pull/378#issuecomment-728113583 https://github.com/ghc-proposals/ghc-proposals/pull/378#issu...) So it looks like it's rare to even have a typed intermediate language, let alone one whose structure forces it to be well typed. The latter would require some sort of dependent types.
- jayd16 6y agoIsn't this just saying the exact same thing as the original article but with pedantic word games? A type checker includes a parser, a lexer, a syntax tree builder and the type checking itself is the specific grammar the author is looking for. Am I missing something?