4 ms·
> > I cannot imagine a single language without the if operator… Production languages (like prolog or make) don’t need an if statement or operator as selection
by gumby 5y ago
> > I cannot imagine a single language without the if operator…
Production languages (like prolog or make) don’t need an if statement or operator as selection is implicit when a production matches.
- preseinger 5y agoI'm not sure I buy your premise -- `make` is a DSL for a very narrow set of problems, and I've never encountered Prolog in production use?
- jayd16 5y agoShader languages are also hellbent on avoiding branches too so if is frowned upon and often not used. I could easily imagine not having it in a shader language.
- raphlinus 5y agoThis is very much changing. IMHO, doing shader language design today, you should give let the programmer express things in the most natural way possible, and let the compiler figure out whether to generate a branch or branchless code. Yes, often you want the latter, but compilers are pretty good at figuring that out.
- mananaysiempre 5y agoThe old assembly-like languages (ARB_fragment_program, NV_fragment_program*, et al.) did indeed not have branches, only selection and conditional termination, because that was the extent of the capabilities of the underlying hardware. (I understand the execution on modern fragment processors can’t actually diverge within a single batch, either, so they execute both branches and select afterwards, but they are at least capable enough not to do that if the branch went the same way everywhere. But it’s been a long time since I’ve had a state-of-the-art GPU to play with.)
- mratsim 5y agoStill true. Cuda warps work by team of 32 threads and if there is a branch they have to take both and then select the result. It's fine for loop termination ``while (i < 1000)`` but if there is actual work it's often significantly better to switch to branchless code.
- Hirrolot 5y agoNice point, didn't know about that. My fail.
- oldsecondhand 5y agoIn Prolog :- is the if operator.
- gumby 5y agoIts use is kind of a code smell, and I believe it was a relatively (prolog is old) late addition. In any case, I wrote "doesn't need", though perhaps you consider that hair splitting.
- Avshalom 5y agoI think you misinterpreted that. Prolog does has an if operator -> as in ( P -> If ; Else ) yes, but oldsecondhand said :- "predicateA is true" if "clauseB is true" and "clauseC is true". or in prolog predicateA :- clauseB, clauseC. so :- is if, just in it's own, Prolog-y way.
- AnimalMuppet 5y agoIs that an "if", though? In C/C++, you could write that as: if (clauseB && clauseC) predicateA = true; else predicateA = false; which is clearly an "if". Or you could write it as: predicateA = claseB && clauseC; which is not an "if" at all, but just a boolean calculation. (Unless you regard all boolean calculations as "if"s in disguise...) The prolog version seems to me to be more in the spirit of the second C version.
- Avshalom 5y agoWell if nothing has any arguments then yeah, it's basically the second version. but if you had arguments you end up with things like %foo(+A,-B) foo(A,42) :- A < 10. foo(A,24) :- A >= 10. foo(_,100). which would be bool foo(int *A, int *B){ if (*A < 10){ *B = 42; }elseif (*A >= 10){ *B = 24; }else{ *B = 100; } return true; } /* my pointer knowledge is rusty though, so grain of salt */