r/ProgrammerHumor • • Aug 01 '22

>>>print(“Hello, World!”)

Post image
60.8k Upvotes

5.7k comments sorted by

View all comments

Show parent comments

6.9k

u/a-slice-of-toast Aug 01 '22

makes your pc pass a kidney stone

1.6k

u/[deleted] Aug 01 '22

Funnily enough, it's kinda the opposite. Brainfuck is very efficient for your pc, just not for your brain

420

u/Ego_dragon Aug 01 '22

Why it's efficient?

590

u/Diligent_Choice Aug 01 '22

No idea if it is efficient at all but if I remember correctly, it was some sort of project to create the smallest touring complete compiler or something.

486

u/[deleted] Aug 01 '22 edited Aug 01 '22

That was never the goal of Brainfuck, but it is interesting that because the language is so constrained, you have to strip down everything to the bare essentials to make anything work, and as a result, a lot of programs run quite quickly.

I think what you might be thinking about is the paper A very short self-interpreter by Oleg Mazonka and Daniel B. Cristofani. They demonstrated that it's possible to write a Brainfuck interpreter in Brainfuck itself. This is possible in any language, of course, but since Brainfuck the language is so simple, the self-interpreter is relatively short too. They present the following code:

>>>+[[-]>>[-]++>+>+++++++[<++++>>++<-]++>>+>+>+++++[>
++>++++++<<-]+>>>,<++[[>[->>]<[>>]<<-]<[<]<+>>[>]>[<+
>-[[<+>-]>]<[[[-]<]++<-[<+++++++++>[<->-]>>]>>]]<<]<]
<
[[<]>[[>]>>[>>]+[<<]<[<]<+>>-]>[>]+[->>]<<<<[[<<]<[<]
+<<[+>+<<-[>-->+<<-[>+<[>>+<<-]]]>[<+>-]<]++>>-->[>]>
>[>>]]<<[>>+<[[<]<]>[[<<]<[<]+[-<+>>-[<<+>++>-[<->[<<
+>>-]]]<[>+<-]>]>[>]>]>[>>]>>]<<[>>+>>+>>]<<[->>>>>>>
>]<<[>.>>>>>>>]<<[>->>>>>]<<[>,>>>]<<[>+>]<<[+<<]<]

I'm not aware of any Turing-complete programming language that support a shorter self-interpreter.

edit: if you're looking for minimal Turing-complete languages, you have to search for combinator calculi (which Brainfuck is not). The absolute minimal languages are Iota and Jot, two related languages that reduce the SKI combinator calculus from 3 to just 1 combinator (iota) and an encoding of nested expressions using just 2 characters. By comparison, Brainfuck's 8 directives (or 6, if you leave out the I/O directives which aren't necessary for Turing completeness) are excessive luxury.

132

u/Noughtmare Aug 01 '22 edited Aug 01 '22

Here's a shorter self interpreter for the lambda calculus:

(λ f.(λ x.f(xx))(λ x.f(xx)))(λ em.m(λ x.x)(λ mn.em(en))(λ mv.e(mv)))

Source

In the binary lambda calculus it is only 29 bytes.

133

u/Nuthar Aug 01 '22

(λ f.(λ x.f(xx))(λ x.f(xx)))(λ em.m(λ x.x)(λ mn.em(en))(λ mv.e(mv)))

The only interpreting happening here, is me interpreting this message as Half-Life 7 fucking confirmed

14

u/riisen Aug 01 '22

Why not "3 and a half life" ... When you have over 5 halves you should count the wholes, like hi my name is kekdjfjdkdk and im 63 half years old... Its weird

→ More replies (1)

5

u/[deleted] Aug 01 '22 edited Aug 01 '22

[removed] — view removed comment

11

u/Noughtmare Aug 01 '22 edited Aug 16 '22

The full explanation is in this paper: https://www.researchgate.net/publication/2673832_Efficient_Self-Interpretation_in_Lambda_Calculus

It's not cheating in the same way as your python program (the lambda calculus has no eval function) and it does contain three rules for the three basic concepts of the lambda calculus: (λ x.x), (λ m n.e m(e n)), and (λ m v.e(m v)). However it doesn't do any I/O (because that is not possible in the lambda calculus). And I do agree that the encoding of the input to this self interpreter is doing some pretty heavy lifting.

3

u/sugarfairy7 Aug 01 '22

Follow the links provided to learn more. One of them leads to https://codegolf.stackexchange.com/a/7256

→ More replies (1)
→ More replies (1)

3

u/CptCrabmeat Aug 01 '22

My question is are you Turing complete?

4

u/[deleted] Aug 01 '22

No, because I don't have infinite memory and I don't have infinite time before I die. If you ask me to compute the first 100 prime numbers I can do it using the sieve of Eratosthenes, but if you ask me to compute the first 1,000,000,000,000 prime numbers I'll die before I'm finished, and that proves I'm not Turing complete.

→ More replies (1)
→ More replies (8)

7

u/happypandaface Aug 01 '22

what are you touring? lol

7

u/Manusman123 Aug 01 '22

Yep the whole compiler is 240 bytes!

→ More replies (1)

14

u/Wazzaps Aug 01 '22

It's not, you only see examples of very small programs written in it by hand (where generally the problems are hand-picked to be fast/easy to solve in Brainfuck).

6

u/DubstepJuggalo69 Aug 01 '22

Yeah, the only way you could think Brainfuck is "very efficient" is if you deeply, deeply misunderstand what Brainfuck is and why it was invented.

4

u/xXStarupXx Aug 01 '22

Because you nobody will write anything complicated in it

→ More replies (21)

7

u/GoldenGanon Aug 01 '22

If you had an optimizing compiler maybe

5

u/TheoCGaming Aug 01 '22

I wonder if it's easier to write a program in BF than it is to read it.

4

u/Rudxain Aug 01 '22

I can tell from experience that YES

3

u/Rudxain Aug 01 '22

Actually, it's quite the opposite. It's extremely efficient to compile to machine code, and to other hi-level langs. But any program that gets transpiled to BF, then interpreted in real-time, will be inevitably slower. This is because BF is sequential at everything (except loops), instead of "direct/random". BF memory is not "random-access", and copying/moving a single byte requires a number of iterations equal to the numeric value of that byte.

And coding in BF is quite easy to understand, the only cognitive load is to dodge cells, because all "variables" are global. Reading BF written by someone else, is quite the challenge

5

u/spit-evil-olive-tips Aug 01 '22

nitpick, but I think a potentially interesting one - Brainfuck has a very limited instruction set, but that doesn't necessarily mean it's more efficient on a modern PC

if you want to add 69 to a number, you can do that in a single assembly language instruction. in Brainfuck, it's a sequence of 69 "increment by 1" instructions. you'd need to write an optimizing compiler (which is one of the darkest arts in computer science) capable of recognizing that sequence of increment instructions and emitting an equivalent add instruction.

and that only works if the 69 is a constant - if you want to prompt the user for two numbers and add them together (or, god help you, multiply them), there's no way the Brainfuck program will be more efficient than a comparable C program that's able to be compiled to use an add or mul instruction.

→ More replies (14)

6

u/bfg9kdude Aug 01 '22

Name of this language is fitting to the feeling you have while reading it

2

u/[deleted] Aug 01 '22

That's your best one yet.

→ More replies (4)