17 ms·
Undefined Behavior != Unsafe Programming
- gus_massa 10y agoHN title filter ate the "!". The title must say "!=" instead of "=".
- steveklabnik 10y agoGah! Thanks. I guess I should use the words in the future. I'm used to copy/paste-ing titles, given the HN guidelines.
- Forge36 10y agoAlways the option to use <>. Though I'm not sure how common it is.
- glandium 10y agoThere is also ≠.
- gravypod 10y agoWhere do I buy a keyboard that emmits that?
- catnaroek 10y agoIn Emacs, using a normal keyboard: “C-\ TeX RET \ne”.
- a3n 10y agoYou already have one: ≠ That works in the linux command line, and in this text field on linux: ctrl-shift-u2260enter Of course now you have to find the code. I've always had luck with wikipedia. Also I'm assuming my locale, etc etc works the same for everyone. http://fsymbols.com/keyboard/linux/unicode/ http://fsymbols.com/keyboard/linux/unicode/ Similar methods available for windows and mac, I imagine.
- deathanatos 10y agoIf you're in Linux, and you have a key mapped to Compose, you can hit "Compose = /" or "Compose / =" to get ≠.
- pmarreck 10y agoOn Mac I believe it's just Option-=. Typing the more unusual characters has always been way easier on Macs. (Literally since Macs were invented.)
- wtbob 10y ago> Typing the more unusual characters has always been way easier on Macs. That used to be the case, but I find the Compose key in X is awesome: as others have noted, Compose / = → ≠ (and → is Compose - >)
- pmarreck 10y agoThat Compose system (which I haven't tried yet) does seem like a vast improvement!
- mcpherrinm 10y agoOn Mac, hit control+command+space, type "not equal", and hit down then left, choosing ≠. Certainly longer than typing !=, but far easier than looking up hex values.
- pmalynin 10y agoActually if you just hit "Alt" + "=" on the Mac it will insert the ≠
- MiddleEndian 10y agoAnd on Linux, there is a Mac style keyboard available. I use that plus the compose key for maximum special character support.
- glandium 10y agoWith a standard keyboard, and an OS with support for compose sequences: compose + / + =
- kzrdude 10y agoYou can pay for ~/.Xmodmap maybe.
- myhf 10y agohttp://www.dyalog.com/apl-font-keyboard.htm http://www.dyalog.com/apl-font-keyboard.htm
- amenghra 10y agohttp://alf.nu/Unicode http://alf.nu/Unicode
- pjmlp 10y agoOr when lacking unicode, #
- wyldfire 10y agoWhen I read pound/hash/octothorpe it does not yield "not equal" in my mind. I would use "!=" and if someone else used "#" I would struggle to understand its meaning.
- pjmlp 10y agoIt was quite common in Niklaus Wirth languages, for example: PROCEDURE & Init(id: LONGINT); BEGIN IF id # NofPhilo-1 THEN first := id; second := (id+1) ELSE first := 0; second := NofPhilo-1 END END Init;
- greglindahl 10y ago... you know you can edit your article title at any time ...
- steveklabnik 10y agoI don't have the option to right now. So not literally any time.
- pygy_ 10y agohttps://news.ycombinator.com/edit?id=13648333 https://news.ycombinator.com/edit?id=13648333 maybe? I think that's how the mods access it IIRC.
- steveklabnik 10y agoNope, doesn't have a way of editing.
- gpvos 10y agoHow?
- caf 10y agoPerhaps the title filter should leave alone ! if followed immediately by non-whitespace.
- catnaroek 10y agoDuly upvoted, but I'd like to note that the problem with so-called “unsafe languages” isn't undefined behavior per se, but rather the lack of appropriate tools (e.g. a formal axiomatic semantics) to reliably write programs with no undefined behavior.
- solidsnack9000 10y agoAs long as you use the new semantics, the unsafe parts of the language are barred to you, so it's like a different language.
- catnaroek 10y agoI'm not talking about changing languages, but rather about giving existing languages more rigorous specifications. My beef with C, C++, unsafe Rust, etc. isn't the presence of undefined behavior in arbitrary programs (for which the solution is very simple: don't write arbitrary programs!), but rather the absence of a calculational method [0] for designing programs that don't have undefined behavior (for which a formal semantics is an indispensable prerequisite). To convince yourself that a C program doesn't have undefined behavior, you need to interpret the English text of the C standard, and that's what I object to. [0] The calculations needn't be performed by a machine. If they are performed by a machine, you have a safe language. If they must be performed by humans, you have an unsafe language, which is nevertheless a lot more usable than C and friends.
- Gankro 10y agofwiw this appears to be a goal of people working on speccing out Unsafe Rust stuff. You also don't need to worry about interpreting any english specs today, because there just aren't any specs at all yet!
- catnaroek 10y agoI'd have to interpret the Rustonomicon, which is still written in English. :-p
- Terr_ 10y ago> Undefined behavior is the result of a design decision: the refusal to systematically trap program errors at one particular level of a system. The responsibility for avoiding these errors is delegated to a higher level of abstraction. Sounds a bit like an argument for Checked Exceptions: If you're going to write code for one layer of abstraction, your code should only emit errors that match its own tier. (And not raw naked primeval stuff occurring many layers down.) ( http://imgur.com/iYE5nLA http://imgur.com/iYE5nLA )
- ktRolster 10y agoIt's also not entirely true.....undefined behavior also exists because it's a serious pain to define every detail of a language. The only language I know of that is completely defined (other than toy languages) is ML......and the authors felt the work to completely define it wasn't entirely worth the effort.
- nostrademons 10y agoAnd HTML. Every sequence of bytes is an HTML document with some defined meaning. It may not be valid HTML (the spec does define parse errors), but it will parse to the same representation in all conforming parsers.
- pron 10y agoJava is also completely defined and pretty completely specified. But don't confuse unspecified behavior with undefined behavior. Unspecified behavior means that one of several things can happen, but it's unspecified which. Undefined behavior means that absolutely anything can happen, including things that are not part of normal application behavior.
- Terr_ 10y ago> Unspecified behavior I feel like "unspecified" still sounds too lax. How about "implementation-defined"? I'm thinking of scenarios like: "When this occurs, The Java Virtual Machine implementation may choose to either X or Y. If it does Y, then it shall throw a Z exception."
- johncolanduoni 10y ago> For example, it is obvious that a safe programming language can be compiled to machine code, and it is also obvious that the unsafety of machine code in no way compromises the high-level guarantees made by the language implementation. I agree with the thrust of the statement, but this isn't nearly as simple as is stated. Compiler bugs exist, and the more standards your code has to interact with on the way to machine code (e.g. C spec => LLVM spec => X86 spec) the dicier this becomes, not to mention the assumptions about the APIs exposed to you by your OS and libraries you use.
- kzrdude 10y ago> Either way, UB at the LLVM level is not a problem for, and cannot be detected by, code in the safe subsets of Swift and Rust. But this is not true, there are things that leak through and cause big headaches for the designers of Rust.
- ithkuil 10y agodesigners of Rust or implementors of the rust compiler? (honest question)
- wyldfire 10y agoI think the two sets (designers of the Rust language) and (rustc implementors) intersect really well. I think that the distinction should matter more if/when someone tries another implementation of the language.
- tomp 10y ago> Undefined behavior is the result of a design decision: the refusal to systematically trap program errors at one particular level of a system. Huh? This is quite a bit of a false dichotomy... Illegal operations could also result in unspecified behaviour (e.g. it's not specified what result an integer overflow gives, but the rest of the program must continue normally). > unsafety of machine code In what way is the machine code unsafe? AFAIK the CPU will always try to execute the code, the worst that can happen is some kind of a trap.
- pasta 10y agoI'm not sure I understand you. What is the difference between undefined and unspecified behavior? If it was decided that an overflow would generate an error, than it was a design decision to trap errors at that level. The program could crash so that everybody knows something is wrong. If it was decided that an overflow just would overflow, than it sounds like a refusal to trap the error. The program could continue in an unexpected state. Maybe it's better to crash the program so you know something is wrong.
- tom_mellior 10y ago> I'm not sure I understand you. What is the difference between undefined and unspecified behavior? They explained in the very next sentence: "e.g. it's not specified what result an integer overflow gives, but the rest of the program must continue normally". (In the C standard, "unspecified" is an explicit marker for things where a compiler must document its choice of semantics.)
- lmm 10y ago> The essence of undefined behavior is the freedom to avoid a forced coupling between error checks and unsafe operations. Maybe they should remain coupled - after all, they're intimately related, the error check is what makes the "unsafe" operation reasonable. For a program to remain correct it is vital that the error check remains adequately coupled to the undefined behaviour it's preventing - e.g. if an operation that would do something weird on overflow is being used, the link to our reason for believing that overflow can't happen in this case should be made explicit. It should be possible to do this in a way that has zero overhead in the final machine code (e.g. a richer type system at the LLVM bytecode level).
- dom96 10y agoThis reminds me of a recent conversation I had with someone about Nim: I suggested it to them and they rejected it based on the premise that compiling to C is "bad" because of undefined behaviour. But now I find out that even LLVM IR has undefined behaviour...
- l0b0 10y agoAFAIK Gödel's incompleteness theorems imply that _any_ language will have at least some undefined behaviour.
- tel 10y agoHardly. It'll imply that any language could get stuck or fail to terminate but UB is something different.
- tambourine_man 10y agoThat would be Turing's Halting problem, but yeah, undecidables permeate both concepts.
- jerf 10y agoUndecidable and undefined are completely different concepts. Note Turing's original proof operates on "Turing Machines"; they are completely, mathematically-rigorously defined, but have some questions you can ask about them that are undecidable. Arguably part of the point of the proof is precisely to show that definedness does not imply decideability, a thing which we now take for granted but was still something mathematics as a human endeavor was working through at the time. (If I recall my timelines correctly, this was not the first such demonstration, but the old habits had not yet died and the understanding not yet fully percolated through.) Nothing stops you from creating a computer language that has absolutely no undefined behavior except the general difficulty of fully defining behavior and the practical issues involved with possibly overspecifying your behavior to the point it can't be practically implemented on real hardware. You also face the possibility that your model of the real hardware may also be flawed, if for no other reason than because of CPU bugs, so in practice you may not be able to manifest your fully-specified language 100.00000%. But it's not in principle impossible.
- tel 10y agoThe real heart here is that program analyses have opened doors to optimizations that cannot be represented at the level of the language (LLVM IR). This is super convenient for compiler authors who want a flexible IR for optimization purposes. It's the same thing that happens with C UB too: optimizations will take advantage of what the compiler knows, not what the programmer believes.